forked from dan-v/rattlesnakeos-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplates.go
295 lines (259 loc) · 8.71 KB
/
templates.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package templates
import (
"archive/zip"
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/dan-v/rattlesnakeos-stack/internal/cloudaws"
"github.com/dan-v/rattlesnakeos-stack/internal/devices"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)
const (
// DefaultReleasesURLTemplate is the URL to use for gathering latest versions of components for builds. This is
// template string and should be provided a branch name (e.g. 11.0)
DefaultReleasesURLTemplate = "https://raw.githubusercontent.com/RattlesnakeOS/releases/%v/latest.json"
// DefaultCoreConfigRepo is the default core config repo to use
DefaultCoreConfigRepo = "https://github.com/rattlesnakeos/core-config-repo"
// DefaultRattlesnakeOSStackReleaseURL is the default rattlesnakeos-stack api releases github page
DefaultRattlesnakeOSStackReleaseURL = "https://api.github.com/repos/dan-v/rattlesnakeos-stack/releases/latest"
)
const (
defaultBuildScriptFilename = "build.sh"
defaultLambdaFunctionFilename = "lambda_spot_function.py"
defaultLambdaZipFilename = "lambda_spot.zip"
defaultTFMainFilename = "main.tf"
defaultGeneratedVarReplaceString = "#### <generated_vars_and_funcs.sh> ####"
)
var (
// ErrTemplateExecute is returned if there is an error executing template
ErrTemplateExecute = errors.New("error executing template")
)
// TemplateFiles are all of the files from the root templates directory
type TemplateFiles struct {
// BuildScript is just the raw build shell script (no templating in this file)
BuildScript string
// BuildScriptVars is a template file with variables and functions that gets inserted into build script after render
BuildScriptVars string
// LambdaTemplate is a template file of the python Lambda function
LambdaTemplate string
// TerraformTemplate is a template file of the Terraform code
TerraformTemplate string
}
// Config contains all of the template config values
type Config struct {
// Version is the version of stack
Version string
// Name is the name of the stack
Name string
// Region is the region to deploy stack
Region string
// Device is the device to build for
Device string
// Device details is full device details
DeviceDetails *devices.Device
// Email is the email address to subscribe to notifications for stack
Email string
// InstanceType is the instance type to use for builds
InstanceType string
// InstanceRegions is the comma separated list of regions to use for builds
InstanceRegions string
// SkipPrice is the spot price at which the build should not start
SkipPrice string
// MaxPrice is the maximum spot price to set
MaxPrice string
// SSHKey is the name of the SSH key to use for launched spot instances
SSHKey string
// Schedule is the cron schedule for builds, can be left empty to disable
Schedule string
// ChromiumBuildDisabled can be used to turn of building Chromium
ChromiumBuildDisabled bool
// ChromiumVersion can be used to lock Chromium to a specific version
ChromiumVersion string
// CoreConfigRepo is the git repo to use for the core configuration of the OS
CoreConfigRepo string
// CoreConfigRepoBranch specifies which branch to use for the core configuration repo
CoreConfigRepoBranch string
// CustomConfigRepo is the git repo to use for customization on top of core
CustomConfigRepo string
// CustomConfigRepoBranch is the branch to use for the custom configuration repo
CustomConfigRepoBranch string
// ReleasesURL is the URL to use for gathering latest versions of components for builds
ReleasesURL string
// Cloud specifies which cloud to build on (only aws supported right now)
Cloud string
// Delay instance shutdown/termination if there are active SSH sessions
InstanceDebugDelayTermination bool
// TODO: apv workaround - remove once alternative is built
// ApvRemote is the git remote that contains an android-prepare-vendor repo
ApvRemote string
// ApvBranch is the branch to use for the android-prepare-vendor repo
ApvBranch string
// ApvRevision is the revision to lock to for android-prepare-vendor repo
ApvRevision string
}
// Templates provides the ability to render templates and write them to disk
type Templates struct {
config *Config
templateFiles *TemplateFiles
buildScriptFilePath string
lambdaFunctionFilePath string
lambdaZipFilePath string
tfMainFilePath string
}
// New returns an initialized Templates
func New(config *Config, templateFiles *TemplateFiles, outputDir string) (*Templates, error) {
return &Templates{
config: config,
templateFiles: templateFiles,
buildScriptFilePath: filepath.Join(outputDir, defaultBuildScriptFilename),
lambdaFunctionFilePath: filepath.Join(outputDir, defaultLambdaFunctionFilename),
lambdaZipFilePath: filepath.Join(outputDir, defaultLambdaZipFilename),
tfMainFilePath: filepath.Join(outputDir, defaultTFMainFilename),
}, nil
}
// RenderAll renders all templates and writes them to output directory
func (t *Templates) RenderAll() error {
renderedBuildScript, err := t.renderBuildScript()
if err != nil {
return err
}
err = t.writeBuildScript(renderedBuildScript)
if err != nil {
return err
}
renderedLambdaFunction, err := t.renderLambdaFunction()
if err != nil {
return err
}
err = t.writeLambdaFunction(renderedLambdaFunction)
if err != nil {
return err
}
renderedTerraform, err := t.renderTerraform()
if err != nil {
return err
}
err = t.writeTerraform(renderedTerraform)
if err != nil {
return err
}
return nil
}
func (t *Templates) renderBuildScript() ([]byte, error) {
renderedBuildScriptTemplate, err := renderTemplate(t.templateFiles.BuildScriptVars, t.config)
if err != nil {
return nil, err
}
// insert the generated vars and funcs into raw build script
updatedBuildScript := strings.Replace(t.templateFiles.BuildScript, defaultGeneratedVarReplaceString, string(renderedBuildScriptTemplate), 1)
fixedBuildScript := strings.Replace(updatedBuildScript, "\r\n", "\n", -1)
return []byte(fixedBuildScript), nil
}
func (t *Templates) renderLambdaFunction() ([]byte, error) {
regionAMIs, err := json.Marshal(cloudaws.GetAMIs())
if err != nil {
return nil, err
}
return renderTemplate(t.templateFiles.LambdaTemplate, struct {
Config *Config
RegionAMIs string
RattlesnakeOSStackReleasesURL string
}{
t.config,
string(regionAMIs),
DefaultRattlesnakeOSStackReleaseURL,
})
}
func (t *Templates) renderTerraform() ([]byte, error) {
return renderTemplate(t.templateFiles.TerraformTemplate, struct {
Config Config
LambdaZipFileLocation string
BuildScriptFileLocation string
}{
*t.config,
strings.Replace(t.lambdaZipFilePath, "\\", "\\\\", -1),
strings.Replace(t.buildScriptFilePath, "\\", "\\\\", -1),
})
}
func (t *Templates) writeBuildScript(renderedBuildScript []byte) error {
return ioutil.WriteFile(t.buildScriptFilePath, renderedBuildScript, 0644)
}
func (t *Templates) writeLambdaFunction(renderedLambdaFunction []byte) error {
if err := ioutil.WriteFile(t.lambdaFunctionFilePath, renderedLambdaFunction, 0644); err != nil {
return err
}
if err := os.Chmod(t.lambdaFunctionFilePath, 0644); err != nil {
return err
}
if err := zipFiles(t.lambdaZipFilePath, []string{t.lambdaFunctionFilePath}); err != nil {
return err
}
return nil
}
func (t *Templates) writeTerraform(renderedTerraform []byte) error {
if err := ioutil.WriteFile(t.tfMainFilePath, renderedTerraform, 0777); err != nil {
return err
}
return nil
}
func renderTemplate(templateStr string, params interface{}) ([]byte, error) {
temp, err := template.New("templates").Delims("<%", "%>").Parse(templateStr)
if err != nil {
return nil, fmt.Errorf("failed to parse templates: %w", err)
}
buffer := new(bytes.Buffer)
if err = temp.Execute(buffer, params); err != nil {
return nil, fmt.Errorf("%v: %w", err, ErrTemplateExecute)
}
outputBytes, err := ioutil.ReadAll(buffer)
if err != nil {
return nil, fmt.Errorf("failed to read generated templates: %w", err)
}
return outputBytes, nil
}
func zipFiles(filename string, files []string) error {
newFile, err := os.Create(filename)
if err != nil {
return err
}
defer func() {
_ = newFile.Close()
}()
zipWriter := zip.NewWriter(newFile)
defer func() {
_ = zipWriter.Close()
}()
for _, file := range files {
zipfile, err := os.Open(file)
if err != nil {
return err
}
defer func() {
_ = zipfile.Close()
}()
info, err := zipfile.Stat()
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Method = zip.Deflate
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, zipfile)
if err != nil {
return err
}
}
return nil
}