2011-01-23 19:52:24 +00:00
|
|
|
module Vagrant
|
|
|
|
module Provisioners
|
|
|
|
class Shell < Base
|
|
|
|
class Config < Vagrant::Config::Base
|
2011-07-06 06:09:36 +00:00
|
|
|
attr_accessor :inline
|
2011-01-23 19:52:24 +00:00
|
|
|
attr_accessor :path
|
|
|
|
attr_accessor :upload_path
|
2011-08-28 07:01:23 +00:00
|
|
|
attr_accessor :args
|
2011-01-23 19:52:24 +00:00
|
|
|
|
|
|
|
def initialize
|
2011-07-06 06:09:36 +00:00
|
|
|
@inline = nil
|
|
|
|
@path = nil
|
2011-01-23 19:52:24 +00:00
|
|
|
@upload_path = "/tmp/vagrant-shell"
|
2011-08-28 07:01:23 +00:00
|
|
|
@args = nil
|
2011-01-23 19:52:24 +00:00
|
|
|
end
|
|
|
|
|
2011-12-14 07:45:56 +00:00
|
|
|
def validate(env, errors)
|
2011-01-23 19:52:24 +00:00
|
|
|
super
|
|
|
|
|
2011-07-06 06:09:36 +00:00
|
|
|
# Validate that the parameters are properly set
|
|
|
|
if path && inline
|
|
|
|
errors.add(I18n.t("vagrant.provisioners.shell.path_and_inline_set"))
|
|
|
|
elsif !path && !inline
|
|
|
|
errors.add(I18n.t("vagrant.provisioners.shell.no_path_or_inline"))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Validate the existence of a script to upload
|
2011-12-15 03:02:10 +00:00
|
|
|
if path
|
|
|
|
expanded_path = Pathname.new(path).expand_path(env.root_path)
|
|
|
|
if !expanded_path.file?
|
|
|
|
errors.add(I18n.t("vagrant.provisioners.shell.path_invalid",
|
|
|
|
:path => expanded_path))
|
|
|
|
end
|
2011-01-23 19:52:24 +00:00
|
|
|
end
|
|
|
|
|
2011-07-06 06:09:36 +00:00
|
|
|
# There needs to be a path to upload the script to
|
2011-01-23 19:52:24 +00:00
|
|
|
if !upload_path
|
|
|
|
errors.add(I18n.t("vagrant.provisioners.shell.upload_path_not_set"))
|
|
|
|
end
|
2011-08-28 07:02:42 +00:00
|
|
|
|
|
|
|
# If there are args and its not a string, that is a problem
|
|
|
|
if args && !args.is_a?(String)
|
|
|
|
errors.add(I18n.t("vagrant.provisioners.shell.args_not_string"))
|
|
|
|
end
|
2011-01-23 19:52:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-15 06:43:45 +00:00
|
|
|
def self.config_class
|
|
|
|
Config
|
|
|
|
end
|
|
|
|
|
2011-07-06 06:09:36 +00:00
|
|
|
# This method yields the path to a script to upload and execute
|
|
|
|
# on the remote server. This method will properly clean up the
|
|
|
|
# script file if needed.
|
|
|
|
def with_script_file
|
|
|
|
if config.path
|
|
|
|
# Just yield the path to that file...
|
2011-12-14 07:45:56 +00:00
|
|
|
yield Pathname.new(config.path).expand_path(env[:root_path])
|
2011-07-06 06:09:36 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# Otherwise we have an inline script, we need to Tempfile it,
|
|
|
|
# and handle it specially...
|
|
|
|
file = Tempfile.new('vagrant-shell')
|
|
|
|
begin
|
|
|
|
file.write(config.inline)
|
|
|
|
file.fsync
|
|
|
|
yield file.path
|
|
|
|
ensure
|
|
|
|
file.close
|
|
|
|
file.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-23 19:52:24 +00:00
|
|
|
def provision!
|
2011-08-28 07:01:23 +00:00
|
|
|
args = ""
|
|
|
|
args = " #{config.args}" if config.args
|
|
|
|
commands = ["chmod +x #{config.upload_path}", "#{config.upload_path}#{args}"]
|
2011-01-25 19:43:25 +00:00
|
|
|
|
2011-07-06 06:09:36 +00:00
|
|
|
with_script_file do |path|
|
|
|
|
# Upload the script to the VM
|
2011-12-14 07:45:56 +00:00
|
|
|
env[:vm].ssh.upload!(path.to_s, config.upload_path)
|
2011-01-23 19:52:24 +00:00
|
|
|
|
2011-07-06 06:09:36 +00:00
|
|
|
# Execute it with sudo
|
2011-12-14 07:45:56 +00:00
|
|
|
env[:vm].ssh.execute do |ssh|
|
2011-07-06 06:09:36 +00:00
|
|
|
ssh.sudo!(commands) do |ch, type, data|
|
|
|
|
if type == :exit_status
|
|
|
|
ssh.check_exit_status(data, commands)
|
|
|
|
else
|
2011-12-14 07:45:56 +00:00
|
|
|
env[:ui].info(data)
|
2011-07-06 06:09:36 +00:00
|
|
|
end
|
2011-01-23 20:06:09 +00:00
|
|
|
end
|
|
|
|
end
|
2011-01-23 19:52:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|