func startDaemon(c *cli.Context) { defer shutdown.Exit() confPath := c.GlobalString("config") port := c.String("port") dev := c.Bool("dev") logger := setupLogger(c.String("log-dir")) conf, err := config.Ensure(confPath, c.String("host-key"), c.String("home")) if err != nil { if os.IsExist(err) { log.Fatal("Host is already running") } log.Fatal(err) } client, err := docker.NewClientFromEnv() if err != nil { log.Fatal(err) } hostAPI := api.New(client, conf, logger) shutdown.BeforeExit(func() { log.Println("Cleaning up...") if err := hostAPI.StopDB(); err != nil { log.Printf("Error stopping host DB: %v", err) } if err := conf.Cleanup(); err != nil { log.Printf("Error cleaning config: %v", err) } }) if dev { log.Printf("Starting host, dev mode\n") } else { log.Printf("Starting host\n") } if err := hostAPI.StartDB(dev); err != nil { log.Printf("Error starting host DB: %v", err) return } if err := http.Serve(port, hostAPI, conf, logger); err != nil { log.Printf("HTTP API error: %v", err) } }
func setAPI(c *cli.Context) error { conf, err := config.Open(c.GlobalString("config")) if err != nil { log.Fatal("Host is not running or invalid config file") } client, err := docker.NewClientFromEnv() if err != nil { return err } host = api.New(client, conf, log.New(os.Stderr, "", log.Flags())) if err := host.ConnectDB(); err != nil { return err } return nil }