-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_stop.go
63 lines (53 loc) · 1.27 KB
/
vm_stop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package cmd
import (
"errors"
"fmt"
"strings"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// stopCmd represents the stop command
var vmStopCmd = &cobra.Command{
Use: "stop <vm name> [vm name] ...",
Short: "Stop virtual machine instance",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
errs := []error{}
for _, v := range args {
if err := stopVirtualMachine(v); err != nil {
errs = append(errs, fmt.Errorf("could not stop %q: %s", v, err))
}
}
if len(errs) == 1 {
return errs[0]
}
if len(errs) > 1 {
var b strings.Builder
for _, err := range errs {
if _, e := fmt.Fprintln(&b, err); e != nil {
return e
}
}
return errors.New(b.String())
}
return nil
},
}
// stopVirtualMachine stop a virtual machine instance
func stopVirtualMachine(vmName string) error {
vm, err := getVirtualMachineByNameOrID(vmName)
if err != nil {
return err
}
state := (string)(egoscale.VirtualMachineRunning)
if vm.State != state {
return fmt.Errorf("%q is not in a %s state, got %s", vmName, state, vm.State)
}
_, err = asyncRequest(&egoscale.StopVirtualMachine{ID: vm.ID}, fmt.Sprintf("Stopping %q ", vm.Name))
return err
}
func init() {
vmCmd.AddCommand(vmStopCmd)
}