Skip to content

Commit

Permalink
add(cmd/manual): manual command to perform a copy/sync without requir…
Browse files Browse the repository at this point in the history
…ing a configuration change

e.g:

crop manual --sync --src remote1:/Media --dst remote2:/Media --sa /opt/service_accounts -- --drive-use-trash=false --transfers=8
  • Loading branch information
l3uddz committed May 2, 2020
1 parent aadfc2b commit f033bea
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 16 deletions.
168 changes: 168 additions & 0 deletions cmd/manual.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package cmd

import (
"github.com/dustin/go-humanize"
"github.com/l3uddz/crop/cache"
"github.com/l3uddz/crop/config"
"github.com/l3uddz/crop/rclone"
"github.com/l3uddz/crop/stringutils"
"github.com/l3uddz/crop/syncer"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"strings"
)

var (
flagSrc string
flagDest string
flagSaFolder string
flagDedupe bool
flagCopy bool
flagSync bool
)

var manualCmd = &cobra.Command{
Use: "manual",
Short: "Perform a manual copy/sync task",
Long: `This command can be used to trigger a copy/sync without requiring configuration changes.`,
Run: func(cmd *cobra.Command, args []string) {
// init core
initCore(true)
defer cache.Close()

// determine destination remotes
syncRemotes := make([]string, 0)
copyRemotes := make([]string, 0)

switch {
case flagCopy && flagSync:
log.Fatal("You should must a single mode to use, --sync / --copy")
case flagCopy:
copyRemotes = append(copyRemotes, flagDest)
case flagSync:
syncRemotes = append(syncRemotes, flagDest)
default:
log.Fatal("You must specify a mode to use, --sync / --copy")
}

// create remote to service account map
remoteSaFolders := make(map[string]string)

switch flagSaFolder != "" {
case true:
if strings.Contains(flagSrc, ":") {
// source is a remote
srcRemote := stringutils.FromLeftUntil(flagSrc, ":")
log.Debugf("Using service account folder for %q: %v", srcRemote, flagSaFolder)
remoteSaFolders[srcRemote] = flagSaFolder
}

if strings.Contains(flagDest, ":") {
// dest is a remote
dstRemote := stringutils.FromLeftUntil(flagDest, ":")
log.Debugf("Using service account folder for %q: %v", dstRemote, flagSaFolder)
remoteSaFolders[dstRemote] = flagSaFolder
}

default:
break
}

// create syncer config
syncerConfig := config.SyncerConfig{
Name: "manual",
Enabled: true,
SourceRemote: flagSrc,
Remotes: config.SyncerRemotes{
Copy: copyRemotes,
Sync: syncRemotes,
},
RcloneParams: config.SyncerRcloneParams{
Copy: args,
Sync: args,
Dedupe: []string{
"--tpslimit=5",
},
},
}

if flagDedupe {
// dedupe was enabled
syncerConfig.Remotes.Dedupe = []string{
flagDest,
}
}

// create a config structure for manual sync
cfg := config.Configuration{
Rclone: config.RcloneConfig{
Path: config.Config.Rclone.Path,
Config: config.Config.Rclone.Config,
Stats: config.Config.Rclone.Stats,
DryRun: config.Config.Rclone.DryRun,
ServiceAccountRemotes: remoteSaFolders,
},
Uploader: nil,
Syncer: []config.SyncerConfig{
syncerConfig,
},
}

// create syncer
sync, err := syncer.New(&cfg, &syncerConfig, syncerConfig.Name)
if err != nil {
log.WithError(err).Fatal("Failed initializing syncer, skipping...")
}

// load service accounts
serviceAccountCount := sync.RemoteServiceAccountFiles.ServiceAccountsCount()
if serviceAccountCount > 0 {
sync.Log.WithField("found_files", serviceAccountCount).Info("Loaded service accounts")
} else {
// no service accounts were loaded
// check to see if any of the copy or sync remote(s) are banned
banned, expiry := rclone.AnyRemotesBanned(sync.Config.Remotes.Copy)
if banned && !expiry.IsZero() {
// one of the copy remotes is banned, abort
sync.Log.WithFields(logrus.Fields{
"expires_time": expiry,
"expires_in": humanize.Time(expiry),
}).Fatal("Cannot proceed as a copy remote is banned")
}

banned, expiry = rclone.AnyRemotesBanned(sync.Config.Remotes.Sync)
if banned && !expiry.IsZero() {
// one of the sync remotes is banned, abort
sync.Log.WithFields(logrus.Fields{
"expires_time": expiry,
"expires_in": humanize.Time(expiry),
}).Fatal("Cannot proceed as a sync remote is banned")
}
}

log.Info("Syncer commencing...")

// perform sync
if err := performSync(sync); err != nil {
sync.Log.WithError(err).Fatal("Error occurred while running syncer, skipping...")
}

log.Info("Finished!")
},
}

