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

     1  package rump
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"path"
     7  	"path/filepath"
     8  
     9  	"github.com/emc-advanced-dev/pkg/errors"
    10  	"github.com/solo-io/unik/pkg/compilers"
    11  	"github.com/solo-io/unik/pkg/types"
    12  	"gopkg.in/yaml.v2"
    13  )
    14  
    15  //compiler for building images from interpreted/scripting languages (python, javascript)
    16  type RumpCCompiler struct {
    17  	RumCompilerBase
    18  }
    19  
    20  type cProjectConfig struct {
    21  	BinaryName string `yaml:"binary_name"`
    22  }
    23  
    24  func (r *RumpCCompiler) CompileRawImage(params types.CompileImageParams) (*types.RawImage, error) {
    25  	sourcesDir := params.SourcesDir
    26  	var config cProjectConfig
    27  	data, err := ioutil.ReadFile(filepath.Join(sourcesDir, "manifest.yaml"))
    28  	if err != nil {
    29  		return nil, errors.New("failed to read manifest.yaml file", err)
    30  	}
    31  	if err := yaml.Unmarshal(data, &config); err != nil {
    32  		return nil, errors.New("failed to parse yaml manifest.yaml file", err)
    33  	}
    34  
    35  	containerEnv := []string{
    36  		fmt.Sprintf("BINARY_NAME=%s", config.BinaryName),
    37  	}
    38  
    39  	if err := r.runContainer(sourcesDir, containerEnv); err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	resultFile := path.Join(sourcesDir, "program.bin")
    44  
    45  	return r.CreateImage(resultFile, params.Args, params.MntPoints, nil, params.NoCleanup)
    46  }
    47  
    48  func (r *RumpCCompiler) Usage() *compilers.CompilerUsage {
    49  	return nil
    50  }
    51  
    52  func NewRumpCCompiler(dockerImage string, createImage func(kernel, args string, mntPoints, bakedEnv []string, noCleanup bool) (*types.RawImage, error)) *RumpCCompiler {
    53  	return &RumpCCompiler{
    54  		RumCompilerBase: RumCompilerBase{
    55  			DockerImage: dockerImage,
    56  			CreateImage: createImage,
    57  		},
    58  	}
    59  }