2017-08-30 03:24:12 +02:00
|
|
|
// Copyright 2017, Pulumi Corporation. All rights reserved.
|
|
|
|
|
2017-08-23 01:56:15 +02:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2017-09-07 16:25:08 +02:00
|
|
|
goerr "github.com/pkg/errors"
|
|
|
|
|
2017-08-23 01:56:15 +02:00
|
|
|
"github.com/pulumi/pulumi-fabric/pkg/compiler/errors"
|
2017-09-09 22:43:51 +02:00
|
|
|
"github.com/pulumi/pulumi-fabric/pkg/diag"
|
2017-08-23 01:56:15 +02:00
|
|
|
"github.com/pulumi/pulumi-fabric/pkg/diag/colors"
|
|
|
|
"github.com/pulumi/pulumi-fabric/pkg/resource"
|
|
|
|
"github.com/pulumi/pulumi-fabric/pkg/resource/deploy"
|
|
|
|
"github.com/pulumi/pulumi-fabric/pkg/tokens"
|
|
|
|
"github.com/pulumi/pulumi-fabric/pkg/util/contract"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DeployOptions struct {
|
|
|
|
Environment string // the environment we are deploying into
|
|
|
|
Package string // the package we are deploying (or "" to use the default)
|
2017-08-24 01:57:03 +02:00
|
|
|
Analyzers []string // an optional set of analyzers to run as part of this deployment.
|
2017-08-23 01:56:15 +02:00
|
|
|
Debug bool // true to enable resource debugging output.
|
|
|
|
DryRun bool // true if we should just print the plan without performing it.
|
|
|
|
ShowConfig bool // true to show the configuration variables being used.
|
|
|
|
ShowReplacementSteps bool // true to show the replacement steps in the plan.
|
|
|
|
ShowSames bool // true to show the resources that aren't updated, in addition to those that are.
|
|
|
|
Summary bool // true if we should only summarize resources and operations.
|
|
|
|
}
|
|
|
|
|
2017-08-23 19:58:35 +02:00
|
|
|
func (eng *Engine) Deploy(opts DeployOptions) error {
|
2017-09-09 22:43:51 +02:00
|
|
|
// Initialize the diagnostics logger with the right stuff.
|
|
|
|
eng.InitDiag(diag.FormatOptions{
|
|
|
|
Colors: true,
|
|
|
|
Debug: opts.Debug,
|
|
|
|
})
|
|
|
|
|
2017-08-23 19:58:35 +02:00
|
|
|
info, err := eng.initEnvCmdName(tokens.QName(opts.Environment), opts.Package)
|
2017-08-23 01:56:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-23 19:58:35 +02:00
|
|
|
return eng.deployLatest(info, deployOptions{
|
2017-08-23 01:56:15 +02:00
|
|
|
Debug: opts.Debug,
|
|
|
|
Destroy: false,
|
|
|
|
DryRun: opts.DryRun,
|
|
|
|
Analyzers: opts.Analyzers,
|
|
|
|
ShowConfig: opts.ShowConfig,
|
|
|
|
ShowReplacementSteps: opts.ShowReplacementSteps,
|
|
|
|
ShowSames: opts.ShowSames,
|
|
|
|
Summary: opts.Summary,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type deployOptions struct {
|
|
|
|
Debug bool // true to enable resource debugging output.
|
|
|
|
Create bool // true if we are creating resources.
|
|
|
|
Destroy bool // true if we are destroying the environment.
|
|
|
|
DryRun bool // true if we should just print the plan without performing it.
|
|
|
|
Analyzers []string // an optional set of analyzers to run as part of this deployment.
|
|
|
|
ShowConfig bool // true to show the configuration variables being used.
|
|
|
|
ShowReplacementSteps bool // true to show the replacement steps in the plan.
|
|
|
|
ShowSames bool // true to show the resources that aren't updated, in addition to those that are.
|
|
|
|
Summary bool // true if we should only summarize resources and operations.
|
|
|
|
DOT bool // true if we should print the DOT file for this plan.
|
|
|
|
}
|
|
|
|
|
2017-08-23 19:58:35 +02:00
|
|
|
func (eng *Engine) deployLatest(info *envCmdInfo, opts deployOptions) error {
|
|
|
|
result, err := eng.plan(info, opts)
|
2017-08-23 01:56:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if result != nil {
|
|
|
|
defer contract.IgnoreClose(result)
|
|
|
|
if opts.DryRun {
|
|
|
|
// If a dry run, just print the plan, don't actually carry out the deployment.
|
2017-08-23 19:58:35 +02:00
|
|
|
if err := eng.printPlan(result, opts); err != nil {
|
2017-08-23 01:56:15 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise, we will actually deploy the latest bits.
|
|
|
|
var header bytes.Buffer
|
|
|
|
printPrelude(&header, result, opts, false)
|
|
|
|
header.WriteString(fmt.Sprintf("%vDeploying changes:%v\n", colors.SpecUnimportant, colors.Reset))
|
2017-08-23 19:58:35 +02:00
|
|
|
fmt.Fprint(eng.Stdout, colors.Colorize(&header))
|
2017-08-23 01:56:15 +02:00
|
|
|
|
|
|
|
// Create an object to track progress and perform the actual operations.
|
|
|
|
start := time.Now()
|
2017-08-23 19:58:35 +02:00
|
|
|
progress := newProgress(opts, eng)
|
2017-08-23 01:56:15 +02:00
|
|
|
summary, _, _, err := result.Plan.Apply(progress)
|
2017-08-27 09:38:17 +02:00
|
|
|
if err != nil && summary == nil {
|
|
|
|
// Something went wrong, and we have no checkpoint to save.
|
|
|
|
return err
|
|
|
|
}
|
2017-08-23 01:56:15 +02:00
|
|
|
contract.Assert(summary != nil)
|
|
|
|
|
|
|
|
// Print a summary.
|
|
|
|
var footer bytes.Buffer
|
|
|
|
// Print out the total number of steps performed (and their kinds), the duration, and any summary info.
|
|
|
|
if c := printChangeSummary(&footer, progress.Ops, false); c != 0 {
|
|
|
|
footer.WriteString(fmt.Sprintf("%vDeployment duration: %v%v\n",
|
|
|
|
colors.SpecUnimportant, time.Since(start), colors.Reset))
|
|
|
|
}
|
|
|
|
|
|
|
|
if progress.MaybeCorrupt {
|
|
|
|
footer.WriteString(fmt.Sprintf(
|
|
|
|
"%vA catastrophic error occurred; resources states may be unknown%v\n",
|
|
|
|
colors.SpecAttention, colors.Reset))
|
|
|
|
}
|
|
|
|
|
2017-08-30 02:57:01 +02:00
|
|
|
// Now save the updated snapshot Notee that if a failure has occurred, the Apply routine above will
|
|
|
|
// have returned a safe checkpoint.
|
2017-08-23 01:56:15 +02:00
|
|
|
targ := result.Info.Target
|
2017-08-30 03:25:50 +02:00
|
|
|
_ = eng.Environment.SaveEnvironment(targ, summary.Snap())
|
2017-08-23 01:56:15 +02:00
|
|
|
|
2017-08-23 19:58:35 +02:00
|
|
|
fmt.Fprint(eng.Stdout, colors.Colorize(&footer))
|
2017-09-09 21:42:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-06 18:35:35 +02:00
|
|
|
if !eng.Diag().Success() {
|
|
|
|
// If any error that wasn't printed above, be sure to make it evident in the output.
|
2017-09-07 16:25:08 +02:00
|
|
|
return goerr.New("One or more errors occurred during this deployment")
|
2017-09-06 18:35:35 +02:00
|
|
|
}
|
2017-08-23 01:56:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// deployProgress pretty-prints the plan application process as it goes.
|
|
|
|
type deployProgress struct {
|
|
|
|
Steps int
|
|
|
|
Ops map[deploy.StepOp]int
|
|
|
|
MaybeCorrupt bool
|
|
|
|
Opts deployOptions
|
2017-08-23 19:58:35 +02:00
|
|
|
Engine *Engine
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|
|
|
|
|
2017-08-23 19:58:35 +02:00
|
|
|
func newProgress(opts deployOptions, engine *Engine) *deployProgress {
|
2017-08-23 01:56:15 +02:00
|
|
|
return &deployProgress{
|
2017-08-23 19:58:35 +02:00
|
|
|
Steps: 0,
|
|
|
|
Ops: make(map[deploy.StepOp]int),
|
|
|
|
Opts: opts,
|
|
|
|
Engine: engine,
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (prog *deployProgress) Before(step deploy.Step) {
|
|
|
|
if shouldShow(step, prog.Opts) {
|
|
|
|
var b bytes.Buffer
|
|
|
|
printStep(&b, step, prog.Opts.Summary, false, "")
|
2017-08-23 19:58:35 +02:00
|
|
|
fmt.Fprint(prog.Engine.Stdout, colors.Colorize(&b))
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (prog *deployProgress) After(step deploy.Step, status resource.Status, err error) {
|
|
|
|
stepop := step.Op()
|
|
|
|
if err != nil {
|
|
|
|
// Issue a true, bonafide error.
|
2017-08-23 19:58:35 +02:00
|
|
|
prog.Engine.Diag().Errorf(errors.ErrorPlanApplyFailed, err)
|
2017-08-23 01:56:15 +02:00
|
|
|
|
|
|
|
// Print the state of the resource; we don't issue the error, because the deploy above will do that.
|
|
|
|
var b bytes.Buffer
|
|
|
|
stepnum := prog.Steps + 1
|
|
|
|
b.WriteString(fmt.Sprintf("Step #%v failed [%v]: ", stepnum, stepop))
|
|
|
|
switch status {
|
|
|
|
case resource.StatusOK:
|
|
|
|
b.WriteString(colors.SpecNote)
|
|
|
|
b.WriteString("provider successfully recovered from this failure")
|
|
|
|
case resource.StatusUnknown:
|
|
|
|
b.WriteString(colors.SpecAttention)
|
|
|
|
b.WriteString("this failure was catastrophic and the provider cannot guarantee recovery")
|
|
|
|
prog.MaybeCorrupt = true
|
|
|
|
default:
|
|
|
|
contract.Failf("Unrecognized resource state: %v", status)
|
|
|
|
}
|
|
|
|
b.WriteString(colors.Reset)
|
|
|
|
b.WriteString("\n")
|
2017-08-23 19:58:35 +02:00
|
|
|
fmt.Fprint(prog.Engine.Stdout, colors.Colorize(&b))
|
2017-08-23 01:56:15 +02:00
|
|
|
} else {
|
|
|
|
// Increment the counters.
|
|
|
|
if step.Logical() {
|
|
|
|
prog.Steps++
|
|
|
|
prog.Ops[stepop]++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print out any output properties that got created as a result of this operation.
|
|
|
|
if shouldShow(step, prog.Opts) && !prog.Opts.Summary {
|
|
|
|
var b bytes.Buffer
|
|
|
|
printResourceOutputProperties(&b, step, "")
|
2017-08-23 19:58:35 +02:00
|
|
|
fmt.Fprint(prog.Engine.Stdout, colors.Colorize(&b))
|
2017-08-23 01:56:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|