Example #1
0
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
}
Example #2
0
func (c *StatusCommand) Execute(args []string) error {
	client, err := rpc.NewClient(false)
	if err != nil {
		return err
	}

	status := args[0]
	var i int
	return client.Call("VM.SetStatus", status, &i)
}
Example #3
0
func (c *StopCommand) Execute(args []string) error {
	steps := Steps{
		{
			"Stopping virtual machine",
			func() error {
				client, err := rpc.NewClient(true)
				if err != nil {
					return err
				}

				var reply int
				return client.Call("VM.Stop", os.ExpandEnv("$USER"), &reply)
			},
		},
	}
	return Spin(steps)
}
Example #4
0
func (c *DaemonCommand) Execute(args []string) error {
	client, err := rpc.NewClient(false)
	if err != nil {
		return err
	}

	log.Println("Polling for commands from server..")
	for {
		time.Sleep(time.Second)
		var cmd rpc.Message
		err := client.Call("VM.GetCommand", "", &cmd)
		if err != nil {
			if e, ok := err.(net.Error); ok && e.Timeout() {
				continue
			}

			log.Println("Got an error:", err.Error())
			return err
		}

		log.Println("Attempting to process message:", cmd.Command)
		switch cmd.Command {
		case "shutdown":
			log.Println("Got shutdown command, halting virtual machine")
			var i int
			err = client.Call("VM.SetStatus", "The virtual machine is shutting down, please wait", &i)
			err = exec.Command("halt").Run()
			if err != nil {
				log.Println(err)
			}
		case "wait":
			continue
		default:
			log.Printf("Unknown command: %s\n", cmd)
		}
	}

	return nil
}