detecting and executing hooks

This commit is contained in:
Shlomi Noach 2016-08-20 08:24:20 +02:00
parent cdf393a30e
commit 6acbe7e3ae
3 changed files with 29 additions and 5 deletions

View File

@ -78,6 +78,7 @@ type MigrationContext struct {
PostponeCutOverFlagFile string
CutOverLockTimeoutSeconds int64
PanicFlagFile string
HooksPath string
DropServeSocket bool
ServeSocketFile string

View File

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

View File

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