feat(worker): added username option for rsync providers

This commit is contained in:
bigeagle 2016-06-14 21:36:19 +08:00
parent 16c49b8083
commit ebeee6bc34
No known key found for this signature in database
GPG Key ID: 9171A4571C27920A
5 changed files with 88 additions and 14 deletions

View File

@ -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"`
}

View File

@ -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,

View File

@ -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")

View File

@ -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
}

View File

@ -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
}