2013-07-12 13:40:41 +00:00
|
|
|
require 'uri'
|
|
|
|
|
2013-01-13 23:48:52 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module Shell
|
|
|
|
class Config < Vagrant.plugin("2", :config)
|
|
|
|
attr_accessor :inline
|
|
|
|
attr_accessor :path
|
2016-11-10 00:00:09 +00:00
|
|
|
attr_accessor :md5
|
|
|
|
attr_accessor :sha1
|
2015-11-24 00:24:06 +00:00
|
|
|
attr_accessor :env
|
2013-01-13 23:48:52 +00:00
|
|
|
attr_accessor :upload_path
|
|
|
|
attr_accessor :args
|
2013-02-11 18:42:36 +00:00
|
|
|
attr_accessor :privileged
|
2013-09-19 11:33:58 +00:00
|
|
|
attr_accessor :binary
|
2013-11-16 12:30:58 +00:00
|
|
|
attr_accessor :keep_color
|
2015-04-18 04:35:21 +00:00
|
|
|
attr_accessor :name
|
2018-01-13 01:33:47 +00:00
|
|
|
attr_accessor :sensitive
|
2014-10-23 16:53:14 +00:00
|
|
|
attr_accessor :powershell_args
|
2015-11-18 20:51:18 +00:00
|
|
|
attr_accessor :powershell_elevated_interactive
|
2018-11-12 23:34:10 +00:00
|
|
|
attr_accessor :reset
|
2013-01-13 23:48:52 +00:00
|
|
|
|
|
|
|
def initialize
|
2015-08-21 20:37:10 +00:00
|
|
|
@args = UNSET_VALUE
|
|
|
|
@inline = UNSET_VALUE
|
|
|
|
@path = UNSET_VALUE
|
2016-11-10 00:00:09 +00:00
|
|
|
@md5 = UNSET_VALUE
|
|
|
|
@sha1 = UNSET_VALUE
|
2015-11-24 00:24:06 +00:00
|
|
|
@env = UNSET_VALUE
|
2015-08-21 20:37:10 +00:00
|
|
|
@upload_path = UNSET_VALUE
|
|
|
|
@privileged = UNSET_VALUE
|
|
|
|
@binary = UNSET_VALUE
|
|
|
|
@keep_color = UNSET_VALUE
|
|
|
|
@name = UNSET_VALUE
|
2018-01-13 01:33:47 +00:00
|
|
|
@sensitive = UNSET_VALUE
|
2018-11-12 23:34:10 +00:00
|
|
|
@reset = UNSET_VALUE
|
2015-08-21 20:37:10 +00:00
|
|
|
@powershell_args = UNSET_VALUE
|
2015-11-18 20:51:18 +00:00
|
|
|
@powershell_elevated_interactive = UNSET_VALUE
|
2013-03-17 23:18:49 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def finalize!
|
2015-08-21 20:37:10 +00:00
|
|
|
@args = nil if @args == UNSET_VALUE
|
|
|
|
@inline = nil if @inline == UNSET_VALUE
|
|
|
|
@path = nil if @path == UNSET_VALUE
|
2016-11-10 00:00:09 +00:00
|
|
|
@md5 = nil if @md5 == UNSET_VALUE
|
|
|
|
@sha1 = nil if @sha1 == UNSET_VALUE
|
2015-11-24 00:24:06 +00:00
|
|
|
@env = {} if @env == UNSET_VALUE
|
2015-08-21 20:37:10 +00:00
|
|
|
@upload_path = "/tmp/vagrant-shell" if @upload_path == UNSET_VALUE
|
|
|
|
@privileged = true if @privileged == UNSET_VALUE
|
|
|
|
@binary = false if @binary == UNSET_VALUE
|
|
|
|
@keep_color = false if @keep_color == UNSET_VALUE
|
|
|
|
@name = nil if @name == UNSET_VALUE
|
2018-01-13 01:33:47 +00:00
|
|
|
@sensitive = false if @sensitive == UNSET_VALUE
|
2018-11-12 23:34:10 +00:00
|
|
|
@reset = false if @reset == UNSET_VALUE
|
2015-08-21 20:37:10 +00:00
|
|
|
@powershell_args = "-ExecutionPolicy Bypass" if @powershell_args == UNSET_VALUE
|
2015-11-18 20:51:18 +00:00
|
|
|
@powershell_elevated_interactive = false if @powershell_elevated_interactive == UNSET_VALUE
|
2014-02-14 20:43:27 +00:00
|
|
|
|
2014-07-11 21:09:31 +00:00
|
|
|
if @args && args_valid?
|
|
|
|
@args = @args.is_a?(Array) ? @args.map { |a| a.to_s } : @args.to_s
|
2014-02-14 20:43:27 +00:00
|
|
|
end
|
2018-01-13 01:33:47 +00:00
|
|
|
|
|
|
|
if @sensitive
|
|
|
|
@env.each do |_, v|
|
|
|
|
Vagrant::Util::CredentialScrubber.sensitive(v)
|
|
|
|
end
|
|
|
|
end
|
2013-01-13 23:48:52 +00:00
|
|
|
end
|
|
|
|
|
2013-01-18 21:06:29 +00:00
|
|
|
def validate(machine)
|
2013-04-03 23:18:37 +00:00
|
|
|
errors = _detected_errors
|
2013-01-18 21:06:29 +00:00
|
|
|
|
2013-01-13 23:48:52 +00:00
|
|
|
# Validate that the parameters are properly set
|
|
|
|
if path && inline
|
2013-01-18 21:06:29 +00:00
|
|
|
errors << I18n.t("vagrant.provisioners.shell.path_and_inline_set")
|
2018-11-12 23:34:10 +00:00
|
|
|
elsif !path && !inline && !reset
|
2013-01-18 21:06:29 +00:00
|
|
|
errors << I18n.t("vagrant.provisioners.shell.no_path_or_inline")
|
2013-01-13 23:48:52 +00:00
|
|
|
end
|
|
|
|
|
2013-07-12 13:40:41 +00:00
|
|
|
# If it is not an URL, we validate the existence of a script to upload
|
2014-02-24 05:50:53 +00:00
|
|
|
if path && !remote?
|
2013-01-18 21:06:29 +00:00
|
|
|
expanded_path = Pathname.new(path).expand_path(machine.env.root_path)
|
2013-01-13 23:48:52 +00:00
|
|
|
if !expanded_path.file?
|
2013-01-18 21:06:29 +00:00
|
|
|
errors << I18n.t("vagrant.provisioners.shell.path_invalid",
|
2014-05-22 16:35:12 +00:00
|
|
|
path: expanded_path)
|
2014-02-24 05:50:53 +00:00
|
|
|
else
|
|
|
|
data = expanded_path.read(16)
|
2014-04-09 21:17:51 +00:00
|
|
|
if data && !data.valid_encoding?
|
2014-02-24 05:50:53 +00:00
|
|
|
errors << I18n.t(
|
|
|
|
"vagrant.provisioners.shell.invalid_encoding",
|
|
|
|
actual: data.encoding.to_s,
|
|
|
|
default: Encoding.default_external.to_s,
|
|
|
|
path: expanded_path.to_s)
|
|
|
|
end
|
2013-01-13 23:48:52 +00:00
|
|
|
end
|
|
|
|
end
|
2015-11-24 00:24:06 +00:00
|
|
|
|
|
|
|
if !env.is_a?(Hash)
|
|
|
|
errors << I18n.t("vagrant.provisioners.shell.env_must_be_a_hash")
|
|
|
|
end
|
2013-01-13 23:48:52 +00:00
|
|
|
|
|
|
|
# There needs to be a path to upload the script to
|
|
|
|
if !upload_path
|
2013-01-18 21:06:29 +00:00
|
|
|
errors << I18n.t("vagrant.provisioners.shell.upload_path_not_set")
|
2013-01-13 23:48:52 +00:00
|
|
|
end
|
|
|
|
|
2013-11-29 08:02:20 +00:00
|
|
|
if !args_valid?
|
2013-11-25 21:36:51 +00:00
|
|
|
errors << I18n.t("vagrant.provisioners.shell.args_bad_type")
|
2013-01-13 23:48:52 +00:00
|
|
|
end
|
2013-01-18 21:06:29 +00:00
|
|
|
|
2015-11-18 20:51:18 +00:00
|
|
|
if powershell_elevated_interactive && !privileged
|
2015-08-21 20:37:10 +00:00
|
|
|
errors << I18n.t("vagrant.provisioners.shell.interactive_not_elevated")
|
|
|
|
end
|
|
|
|
|
2013-01-18 21:06:29 +00:00
|
|
|
{ "shell provisioner" => errors }
|
2013-01-13 23:48:52 +00:00
|
|
|
end
|
2013-07-12 13:40:41 +00:00
|
|
|
|
2013-11-29 01:54:10 +00:00
|
|
|
# Args are optional, but if they're provided we only support them as a
|
|
|
|
# string or as an array.
|
|
|
|
def args_valid?
|
2013-11-29 08:02:20 +00:00
|
|
|
return true if !args
|
2014-02-14 20:43:27 +00:00
|
|
|
return true if args.is_a?(String)
|
2017-02-14 09:13:29 +00:00
|
|
|
return true if args.is_a?(Integer)
|
2014-02-14 19:47:36 +00:00
|
|
|
if args.is_a?(Array)
|
|
|
|
args.each do |a|
|
2017-02-14 09:13:29 +00:00
|
|
|
return false if !a.kind_of?(String) && !a.kind_of?(Integer)
|
2014-02-14 19:47:36 +00:00
|
|
|
end
|
2014-02-14 20:43:27 +00:00
|
|
|
|
2014-02-14 19:47:36 +00:00
|
|
|
return true
|
|
|
|
end
|
2013-11-29 01:54:10 +00:00
|
|
|
end
|
|
|
|
|
2013-07-12 13:40:41 +00:00
|
|
|
def remote?
|
2013-09-03 17:44:16 +00:00
|
|
|
path =~ URI.regexp(["ftp", "http", "https"])
|
2013-07-12 13:40:41 +00:00
|
|
|
end
|
2013-01-13 23:48:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|