pulumi/pkg/workspace/plugin.go

102 lines
3.4 KiB
Go
Raw Permalink Normal View History

// Copyright 2016-2023, 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 workspace
import (
"fmt"
"io"
"time"
"github.com/blang/semver"
"github.com/pulumi/pulumi/pkg/v3/util"
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/logging"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
)
// InstallPluginError is returned by InstallPlugin if we couldn't install the plugin
type InstallPluginError struct {
// The specification of the plugin to install
Spec workspace.PluginSpec
// The underlying error that occurred during the download or install.
Err error
}
func (err *InstallPluginError) Error() string {
var server string
if err.Spec.PluginDownloadURL != "" {
Enable perfsprint linter (#14813) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Prompted by a comment in another review: https://github.com/pulumi/pulumi/pull/14654#discussion_r1419995945 This lints that we don't use `fmt.Errorf` when `errors.New` will suffice, it also covers a load of other cases where `Sprintf` is sub-optimal. Most of these edits were made by running `perfsprint --fix`. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-12 12:19:42 +00:00
server = " --server " + err.Spec.PluginDownloadURL
}
if err.Spec.Version != nil {
return fmt.Sprintf("Could not automatically download and install %[1]s plugin 'pulumi-%[1]s-%[2]s'"+
" at version v%[3]s"+
", install the plugin using `pulumi plugin install %[1]s %[2]s v%[3]s%[4]s`: %[5]v",
err.Spec.Kind, err.Spec.Name, err.Spec.Version, server, err.Err)
}
return fmt.Sprintf("Could not automatically download and install %[1]s plugin 'pulumi-%[1]s-%[2]s'"+
", install the plugin using `pulumi plugin install %[1]s %[2]s%[3]s`: %[4]v",
err.Spec.Kind, err.Spec.Name, server, err.Err)
}
func (err *InstallPluginError) Unwrap() error {
return err.Err
}
func InstallPlugin(pluginSpec workspace.PluginSpec, log func(sev diag.Severity, msg string)) (*semver.Version, error) {
util.SetKnownPluginDownloadURL(&pluginSpec)
Use the yaml converter plugin rather than Eject (#14437) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> We add some hooks to ensure we always use the version of pulumi-converter-yaml that matches the version of yaml we've linked the CLI to, this preserves the behaviour we have today that eject code matches the gen code. At some point it will make sense to decouple these and often just default to the the latest converter instead. This unlinks one part of the yaml codebase from the cli. We still need to do codegen and docgen, but this at least means breaking changes can be made to the eject interface without breaking the build because of the circular dependency cycle. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works - Covered by `TestYamlConvertSmoke` <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-08 08:55:49 +00:00
util.SetKnownPluginVersion(&pluginSpec)
if pluginSpec.Version == nil {
var err error
pluginSpec.Version, err = pluginSpec.GetLatestVersion()
if err != nil {
return nil, fmt.Errorf("could not find latest version for provider %s: %w", pluginSpec.Name, err)
}
}
wrapper := func(stream io.ReadCloser, size int64) io.ReadCloser {
// Log at info but to stderr so we don't pollute stdout for commands like `package get-schema`
Enable perfsprint linter (#14813) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Prompted by a comment in another review: https://github.com/pulumi/pulumi/pull/14654#discussion_r1419995945 This lints that we don't use `fmt.Errorf` when `errors.New` will suffice, it also covers a load of other cases where `Sprintf` is sub-optimal. Most of these edits were made by running `perfsprint --fix`. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-12 12:19:42 +00:00
log(diag.Infoerr, "Downloading provider: "+pluginSpec.Name)
return stream
}
retry := func(err error, attempt int, limit int, delay time.Duration) {
log(diag.Warning, fmt.Sprintf("error downloading provider: %s\n"+
"Will retry in %v [%d/%d]", err, delay, attempt, limit))
}
logging.V(1).Infof("Automatically downloading provider %s", pluginSpec.Name)
downloadedFile, err := workspace.DownloadToFile(pluginSpec, wrapper, retry)
if err != nil {
return nil, &InstallPluginError{
Spec: pluginSpec,
Err: fmt.Errorf("error downloading provider %s to file: %w", pluginSpec.Name, err),
}
}
logging.V(1).Infof("Automatically installing provider %s", pluginSpec.Name)
err = pluginSpec.Install(downloadedFile, false)
if err != nil {
return nil, &InstallPluginError{
Spec: pluginSpec,
Err: fmt.Errorf("error installing provider %s: %w", pluginSpec.Name, err),
}
}
return pluginSpec.Version, nil
}