-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_reset.go
143 lines (117 loc) · 3.36 KB
/
vm_reset.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package cmd
import (
"fmt"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
var vmResetCmd = &cobra.Command{
Use: "reset NAME|ID",
Short: "Reset (reinstall) a Compute instance",
ValidArgsFunction: completeVMNames,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
diskValue, err := getInt64CustomFlag(cmd, "disk")
if err != nil {
return err
}
templateFilterCmd, err := cmd.Flags().GetString("template-filter")
if err != nil {
return err
}
templateFilter, err := validateTemplateFilter(templateFilterCmd)
if err != nil {
return err
}
template, err := cmd.Flags().GetString("template")
if err != nil {
return err
}
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 reset Compute instance %q?", v)) {
continue
}
}
task, err := makeResetVirtualMachineCMD(v, diskValue, template, templateFilter)
if err != nil {
return err
}
tasks = append(tasks, *task)
}
taskResponses := asyncTasks(tasks)
errors := filterErrors(taskResponses)
if len(errors) > 0 {
return errors[0]
}
return nil
},
}
func makeResetVirtualMachineCMD(vmName string, diskValue int64PtrValue, templateName string, templateFilter string) (*task, error) {
vm, err := getVirtualMachineByNameOrID(vmName)
if err != nil {
return nil, err
}
var template *egoscale.Template
if templateName != "" {
template, err = getTemplateByNameOrID(vm.ZoneID, templateName, templateFilter)
if err != nil {
return nil, err
}
} else {
resp, err := globalstate.EgoscaleClient.ListWithContext(gContext, egoscale.Template{
IsFeatured: true,
ID: vm.TemplateID,
ZoneID: vm.ZoneID,
})
if err != nil {
return nil, err
}
if len(resp) > 0 {
template = resp[0].(*egoscale.Template)
}
}
resp, err := globalstate.EgoscaleClient.GetWithContext(gContext, egoscale.Volume{
VirtualMachineID: vm.ID,
Type: "ROOT",
})
if err != nil {
return nil, fmt.Errorf("unable to retrieve Compute instance volume: %v", err)
}
volume := resp.(*egoscale.Volume)
volumeSize := volume.Size >> 30
rootDiskSize := int64(volumeSize)
if diskValue.int64 != nil {
if template != nil && *diskValue.int64 < (template.Size>>30) {
return nil, fmt.Errorf("root disk size must be equal or greater than %dGB", template.Size>>30)
}
rootDiskSize = *diskValue.int64
}
cmd := &egoscale.RestoreVirtualMachine{
VirtualMachineID: vm.ID,
RootDiskSize: rootDiskSize,
}
var msg string
if template != nil {
cmd.TemplateID = template.ID
msg = fmt.Sprintf("Resetting %q using %q", vm.Name, template.DisplayText)
} else {
msg = fmt.Sprintf("Resetting %q ", vm.Name)
}
return &task{cmd, msg}, nil
}
func init() {
vmCmd.AddCommand(vmResetCmd)
diskSizeVarP := new(int64PtrValue)
vmResetCmd.Flags().VarP(diskSizeVarP, "disk", "d", "New disk size after reset in GB")
vmResetCmd.Flags().StringP("template", "t", "", fmt.Sprintf("template NAME|ID (default: %s)", defaultTemplate))
vmResetCmd.Flags().StringP("template-filter", "", defaultTemplateFilter, templateFilterHelp)
vmResetCmd.Flags().BoolP("force", "f", false, cmdFlagForceHelp)
}