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
|
|
|
|
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
|
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
|
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
|
|
|
|
@upload_path = UNSET_VALUE
|
|
|
|
@privileged = UNSET_VALUE
|
|
|
|
@binary = UNSET_VALUE
|
|
|
|
@keep_color = UNSET_VALUE
|
|
|
|
@name = UNSET_VALUE
|
|
|
|
@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
|
|
|
|
@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
|
|
|
|
@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
|
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")
|
2013-01-13 23:48:52 +00:00
|
|
|
elsif !path && !inline
|
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
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
return true if args.is_a?(Fixnum)
|
2014-02-14 19:47:36 +00:00
|
|
|
if args.is_a?(Array)
|
|
|
|
args.each do |a|
|
2014-02-14 20:43:27 +00:00
|
|
|
return false if !a.kind_of?(String) && !a.kind_of?(Fixnum)
|
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
|