forked from leg100/otf
/
operation.go
550 lines (499 loc) · 15.4 KB
/
operation.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
package agent
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/fatih/color"
"github.com/tofutf/tofutf/internal"
"github.com/tofutf/tofutf/internal/logr"
"github.com/tofutf/tofutf/internal/logs"
"github.com/tofutf/tofutf/internal/run"
"github.com/tofutf/tofutf/internal/state"
"github.com/tofutf/tofutf/internal/variable"
)
const (
localStateFilename = "terraform.tfstate"
planFilename = "plan.out"
jsonPlanFilename = "plan.out.json"
lockFilename = ".terraform.lock.hcl"
)
var ascii = regexp.MustCompile("[[:^ascii:]]")
// operation performs the execution of a job
type operation struct {
*daemonClient
*run.Run
logr.Logger
config Config
job *Job
canceled bool
ctx context.Context
cancelfn context.CancelFunc
out io.Writer
terraformPath string
envs []string
variables []*variable.Variable
proc *os.Process
downloader downloader
token []byte
agentID string
isPoolAgent bool
*workdir
}
type newOperationOptions struct {
logger logr.Logger
client *daemonClient
config Config
job *Job
downloader downloader
envs []string
token []byte
agentID string
isPoolAgent bool
}
func newOperation(opts newOperationOptions) *operation {
// an operation has its own uninherited context; the operation is instead
// canceled via its cancel() method.
ctx, cancelfn := context.WithCancel(context.Background())
return &operation{
Logger: opts.logger.WithValues("job", opts.job),
daemonClient: opts.client,
config: opts.config,
job: opts.job,
envs: opts.envs,
downloader: opts.downloader,
token: opts.token,
ctx: ctx,
cancelfn: cancelfn,
agentID: opts.agentID,
isPoolAgent: opts.isPoolAgent,
}
}
// doAndFinish executes the job and marks the job as complete with the
// appropriate status.
func (o *operation) doAndFinish() {
// do the job, and then handle any error and send appropriate job status
// update
err := o.do()
var opts finishJobOptions
switch {
case o.canceled:
if o.ctx.Err() != nil {
// the context is closed, which only occurs when the server has
// already canceled the job and the server has sent the operation a
// force-cancel signal. In which case there is nothing more to be
// done other than tell the user what happened.
o.Error(err, "job forceably canceled")
return
}
opts.Status = JobCanceled
o.Error(err, "job canceled")
case err != nil:
opts.Status = JobErrored
opts.Error = err.Error()
o.Error(err, "finished job with error")
default:
opts.Status = JobFinished
o.V(0).Info("finished job successfully")
}
if err := o.agents.finishJob(o.ctx, o.job.Spec, opts); err != nil {
o.Error(err, "sending job status", "status", opts.Status)
}
}
// do executes the job
func (o *operation) do() error {
// if this is a pool agent using RPC to communicate with the server
// then use a new client for this job, configured to authenticate using the
// job token and to retry requests upon encountering transient errors.
if o.isPoolAgent {
jc, err := o.newJobClient(o.agentID, o.token, o.Logger)
if err != nil {
return fmt.Errorf("initializing job client: %w", err)
}
o.daemonClient = jc
} else {
// this is a server agent: directly authenticate as job with services
o.ctx = internal.AddSubjectToContext(o.ctx, o.job)
}
// make token available to terraform CLI
o.envs = append(o.envs, internal.CredentialEnv(o.server.Hostname(), o.token))
run, err := o.runs.Get(o.ctx, o.job.Spec.RunID)
if err != nil {
return err
}
o.Run = run
// Get workspace in order to get working directory path
//
// TODO: add working directory to run.Run so we skip having to retrieve
// workspace.
ws, err := o.workspaces.Get(o.ctx, o.job.WorkspaceID)
if err != nil {
return fmt.Errorf("retreiving workspace: %w", err)
}
wd, err := newWorkdir(ws.WorkingDirectory)
if err != nil {
return fmt.Errorf("constructing working directory: %w", err)
}
defer wd.close()
o.workdir = wd
// retrieve variables and add them to the environment
variables, err := o.daemonClient.variables.ListEffectiveVariables(o.ctx, run.ID)
if err != nil {
return fmt.Errorf("retrieving variables: %w", err)
}
o.variables = variables
// append variables that are environment variables to the list of
// environment variables
for _, v := range variables {
if v.Category == variable.CategoryEnv {
ev := fmt.Sprintf("%s=%s", v.Key, v.Value)
o.envs = append(o.envs, ev)
}
}
writer := logs.NewPhaseWriter(o.ctx, logs.PhaseWriterOptions{
RunID: run.ID,
Phase: run.Phase(),
Writer: o.logs,
})
defer writer.Close()
o.out = writer
// dump info if in debug mode
if o.config.Debug {
hostname, err := os.Hostname()
if err != nil {
return err
}
fmt.Fprintln(o.out)
fmt.Fprintln(o.out, "Debug mode enabled")
fmt.Fprintln(o.out, "------------------")
fmt.Fprintf(o.out, "Hostname: %s\n", hostname)
fmt.Fprintf(o.out, "External agent: %t\n", o.isPoolAgent)
fmt.Fprintf(o.out, "Sandbox mode: %t\n", o.config.Sandbox)
fmt.Fprintln(o.out, "------------------")
fmt.Fprintln(o.out)
}
// compile list of steps comprising operation
type step func(context.Context) error
steps := []step{
o.downloadTerraform,
o.downloadConfig,
o.writeTerraformVars,
o.deleteBackendConfig,
o.downloadState,
}
switch run.Phase() {
case internal.PlanPhase:
steps = append(steps, o.terraformInit)
steps = append(steps, o.terraformPlan)
steps = append(steps, o.convertPlanToJSON)
steps = append(steps, o.uploadPlan)
steps = append(steps, o.uploadJSONPlan)
steps = append(steps, o.uploadLockFile)
case internal.ApplyPhase:
// Download lock file from plan phase for the apply phase, to ensure
// same providers are used in both phases.
steps = append(steps, o.downloadLockFile)
steps = append(steps, o.downloadPlanFile)
steps = append(steps, o.terraformInit)
steps = append(steps, o.terraformApply)
}
// do each step
for _, step := range steps {
// skip remaining steps if op is canceled
if o.canceled {
return fmt.Errorf("execution canceled")
}
// do step
if err := step(o.ctx); err != nil {
// write error message to output
errbuilder := strings.Builder{}
errbuilder.WriteRune('\n')
red := color.New(color.FgHiRed)
red.EnableColor() // force color on non-tty output
red.Fprint(&errbuilder, "Error: ")
errbuilder.WriteString(err.Error())
errbuilder.WriteRune('\n')
fmt.Fprint(o.out, errbuilder.String())
return err
}
}
return nil
}
func (o *operation) cancel(force, sendSignal bool) {
o.canceled = true
// cancel context only if forced and if there is a context to cancel
if force && o.cancelfn != nil {
o.cancelfn()
}
// signal current process if there is one.
if sendSignal && o.proc != nil {
if force {
o.V(2).Info("sending SIGKILL to terraform process", "pid", o.proc.Pid)
o.proc.Signal(os.Kill) //nolint:errcheck
} else {
o.V(2).Info("sending SIGINT to terraform process", "pid", o.proc.Pid)
o.proc.Signal(os.Interrupt) //nolint:errcheck
}
}
}
type (
// executionOptions are options that modify the execution of a process.
executionOptions struct {
sandboxIfEnabled bool
redirectStdout *string
}
executionOptionFunc func(*executionOptions)
)
// sandboxIfEnabled sandboxes the execution process *if* the daemon is configured
// with a sandbox.
func sandboxIfEnabled() executionOptionFunc {
return func(e *executionOptions) {
e.sandboxIfEnabled = true
}
}
// redirectStdout redirects stdout to the destination path.
func redirectStdout(dst string) executionOptionFunc {
return func(e *executionOptions) {
e.redirectStdout = &dst
}
}
// execute executes a process.
func (o *operation) execute(args []string, funcs ...executionOptionFunc) error {
if len(args) == 0 {
return fmt.Errorf("missing command name")
}
var opts executionOptions
for _, fn := range funcs {
fn(&opts)
}
if opts.sandboxIfEnabled && o.config.Sandbox {
args = o.addSandboxWrapper(args)
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = o.workdir.String()
cmd.Env = os.Environ()
cmd.Env = append(os.Environ(), o.envs...)
if opts.redirectStdout != nil {
dst, err := os.Create(path.Join(o.workdir.String(), *opts.redirectStdout))
if err != nil {
return err
}
defer dst.Close()
cmd.Stdout = dst
} else {
cmd.Stdout = o.out
}
// send stderr to both output (for sending to client) and to
// buffer, so that upon error its contents can be relayed.
stderr := new(bytes.Buffer)
cmd.Stderr = io.MultiWriter(o.out, stderr)
if err := cmd.Start(); err != nil {
return err
}
o.proc = cmd.Process
if err := cmd.Wait(); err != nil {
return fmt.Errorf("%w: %s", err, cleanStderr(stderr.String()))
}
return nil
}
// addSandboxWrapper wraps the args within a bubblewrap sandbox.
func (o *operation) addSandboxWrapper(args []string) []string {
bargs := []string{
"bwrap",
"--ro-bind", args[0], path.Join("/bin", path.Base(args[0])),
"--bind", o.root, "/config",
// for DNS lookups
"--ro-bind", "/etc/resolv.conf", "/etc/resolv.conf",
// for verifying SSL connections
"--ro-bind", internal.SSLCertsDir(), internal.SSLCertsDir(),
"--chdir", path.Join("/config", o.relative),
// terraform v1.0.10 (but not v1.2.2) reads /proc/self/exe.
"--proc", "/proc",
// avoids provider error "failed to read schema..."
"--tmpfs", "/tmp",
}
if o.config.PluginCache {
bargs = append(bargs, "--ro-bind", PluginCacheDir, PluginCacheDir)
}
bargs = append(bargs, path.Join("/bin", path.Base(args[0])))
return append(bargs, args[1:]...)
}
func (o *operation) downloadTerraform(ctx context.Context) error {
var err error
o.terraformPath, err = o.downloader.Download(ctx, o.TerraformVersion, o.out)
return err
}
func (o *operation) downloadConfig(ctx context.Context) error {
cv, err := o.configs.DownloadConfig(ctx, o.ConfigurationVersionID)
if err != nil {
return fmt.Errorf("unable to download config: %w", err)
}
// Decompress and untar config into root dir
if err := internal.Unpack(bytes.NewBuffer(cv), o.root); err != nil {
return fmt.Errorf("unable to unpack config: %w", err)
}
return nil
}
func (o *operation) deleteBackendConfig(ctx context.Context) error {
if err := internal.RewriteHCL(o.workdir.String(), internal.RemoveBackendBlock); err != nil {
return fmt.Errorf("removing backend config: %w", err)
}
return nil
}
// downloadState downloads current state to disk. If there is no state yet then
// nothing will be downloaded and no error will be reported.
func (o *operation) downloadState(ctx context.Context) error {
statefile, err := o.state.DownloadCurrent(ctx, o.WorkspaceID)
if errors.Is(err, internal.ErrResourceNotFound) {
return nil
} else if err != nil {
return fmt.Errorf("downloading state version: %w", err)
}
if err := o.writeFile(localStateFilename, statefile); err != nil {
return fmt.Errorf("saving state to local disk: %w", err)
}
return nil
}
// downloadLockFile downloads the .terraform.lock.hcl file into the working
// directory. If one has not been uploaded then this will simply write an empty
// file, which is harmless.
func (o *operation) downloadLockFile(ctx context.Context) error {
lockFile, err := o.runs.GetLockFile(ctx, o.ID)
if err != nil {
return err
}
return o.writeFile(lockFilename, lockFile)
}
func (o *operation) writeTerraformVars(ctx context.Context) error {
if err := variable.WriteTerraformVars(o.workdir.String(), o.variables); err != nil {
return fmt.Errorf("writing terraform.fvars: %w", err)
}
return nil
}
func (o *operation) terraformInit(ctx context.Context) error {
return o.execute([]string{o.terraformPath, "init"})
}
func (o *operation) terraformPlan(ctx context.Context) error {
args := []string{"plan"}
if o.IsDestroy {
args = append(args, "-destroy")
}
args = append(args, "-out="+planFilename)
return o.execute(append([]string{o.terraformPath}, args...))
}
func (o *operation) terraformApply(ctx context.Context) (err error) {
// prior to running an apply, capture info about local state file
// so we can detect changes...
statePath := filepath.Join(o.workdir.String(), localStateFilename)
stateInfoBefore, _ := os.Stat(statePath)
// ...and after the apply finishes, determine if there were changes, and if
// so, create a new state version. We do this even if the apply failed
// because since terraform v1.5, an apply can persist partial updates:
//
// https://github.com/hashicorp/terraform/pull/32680
defer func() {
stateInfoAfter, _ := os.Stat(statePath)
if stateInfoAfter == nil {
// no state file found
return
}
if stateInfoBefore != nil && stateInfoAfter.ModTime().Equal(stateInfoBefore.ModTime()) {
// no change to state file
return
}
// either there was no state file before and there is one now, or the
// state file modification time has changed. In either case we upload
// the new state.
if stateErr := o.uploadState(ctx); stateErr != nil {
err = errors.Join(err, stateErr)
}
}()
args := []string{"apply"}
if o.IsDestroy {
args = append(args, "-destroy")
}
args = append(args, planFilename)
return o.execute(append([]string{o.terraformPath}, args...), sandboxIfEnabled())
}
func (o *operation) convertPlanToJSON(ctx context.Context) error {
args := []string{"show", "-json", planFilename}
return o.execute(
append([]string{o.terraformPath}, args...),
redirectStdout(jsonPlanFilename),
)
}
func (o *operation) uploadPlan(ctx context.Context) error {
file, err := o.readFile(planFilename)
if err != nil {
return err
}
if err := o.runs.UploadPlanFile(ctx, o.ID, file, run.PlanFormatBinary); err != nil {
return fmt.Errorf("unable to upload plan: %w", err)
}
return nil
}
func (o *operation) uploadJSONPlan(ctx context.Context) error {
jsonFile, err := o.readFile(jsonPlanFilename)
if err != nil {
return err
}
if err := o.runs.UploadPlanFile(ctx, o.ID, jsonFile, run.PlanFormatJSON); err != nil {
return fmt.Errorf("unable to upload JSON plan: %w", err)
}
return nil
}
func (o *operation) uploadLockFile(ctx context.Context) error {
lockFile, err := o.readFile(lockFilename)
if errors.Is(err, fs.ErrNotExist) {
// there is no lock file to upload, which is ok
return nil
} else if err != nil {
return fmt.Errorf("reading lock file: %w", err)
}
if err := o.runs.UploadLockFile(ctx, o.ID, lockFile); err != nil {
return fmt.Errorf("unable to upload lock file: %w", err)
}
return nil
}
func (o *operation) downloadPlanFile(ctx context.Context) error {
plan, err := o.runs.GetPlanFile(ctx, o.ID, run.PlanFormatBinary)
if err != nil {
return err
}
return o.writeFile(planFilename, plan)
}
// uploadState reads, parses, and uploads terraform state
func (o *operation) uploadState(ctx context.Context) error {
statefile, err := o.readFile(localStateFilename)
if err != nil {
return err
}
// extract serial from state file
var f state.File
if err := json.Unmarshal(statefile, &f); err != nil {
return err
}
_, err = o.state.Create(ctx, state.CreateStateVersionOptions{
WorkspaceID: &o.WorkspaceID,
State: statefile,
Serial: &f.Serial,
})
return err
}
// cleanStderr cleans up stderr output to make it suitable for logging:
// newlines, ansi escape sequences, and non-ascii characters are removed
func cleanStderr(stderr string) string {
stderr = internal.StripAnsi(stderr)
stderr = ascii.ReplaceAllLiteralString(stderr, "")
stderr = strings.Join(strings.Fields(stderr), " ")
return stderr
}