Handle invalid Pulumi CLI version (#8289)

* Handle invalid Pulumi CLI version

* Add clarifying comments and improve error message.

* Update go version of `validatePulumiVersion`
This commit is contained in:
Ian Wahbe 2021-10-26 16:20:45 -07:00 committed by GitHub
parent a199ba8bb5
commit c8e117a37d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 15 deletions

View file

@ -28,6 +28,7 @@ import (
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
@ -591,18 +592,24 @@ func NewLocalWorkspace(ctx context.Context, opts ...LocalWorkspaceOption) (Works
pulumiHome: lwOpts.PulumiHome,
}
v, err := l.getPulumiVersion(ctx)
if err != nil {
return nil, errors.Wrap(err, "failed to create workspace, unable to get pulumi version")
}
l.pulumiVersion = v
optOut := os.Getenv(skipVersionCheckVar) != ""
// optOut indicates we should skip the version check.
optOut := cmdutil.IsTruthy(os.Getenv(skipVersionCheckVar))
if val, ok := lwOpts.EnvVars[skipVersionCheckVar]; ok {
optOut = optOut || val != ""
optOut = optOut || cmdutil.IsTruthy(val)
}
v, err := l.getPulumiVersion(ctx)
if err == nil {
l.pulumiVersion = v
}
if !optOut {
if err != nil {
return nil, errors.Wrapf(err,
"failed to create workspace, unable to get pulumi version (skip with %s=true)", skipVersionCheckVar)
}
if err = validatePulumiVersion(minimumVersion, l.pulumiVersion, optOut); err != nil {
return nil, err
}
if err = validatePulumiVersion(minimumVersion, l.pulumiVersion, optOut); err != nil {
return nil, err
}
if lwOpts.Project != nil {

View file

@ -1,4 +1,4 @@
// Copyright 2016-2020, Pulumi Corporation.
// Copyright 2016-2021, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -68,9 +68,14 @@ export class LocalWorkspace implements Workspace {
private _pulumiVersion?: semver.SemVer;
/**
* The version of the underlying Pulumi CLI/Engine.
*
* @returns A string representation of the version, if available. `null` otherwise.
*/
public get pulumiVersion(): string {
return this._pulumiVersion!.toString();
if (this._pulumiVersion === undefined) {
throw new Error(`Failed to get Pulumi version`);
}
return this._pulumiVersion.toString();
}
private ready: Promise<any[]>;
/**
@ -587,10 +592,12 @@ export class LocalWorkspace implements Workspace {
}
private async getPulumiVersion(minVersion: semver.SemVer) {
const result = await this.runPulumiCmd(["version"]);
const version = new semver.SemVer(result.stdout.trim());
const version = semver.parse(result.stdout.trim());
const optOut = !!this.envVars[SKIP_VERSION_CHECK_VAR] || !!process.env[SKIP_VERSION_CHECK_VAR];
validatePulumiVersion(minVersion, version, optOut);
this._pulumiVersion = version;
if (version != null) {
this._pulumiVersion = version;
}
}
private async runPulumiCmd(
args: string[],
@ -717,11 +724,21 @@ function loadProjectSettings(workDir: string) {
throw new Error(`failed to find project settings file in workdir: ${workDir}`);
}
/** @internal */
export function validatePulumiVersion(minVersion: semver.SemVer, currentVersion: semver.SemVer, optOut: boolean) {
/**
* @internal
* Throws an error if the Pulumi CLI version is not valid.
*
* @param minVersion The minimum acceptable version of the Pulumi CLI.
* @param currentVersion The currently known version. `null` indicates that the current version is unknown.
* @paramoptOut If the user has opted out of the version check.
*/
export function validatePulumiVersion(minVersion: semver.SemVer, currentVersion: semver.SemVer | null, optOut: boolean) {
if (optOut) {
return;
}
if (currentVersion == null) {
throw new Error(`Failed to parse Pulumi CLI version. This is probably an internal error. You can override this by setting "${SKIP_VERSION_CHECK_VAR}" to "true".`);
}
if (minVersion.major < currentVersion.major) {
throw new Error(`Major version mismatch. You are using Pulumi CLI version ${currentVersion.toString()} with Automation SDK v${minVersion.major}. Please update the SDK.`);
}