gopkg.in/openshift/source-to-image.v1@v1.2.0/pkg/create/create.go (about) 1 package create 2 3 import ( 4 "os" 5 "text/template" 6 7 "github.com/openshift/source-to-image/pkg/create/templates" 8 utillog "github.com/openshift/source-to-image/pkg/util/log" 9 ) 10 11 var log = utillog.StderrLog 12 13 // Bootstrap defines parameters for the template processing 14 type Bootstrap struct { 15 DestinationDir string 16 ImageName string 17 } 18 19 // New returns a new bootstrap for given image name and destination directory 20 func New(name, dst string) *Bootstrap { 21 return &Bootstrap{ImageName: name, DestinationDir: dst} 22 } 23 24 // AddSTIScripts creates the STI scripts directory structure and process 25 // templates for STI scripts 26 func (b *Bootstrap) AddSTIScripts() { 27 os.MkdirAll(b.DestinationDir+"/"+"s2i/bin", 0700) 28 b.process(templates.AssembleScript, "s2i/bin/assemble", 0755) 29 b.process(templates.RunScript, "s2i/bin/run", 0755) 30 b.process(templates.UsageScript, "s2i/bin/usage", 0755) 31 b.process(templates.SaveArtifactsScript, "s2i/bin/save-artifacts", 0755) 32 } 33 34 // AddDockerfile creates an example Dockerfile 35 func (b *Bootstrap) AddDockerfile() { 36 b.process(templates.Dockerfile, "Dockerfile", 0600) 37 } 38 39 // AddReadme creates a README.md 40 func (b *Bootstrap) AddReadme() { 41 b.process(templates.Readme, "README.md", 0600) 42 } 43 44 // AddTests creates an example test for the STI image and the Makefile 45 func (b *Bootstrap) AddTests() { 46 os.MkdirAll(b.DestinationDir+"/"+"test/test-app", 0700) 47 b.process(templates.Index, "test/test-app/index.html", 0600) 48 b.process(templates.TestRunScript, "test/run", 0700) 49 b.process(templates.Makefile, "Makefile", 0600) 50 } 51 52 func (b *Bootstrap) process(t string, dst string, perm os.FileMode) { 53 tpl := template.Must(template.New("").Parse(t)) 54 if _, err := os.Stat(b.DestinationDir + "/" + dst); err == nil { 55 log.Errorf("File already exists: %s, skipping", dst) 56 return 57 } 58 f, err := os.Create(b.DestinationDir + "/" + dst) 59 if err != nil { 60 log.Errorf("Unable to create %s file, skipping: %v", dst, err) 61 return 62 } 63 if err := os.Chmod(b.DestinationDir+"/"+dst, perm); err != nil { 64 log.Errorf("Unable to chmod %s file to %v, skipping: %v", dst, perm, err) 65 return 66 } 67 defer f.Close() 68 if err := tpl.Execute(f, b); err != nil { 69 log.Errorf("Error processing %s template: %v", dst, err) 70 } 71 }