-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_reboot.go
63 lines (53 loc) · 1.3 KB
/
vm_reboot.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"
)
// rebootCmd represents the reboot command
var vmRebootCmd = &cobra.Command{
Use: "reboot <vm name> [vm name] ...",
Short: "Reboot 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 := rebootVirtualMachine(v); err != nil {
errs = append(errs, fmt.Errorf("could not reboot %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
},
}
// rebootVirtualMachine reboot a virtual machine instance Async
func rebootVirtualMachine(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.RebootVirtualMachine{ID: vm.ID}, fmt.Sprintf("Rebooting %q ", vm.Name))
return err
}
func init() {
vmCmd.AddCommand(vmRebootCmd)
}