2018-09-05 16:20:25 +02:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package backend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
survey "gopkg.in/AlecAivazis/survey.v1"
|
|
|
|
surveycore "gopkg.in/AlecAivazis/survey.v1/core"
|
|
|
|
|
|
|
|
"github.com/pulumi/pulumi/pkg/apitype"
|
|
|
|
"github.com/pulumi/pulumi/pkg/backend/display"
|
|
|
|
"github.com/pulumi/pulumi/pkg/diag/colors"
|
|
|
|
"github.com/pulumi/pulumi/pkg/engine"
|
|
|
|
"github.com/pulumi/pulumi/pkg/resource"
|
|
|
|
"github.com/pulumi/pulumi/pkg/util/contract"
|
|
|
|
)
|
|
|
|
|
2018-09-21 22:57:57 +02:00
|
|
|
// ApplierOptions is a bag of configuration settings for an Applier.
|
|
|
|
type ApplierOptions struct {
|
|
|
|
// DryRun indiciates if the update should not change any resource state and instead just preview changes.
|
|
|
|
DryRun bool
|
|
|
|
// ShowLink indiciates if a link to the update persisted result should be displayed.
|
|
|
|
ShowLink bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// Applier applies the changes specified by this update operation against the target stack.
|
2018-09-05 16:20:25 +02:00
|
|
|
type Applier func(ctx context.Context, kind apitype.UpdateKind, stack Stack, op UpdateOperation,
|
2018-09-21 22:57:57 +02:00
|
|
|
opts ApplierOptions, events chan<- engine.Event) (engine.ResourceChanges, error)
|
2018-09-05 16:20:25 +02:00
|
|
|
|
|
|
|
func ActionLabel(kind apitype.UpdateKind, dryRun bool) string {
|
|
|
|
v := updateTextMap[kind]
|
|
|
|
contract.Assert(v.previewText != "")
|
|
|
|
contract.Assert(v.text != "")
|
|
|
|
|
|
|
|
if dryRun {
|
|
|
|
return "Previewing " + v.previewText
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.text
|
2018-09-22 22:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var updateTextMap = map[apitype.UpdateKind]struct {
|
|
|
|
previewText string
|
|
|
|
text string
|
|
|
|
}{
|
Make a smattering of CLI UX improvements
Since I was digging around over the weekend after the change to move
away from light black, and the impact it had on less important
information showing more prominently than it used to, I took a step
back and did a deeper tidying up of things. Another side goal of this
exercise was to be a little more respectful of terminal width; when
we could say things with fewer words, I did so.
* Stylize the preview/update summary differently, so that it stands
out as a section. Also highlight the total changes with bold -- it
turns out this has a similar effect to the bright white colorization,
just without the negative effects on e.g. white terminals.
* Eliminate some verbosity in the phrasing of change summaries.
* Make all heading sections stylized consistently. This includes
the color (bright magenta) and the vertical spacing (always a newline
separating headings). We were previously inconsistent on this (e.g.,
outputs were under "---outputs---"). Now the headings are:
Previewing (etc), Diagnostics, Outputs, Resources, Duration, and Permalink.
* Fix an issue where we'd parent things to "global" until the stack
object later showed up. Now we'll simply mock up a stack resource.
* Don't show messages like "no change" or "unchanged". Prior to the
light black removal, these faded into the background of the terminal.
Now they just clutter up the display. Similar to the elision of "*"
for OpSames in a prior commit, just leave these out. Now anything
that's written is actually a meaningful status for the user to note.
* Don't show the "3 info messages," etc. summaries in the Info column
while an update is ongoing. Instead, just show the latest line. This
is more respectful of width -- I often find that the important
messages scroll off the right of my screen before this change.
For discussion:
- I actually wonder if we should eliminate the summary
altogether and always just show the latest line. Or even
blank it out. The summary feels better suited for the
Diagnostics section, and the Status concisely tells us
how a resource's update ended up (failed, succeeded, etc).
- Similarly, I question the idea of showing only the "worst"
message. I'd vote for always showing the latest, and again
leaving it to the Status column for concisely telling the
user about the final state a resource ended up in.
* Stop prepending "info: " to every stdout/stderr message. It adds
no value, clutters up the display, and worsens horizontal usage.
* Lessen the verbosity of update headline messages, so we now instead
of e.g. "Previewing update of stack 'x':", we just say
"Previewing update (x):".
* Eliminate vertical whitespace in the Diagnostics section. Every
independent console.out previously was separated by an entire newline,
which made the section look cluttered to my eyes. These are just
streams of logs, there's no reason for the extra newlines.
* Colorize the resource headers in the Diagnostic section light blue.
Note that this will change various test baselines, which I will
update next. I didn't want those in the same commit.
2018-09-24 17:31:19 +02:00
|
|
|
apitype.PreviewUpdate: {"update", "Previewing"},
|
|
|
|
apitype.UpdateUpdate: {"update", "Updating"},
|
|
|
|
apitype.RefreshUpdate: {"refresh", "Refreshing"},
|
|
|
|
apitype.DestroyUpdate: {"destroy", "Destroying"},
|
|
|
|
apitype.ImportUpdate: {"import", "Importing"},
|
2018-09-05 16:20:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type response string
|
|
|
|
|
|
|
|
const (
|
|
|
|
yes response = "yes"
|
|
|
|
no response = "no"
|
|
|
|
details response = "details"
|
|
|
|
)
|
|
|
|
|
|
|
|
func PreviewThenPrompt(ctx context.Context, kind apitype.UpdateKind, stack Stack,
|
|
|
|
op UpdateOperation, apply Applier) (engine.ResourceChanges, error) {
|
|
|
|
// create a channel to hear about the update events from the engine. this will be used so that
|
|
|
|
// we can build up the diff display in case the user asks to see the details of the diff
|
2018-09-06 02:50:48 +02:00
|
|
|
|
|
|
|
// Note that eventsChannel is not closed in a `defer`. It is generally unsafe to do so, since defers run during
|
|
|
|
// panics and we can't know whether or not we were in the middle of writing to this channel when the panic occurred.
|
|
|
|
//
|
|
|
|
// Instead of using a `defer`, we manually close `eventsChannel` on every exit of this function.
|
2018-09-05 16:20:25 +02:00
|
|
|
eventsChannel := make(chan engine.Event)
|
|
|
|
|
|
|
|
var events []engine.Event
|
|
|
|
go func() {
|
|
|
|
// pull the events from the channel and store them locally
|
|
|
|
for e := range eventsChannel {
|
|
|
|
if e.Type == engine.ResourcePreEvent ||
|
|
|
|
e.Type == engine.ResourceOutputsEvent ||
|
|
|
|
e.Type == engine.SummaryEvent {
|
|
|
|
|
|
|
|
events = append(events, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Perform the update operations, passing true for dryRun, so that we get a preview.
|
|
|
|
changes := engine.ResourceChanges(nil)
|
|
|
|
if !op.Opts.SkipPreview {
|
2018-09-21 22:57:57 +02:00
|
|
|
// We perform the preview (DryRun), but don't display the cloud link since the
|
|
|
|
// thing the user cares about would be the link to the actual update if they
|
|
|
|
// confirm the prompt.
|
|
|
|
opts := ApplierOptions{
|
|
|
|
DryRun: true,
|
|
|
|
ShowLink: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := apply(ctx, kind, stack, op, opts, eventsChannel)
|
2018-09-05 16:20:25 +02:00
|
|
|
if err != nil {
|
2018-09-06 02:50:48 +02:00
|
|
|
close(eventsChannel)
|
2018-09-05 16:20:25 +02:00
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
changes = c
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no changes, or we're auto-approving or just previewing, we can skip the confirmation prompt.
|
|
|
|
if op.Opts.AutoApprove || kind == apitype.PreviewUpdate {
|
2018-09-06 02:50:48 +02:00
|
|
|
close(eventsChannel)
|
2018-09-05 16:20:25 +02:00
|
|
|
return changes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, ensure the user wants to proceed.
|
2018-09-06 02:50:48 +02:00
|
|
|
err := confirmBeforeUpdating(kind, stack, events, op.Opts)
|
|
|
|
close(eventsChannel)
|
|
|
|
return changes, err
|
2018-09-05 16:20:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// confirmBeforeUpdating asks the user whether to proceed. A nil error means yes.
|
|
|
|
func confirmBeforeUpdating(kind apitype.UpdateKind, stack Stack,
|
|
|
|
events []engine.Event, opts UpdateOptions) error {
|
|
|
|
for {
|
|
|
|
var response string
|
|
|
|
|
|
|
|
surveycore.DisableColor = true
|
|
|
|
surveycore.QuestionIcon = ""
|
|
|
|
surveycore.SelectFocusIcon = opts.Display.Color.Colorize(colors.BrightGreen + ">" + colors.Reset)
|
|
|
|
|
|
|
|
choices := []string{string(yes), string(no)}
|
|
|
|
|
|
|
|
// For non-previews, we can also offer a detailed summary.
|
|
|
|
if !opts.SkipPreview {
|
|
|
|
choices = append(choices, string(details))
|
|
|
|
}
|
|
|
|
|
|
|
|
var previewWarning string
|
|
|
|
if opts.SkipPreview {
|
2018-09-19 10:40:03 +02:00
|
|
|
previewWarning = colors.SpecWarning + " without a preview" + colors.Bold
|
2018-09-05 16:20:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a prompt. If this is a refresh, we'll add some extra text so it's clear we aren't updating resources.
|
|
|
|
prompt := "\b" + opts.Display.Color.Colorize(
|
2018-09-19 10:40:03 +02:00
|
|
|
colors.SpecPrompt+fmt.Sprintf("Do you want to perform this %s%s?",
|
2018-09-05 16:20:25 +02:00
|
|
|
kind, previewWarning)+colors.Reset)
|
|
|
|
if kind == apitype.RefreshUpdate {
|
|
|
|
prompt += "\n" +
|
2018-09-19 10:40:03 +02:00
|
|
|
opts.Display.Color.Colorize(colors.SpecImportant+
|
2018-09-05 16:20:25 +02:00
|
|
|
"No resources will be modified as part of this refresh; just your stack's state will be."+
|
|
|
|
colors.Reset)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now prompt the user for a yes, no, or details, and then proceed accordingly.
|
|
|
|
if err := survey.AskOne(&survey.Select{
|
|
|
|
Message: prompt,
|
|
|
|
Options: choices,
|
|
|
|
Default: string(no),
|
|
|
|
}, &response, nil); err != nil {
|
|
|
|
return errors.Wrapf(err, "confirmation cancelled, not proceeding with the %s", kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response == string(no) {
|
|
|
|
return errors.Errorf("confirmation declined, not proceeding with the %s", kind)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response == string(yes) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if response == string(details) {
|
|
|
|
diff := createDiff(kind, events, opts.Display)
|
Make a smattering of CLI UX improvements
Since I was digging around over the weekend after the change to move
away from light black, and the impact it had on less important
information showing more prominently than it used to, I took a step
back and did a deeper tidying up of things. Another side goal of this
exercise was to be a little more respectful of terminal width; when
we could say things with fewer words, I did so.
* Stylize the preview/update summary differently, so that it stands
out as a section. Also highlight the total changes with bold -- it
turns out this has a similar effect to the bright white colorization,
just without the negative effects on e.g. white terminals.
* Eliminate some verbosity in the phrasing of change summaries.
* Make all heading sections stylized consistently. This includes
the color (bright magenta) and the vertical spacing (always a newline
separating headings). We were previously inconsistent on this (e.g.,
outputs were under "---outputs---"). Now the headings are:
Previewing (etc), Diagnostics, Outputs, Resources, Duration, and Permalink.
* Fix an issue where we'd parent things to "global" until the stack
object later showed up. Now we'll simply mock up a stack resource.
* Don't show messages like "no change" or "unchanged". Prior to the
light black removal, these faded into the background of the terminal.
Now they just clutter up the display. Similar to the elision of "*"
for OpSames in a prior commit, just leave these out. Now anything
that's written is actually a meaningful status for the user to note.
* Don't show the "3 info messages," etc. summaries in the Info column
while an update is ongoing. Instead, just show the latest line. This
is more respectful of width -- I often find that the important
messages scroll off the right of my screen before this change.
For discussion:
- I actually wonder if we should eliminate the summary
altogether and always just show the latest line. Or even
blank it out. The summary feels better suited for the
Diagnostics section, and the Status concisely tells us
how a resource's update ended up (failed, succeeded, etc).
- Similarly, I question the idea of showing only the "worst"
message. I'd vote for always showing the latest, and again
leaving it to the Status column for concisely telling the
user about the final state a resource ended up in.
* Stop prepending "info: " to every stdout/stderr message. It adds
no value, clutters up the display, and worsens horizontal usage.
* Lessen the verbosity of update headline messages, so we now instead
of e.g. "Previewing update of stack 'x':", we just say
"Previewing update (x):".
* Eliminate vertical whitespace in the Diagnostics section. Every
independent console.out previously was separated by an entire newline,
which made the section look cluttered to my eyes. These are just
streams of logs, there's no reason for the extra newlines.
* Colorize the resource headers in the Diagnostic section light blue.
Note that this will change various test baselines, which I will
update next. I didn't want those in the same commit.
2018-09-24 17:31:19 +02:00
|
|
|
_, err := os.Stdout.WriteString(diff + "\n")
|
2018-09-05 16:20:25 +02:00
|
|
|
contract.IgnoreError(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func PreviewThenPromptThenExecute(ctx context.Context, kind apitype.UpdateKind, stack Stack,
|
|
|
|
op UpdateOperation, apply Applier) (engine.ResourceChanges, error) {
|
|
|
|
// Preview the operation to the user and ask them if they want to proceed.
|
|
|
|
changes, err := PreviewThenPrompt(ctx, kind, stack, op, apply)
|
|
|
|
if err != nil || kind == apitype.PreviewUpdate {
|
|
|
|
return changes, err
|
|
|
|
}
|
|
|
|
|
2018-09-21 22:57:57 +02:00
|
|
|
// Perform the change (!DryRun) and show the cloud link to the result.
|
|
|
|
// We don't care about the events it issues, so just pass a nil channel along.
|
|
|
|
opts := ApplierOptions{
|
|
|
|
DryRun: false,
|
|
|
|
ShowLink: true,
|
|
|
|
}
|
|
|
|
return apply(ctx, kind, stack, op, opts, nil /*events*/)
|
2018-09-05 16:20:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func createDiff(updateKind apitype.UpdateKind, events []engine.Event, displayOpts display.Options) string {
|
|
|
|
buff := &bytes.Buffer{}
|
|
|
|
|
|
|
|
seen := make(map[resource.URN]engine.StepEventMetadata)
|
|
|
|
displayOpts.SummaryDiff = true
|
|
|
|
|
|
|
|
for _, e := range events {
|
|
|
|
msg := display.RenderDiffEvent(updateKind, e, seen, displayOpts)
|
Make a smattering of CLI UX improvements
Since I was digging around over the weekend after the change to move
away from light black, and the impact it had on less important
information showing more prominently than it used to, I took a step
back and did a deeper tidying up of things. Another side goal of this
exercise was to be a little more respectful of terminal width; when
we could say things with fewer words, I did so.
* Stylize the preview/update summary differently, so that it stands
out as a section. Also highlight the total changes with bold -- it
turns out this has a similar effect to the bright white colorization,
just without the negative effects on e.g. white terminals.
* Eliminate some verbosity in the phrasing of change summaries.
* Make all heading sections stylized consistently. This includes
the color (bright magenta) and the vertical spacing (always a newline
separating headings). We were previously inconsistent on this (e.g.,
outputs were under "---outputs---"). Now the headings are:
Previewing (etc), Diagnostics, Outputs, Resources, Duration, and Permalink.
* Fix an issue where we'd parent things to "global" until the stack
object later showed up. Now we'll simply mock up a stack resource.
* Don't show messages like "no change" or "unchanged". Prior to the
light black removal, these faded into the background of the terminal.
Now they just clutter up the display. Similar to the elision of "*"
for OpSames in a prior commit, just leave these out. Now anything
that's written is actually a meaningful status for the user to note.
* Don't show the "3 info messages," etc. summaries in the Info column
while an update is ongoing. Instead, just show the latest line. This
is more respectful of width -- I often find that the important
messages scroll off the right of my screen before this change.
For discussion:
- I actually wonder if we should eliminate the summary
altogether and always just show the latest line. Or even
blank it out. The summary feels better suited for the
Diagnostics section, and the Status concisely tells us
how a resource's update ended up (failed, succeeded, etc).
- Similarly, I question the idea of showing only the "worst"
message. I'd vote for always showing the latest, and again
leaving it to the Status column for concisely telling the
user about the final state a resource ended up in.
* Stop prepending "info: " to every stdout/stderr message. It adds
no value, clutters up the display, and worsens horizontal usage.
* Lessen the verbosity of update headline messages, so we now instead
of e.g. "Previewing update of stack 'x':", we just say
"Previewing update (x):".
* Eliminate vertical whitespace in the Diagnostics section. Every
independent console.out previously was separated by an entire newline,
which made the section look cluttered to my eyes. These are just
streams of logs, there's no reason for the extra newlines.
* Colorize the resource headers in the Diagnostic section light blue.
Note that this will change various test baselines, which I will
update next. I didn't want those in the same commit.
2018-09-24 17:31:19 +02:00
|
|
|
if msg != "" && e.Type != engine.SummaryEvent {
|
2018-09-05 16:20:25 +02:00
|
|
|
_, err := buff.WriteString(msg)
|
|
|
|
contract.IgnoreError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.TrimSpace(buff.String())
|
|
|
|
}
|