func NewServer() (*Server, error) { cfg, err := config.New(os.ExpandEnv("$SUDO_USER")) if err != nil { return nil, err } err = cfg.Load() if err != nil { return nil, err } srv := &Server{ config: cfg, done: make(chan bool), Status: "The virtual machine has not been started", queue: []Message{}, } v := &VM{ server: srv, } srv.svc = v rpc.Register(v) rpc.HandleHTTP() return srv, nil }
func (c *VMCommand) Execute(args []string) error { client, err := rpc.NewClient(true) if err != nil { return err } cfg, err := config.New(os.ExpandEnv("$SUDO_USER")) if err != nil { return err } err = cfg.Load() if err != nil { return err } vm := vm.New(cfg) done, err := vm.Start() if err != nil { return err } shutdown := make(chan os.Signal, 1) signal.Notify(shutdown, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL) go func() { <-shutdown var i int client.Call("VM.Stop", "", &i) }() err = <-done return err }
func (c *RebuildCommand) Execute(args []string) error { var cfg *config.Config steps := Steps{ { "Reading configuration", func() error { cfg, err := config.New(os.ExpandEnv("$USER")) if err != nil { return err } return cfg.Load() }, }, { "Rebuilding disk image", func() error { if cfg.DiskSize != c.Disk { cfg.DiskSize = c.Disk err := cfg.Save() if err != nil { return err } } d := disk.New(cfg) d.Detach() return d.Create() }, }, } return Spin(steps) }
func (c *IPCommand) Execute(args []string) error { cfg, err := config.New(os.ExpandEnv("$USER")) if err != nil { return err } err = cfg.Load() if err != nil { return err } v := vm.New(cfg) ip, err := v.IP() if err != nil { return err } fmt.Println(ip) return nil }
func main() { cmd.SubcommandsOptional = true _, err := cmd.Parse() if err != nil { os.Exit(1) } if cmd.Command.Active == nil { cfg, err := config.New(os.ExpandEnv("$USER")) if err != nil { panic(err) } err = cfg.Load() if err != nil { panic(err) } if err != nil { if os.IsNotExist(err) { fmt.Println("DLite has not been installed. Please run 'sudo dlite install'") } else { fmt.Println(err) } os.Exit(1) } fmt.Println("DLite configuration:") fmt.Printf(" uuid : %s\n", cfg.Uuid) fmt.Printf(" cpu count : %d\n", cfg.CpuCount) fmt.Printf(" memory : %d GiB\n", cfg.Memory) fmt.Printf(" disk size : %d GiB\n", cfg.DiskSize) fmt.Printf(" hostname : %s\n", cfg.Hostname) fmt.Printf(" dns server : %s\n", cfg.DNSServer) fmt.Printf(" docker version : %s\n", cfg.DockerVersion) if cfg.Extra != "" { fmt.Printf(" docker args : %s\n", cfg.Extra) } } }
func (v *VM) Start(user string, _ *int) error { if v.server.cmd != nil && v.server.cmd.Process != nil { return fmt.Errorf("Virtual machine is already running") } v.server.Status = "The virtual machine is booting, please wait" cfg, err := config.New(user) if err != nil { return err } err = cfg.Load() if err != nil { return err } n := nfs.New(cfg) err = n.AddExport() if err != nil { return err } err = n.Start() if err != nil { return err } self, err := exec.LookPath(os.Args[0]) if err != nil { return err } c := exec.Command(self, "vm") v.server.cmd = c return c.Start() }
func (c *InstallCommand) Execute(args []string) error { versionMsg := "the latest version" if c.Version != "" { versionMsg = "version " + c.Version } fmt.Printf(` The install command will make the following changes to your system: - Create a '.dlite' directory in your home - Create a %d GiB sparse disk image in the '.dlite' directory - Download %s of DhyveOS to the '.dlite' directory - Create a 'config.json' file in the '.dlite' directory - Create a new SSH key pair in the '.dlite' directory for the vm - Create a launchd agent in '/Library/LaunchDaemons' used to run the privileged daemon - Store logs from the daemon in '/Library/Logs' - Create a launchd agent in '~/Library/LaunchAgents' used to start the privileged daemon under this user's context `, c.Disk, versionMsg) fmt.Print("Would you like to continue? (Y/n): ") reader := bufio.NewReader(os.Stdin) response, err := reader.ReadString('\n') if err != nil { return err } response = strings.ToLower(strings.TrimSpace(response)) if response == "n" || response == "no" { return fmt.Errorf("Aborted install due to user input") } if response != "" && response != "y" && response != "yes" { return fmt.Errorf("Aborted install due to invalid user input") } var cfg *config.Config steps := Steps{ { "Creating configuration", func() error { cfg, err := config.New(os.ExpandEnv("$USER")) if err != nil { return err } if c.DockerVersion == "" { latest, err := docker.Latest() if err != nil { return err } c.DockerVersion = latest } if c.Cpus == 0 { c.Cpus = runtime.NumCPU() } cfg.CpuCount = c.Cpus cfg.DockerVersion = c.DockerVersion cfg.Uuid = uuid.NewV1().String() cfg.Memory = c.Memory cfg.Hostname = c.Hostname cfg.DockerVersion = c.DockerVersion cfg.Extra = c.Extra cfg.DNSServer = c.DNSServer cfg.DiskSize = c.Disk cfg.Route = c.Route return cfg.Save() }, }, { "Building disk image", func() error { d := disk.New(cfg) d.Detach() return d.Create() }, }, { "Downloading OS", func() error { var vers dliteos.Version var err error if c.Version == "" { vers, err = dliteos.Latest() } else { vers, err = dliteos.Specific(c.Version) } if err != nil { return err } return dliteos.Download(cfg.Dir, vers) }, }, { "Generating SSH key", func() error { s := ssh.New(cfg) return s.Generate() }, }, } return Spin(steps) }