pulumi/cmd/pulumi.go
Matt Ellis d3240fdc64 Require pulumi login before commands that need a backend
This change does three major things:

1. Removes the ability to be logged into multiple clouds at the same
time. Previously, we supported being logged into multiple clouds at
the same time and the CLI would fan out requests and join responses
when needed. In general, this was only useful for Pulumi employees
that wanted run against multiple copies of the service (say production
and staging) but overall was very confusing (for example in the old
world a stack with the same identity could appear twice (since it was
in two backends) which the CLI didn't handle very well).

2. Stops treating the "local" backend as a special thing, from the
point of view of the CLI. Previouly we'd always connect to the local
backend and merge that data with whatever was in clouds we were
connected to. We had gestures like `--local` in `pulumi stack init`
that meant "use the local mode". Instead, to use the local mode now
you run `pulumi login --cloud-url local://` and then you are logged in
the local backend. Since you can only ever be logged into a single
backend, we can remove the `--local` and `--remote` flags from `pulumi
stack init`, it just now requires you to be logged in and creates a
stack in whatever back end you were logged into. When logging into the
local backend, you are not prompted for an access key.

3. Prompt for login in places where you have to log in, if you are not
already logged in.
2018-04-05 10:19:41 -07:00

127 lines
4 KiB
Go

// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
package cmd
import (
"bufio"
"flag"
"fmt"
"os"
"runtime"
"strings"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/backend/cloud"
"github.com/pulumi/pulumi/pkg/backend/local"
"github.com/pulumi/pulumi/pkg/diag/colors"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
"github.com/pulumi/pulumi/pkg/workspace"
)
// NewPulumiCmd creates a new Pulumi Cmd instance.
func NewPulumiCmd() *cobra.Command {
var logFlow bool
var logToStderr bool
var tracing string
var verbose int
var cwd string
cmd := &cobra.Command{
Use: "pulumi",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if cwd != "" {
if err := os.Chdir(cwd); err != nil {
cmdutil.ExitError(err.Error())
}
}
if err := upgradeConfigurationFiles(); err != nil {
cmdutil.ExitError(err.Error())
}
cmdutil.InitLogging(logToStderr, verbose, logFlow)
cmdutil.InitTracing("pulumi-cli", tracing)
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
glog.Flush()
cmdutil.CloseTracing()
},
}
// Add additional help that includes which clouds we are logged into.
defaultHelp := cmd.HelpFunc()
cmd.SetHelpFunc(func(cmd *cobra.Command, args []string) {
defaultHelp(cmd, args)
url, err := workspace.GetCurrentCloudURL()
if err == nil && url != "" && !local.IsLocalBackendURL(url) {
fmt.Printf("\n")
suffix := ""
if url != cloud.PulumiCloudURL {
suffix = fmt.Sprintf(" (%s)", url)
}
fmt.Printf("Currently logged into the Pulumi Cloud%s%s\n", cmdutil.EmojiOr(" ☁️", ""), suffix)
}
})
cmd.PersistentFlags().StringVarP(&cwd, "cwd", "C", "", "Run pulumi as if it had been started in another directory")
cmd.PersistentFlags().BoolVarP(&cmdutil.Emoji, "emoji", "e",
runtime.GOOS == "darwin", "Enable emojis in the output")
cmd.PersistentFlags().BoolVar(&local.DisableIntegrityChecking, "disable-integrity-checking", false,
"Disable integrity checking of checkpoint files")
cmd.PersistentFlags().BoolVar(&logFlow, "logflow", false, "Flow log settings to child processes (like plugins)")
cmd.PersistentFlags().BoolVar(&logToStderr, "logtostderr", false, "Log to stderr instead of to files")
cmd.PersistentFlags().StringVar(&tracing, "tracing", "", "Emit tracing to a Zipkin-compatible tracing endpoint")
cmd.PersistentFlags().IntVarP(
&verbose, "verbose", "v", 0, "Enable verbose logging (e.g., v=3); anything >3 is very verbose")
cmd.AddCommand(newConfigCmd())
cmd.AddCommand(newDestroyCmd())
cmd.AddCommand(newHistoryCmd())
cmd.AddCommand(newInitCmd())
cmd.AddCommand(newLogsCmd())
cmd.AddCommand(newNewCmd())
cmd.AddCommand(newPluginCmd())
cmd.AddCommand(newPreviewCmd())
cmd.AddCommand(newStackCmd())
cmd.AddCommand(newUpdateCmd())
cmd.AddCommand(newVersionCmd())
// Commands specific to the Pulumi Cloud Management Console.
cmd.AddCommand(newLoginCmd())
cmd.AddCommand(newLogoutCmd())
// We have a set of commands that are useful for developers of pulumi that we add when PULUMI_DEBUG_COMMANDS is
// set to true.
if cmdutil.IsTruthy(os.Getenv("PULUMI_DEBUG_COMMANDS")) {
cmd.AddCommand(newArchiveCommand())
}
// Tell flag about -C, so someone can do pulumi -C <working-directory> stack and the call to cmdutil.InitLogging
// which calls flag.Parse under the hood doesn't yell at you.
//
// TODO[pulumi/pulumi#301]: when we move away from using glog, it should be safe to remove this.
flag.StringVar(&cwd, "C", "", "Run pulumi as if it had been started in another directory")
return cmd
}
func confirmPrompt(prompt string, name string) bool {
if prompt != "" {
fmt.Print(
colors.ColorizeText(
fmt.Sprintf("%s%s%s\n", colors.SpecAttention, prompt, colors.Reset)))
}
fmt.Print(
colors.ColorizeText(
fmt.Sprintf("%sPlease confirm that this is what you'd like to do by typing (%s\"%s\"%s):%s ",
colors.SpecAttention, colors.BrightWhite, name, colors.SpecAttention, colors.Reset)))
reader := bufio.NewReader(os.Stdin)
line, _ := reader.ReadString('\n')
return strings.TrimSpace(line) == name
}