2018-03-21 20:43:21 +01:00
|
|
|
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
|
2018-01-05 21:46:13 +01:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2018-02-28 21:46:18 +01:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2018-01-05 21:46:13 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2018-02-28 19:02:02 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/apitype"
|
2018-02-28 21:46:18 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/diag"
|
2018-01-05 21:46:13 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/util/cmdutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newStackImportCmd() *cobra.Command {
|
2018-02-28 21:46:18 +01:00
|
|
|
var force bool
|
2018-04-03 06:34:54 +02:00
|
|
|
var file string
|
2018-02-28 21:46:18 +01:00
|
|
|
cmd := &cobra.Command{
|
2018-01-05 21:46:13 +01:00
|
|
|
Use: "import",
|
|
|
|
Args: cmdutil.MaximumNArgs(0),
|
2018-01-27 18:40:05 +01:00
|
|
|
Short: "Import a deployment from standard in into an existing stack",
|
2018-01-05 21:46:13 +01:00
|
|
|
Long: "Import a deployment from standard in into an existing stack.\n" +
|
|
|
|
"\n" +
|
|
|
|
"A deployment that was exported from a stack using `pulumi stack export` and\n" +
|
|
|
|
"hand-edited to correct inconsistencies due to failed updates, manual changes\n" +
|
|
|
|
"to cloud resources, etc. can be reimported to the stack using this command.\n" +
|
|
|
|
"The updated deployment will be read from standard in.",
|
|
|
|
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
|
|
|
// Fetch the current stack and import a deployment.
|
Make some stack-related CLI improvements (#947)
This change includes a handful of stack-related CLI formatting
improvements that I've been noodling on in the background for a while,
based on things that tend to trip up demos and the inner loop workflow.
This includes:
* If `pulumi stack select` is run by itself, use an interactive
CLI menu to let the user select an existing stack, or choose to
create a new one. This looks as follows
$ pulumi stack select
Please choose a stack, or choose to create a new one:
abcdef
babblabblabble
> currentlyselected
defcon
<create a new stack>
and is navigated in the usual way (key up, down, enter).
* If a stack name is passed that does not exist, prompt the user
to ask whether s/he wants to create one on-demand. This hooks
interesting moments in time, like `pulumi stack select foo`,
and cuts down on the need to run additional commands.
* If a current stack is required, but none is currently selected,
then pop the same interactive menu shown above to select one.
Depending on the command being run, we may or may not show the
option to create a new stack (e.g., that doesn't make much sense
when you're running `pulumi destroy`, but might when you're
running `pulumi stack`). This again lets you do with a single
command what would have otherwise entailed an error with multiple
commands to recover from it.
* If you run `pulumi stack init` without any additional arguments,
we interactively prompt for the stack name. Before, we would
error and you'd then need to run `pulumi stack init <name>`.
* Colorize some things nicely; for example, now all prompts will
by default become bright white.
2018-02-17 00:03:54 +01:00
|
|
|
s, err := requireCurrentStack(false)
|
2018-01-05 21:46:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-03 06:34:54 +02:00
|
|
|
// Read from stdin or a specified file
|
|
|
|
reader := os.Stdin
|
|
|
|
if file != "" {
|
|
|
|
reader, err = os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not open file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 21:12:54 +01:00
|
|
|
// Read the checkpoint from stdin. We decode this into a json.RawMessage so as not to lose any fields
|
|
|
|
// sent by the server that the client CLI does not recognize (enabling round-tripping).
|
2018-03-31 00:51:40 +02:00
|
|
|
var deployment apitype.UntypedDeployment
|
2018-04-03 06:34:54 +02:00
|
|
|
if err = json.NewDecoder(reader).Decode(&deployment); err != nil {
|
2018-01-05 21:46:13 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-03 21:12:54 +01:00
|
|
|
// We do, however, now want to unmarshal the json.RawMessage into a real, typed deployment. We do this so
|
2018-04-03 06:34:54 +02:00
|
|
|
// we can check that the deployment doesn't contain resources from a stack other than the selected one. This
|
|
|
|
// catches errors wherein someone imports the wrong stack's deployment (which can seriously hork things).
|
2018-03-03 21:12:54 +01:00
|
|
|
var typed apitype.Deployment
|
2018-03-31 00:51:40 +02:00
|
|
|
if err = json.Unmarshal(deployment.Deployment, &typed); err != nil {
|
2018-03-03 21:12:54 +01:00
|
|
|
return err
|
|
|
|
}
|
2018-02-28 21:46:18 +01:00
|
|
|
var result error
|
2018-03-03 21:12:54 +01:00
|
|
|
for _, res := range typed.Resources {
|
2018-02-28 21:46:18 +01:00
|
|
|
if res.URN.Stack() != s.Name() {
|
|
|
|
msg := fmt.Sprintf("resource '%s' is from a different stack (%s != %s)",
|
|
|
|
res.URN, res.URN.Stack(), s.Name())
|
|
|
|
if force {
|
|
|
|
// If --force was passed, just issue a warning and proceed anyway.
|
2018-04-10 21:03:11 +02:00
|
|
|
// Note: we could associate this diagnostic with the resource URN
|
|
|
|
// we have. However, this sort of message seems to be better as
|
|
|
|
// something associated with the stack as a whole.
|
|
|
|
cmdutil.Diag().Warningf(diag.Message("" /*urn*/, msg))
|
2018-02-28 21:46:18 +01:00
|
|
|
} else {
|
|
|
|
// Otherwise, gather up an error so that we can quit before doing damage.
|
|
|
|
result = multierror.Append(result, errors.New(msg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if result != nil {
|
2018-02-28 22:12:19 +01:00
|
|
|
return multierror.Append(result,
|
|
|
|
errors.New("importing this file could be dangerous; rerun with --force to proceed anyway"))
|
2018-02-28 21:46:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now perform the deployment.
|
2018-03-31 00:51:40 +02:00
|
|
|
if err = s.ImportDeployment(&deployment); err != nil {
|
2018-01-05 21:46:13 +01:00
|
|
|
return errors.Wrap(err, "could not import deployment")
|
|
|
|
}
|
|
|
|
fmt.Printf("Import successful.\n")
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
}
|
2018-02-28 21:46:18 +01:00
|
|
|
|
|
|
|
cmd.PersistentFlags().BoolVarP(
|
|
|
|
&force, "force", "f", false,
|
|
|
|
"Force the import to occur, even if apparent errors are discovered beforehand (not recommended)")
|
2018-04-03 06:34:54 +02:00
|
|
|
cmd.PersistentFlags().StringVarP(
|
|
|
|
&file, "file", "", "", "A filename to read stack input from")
|
2018-02-28 21:46:18 +01:00
|
|
|
|
|
|
|
return cmd
|
2018-01-05 21:46:13 +01:00
|
|
|
}
|