Merge pull request #8839 from chrisroberts/windows/req-min-ps
Validate powershell prior to powershell use
This commit is contained in:
commit
4eb8d7a098
|
@ -536,6 +536,14 @@ module Vagrant
|
||||||
error_key(:requires_directory, "vagrant.actions.general.package")
|
error_key(:requires_directory, "vagrant.actions.general.package")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class PowerShellNotFound < VagrantError
|
||||||
|
error_key(:powershell_not_found)
|
||||||
|
end
|
||||||
|
|
||||||
|
class PowerShellInvalidVersion < VagrantError
|
||||||
|
error_key(:powershell_invalid_version)
|
||||||
|
end
|
||||||
|
|
||||||
class ProviderCantInstall < VagrantError
|
class ProviderCantInstall < VagrantError
|
||||||
error_key(:provider_cant_install)
|
error_key(:provider_cant_install)
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,8 +8,15 @@ module Vagrant
|
||||||
# This is primarily a convenience wrapper around Subprocess that
|
# This is primarily a convenience wrapper around Subprocess that
|
||||||
# properly sets powershell flags for you.
|
# properly sets powershell flags for you.
|
||||||
class PowerShell
|
class PowerShell
|
||||||
|
# NOTE: Version checks are only on Major
|
||||||
|
MINIMUM_REQUIRED_VERSION = 3
|
||||||
|
|
||||||
|
# @return [Boolean] powershell executable available on PATH
|
||||||
def self.available?
|
def self.available?
|
||||||
!!Which.which("powershell")
|
if !defined?(@_powershell_available)
|
||||||
|
@_powershell_available = !!Which.which("powershell")
|
||||||
|
end
|
||||||
|
@_powershell_available
|
||||||
end
|
end
|
||||||
|
|
||||||
# Execute a powershell script.
|
# Execute a powershell script.
|
||||||
|
@ -17,6 +24,7 @@ module Vagrant
|
||||||
# @param [String] path Path to the PowerShell script to execute.
|
# @param [String] path Path to the PowerShell script to execute.
|
||||||
# @return [Subprocess::Result]
|
# @return [Subprocess::Result]
|
||||||
def self.execute(path, *args, **opts, &block)
|
def self.execute(path, *args, **opts, &block)
|
||||||
|
validate_install!
|
||||||
command = [
|
command = [
|
||||||
"powershell",
|
"powershell",
|
||||||
"-NoLogo",
|
"-NoLogo",
|
||||||
|
@ -39,6 +47,7 @@ module Vagrant
|
||||||
# @param [String] command PowerShell command to execute.
|
# @param [String] command PowerShell command to execute.
|
||||||
# @return [Subprocess::Result]
|
# @return [Subprocess::Result]
|
||||||
def self.execute_cmd(command)
|
def self.execute_cmd(command)
|
||||||
|
validate_install!
|
||||||
c = [
|
c = [
|
||||||
"powershell",
|
"powershell",
|
||||||
"-NoLogo",
|
"-NoLogo",
|
||||||
|
@ -58,19 +67,39 @@ module Vagrant
|
||||||
#
|
#
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def self.version
|
def self.version
|
||||||
command = [
|
if !defined?(@_powershell_version)
|
||||||
"powershell",
|
command = [
|
||||||
"-NoLogo",
|
"powershell",
|
||||||
"-NoProfile",
|
"-NoLogo",
|
||||||
"-NonInteractive",
|
"-NoProfile",
|
||||||
"-ExecutionPolicy", "Bypass",
|
"-NonInteractive",
|
||||||
"-Command",
|
"-ExecutionPolicy", "Bypass",
|
||||||
"$PSVersionTable.PSVersion.Major"
|
"-Command",
|
||||||
].flatten
|
"$PSVersionTable.PSVersion.Major"
|
||||||
|
].flatten
|
||||||
|
|
||||||
r = Subprocess.execute(*command)
|
r = Subprocess.execute(*command)
|
||||||
return nil if r.exit_code != 0
|
@_powershell_version = r.exit_code != 0 ? nil : r.stdout.chomp
|
||||||
return r.stdout.chomp
|
end
|
||||||
|
@_powershell_version
|
||||||
|
end
|
||||||
|
|
||||||
|
# Validates that powershell is installed, available, and
|
||||||
|
# at or above minimum required version
|
||||||
|
#
|
||||||
|
# @return [Boolean]
|
||||||
|
# @raises []
|
||||||
|
def self.validate_install!
|
||||||
|
if !defined?(@_powershell_validation)
|
||||||
|
raise Errors::PowerShellNotFound if !available?
|
||||||
|
if version.to_i < MINIMUM_REQUIRED_VERSION
|
||||||
|
raise Errors::PowerShellInvalidVersion,
|
||||||
|
minimum_version: MINIMUM_REQUIRED_VERSION,
|
||||||
|
installed_version: version ? version : "N/A"
|
||||||
|
end
|
||||||
|
@_powershell_validation = true
|
||||||
|
end
|
||||||
|
@_powershell_validation
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1051,6 +1051,17 @@ en:
|
||||||
%{error_msg}
|
%{error_msg}
|
||||||
|
|
||||||
Source: %{source}
|
Source: %{source}
|
||||||
|
powershell_not_found: |-
|
||||||
|
Failed to locate the powershell executable on the available PATH. Please
|
||||||
|
ensure powershell is installed and available on the local PATH, then
|
||||||
|
run the command again.
|
||||||
|
powershell_invalid_version: |-
|
||||||
|
The version of powershell currently installed on this host is less than
|
||||||
|
the required minimum version. Please upgrade the installed version of
|
||||||
|
powershell to the minimum required version and run the command again.
|
||||||
|
|
||||||
|
Installed version: %{installed_version}
|
||||||
|
Minimum required version: %{minimum_version}
|
||||||
pushes_not_defined: |-
|
pushes_not_defined: |-
|
||||||
The Vagrantfile does not define any 'push' strategies. In order to use
|
The Vagrantfile does not define any 'push' strategies. In order to use
|
||||||
`vagrant push`, you must define at least one push strategy:
|
`vagrant push`, you must define at least one push strategy:
|
||||||
|
|
Loading…
Reference in New Issue