-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_stop.go
62 lines (52 loc) · 1.26 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
package cmd
import (
"fmt"
"os"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var vmStopCmd = &cobra.Command{
Use: "stop NAME|ID",
Short: "Stop a running Compute instance",
ValidArgsFunction: completeVMNames,
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 Compute instance %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
},
}
func init() {
vmStopCmd.Flags().BoolP("force", "f", false, cmdFlagForceHelp)
vmCmd.AddCommand(vmStopCmd)
}