2018-09-19 21:01:40 +02:00
|
|
|
package remote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
tfe "github.com/hashicorp/go-tfe"
|
|
|
|
"github.com/hashicorp/terraform/backend"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2018-09-20 17:05:51 +02:00
|
|
|
func (b *Remote) opApply(stopCtx, cancelCtx context.Context, op *backend.Operation) (*tfe.Run, error) {
|
2018-09-19 21:01:40 +02:00
|
|
|
log.Printf("[INFO] backend/remote: starting Apply operation")
|
|
|
|
|
|
|
|
// 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-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
2018-10-04 22:53:56 +02:00
|
|
|
if !w.Permissions.CanUpdate {
|
|
|
|
return nil, fmt.Errorf(strings.TrimSpace(
|
|
|
|
fmt.Sprintf(applyErrNoUpdateRights, b.hostname, b.organization, op.Workspace)))
|
|
|
|
}
|
|
|
|
|
2018-09-19 21:01:40 +02:00
|
|
|
if w.VCSRepo != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return nil, fmt.Errorf(strings.TrimSpace(applyErrVCSNotSupported))
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 15:25:17 +02:00
|
|
|
if op.Parallelism != defaultParallelism {
|
|
|
|
return nil, fmt.Errorf(strings.TrimSpace(applyErrParallelismNotSupported))
|
|
|
|
}
|
|
|
|
|
2018-09-19 21:01:40 +02:00
|
|
|
if op.Plan != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return nil, fmt.Errorf(strings.TrimSpace(applyErrPlanNotSupported))
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 15:25:17 +02:00
|
|
|
if !op.PlanRefresh {
|
|
|
|
return nil, fmt.Errorf(strings.TrimSpace(applyErrNoRefreshNotSupported))
|
|
|
|
}
|
|
|
|
|
2018-09-19 21:01:40 +02:00
|
|
|
if op.Targets != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return nil, fmt.Errorf(strings.TrimSpace(applyErrTargetsNotSupported))
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
2018-10-05 15:25:17 +02:00
|
|
|
if op.Variables != nil {
|
|
|
|
return nil, fmt.Errorf(strings.TrimSpace(
|
|
|
|
fmt.Sprintf(applyErrVariablesNotSupported, b.hostname, b.organization, op.Workspace)))
|
|
|
|
}
|
|
|
|
|
2018-09-19 21:01:40 +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(applyErrNoConfig))
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 17:05:51 +02:00
|
|
|
// Run the plan phase.
|
2018-09-19 21:01:40 +02:00
|
|
|
r, err := b.plan(stopCtx, cancelCtx, op, w)
|
|
|
|
if err != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return r, err
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 17:05:51 +02:00
|
|
|
// Retrieve the run to get its current status.
|
|
|
|
r, err = b.client.Runs.Read(stopCtx, r.ID)
|
|
|
|
if err != nil {
|
|
|
|
return r, generalError("error retrieving run", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return if there are no changes or the run errored. We return
|
|
|
|
// without an error, even if the run errored, as the error is
|
|
|
|
// already displayed by the output of the remote run.
|
|
|
|
if !r.HasChanges || r.Status == tfe.RunErrored {
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check any configured sentinel policies.
|
2018-09-19 21:01:40 +02:00
|
|
|
if len(r.PolicyChecks) > 0 {
|
|
|
|
err = b.checkPolicy(stopCtx, cancelCtx, op, r)
|
|
|
|
if err != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the run to get its current status.
|
|
|
|
r, err = b.client.Runs.Read(stopCtx, r.ID)
|
|
|
|
if err != nil {
|
|
|
|
return r, generalError("error retrieving run", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return if the run cannot be confirmed.
|
2018-10-08 12:31:21 +02:00
|
|
|
if !w.AutoApply && !r.Actions.IsConfirmable {
|
2018-09-20 17:05:51 +02:00
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2018-10-04 22:53:56 +02:00
|
|
|
// Since we already checked the permissions before creating the run
|
|
|
|
// this should never happen. But it doesn't hurt to keep this in as
|
|
|
|
// a safeguard for any unexpected situations.
|
2018-10-08 12:31:21 +02:00
|
|
|
if !w.AutoApply && !r.Permissions.CanApply {
|
2018-09-20 17:05:51 +02:00
|
|
|
// Make sure we discard the run if possible.
|
|
|
|
if r.Actions.IsDiscardable {
|
|
|
|
err = b.client.Runs.Discard(stopCtx, r.ID, tfe.RunDiscardOptions{})
|
|
|
|
if err != nil {
|
|
|
|
if op.Destroy {
|
|
|
|
return r, generalError("error disarding destroy", err)
|
|
|
|
}
|
|
|
|
return r, generalError("error disarding apply", err)
|
|
|
|
}
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
2018-09-20 17:05:51 +02:00
|
|
|
return r, fmt.Errorf(strings.TrimSpace(
|
2018-10-05 15:25:17 +02:00
|
|
|
fmt.Sprintf(applyErrNoApplyRights, b.hostname, b.organization, op.Workspace)))
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 12:31:21 +02:00
|
|
|
mustConfirm := (op.UIIn != nil && op.UIOut != nil) &&
|
2018-09-26 17:27:12 +02:00
|
|
|
((op.Destroy && (!op.DestroyForce && !op.AutoApprove)) || (!op.Destroy && !op.AutoApprove))
|
2018-10-08 12:31:21 +02:00
|
|
|
|
|
|
|
if !w.AutoApply {
|
|
|
|
if mustConfirm {
|
|
|
|
opts := &terraform.InputOpts{Id: "approve"}
|
|
|
|
|
|
|
|
if op.Destroy {
|
|
|
|
opts.Query = "\nDo you really want to destroy all resources in workspace \"" + op.Workspace + "\"?"
|
|
|
|
opts.Description = "Terraform will destroy all your managed infrastructure, as shown above.\n" +
|
|
|
|
"There is no undo. Only 'yes' will be accepted to confirm."
|
|
|
|
} else {
|
|
|
|
opts.Query = "\nDo you want to perform these actions in workspace \"" + op.Workspace + "\"?"
|
|
|
|
opts.Description = "Terraform will perform the actions described above.\n" +
|
|
|
|
"Only 'yes' will be accepted to approve."
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = b.confirm(stopCtx, op, opts, r, "yes"); err != nil {
|
|
|
|
return r, err
|
|
|
|
}
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 12:31:21 +02:00
|
|
|
err = b.client.Runs.Apply(stopCtx, r.ID, tfe.RunApplyOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return r, generalError("error approving the apply command", err)
|
2018-09-20 17:05:51 +02:00
|
|
|
}
|
2018-10-08 12:31:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't need to ask for confirmation, insert a blank
|
|
|
|
// line to separate the ouputs.
|
|
|
|
if w.AutoApply || !mustConfirm {
|
2018-09-20 17:05:51 +02:00
|
|
|
if b.CLI != nil {
|
|
|
|
b.CLI.Output("")
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 17:50:21 +02:00
|
|
|
r, err = b.waitForRun(stopCtx, cancelCtx, op, "apply", r, w)
|
|
|
|
if err != nil {
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.CLI != nil {
|
|
|
|
// Insert a blank line to separate the ouputs.
|
|
|
|
b.CLI.Output("")
|
|
|
|
}
|
|
|
|
|
2018-09-19 21:01:40 +02:00
|
|
|
logs, err := b.client.Applies.Logs(stopCtx, r.Apply.ID)
|
|
|
|
if err != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return r, generalError("error retrieving logs", err)
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(logs)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
2018-10-11 22:08:09 +02:00
|
|
|
if scanner.Text() == "\x02" || scanner.Text() == "\x03" {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-19 21:01:40 +02:00
|
|
|
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-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 17:05:51 +02:00
|
|
|
return r, nil
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
2018-10-04 23:03:40 +02:00
|
|
|
func (b *Remote) checkPolicy(stopCtx, cancelCtx context.Context, op *backend.Operation, r *tfe.Run) error {
|
2018-09-20 17:05:51 +02:00
|
|
|
if b.CLI != nil {
|
|
|
|
b.CLI.Output("\n------------------------------------------------------------------------\n")
|
|
|
|
}
|
2018-09-19 21:01:40 +02:00
|
|
|
for _, pc := range r.PolicyChecks {
|
2018-10-04 23:03:40 +02:00
|
|
|
logs, err := b.client.PolicyChecks.Logs(stopCtx, pc.ID)
|
|
|
|
if err != nil {
|
|
|
|
return generalError("error retrieving policy check logs", err)
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(logs)
|
2018-10-01 12:00:23 +02:00
|
|
|
|
2018-10-04 23:03:40 +02:00
|
|
|
// Retrieve the policy check to get its current status.
|
|
|
|
pc, err := b.client.PolicyChecks.Read(stopCtx, pc.ID)
|
|
|
|
if err != nil {
|
|
|
|
return generalError("error retrieving policy check", err)
|
2018-09-20 17:05:51 +02:00
|
|
|
}
|
|
|
|
|
2018-09-19 21:01:40 +02:00
|
|
|
var msgPrefix string
|
|
|
|
switch pc.Scope {
|
|
|
|
case tfe.PolicyScopeOrganization:
|
|
|
|
msgPrefix = "Organization policy check"
|
|
|
|
case tfe.PolicyScopeWorkspace:
|
|
|
|
msgPrefix = "Workspace policy check"
|
|
|
|
default:
|
|
|
|
msgPrefix = fmt.Sprintf("Unknown policy check (%s)", pc.Scope)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.CLI != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
b.CLI.Output(b.Colorize().Color(msgPrefix + ":\n"))
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
if b.CLI != nil {
|
|
|
|
b.CLI.Output(b.Colorize().Color(scanner.Text()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
return generalError("error reading logs", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch pc.Status {
|
2018-10-04 23:03:40 +02:00
|
|
|
case tfe.PolicyPasses:
|
|
|
|
if b.CLI != nil {
|
|
|
|
b.CLI.Output("\n------------------------------------------------------------------------")
|
|
|
|
}
|
|
|
|
continue
|
2018-09-19 21:01:40 +02:00
|
|
|
case tfe.PolicyErrored:
|
|
|
|
return fmt.Errorf(msgPrefix + " errored.")
|
|
|
|
case tfe.PolicyHardFailed:
|
|
|
|
return fmt.Errorf(msgPrefix + " hard failed.")
|
|
|
|
case tfe.PolicySoftFailed:
|
2018-10-09 20:12:33 +02:00
|
|
|
if op.UIOut == nil || op.UIIn == nil || op.AutoApprove ||
|
2018-09-19 21:01:40 +02:00
|
|
|
!pc.Actions.IsOverridable || !pc.Permissions.CanOverride {
|
|
|
|
return fmt.Errorf(msgPrefix + " soft failed.")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Unknown or unexpected policy state: %s", pc.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := &terraform.InputOpts{
|
|
|
|
Id: "override",
|
2018-09-20 17:05:51 +02:00
|
|
|
Query: "\nDo you want to override the soft failed policy check?",
|
|
|
|
Description: "Only 'override' will be accepted to override.",
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 17:05:51 +02:00
|
|
|
if err = b.confirm(stopCtx, op, opts, r, "override"); err != nil {
|
2018-09-19 21:01:40 +02:00
|
|
|
return err
|
|
|
|
}
|
2018-09-20 17:05:51 +02:00
|
|
|
|
|
|
|
if _, err = b.client.PolicyChecks.Override(stopCtx, pc.ID); err != nil {
|
|
|
|
return generalError("error overriding policy check", err)
|
|
|
|
}
|
2018-10-01 12:00:23 +02:00
|
|
|
|
|
|
|
if b.CLI != nil {
|
|
|
|
b.CLI.Output("------------------------------------------------------------------------")
|
|
|
|
}
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-20 17:05:51 +02:00
|
|
|
func (b *Remote) confirm(stopCtx context.Context, op *backend.Operation, opts *terraform.InputOpts, r *tfe.Run, keyword string) error {
|
2018-09-19 21:01:40 +02:00
|
|
|
v, err := op.UIIn.Input(opts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error asking %s: %v", opts.Id, err)
|
|
|
|
}
|
2018-09-20 17:05:51 +02:00
|
|
|
if v != keyword {
|
|
|
|
// Retrieve the run again to get its current status.
|
|
|
|
r, err = b.client.Runs.Read(stopCtx, r.ID)
|
2018-09-19 21:01:40 +02:00
|
|
|
if err != nil {
|
2018-09-20 17:05:51 +02:00
|
|
|
return generalError("error retrieving run", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we discard the run if possible.
|
|
|
|
if r.Actions.IsDiscardable {
|
|
|
|
err = b.client.Runs.Discard(stopCtx, r.ID, tfe.RunDiscardOptions{})
|
|
|
|
if err != nil {
|
|
|
|
if op.Destroy {
|
|
|
|
return generalError("error disarding destroy", err)
|
|
|
|
}
|
|
|
|
return generalError("error disarding apply", err)
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even if the run was disarding successfully, we still
|
|
|
|
// return an error as the apply command was cancelled.
|
|
|
|
if op.Destroy {
|
2018-09-20 17:05:51 +02:00
|
|
|
return errors.New("Destroy discarded.")
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
2018-09-20 17:05:51 +02:00
|
|
|
return errors.New("Apply discarded.")
|
2018-09-19 21:01:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-04 22:53:56 +02:00
|
|
|
const applyErrNoUpdateRights = `
|
|
|
|
Insufficient rights to apply changes!
|
|
|
|
|
|
|
|
[reset][yellow]The provided credentials have insufficient rights to apply changes. In order
|
|
|
|
to apply changes at least write permissions on the workspace are required. To
|
|
|
|
queue a run that can be approved by someone else, please use the 'Queue Plan'
|
|
|
|
button in the web UI:
|
|
|
|
https://%s/app/%s/%s/runs[reset]
|
|
|
|
`
|
|
|
|
|
2018-09-19 21:01:40 +02:00
|
|
|
const applyErrVCSNotSupported = `
|
2018-10-04 22:53:56 +02:00
|
|
|
Apply not allowed for workspaces with a VCS connection.
|
2018-09-19 21:01:40 +02:00
|
|
|
|
2018-10-04 22:53:56 +02:00
|
|
|
A workspace that is connected to a VCS requires the VCS-driven workflow
|
2018-09-19 21:01:40 +02:00
|
|
|
to ensure that the VCS remains the single source of truth.
|
|
|
|
`
|
|
|
|
|
2018-10-05 15:25:17 +02:00
|
|
|
const applyErrParallelismNotSupported = `
|
|
|
|
Custom parallelism values are currently not supported!
|
|
|
|
|
|
|
|
The "remote" backend does not support setting a custom parallelism
|
|
|
|
value at this time.
|
|
|
|
`
|
|
|
|
|
2018-09-19 21:01:40 +02:00
|
|
|
const applyErrPlanNotSupported = `
|
|
|
|
Applying a saved plan is currently not supported!
|
|
|
|
|
2018-10-05 15:25:17 +02:00
|
|
|
The "remote" backend currently requires configuration to be present and
|
|
|
|
does not accept an existing saved plan as an argument at this time.
|
|
|
|
`
|
|
|
|
|
|
|
|
const applyErrNoRefreshNotSupported = `
|
|
|
|
Applying without refresh is currently not supported!
|
|
|
|
|
|
|
|
Currently the "remote" backend will always do an in-memory refresh of
|
|
|
|
the Terraform state prior to generating the plan.
|
2018-09-19 21:01:40 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const applyErrTargetsNotSupported = `
|
|
|
|
Resource targeting is currently not supported!
|
|
|
|
|
|
|
|
The "remote" backend does not support resource targeting at this time.
|
|
|
|
`
|
|
|
|
|
2018-10-05 15:25:17 +02:00
|
|
|
const applyErrVariablesNotSupported = `
|
|
|
|
Run variables are currently not supported!
|
|
|
|
|
|
|
|
The "remote" backend does not support setting run variables at this time.
|
|
|
|
Currently the only to way to pass variables to the remote backend is by
|
|
|
|
creating a '*.auto.tfvars' variables file. This file will automatically
|
|
|
|
be loaded by the "remote" backend when the workspace is configured to use
|
|
|
|
Terraform v0.10.0 or later.
|
|
|
|
|
|
|
|
Additionally you can also set variables on the workspace in the web UI:
|
|
|
|
https://%s/app/%s/%s/variables
|
|
|
|
`
|
|
|
|
|
2018-09-19 21:01:40 +02:00
|
|
|
const applyErrNoConfig = `
|
|
|
|
No configuration files found!
|
|
|
|
|
|
|
|
Apply requires configuration to be present. Applying without a configuration
|
|
|
|
would mark everything for destruction, which is normally not what is desired.
|
|
|
|
If you would like to destroy everything, please run 'terraform destroy' which
|
|
|
|
does not require any configuration files.
|
|
|
|
`
|
|
|
|
|
2018-09-20 17:05:51 +02:00
|
|
|
const applyErrNoApplyRights = `
|
|
|
|
Insufficient rights to approve the pending changes!
|
|
|
|
|
2018-10-04 22:53:56 +02:00
|
|
|
[reset][yellow]There are pending changes, but the provided credentials have insufficient rights
|
|
|
|
to approve them. The run will be discarded to prevent it from blocking the queue
|
|
|
|
waiting for external approval. To queue a run that can be approved by someone
|
|
|
|
else, please use the 'Queue Plan' button in the web UI:
|
2018-09-20 17:05:51 +02:00
|
|
|
https://%s/app/%s/%s/runs[reset]
|
|
|
|
`
|
|
|
|
|
2018-09-19 21:01:40 +02:00
|
|
|
const applyDefaultHeader = `
|
|
|
|
[reset][yellow]Running apply in the remote backend. Output will stream here. Pressing Ctrl-C
|
2018-09-20 17:05:51 +02:00
|
|
|
will cancel the remote apply if its still pending. If the apply started it
|
2018-09-19 21:01:40 +02:00
|
|
|
will stop streaming the logs, but will not stop the apply running remotely.
|
|
|
|
To view this run in a browser, visit:
|
|
|
|
https://%s/app/%s/%s/runs/%s[reset]
|
|
|
|
`
|