github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/compilers/rump/generic.go (about) 1 package rump 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "regexp" 7 "strings" 8 9 "github.com/emc-advanced-dev/pkg/errors" 10 11 "github.com/solo-io/unik/pkg/types" 12 unikutil "github.com/solo-io/unik/pkg/util" 13 ) 14 15 func execContainer(imageName string, cmds []string, binds map[string]string, privileged bool, env map[string]string) error { 16 container := unikutil.NewContainer(imageName).Privileged(privileged).WithVolumes(binds).WithEnvs(env) 17 if err := container.Run(cmds...); err != nil { 18 return errors.New("running container "+imageName, err) 19 } 20 return nil 21 } 22 23 type RumCompilerBase struct { 24 DockerImage string 25 CreateImage func(kernel, args string, mntPoints, bakedEnv []string, noCleanup bool) (*types.RawImage, error) 26 } 27 28 func (r *RumCompilerBase) runContainer(localFolder string, envPairs []string) error { 29 env := make(map[string]string) 30 for _, pair := range envPairs { 31 split := strings.Split(pair, "=") 32 if len(split) != 2 { 33 return errors.New(pair+" is invaid string for env pair", nil) 34 } 35 env[split[0]] = split[1] 36 } 37 38 if unikutil.IsDockerToolbox() { 39 localFolder = unikutil.GetToolboxMountPath(localFolder) 40 } 41 return unikutil.NewContainer(r.DockerImage).WithVolume(localFolder, "/opt/code").WithEnvs(env).Run() 42 43 } 44 45 func setRumpCmdLine(c rumpConfig, prog string, argv []string, addStub bool) rumpConfig { 46 if addStub { 47 stub := commandLine{ 48 Bin: "stub", 49 Argv: []string{}, 50 } 51 c.Rc = append(c.Rc, stub) 52 } 53 progrc := commandLine{ 54 Bin: "program", 55 Argv: argv, 56 } 57 c.Rc = append(c.Rc, progrc) 58 return c 59 } 60 61 var netRegEx = regexp.MustCompile("net[1-9]") 62 var envRegEx = regexp.MustCompile("\"env\":\\{(.*?)\\}") 63 var envRegEx2 = regexp.MustCompile("env[0-9]") 64 65 // rump special json 66 func toRumpJson(c rumpConfig) (string, error) { 67 68 blk := c.Blk 69 c.Blk = nil 70 71 jsonConfig, err := json.Marshal(c) 72 if err != nil { 73 return "", err 74 } 75 76 blks := "" 77 for _, b := range blk { 78 79 blkjson, err := json.Marshal(b) 80 if err != nil { 81 return "", err 82 } 83 blks += fmt.Sprintf("\"blk\": %s,", string(blkjson)) 84 } 85 var jsonString string 86 if len(blks) > 0 { 87 88 jsonString = string(jsonConfig[:len(jsonConfig)-1]) + "," + blks[:len(blks)-1] + "}" 89 90 } else { 91 jsonString = string(jsonConfig) 92 } 93 94 jsonString = netRegEx.ReplaceAllString(jsonString, "net") 95 96 jsonString = string(envRegEx.ReplaceAllString(jsonString, "$1")) 97 98 jsonString = string(envRegEx2.ReplaceAllString(jsonString, "env")) 99 100 return jsonString, nil 101 102 }