pulumi/cmd/plugin_install.go
joeduffy c04341edb2 Consult the program for its list of plugins
This change adds a GetRequiredPlugins RPC method to the language
host, enabling us to query it for its list of plugin requirements.
This is language-specific because it requires looking at the set
of dependencies (e.g., package.json files).

It also adds a call up front during any update/preview operation
to compute the set of plugins and require that they are present.
These plugins are populated in the cache and will be used for all
subsequent plugin-related operations during the engine's activity.

We now cache the language plugins, so that we may load them
eagerly too, which we never did previously due to the fact that
we needed to pass the monitor address at load time.  This was a
bit bizarre anyhow, since it's really the Run RPC function that
needs this information.  So, to enable caching and eager loading
-- which we need in order to invoke GetRequiredPlugins -- the
"phone home" monitor RPC address is passed at Run time.

In a subsequent change, we will switch to faulting in the plugins
that are missing -- rather than erroring -- in addition to
supporting the `pulumi plugin install` CLI command.
2018-02-18 08:08:15 -08:00

79 lines
2.9 KiB
Go

// Copyright 2016-2017, Pulumi Corporation. All rights reserved.
package cmd
import (
"github.com/blang/semver"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/pulumi/pulumi/pkg/backend/cloud"
"github.com/pulumi/pulumi/pkg/util/cmdutil"
"github.com/pulumi/pulumi/pkg/workspace"
)
func newPluginInstallCmd() *cobra.Command {
var cloudURL string
var cmd = &cobra.Command{
Use: "install [KIND NAME VERSION]",
Args: cmdutil.MaximumNArgs(3),
Short: "Install one or more plugins",
Long: "Install one or more plugins.\n" +
"\n" +
"By default, Pulumi will download plugins as needed during program execution.\n" +
"If you prefer, you may use the install command to manually install plugins:\n" +
"either by running it with a specific KIND, NAME, and VERSION, or by omitting\n" +
"these and letting Pulumi compute the set of plugins that may be required by\n" +
"the current project. VERSION cannot be a range: it must be a specific number.\n" +
"\n" +
"Note that in this latter mode, Pulumi is conservative and may download more\n" +
"than is strictly required. To only download the precise list of what a project\n" +
"needs, simply run Pulumi in its default mode of downloading them on demand: it\n" +
"will download precisely what it needs.",
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
// Parse the kind, name, and version, if specified.
var installs []workspace.PluginInfo
if len(args) > 0 {
if !workspace.IsPluginKind(args[0]) {
return errors.Errorf("unrecognized plugin kind: %s", args[0])
} else if len(args) < 2 {
return errors.New("missing plugin name argument")
} else if len(args) < 3 {
return errors.New("missing plugin version argument")
}
version, err := semver.ParseTolerant(args[2])
if err != nil {
return errors.Wrap(err, "invalid plugin semver")
}
installs = append(installs, workspace.PluginInfo{
Kind: workspace.PluginKind(args[0]),
Name: args[1],
Version: &version,
})
}
// If a specific plugin wasn't given, compute the set of plugins the current project needs.
// TODO[pulumi/home#11]: before calling this work item complete, we need to implement this functionality.
// Target the cloud URL for downloads.
releases := cloud.New(cmdutil.Diag(), cloud.ValueOrDefaultURL(cloudURL))
// Now for each kind, name, version pair, download it from the release website, and install it.
for _, install := range installs {
tarball, err := releases.DownloadPlugin(install, true)
if err != nil {
return errors.Wrapf(err, "downloading %s", install.String())
}
if err = install.Install(tarball); err != nil {
return errors.Wrapf(err, "installing %s", install.String())
}
}
return nil
}),
}
cmd.PersistentFlags().StringVarP(&cloudURL, "cloud-url", "c", "", "A cloud URL to download releases from")
return cmd
}