func init() {
rootCmd.AddCommand(manualCmd)

manualCmd.Flags().StringVar(&flagSrc, "src", "", "Source")
manualCmd.Flags().StringVar(&flagDest, "dst", "", "Destination")

_ = manualCmd.MarkFlagRequired("from")
_ = manualCmd.MarkFlagRequired("dest")

manualCmd.Flags().StringVar(&flagSaFolder, "sa", "", "Service account folder")

manualCmd.Flags().BoolVar(&flagCopy, "copy", false, "Copy to destination")
manualCmd.Flags().BoolVar(&flagSync, "sync", false, "Sync to destination")
manualCmd.Flags().BoolVar(&flagDedupe, "dedupe", false, "Dedupe destination")
}
2 changes: 1 addition & 1 deletion cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var syncCmd = &cobra.Command{
sync.Log.WithField("found_files", serviceAccountCount).Info("Loaded service accounts")
} else {
// no service accounts were loaded
// check to see if any of the copy or move remote(s) are banned
// check to see if any of the copy or sync remote(s) are banned
banned, expiry := rclone.AnyRemotesBanned(sync.Config.Remotes.Copy)
if banned && !expiry.IsZero() {
// one of the copy remotes is banned, abort
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-cmd/cmd v1.2.0
github.com/golang/protobuf v1.4.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/mitchellh/mapstructure v1.2.2 // indirect
github.com/mitchellh/mapstructure v1.3.0 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/onsi/ginkgo v1.12.0 // indirect
github.com/onsi/gomega v1.9.0 // indirect
Expand All @@ -32,10 +32,10 @@ require (
github.com/x-cray/logrus-prefixed-formatter v0.5.2
github.com/yale8848/gorpool v0.1.0
github.com/zippoxer/bow v0.0.0-20200229231453-bf1012ae7ab9
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5 // indirect
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 // indirect
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79 // indirect
golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f // indirect
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3 // indirect
google.golang.org/appengine v1.6.6 // indirect
gopkg.in/ini.v1 v1.55.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand All @@ -137,8 +137,8 @@ github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyex
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.2.2 h1:dxe5oCinTXiTIcfgmZecdCzPmAJKd46KsCWc35r0TV4=
github.com/mitchellh/mapstructure v1.2.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.3.0 h1:iDwIio/3gk2QtLLEsqU5lInaMzos0hDTz8a6lazSFVw=
github.com/mitchellh/mapstructure v1.3.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
Expand Down Expand Up @@ -258,8 +258,8 @@ go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5 h1:Q7tZBpemrlsc2I7IyODzhtallWRSm4Q0d09pL6XbQtU=
golang.org/x/crypto v0.0.0-20200423211502-4bdfaf469ed5/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79 h1:IaQbIIB2X/Mp/DKctl6ROxz1KyMlKp4uyvL6+kQ7C88=
golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand All @@ -276,8 +276,8 @@ golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0 h1:Jcxah/M+oLZ/R4/z5RzfPzGbPXnVDPkEDtf2JnuxN+U=
golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5 h1:WQ8q63x+f/zpC8Ac1s9wLElVoHhm32p6tudrU72n1QA=
golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288 h1:JIqe8uIcRBHXDQVvZtHwp80ai3Lw3IJAeJEs55Dc1W0=
golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
Expand All @@ -304,8 +304,8 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f h1:gWF768j/LaZugp8dyS4UwsslYCYz9XgFxvlgsn0n9H8=
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3 h1:5B6i6EAiSYyejWfvc5Rc9BbI3rzIsrrXfAQBWnYfn+w=
golang.org/x/sys v0.0.0-20200501145240-bc7a7d42d5c3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
Expand Down

0 comments on commit f033bea

Please sign in to comment.