Skip to content

Commit 8cb69a6

Browse files
authored
Merge pull request #2 from l3uddz/feat/service-account-env-vars
Feat/service account env vars
2 parents 4ac8936 + 9ac93a3 commit 8cb69a6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+689
-522
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ rclone:
1616
config: /home/seed/.config/rclone/rclone.conf
1717
path: /usr/bin/rclone
1818
stats: 30s
19+
service_account_remotes:
20+
tv: /opt/rclone/service_accounts/crop
21+
movies: /opt/rclone/service_accounts/crop
22+
music: /opt/rclone/service_accounts/crop
23+
4k_movies: /opt/rclone/service_accounts/crop
24+
source_4k_movies: /opt/rclone/service_accounts/crop
25+
staging: /opt/rclone/service_accounts/staging
1926
uploader:
2027
google:
2128
check:
@@ -36,7 +43,6 @@ uploader:
3643
move_server_side:
3744
- from: 'staging:/Media'
3845
to: 'gdrive:/Media'
39-
sa_folder: /opt/service_account_maker/service_accounts/crop
4046
rclone_params:
4147
move:
4248
- '--transfers=8'
@@ -45,10 +51,35 @@ uploader:
4551
- '--delete-empty-src-dirs'
4652
dedupe:
4753
- '--tpslimit=50'
54+
tv:
55+
enabled: true
56+
check:
57+
limit: 1440
58+
type: age
59+
local_folder: /mnt/local/Media/TV
60+
remotes:
61+
move: 'tv:/Media/TV'
62+
rclone_params:
63+
move:
64+
- '--order-by=modtime,ascending'
65+
- '--transfers=8'
66+
- '--delete-empty-src-dirs'
67+
movies:
68+
enabled: true
69+
check:
70+
limit: 720
71+
type: age
72+
local_folder: /mnt/local/Media/Movies
73+
remotes:
74+
move: 'movies:/Media/Movies'
75+
rclone_params:
76+
move:
77+
- '--order-by=modtime,ascending'
78+
- '--transfers=8'
79+
- '--delete-empty-src-dirs'
4880
syncer:
4981
4k_movies:
5082
enabled: true
51-
sa_folder: /opt/service_account_maker/service_accounts/crop
5283
source_remote: 'source_4k_movies:/'
5384
remotes:
5485
sync:

cache/cache.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,51 @@
11
package cache
22

33
import (
4-
jsoniter "github.com/json-iterator/go"
54
"github.com/l3uddz/crop/logger"
65
"github.com/l3uddz/crop/stringutils"
7-
"sync"
8-
"time"
6+
"github.com/pkg/errors"
7+
"github.com/zippoxer/bow"
98
)
109

1110
var (
1211
log = logger.GetLogger("cache")
1312
cacheFilePath string
14-
mtx sync.Mutex
15-
vault map[string]time.Time
1613

1714
// Internal
18-
json = jsoniter.ConfigCompatibleWithStandardLibrary
15+
db *bow.DB
1916
)
2017

2118
/* Public */
2219

