github.com/openshift/installer@v1.4.17/pkg/infrastructure/openstack/preprovision/rhcosimage.go (about)

     1  package preprovision
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/url"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/gophercloud/gophercloud/v2/openstack/image/v2/imagedata"
    11  	"github.com/gophercloud/gophercloud/v2/openstack/image/v2/images"
    12  	"github.com/sirupsen/logrus"
    13  
    14  	"github.com/openshift/installer/pkg/rhcos/cache"
    15  	openstackdefaults "github.com/openshift/installer/pkg/types/openstack/defaults"
    16  )
    17  
    18  // UploadBaseImage creates a new image in Glance and uploads the RHCOS image there.
    19  func UploadBaseImage(ctx context.Context, cloud string, rhcosImage string, imageName string, infraID string, imageProperties map[string]string) error {
    20  	url, err := url.Parse(rhcosImage)
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	// We support 'http(s)' and 'file' schemes. If the scheme is http(s), then we will upload a file from that
    26  	// location. Otherwise will take local file path from the URL.
    27  	var localFilePath string
    28  	switch url.Scheme {
    29  	case "http", "https":
    30  		localFilePath, err = cache.DownloadImageFile(rhcosImage, cache.InstallerApplicationName)
    31  		if err != nil {
    32  			return err
    33  		}
    34  	case "file":
    35  		localFilePath = filepath.FromSlash(url.Path)
    36  	default:
    37  		return fmt.Errorf("unsupported URL scheme: %q", url.Scheme)
    38  	}
    39  
    40  	logrus.Debugln("Creating a Glance image for RHCOS...")
    41  
    42  	f, err := os.Open(localFilePath)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	defer f.Close()
    47  
    48  	conn, err := openstackdefaults.NewServiceClient(ctx, "image", openstackdefaults.DefaultClientOpts(cloud))
    49  	if err != nil {
    50  		return err
    51  	}
    52  
    53  	// By default we use "qcow2" disk format, but if the file extension is "raw",
    54  	// then we set the disk format as "raw".
    55  	diskFormat := "qcow2"
    56  	if extension := filepath.Ext(localFilePath); extension == "raw" {
    57  		diskFormat = "raw"
    58  	}
    59  
    60  	img, err := images.Create(ctx, conn, images.CreateOpts{
    61  		Name:            imageName,
    62  		ContainerFormat: "bare",
    63  		DiskFormat:      diskFormat,
    64  		Tags:            []string{"openshiftClusterID=" + infraID},
    65  		Properties:      imageProperties,
    66  	}).Extract()
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	// Use direct upload (see
    72  	// https://github.com/openshift/installer/issues/3403 for a discussion
    73  	// on web-download)
    74  	logrus.Debugf("Upload RHCOS to the image %q (%s)", img.Name, img.ID)
    75  	res := imagedata.Upload(ctx, conn, img.ID, f)
    76  	if res.Err != nil {
    77  		return err
    78  	}
    79  	logrus.Debugf("RHCOS image upload completed.")
    80  
    81  	return nil
    82  }