-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathcreate.go
More file actions
401 lines (357 loc) · 13.5 KB
/
create.go
File metadata and controls
401 lines (357 loc) · 13.5 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package create
import (
"bufio"
"context"
"encoding/json"
goerrors "errors"
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
)
const (
nameFlag = "name"
diskFormatFlag = "disk-format"
localFilePathFlag = "local-file-path"
noProgressIndicatorFlag = "no-progress"
bootMenuFlag = "boot-menu"
cdromBusFlag = "cdrom-bus"
diskBusFlag = "disk-bus"
nicModelFlag = "nic-model"
operatingSystemFlag = "os"
operatingSystemDistroFlag = "os-distro"
operatingSystemVersionFlag = "os-version"
rescueBusFlag = "rescue-bus"
rescueDeviceFlag = "rescue-device"
secureBootFlag = "secure-boot"
uefiFlag = "uefi"
videoModelFlag = "video-model"
virtioScsiFlag = "virtio-scsi"
labelsFlag = "labels"
minDiskSizeFlag = "min-disk-size"
minRamFlag = "min-ram"
protectedFlag = "protected"
)
type imageConfig struct {
BootMenu *bool
CdromBus *string
DiskBus *string
NicModel *string
OperatingSystem *string
OperatingSystemDistro *string
OperatingSystemVersion *string
RescueBus *string
RescueDevice *string
SecureBoot *bool
Uefi bool
VideoModel *string
VirtioScsi *bool
}
type inputModel struct {
*globalflags.GlobalFlagModel
Id *string
Name string
DiskFormat string
LocalFilePath string
Labels *map[string]string
Config *imageConfig
MinDiskSize *int64
MinRam *int64
Protected *bool
NoProgressIndicator *bool
}
func NewCmd(params *params.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates images",
Long: "Creates images.",
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Create an image with name 'my-new-image' from a raw disk image located in '/my/raw/image'`,
`$ stackit image create --name my-new-image --disk-format=raw --local-file-path=/my/raw/image`,
),
examples.NewExample(
`Create an image with name 'my-new-image' from a qcow2 image read from '/my/qcow2/image' with labels describing its contents`,
`$ stackit image create --name my-new-image --disk-format=qcow2 --local-file-path=/my/qcow2/image --labels os=linux,distro=alpine,version=3.12`,
),
),
RunE: func(cmd *cobra.Command, _ []string) (err error) {
ctx := context.Background()
model, err := parseInput(params.Printer, cmd)
if err != nil {
return err
}
// Configure API client
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
// we open input file first to fail fast, if it is not readable
file, err := os.Open(model.LocalFilePath)
if err != nil {
return fmt.Errorf("create image: file %q is not readable: %w", model.LocalFilePath, err)
}
defer func() {
if inner := file.Close(); inner != nil {
err = fmt.Errorf("error closing input file: %w (%w)", inner, err)
}
}()
if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to create the image %q?", model.Name)
err = params.Printer.PromptForConfirmation(prompt)
if err != nil {
return err
}
}
// Call API
request := buildRequest(ctx, model, apiClient)
result, err := request.Execute()
if err != nil {
return fmt.Errorf("create image: %w", err)
}
model.Id = result.Id
url, ok := result.GetUploadUrlOk()
if !ok {
return fmt.Errorf("create image: no upload URL has been provided")
}
if err := uploadAsync(ctx, params.Printer, model, file, url); err != nil {
return err
}
if err := outputResult(params.Printer, model, result); err != nil {
return err
}
return nil
},
}
configureFlags(cmd)
return cmd
}
func uploadAsync(ctx context.Context, p *print.Printer, model *inputModel, file *os.File, url string) error {
stat, err := file.Stat()
if err != nil {
return fmt.Errorf("upload file: %w", err)
}
var reader io.Reader
if model.NoProgressIndicator != nil && *model.NoProgressIndicator {
reader = file
} else {
var ch <-chan int
reader, ch = newProgressReader(file)
go func() {
ticker := time.NewTicker(2 * time.Second)
var uploaded int
done:
for {
select {
case <-ticker.C:
p.Info("uploaded %3.1f%%\r", 100.0/float64(stat.Size())*float64(uploaded))
case n, ok := <-ch:
if !ok {
break done
}
if n >= 0 {
uploaded += n
}
}
}
}()
}
if err = uploadFile(ctx, p, reader, stat.Size(), url); err != nil {
return fmt.Errorf("upload file: %w", err)
}
return nil
}
var _ io.Reader = (*progressReader)(nil)
type progressReader struct {
delegate io.Reader
ch chan int
}
func newProgressReader(delegate io.Reader) (reader io.Reader, result <-chan int) {
ch := make(chan int)
return &progressReader{
delegate: delegate,
ch: ch,
}, ch
}
// Read implements io.Reader.
func (pr *progressReader) Read(p []byte) (int, error) {
n, err := pr.delegate.Read(p)
if goerrors.Is(err, io.EOF) && n <= 0 {
close(pr.ch)
} else {
pr.ch <- n
}
return n, err
}
func uploadFile(ctx context.Context, p *print.Printer, reader io.Reader, filesize int64, url string) error {
p.Debug(print.DebugLevel, "uploading image to %s", url)
start := time.Now()
// pass the file contents as stream, as they can get arbitrarily large. We do
// _not_ want to load them into an internal buffer. The downside is, that we
// have to set the content-length header manually
uploadRequest, err := http.NewRequestWithContext(ctx, http.MethodPut, url, bufio.NewReader(reader))
if err != nil {
return fmt.Errorf("create image: cannot create request: %w", err)
}
uploadRequest.Header.Add("Content-Type", "application/octet-stream")
uploadRequest.ContentLength = filesize
uploadResponse, err := http.DefaultClient.Do(uploadRequest)
if err != nil {
return fmt.Errorf("create image: error contacting server for upload: %w", err)
}
defer func() {
if inner := uploadResponse.Body.Close(); inner != nil {
err = fmt.Errorf("error closing file: %w (%w)", inner, err)
}
}()
if uploadResponse.StatusCode != http.StatusOK {
return fmt.Errorf("create image: server rejected image upload with %s", uploadResponse.Status)
}
delay := time.Since(start)
p.Debug(print.DebugLevel, "uploaded %d bytes in %v", filesize, delay)
return nil
}
func configureFlags(cmd *cobra.Command) {
cmd.Flags().String(nameFlag, "", "The name of the image.")
cmd.Flags().String(diskFormatFlag, "", "The disk format of the image. ")
cmd.Flags().String(localFilePathFlag, "", "The path to the local disk image file.")
cmd.Flags().Bool(noProgressIndicatorFlag, false, "Show no progress indicator for upload.")
cmd.Flags().Bool(bootMenuFlag, false, "Enables the BIOS bootmenu.")
cmd.Flags().String(cdromBusFlag, "", "Sets CDROM bus controller type.")
cmd.Flags().String(diskBusFlag, "", "Sets Disk bus controller type.")
cmd.Flags().String(nicModelFlag, "", "Sets virtual nic model.")
cmd.Flags().String(operatingSystemFlag, "", "Enables OS specific optimizations.")
cmd.Flags().String(operatingSystemDistroFlag, "", "Operating System Distribution.")
cmd.Flags().String(operatingSystemVersionFlag, "", "Version of the OS.")
cmd.Flags().String(rescueBusFlag, "", "Sets the device bus when the image is used as a rescue image.")
cmd.Flags().String(rescueDeviceFlag, "", "Sets the device when the image is used as a rescue image.")
cmd.Flags().Bool(secureBootFlag, false, "Enables Secure Boot.")
cmd.Flags().Bool(uefiFlag, true, "Enables UEFI boot.")
cmd.Flags().String(videoModelFlag, "", "Sets Graphic device model.")
cmd.Flags().Bool(virtioScsiFlag, false, "Enables the use of VirtIO SCSI to provide block device access. By default instances use VirtIO Block.")
cmd.Flags().StringToString(labelsFlag, nil, "Labels are key-value string pairs which can be attached to a network-interface. E.g. '--labels key1=value1,key2=value2,...'")
cmd.Flags().Int64(minDiskSizeFlag, 0, "Size in Gigabyte.")
cmd.Flags().Int64(minRamFlag, 0, "Size in Megabyte.")
cmd.Flags().Bool(protectedFlag, false, "Protected VM.")
if err := flags.MarkFlagsRequired(cmd, nameFlag, diskFormatFlag, localFilePathFlag); err != nil {
cobra.CheckErr(err)
}
cmd.MarkFlagsRequiredTogether(rescueBusFlag, rescueDeviceFlag)
}
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
}
name := flags.FlagToStringValue(p, cmd, nameFlag)
model := inputModel{
GlobalFlagModel: globalFlags,
Name: name,
DiskFormat: flags.FlagToStringValue(p, cmd, diskFormatFlag),
LocalFilePath: flags.FlagToStringValue(p, cmd, localFilePathFlag),
Labels: flags.FlagToStringToStringPointer(p, cmd, labelsFlag),
NoProgressIndicator: flags.FlagToBoolPointer(p, cmd, noProgressIndicatorFlag),
Config: &imageConfig{
BootMenu: flags.FlagToBoolPointer(p, cmd, bootMenuFlag),
CdromBus: flags.FlagToStringPointer(p, cmd, cdromBusFlag),
DiskBus: flags.FlagToStringPointer(p, cmd, diskBusFlag),
NicModel: flags.FlagToStringPointer(p, cmd, nicModelFlag),
OperatingSystem: flags.FlagToStringPointer(p, cmd, operatingSystemFlag),
OperatingSystemDistro: flags.FlagToStringPointer(p, cmd, operatingSystemDistroFlag),
OperatingSystemVersion: flags.FlagToStringPointer(p, cmd, operatingSystemVersionFlag),
RescueBus: flags.FlagToStringPointer(p, cmd, rescueBusFlag),
RescueDevice: flags.FlagToStringPointer(p, cmd, rescueDeviceFlag),
SecureBoot: flags.FlagToBoolPointer(p, cmd, secureBootFlag),
Uefi: flags.FlagToBoolValue(p, cmd, uefiFlag),
VideoModel: flags.FlagToStringPointer(p, cmd, videoModelFlag),
VirtioScsi: flags.FlagToBoolPointer(p, cmd, virtioScsiFlag),
},
MinDiskSize: flags.FlagToInt64Pointer(p, cmd, minDiskSizeFlag),
MinRam: flags.FlagToInt64Pointer(p, cmd, minRamFlag),
Protected: flags.FlagToBoolPointer(p, cmd, protectedFlag),
}
if p.IsVerbosityDebug() {
modelStr, err := print.BuildDebugStrFromInputModel(model)
if err != nil {
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
} else {
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
}
}
return &model, nil
}
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiCreateImageRequest {
request := apiClient.CreateImage(ctx, model.ProjectId).
CreateImagePayload(createPayload(ctx, model))
return request
}
func createPayload(_ context.Context, model *inputModel) iaas.CreateImagePayload {
payload := iaas.CreateImagePayload{
DiskFormat: &model.DiskFormat,
Name: &model.Name,
Labels: utils.ConvertStringMapToInterfaceMap(model.Labels),
MinDiskSize: model.MinDiskSize,
MinRam: model.MinRam,
Protected: model.Protected,
}
if model.Config != nil {
payload.Config = &iaas.ImageConfig{
BootMenu: model.Config.BootMenu,
CdromBus: iaas.NewNullableString(model.Config.CdromBus),
DiskBus: iaas.NewNullableString(model.Config.DiskBus),
NicModel: iaas.NewNullableString(model.Config.NicModel),
OperatingSystem: model.Config.OperatingSystem,
OperatingSystemDistro: iaas.NewNullableString(model.Config.OperatingSystemDistro),
OperatingSystemVersion: iaas.NewNullableString(model.Config.OperatingSystemVersion),
RescueBus: iaas.NewNullableString(model.Config.RescueBus),
RescueDevice: iaas.NewNullableString(model.Config.RescueDevice),
SecureBoot: model.Config.SecureBoot,
Uefi: utils.Ptr(model.Config.Uefi),
VideoModel: iaas.NewNullableString(model.Config.VideoModel),
VirtioScsi: model.Config.VirtioScsi,
}
}
return payload
}
func outputResult(p *print.Printer, model *inputModel, resp *iaas.ImageCreateResponse) error {
if model == nil {
return fmt.Errorf("input model is nil")
}
var outputFormat string
if model.GlobalFlagModel != nil {
outputFormat = model.OutputFormat
}
switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return fmt.Errorf("marshal image: %w", err)
}
p.Outputln(string(details))
return nil
case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal image: %w", err)
}
p.Outputln(string(details))
return nil
default:
p.Outputf("Created image %q with id %s\n", model.Name, utils.PtrString(model.Id))
return nil
}
}