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

     1  package ukvm
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"time"
     7  
     8  	"github.com/sirupsen/logrus"
     9  	"github.com/emc-advanced-dev/pkg/errors"
    10  	unikos "github.com/solo-io/unik/pkg/os"
    11  	"github.com/solo-io/unik/pkg/types"
    12  )
    13  
    14  func (p *UkvmProvider) Stage(params types.StageImageParams) (_ *types.Image, err error) {
    15  	images, err := p.ListImages()
    16  	if err != nil {
    17  		return nil, errors.New("retrieving image list for existing image", err)
    18  	}
    19  	for _, image := range images {
    20  		if image.Name == params.Name {
    21  			if !params.Force {
    22  				return nil, errors.New("an image already exists with name '"+params.Name+"', try again with --force", nil)
    23  			} else {
    24  				logrus.WithField("image", image).Warnf("force: deleting previous image with name " + params.Name)
    25  				if err := p.DeleteImage(image.Id, true); err != nil {
    26  					logrus.Warn("failed to remove previously existing image", err)
    27  				}
    28  			}
    29  		}
    30  	}
    31  	imageName := params.Name
    32  	imageDir := getImageDir(imageName)
    33  	logrus.Debugf("making directory: %s", imageDir)
    34  	if err := os.MkdirAll(imageDir, 0777); err != nil {
    35  		return nil, errors.New("creating directory for boot image", err)
    36  	}
    37  	defer func() {
    38  		if err != nil && !params.NoCleanup {
    39  			os.RemoveAll(imageDir)
    40  		}
    41  	}()
    42  
    43  	kernelPath := filepath.Join(params.RawImage.LocalImagePath, "program.bin")
    44  	if err := unikos.CopyFile(kernelPath, getKernelPath(imageName)); err != nil {
    45  		return nil, errors.New("program.bin cannot be copied", err)
    46  
    47  	}
    48  	ukvmPath := filepath.Join(params.RawImage.LocalImagePath, "ukvm-bin")
    49  	if err := unikos.CopyFile(ukvmPath, getUkvmPath(imageName)); err != nil {
    50  		return nil, errors.New("ukvm-bin cannot be copied", err)
    51  	}
    52  
    53  	kernelPathInfo, err := os.Stat(kernelPath)
    54  	if err != nil {
    55  		return nil, errors.New("statting unikernel file", err)
    56  	}
    57  	ukvmPathInfo, err := os.Stat(ukvmPath)
    58  	if err != nil {
    59  		return nil, errors.New("statting ukvm file", err)
    60  	}
    61  	sizeMb := (ukvmPathInfo.Size() + kernelPathInfo.Size()) >> 20
    62  
    63  	logrus.WithFields(logrus.Fields{
    64  		"name": params.Name,
    65  		"id":   params.Name,
    66  		"size": sizeMb,
    67  	}).Infof("copying raw boot image")
    68  
    69  	image := &types.Image{
    70  		Id:             params.Name,
    71  		Name:           params.Name,
    72  		RunSpec:        params.RawImage.RunSpec,
    73  		StageSpec:      params.RawImage.StageSpec,
    74  		SizeMb:         sizeMb,
    75  		Infrastructure: types.Infrastructure_UKVM,
    76  		Created:        time.Now(),
    77  	}
    78  
    79  	if err := p.state.ModifyImages(func(images map[string]*types.Image) error {
    80  		images[params.Name] = image
    81  		return nil
    82  	}); err != nil {
    83  		return nil, errors.New("modifying image map in state", err)
    84  	}
    85  
    86  	logrus.WithFields(logrus.Fields{"image": image}).Infof("image created succesfully")
    87  	return image, nil
    88  }