forked from hashicorp/consul-template
/
runner.go
392 lines (334 loc) · 11.5 KB
/
runner.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"github.com/hashicorp/consul-template/util"
)
// Runner responsible rendering Templates and invoking Commands.
type Runner struct {
// outStream and errStream are the io.Writer streams where the runner will
// write information. These streams can be set using the SetOutStream()
// and SetErrStream() functions.
outStream, errStream io.Writer
// configTemplates, templates, and dependencies are internally calculated
// caches of all the data this Runner knows about.
configTemplates []*ConfigTemplate
templates []*Template
dependencies []util.Dependency
// templateConfigTemplateMap is a map of each template to the ConfigTemplates
// that made it.
templateConfigTemplateMap map[string][]*ConfigTemplate
// dependencyDataMap is a map of each dependency to its data.
dependencyDataReceivedMap map[string]struct{}
dependencyDataMap map[string]interface{}
}
// NewRunner accepts a slice of ConfigTemplates and returns a pointer to the new
// Runner and any error that occurred during creation.
func NewRunner(configTemplates []*ConfigTemplate) (*Runner, error) {
runner := &Runner{configTemplates: configTemplates}
if err := runner.init(); err != nil {
return nil, err
}
return runner, nil
}
// SetOutStream accepts an io.Writer and sets the internal outStream for this
// Runner.
func (r *Runner) SetOutStream(s io.Writer) {
r.outStream = s
}
// SetErrStream accepts an io.Writer and sets the internal outStream for this
// Runner.
func (r *Runner) SetErrStream(s io.Writer) {
r.errStream = s
}
// Receive accepts a Dependency and data for that Dependency. This data is
// cached on the Runner. This data is then used to determine if a Template
// is "renderable" (i.e. all its Dependencies have been downloaded at least
// once).
func (r *Runner) Receive(dependency util.Dependency, data interface{}) {
r.dependencyDataReceivedMap[dependency.HashCode()] = struct{}{}
r.dependencyDataMap[dependency.HashCode()] = data
}
// Dependencies returns the unique slice of Dependency in the Runner
func (r *Runner) Dependencies() []util.Dependency {
return r.dependencies
}
// RunAll iterates over each template in this Runner and conditionally executes
// the template rendering and command execution.
//
// The template is rendered atomicly. If and only if the template render
// completes successfully, the optional commands will be executed, if given.
// Please note that all templates are rendered **and then** any commands are
// executed.
//
// If the dry flag is given, the template will be rendered to the outStream,
// which defaults to os.Stdout. In dry mode, commands are never executed.
func (r *Runner) RunAll(dry bool) error {
commands := make([]string, 0)
for _, template := range r.templates {
// If the template is not ready to be rendered, just return
if !r.canRender(template) {
return nil
}
for _, ctemplate := range r.configTemplatesFor(template) {
// Render the template, taking dry mode into account
rendered, err := r.render(template, ctemplate.Destination, dry)
if err != nil {
return err
}
// If the template was rendered (changed) and we are not in dry-run mode,
// aggregate commands
if rendered && !dry {
if ctemplate.Command != "" {
commands = append(commands, ctemplate.Command)
}
}
}
}
// Execute each command in sequence, collecting any errors that occur - this
// ensures all commands execute at least once
errs := make([]error, 0)
for _, command := range commands {
if err := r.execute(command); err != nil {
errs = append(errs, err)
}
}
// If any errors were returned, convert them to an ErrorList for human
// readability
if len(errs) != 0 {
errors := NewErrorList("running commands")
for _, err := range errs {
errors.Append(err)
}
return errors.GetError()
}
return nil
}
// init() creates the Runner's underlying data structures and returns an error
// if any problems occur.
func (r *Runner) init() error {
if len(r.configTemplates) == 0 {
r.configTemplates = make([]*ConfigTemplate, 0)
}
templatesMap := make(map[string]*Template)
dependenciesMap := make(map[string]util.Dependency)
r.templateConfigTemplateMap = make(map[string][]*ConfigTemplate)
// Process all Template first, so we can return errors
for _, configTemplate := range r.configTemplates {
template := &Template{Path: configTemplate.Source}
if _, ok := templatesMap[template.HashCode()]; !ok {
template, err := NewTemplate(configTemplate.Source)
if err != nil {
return err
}
templatesMap[template.HashCode()] = template
}
if len(r.templateConfigTemplateMap[template.HashCode()]) == 0 {
r.templateConfigTemplateMap[template.HashCode()] = make([]*ConfigTemplate, 0, 1)
}
r.templateConfigTemplateMap[template.HashCode()] = append(r.templateConfigTemplateMap[template.HashCode()], configTemplate)
}
// For each Template, setup the mappings for O(1) lookups
for _, template := range templatesMap {
for _, dep := range template.Dependencies() {
if _, ok := dependenciesMap[dep.HashCode()]; !ok {
dependenciesMap[dep.HashCode()] = dep
}
}
}
// Calculate the list of Templates
r.templates = make([]*Template, 0, len(templatesMap))
for _, template := range templatesMap {
r.templates = append(r.templates, template)
}
// Calculate the list of Dependency
r.dependencies = make([]util.Dependency, 0, len(dependenciesMap))
for _, dependency := range dependenciesMap {
r.dependencies = append(r.dependencies, dependency)
}
r.dependencyDataReceivedMap = make(map[string]struct{})
r.dependencyDataMap = make(map[string]interface{})
r.outStream = os.Stdout
return nil
}
// canRender accepts a Template and returns true if and only if all of the
// Dependencies of that template have data in the Runner.
func (r *Runner) canRender(template *Template) bool {
for _, dependency := range template.Dependencies() {
if !r.receivedData(dependency) {
return false
}
}
return true
}
// Render accepts a Template and a destinations. This will return an error if
// the Template is not ready to be rendered. You can check if a Template is
// renderable using canRender().
//
// If the template has changed on disk, this method return true.
//
// If the template already exists and has the same contents as the "would-be"
// template, no action is taken. In this scenario, the render function returns
// false, indicating no template change has occurred.
func (r *Runner) render(template *Template, destination string, dry bool) (bool, error) {
if !r.canRender(template) {
return false, fmt.Errorf("runner: template data not ready")
}
context, err := r.templateContextFor(template)
if err != nil {
return false, err
}
contents, err := template.Execute(context)
if err != nil {
return false, err
}
existingContents, err := ioutil.ReadFile(destination)
if err != nil && !os.IsNotExist(err) {
return false, err
}
if bytes.Equal(contents, existingContents) {
return false, nil
}
if dry {
fmt.Fprintf(r.outStream, "> %s\n%s", destination, contents)
} else {
if err := r.atomicWrite(destination, contents); err != nil {
return false, err
}
}
return true, nil
}
// execute accepts a command string and runs that command string on the current
// system.
func (r *Runner) execute(command string) error {
var shell, flag string
if runtime.GOOS == "windows" {
shell, flag = "cmd", "/C"
} else {
shell, flag = "/bin/sh", "-c"
}
// Create an invoke the command
cmd := exec.Command(shell, flag, command)
cmd.Stdout = r.outStream
cmd.Stderr = r.errStream
return cmd.Run()
}
// receivedData returns true if the Runner has ever received data for the given
// dependency and false otherwise.
func (r *Runner) receivedData(dependency util.Dependency) bool {
_, ok := r.dependencyDataReceivedMap[dependency.HashCode()]
return ok
}
// data returns the data for the given dependency.
func (r *Runner) data(dependency util.Dependency) interface{} {
return r.dependencyDataMap[dependency.HashCode()]
}
// ConfigTemplateFor returns the ConfigTemplate for the given Template
func (r *Runner) configTemplatesFor(template *Template) []*ConfigTemplate {
return r.templateConfigTemplateMap[template.HashCode()]
}
// templateContextFor creates and returns a new TemplateContext for the given
// Template, iterating through all the Template's Dependencies and appending
// them where appropriate in the TemplateContext.
//
// If an unknown Dependency.(type) is encountered, an error is returned.
func (r *Runner) templateContextFor(template *Template) (*TemplateContext, error) {
context := &TemplateContext{
File: make(map[string]string),
KeyPrefixes: make(map[string][]*util.KeyPair),
Keys: make(map[string]string),
Nodes: make(map[string][]*util.Node),
Services: make(map[string][]*util.Service),
}
for _, dependency := range template.Dependencies() {
data := r.data(dependency)
switch dependency := dependency.(type) {
case *util.FileDependency:
context.File[dependency.Key()] = data.(string)
case *util.KeyPrefixDependency:
context.KeyPrefixes[dependency.Key()] = data.([]*util.KeyPair)
case *util.KeyDependency:
context.Keys[dependency.Key()] = data.(string)
case *util.NodeDependency:
context.Nodes[dependency.Key()] = data.([]*util.Node)
case *util.ServiceDependency:
context.Services[dependency.Key()] = data.([]*util.Service)
default:
return nil, fmt.Errorf("unknown dependency type %+v", dependency)
}
}
return context, nil
}
// atomicWrite accepts a destination path and the template contents. It writes
// the template contents to a TempFile on disk, returning if any errors occur.
//
// If the parent destination directory does not exist, it will be created
// automatically with permissions 0755. To use a different permission, create
// the directory first or use `chmod` in a Command.
//
// If the destination path exists, all attempts will be made to preserve the
// existing file permissions. If those permissions cannot be read, an error is
// returned. If the file does not exist, it will be created automatically with
// permissions 0644. To use a different permission, create the destination file
// first or use `chmod` in a Command.
//
// If no errors occur, the Tempfile is "renamed" (moved) to the destination
// path.
func (r *Runner) atomicWrite(path string, contents []byte) error {
var mode os.FileMode
// If the current file exists, get permissions so we can preserve them
stat, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
mode = 0644
} else {
return err
}
} else {
mode = stat.Mode()
}
parent := filepath.Dir(path)
if _, err := os.Stat(parent); os.IsNotExist(err) {
if err := os.MkdirAll(parent, 0755); err != nil {
return err
}
}
f, err := ioutil.TempFile(parent, "")
if err != nil {
return err
}
defer os.Remove(f.Name())
if _, err := f.Write(contents); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
if err := os.Chmod(f.Name(), mode); err != nil {
return err
}
// Remove the file if we are running on Windows. There is a bug in Go on
// Windows such that Go uses MoveFile which raises an exception if the file
// already exists.
//
// See: http://grokbase.com/t/gg/golang-nuts/13aab5f210/go-nuts-atomic-replacement-of-files-on-windows
// for more information.
if runtime.GOOS == "windows" {
if err := os.Remove(path); err != nil {
return err
}
}
if err := os.Rename(f.Name(), path); err != nil {
return err
}
return nil
}