Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
feat(triggers): bernard (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-rots authored Aug 8, 2020
1 parent e63749f commit 22df8c5
Show file tree
Hide file tree
Showing 25 changed files with 1,424 additions and 211 deletions.
18 changes: 14 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ on:
tags:
- 'v*'
pull_request:
types:
- opened
- reopened
- edited
types: [opened, reopened, edited, ready_for_review]

jobs:
build:
Expand Down Expand Up @@ -105,3 +102,16 @@ jobs:
tags: master
tag_with_sha: true
always_pull: true

# docker build (branch)
- name: docker - build other
if: startsWith(github.ref, 'refs/heads/master') == false
uses: docker/build-push-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
repository: cloudb0x/autoscan
dockerfile: docker/Dockerfile
tag_with_ref: true
tag_with_sha: false
always_pull: true
57 changes: 57 additions & 0 deletions .github/workflows/cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Docker Cleanup

on:
pull_request:
types: [closed, removed]
delete:

jobs:
cleanup_branch:
if: startsWith(github.event.ref_type, 'branch') == true
runs-on: ubuntu-latest
steps:
- name: Determine docker tag
env:
GH_EVENT_REF: ${{ github.event.ref }}
id: dockertag
shell: bash
run: echo "::set-output name=tag::${GH_EVENT_REF#refs/heads/}"

- name: Remove branch docker tag
shell: bash
env:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
tag: ${{ steps.dockertag.outputs.tag }}
run: |
docker run --rm lumir/remove-dockerhub-tag --user "$username" --password "$password" cloudb0x/autoscan:$tag
cleanup_pr:
if: startsWith(github.event.ref, 'refs/pull/') == true
env:
GH_EVENT_REF: ${{ github.event.ref }}
runs-on: ubuntu-latest
steps:
- name: Determine pull request tag
id: githubpr
shell: bash
run: echo "::set-output name=tag::${GH_EVENT_REF#refs/pull/}"

- name: Determine docker tag
uses: frabert/replace-string-action@master
id: dockertag
with:
pattern: '[:\.]+'
string: "${{ steps.githubpr.outputs.tag }}"
replace-with: '-'
flags: 'g'

- name: Remove pull request docker tag
shell: bash
env:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
tag: ${{ steps.dockertag.outputs.replaced }}
run: |
docker run --rm lumir/remove-dockerhub-tag --user "$username" --password "$password" cloudb0x/autoscan:pr-$tag
82 changes: 55 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ That's where rewrite rules come into play. They allow you to translate paths bet
**Before you begin, make sure you understand how regular expressions work!** \
Make sure you know how capture groups work, as these are used for the `to` field.

Triggers can receive paths from any source: A remote server, a Docker container and the local file system. The `rewrite` field can be defined for each individual trigger. The `from` should be a regexp pattern describing the path from the trigger's perspective. The `to` should then convert this path into a path which is local to Autoscan.
Triggers can receive paths from any source: A remote server, a Docker container and the local file system. The `rewrite` field can be defined for each individual trigger. This field can contain multiple rewriting rules. Therefore, each rule should have a `-` to indicate the next rule on the list. The `from` should be a regexp pattern describing the path from the trigger's perspective. The `to` should then convert this path into a path which is local to Autoscan.

Targets work the other way around. They have to convert the path local to Autoscan to a path understood by the target, which can be a Docker container, remote server, etc. The `from` should be a regexp pattern describing the path from Autoscan's perspective. The `to` should then convert this path into a path which is local to the target.

Expand All @@ -96,24 +96,24 @@ The following config only defines rewrite paths, this should not be used directl
triggers:
sonarr:
- rewrite:
# /tv contains folders with tv shows
# This path is used within the Sonarr Docker container
from: /tv/*
# /tv contains folders with tv shows
# This path is used within the Sonarr Docker container
- from: /tv/*

# /mnt/unionfs/Media/TV links to the same folder, though from the host OS
# This folder is accessed by Autoscan
to: /mnt/unionfs/Media/TV/$1
# /mnt/unionfs/Media/TV links to the same folder, though from the host OS
# This folder is accessed by Autoscan
to: /mnt/unionfs/Media/TV/$1

targets:
plex:
- rewrite:
# Same folder as above, accessible by Autoscan.
# Note how we strip the "TV" part,
# as we want both Movies and TV.
from: /mnt/unionfs/Media/*
# Same folder as above, accessible by Autoscan.
# Note how we strip the "TV" part,
# as we want both Movies and TV.
- from: /mnt/unionfs/Media/*

# This path is used within the Plex Docker container
to: /data/$1
# This path is used within the Plex Docker container
to: /data/$1
```
Let's take a look at the journey of the path `/tv/Westworld/Season 1/s01e01.mkv` coming from Sonarr.
Expand All @@ -135,7 +135,7 @@ We plan to support two kinds of triggers in GA:

- Daemon processes.
These triggers run in the background and fetch resources based on a cron schedule or in real-time. \
*Currently not available, but expected to arrive in GA.*
*Available, but bugs may still exist.*

- Webhooks.
These triggers expose HTTP handlers which can be added to the trigger's software. \
Expand All @@ -152,6 +152,14 @@ Each trigger consists of at least:
- RegExp-based rewriting rules: translate a path given by the trigger to a path on the local file system. \
*If the paths are identical between the trigger and the local file system, then the `rewrite` field should be ignored.*

#### Daemons

Daemons run in the background and continuously fetch new changes based on a [cron expression](https://crontab.guru).

The following daemons are currently provided by Autoscan:

- Google Drive

#### Webhooks

Webhooks, also known as HTTPTriggers internally, process HTTP requests on their exposed endpoints.
Expand Down Expand Up @@ -183,15 +191,35 @@ authentication:
port: 3030
triggers:
bernard:
- account: service-account.json
cron: "*/5 * * * *" # every five minutes (the "" are important)
priority: 0
drives:
- id: Shared Drive 1
- id: Shared Drive 2
# rewrite drive to the local filesystem
rewrite:
- from: ^/Media/*
to: /mnt/unionfs/Media/$1
# filter with regular expressions
include: # if set, then exclude is ignored
- "^/mnt/unionfs/Media/*"
exclude:
- "\.srt$"
sonarr:
- name: sonarr-docker # /triggers/sonarr-docker
priority: 2
# Rewrite the path from within the container
# to your local filesystem.
rewrite:
from: /tv/*
to: /mnt/unionfs/Media/TV/$1
- from: /tv/*
to: /mnt/unionfs/Media/TV/$1
radarr:
- name: radarr # /triggers/radarr
Expand Down Expand Up @@ -299,8 +327,8 @@ targets:
- url: https://plex.domain.tld # URL of your Plex server
token: XXXX # Plex API Token
rewrite:
from: /mnt/unionfs/Media/* # local file system
to: /data/$1 # path accessible by the Plex docker container (if applicable)
- from: /mnt/unionfs/Media/* # local file system
to: /data/$1 # path accessible by the Plex docker container (if applicable)
```

There are a couple of things to take note of in the config:
Expand All @@ -321,8 +349,8 @@ targets:
- url: https://emby.domain.tld # URL of your Emby server
token: XXXX # Emby API Token
rewrite:
from: /mnt/unionfs/Media/* # local file system
to: /data/$1 # path accessible by the Emby docker container (if applicable)
- from: /mnt/unionfs/Media/* # local file system
to: /data/$1 # path accessible by the Emby docker container (if applicable)
```

- URL. The URL can link to the docker container directly, the localhost or a reverse proxy sitting in front of Emby.
Expand Down Expand Up @@ -366,8 +394,8 @@ triggers:
# Rewrite the path from within the container
# to your local filesystem.
rewrite:
from: /tv/*
to: /mnt/unionfs/Media/TV/$1
- from: /tv/*
to: /mnt/unionfs/Media/TV/$1
radarr:
- name: radarr # /triggers/radarr
Expand All @@ -385,15 +413,15 @@ targets:
- url: https://plex.domain.tld # URL of your Plex server
token: XXXX # Plex API Token
rewrite:
from: /mnt/unionfs/Media/* # local file system
to: /data/$1 # path accessible by the Plex docker container (if applicable)
- from: /mnt/unionfs/Media/* # local file system
to: /data/$1 # path accessible by the Plex docker container (if applicable)
emby:
- url: https://emby.domain.tld # URL of your Emby server
token: XXXX # Emby API Token
rewrite:
from: /mnt/unionfs/Media/* # local file system
to: /data/$1 # path accessible by the Emby docker container (if applicable)
- from: /mnt/unionfs/Media/* # local file system
to: /data/$1 # path accessible by the Emby docker container (if applicable)
```

## Other installation options
Expand Down Expand Up @@ -423,7 +451,7 @@ Autoscan's Docker image provides various versions that are available via tags. T
docker run \
--name=autoscan \
-e "PUID=1000" \
-e "PGID=1000" \
-e "PGID=1001" \
-p 3030:3030 \
-v "/opt/autoscan:/config" \
-v "/mnt/unionfs:/mnt/unionfs:ro" \
Expand Down
28 changes: 17 additions & 11 deletions autoscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/http"
"regexp"
"time"
)

// A Scan is at the core of Autoscan.
Expand All @@ -15,6 +16,8 @@ type Scan struct {
File string
Priority int
Retries int
Removed bool
Time time.Time
}

type ProcessorFunc func(...Scan) error
Expand Down Expand Up @@ -71,22 +74,25 @@ type Rewrite struct {

type Rewriter func(string) string

func NewRewriter(r Rewrite) (Rewriter, error) {
if r.From == "" || r.To == "" {
rewriter := func(input string) string {
return input
func NewRewriter(rewriteRules []Rewrite) (Rewriter, error) {
var rewrites []regexp.Regexp
for _, rule := range rewriteRules {
re, err := regexp.Compile(rule.From)
if err != nil {
return nil, err
}

return rewriter, nil
}

re, err := regexp.Compile(r.From)
if err != nil {
return nil, err
rewrites = append(rewrites, *re)
}

rewriter := func(input string) string {
return re.ReplaceAllString(input, r.To)
for i, r := range rewrites {
if r.MatchString(input) {
return r.ReplaceAllString(input, rewriteRules[i].To)
}
}

return input
}

return rewriter, nil
Expand Down
Loading

0 comments on commit 22df8c5

Please sign in to comment.