From cde5d0bafb23fe8889362cb8c4c18bb2e0232808 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 11 Apr 2014 21:38:26 -0700 Subject: [PATCH] privisioners/shell: execute windows script --- plugins/provisioners/shell/provisioner.rb | 65 ++++++++++++++++++----- 1 file changed, 51 insertions(+), 14 deletions(-) diff --git a/plugins/provisioners/shell/provisioner.rb b/plugins/provisioners/shell/provisioner.rb index 150f43df8..de0161e28 100644 --- a/plugins/provisioners/shell/provisioner.rb +++ b/plugins/provisioners/shell/provisioner.rb @@ -7,18 +7,6 @@ module VagrantPlugins module Shell class Provisioner < Vagrant.plugin("2", :provisioner) def provision - if @config.vm.communicator == :winrm - provision_winrm - else - provision_ssh - end - end - - protected - - # This is the provision method called if SSH is what is running - # on the remote end, which assumes a POSIX-style host. - def provision_ssh args = "" if config.args.is_a?(String) args = " #{config.args.to_s}" @@ -27,6 +15,18 @@ module VagrantPlugins args = " #{args.join(" ")}" end + if @config.vm.communicator == :winrm + provision_winrm(args) + else + provision_ssh(args) + end + end + + protected + + # This is the provision method called if SSH is what is running + # on the remote end, which assumes a POSIX-style host. + def provision_ssh(args) command = "chmod +x #{config.upload_path} && #{config.upload_path}#{args}" with_script_file do |path| @@ -68,8 +68,45 @@ module VagrantPlugins # This provisions using WinRM, which assumes a PowerShell # console on the other side. - def provision_winrm - # TODO + def provision_winrm(args) + # TODO: Wait for rebooting + + with_script_file do |path| + @machine.communicate.tap do |comm| + # Make sure that the upload path has an extension, since + # having an extension is critical for Windows execution + upload_path = config.upload_path.to_s + if File.extname(upload_path) == "" + upload_path += File.extname(path.to_s) + end + + # Upload it + comm.upload(path.to_s, upload_path) + + # Calculate the path that we'll be executing + exec_path = upload_path + exec_path.gsub!('/', '\\') + exec_path = "c:#{exec_path}" if exec_path.start_with?("\\") + + command = <<-EOH + $old = Get-ExecutionPolicy; + Set-ExecutionPolicy Unrestricted -force; + #{exec_path}#{args}; + Set-ExecutionPolicy $old -force + EOH + + # 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 + + @machine.ui.info( + data, + :color => color, :new_line => false, :prefix => false) + end + end + end end # Quote and escape strings for shell execution, thanks to Capistrano.