Bläddra i källkod

Merge pull request #31 from tuna/dev

feat(worker): added username option for rsync providers
bigeagle 9 år sedan
förälder
incheckning
2ab9ed6e21
5 ändrade filer med 88 tillägg och 14 borttagningar
  1. 1 0
      worker/config.go
  2. 2 0
      worker/provider.go
  3. 66 1
      worker/provider_test.go
  4. 9 6
      worker/rsync_provider.go
  5. 10 7
      worker/two_stage_rsync_provider.go

+ 1 - 0
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"`
 }

+ 2 - 0
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,

+ 66 - 1
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")

+ 9 - 6
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
 	}

+ 10 - 7
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
 	}