github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/compilers/osv/osv_dynamic.go (about) 1 package osv 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 9 "github.com/sirupsen/logrus" 10 "github.com/emc-advanced-dev/pkg/errors" 11 unikos "github.com/solo-io/unik/pkg/os" 12 "github.com/solo-io/unik/pkg/types" 13 unikutil "github.com/solo-io/unik/pkg/util" 14 "gopkg.in/yaml.v2" 15 ) 16 17 const OSV_DEFAULT_SIZE = "1GB" 18 19 type dynamicProjectConfig struct { 20 // Size is a string representing logical image size e.g. "10GB" 21 Size string `yaml:"image_size"` 22 } 23 24 // CreateImageDynamic creates OSv image from project source directory and returns filepath of it. 25 func CreateImageDynamic(params types.CompileImageParams, useEc2Bootstrap bool) (string, error) { 26 container := unikutil.NewContainer("compilers-osv-dynamic"). 27 WithVolume(params.SourcesDir+"/", "/project_directory"). 28 WithEnv("MAX_IMAGE_SIZE", fmt.Sprintf("%dMB", params.SizeMB)) 29 30 logrus.WithFields(logrus.Fields{ 31 "params": params, 32 }).Debugf("running compilers-osv-dynamic container") 33 34 if err := container.Run(); err != nil { 35 return "", errors.New("failed running compilers-osv-dynamic on "+params.SourcesDir, err) 36 } 37 38 resultFile, err := ioutil.TempFile("", "osv-dynamic.qemu.") 39 if err != nil { 40 return "", errors.New("failed to create tmpfile for result", err) 41 } 42 defer func() { 43 if err != nil && !params.NoCleanup { 44 os.Remove(resultFile.Name()) 45 } 46 }() 47 48 if err := os.Rename(filepath.Join(params.SourcesDir, "boot.qcow2"), resultFile.Name()); err != nil { 49 return "", errors.New("failed to rename result file", err) 50 } 51 return resultFile.Name(), nil 52 } 53 54 // readImageSizeFromManifest parses manifest.yaml and returns image size. 55 // Returns default image size if anything goes wrong. 56 func readImageSizeFromManifest(projectDir string) unikos.MegaBytes { 57 config := dynamicProjectConfig{ 58 Size: OSV_DEFAULT_SIZE, 59 } 60 defaultMB, _ := unikos.ParseSize(OSV_DEFAULT_SIZE) 61 62 data, err := ioutil.ReadFile(filepath.Join(projectDir, "manifest.yaml")) 63 if err != nil { 64 logrus.WithFields(logrus.Fields{ 65 "err": err, 66 "defaultSize": OSV_DEFAULT_SIZE, 67 }).Warning("could not find manifest.yaml. Fallback to using default unikernel size.") 68 return defaultMB 69 } 70 if err := yaml.Unmarshal(data, &config); err != nil { 71 logrus.WithFields(logrus.Fields{ 72 "err": err, 73 "defaultSize": OSV_DEFAULT_SIZE, 74 }).Warning("failed to parse manifest.yaml. Fallback to using default unikernel size.") 75 return defaultMB 76 } 77 78 sizeMB, err := unikos.ParseSize(config.Size) 79 if err != nil { 80 return defaultMB 81 } 82 83 return sizeMB 84 }