From 2b83ea4b455b7778aca97c9c90a538c82bc9803a Mon Sep 17 00:00:00 2001 From: bigeagle Date: Fri, 21 Oct 2016 01:20:31 +0800 Subject: [PATCH] feat(worker): added global `exec_on_success` and `exec_on_failure` options if `exec_on_success` is set on mirror-level config, it overrides the global option; if on mirror level, extra hook cmd is needed, use `exec_on_success_extra` option --- worker/config.go | 12 ++++++++++-- worker/config_test.go | 8 ++++++-- worker/provider.go | 39 +++++++++++++++++++++++++-------------- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/worker/config.go b/worker/config.go index 6f162f0..6f3a636 100644 --- a/worker/config.go +++ b/worker/config.go @@ -47,6 +47,9 @@ type globalConfig struct { MirrorDir string `toml:"mirror_dir"` Concurrent int `toml:"concurrent"` Interval int `toml:"interval"` + + ExecOnSuccess []string `toml:"exec_on_success"` + ExecOnFailure []string `toml:"exec_on_failure"` } type managerConfig struct { @@ -87,8 +90,13 @@ type mirrorConfig struct { Env map[string]string `toml:"env"` Role string `toml:"role"` - ExecOnSuccess string `toml:"exec_on_success"` - ExecOnFailure string `toml:"exec_on_failure"` + // These two options over-write the global options + 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"` UseIPv6 bool `toml:"use_ipv6"` diff --git a/worker/config_test.go b/worker/config_test.go index 1fb4488..abed134 100644 --- a/worker/config_test.go +++ b/worker/config_test.go @@ -36,7 +36,9 @@ provider = "command" upstream = "https://aosp.google.com/" interval = 720 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] REPO = "/usr/local/bin/aosp-repo" @@ -53,7 +55,9 @@ provider = "rsync" upstream = "rsync://ftp.fedoraproject.org/fedora/" use_ipv6 = true 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() { diff --git a/worker/provider.go b/worker/provider.go index 923cf2c..8358eeb 100644 --- a/worker/provider.go +++ b/worker/provider.go @@ -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 - if mirror.ExecOnSuccess != "" { - h, err := newExecPostHook(provider, execOnSuccess, mirror.ExecOnSuccess) - if err != nil { - logger.Errorf("Error initializing mirror %s: %s", mirror.Name, err.Error()) - panic(err) - } - provider.AddHook(h) + if len(mirror.ExecOnSuccess) > 0 { + addHookFromCmdList(mirror.ExecOnSuccess, execOnSuccess) + } else { + addHookFromCmdList(cfg.Global.ExecOnSuccess, execOnSuccess) } + addHookFromCmdList(mirror.ExecOnSuccessExtra, execOnSuccess) + // ExecOnFailure hook - if mirror.ExecOnFailure != "" { - h, err := newExecPostHook(provider, execOnFailure, mirror.ExecOnFailure) - if err != nil { - logger.Errorf("Error initializing mirror %s: %s", mirror.Name, err.Error()) - panic(err) - } - provider.AddHook(h) + if len(mirror.ExecOnFailure) > 0 { + addHookFromCmdList(mirror.ExecOnFailure, execOnFailure) + } else { + addHookFromCmdList(cfg.Global.ExecOnFailure, execOnFailure) } + addHookFromCmdList(mirror.ExecOnFailureExtra, execOnFailure) return provider }