func mainDaemon(opts *Options) { config := opts.Config glog.V(1).Infof("The config file is %s", config) if config == "" { config = "/etc/hyper/config" } if _, err := os.Stat(config); err != nil { if os.IsNotExist(err) { glog.Errorf("Can not find config file(%s)", config) return } glog.Errorf(err.Error()) return } os.Setenv("HYPER_CONFIG", config) cfg, err := goconfig.LoadConfigFile(config) if err != nil { glog.Errorf("Read config file (%s) failed, %s", config, err.Error()) return } hyperRoot, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Root") if hyperRoot == "" { hyperRoot = "/var/lib/hyper" } utils.HYPER_ROOT = hyperRoot if _, err := os.Stat(hyperRoot); err != nil { if err := os.MkdirAll(hyperRoot, 0755); err != nil { glog.Errorf(err.Error()) return } } storageDriver, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "StorageDriver") if storageDriver != "" { graphdriver.DefaultDriver = storageDriver } docker.Init(strings.Split(opts.Mirrors, ","), strings.Split(opts.InsecureRegistries, ",")) eng := engine.New(config) d, err := daemon.NewDaemon(eng) if err != nil { glog.Errorf("The hyperd create failed, %s", err.Error()) return } driver, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Hypervisor") driver = strings.ToLower(driver) if hypervisor.HDriver, err = driverloader.Probe(driver); err != nil { glog.Warningf("%s", err.Error()) glog.Errorf("Please specify the correct and available hypervisor, such as 'kvm', 'qemu-kvm', 'libvirt', 'xen', 'qemu', 'vbox' or ''") return } else { d.Hypervisor = driver glog.Infof("The hypervisor's driver is %s", driver) } disableIptables := cfg.MustBool(goconfig.DEFAULT_SECTION, "DisableIptables", false) if err = hypervisor.InitNetwork(d.BridgeIface, d.BridgeIP, disableIptables || opts.DisableIptables); err != nil { glog.Errorf("InitNetwork failed, %s", err.Error()) return } defaultLog, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Logger") defaultLogCfg, _ := cfg.GetSection("Log") d.DefaultLogCfg(defaultLog, defaultLogCfg) // Set the daemon object as the global varibal // which will be used for puller and builder utils.SetDaemon(d) if err := d.DockerCli.Setup(); err != nil { glog.Error(err.Error()) return } stopAll := make(chan os.Signal, 1) signal.Notify(stopAll, syscall.SIGINT, syscall.SIGTERM) stop := make(chan os.Signal, 1) signal.Notify(stop, syscall.SIGHUP) // Install the accepted jobs if err := d.Install(eng); err != nil { glog.Errorf("The hyperd install failed, %s", err.Error()) return } glog.V(0).Infof("Hyper daemon: %s %s", utils.VERSION, utils.GITCOMMIT, ) // after the daemon is done setting up we can tell the api to start // accepting connections if err := eng.Job("acceptconnections").Run(); err != nil { glog.Error("the acceptconnections job run failed!") return } defaultHost := []string{} if opts.Hosts != "" { defaultHost = append(defaultHost, opts.Hosts) } defaultHost = append(defaultHost, "unix:///var/run/hyper.sock") if d.Host != "" { defaultHost = append(defaultHost, d.Host) } job := eng.Job("serveapi", defaultHost...) // The serve API job never exits unless an error occurs // We need to start it as a goroutine and wait on it so // daemon doesn't exit serveAPIWait := make(chan error) go func() { if err := job.Run(); err != nil { glog.Errorf("ServeAPI error: %v", err) serveAPIWait <- err return } serveAPIWait <- nil }() glog.V(0).Info("Daemon has completed initialization") if err := d.Restore(); err != nil { glog.Warningf("Fail to restore the previous VM") return } vmCachePolicy, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "VmCachePolicy") d.InitVmCache(vmCachePolicy) // Daemon is fully initialized and handling API traffic // Wait for serve API job to complete select { case errAPI := <-serveAPIWait: // If we have an error here it is unique to API (as daemonErr would have // exited the daemon process above) eng.Shutdown() if errAPI != nil { glog.Warningf("Shutting down due to ServeAPI error: %v", errAPI) } break case <-stop: d.DestroyAndKeepVm() eng.Shutdown() break case <-stopAll: d.DestroyAllVm() eng.Shutdown() break } }
func mainDaemon(config, host string, flDisableIptables bool) { glog.V(1).Infof("The config file is %s", config) if config == "" { config = "/etc/hyper/config" } if _, err := os.Stat(config); err != nil { if os.IsNotExist(err) { glog.Errorf("Can not find config file(%s)", config) return } glog.Errorf(err.Error()) return } os.Setenv("HYPER_CONFIG", config) cfg, err := goconfig.LoadConfigFile(config) if err != nil { glog.Errorf("Read config file (%s) failed, %s", config, err.Error()) return } driver, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Hypervisor") if runtime.GOOS == "darwin" { driver = "vbox" } else { if driver == "" { // We need to provide a defaut exec drvier var drivers = []string{"kvm", "xen", "vbox"} driver = utils.GetAvailableDriver(drivers) } driver = strings.ToLower(driver) } if driver == "" || (driver != "kvm" && driver != "xen" && driver != "vbox") { fmt.Printf("Please specify the exec driver, such as 'kvm', 'xen' or 'vbox'\n") return } hyperRoot, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Root") iso, _ := cfg.GetValue(goconfig.DEFAULT_SECTION, "Vbox") if hyperRoot == "" { hyperRoot = "/var/lib/hyper" } if _, err := os.Stat(hyperRoot); err != nil { if err := os.MkdirAll(hyperRoot, 0755); err != nil { glog.Errorf(err.Error()) return } } utils.SetHyperEnv(fmt.Sprintf("%s/.hyperconfig", os.Getenv("HOME")), hyperRoot, iso) eng := engine.New(config) docker.Init() d, err := daemon.NewDaemon(eng) if err != nil { glog.Errorf("The hyperd create failed, %s\n", err.Error()) return } if hypervisor.HDriver, err = driverloader.Probe(driver); err != nil { glog.Errorf("%s\n", err.Error()) return } d.Hypervisor = driver disableIptables := cfg.MustBool(goconfig.DEFAULT_SECTION, "DisableIptables", false) if err = hypervisor.InitNetwork(d.BridgeIface, d.BridgeIP, disableIptables || flDisableIptables); err != nil { glog.Errorf("InitNetwork failed, %s\n", err.Error()) return } // Set the daemon object as the global varibal // which will be used for puller and builder utils.SetDaemon(d) if err := d.DockerCli.Setup(); err != nil { glog.Error(err.Error()) return } stopAll := make(chan os.Signal, 1) signal.Notify(stopAll, syscall.SIGINT, syscall.SIGTERM) stop := make(chan os.Signal, 1) signal.Notify(stop, syscall.SIGHUP) // Install the accepted jobs if err := d.Install(eng); err != nil { glog.Errorf("The hyperd install failed, %s\n", err.Error()) return } glog.V(0).Infof("Hyper daemon: %s %s\n", utils.VERSION, utils.GITCOMMIT, ) // after the daemon is done setting up we can tell the api to start // accepting connections if err := eng.Job("acceptconnections").Run(); err != nil { glog.Error("the acceptconnections job run failed!\n") return } defaultHost := []string{} if host != "" { defaultHost = append(defaultHost, host) } defaultHost = append(defaultHost, "unix:///var/run/hyper.sock") if d.Host != "" { defaultHost = append(defaultHost, d.Host) } job := eng.Job("serveapi", defaultHost...) // The serve API job never exits unless an error occurs // We need to start it as a goroutine and wait on it so // daemon doesn't exit serveAPIWait := make(chan error) go func() { if err := job.Run(); err != nil { glog.Errorf("ServeAPI error: %v\n", err) serveAPIWait <- err return } serveAPIWait <- nil }() glog.V(0).Info("Daemon has completed initialization\n") if err := d.Restore(); err != nil { glog.Warningf("Fail to restore the previous VM") return } // Daemon is fully initialized and handling API traffic // Wait for serve API job to complete select { case errAPI := <-serveAPIWait: // If we have an error here it is unique to API (as daemonErr would have // exited the daemon process above) eng.Shutdown() if errAPI != nil { glog.Warningf("Shutting down due to ServeAPI error: %v\n", errAPI) } break case <-stop: d.DestroyAndKeepVm() eng.Shutdown() break case <-stopAll: d.DestroyAllVm() eng.Shutdown() break } }