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-11-07 05:21:36 +00:00
|
|
|
class Provisioner < Vagrant.plugin("2", :provisioner)
|
2013-01-13 23:48:52 +00:00
|
|
|
def provision
|
|
|
|
args = ""
|
|
|
|
args = " #{config.args}" if config.args
|
|
|
|
command = "chmod +x #{config.upload_path} && #{config.upload_path}#{args}"
|
2011-01-23 19:52:24 +00:00
|
|
|
|
2013-01-13 23:48:52 +00:00
|
|
|
with_script_file do |path|
|
|
|
|
# Upload the script to the machine
|
|
|
|
@machine.communicate.tap do |comm|
|
2013-04-11 12:05:50 +00:00
|
|
|
# Reset upload path permissions for the current ssh user
|
|
|
|
user = @machine.ssh_info[:username]
|
|
|
|
comm.sudo("chown -R #{user} #{config.upload_path}",
|
|
|
|
:error_check => false)
|
|
|
|
|
2013-01-13 23:48:52 +00:00
|
|
|
comm.upload(path.to_s, config.upload_path)
|
2011-01-23 19:52:24 +00:00
|
|
|
|
2013-04-10 18:27:45 +00:00
|
|
|
if config.path
|
|
|
|
@machine.ui.info(I18n.t("vagrant.provisioners.shell.running",
|
|
|
|
script: path.to_s))
|
|
|
|
else
|
|
|
|
@machine.ui.info(I18n.t("vagrant.provisioners.shell.running",
|
|
|
|
script: "inline script"))
|
|
|
|
end
|
|
|
|
|
2013-01-13 23:48:52 +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-07-06 06:09:36 +00:00
|
|
|
|
2013-01-13 23:48:52 +00:00
|
|
|
# Note: Be sure to chomp the data to avoid the newlines that the
|
|
|
|
# Chef outputs.
|
2013-07-20 03:38:25 +00:00
|
|
|
@machine.env.ui.info(
|
|
|
|
data,
|
|
|
|
:color => color, :new_line => false, :prefix => false)
|
2013-01-13 23:48:52 +00:00
|
|
|
end
|
2011-12-15 03:02:10 +00:00
|
|
|
end
|
2011-01-23 19:52:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-13 23:48:52 +00:00
|
|
|
protected
|
2011-12-15 06:43:45 +00:00
|
|
|
|
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
|
2013-04-23 06:32:13 +00:00
|
|
|
script = nil
|
|
|
|
|
2011-07-06 06:09:36 +00:00
|
|
|
if config.path
|
|
|
|
# Just yield the path to that file...
|
2013-01-13 23:48:52 +00:00
|
|
|
root_path = @machine.env.root_path
|
2013-04-23 06:32:13 +00:00
|
|
|
script = Pathname.new(config.path).expand_path(root_path).read
|
|
|
|
else
|
|
|
|
# The script is just the inline code...
|
|
|
|
script = config.inline
|
2011-07-06 06:09:36 +00:00
|
|
|
end
|
|
|
|
|
2013-04-23 06:32:13 +00:00
|
|
|
# Replace Windows line endings with Unix ones
|
|
|
|
script.gsub!(/\r\n?$/, "\n")
|
|
|
|
|
2011-07-06 06:09:36 +00:00
|
|
|
# Otherwise we have an inline script, we need to Tempfile it,
|
|
|
|
# and handle it specially...
|
|
|
|
file = Tempfile.new('vagrant-shell')
|
2012-10-13 02:51:25 +00:00
|
|
|
|
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
|
2012-10-13 02:51:25 +00:00
|
|
|
|
2011-07-06 06:09:36 +00:00
|
|
|
begin
|
2013-04-23 06:32:13 +00:00
|
|
|
file.write(script)
|
2011-07-06 06:09:36 +00:00
|
|
|
file.fsync
|
2013-02-07 23:23:18 +00:00
|
|
|
file.close
|
2011-07-06 06:09:36 +00:00
|
|
|
yield file.path
|
|
|
|
ensure
|
|
|
|
file.close
|
|
|
|
file.unlink
|
|
|
|
end
|
|
|
|
end
|
2011-01-23 19:52:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|