2018-07-04 17:24:49 +02:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
2018-09-19 21:01:40 +02:00
|
|
|
"errors"
|
2018-07-04 17:24:49 +02:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2018-08-30 22:18:43 +02:00
|
|
|
"path/filepath"
|
2018-07-04 17:24:49 +02:00
|
|
|
"strings"
|
2018-09-20 17:05:51 +02:00
|
|
|
"syscall"
|
2018-07-04 17:24:49 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
tfe "github.com/hashicorp/go-tfe"
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
)
|
|
|
|
|
2018-09-20 17:05:51 +02:00
|
|
|
func (b *Remote) opPlan(stopCtx, cancelCtx context.Context, op *backend.Operation) (*tfe.Run, error) {
|
2018-07-04 17:24:49 +02:00
|
|
|
log.Printf("[INFO] backend/remote: starting Plan operation")
|
|
|
|
|
|
|
|
if op.Plan != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return nil, fmt.Errorf(strings.TrimSpace(planErrPlanNotSupported))
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if op.PlanOutPath != "" {
|
2018-09-20 17:05:51 +02:00
|
|
|
return nil, fmt.Errorf(strings.TrimSpace(planErrOutPathNotSupported))
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if op.Targets != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return nil, fmt.Errorf(strings.TrimSpace(planErrTargetsNotSupported))
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (op.Module == nil || op.Module.Config().Dir == "") && !op.Destroy {
|
2018-09-20 17:05:51 +02:00
|
|
|
return nil, fmt.Errorf(strings.TrimSpace(planErrNoConfig))
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the workspace used to run this operation in.
|
|
|
|
w, err := b.client.Workspaces.Read(stopCtx, b.organization, op.Workspace)
|
|
|
|
if err != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return nil, generalError("error retrieving workspace", err)
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 17:05:51 +02:00
|
|
|
return b.plan(stopCtx, cancelCtx, op, w)
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Remote) plan(stopCtx, cancelCtx context.Context, op *backend.Operation, w *tfe.Workspace) (*tfe.Run, error) {
|
2018-07-04 17:24:49 +02:00
|
|
|
configOptions := tfe.ConfigurationVersionCreateOptions{
|
|
|
|
AutoQueueRuns: tfe.Bool(false),
|
2018-09-19 21:01:40 +02:00
|
|
|
Speculative: tfe.Bool(op.Type == backend.OperationTypePlan),
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cv, err := b.client.ConfigurationVersions.Create(stopCtx, w.ID, configOptions)
|
|
|
|
if err != nil {
|
2018-09-19 21:01:40 +02:00
|
|
|
return nil, generalError("error creating configuration version", err)
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var configDir string
|
|
|
|
if op.Module != nil && op.Module.Config().Dir != "" {
|
2018-08-30 22:18:43 +02:00
|
|
|
// Make sure to take the working directory into account by removing
|
|
|
|
// the working directory from the current path. This will result in
|
|
|
|
// a path that points to the expected root of the workspace.
|
|
|
|
configDir = filepath.Clean(strings.TrimSuffix(
|
|
|
|
filepath.Clean(op.Module.Config().Dir),
|
|
|
|
filepath.Clean(w.WorkingDirectory),
|
|
|
|
))
|
2018-07-04 17:24:49 +02:00
|
|
|
} else {
|
2018-08-30 22:18:43 +02:00
|
|
|
// We did a check earlier to make sure we either have a config dir,
|
|
|
|
// or the plan is run with -destroy. So this else clause will only
|
|
|
|
// be executed when we are destroying and doesn't need the config.
|
2018-07-04 17:24:49 +02:00
|
|
|
configDir, err = ioutil.TempDir("", "tf")
|
|
|
|
if err != nil {
|
2018-09-19 21:01:40 +02:00
|
|
|
return nil, generalError("error creating temporary directory", err)
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(configDir)
|
2018-08-30 22:18:43 +02:00
|
|
|
|
|
|
|
// Make sure the configured working directory exists.
|
|
|
|
err = os.MkdirAll(filepath.Join(configDir, w.WorkingDirectory), 0700)
|
|
|
|
if err != nil {
|
2018-09-19 21:01:40 +02:00
|
|
|
return nil, generalError(
|
|
|
|
"error creating temporary working directory", err)
|
2018-08-30 22:18:43 +02:00
|
|
|
}
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = b.client.ConfigurationVersions.Upload(stopCtx, cv.UploadURL, configDir)
|
|
|
|
if err != nil {
|
2018-09-19 21:01:40 +02:00
|
|
|
return nil, generalError("error uploading configuration files", err)
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uploaded := false
|
|
|
|
for i := 0; i < 60 && !uploaded; i++ {
|
|
|
|
select {
|
|
|
|
case <-stopCtx.Done():
|
2018-09-19 21:01:40 +02:00
|
|
|
return nil, context.Canceled
|
2018-07-04 17:24:49 +02:00
|
|
|
case <-cancelCtx.Done():
|
2018-09-19 21:01:40 +02:00
|
|
|
return nil, context.Canceled
|
2018-07-04 17:24:49 +02:00
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
cv, err = b.client.ConfigurationVersions.Read(stopCtx, cv.ID)
|
|
|
|
if err != nil {
|
2018-09-19 21:01:40 +02:00
|
|
|
return nil, generalError("error retrieving configuration version", err)
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if cv.Status == tfe.ConfigurationUploaded {
|
|
|
|
uploaded = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !uploaded {
|
2018-09-19 21:01:40 +02:00
|
|
|
return nil, generalError(
|
|
|
|
"error uploading configuration files", errors.New("operation timed out"))
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
runOptions := tfe.RunCreateOptions{
|
|
|
|
IsDestroy: tfe.Bool(op.Destroy),
|
|
|
|
Message: tfe.String("Queued manually using Terraform"),
|
|
|
|
ConfigurationVersion: cv,
|
|
|
|
Workspace: w,
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := b.client.Runs.Create(stopCtx, runOptions)
|
|
|
|
if err != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return r, generalError("error creating run", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the lock timeout is set,
|
|
|
|
if op.StateLockTimeout > 0 {
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-stopCtx.Done():
|
|
|
|
return
|
|
|
|
case <-cancelCtx.Done():
|
|
|
|
return
|
|
|
|
case <-time.After(op.StateLockTimeout):
|
|
|
|
// Retrieve the run to get its current status.
|
|
|
|
r, err := b.client.Runs.Read(cancelCtx, r.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] error reading run: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Status == tfe.RunPending {
|
|
|
|
if b.CLI != nil {
|
|
|
|
b.CLI.Output(b.Colorize().Color(strings.TrimSpace(lockTimeoutErr)))
|
|
|
|
}
|
2018-09-26 21:35:26 +02:00
|
|
|
p, err := os.FindProcess(os.Getpid())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[ERROR] error searching process ID: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.Signal(syscall.SIGINT)
|
2018-09-20 17:05:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
r, err = b.client.Runs.Read(stopCtx, r.ID)
|
|
|
|
if err != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return r, generalError("error retrieving run", err)
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.CLI != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
header := planDefaultHeader
|
|
|
|
if op.Type == backend.OperationTypeApply {
|
|
|
|
header = applyDefaultHeader
|
|
|
|
}
|
2018-07-04 17:24:49 +02:00
|
|
|
b.CLI.Output(b.Colorize().Color(strings.TrimSpace(fmt.Sprintf(
|
2018-09-20 17:05:51 +02:00
|
|
|
header, b.hostname, b.organization, op.Workspace, r.ID)) + "\n"))
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logs, err := b.client.Plans.Logs(stopCtx, r.Plan.ID)
|
|
|
|
if err != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return r, generalError("error retrieving logs", err)
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(logs)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
if b.CLI != nil {
|
|
|
|
b.CLI.Output(b.Colorize().Color(scanner.Text()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return r, generalError("error reading logs", err)
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
2018-09-19 21:01:40 +02:00
|
|
|
|
|
|
|
return r, nil
|
2018-07-04 17:24:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const planErrPlanNotSupported = `
|
|
|
|
Displaying a saved plan is currently not supported!
|
|
|
|
|
|
|
|
The "remote" backend currently requires configuration to be present
|
|
|
|
and does not accept an existing saved plan as an argument at this time.
|
|
|
|
`
|
|
|
|
|
|
|
|
const planErrOutPathNotSupported = `
|
|
|
|
Saving a generated plan is currently not supported!
|
|
|
|
|
|
|
|
The "remote" backend does not support saving the generated execution
|
|
|
|
plan locally at this time.
|
|
|
|
`
|
|
|
|
|
|
|
|
const planErrTargetsNotSupported = `
|
|
|
|
Resource targeting is currently not supported!
|
|
|
|
|
|
|
|
The "remote" backend does not support resource targeting at this time.
|
|
|
|
`
|
|
|
|
|
|
|
|
const planErrNoConfig = `
|
|
|
|
No configuration files found!
|
|
|
|
|
|
|
|
Plan requires configuration to be present. Planning without a configuration
|
|
|
|
would mark everything for destruction, which is normally not what is desired.
|
|
|
|
If you would like to destroy everything, please run plan with the "-destroy"
|
|
|
|
flag or create a single empty configuration file. Otherwise, please create
|
|
|
|
a Terraform configuration file in the path being executed and try again.
|
|
|
|
`
|
|
|
|
|
|
|
|
const planDefaultHeader = `
|
|
|
|
[reset][yellow]Running plan in the remote backend. Output will stream here. Pressing Ctrl-C
|
|
|
|
will stop streaming the logs, but will not stop the plan running remotely.
|
2018-09-19 21:01:40 +02:00
|
|
|
To view this run in a browser, visit:
|
2018-07-04 17:24:49 +02:00
|
|
|
https://%s/app/%s/%s/runs/%s[reset]
|
|
|
|
|
|
|
|
Waiting for the plan to start...
|
|
|
|
`
|
2018-09-20 17:05:51 +02:00
|
|
|
|
|
|
|
// The newline in this error is to make it look good in the CLI!
|
|
|
|
const lockTimeoutErr = `
|
|
|
|
[reset][red]Lock timeout exceeded, sending interrupt to cancel the remote operation.
|
|
|
|
[reset]
|
|
|
|
`
|