github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/compilers/rump/rump-scripting.go (about) 1 package rump 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path" 8 "path/filepath" 9 10 "github.com/sirupsen/logrus" 11 "github.com/emc-advanced-dev/pkg/errors" 12 "github.com/solo-io/unik/pkg/compilers" 13 "github.com/solo-io/unik/pkg/types" 14 "gopkg.in/yaml.v2" 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 const ( 22 BootstrapTypeEC2 = "ec2" 23 BootstrapTypeUDP = "udp" 24 BootstrapTypeGCLOUD = "gcloud" 25 BootstrapTypeNoStub = "nostub" 26 ) 27 28 //compiler for building images from interpreted/scripting languages (python, javascript) 29 type RumpScriptCompiler struct { 30 RumCompilerBase 31 32 BootstrapType string //ec2 vs udp 33 RunScriptArgs string 34 ScriptEnv []string 35 } 36 37 type scriptProjectConfig struct { 38 MainFile string `yaml:"main_file"` 39 RuntimeArgs string `yaml:"runtime_args"` 40 } 41 42 func (r *RumpScriptCompiler) CompileRawImage(params types.CompileImageParams) (*types.RawImage, error) { 43 sourcesDir := params.SourcesDir 44 var config scriptProjectConfig 45 data, err := ioutil.ReadFile(filepath.Join(sourcesDir, "manifest.yaml")) 46 if err != nil { 47 return nil, errors.New("failed to read manifest.yaml file", err) 48 } 49 if err := yaml.Unmarshal(data, &config); err != nil { 50 return nil, errors.New("failed to parse yaml manifest.yaml file", err) 51 } 52 53 if _, err := os.Stat(filepath.Join(sourcesDir, config.MainFile)); err != nil || config.MainFile == "" { 54 return nil, errors.New("invalid main file specified", err) 55 } 56 57 logrus.Debugf("using main file %s", config.MainFile) 58 59 containerEnv := []string{ 60 fmt.Sprintf("MAIN_FILE=%s", config.MainFile), 61 fmt.Sprintf("BOOTSTRAP_TYPE=%s", r.BootstrapType), 62 } 63 64 if err := r.runContainer(sourcesDir, containerEnv); err != nil { 65 return nil, err 66 } 67 68 resultFile := path.Join(sourcesDir, "program.bin") 69 70 //build args string 71 args := r.RunScriptArgs 72 if config.RuntimeArgs != "" { 73 args = config.RuntimeArgs + " " + args 74 } 75 if params.Args != "" { 76 args = args + " " + params.Args 77 } 78 79 return r.CreateImage(resultFile, args, params.MntPoints, append(r.ScriptEnv, fmt.Sprintf("MAIN_FILE=%s", config.MainFile), fmt.Sprintf("BOOTSTRAP_TYPE=%s", r.BootstrapType)), params.NoCleanup) 80 } 81 82 func (r *RumpScriptCompiler) Usage() *compilers.CompilerUsage { 83 return nil 84 } 85 86 func NewRumpPythonCompiler(dockerImage string, createImage func(kernel, args string, mntPoints, bakedEnv []string, noCleanup bool) (*types.RawImage, error), bootStrapType string) *RumpScriptCompiler { 87 return &RumpScriptCompiler{ 88 RumCompilerBase: RumCompilerBase{ 89 DockerImage: dockerImage, 90 CreateImage: createImage, 91 }, 92 BootstrapType: bootStrapType, 93 RunScriptArgs: "/bootpart/python-wrapper.py", 94 ScriptEnv: []string{ 95 "PYTHONHOME=/bootpart/python", 96 "PYTHONPATH=/bootpart/lib/python3.5/site-packages/:/bootpart/bin/", 97 }, 98 } 99 } 100 101 func NewRumpJavaCompiler(dockerImage string, createImage func(kernel, args string, mntPoints, bakedEnv []string, noCleanup bool) (*types.RawImage, error), bootStrapType string) *RumpScriptCompiler { 102 return &RumpScriptCompiler{ 103 RumCompilerBase: RumCompilerBase{ 104 DockerImage: dockerImage, 105 CreateImage: createImage, 106 }, 107 BootstrapType: bootStrapType, 108 RunScriptArgs: "-jar /bootpart/program.jar", 109 ScriptEnv: []string{ 110 "CLASSPATH=/bootpart/jetty:/bootpart/jdk/jre/lib", 111 "JAVA_HOME=/bootpart/jdk/", 112 }, 113 } 114 }