-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_start.go
74 lines (62 loc) · 1.77 KB
/
vm_start.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
package cmd
import (
"fmt"
"os"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
// startCmd represents the start command
var vmStartCmd = &cobra.Command{
Use: "start <vm name | id>+",
Short: "Start virtual machine",
ValidArgsFunction: completeVMNames,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
rescueProfile, err := cmd.Flags().GetString("rescue-profile")
if err != nil {
return err
}
tasks := make([]task, 0, len(args))
for _, v := range args {
vm, err := getVirtualMachineByNameOrID(v)
if err != nil {
return err
}
state := (string)(egoscale.VirtualMachineStopped)
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.StartVirtualMachine{ID: vm.ID, RescueProfile: rescueProfile},
fmt.Sprintf("Starting %q ", vm.Name),
})
}
taskResponses := asyncTasks(tasks)
errors := filterErrors(taskResponses)
if len(errors) > 0 {
return errors[0]
}
return nil
},
}
// startVirtualMachine start a virtual machine instance Async
func startVirtualMachine(vmName string, vmRescueProfile string) error {
vm, err := getVirtualMachineByNameOrID(vmName)
if err != nil {
return err
}
state := (string)(egoscale.VirtualMachineStopped)
if vm.State != state {
return fmt.Errorf("%q is not in a %s state, got: %s", vmName, state, vm.State)
}
_, err = asyncRequest(&egoscale.StartVirtualMachine{ID: vm.ID, RescueProfile: vmRescueProfile},
fmt.Sprintf("Starting %q ", vm.Name))
return err
}
func init() {
vmStartCmd.Flags().StringP("rescue-profile", "", "", "option rescue profile when starting VM")
vmCmd.AddCommand(vmStartCmd)
}