-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_start.go
56 lines (47 loc) · 1.22 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
package cmd
import (
"fmt"
"os"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var vmStartCmd = &cobra.Command{
Use: "start NAME|ID",
Short: "Start a stopped Compute instance",
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
},
}
func init() {
vmStartCmd.Flags().StringP("rescue-profile", "", "", "option rescue profile when starting VM")
vmCmd.AddCommand(vmStartCmd)
}