github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/virtualbox/stage.go (about)

     1  package virtualbox
     2  
     3  import (
     4  	"github.com/sirupsen/logrus"
     5  	"github.com/emc-advanced-dev/pkg/errors"
     6  	"github.com/solo-io/unik/pkg/providers/common"
     7  	"github.com/solo-io/unik/pkg/types"
     8  	"os"
     9  	"path/filepath"
    10  	"time"
    11  )
    12  
    13  func (p *VirtualboxProvider) Stage(params types.StageImageParams) (_ *types.Image, err error) {
    14  	images, err := p.ListImages()
    15  	if err != nil {
    16  		return nil, errors.New("retrieving image list for existing image", err)
    17  	}
    18  	for _, image := range images {
    19  		if image.Name == params.Name {
    20  			if !params.Force {
    21  				return nil, errors.New("an image already exists with name '"+params.Name+"', try again with --force", nil)
    22  			} else {
    23  				logrus.WithField("image", image).Warnf("force: deleting previous image with name " + params.Name)
    24  				if err := p.DeleteImage(image.Id, true); err != nil {
    25  					logrus.Warn(errors.New("failed removing previously existing image", err))
    26  				}
    27  			}
    28  		}
    29  	}
    30  	imagePath := getImagePath(params.Name)
    31  	logrus.Debugf("making directory: %s", filepath.Dir(imagePath))
    32  	if err := os.MkdirAll(filepath.Dir(imagePath), 0755); err != nil {
    33  		return nil, errors.New("creating directory for boot image", err)
    34  	}
    35  	defer func() {
    36  		if err != nil && !params.NoCleanup {
    37  			os.RemoveAll(filepath.Dir(imagePath))
    38  		}
    39  	}()
    40  
    41  	logrus.WithField("raw-image", params.RawImage).Infof("creating boot volume from raw image")
    42  	if err := common.ConvertRawImage(params.RawImage.StageSpec.ImageFormat, types.ImageFormat_VMDK, params.RawImage.LocalImagePath, imagePath); err != nil {
    43  		return nil, errors.New("converting raw image to vmdk", err)
    44  	}
    45  
    46  	vmdkFile, err := os.Stat(imagePath)
    47  	if err != nil {
    48  		return nil, errors.New("statting raw image file", err)
    49  	}
    50  	sizeMb := vmdkFile.Size() >> 20
    51  
    52  	logrus.WithFields(logrus.Fields{
    53  		"name": params.Name,
    54  		"id":   params.Name,
    55  		"size": sizeMb,
    56  	}).Infof("creating base vmdk for unikernel image")
    57  
    58  	image := &types.Image{
    59  		Id:             params.Name,
    60  		Name:           params.Name,
    61  		RunSpec:        params.RawImage.RunSpec,
    62  		StageSpec:      params.RawImage.StageSpec,
    63  		SizeMb:         sizeMb,
    64  		Infrastructure: types.Infrastructure_VIRTUALBOX,
    65  		Created:        time.Now(),
    66  	}
    67  
    68  	if err := p.state.ModifyImages(func(images map[string]*types.Image) error {
    69  		images[params.Name] = image
    70  		return nil
    71  	}); err != nil {
    72  		return nil, errors.New("modifying image map in state", err)
    73  	}
    74  
    75  	logrus.WithFields(logrus.Fields{"image": image}).Infof("image created succesfully")
    76  	return image, nil
    77  }