2012-04-19 04:53:19 +00:00
|
|
|
require "pathname"
|
|
|
|
require "tempfile"
|
2012-01-07 01:36:51 +00:00
|
|
|
|
2012-04-19 04:53:19 +00:00
|
|
|
module VagrantPlugins
|
|
|
|
module Shell
|
2012-06-26 23:04:51 +00:00
|
|
|
class Provisioner < Vagrant.plugin("1", :provisioner)
|
2012-06-26 23:02:44 +00:00
|
|
|
class Config < Vagrant.plugin("1", :config)
|
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
|
|
|
|
@upload_path = "/tmp/vagrant-shell"
|
|
|
|
end
|
|
|
|
|
2011-12-14 07:45:56 +00:00
|
|
|
def validate(env, errors)
|
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')
|
2012-10-12 00:14:59 +00:00
|
|
|
# Unless you set binmode, on a Windows host the shell script will
|
|
|
|
# have CRLF line endings instead of LF line endings, causing havoc
|
|
|
|
# when the guest executes it. This fixes [GH-1181].
|
|
|
|
file.binmode
|
2011-07-06 06:09:36 +00:00
|
|
|
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
|
2012-01-07 04:03:56 +00:00
|
|
|
command = "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|
|
2012-08-21 23:57:59 +00:00
|
|
|
# Upload the script to the machine
|
|
|
|
env[:machine].communicate.tap do |comm|
|
|
|
|
comm.upload(path.to_s, config.upload_path)
|
2011-01-23 19:52:24 +00:00
|
|
|
|
2012-08-21 23:57:59 +00:00
|
|
|
# Execute it with sudo
|
|
|
|
comm.sudo(command) do |type, data|
|
|
|
|
if [:stderr, :stdout].include?(type)
|
|
|
|
# Output the data with the proper color based on the stream.
|
|
|
|
color = type == :stdout ? :green : :red
|
2011-12-17 01:55:12 +00:00
|
|
|
|
2012-08-21 23:57:59 +00:00
|
|
|
# Note: Be sure to chomp the data to avoid the newlines that the
|
|
|
|
# Chef outputs.
|
|
|
|
env[:ui].info(data.chomp, :color => color, :prefix => false)
|
|
|
|
end
|
2012-01-11 06:03:22 +00:00
|
|
|
end
|
2011-01-23 20:06:09 +00:00
|
|
|
end
|
2011-01-23 19:52:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|