(filePath string, imageName string, client *gophercloud.ServiceClient)
| 17 | ) |
| 18 | |
| 19 | func createOpenStackImage(filePath string, imageName string, client *gophercloud.ServiceClient) { |
| 20 | // Image formats that are supported by both LinuxKit and OpenStack Glance V2 |
| 21 | formats := []string{"ami", "vhd", "vhdx", "vmdk", "raw", "qcow2", "iso"} |
| 22 | |
| 23 | // Find extension of the filename and remove the leading stop |
| 24 | fileExtension := strings.ReplaceAll(path.Ext(filePath), ".", "") |
| 25 | fileName := strings.TrimSuffix(path.Base(filePath), filepath.Ext(filePath)) |
| 26 | // Check for Supported extension |
| 27 | var supportedExtension bool |
| 28 | supportedExtension = false |
| 29 | for i := 0; i < len(formats); i++ { |
| 30 | if strings.ContainsAny(fileExtension, formats[i]) { |
| 31 | supportedExtension = true |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | if !supportedExtension { |
| 36 | log.Fatalf("Extension [%s] is not supported", fileExtension) |
| 37 | } |
| 38 | |
| 39 | if imageName == "" { |
| 40 | imageName = fileName |
| 41 | } |
| 42 | |
| 43 | imageOpts := images.CreateOpts{ |
| 44 | Name: imageName, |
| 45 | ContainerFormat: "bare", |
| 46 | DiskFormat: fileExtension, |
| 47 | } |
| 48 | image, err := images.Create(client, imageOpts).Extract() |
| 49 | if err != nil { |
| 50 | log.Fatalf("Error creating image: %s", err) |
| 51 | } |
| 52 | |
| 53 | f, err := os.Open(filePath) |
| 54 | if err != nil { |
| 55 | log.Fatalf("Can't read image file: %s", err) |
| 56 | } |
| 57 | defer func() { _ = f.Close() }() |
| 58 | |
| 59 | log.Infof("Uploading file %s with Image ID %s", filePath, image.ID) |
| 60 | imagedata.Upload(client, image.ID, f) |
| 61 | |
| 62 | // Validate the uploaded image. If it's anything other than 'active' |
| 63 | // then there's been a problem |
| 64 | validImage, _ := images.Get(client, image.ID).Extract() |
| 65 | if validImage.Status != "active" { |
| 66 | log.Fatalf("error uploading image, status is %s", validImage.Status) |
| 67 | } else { |
| 68 | log.Infof("Image uploaded successfully!") |
| 69 | fmt.Println(image.ID) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func pushOpenstackCmd() *cobra.Command { |
| 74 | var ( |
no test coverage detected