github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/compilers/osv/generic.go (about) 1 package osv 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "regexp" 8 9 "github.com/emc-advanced-dev/pkg/errors" 10 "github.com/solo-io/unik/pkg/types" 11 ) 12 13 // ConvertParams contains all the information needed when bootstrapping. 14 type FinishParams struct { 15 // CapstanImagePath points to image that was composed by Capstan 16 CapstanImagePath string 17 18 // CompileParams stores parameters that were used for composing image 19 CompileParams types.CompileImageParams 20 } 21 22 // ImageFinisher implements conversion of Capstan result into provider-specific image. 23 // It should be implemented per provider. In this context converting means e.g. 24 // converting .qcow2 image (Capstan result) into .wmdk for VirtualBox provider. 25 type ImageFinisher interface { 26 // Convert converts Capstan-provided image into appropriate format 27 // for the provider 28 FinishImage(params FinishParams) (*types.RawImage, error) 29 30 // UseEc2 tells whether or not to prepare image for EC2 (Elastic Compute Cloud) 31 UseEc2() bool 32 } 33 34 // addRuntimeStanzaToMetaRun assures that meta/run.yaml requires appropriate runtime. 35 // Runtime is required with a line "runtime: <runtime-name>". 36 // NOTE: this function should operate over a *copy* of original meta/run.yaml 37 // so that user does not see the changes. 38 // See: 39 // https://github.com/mikelangelo-project/capstan/blob/master/Documentation/ConfigurationFiles.md 40 func addRuntimeStanzaToMetaRun(sourcesDir, runtime string) error { 41 filepath := filepath.Join(sourcesDir, "meta", "run.yaml") 42 43 // - Read. 44 data, err := ioutil.ReadFile(filepath) 45 if err != nil { 46 return errors.New("failed to read meta/run.yaml file", err) 47 } 48 // - Modify. 49 content := string(data) 50 re := regexp.MustCompile("(?m)[\r\n]+^.*runtime:.*$") 51 content = re.ReplaceAllString(content, "") 52 content = "runtime: " + runtime + "\n\n" + content 53 54 // - Write. 55 if err = ioutil.WriteFile(filepath, []byte(content), 0644); err != nil { 56 return errors.New("failed to write to meta/run.yaml", err) 57 } 58 59 return nil 60 } 61 62 // assureMetaPackage assures that meta/package.yaml exists. 63 // See: 64 // https://github.com/mikelangelo-project/capstan/blob/master/Documentation/ConfigurationFiles.md 65 func assureMetaPackage(sourcesDir string) error { 66 filepath := filepath.Join(sourcesDir, "meta", "package.yaml") 67 68 // Do nothing if meta/package.yaml already exist. 69 if _, err := os.Stat(filepath); os.IsNotExist(err) { 70 // Write. 71 content := ` 72 name: package.unik 73 title: Capstan package auto-generated by UniK 74 author: UniK 75 ` 76 if err := ioutil.WriteFile(filepath, []byte(content), 0644); err != nil { 77 return errors.New("failed to write to meta/package.yaml", err) 78 } 79 } 80 81 return nil 82 }