MCPcopy Index your code
hub / github.com/linuxkit/linuxkit / createOpenStackImage

Function createOpenStackImage

src/cmd/linuxkit/push_openstack.go:19–71  ·  view source on GitHub ↗
(filePath string, imageName string, client *gophercloud.ServiceClient)

Source from the content-addressed store, hash-verified

17)
18
19func 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
73func pushOpenstackCmd() *cobra.Command {
74 var (

Callers 1

pushOpenstackCmdFunction · 0.85

Calls 4

ExtractMethod · 0.65
OpenMethod · 0.65
CloseMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected