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")
|
||||
end
|
||||
|
||||
class PowerShellNotFound < VagrantError
|
||||
error_key(:powershell_not_found)
|
||||
end
|
||||
|
||||
class PowerShellInvalidVersion < VagrantError
|
||||
error_key(:powershell_invalid_version)
|
||||
end
|
||||
|
||||
class ProviderCantInstall < VagrantError
|
||||
error_key(:provider_cant_install)
|
||||
end
|
||||
|
|
|
@ -8,8 +8,15 @@ module Vagrant
|
|||
# This is primarily a convenience wrapper around Subprocess that
|
||||
# properly sets powershell flags for you.
|
||||
class PowerShell
|
||||
# NOTE: Version checks are only on Major
|
||||
MINIMUM_REQUIRED_VERSION = 3
|
||||
|
||||
# @return [Boolean] powershell executable available on PATH
|
||||
def self.available?
|
||||
!!Which.which("powershell")
|
||||
if !defined?(@_powershell_available)
|
||||
@_powershell_available = !!Which.which("powershell")
|
||||
end
|
||||
@_powershell_available
|
||||
end
|
||||
|
||||
# Execute a powershell script.
|
||||
|
@ -17,6 +24,7 @@ module Vagrant
|
|||
# @param [String] path Path to the PowerShell script to execute.
|
||||
# @return [Subprocess::Result]
|
||||
def self.execute(path, *args, **opts, &block)
|
||||
validate_install!
|
||||
command = [
|
||||
"powershell",
|
||||
"-NoLogo",
|
||||
|
@ -39,6 +47,7 @@ module Vagrant
|
|||
# @param [String] command PowerShell command to execute.
|
||||
# @return [Subprocess::Result]
|
||||
def self.execute_cmd(command)
|
||||
validate_install!
|
||||
c = [
|
||||
"powershell",
|
||||
"-NoLogo",
|
||||
|
@ -58,6 +67,7 @@ module Vagrant
|
|||
#
|
||||
# @return [String]
|
||||
def self.version
|
||||
if !defined?(@_powershell_version)
|
||||
command = [
|
||||
"powershell",
|
||||
"-NoLogo",
|
||||
|
@ -69,8 +79,27 @@ module Vagrant
|
|||
].flatten
|
||||
|
||||
r = Subprocess.execute(*command)
|
||||
return nil if r.exit_code != 0
|
||||
return r.stdout.chomp
|
||||
@_powershell_version = r.exit_code != 0 ? nil : 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
|
||||
|
|
|
@ -1051,6 +1051,17 @@ en:
|
|||
%{error_msg}
|
||||
|
||||
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: |-
|
||||
The Vagrantfile does not define any 'push' strategies. In order to use
|
||||
`vagrant push`, you must define at least one push strategy:
|
||||
|
|
Loading…
Reference in New Issue