diff --git a/go/base/context.go b/go/base/context.go index ea5ab68..78a7a41 100644 --- a/go/base/context.go +++ b/go/base/context.go @@ -78,6 +78,7 @@ type MigrationContext struct { PostponeCutOverFlagFile string CutOverLockTimeoutSeconds int64 PanicFlagFile string + HooksPath string DropServeSocket bool ServeSocketFile string diff --git a/go/logic/hooks.go b/go/logic/hooks.go index 2d71dc7..c6c5981 100644 --- a/go/logic/hooks.go +++ b/go/logic/hooks.go @@ -9,6 +9,7 @@ import ( "fmt" "os" "os/exec" + "path/filepath" "github.com/github/gh-ost/go/base" "github.com/openark/golib/log" @@ -24,7 +25,7 @@ func NewHooksExecutor() *HooksExecutor { } } -func (this *HooksExecutor) detectHooks() error { +func (this *HooksExecutor) initHooks() error { return nil } @@ -39,9 +40,9 @@ func (this *HooksExecutor) applyEnvironmentVairables() []string { return env } -// commandRun executes a command with arguments, and set relevant environment variables -func (this *HooksExecutor) commandRun(commandText string, arguments ...string) error { - cmd := exec.Command(commandText, arguments...) +// executeHook executes a command with arguments, and set relevant environment variables +func (this *HooksExecutor) executeHook(hook string, arguments ...string) error { + cmd := exec.Command(hook, arguments...) cmd.Env = this.applyEnvironmentVairables() if err := cmd.Run(); err != nil { @@ -50,6 +51,28 @@ func (this *HooksExecutor) commandRun(commandText string, arguments ...string) e return nil } +func (this *HooksExecutor) detectHooks(baseName string) (hooks []string, err error) { + if this.migrationContext.HooksPath == "" { + return hooks, err + } + pattern := fmt.Sprintf("%s/%s*", this.migrationContext.HooksPath, baseName) + hooks, err = filepath.Glob(pattern) + return hooks, err +} + +func (this *HooksExecutor) executeHooks(baseName string) error { + hooks, err := this.detectHooks(baseName) + if err != nil { + return err + } + for _, hook := range hooks { + if err := this.executeHook(hook); err != nil { + return err + } + } + return nil +} + func (this *HooksExecutor) onStartup() error { return nil } diff --git a/go/logic/migrator.go b/go/logic/migrator.go index 2850e91..efad40a 100644 --- a/go/logic/migrator.go +++ b/go/logic/migrator.go @@ -114,7 +114,7 @@ func (this *Migrator) acceptSignals() { // initiateHooksExecutor func (this *Migrator) initiateHooksExecutor() (err error) { this.hooksExecutor = NewHooksExecutor() - if err := this.hooksExecutor.detectHooks(); err != nil { + if err := this.hooksExecutor.initHooks(); err != nil { return err } return nil