-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_stop.go
78 lines (65 loc) · 1.74 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package cmd
import (
"fmt"
"os"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// stopCmd represents the stop command
var vmStopCmd = &cobra.Command{
Use: "stop <vm name>+",
Short: "Stop virtual machine instance",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
tasks := make([]task, 0, len(args))
for _, v := range args {
if !force {
if !askQuestion(fmt.Sprintf("Are you sure you want to stop virtual machine %q?", v)) {
continue
}
}
vm, err := getVirtualMachineByNameOrID(v)
if err != nil {
return err
}
state := (string)(egoscale.VirtualMachineRunning)
if vm.State != state {
fmt.Fprintf(os.Stderr, "%q is not in a %s state, got: %s\n", vm.Name, state, vm.State)
continue
}
tasks = append(tasks, task{
egoscale.StopVirtualMachine{ID: vm.ID},
fmt.Sprintf("Stopping %q ", vm.Name),
})
}
taskResponses := asyncTasks(tasks)
errors := filterErrors(taskResponses)
if len(errors) > 0 {
return errors[0]
}
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() {
vmStopCmd.Flags().BoolP("force", "f", false, "Attempt to stop virtual machine without prompting for confirmation")
vmCmd.AddCommand(vmStopCmd)
}