github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/compilers/includeos/includeos_virtualbox.go (about)

     1  package includeos
     2  
     3  import (
     4  	goerrors "errors"
     5  	"os"
     6  	"path"
     7  	"path/filepath"
     8  
     9  	"github.com/sirupsen/logrus"
    10  	"github.com/emc-advanced-dev/pkg/errors"
    11  	"github.com/solo-io/unik/pkg/compilers"
    12  	"github.com/solo-io/unik/pkg/types"
    13  	unikutil "github.com/solo-io/unik/pkg/util"
    14  )
    15  
    16  type IncludeosVirtualboxCompiler struct{}
    17  
    18  func (i *IncludeosVirtualboxCompiler) CompileRawImage(params types.CompileImageParams) (*types.RawImage, error) {
    19  	sourcesDir := params.SourcesDir
    20  	env := make(map[string]string)
    21  	if err := unikutil.NewContainer("compilers-includeos-cpp-hw").WithVolume(sourcesDir, "/opt/code").WithEnvs(env).Run(); err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	res := &types.RawImage{}
    26  	localImageFile, err := i.findFirstImageFile(sourcesDir)
    27  	if err != nil {
    28  		return nil, errors.New("error getting local image file name", err)
    29  	}
    30  	res.LocalImagePath = path.Join(sourcesDir, localImageFile)
    31  	res.StageSpec.ImageFormat = types.ImageFormat_RAW
    32  	res.RunSpec.StorageDriver = types.StorageDriver_IDE
    33  	res.RunSpec.DefaultInstanceMemory = 256
    34  	return res, nil
    35  }
    36  
    37  func (i *IncludeosVirtualboxCompiler) findFirstImageFile(directory string) (string, error) {
    38  	dir, err := os.Open(directory)
    39  	if err != nil {
    40  		return "", errors.New("could not open dir", err)
    41  	}
    42  	defer dir.Close()
    43  	files, err := dir.Readdir(-1)
    44  	if err != nil {
    45  		return "", errors.New("could not read dir", err)
    46  	}
    47  	for _, file := range files {
    48  		logrus.Debugf("searching for .img file: %v", file.Name())
    49  		if file.Mode().IsRegular() {
    50  			if filepath.Ext(file.Name()) == ".img" {
    51  				return file.Name(), nil
    52  			}
    53  		}
    54  	}
    55  	return "", errors.New("no image file found", goerrors.New("end of dir"))
    56  }
    57  
    58  func (r *IncludeosVirtualboxCompiler) Usage() *compilers.CompilerUsage {
    59  	return nil
    60  }