Skip to content

Commit bb22e08

Browse files
committed
change(syncer/uploader/deduper): decouple from gclone, deduper will use a random service account when there are service accounts associated with it
1 parent a981598 commit bb22e08

File tree

10 files changed

+59
-17
lines changed

10 files changed

+59
-17
lines changed

cmd/sync.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,13 @@ func worker(wg *sync.WaitGroup, jobs <-chan *syncer.Syncer) {
130130
func performSync(s *syncer.Syncer) error {
131131
s.Log.Info("Running...")
132132

133-
var gcloneParams []string
134-
if strings.Contains(s.GlobalConfig.Rclone.Path, "gclone") &&
135-
s.RemoteServiceAccountFiles.ServiceAccountsCount() > 0 {
133+
var liveRotateParams []string
134+
if s.GlobalConfig.Rclone.LiveRotate && s.RemoteServiceAccountFiles.ServiceAccountsCount() > 0 {
136135
// start web-server
137136
s.Ws.Run()
138137
defer s.Ws.Stop()
139138

140-
gcloneParams = append(gcloneParams,
139+
liveRotateParams = append(liveRotateParams,
141140
"--drive-service-account-url",
142141
fmt.Sprintf("http://%s:%d", s.Ws.Host, s.Ws.Port),
143142
)
@@ -147,7 +146,7 @@ func performSync(s *syncer.Syncer) error {
147146
if len(s.Config.Remotes.Copy) > 0 {
148147
s.Log.Info("Running copies...")
149148

150-
if err := s.Copy(gcloneParams); err != nil {
149+
if err := s.Copy(liveRotateParams); err != nil {
151150
return errors.WithMessage(err, "failed performing all copies")
152151
}
153152

@@ -158,7 +157,7 @@ func performSync(s *syncer.Syncer) error {
158157
if len(s.Config.Remotes.Sync) > 0 {
159158
s.Log.Info("Running syncs...")
160159

161-
if err := s.Sync(gcloneParams); err != nil {
160+
if err := s.Sync(liveRotateParams); err != nil {
162161
return errors.WithMessage(err, "failed performing all syncs")
163162
}
164163

cmd/upload.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,14 @@ func init() {
122122
func performUpload(u *uploader.Uploader) error {
123123
u.Log.Info("Running...")
124124

125-
var gcloneParams []string
125+
var liveRotateParams []string
126126

127-
if strings.Contains(u.GlobalConfig.Rclone.Path, "gclone") &&
128-
u.RemoteServiceAccountFiles.ServiceAccountsCount() > 0 {
127+
if u.GlobalConfig.Rclone.LiveRotate && u.RemoteServiceAccountFiles.ServiceAccountsCount() > 0 {
129128
// start web-server
130129
u.Ws.Run()
131130
defer u.Ws.Stop()
132131

133-
gcloneParams = append(gcloneParams,
132+
liveRotateParams = append(liveRotateParams,
134133
"--drive-service-account-url",
135134
fmt.Sprintf("http://%s:%d", u.Ws.Host, u.Ws.Port),
136135
)
@@ -152,9 +151,9 @@ func performUpload(u *uploader.Uploader) error {
152151
additionalRcloneParams = u.CheckRcloneParams()
153152
}
154153

155-
// add gclone params set
156-
if len(gcloneParams) > 0 {
157-
additionalRcloneParams = append(additionalRcloneParams, gcloneParams...)
154+
// add live rotate params set
155+
if len(liveRotateParams) > 0 {
156+
additionalRcloneParams = append(additionalRcloneParams, liveRotateParams...)
158157
}
159158

160159
/* Copies */

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func setConfigDefaults(check bool) error {
123123
// rclone settings
124124
added += setConfigDefault("rclone.path", "/usr/bin/rclone", check)
125125
added += setConfigDefault("rclone.config", "/Users/l3uddz/.config/rclone/rclone.conf", check)
126+
added += setConfigDefault("rclone.live_rotate", false, check)
126127

127128
// were new settings added?
128129
if check && added > 0 {

config/rclone.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ type RcloneConfig struct {
44
Path string
55
Config string
66
Stats string
7+
LiveRotate bool `mapstructure:"live_rotate"`
78
DryRun bool `mapstructure:"dry_run"`
89
ServiceAccountRemotes map[string]string `mapstructure:"service_account_remotes"`
910
GlobalParams map[string]RcloneParams `mapstructure:"global_params"`

rclone/sa.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/l3uddz/crop/stringutils"
1212
"github.com/sirupsen/logrus"
1313
"go/types"
14+
"math/rand"
1415
"sort"
1516
"strconv"
1617
"strings"
@@ -145,6 +146,30 @@ func (m *ServiceAccountManager) LoadServiceAccounts(remotePaths []string) error
145146
return nil
146147
}
147148

149+
func (m *ServiceAccountManager) GetRandomServiceAccount(remotePath string) (string, error) {
150+
// parse remote name
151+
remoteName := stringutils.FromLeftUntil(remotePath, ":")
152+
if remoteName == "" {
153+
// no remote name was parsed, so ignore this request
154+
m.log.Tracef("No remote determined for: %q, not providing service account", remotePath)
155+
return "", nil
156+
}
157+
158+
// service accounts loaded for this remote?
159+
remote, ok := m.remoteServiceAccounts[remoteName]
160+
if !ok || len(remote.ServiceAccounts) == 0 {
161+
// no service accounts found for this remote
162+
m.log.Tracef("No service accounts loaded for remote: %q, not providing service account", remoteName)
163+
return "", nil
164+
}
165+
166+
// random service account
167+
rand.Seed(time.Now().Unix())
168+
sa := remote.ServiceAccounts[rand.Intn(len(remote.ServiceAccounts))]
169+
170+
return sa.RealPath, nil
171+
}
172+
148173
func (m *ServiceAccountManager) GetServiceAccount(remotePaths ...string) ([]*RemoteServiceAccount, error) {
149174
var serviceAccounts []*RemoteServiceAccount
150175
var err error

syncer/copy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (s *Syncer) Copy(additionalRcloneParams []string) error {
6363
} else if success {
6464
// successful exit code
6565
if !s.Ws.Running {
66-
// web service is not running (no gclone)
66+
// web service is not running (no live rotate)
6767
rclone.RemoveServiceAccountsFromTempCache(serviceAccounts)
6868
}
6969
break

syncer/dedupe.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ func (s *Syncer) Dedupe(additionalRcloneParams []string) error {
2424
"dedupe_remote": dedupeRemote,
2525
})
2626

27+
// service account
28+
if s.RemoteServiceAccountFiles.ServiceAccountsCount() > 0 {
29+
sa, err := s.RemoteServiceAccountFiles.GetRandomServiceAccount(dedupeRemote)
30+
if err == nil && sa != "" {
31+
extraParams = append(extraParams, "--drive-service-account-file", sa)
32+
}
33+
}
34+
2735
// dedupe remote
2836
rLog.Info("Deduping...")
2937
success, exitCode, err := rclone.Dedupe(dedupeRemote, extraParams)

syncer/sync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (s *Syncer) Sync(additionalRcloneParams []string) error {
6363
} else if success {
6464
// successful exit code
6565
if !s.Ws.Running {
66-
// web service is not running (no gclone)
66+
// web service is not running (no live rotate)
6767
rclone.RemoveServiceAccountsFromTempCache(serviceAccounts)
6868
}
6969
break

uploader/dedupe.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ func (u *Uploader) Dedupe(additionalRcloneParams []string) error {
2424
"dedupe_remote": dedupeRemote,
2525
})
2626

27+
// service account
28+
if u.RemoteServiceAccountFiles.ServiceAccountsCount() > 0 {
29+
sa, err := u.RemoteServiceAccountFiles.GetRandomServiceAccount(dedupeRemote)
30+
if err == nil && sa != "" {
31+
extraParams = append(extraParams, "--drive-service-account-file", sa)
32+
}
33+
}
34+
2735
// dedupe remote
2836
rLog.Info("Deduping...")
2937
success, exitCode, err := rclone.Dedupe(dedupeRemote, extraParams)

web/handler.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (ws *Server) ServiceAccountHandler(c *fiber.Ctx) {
1717
// parse body
1818
req := new(ServiceAccountRequest)
1919
if err := c.BodyParser(req); err != nil {
20-
ws.log.WithError(err).Error("Failed parsing service account request from gclone...")
20+
ws.log.WithError(err).Error("Failed parsing service account request from rclone...")
2121
c.SendStatus(500)
2222
return
2323
}
@@ -80,9 +80,10 @@ func (ws *Server) ServiceAccountHandler(c *fiber.Ctx) {
8080
ws.saCache.cache[req.OldServiceAccount] = cacheEntry
8181

8282
// store cache entry for the new account
83-
// (so if another gclone transfer routine requests within N duration, re-issue the same sa)
83+
// (so if another transfer routine requests within N duration, re-issue the same sa)
8484
ws.saCache.cache[sa[0].ServiceAccountPath] = cacheEntry
8585

8686
// return service account
87+
ws.log.Warnf("New service account for remote %q, sa: %v", req.Remote, sa[0].ServiceAccountPath)
8788
c.SendString(sa[0].ServiceAccountPath)
8889
}

0 commit comments

Comments
 (0)