2018-05-22 21:43:36 +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.
|
2017-04-12 20:04:04 +02:00
|
|
|
|
2017-09-22 04:18:21 +02:00
|
|
|
package cmd
|
2017-04-12 20:04:04 +02:00
|
|
|
|
|
|
|
import (
|
2017-08-23 01:56:15 +02:00
|
|
|
"bufio"
|
2019-03-06 19:56:44 +01:00
|
|
|
"bytes"
|
2018-08-02 23:45:50 +02:00
|
|
|
"encoding/json"
|
2017-08-23 01:56:15 +02:00
|
|
|
"fmt"
|
2019-10-01 22:34:42 +02:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-08-23 01:56:15 +02:00
|
|
|
"os"
|
2018-08-02 23:45:50 +02:00
|
|
|
"os/exec"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
2018-02-21 18:42:06 +01:00
|
|
|
"runtime"
|
2017-11-07 22:58:56 +01:00
|
|
|
"strings"
|
2018-08-02 23:45:50 +02:00
|
|
|
"time"
|
2017-08-23 01:56:15 +02:00
|
|
|
|
2018-08-02 23:45:50 +02:00
|
|
|
"github.com/blang/semver"
|
2018-09-05 00:40:15 +02:00
|
|
|
"github.com/djherbis/times"
|
2018-12-05 01:58:32 +01:00
|
|
|
"github.com/docker/docker/pkg/term"
|
2018-09-05 00:40:15 +02:00
|
|
|
"github.com/pkg/errors"
|
2017-04-12 20:04:04 +02:00
|
|
|
"github.com/spf13/cobra"
|
2017-04-13 14:27:45 +02:00
|
|
|
|
2018-09-05 00:40:15 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/backend/display"
|
2018-09-04 21:38:58 +02:00
|
|
|
"github.com/pulumi/pulumi/pkg/backend/filestate"
|
|
|
|
"github.com/pulumi/pulumi/pkg/backend/httpstate"
|
|
|
|
"github.com/pulumi/pulumi/pkg/backend/httpstate/client"
|
2020-03-19 20:49:34 +01:00
|
|
|
"github.com/pulumi/pulumi/pkg/version"
|
2020-03-18 23:09:29 +01:00
|
|
|
"github.com/pulumi/pulumi/sdk/go/common/diag"
|
|
|
|
"github.com/pulumi/pulumi/sdk/go/common/diag/colors"
|
2020-03-18 23:39:00 +01:00
|
|
|
"github.com/pulumi/pulumi/sdk/go/common/util/cmdutil"
|
2020-03-18 22:40:07 +01:00
|
|
|
"github.com/pulumi/pulumi/sdk/go/common/util/contract"
|
2020-03-18 22:45:00 +01:00
|
|
|
"github.com/pulumi/pulumi/sdk/go/common/util/httputil"
|
2020-03-18 23:34:58 +01:00
|
|
|
"github.com/pulumi/pulumi/sdk/go/common/util/logging"
|
2020-03-18 22:35:53 +01:00
|
|
|
"github.com/pulumi/pulumi/sdk/go/common/workspace"
|
2017-04-12 20:04:04 +02:00
|
|
|
)
|
|
|
|
|
2017-09-22 04:18:21 +02:00
|
|
|
// NewPulumiCmd creates a new Pulumi Cmd instance.
|
2017-12-01 22:50:32 +01:00
|
|
|
func NewPulumiCmd() *cobra.Command {
|
2018-04-28 20:18:21 +02:00
|
|
|
var cwd string
|
2017-05-31 18:14:49 +02:00
|
|
|
var logFlow bool
|
2017-04-12 20:04:04 +02:00
|
|
|
var logToStderr bool
|
2017-11-09 02:08:51 +01:00
|
|
|
var tracing string
|
2018-05-14 22:44:44 +02:00
|
|
|
var tracingHeaderFlag string
|
2018-05-04 20:26:53 +02:00
|
|
|
var profiling string
|
2017-04-12 20:04:04 +02:00
|
|
|
var verbose int
|
2018-10-05 01:20:01 +02:00
|
|
|
var color string
|
2018-04-28 20:18:21 +02:00
|
|
|
|
2019-11-06 19:44:27 +01:00
|
|
|
updateCheckResult := make(chan *diag.Diag)
|
|
|
|
|
2017-04-12 20:04:04 +02:00
|
|
|
cmd := &cobra.Command{
|
2018-09-13 15:45:52 +02:00
|
|
|
Use: "pulumi",
|
|
|
|
Short: "Pulumi command line",
|
2019-08-27 05:24:00 +02:00
|
|
|
Long: "Pulumi - Modern Infrastructure as Code\n" +
|
2018-09-06 22:55:12 +02:00
|
|
|
"\n" +
|
|
|
|
"To begin working with Pulumi, run the 'pulumi new' command:\n" +
|
|
|
|
"\n" +
|
|
|
|
" $ pulumi new\n" +
|
|
|
|
"\n" +
|
|
|
|
"This will prompt you to create a new project for your cloud and language of choice.\n" +
|
|
|
|
"\n" +
|
|
|
|
"The most common commands from there are:\n" +
|
|
|
|
"\n" +
|
|
|
|
" - pulumi up : Deploy code and/or resource changes\n" +
|
|
|
|
" - pulumi stack : Manage instances of your project\n" +
|
|
|
|
" - pulumi config : Alter your stack's configuration or secrets\n" +
|
|
|
|
" - pulumi destroy : Tear down your stack's resources entirely\n" +
|
|
|
|
"\n" +
|
2019-07-16 19:05:37 +02:00
|
|
|
"For more information, please visit the project page: https://www.pulumi.com/docs/",
|
2018-04-28 20:18:21 +02:00
|
|
|
PersistentPreRun: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
2018-12-05 01:58:32 +01:00
|
|
|
// We run this method for its side-effects. On windows, this will enable the windows terminal
|
|
|
|
// to understand ANSI escape codes.
|
|
|
|
_, _, _ = term.StdStreams()
|
|
|
|
|
2018-07-07 06:30:00 +02:00
|
|
|
// For all commands, attempt to grab out the --color value provided so we
|
|
|
|
// can set the GlobalColorization value to be used by any code that doesn't
|
|
|
|
// get DisplayOptions passed in.
|
|
|
|
cmdFlag := cmd.Flag("color")
|
|
|
|
if cmdFlag != nil {
|
2018-10-05 01:20:01 +02:00
|
|
|
err := cmdutil.SetGlobalColorization(cmdFlag.Value.String())
|
2018-07-07 06:30:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 17:50:54 +02:00
|
|
|
if cwd != "" {
|
2018-02-28 04:21:31 +01:00
|
|
|
if err := os.Chdir(cwd); err != nil {
|
2018-04-28 20:18:21 +02:00
|
|
|
return err
|
2017-10-06 17:50:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 00:28:00 +02:00
|
|
|
logging.InitLogging(logToStderr, verbose, logFlow)
|
2018-05-08 03:23:03 +02:00
|
|
|
cmdutil.InitTracing("pulumi-cli", "pulumi", tracing)
|
2018-05-14 22:44:44 +02:00
|
|
|
if tracingHeaderFlag != "" {
|
|
|
|
tracingHeader = tracingHeaderFlag
|
|
|
|
}
|
2018-05-04 20:26:53 +02:00
|
|
|
|
|
|
|
if profiling != "" {
|
|
|
|
if err := cmdutil.InitProfiling(profiling); err != nil {
|
2018-05-16 00:28:00 +02:00
|
|
|
logging.Warningf("could not initialize profiling: %v", err)
|
2018-05-04 20:26:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-27 22:37:26 +02:00
|
|
|
if cmdutil.IsTruthy(os.Getenv("PULUMI_SKIP_UPDATE_CHECK")) {
|
2020-03-19 19:45:10 +01:00
|
|
|
logging.V(5).Infof("skipping update check")
|
2019-11-06 19:44:27 +01:00
|
|
|
close(updateCheckResult)
|
2019-04-27 22:37:26 +02:00
|
|
|
} else {
|
2019-11-06 19:44:27 +01:00
|
|
|
// Run the version check in parallel so that it doesn't block executing the command.
|
|
|
|
// If there is a new version to report, we will do so after the command has finished.
|
|
|
|
go func() {
|
|
|
|
updateCheckResult <- checkForUpdate()
|
|
|
|
close(updateCheckResult)
|
|
|
|
}()
|
2019-04-27 22:37:26 +02:00
|
|
|
}
|
2018-08-02 23:45:50 +02:00
|
|
|
|
2018-04-28 20:18:21 +02:00
|
|
|
return nil
|
|
|
|
}),
|
2017-04-12 20:04:04 +02:00
|
|
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
2019-11-06 19:44:27 +01:00
|
|
|
// Before exiting, if there is a new version of the CLI available, print it out.
|
|
|
|
if checkVersionMsg := <-updateCheckResult; checkVersionMsg != nil {
|
|
|
|
cmdutil.Diag().Warningf(checkVersionMsg)
|
|
|
|
}
|
|
|
|
|
2018-05-16 00:28:00 +02:00
|
|
|
logging.Flush()
|
2017-11-09 02:08:51 +01:00
|
|
|
cmdutil.CloseTracing()
|
2018-05-04 20:26:53 +02:00
|
|
|
|
|
|
|
if profiling != "" {
|
|
|
|
if err := cmdutil.CloseProfiling(profiling); err != nil {
|
2018-05-16 00:28:00 +02:00
|
|
|
logging.Warningf("could not close profiling: %v", err)
|
2018-05-04 20:26:53 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-12 20:04:04 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-04-28 20:18:21 +02:00
|
|
|
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")
|
2018-09-04 21:38:58 +02:00
|
|
|
cmd.PersistentFlags().BoolVar(&filestate.DisableIntegrityChecking, "disable-integrity-checking", false,
|
2017-12-01 01:42:55 +01:00
|
|
|
"Disable integrity checking of checkpoint files")
|
2018-04-28 20:18:21 +02:00
|
|
|
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")
|
2018-09-29 19:49:14 +02:00
|
|
|
cmd.PersistentFlags().BoolVar(&cmdutil.DisableInteractive, "non-interactive", false,
|
Make --non-interactive a global flag
Right now, we only support --non-interactive in a few places (up,
refresh, destroy, etc). Over time, we've added it to more (like new).
And now, as we're working on better Docker support (pulumi/pulumi#1991),
we want to support this more globally, so we can, for example, avoid
popping up a web browser inside a Docker contain for logging in.
So, this change makes --non-interactive a global flag. Because it is
a persistent flag, it still works in the old positions, so this isn't
a breaking change to existing commands that use it.
2018-09-29 19:41:02 +02:00
|
|
|
"Disable interactive mode for all commands")
|
2018-04-28 20:18:21 +02:00
|
|
|
cmd.PersistentFlags().StringVar(&tracing, "tracing", "",
|
2019-09-16 23:16:43 +02:00
|
|
|
"Emit tracing to the specified endpoint. Use the `file:` scheme to write tracing data to a local file")
|
2018-05-04 20:26:53 +02:00
|
|
|
cmd.PersistentFlags().StringVar(&profiling, "profiling", "",
|
2018-05-04 22:09:35 +02:00
|
|
|
"Emit CPU and memory profiles and an execution trace to '[filename].[pid].{cpu,mem,trace}', respectively")
|
2018-04-28 20:18:21 +02:00
|
|
|
cmd.PersistentFlags().IntVarP(&verbose, "verbose", "v", 0,
|
|
|
|
"Enable verbose logging (e.g., v=3); anything >3 is very verbose")
|
2018-10-05 01:20:01 +02:00
|
|
|
cmd.PersistentFlags().StringVar(
|
|
|
|
&color, "color", "auto", "Colorize output. Choices are: always, never, raw, auto")
|
2017-04-12 20:04:04 +02:00
|
|
|
|
2018-04-28 20:18:21 +02:00
|
|
|
// Common commands:
|
2019-11-06 21:56:29 +01:00
|
|
|
// - Getting Started Commands:
|
2018-09-06 22:55:12 +02:00
|
|
|
cmd.AddCommand(newNewCmd())
|
2019-11-06 21:56:29 +01:00
|
|
|
// - Deploy Commands:
|
2018-09-06 22:55:12 +02:00
|
|
|
cmd.AddCommand(newUpCmd())
|
|
|
|
cmd.AddCommand(newPreviewCmd())
|
2017-04-12 20:04:04 +02:00
|
|
|
cmd.AddCommand(newDestroyCmd())
|
2020-03-02 23:23:40 +01:00
|
|
|
cmd.AddCommand(newWatchCmd())
|
2018-09-06 22:55:12 +02:00
|
|
|
// - Stack Management Commands:
|
|
|
|
cmd.AddCommand(newStackCmd())
|
|
|
|
cmd.AddCommand(newConfigCmd())
|
|
|
|
// - Service Commands:
|
2018-04-28 20:18:21 +02:00
|
|
|
cmd.AddCommand(newLoginCmd())
|
|
|
|
cmd.AddCommand(newLogoutCmd())
|
2018-09-06 22:55:12 +02:00
|
|
|
cmd.AddCommand(newWhoAmICmd())
|
2020-03-02 23:23:40 +01:00
|
|
|
// - Policy Management Commands:
|
|
|
|
cmd.AddCommand(newPolicyCmd())
|
2018-09-06 22:55:12 +02:00
|
|
|
// - Advanced Commands:
|
|
|
|
cmd.AddCommand(newCancelCmd())
|
|
|
|
cmd.AddCommand(newRefreshCmd())
|
2018-10-15 18:52:55 +02:00
|
|
|
cmd.AddCommand(newStateCmd())
|
2018-09-06 22:55:12 +02:00
|
|
|
// - Other Commands:
|
Implement basic plugin management
This change implements basic plugin management, but we do not yet
actually use the plugins for anything (that comes next).
Plugins are stored in `~/.pulumi/plugins`, and are expected to be
in the format `pulumi-<KIND>-<NAME>-v<VERSION>[.exe]`. The KIND is
one of `analyzer`, `language`, or `resource`, the NAME is a hyphen-
delimited name (e.g., `aws` or `foo-bar`), and VERSION is the
plugin's semantic version (e.g., `0.9.11`, `1.3.7-beta.a736cf`, etc).
This commit includes four new CLI commands:
* `pulumi plugin` is the top-level plugin command. It does nothing
but show the help text for associated child commands.
* `pulumi plugin install` can be used to install plugins manually.
If run with no additional arguments, it will compute the set of
plugins used by the current project, and download them all. It
may be run to explicitly download a single plugin, however, by
invoking it as `pulumi plugin install KIND NAME VERSION`. For
example, `pulumi plugin install resource aws v0.9.11`. By default,
this command uses the cloud backend in the usual way to perform the
download, although a separate URL may be given with --cloud-url,
just like all other commands that interact with our backend service.
* `pulumi plugin ls` lists all plugins currently installed in the
plugin cache. It displays some useful statistics, like the size
of the plugin, when it was installed, when it was last used, and
so on. It sorts the display alphabetically by plugin name, and
for plugins with multiple versions, it shows the newest at the top.
The command also summarizes how much disk space is currently being
consumed by the plugin cache. There are no filtering capabilities yet.
* `pulumi plugin prune` will delete plugins from the cache. By
default, when run with no arguments, it will delete everything.
It may be run with additional arguments, KIND, NAME, and VERSION,
each one getting more specific about what it will delete. For
instance, `pulumi plugin prune resource aws` will delete all AWS
plugin versions, while `pulumi plugin prune resource aws <0.9`
will delete all AWS plugins before version 0.9. Unless --yes is
passed, the command will confirm the deletion with a count of how
many plugins will be affected by the command.
We do not yet actually download plugins on demand yet. That will
come in a subsequent change.
2018-02-04 19:51:29 +01:00
|
|
|
cmd.AddCommand(newLogsCmd())
|
|
|
|
cmd.AddCommand(newPluginCmd())
|
2017-12-01 22:50:32 +01:00
|
|
|
cmd.AddCommand(newVersionCmd())
|
2019-01-15 03:19:24 +01:00
|
|
|
cmd.AddCommand(newHistoryCmd())
|
2017-04-12 20:04:04 +02:00
|
|
|
|
2018-04-28 20:18:21 +02:00
|
|
|
// Less common, and thus hidden, commands:
|
2018-09-24 15:25:16 +02:00
|
|
|
cmd.AddCommand(newGenCompletionCmd(cmd))
|
2018-04-28 20:18:21 +02:00
|
|
|
cmd.AddCommand(newGenMarkdownCmd(cmd))
|
2017-10-20 00:22:07 +02:00
|
|
|
|
2019-11-06 21:56:29 +01:00
|
|
|
// We have a set of commands that are still experimental and that we add only when PULUMI_EXPERIMENTAL is set
|
|
|
|
// to true.
|
|
|
|
if hasExperimentalCommands() {
|
|
|
|
// - Query Commands:
|
|
|
|
cmd.AddCommand(newQueryCmd())
|
|
|
|
}
|
|
|
|
|
2018-10-27 01:08:18 +02:00
|
|
|
// We have a set of options that are useful for developers of pulumi that we add when PULUMI_DEBUG_COMMANDS is
|
2017-11-21 21:11:43 +01:00
|
|
|
// set to true.
|
2018-05-23 02:02:45 +02:00
|
|
|
if hasDebugCommands() {
|
2018-05-14 22:44:44 +02:00
|
|
|
cmd.PersistentFlags().StringVar(&tracingHeaderFlag, "tracing-header", "",
|
|
|
|
"Include the tracing header with the given contents.")
|
2019-11-06 21:56:29 +01:00
|
|
|
// - Diagnostic Commands:
|
|
|
|
cmd.AddCommand(newViewTraceCmd())
|
|
|
|
|
2020-03-02 23:23:40 +01:00
|
|
|
// For legacy reasons, we make this command available also under PULUMI_DEBUG_COMMANDS, though
|
2019-11-06 21:56:29 +01:00
|
|
|
// PULUMI_EXPERIMENTAL should be preferred.
|
|
|
|
|
|
|
|
// - Query Commands:
|
2019-05-03 01:30:18 +02:00
|
|
|
cmd.AddCommand(newQueryCmd())
|
2017-11-21 21:11:43 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 20:04:04 +02:00
|
|
|
return cmd
|
|
|
|
}
|
2017-06-02 21:43:04 +02:00
|
|
|
|
2018-08-02 23:45:50 +02:00
|
|
|
// checkForUpdate checks to see if the CLI needs to be updated, and if so emits a warning, as well as information
|
|
|
|
// as to how it can be upgraded.
|
2019-11-06 19:44:27 +01:00
|
|
|
func checkForUpdate() *diag.Diag {
|
2018-08-02 23:45:50 +02:00
|
|
|
curVer, err := semver.ParseTolerant(version.Version)
|
|
|
|
if err != nil {
|
2019-07-31 20:23:33 +02:00
|
|
|
logging.V(3).Infof("error parsing current version: %s", err)
|
2018-08-02 23:45:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't care about warning for you to update if you have installed a developer version
|
|
|
|
if isDevVersion(curVer) {
|
2019-11-06 19:44:27 +01:00
|
|
|
return nil
|
2018-08-02 23:45:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
latestVer, oldestAllowedVer, err := getCLIVersionInfo()
|
|
|
|
if err != nil {
|
2019-07-31 20:23:33 +02:00
|
|
|
logging.V(3).Infof("error fetching latest version information: %s", err)
|
2018-08-02 23:45:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if oldestAllowedVer.GT(curVer) {
|
2019-11-06 19:44:27 +01:00
|
|
|
return diag.RawMessage("", getUpgradeMessage(latestVer, curVer))
|
2018-08-02 23:45:50 +02:00
|
|
|
}
|
2019-11-06 19:44:27 +01:00
|
|
|
|
|
|
|
return nil
|
2018-08-02 23:45:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// getCLIVersionInfo returns information about the latest version of the CLI and the oldest version that should be
|
|
|
|
// allowed without warning. It caches data from the server for a day.
|
|
|
|
func getCLIVersionInfo() (semver.Version, semver.Version, error) {
|
|
|
|
latest, oldest, err := getCachedVersionInfo()
|
|
|
|
if err == nil {
|
|
|
|
return latest, oldest, err
|
|
|
|
}
|
|
|
|
|
2018-09-24 23:49:09 +02:00
|
|
|
client := client.NewClient(httpstate.DefaultURL(), "", cmdutil.Diag())
|
2018-08-02 23:45:50 +02:00
|
|
|
latest, oldest, err = client.GetCLIVersionInfo(commandContext())
|
|
|
|
if err != nil {
|
|
|
|
return semver.Version{}, semver.Version{}, err
|
|
|
|
}
|
|
|
|
|
2019-10-01 22:34:42 +02:00
|
|
|
brewLatest, isBrew, err := getLatestBrewFormulaVersion()
|
|
|
|
if err != nil {
|
2019-10-17 04:18:04 +02:00
|
|
|
logging.V(3).Infof("error determining if the running executable was installed with brew: %s", err)
|
2019-10-01 22:34:42 +02:00
|
|
|
}
|
|
|
|
if isBrew {
|
|
|
|
// When consulting Homebrew for version info, we just use the latest version as the oldest allowed.
|
|
|
|
latest, oldest = brewLatest, brewLatest
|
|
|
|
}
|
|
|
|
|
2018-08-02 23:45:50 +02:00
|
|
|
err = cacheVersionInfo(latest, oldest)
|
|
|
|
if err != nil {
|
2019-07-31 20:23:33 +02:00
|
|
|
logging.V(3).Infof("failed to cache version info: %s", err)
|
2018-08-02 23:45:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return latest, oldest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// cacheVersionInfo saves version information in a cache file to be looked up later.
|
|
|
|
func cacheVersionInfo(latest semver.Version, oldest semver.Version) error {
|
|
|
|
updateCheckFile, err := workspace.GetCachedVersionFilePath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.OpenFile(updateCheckFile, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer contract.IgnoreClose(file)
|
|
|
|
|
|
|
|
return json.NewEncoder(file).Encode(cachedVersionInfo{
|
|
|
|
LatestVersion: latest.String(),
|
|
|
|
OldestWithoutWarning: oldest.String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// getCachedVersionInfo reads cached information about the newest CLI version, returning the newest version avaliaible
|
|
|
|
// as well as the oldest version that should be allowed without warning the user they should upgrade.
|
|
|
|
func getCachedVersionInfo() (semver.Version, semver.Version, error) {
|
|
|
|
updateCheckFile, err := workspace.GetCachedVersionFilePath()
|
|
|
|
if err != nil {
|
|
|
|
return semver.Version{}, semver.Version{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ts, err := times.Stat(updateCheckFile)
|
|
|
|
if err != nil {
|
|
|
|
return semver.Version{}, semver.Version{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Now().After(ts.ModTime().Add(24 * time.Hour)) {
|
|
|
|
return semver.Version{}, semver.Version{}, errors.New("cached expired")
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.OpenFile(updateCheckFile, os.O_RDONLY, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return semver.Version{}, semver.Version{}, err
|
|
|
|
}
|
|
|
|
defer contract.IgnoreClose(file)
|
|
|
|
|
|
|
|
var cached cachedVersionInfo
|
|
|
|
if err = json.NewDecoder(file).Decode(&cached); err != nil {
|
|
|
|
return semver.Version{}, semver.Version{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
latest, err := semver.ParseTolerant(cached.LatestVersion)
|
|
|
|
if err != nil {
|
|
|
|
return semver.Version{}, semver.Version{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
oldest, err := semver.ParseTolerant(cached.OldestWithoutWarning)
|
|
|
|
if err != nil {
|
|
|
|
return semver.Version{}, semver.Version{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return latest, oldest, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// cachedVersionInfo is the on disk format of the version information the CLI caches between runs.
|
|
|
|
type cachedVersionInfo struct {
|
|
|
|
LatestVersion string `json:"latestVersion"`
|
|
|
|
OldestWithoutWarning string `json:"oldestWithoutWarning"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// getUpgradeMessage gets a message to display to a user instructing them they are out of date and how to move from
|
|
|
|
// current to latest.
|
|
|
|
func getUpgradeMessage(latest semver.Version, current semver.Version) string {
|
|
|
|
cmd := getUpgradeCommand()
|
|
|
|
|
|
|
|
msg := fmt.Sprintf("A new version of Pulumi is available. To upgrade from version '%s' to '%s', ", current, latest)
|
|
|
|
if cmd != "" {
|
|
|
|
msg += "run \n " + cmd + "\nor "
|
|
|
|
}
|
|
|
|
|
2019-07-16 19:05:37 +02:00
|
|
|
msg += "visit https://pulumi.com/docs/reference/install/ for manual instructions and release notes."
|
2018-08-02 23:45:50 +02:00
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// getUpgradeCommand returns a command that will upgrade the CLI to the newest version. If we can not determine how
|
|
|
|
// the CLI was installed, the empty string is returned.
|
|
|
|
func getUpgradeCommand() string {
|
|
|
|
curUser, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
exe, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-03-06 19:56:44 +01:00
|
|
|
isBrew, err := isBrewInstall(exe)
|
|
|
|
if err != nil {
|
2019-07-31 20:23:33 +02:00
|
|
|
logging.V(3).Infof("error determining if the running executable was installed with brew: %s", err)
|
2019-03-06 19:56:44 +01:00
|
|
|
}
|
|
|
|
if isBrew {
|
|
|
|
return "$ brew upgrade pulumi"
|
|
|
|
}
|
|
|
|
|
2019-10-09 00:01:46 +02:00
|
|
|
if filepath.Dir(exe) != filepath.Join(curUser.HomeDir, workspace.BookkeepingDir, "bin") {
|
2018-08-02 23:45:50 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
return "$ curl -sSL https://get.pulumi.com | sh"
|
|
|
|
}
|
|
|
|
|
|
|
|
powershellCmd := `"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"`
|
|
|
|
|
|
|
|
if _, err := exec.LookPath("powershell"); err == nil {
|
|
|
|
powershellCmd = "powershell"
|
|
|
|
}
|
|
|
|
|
2019-01-17 00:54:00 +01:00
|
|
|
return "> " + powershellCmd + ` -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ` +
|
2018-08-02 23:45:50 +02:00
|
|
|
`((New-Object System.Net.WebClient).DownloadString('https://get.pulumi.com/install.ps1'))"`
|
|
|
|
}
|
|
|
|
|
2019-03-06 19:56:44 +01:00
|
|
|
// isBrewInstall returns true if the current running executable is running on macOS and was installed with brew.
|
|
|
|
func isBrewInstall(exe string) (bool, error) {
|
|
|
|
if runtime.GOOS != "darwin" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
exePath, err := filepath.EvalSymlinks(exe)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
brewBin, err := exec.LookPath("brew")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
brewPrefixCmd := exec.Command(brewBin, "--prefix", "pulumi")
|
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
brewPrefixCmd.Stdout = &stdout
|
2019-03-07 00:21:32 +01:00
|
|
|
brewPrefixCmd.Stderr = &stderr
|
2019-03-06 19:56:44 +01:00
|
|
|
if err = brewPrefixCmd.Run(); err != nil {
|
|
|
|
if ee, ok := err.(*exec.ExitError); ok {
|
|
|
|
ee.Stderr = stderr.Bytes()
|
|
|
|
}
|
|
|
|
return false, errors.Wrapf(err, "'brew --prefix pulumi' failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
brewPrefixCmdOutput := strings.TrimSpace(stdout.String())
|
|
|
|
if brewPrefixCmdOutput == "" {
|
|
|
|
return false, errors.New("trimmed output from 'brew --prefix pulumi' is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
brewPrefixPath, err := filepath.EvalSymlinks(brewPrefixCmdOutput)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
brewPrefixExePath := filepath.Join(brewPrefixPath, "bin", "pulumi")
|
|
|
|
return exePath == brewPrefixExePath, nil
|
|
|
|
}
|
|
|
|
|
2019-10-01 22:34:42 +02:00
|
|
|
func getLatestBrewFormulaVersion() (semver.Version, bool, error) {
|
|
|
|
exe, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return semver.Version{}, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
isBrew, err := isBrewInstall(exe)
|
|
|
|
if err != nil {
|
|
|
|
return semver.Version{}, false, err
|
|
|
|
}
|
|
|
|
if !isBrew {
|
|
|
|
return semver.Version{}, false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
url, err := url.Parse("https://formulae.brew.sh/api/formula/pulumi.json")
|
|
|
|
contract.AssertNoError(err)
|
|
|
|
|
|
|
|
resp, err := httputil.DoWithRetry(&http.Request{
|
|
|
|
Method: http.MethodGet,
|
|
|
|
URL: url,
|
|
|
|
}, http.DefaultClient)
|
|
|
|
if err != nil {
|
|
|
|
return semver.Version{}, false, err
|
|
|
|
}
|
|
|
|
defer contract.IgnoreClose(resp.Body)
|
|
|
|
|
|
|
|
type versions struct {
|
|
|
|
Stable string `json:"stable"`
|
|
|
|
}
|
|
|
|
var formula struct {
|
|
|
|
Versions versions `json:"versions"`
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&formula); err != nil {
|
|
|
|
return semver.Version{}, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stable, err := semver.ParseTolerant(formula.Versions.Stable)
|
|
|
|
if err != nil {
|
|
|
|
return semver.Version{}, false, err
|
|
|
|
}
|
|
|
|
return stable, true, nil
|
|
|
|
}
|
|
|
|
|
2018-08-02 23:45:50 +02:00
|
|
|
func isDevVersion(s semver.Version) bool {
|
|
|
|
if len(s.Pre) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return !s.Pre[0].IsNum && strings.HasPrefix("dev", s.Pre[0].VersionStr)
|
|
|
|
}
|
|
|
|
|
2018-09-05 00:40:15 +02:00
|
|
|
func confirmPrompt(prompt string, name string, opts display.Options) bool {
|
2018-02-19 18:06:02 +01:00
|
|
|
if prompt != "" {
|
|
|
|
fmt.Print(
|
2018-07-07 06:30:00 +02:00
|
|
|
opts.Color.Colorize(
|
2018-02-19 18:06:02 +01:00
|
|
|
fmt.Sprintf("%s%s%s\n", colors.SpecAttention, prompt, colors.Reset)))
|
|
|
|
}
|
|
|
|
|
2017-08-23 01:56:15 +02:00
|
|
|
fmt.Print(
|
2018-07-07 06:30:00 +02:00
|
|
|
opts.Color.Colorize(
|
2018-02-19 18:06:02 +01:00
|
|
|
fmt.Sprintf("%sPlease confirm that this is what you'd like to do by typing (%s\"%s\"%s):%s ",
|
2018-09-19 10:40:03 +02:00
|
|
|
colors.SpecAttention, colors.SpecPrompt, name, colors.SpecAttention, colors.Reset)))
|
2018-02-19 18:06:02 +01:00
|
|
|
|
2017-08-23 01:56:15 +02:00
|
|
|
reader := bufio.NewReader(os.Stdin)
|
2018-02-14 23:57:15 +01:00
|
|
|
line, _ := reader.ReadString('\n')
|
|
|
|
return strings.TrimSpace(line) == name
|
2017-06-02 21:43:04 +02:00
|
|
|
}
|