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

     1  package rump
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path"
     9  	"path/filepath"
    10  
    11  	"github.com/sirupsen/logrus"
    12  	"github.com/emc-advanced-dev/pkg/errors"
    13  	"github.com/solo-io/unik/pkg/compilers"
    14  	"github.com/solo-io/unik/pkg/types"
    15  )
    16  
    17  // uses rump docker conter container
    18  // the container expectes code in /opt/code and will produce program.bin in the same folder.
    19  // we need to take the program bin and combine with json config produce an image
    20  
    21  type RumpGoCompiler struct {
    22  	RumCompilerBase
    23  	BootstrapType string //ec2 | udp | nostub | gcloud
    24  }
    25  
    26  func (r *RumpGoCompiler) CompileRawImage(params types.CompileImageParams) (*types.RawImage, error) {
    27  	sourcesDir := params.SourcesDir
    28  	godepsFile := filepath.Join(sourcesDir, "Godeps", "Godeps.json")
    29  	_, err := os.Stat(godepsFile)
    30  	if err != nil {
    31  		return nil, errors.New("the Go compiler requires Godeps file in the root of your project. see https://github.com/tools/godep", nil)
    32  	}
    33  	data, err := ioutil.ReadFile(godepsFile)
    34  	if err != nil {
    35  		return nil, errors.New("could not read godeps file", err)
    36  	}
    37  	var g godeps
    38  	if err := json.Unmarshal(data, &g); err != nil {
    39  		return nil, errors.New("invalid json in godeps file", err)
    40  	}
    41  	containerEnv := []string{
    42  		fmt.Sprintf("ROOT_PATH=%s", g.ImportPath),
    43  		fmt.Sprintf("BOOTSTRAP_TYPE=%s", r.BootstrapType),
    44  	}
    45  
    46  	if err := r.runContainer(sourcesDir, containerEnv); err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	// now we should program.bin
    51  	resultFile := path.Join(sourcesDir, "program.bin")
    52  	logrus.Debugf("finished kernel binary at %s", resultFile)
    53  	img, err := r.CreateImage(resultFile, params.Args, params.MntPoints, nil, params.NoCleanup)
    54  	if err != nil {
    55  		return nil, errors.New("creating boot volume from kernel binary", err)
    56  	}
    57  	return img, nil
    58  }
    59  
    60  func (r *RumpGoCompiler) Usage() *compilers.CompilerUsage {
    61  	return nil
    62  }
    63  
    64  type godeps struct {
    65  	ImportPath   string   `json:"ImportPath"`
    66  	GoVersion    string   `json:"GoVersion"`
    67  	GodepVersion string   `json:"GodepVersion"`
    68  	Packages     []string `json:"Packages"`
    69  	Deps         []struct {
    70  		ImportPath string `json:"ImportPath"`
    71  		Rev        string `json:"Rev"`
    72  		Comment    string `json:"Comment,omitempty"`
    73  	} `json:"Deps"`
    74  }