diff --git a/worker/config.go b/worker/config.go index 0d8a20b..6f162f0 100644 --- a/worker/config.go +++ b/worker/config.go @@ -93,6 +93,7 @@ type mirrorConfig struct { Command string `toml:"command"` UseIPv6 bool `toml:"use_ipv6"` ExcludeFile string `toml:"exclude_file"` + Username string `toml:"username"` Password string `toml:"password"` Stage1Profile string `toml:"stage1_profile"` } diff --git a/worker/provider.go b/worker/provider.go index 33100fb..923cf2c 100644 --- a/worker/provider.go +++ b/worker/provider.go @@ -119,6 +119,7 @@ func newMirrorProvider(mirror mirrorConfig, cfg *Config) mirrorProvider { name: mirror.Name, upstreamURL: mirror.Upstream, rsyncCmd: mirror.Command, + username: mirror.Username, password: mirror.Password, excludeFile: mirror.ExcludeFile, workingDir: mirrorDir, @@ -139,6 +140,7 @@ func newMirrorProvider(mirror mirrorConfig, cfg *Config) mirrorProvider { stage1Profile: mirror.Stage1Profile, upstreamURL: mirror.Upstream, rsyncCmd: mirror.Command, + username: mirror.Username, password: mirror.Password, excludeFile: mirror.ExcludeFile, workingDir: mirrorDir, diff --git a/worker/provider_test.go b/worker/provider_test.go index c157de6..ab29621 100644 --- a/worker/provider_test.go +++ b/worker/provider_test.go @@ -71,7 +71,7 @@ func TestRsyncProvider(t *testing.T) { Convey("Let's try a run", func() { scriptContent := `#!/bin/bash echo "syncing to $(pwd)" -echo $@ +echo $RSYNC_PASSWORD $@ sleep 1 echo "Done" exit 0 @@ -103,6 +103,71 @@ exit 0 }) } +func TestRsyncProviderWithAuthentication(t *testing.T) { + Convey("Rsync Provider with password should work", t, func() { + tmpDir, err := ioutil.TempDir("", "tunasync") + defer os.RemoveAll(tmpDir) + So(err, ShouldBeNil) + scriptFile := filepath.Join(tmpDir, "myrsync") + tmpFile := filepath.Join(tmpDir, "log_file") + + c := rsyncConfig{ + name: "tuna", + upstreamURL: "rsync://rsync.tuna.moe/tuna/", + rsyncCmd: scriptFile, + username: "tunasync", + password: "tunasyncpassword", + workingDir: tmpDir, + logDir: tmpDir, + logFile: tmpFile, + useIPv6: true, + interval: 600 * time.Second, + } + + provider, err := newRsyncProvider(c) + So(err, ShouldBeNil) + + So(provider.Name(), ShouldEqual, c.name) + So(provider.WorkingDir(), ShouldEqual, c.workingDir) + So(provider.LogDir(), ShouldEqual, c.logDir) + So(provider.LogFile(), ShouldEqual, c.logFile) + So(provider.Interval(), ShouldEqual, c.interval) + + Convey("Let's try a run", func() { + scriptContent := `#!/bin/bash +echo "syncing to $(pwd)" +echo $USER $RSYNC_PASSWORD $@ +sleep 1 +echo "Done" +exit 0 + ` + err = ioutil.WriteFile(scriptFile, []byte(scriptContent), 0755) + So(err, ShouldBeNil) + + expectedOutput := fmt.Sprintf( + "syncing to %s\n"+ + "%s\n"+ + "Done\n", + provider.WorkingDir(), + fmt.Sprintf( + "%s %s -aHvh --no-o --no-g --stats --exclude .~tmp~/ "+ + "--delete --delete-after --delay-updates --safe-links "+ + "--timeout=120 --contimeout=120 -6 %s %s", + provider.username, provider.password, provider.upstreamURL, provider.WorkingDir(), + ), + ) + + err = provider.Run() + So(err, ShouldBeNil) + loggedContent, err := ioutil.ReadFile(provider.LogFile()) + So(err, ShouldBeNil) + So(string(loggedContent), ShouldEqual, expectedOutput) + // fmt.Println(string(loggedContent)) + }) + + }) +} + func TestCmdProvider(t *testing.T) { Convey("Command Provider should work", t, func(ctx C) { tmpDir, err := ioutil.TempDir("", "tunasync") diff --git a/worker/rsync_provider.go b/worker/rsync_provider.go index 2d70174..aafd761 100644 --- a/worker/rsync_provider.go +++ b/worker/rsync_provider.go @@ -7,12 +7,12 @@ import ( ) type rsyncConfig struct { - name string - rsyncCmd string - upstreamURL, password, excludeFile string - workingDir, logDir, logFile string - useIPv6 bool - interval time.Duration + name string + rsyncCmd string + upstreamURL, username, password, excludeFile string + workingDir, logDir, logFile string + useIPv6 bool + interval time.Duration } // An RsyncProvider provides the implementation to rsync-based syncing jobs @@ -81,6 +81,9 @@ func (p *rsyncProvider) Run() error { func (p *rsyncProvider) Start() error { env := map[string]string{} + if p.username != "" { + env["USER"] = p.username + } if p.password != "" { env["RSYNC_PASSWORD"] = p.password } diff --git a/worker/two_stage_rsync_provider.go b/worker/two_stage_rsync_provider.go index 37799b3..21324ca 100644 --- a/worker/two_stage_rsync_provider.go +++ b/worker/two_stage_rsync_provider.go @@ -8,13 +8,13 @@ import ( ) type twoStageRsyncConfig struct { - name string - rsyncCmd string - stage1Profile string - upstreamURL, password, excludeFile string - workingDir, logDir, logFile string - useIPv6 bool - interval time.Duration + name string + rsyncCmd string + stage1Profile string + upstreamURL, username, password, excludeFile string + workingDir, logDir, logFile string + useIPv6 bool + interval time.Duration } // An RsyncProvider provides the implementation to rsync-based syncing jobs @@ -110,6 +110,9 @@ func (p *twoStageRsyncProvider) Options(stage int) ([]string, error) { func (p *twoStageRsyncProvider) Run() error { env := map[string]string{} + if p.username != "" { + env["USER"] = p.username + } if p.password != "" { env["RSYNC_PASSWORD"] = p.password }