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

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