func main() { flag.StringVar(&addr, "a", addr, "listen address") flag.StringVar(&dir, "d", dir, "manifest directory") flag.StringVar(&name, "n", name, "govisor name") flag.BoolVar(&enable, "e", enable, "enable all services") flag.Parse() m := govisor.NewManager(name) m.StartMonitoring() svcDir := path.Join(dir, "services") if d, e := os.Open(svcDir); e != nil { log.Fatalf("Failed to open services directory %s: %v", svcDir, e) } else if files, e := d.Readdirnames(-1); e != nil { log.Fatalf("Failed to scan scan services: %v", e) } else { for _, f := range files { fname := path.Join(svcDir, f) if mf, e := os.Open(fname); e != nil { log.Printf("Failed to open manifest %s: %v", fname, e) mf.Close() continue } else if p, e := govisor.NewProcessFromJson(mf); e != nil { log.Printf("Failed to load manifest %s: %v", fname, e) mf.Close() continue } else { m.AddService(p) mf.Close() } } } if enable { for _, s := range m.Services() { s.Enable() } } sigs := make(chan os.Signal, 1) done := make(chan bool, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) go func() { log.Fatal(http.ListenAndServe(addr, rpc.NewHandler(m))) }() // Set up a handler, so that we shutdown cleanly if possible. go func() { <-sigs done <- true }() // Wait for a termination signal, and shutdown cleanly if we get it. <-done m.Shutdown() os.Exit(1) }
func main() { dir := "." name := "govisord" enable := true passFile := "" genpass := "" certFile := "" keyFile := "" m := govisor.NewManager(name) flag.StringVar(&certFile, "certfile", certFile, "certificate file (for TLS)") flag.StringVar(&keyFile, "keyfile", keyFile, "key file (for TLS)") flag.StringVar(&addr, "addr", addr, "listen address") flag.StringVar(&dir, "dir", dir, "configuration directory") flag.StringVar(&name, "name", name, "govisor name") flag.BoolVar(&enable, "enable", enable, "enable all services") flag.StringVar(&passFile, "passfile", passFile, "password file") flag.StringVar(&genpass, "passwd", genpass, "generate password") flag.StringVar(&logFile, "logfile", logFile, "log file") flag.Parse() var lf *os.File var e error if logFile != "" { lf, e = os.OpenFile(logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644) if e != nil { die("Failed to open log file: %v", e) } log.SetOutput(lf) m.SetLogger(log.New(lf, "", log.LstdFlags)) } sigs := make(chan os.Signal, 1) done := make(chan bool, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP) h := &MyHandler{ h: server.NewHandler(m), name: name, auth: false, passwd: make(map[string]string), } if genpass != "" { h.auth = true rec := strings.SplitN(genpass, ":", 2) if len(rec) != 2 { die("Missing user:password") } enc, e := bcrypt.GenerateFromPassword([]byte(rec[1]), 0) if e != nil { die("bcrypt: %v", e) } h.passwd[rec[0]] = string(enc) log.Printf("Encrypted password is '%s'", string(enc)) } if passFile != "" { if e := h.loadPasswdFile(passFile); e != nil { die("Unable to load passwd file: %v", e) } } else if _, err := os.Stat(path.Join(dir, "passwd")); err == nil { if e := h.loadPasswdFile(path.Join(dir, "passwd")); e != nil { die("Unable to load passwd file: %v", e) } } if certFile == "" { certFile = path.Join(dir, "cert.pem") } if keyFile == "" { keyFile = path.Join(dir, "key.pem") } go func() { var e error if strings.HasPrefix(addr, "https://") { e = http.ListenAndServeTLS(addr[len("https://"):], certFile, keyFile, h) } else if strings.HasPrefix(addr, "http://") { e = http.ListenAndServe(addr[len("http://"):], h) } else { e = http.ListenAndServe(addr, h) } if e != nil { die("HTTP/HTTPS failed: %v", e) } }() /* This sleep is long enough to verify that our HTTP service started */ time.Sleep(time.Millisecond * 100) svcDir := path.Join(dir, "services") if d, e := os.Open(svcDir); e != nil { die("Failed to open services directory %s: %v", svcDir, e) } else if files, e := d.Readdirnames(-1); e != nil { die("Failed to scan scan services: %v", e) } else { for _, f := range files { fname := path.Join(svcDir, f) if mf, e := os.Open(fname); e != nil { log.Printf("Failed to open manifest %s: %v", fname, e) mf.Close() continue } else if p, e := govisor.NewProcessFromJson(mf); e != nil { log.Printf("Failed to load manifest %s: %v", fname, e) mf.Close() continue } else { m.AddService(p) mf.Close() } } } m.StartMonitoring() if enable { svcs, _, _ := m.Services() for _, s := range svcs { s.Enable() } } // Set up a handler, so that we shutdown cleanly if possible. go func() { <-sigs done <- true }() // Wait for a termination signal, and shutdown cleanly if we get it. <-done m.Shutdown() os.Exit(1) }