-
Notifications
You must be signed in to change notification settings - Fork 5
/
generator.go
138 lines (121 loc) · 3.14 KB
/
generator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"fmt"
"os"
"text/template"
lib "github.com/bazooka-ci/bazooka/commons"
)
type Generator struct {
Config *lib.Config
OutputFolder string
Index string
}
type TemplateValues struct {
Generator *Generator
BzkBuildDir string
Phases []*BuildPhase
}
type BuildPhase struct {
Name string
Commands []string
ContinueOnCmdError bool
}
func (g *Generator) GenerateDockerfile() error {
err := os.MkdirAll(fmt.Sprintf("%s/%s", g.OutputFolder, g.Index), 0755)
if err != nil {
return err
}
phases := []*BuildPhase{
&BuildPhase{
Name: "before_install",
Commands: g.Config.BeforeInstall,
},
&BuildPhase{
Name: "install",
Commands: g.Config.Install,
},
&BuildPhase{
Name: "before_script",
Commands: g.Config.BeforeScript,
},
&BuildPhase{
Name: "script",
Commands: g.Config.Script,
},
&BuildPhase{
Name: "archive",
Commands: archiveCommands(g.Config.Archive),
ContinueOnCmdError: true,
},
&BuildPhase{
Name: "archive_success",
Commands: archiveCommands(g.Config.ArchiveSuccess),
ContinueOnCmdError: true,
},
&BuildPhase{
Name: "archive_failure",
Commands: archiveCommands(g.Config.ArchiveFailure),
ContinueOnCmdError: true,
},
&BuildPhase{
Name: "after_success",
Commands: g.Config.AfterSuccess,
},
&BuildPhase{
Name: "after_failure",
Commands: g.Config.AfterFailure,
},
&BuildPhase{
Name: "after_script",
Commands: g.Config.AfterScript,
},
}
templateValues := &TemplateValues{
Generator: g,
BzkBuildDir: lib.GetEnvMap(g.Config.Env)["BZK_BUILD_DIR"][0].Value,
Phases: phases,
}
err = writeTemplate(templateValues, "/template/Dockerfile", fmt.Sprintf("%s/%s/Dockerfile", g.OutputFolder, g.Index))
if err != nil {
return err
}
err = writeTemplate(templateValues, "/template/bazooka_run.sh", fmt.Sprintf("%s/%s/bazooka_run.sh", g.OutputFolder, g.Index))
if err != nil {
return err
}
for _, phase := range phases {
err = writeTemplate(phase, "/template/bazooka_phase.sh", fmt.Sprintf("%s/%s/bazooka_%s.sh", g.OutputFolder, g.Index, phase.Name))
if err != nil {
return err
}
}
if len(g.Config.Services) > 0 {
err = lib.Flush(g.Config.Services, fmt.Sprintf("%s/%s/services", g.OutputFolder, g.Index))
if err != nil {
return fmt.Errorf("Phase [%s/services]: writing file failed: %v", g.Index, err)
}
}
return nil
}
func archiveCommands(globs lib.Globs) []string {
res := make([]string, len(globs))
for i, pat := range globs {
res[i] = fmt.Sprintf("cp -R \"%s\" /artifacts/", pat)
}
return res
}
func writeTemplate(t interface{}, templateFile, outputFile string) error {
tmpl, err := template.ParseFiles(templateFile)
if err != nil {
return fmt.Errorf("Error parsing template file %s: %v", templateFile, err)
}
file, err := os.Create(outputFile)
if err != nil {
return fmt.Errorf("Error creating file %s: %v", outputFile, err)
}
err = tmpl.Execute(file, t)
if err != nil {
return fmt.Errorf("Error executing template file %s: %v", templateFile, err)
}
return nil
}