feature(worker): worker config file

This commit is contained in:
bigeagle 2016-04-24 21:13:11 +08:00
parent b077db1d0b
commit d8b45d7231
No known key found for this signature in database
GPG Key ID: 9171A4571C27920A
2 changed files with 178 additions and 0 deletions

90
worker/config.go Normal file
View File

@ -0,0 +1,90 @@
package worker
import (
"errors"
"os"
"github.com/BurntSushi/toml"
)
type ProviderEnum uint8
const (
ProvRsync ProviderEnum = iota
ProvTwoStageRsync
ProvCommand
)
func (p ProviderEnum) MarshalText() ([]byte, error) {
switch p {
case ProvCommand:
return []byte("command"), nil
case ProvRsync:
return []byte("rsync"), nil
case ProvTwoStageRsync:
return []byte("two-stage-rsync"), nil
default:
return []byte{}, errors.New("Invalid ProviderEnum value")
}
}
func (p *ProviderEnum) UnmarshalText(text []byte) error {
s := string(text)
switch s {
case `command`:
*p = ProvCommand
case `rsync`:
*p = ProvRsync
case `two-stage-rsync`:
*p = ProvTwoStageRsync
default:
return errors.New("Invalid value to provierEnum")
}
return nil
}
type Config struct {
Global globalConfig `toml:"global"`
Mirrors []mirrorConfig `toml:"mirrors"`
}
type globalConfig struct {
Name string `toml:"name"`
Token string `toml:"token"`
LogDir string `toml:"log_dir"`
MirrorDir string `toml:"mirror_dir"`
Concurrent int `toml:"concurrent"`
Interval int `toml:"interval"`
}
type mirrorConfig struct {
Name string `toml:"name"`
Provider ProviderEnum `toml:"provider"`
Upstream string `toml:"upstream"`
Interval int `toml:"interval"`
MirrorDir string `toml:"mirror_dir"`
LogDir string `toml:"log_dir"`
Env map[string]string `toml:"env"`
Command string `toml:"command"`
UseIPv6 bool `toml:"use_ipv6"`
ExcludeFile string `toml:"exclude_file"`
Password string `toml:"password"`
Stage1Profile string `toml:"stage1_profile"`
}
func loadConfig(cfgFile string) (*Config, error) {
if _, err := os.Stat(cfgFile); err != nil {
return nil, err
}
cfg := new(Config)
if _, err := toml.DecodeFile(cfgFile, cfg); err != nil {
logger.Error(err.Error())
return nil, err
}
return cfg, nil
}

88
worker/config_test.go Normal file
View File

@ -0,0 +1,88 @@
package worker
import (
"io/ioutil"
"os"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestConfig(t *testing.T) {
var cfgBlob = `
[global]
name = "test_worker"
token = "some_token"
log_dir = "/var/log/tunasync"
mirror_dir = "/data/mirrors"
concurrent = 10
interval = 240
[[mirrors]]
name = "AOSP"
provider = "command"
upstream = "https://aosp.google.com/"
interval = 720
mirror_dir = "/data/git/AOSP"
[mirrors.env]
REPO = "/usr/local/bin/aosp-repo"
[[mirrors]]
name = "debian"
provider = "two-stage-rsync"
stage1_profile = "debian"
upstream = "rsync://ftp.debian.org/debian/"
use_ipv6 = true
[[mirrors]]
name = "fedora"
provider = "rsync"
upstream = "rsync://ftp.fedoraproject.org/fedora/"
use_ipv6 = true
exclude_file = "/etc/tunasync.d/fedora-exclude.txt"
`
Convey("When giving invalid file", t, func() {
cfg, err := loadConfig("/path/to/invalid/file")
So(err, ShouldNotBeNil)
So(cfg, ShouldBeNil)
})
Convey("Everything should work on valid config file", t, func() {
tmpfile, err := ioutil.TempFile("", "tunasync")
So(err, ShouldEqual, nil)
defer os.Remove(tmpfile.Name())
err = ioutil.WriteFile(tmpfile.Name(), []byte(cfgBlob), 0644)
So(err, ShouldEqual, nil)
defer tmpfile.Close()
cfg, err := loadConfig(tmpfile.Name())
So(err, ShouldBeNil)
So(cfg.Global.Name, ShouldEqual, "test_worker")
So(cfg.Global.Interval, ShouldEqual, 240)
So(cfg.Global.MirrorDir, ShouldEqual, "/data/mirrors")
m := cfg.Mirrors[0]
So(m.Name, ShouldEqual, "AOSP")
So(m.MirrorDir, ShouldEqual, "/data/git/AOSP")
So(m.Provider, ShouldEqual, ProvCommand)
So(m.Interval, ShouldEqual, 720)
So(m.Env["REPO"], ShouldEqual, "/usr/local/bin/aosp-repo")
m = cfg.Mirrors[1]
So(m.Name, ShouldEqual, "debian")
So(m.MirrorDir, ShouldEqual, "")
So(m.Provider, ShouldEqual, ProvTwoStageRsync)
m = cfg.Mirrors[2]
So(m.Name, ShouldEqual, "fedora")
So(m.MirrorDir, ShouldEqual, "")
So(m.Provider, ShouldEqual, ProvRsync)
So(m.ExcludeFile, ShouldEqual, "/etc/tunasync.d/fedora-exclude.txt")
So(len(cfg.Mirrors), ShouldEqual, 3)
})
}