-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm_create.go
222 lines (184 loc) · 5.56 KB
/
vm_create.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/egoscale"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/utils"
)
var vmCreateCmd = &cobra.Command{
Use: "create NAME",
Short: "Deploy a Compute instance",
Long: fmt.Sprintf(`This command deploys a new Compute instance.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&vmShowOutput{}), ", ")),
Aliases: gCreateAlias,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{"zone"})
},
RunE: func(cmd *cobra.Command, args []string) error {
vmName := args[0]
userDataPath, err := cmd.Flags().GetString("cloud-init-file")
if err != nil {
return err
}
userDataCompress, err := cmd.Flags().GetBool("cloud-init-compress")
if err != nil {
return err
}
userData := ""
if userDataPath != "" {
userData, err = getUserDataFromFile(userDataPath, userDataCompress)
if err != nil {
return err
}
}
zoneName, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
zone, err := getZoneByNameOrID(zoneName)
if err != nil {
return err
}
templateFilter, err := cmd.Flags().GetString("template-filter")
if err != nil {
return err
}
if templateFilter, err = validateTemplateFilter(templateFilter); err != nil {
return err
}
templateName, err := cmd.Flags().GetString("template")
if err != nil {
return err
}
template, err := getTemplateByNameOrID(zone.ID, templateName, templateFilter)
if err != nil {
return err
}
diskSize, err := cmd.Flags().GetInt64("disk")
if err != nil {
return err
}
keypair, err := cmd.Flags().GetString("keypair")
if err != nil {
return err
}
if keypair == "" {
keypair = account.CurrentAccount.DefaultSSHKey
}
sg, err := cmd.Flags().GetStringSlice("security-group")
if err != nil {
return err
}
sgs, err := getSecurityGroupIDs(sg)
if err != nil {
return err
}
ipv6, err := cmd.Flags().GetBool("ipv6")
if err != nil {
return err
}
privnets, err := cmd.Flags().GetStringSlice("privnet")
if err != nil {
return err
}
pvs, err := getPrivnetIDs(privnets, zone.ID)
if err != nil {
return err
}
so, err := cmd.Flags().GetString("service-offering")
if err != nil {
return err
}
servOffering, err := getServiceOfferingByNameOrID(so)
if err != nil {
return err
}
antiAffinityGroups, err := cmd.Flags().GetStringSlice("anti-affinity-group")
if err != nil {
return err
}
aags, err := getAffinityGroupIDs(antiAffinityGroups)
if err != nil {
return err
}
vm, err := createVM(&egoscale.DeployVirtualMachine{
Name: vmName,
UserData: userData,
ZoneID: zone.ID,
TemplateID: template.ID,
RootDiskSize: diskSize,
KeyPair: keypair,
SecurityGroupIDs: sgs,
IP6: &ipv6,
NetworkIDs: pvs,
ServiceOfferingID: servOffering.ID,
AffinityGroupIDs: aags,
})
if err != nil {
return err
}
if !globalstate.Quiet {
return printOutput(showVM(vm.ID.String()))
}
return nil
},
}
func createVM(deployVM *egoscale.DeployVirtualMachine) (*egoscale.VirtualMachine, error) {
var (
sshKey *egoscale.SSHKeyPair
singleUseSSHKey bool
)
// If not SSH key is specified, create a single-use SSH key, store the private key locally
// and delete the public key from the API once the Instance has been deployed.
if deployVM.KeyPair == "" {
singleUseSSHKey = true
if !globalstate.Quiet {
fmt.Fprintln(os.Stderr, "Creating single-use SSH key")
}
keyName, err := utils.RandStringBytes(64)
if err != nil {
return nil, err
}
sshKey, err = createSSHKey(keyName)
if err != nil {
return nil, fmt.Errorf("error creating single-use SSH keypair: %s", err)
}
deployVM.KeyPair = sshKey.Name
defer deleteSSHKey(sshKey.Name) // nolint: errcheck
}
resp := asyncTasks([]task{{deployVM, fmt.Sprintf("Deploying %q", deployVM.Name)}})
errors := filterErrors(resp)
if len(errors) > 0 {
return nil, errors[0]
}
vm := resp[0].resp.(*egoscale.VirtualMachine)
if singleUseSSHKey {
saveKeyPair(sshKey, *vm.ID)
}
return vm, nil
}
func init() {
vmCreateCmd.Flags().StringP(zoneFlagLong, zoneFlagShort, "", zoneFlagMsg)
vmCreateCmd.Flags().StringP("template", "t", defaultTemplate, "template NAME|ID")
vmCreateCmd.Flags().StringP("template-filter", "", defaultTemplateFilter, templateFilterHelp)
vmCreateCmd.Flags().StringP("service-offering", "o", defaultServiceOffering, serviceOfferingHelp)
vmCreateCmd.Flags().Int64P("disk", "d", 50, "disk size")
vmCreateCmd.Flags().StringP("keypair", "k", "", "SSH keypair name. If not specified, a single-use SSH key will be created.")
vmCreateCmd.Flags().StringSliceP("security-group", "s", nil, "Security Group NAME|ID. Can be specified multiple times.")
vmCreateCmd.Flags().StringSliceP("privnet", "p", nil, "Private Network NAME|ID. Can be specified multiple times.")
vmCreateCmd.Flags().StringSliceP("anti-affinity-group", "a", nil, "Anti-Affinity Group NAME|ID. Can be specified multiple times.")
vmCreateCmd.Flags().StringP("cloud-init-file", "f", "", "instance cloud-init userdata")
vmCreateCmd.Flags().BoolP("cloud-init-compress", "", false, "compress instance cloud-init user data")
vmCreateCmd.Flags().BoolP("ipv6", "6", false, "enable IPv6")
vmCmd.AddCommand(vmCreateCmd)
}