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

Commit 22df8c5

Browse files
authored
feat(triggers): bernard (#31)
1 parent e63749f commit 22df8c5

File tree

25 files changed

+1424
-211
lines changed

25 files changed

+1424
-211
lines changed

.github/workflows/build.yml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ on:
77
tags:
88
- 'v*'
99
pull_request:
10-
types:
11-
- opened
12-
- reopened
13-
- edited
10+
types: [opened, reopened, edited, ready_for_review]
1411

1512
jobs:
1613
build:
@@ -105,3 +102,16 @@ jobs:
105102
tags: master
106103
tag_with_sha: true
107104
always_pull: true
105+
106+
# docker build (branch)
107+
- name: docker - build other
108+
if: startsWith(github.ref, 'refs/heads/master') == false
109+
uses: docker/build-push-action@v1
110+
with:
111+
username: ${{ secrets.DOCKER_USERNAME }}
112+
password: ${{ secrets.DOCKER_PASSWORD }}
113+
repository: cloudb0x/autoscan
114+
dockerfile: docker/Dockerfile
115+
tag_with_ref: true
116+
tag_with_sha: false
117+
always_pull: true

.github/workflows/cleanup.yml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: Docker Cleanup
2+
3+
on:
4+
pull_request:
5+
types: [closed, removed]
6+
delete:
7+
8+
jobs:
9+
cleanup_branch:
10+
if: startsWith(github.event.ref_type, 'branch') == true
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Determine docker tag
14+
env:
15+
GH_EVENT_REF: ${{ github.event.ref }}
16+
id: dockertag
17+
shell: bash
18+
run: echo "::set-output name=tag::${GH_EVENT_REF#refs/heads/}"
19+
20+
- name: Remove branch docker tag
21+
shell: bash
22+
env:
23+
username: ${{ secrets.DOCKER_USERNAME }}
24+
password: ${{ secrets.DOCKER_PASSWORD }}
25+
tag: ${{ steps.dockertag.outputs.tag }}
26+
run: |
27+
docker run --rm lumir/remove-dockerhub-tag --user "$username" --password "$password" cloudb0x/autoscan:$tag
28+
29+
cleanup_pr:
30+
if: startsWith(github.event.ref, 'refs/pull/') == true
31+
env:
32+
GH_EVENT_REF: ${{ github.event.ref }}
33+
runs-on: ubuntu-latest
34+
steps:
35+
- name: Determine pull request tag
36+
id: githubpr
37+
shell: bash
38+
run: echo "::set-output name=tag::${GH_EVENT_REF#refs/pull/}"
39+
40+
- name: Determine docker tag
41+
uses: frabert/replace-string-action@master
42+
id: dockertag
43+
with:
44+
pattern: '[:\.]+'
45+
string: "${{ steps.githubpr.outputs.tag }}"
46+
replace-with: '-'
47+
flags: 'g'
48+
49+
- name: Remove pull request docker tag
50+
shell: bash
51+
env:
52+
username: ${{ secrets.DOCKER_USERNAME }}
53+
password: ${{ secrets.DOCKER_PASSWORD }}
54+
tag: ${{ steps.dockertag.outputs.replaced }}
55+
run: |
56+
docker run --rm lumir/remove-dockerhub-tag --user "$username" --password "$password" cloudb0x/autoscan:pr-$tag
57+

README.md

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ That's where rewrite rules come into play. They allow you to translate paths bet
7878
**Before you begin, make sure you understand how regular expressions work!** \
7979
Make sure you know how capture groups work, as these are used for the `to` field.
8080

81-
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.
81+
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.
8282

8383
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.
8484

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

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

107107
targets:
108108
plex:
109109
- rewrite:
110-
# Same folder as above, accessible by Autoscan.
111-
# Note how we strip the "TV" part,
112-
# as we want both Movies and TV.
113-
from: /mnt/unionfs/Media/*
110+
# Same folder as above, accessible by Autoscan.
111+
# Note how we strip the "TV" part,
112+
# as we want both Movies and TV.
113+
- from: /mnt/unionfs/Media/*
114114

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

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

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

155+
#### Daemons
156+
157+
Daemons run in the background and continuously fetch new changes based on a [cron expression](https://crontab.guru).
158+
159+
The following daemons are currently provided by Autoscan:
160+
161+
- Google Drive
162+
155163
#### Webhooks
156164

157165
Webhooks, also known as HTTPTriggers internally, process HTTP requests on their exposed endpoints.
@@ -183,15 +191,35 @@ authentication:
183191
port: 3030
184192
185193
triggers:
194+
bernard:
195+
- account: service-account.json
196+
cron: "*/5 * * * *" # every five minutes (the "" are important)
197+
priority: 0
198+
drives:
199+
- id: Shared Drive 1
200+
- id: Shared Drive 2
201+
202+
# rewrite drive to the local filesystem
203+
rewrite:
204+
- from: ^/Media/*
205+
to: /mnt/unionfs/Media/$1
206+
207+
# filter with regular expressions
208+
include: # if set, then exclude is ignored
209+
- "^/mnt/unionfs/Media/*"
210+
211+
exclude:
212+
- "\.srt$"
213+
186214
sonarr:
187215
- name: sonarr-docker # /triggers/sonarr-docker
188216
priority: 2
189217
190218
# Rewrite the path from within the container
191219
# to your local filesystem.
192220
rewrite:
193-
from: /tv/*
194-
to: /mnt/unionfs/Media/TV/$1
221+
- from: /tv/*
222+
to: /mnt/unionfs/Media/TV/$1
195223
196224
radarr:
197225
- name: radarr # /triggers/radarr
@@ -299,8 +327,8 @@ targets:
299327
- url: https://plex.domain.tld # URL of your Plex server
300328
token: XXXX # Plex API Token
301329
rewrite:
302-
from: /mnt/unionfs/Media/* # local file system
303-
to: /data/$1 # path accessible by the Plex docker container (if applicable)
330+
- from: /mnt/unionfs/Media/* # local file system
331+
to: /data/$1 # path accessible by the Plex docker container (if applicable)
304332
```
305333

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

328356
- URL. The URL can link to the docker container directly, the localhost or a reverse proxy sitting in front of Emby.
@@ -366,8 +394,8 @@ triggers:
366394
# Rewrite the path from within the container
367395
# to your local filesystem.
368396
rewrite:
369-
from: /tv/*
370-
to: /mnt/unionfs/Media/TV/$1
397+
- from: /tv/*
398+
to: /mnt/unionfs/Media/TV/$1
371399
372400
radarr:
373401
- name: radarr # /triggers/radarr
@@ -385,15 +413,15 @@ targets:
385413
- url: https://plex.domain.tld # URL of your Plex server
386414
token: XXXX # Plex API Token
387415
rewrite:
388-
from: /mnt/unionfs/Media/* # local file system
389-
to: /data/$1 # path accessible by the Plex docker container (if applicable)
416+
- from: /mnt/unionfs/Media/* # local file system
417+
to: /data/$1 # path accessible by the Plex docker container (if applicable)
390418
391419
emby:
392420
- url: https://emby.domain.tld # URL of your Emby server
393421
token: XXXX # Emby API Token
394422
rewrite:
395-
from: /mnt/unionfs/Media/* # local file system
396-
to: /data/$1 # path accessible by the Emby docker container (if applicable)
423+
- from: /mnt/unionfs/Media/* # local file system
424+
to: /data/$1 # path accessible by the Emby docker container (if applicable)
397425
```
398426

399427
## Other installation options
@@ -423,7 +451,7 @@ Autoscan's Docker image provides various versions that are available via tags. T
423451
docker run \
424452
--name=autoscan \
425453
-e "PUID=1000" \
426-
-e "PGID=1000" \
454+
-e "PGID=1001" \
427455
-p 3030:3030 \
428456
-v "/opt/autoscan:/config" \
429457
-v "/mnt/unionfs:/mnt/unionfs:ro" \

autoscan.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"net/http"
66
"regexp"
7+
"time"
78
)
89

910
// A Scan is at the core of Autoscan.
@@ -15,6 +16,8 @@ type Scan struct {
1516
File string
1617
Priority int
1718
Retries int
19+
Removed bool
20+
Time time.Time
1821
}
1922

2023
type ProcessorFunc func(...Scan) error
@@ -71,22 +74,25 @@ type Rewrite struct {
7174

7275
type Rewriter func(string) string
7376

74-
func NewRewriter(r Rewrite) (Rewriter, error) {
75-
if r.From == "" || r.To == "" {
76-
rewriter := func(input string) string {
77-
return input
77+
func NewRewriter(rewriteRules []Rewrite) (Rewriter, error) {
78+
var rewrites []regexp.Regexp
79+
for _, rule := range rewriteRules {
80+
re, err := regexp.Compile(rule.From)
81+
if err != nil {
82+
return nil, err
7883
}
7984

80-
return rewriter, nil
81-
}
82-
83-
re, err := regexp.Compile(r.From)
84-
if err != nil {
85-
return nil, err
85+
rewrites = append(rewrites, *re)
8686
}
8787

8888
rewriter := func(input string) string {
89-
return re.ReplaceAllString(input, r.To)
89+
for i, r := range rewrites {
90+
if r.MatchString(input) {
91+
return r.ReplaceAllString(input, rewriteRules[i].To)
92+
}
93+
}
94+
95+
return input
9096
}
9197

9298
return rewriter, nil

0 commit comments

Comments
 (0)