From bbd7a761cfed0e98da8136011f108d9ffda1f01e Mon Sep 17 00:00:00 2001 From: Dave Tucker Date: Tue, 17 May 2016 09:04:56 +0100 Subject: [PATCH] Make hupper watch for proxy changes too Signed-off-by: Dave Tucker --- alpine/packages/hupper/etc/init.d/hupper | 8 +- alpine/packages/hupper/main.go | 97 +++++++++++++++--------- 2 files changed, 65 insertions(+), 40 deletions(-) diff --git a/alpine/packages/hupper/etc/init.d/hupper b/alpine/packages/hupper/etc/init.d/hupper index 43b014ca1..9dfdcd2d1 100755 --- a/alpine/packages/hupper/etc/init.d/hupper +++ b/alpine/packages/hupper/etc/init.d/hupper @@ -13,16 +13,18 @@ start() PIDFILE=/run/hupper.pid DOCKERPIDFILE=/run/docker.pid - WATCH=$(mobyconfig watch /etc/docker/daemon.json) + WATCH_CONFIG=$(mobyconfig watch /etc/docker/daemon.json) + WATCH_PROXY=$(mobyconfig watch proxy) - [ -z "${WATCH}" ] && exit 1 + [ -z "${WATCH_CONFIG}" ] && exit 1 + [ -z "${WATCH_PROXY}" ] && exit 1 start-stop-daemon --start --quiet \ --background \ --exec /bin/hupper \ --make-pidfile --pidfile ${PIDFILE} \ -- -pidfile ${PIDFILE} -huppidfile ${DOCKERPIDFILE} \ - -path ${WATCH} + -path ${WATCH_CONFIG} -path ${WATCH_PROXY} eend $? "Failed to start hupper" } diff --git a/alpine/packages/hupper/main.go b/alpine/packages/hupper/main.go index 7005e6c37..4019d12c7 100644 --- a/alpine/packages/hupper/main.go +++ b/alpine/packages/hupper/main.go @@ -2,23 +2,36 @@ package main import ( "flag" + "fmt" "io" "io/ioutil" "log" "os" "os/exec" "strconv" + "sync" "syscall" ) var ( - path string + paths stringSlice huppidfile string pidfile string ) +type stringSlice []string + +func (s *stringSlice) String() string { + return fmt.Sprintf("%s", *s) +} + +func (s *stringSlice) Set(value string) error { + *s = append(*s, value) + return nil +} + func init() { - flag.StringVar(&path, "path", "", "path of the file to watch") + flag.Var(&paths, "path", "paths of the files to watch") flag.StringVar(&huppidfile, "huppidfile", "", "pidfile for process to signal") flag.StringVar(&pidfile, "pidfile", "", "my pidfile") } @@ -27,7 +40,7 @@ func main() { log.SetFlags(0) flag.Parse() - if path == "" { + if len(paths) < 1 { log.Fatal("watch path not set") } @@ -41,43 +54,53 @@ func main() { _ = ioutil.WriteFile(pidfile, pidbytes, 0644) } - watch, err := os.Open(path) - if err != nil { - log.Fatalln("Failed to open file", path, err) - } - // 43 bytes is the record size of the watch - buf := make([]byte, 43) - // initial state - _, err = watch.Read(buf) - if err != nil && err != io.EOF { - log.Fatalln("Error reading watch file", err) - } - for { - _, err := watch.Read(buf) + var wg sync.WaitGroup + wg.Add(len(paths)) + + for _, path := range paths { + watch, err := os.Open(path) + if err != nil { + log.Fatalln("Failed to open file", path, err) + } + // 43 bytes is the record size of the watch + buf := make([]byte, 43) + // initial state + _, err = watch.Read(buf) if err != nil && err != io.EOF { log.Fatalln("Error reading watch file", err) } - if err == io.EOF { - continue - } - // a few changes eg debug do not require a daemon restart - // however at present we cannot check changes, and most do - restart := true - if restart { - cmd := exec.Command("service", "docker", "restart") - // not much we can do if it does not restart - _ = cmd.Run() - } else { - bytes, err := ioutil.ReadFile(huppidfile) - if err != nil { - continue + + go func() { + for { + _, err := watch.Read(buf) + if err != nil && err != io.EOF { + log.Fatalln("Error reading watch file", err) + } + if err == io.EOF { + continue + } + // a few changes eg debug do not require a daemon restart + // however at present we cannot check changes, and most do + restart := true + if restart { + log.Println("Restarting docker") + cmd := exec.Command("service", "docker", "restart") + // not much we can do if it does not restart + _ = cmd.Run() + } else { + bytes, err := ioutil.ReadFile(huppidfile) + if err != nil { + continue + } + pidstring := string(bytes[:]) + pid, err := strconv.Atoi(pidstring) + if err != nil { + continue + } + syscall.Kill(pid, syscall.SIGHUP) + } } - pidstring := string(bytes[:]) - pid, err := strconv.Atoi(pidstring) - if err != nil { - continue - } - syscall.Kill(pid, syscall.SIGHUP) - } + }() } + wg.Wait() }