Merge pull request #40 from tuna/dev

feat(worker): added global `exec_on_success` and `exec_on_failure` op…
This commit is contained in:
bigeagle 2016-10-21 01:27:56 +08:00 committed by GitHub
commit 22a7f867ae
3 changed files with 41 additions and 18 deletions

View File

@ -47,6 +47,9 @@ type globalConfig struct {
MirrorDir string `toml:"mirror_dir"` MirrorDir string `toml:"mirror_dir"`
Concurrent int `toml:"concurrent"` Concurrent int `toml:"concurrent"`
Interval int `toml:"interval"` Interval int `toml:"interval"`
ExecOnSuccess []string `toml:"exec_on_success"`
ExecOnFailure []string `toml:"exec_on_failure"`
} }
type managerConfig struct { type managerConfig struct {
@ -87,8 +90,13 @@ type mirrorConfig struct {
Env map[string]string `toml:"env"` Env map[string]string `toml:"env"`
Role string `toml:"role"` Role string `toml:"role"`
ExecOnSuccess string `toml:"exec_on_success"` // These two options over-write the global options
ExecOnFailure string `toml:"exec_on_failure"` ExecOnSuccess []string `toml:"exec_on_success"`
ExecOnFailure []string `toml:"exec_on_failure"`
// These two options the global options
ExecOnSuccessExtra []string `toml:"exec_on_success_extra"`
ExecOnFailureExtra []string `toml:"exec_on_failure_extra"`
Command string `toml:"command"` Command string `toml:"command"`
UseIPv6 bool `toml:"use_ipv6"` UseIPv6 bool `toml:"use_ipv6"`

View File

@ -36,7 +36,9 @@ provider = "command"
upstream = "https://aosp.google.com/" upstream = "https://aosp.google.com/"
interval = 720 interval = 720
mirror_dir = "/data/git/AOSP" mirror_dir = "/data/git/AOSP"
exec_on_success = "bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'" exec_on_success = [
"bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'"
]
[mirrors.env] [mirrors.env]
REPO = "/usr/local/bin/aosp-repo" REPO = "/usr/local/bin/aosp-repo"
@ -53,7 +55,9 @@ provider = "rsync"
upstream = "rsync://ftp.fedoraproject.org/fedora/" upstream = "rsync://ftp.fedoraproject.org/fedora/"
use_ipv6 = true use_ipv6 = true
exclude_file = "/etc/tunasync.d/fedora-exclude.txt" exclude_file = "/etc/tunasync.d/fedora-exclude.txt"
exec_on_failure = "bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'" exec_on_failure = [
"bash -c 'echo ${TUNASYNC_JOB_EXIT_STATUS} > ${TUNASYNC_WORKING_DIR}/exit_status'"
]
` `
Convey("When giving invalid file", t, func() { Convey("When giving invalid file", t, func() {

View File

@ -169,24 +169,35 @@ func newMirrorProvider(mirror mirrorConfig, cfg *Config) mirrorProvider {
) )
} }
addHookFromCmdList := func(cmdList []string, execOn uint8) {
if execOn != execOnSuccess && execOn != execOnFailure {
panic("Invalid option for exec-on")
}
for _, cmd := range cmdList {
h, err := newExecPostHook(provider, execOn, cmd)
if err != nil {
logger.Errorf("Error initializing mirror %s: %s", mirror.Name, err.Error())
panic(err)
}
provider.AddHook(h)
}
}
// ExecOnSuccess hook // ExecOnSuccess hook
if mirror.ExecOnSuccess != "" { if len(mirror.ExecOnSuccess) > 0 {
h, err := newExecPostHook(provider, execOnSuccess, mirror.ExecOnSuccess) addHookFromCmdList(mirror.ExecOnSuccess, execOnSuccess)
if err != nil { } else {
logger.Errorf("Error initializing mirror %s: %s", mirror.Name, err.Error()) addHookFromCmdList(cfg.Global.ExecOnSuccess, execOnSuccess)
panic(err)
}
provider.AddHook(h)
} }
addHookFromCmdList(mirror.ExecOnSuccessExtra, execOnSuccess)
// ExecOnFailure hook // ExecOnFailure hook
if mirror.ExecOnFailure != "" { if len(mirror.ExecOnFailure) > 0 {
h, err := newExecPostHook(provider, execOnFailure, mirror.ExecOnFailure) addHookFromCmdList(mirror.ExecOnFailure, execOnFailure)
if err != nil { } else {
logger.Errorf("Error initializing mirror %s: %s", mirror.Name, err.Error()) addHookFromCmdList(cfg.Global.ExecOnFailure, execOnFailure)
panic(err)
}
provider.AddHook(h)
} }
addHookFromCmdList(mirror.ExecOnFailureExtra, execOnFailure)
return provider return provider
} }