23-
func Init(cachePath string) error {
20+
func Init(cachePath string, logLevel int) error {
2421
// set globals
2522
cacheFilePath = cachePath
26-
vault = make(map[string]time.Time)
2723

28-
// load cache from disk
29-
if err := loadFromFile(cachePath); err != nil {
30-
return err
24+
// set badger options
25+
opts := make([]bow.Option, 0)
26+
27+
if logLevel < 2 {
28+
// disable badger logging for non trace log level
29+
opts = append(opts, bow.SetLogger(nil))
30+
}
31+
32+
// init database
33+
v, err := bow.Open(cachePath, opts...)
34+
if err != nil {
35+
return errors.WithMessage(err, "failed opening cache")
3136
}
3237

38+
db = v
39+
3340
return nil
3441
}
3542

43+
func Close() {
44+
if err := db.Close(); err != nil {
45+
log.WithError(err).Error("Failed closing cache gracefully...")
46+
}
47+
}
48+
3649
func ShowUsing() {
3750
log.Infof("Using %s = %q", stringutils.LeftJust("CACHE", " ", 10), cacheFilePath)
3851
}

cache/dump.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

cache/get.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

cache/load.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

cache/sa.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cache
2+
3+
import (
4+
"github.com/zippoxer/bow"
5+
"time"
6+
)
7+
8+
type Banned struct {
9+
Path string `bow:"key"`
10+
Expires time.Time
11+
}
12+
13+
func IsBanned(key string) (bool, time.Time) {
14+
// check if key was found in banned bucket
15+
var item Banned
16+
err := db.Bucket("banned").Get(key, &item)
17+
18+
// was key not found
19+
if err == bow.ErrNotFound {
20+
// this key is not banned
21+
return false, time.Time{}
22+
} else if err != nil {
23+
log.WithError(err).Errorf("Failed checking banned bucket for: %q", key)
24+
return false, time.Time{}
25+
}
26+
27+
// check if the ban has expired
28+
if item.Expires.Before(time.Now().UTC()) {
29+
// the ban has expired, remove
30+
log.Warnf("Expired %q: %v", key, item.Expires)
31+
32+
err := db.Bucket("banned").Delete(key)
33+
if err != nil {
34+
log.WithError(err).Errorf("Failed removing from banned bucket: %q", key)
35+
return false, time.Time{}
36+
}
37+
38+
return false, time.Time{}
39+
}
40+
41+
// this key is still banned
42+
return true, item.Expires
43+
}
44+
45+
func SetBanned(key string, hours int) error {
46+
expiry := time.Now().UTC().Add(time.Duration(hours) * time.Hour)
47+
48+
return db.Bucket("banned").Put(Banned{
49+
Path: key,
50+
Expires: expiry,
51+
})
52+
}

cache/set.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

cmd/clean.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var cleanCmd = &cobra.Command{
2020

2121
// iterate uploader's
2222
for uploaderName, uploaderConfig := range config.Config.Uploader {
23+
uploaderConfig := uploaderConfig
2324
log := log.WithField("uploader", uploaderName)
2425

2526
// skip disabled uploader(s)

cmd/root.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var (
2020
flagLogLevel = 0
2121
flagConfigFolder = pathutils.GetCurrentBinaryPath()
2222
flagConfigFile = "config.yaml"
23-
flagCacheFile = "cache.json"
23+
flagCachePath = "cache"
2424
flagLogFile = "activity.log"
2525

2626
flagDryRun bool
@@ -50,7 +50,7 @@ func init() {
5050
// Parse persistent flags
5151
rootCmd.PersistentFlags().StringVar(&flagConfigFolder, "config-dir", flagConfigFolder, "Config folder")
5252
rootCmd.PersistentFlags().StringVarP(&flagConfigFile, "config", "c", flagConfigFile, "Config file")
53-
rootCmd.PersistentFlags().StringVarP(&flagCacheFile, "cache", "d", flagCacheFile, "Cache file")
53+
rootCmd.PersistentFlags().StringVarP(&flagCachePath, "cache", "d", flagCachePath, "Cache path")
5454
rootCmd.PersistentFlags().StringVarP(&flagLogFile, "log", "l", flagLogFile, "Log file")
5555
rootCmd.PersistentFlags().CountVarP(&flagLogLevel, "verbose", "v", "Verbose level")
5656

@@ -63,7 +63,7 @@ func initCore(showAppInfo bool) {
6363
flagConfigFile = filepath.Join(flagConfigFolder, flagConfigFile)
6464
}
6565
if !rootCmd.PersistentFlags().Changed("cache") {
66-
flagCacheFile = filepath.Join(flagConfigFolder, flagCacheFile)
66+
flagCachePath = filepath.Join(flagConfigFolder, flagCachePath)
6767
}
6868
if !rootCmd.PersistentFlags().Changed("log") {
6969
flagLogFile = filepath.Join(flagConfigFolder, flagLogFile)
@@ -84,7 +84,7 @@ func initCore(showAppInfo bool) {
8484
setConfigOverrides()
8585

8686
// Init Cache
87-
if err := cache.Init(flagCacheFile); err != nil {
87+
if err := cache.Init(flagCachePath, flagLogLevel); err != nil {
8888
log.WithError(err).Fatal("Failed to initialize cache")
8989
}
9090

cmd/sync.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"github.com/dustin/go-humanize"
5+
"github.com/l3uddz/crop/cache"
56
"github.com/l3uddz/crop/config"
67
"github.com/l3uddz/crop/rclone"
78
"github.com/l3uddz/crop/syncer"
@@ -23,9 +24,11 @@ var syncCmd = &cobra.Command{
2324
Run: func(cmd *cobra.Command, args []string) {
2425
// init core
2526
initCore(true)
27+
defer cache.Close()
2628

2729
// iterate syncer's
2830
for syncerName, syncerConfig := range config.Config.Syncer {
31+
syncerConfig := syncerConfig
2932
log := log.WithField("syncer", syncerName)
3033

3134
// skip disabled syncer(s)
@@ -47,9 +50,9 @@ var syncCmd = &cobra.Command{
4750
continue
4851
}
4952

50-
if sync.ServiceAccountCount > 0 {
51-
sync.Log.WithField("found_files", sync.ServiceAccountCount).
52-
Info("Loaded service accounts")
53+
serviceAccountCount := sync.RemoteServiceAccountFiles.ServiceAccountsCount()
54+
if serviceAccountCount > 0 {
55+
sync.Log.WithField("found_files", serviceAccountCount).Info("Loaded service accounts")
5356
} else {
5457
// no service accounts were loaded
5558
// check to see if any of the copy or move remote(s) are banned

0 commit comments

Comments
 (0)