vagrant-windows 1.3.0.pre.2 → 1.3.0.pre.3
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/vagrant-windows/communication/winrmcommunicator.rb +20 -59
- data/lib/vagrant-windows/communication/winrmfinder.rb +17 -14
- data/lib/vagrant-windows/communication/winrmshell.rb +20 -0
- data/lib/vagrant-windows/communication/winrmshell_factory.rb +34 -0
- data/lib/vagrant-windows/guest/cap/configure_networks.rb +8 -6
- data/lib/vagrant-windows/guest/cap/halt.rb +4 -0
- data/lib/vagrant-windows/guest/windows.rb +3 -1
- data/lib/vagrant-windows/helper.rb +12 -5
- data/lib/vagrant-windows/monkey_patches/plugins/provisioners/chef/provisioner/chef_solo.rb +7 -20
- data/lib/vagrant-windows/monkey_patches/plugins/provisioners/puppet/provisioner/puppet.rb +15 -4
- data/lib/vagrant-windows/monkey_patches/plugins/provisioners/shell/provisioner.rb +6 -6
- data/lib/vagrant-windows/version.rb +1 -1
- data/lib/vagrant-windows/windows_machine.rb +76 -0
- data/spec/vagrant-windows/guestnetwork_spec.rb +1 -1
- data/spec/vagrant-windows/helper_spec.rb +0 -18
- data/spec/vagrant-windows/windows_machine_spec.rb +44 -0
- data/spec/vagrant-windows/winrmcommunicator_spec.rb +21 -2
- data/spec/vagrant-windows/winrmfinder_spec.rb +2 -4
- data/spec/vagrant-windows/winrmshell_spec.rb +1 -1
- metadata +7 -3
@@ -1,37 +1,36 @@
|
|
1
1
|
require 'timeout'
|
2
2
|
require 'log4r'
|
3
|
-
require_relative '
|
3
|
+
require_relative 'winrmshell_factory'
|
4
4
|
require_relative 'winrmshell'
|
5
5
|
require_relative 'winrmfinder'
|
6
|
+
require_relative '../errors'
|
7
|
+
require_relative '../windows_machine'
|
6
8
|
|
7
9
|
module VagrantWindows
|
8
10
|
module Communication
|
9
|
-
# Provides communication
|
11
|
+
# Provides communication channel for Vagrant commands via WinRM.
|
10
12
|
class WinRMCommunicator < Vagrant.plugin("2", :communicator)
|
11
|
-
|
12
|
-
attr_reader :logger
|
13
|
-
attr_reader :machine
|
14
|
-
attr_reader :winrm_finder
|
15
13
|
|
16
14
|
def self.match?(machine)
|
17
|
-
|
15
|
+
VagrantWindows::WindowsMachine.is_windows?(machine)
|
18
16
|
end
|
19
17
|
|
20
18
|
def initialize(machine)
|
21
|
-
@
|
19
|
+
@windows_machine = VagrantWindows::WindowsMachine.new(machine)
|
20
|
+
@winrm_shell_factory = WinRMShellFactory.new(@windows_machine, WinRMFinder.new(@windows_machine))
|
21
|
+
|
22
22
|
@logger = Log4r::Logger.new("vagrant_windows::communication::winrmcommunicator")
|
23
23
|
@logger.debug("initializing WinRMCommunicator")
|
24
|
-
@winrm_finder = WinRMFinder.new(machine)
|
25
24
|
end
|
26
25
|
|
27
26
|
def ready?
|
28
|
-
logger.debug("Checking whether WinRM is ready...")
|
27
|
+
@logger.debug("Checking whether WinRM is ready...")
|
29
28
|
|
30
|
-
Timeout.timeout(@
|
31
|
-
|
29
|
+
Timeout.timeout(@windows_machine.winrm_config.timeout) do
|
30
|
+
winrmshell.powershell("hostname")
|
32
31
|
end
|
33
32
|
|
34
|
-
logger.info("WinRM is ready!")
|
33
|
+
@logger.info("WinRM is ready!")
|
35
34
|
return true
|
36
35
|
|
37
36
|
rescue Vagrant::Errors::VagrantError => e
|
@@ -69,56 +68,30 @@ module VagrantWindows
|
|
69
68
|
|
70
69
|
def upload(from, to)
|
71
70
|
@logger.debug("Uploading: #{from} to #{to}")
|
72
|
-
|
73
|
-
file_name = (session.cmd("echo %TEMP%\\#{file}"))[:data][0][:stdout].chomp
|
74
|
-
session.powershell <<-EOH
|
75
|
-
if(Test-Path #{to})
|
76
|
-
{
|
77
|
-
rm #{to}
|
78
|
-
}
|
79
|
-
EOH
|
80
|
-
Base64.encode64(IO.binread(from)).gsub("\n",'').chars.to_a.each_slice(8000-file_name.size) do |chunk|
|
81
|
-
out = session.cmd("echo #{chunk.join} >> \"#{file_name}\"")
|
82
|
-
end
|
83
|
-
session.powershell("mkdir $([System.IO.Path]::GetDirectoryName(\"#{to}\"))")
|
84
|
-
session.powershell <<-EOH
|
85
|
-
$base64_string = Get-Content \"#{file_name}\"
|
86
|
-
$bytes = [System.Convert]::FromBase64String($base64_string)
|
87
|
-
$new_file = [System.IO.Path]::GetFullPath(\"#{to}\")
|
88
|
-
[System.IO.File]::WriteAllBytes($new_file,$bytes)
|
89
|
-
EOH
|
71
|
+
winrmshell.upload(from, to)
|
90
72
|
end
|
91
73
|
|
92
74
|
def download(from, to=nil)
|
93
75
|
@logger.warn("Downloading: #{from} to #{to} not supported on Windows guests")
|
94
76
|
end
|
95
77
|
|
96
|
-
|
97
|
-
|
98
|
-
# Note: This is not part of the standard Vagrant communicator interface, but
|
99
|
-
# guest capabilities may need to use this.
|
100
|
-
def wql(query)
|
101
|
-
session.wql(query)
|
78
|
+
def winrmshell=(winrmshell)
|
79
|
+
@winrmshell = winrmshell
|
102
80
|
end
|
103
81
|
|
104
|
-
def
|
105
|
-
@
|
82
|
+
def winrmshell
|
83
|
+
@winrmshell ||= @winrm_shell_factory.create_winrm_shell()
|
106
84
|
end
|
107
85
|
|
108
|
-
def session
|
109
|
-
@session ||= new_session
|
110
|
-
end
|
111
|
-
alias_method :winrmshell, :session
|
112
|
-
|
113
86
|
|
114
87
|
protected
|
115
88
|
|
116
89
|
def do_execute(command, shell, &block)
|
117
90
|
if shell.eql? :cmd
|
118
|
-
|
91
|
+
winrmshell.cmd(command, &block)[:exitcode]
|
119
92
|
else
|
120
93
|
command = VagrantWindows.load_script("command_alias.ps1") << "\r\n" << command
|
121
|
-
|
94
|
+
winrmshell.powershell(command, &block)[:exitcode]
|
122
95
|
end
|
123
96
|
end
|
124
97
|
|
@@ -130,18 +103,6 @@ module VagrantWindows
|
|
130
103
|
raise opts[:error_class], error_opts
|
131
104
|
end
|
132
105
|
|
133
|
-
def new_session
|
134
|
-
WinRMShell.new(
|
135
|
-
@winrm_finder.winrm_host_address(),
|
136
|
-
@machine.config.winrm.username,
|
137
|
-
@machine.config.winrm.password,
|
138
|
-
{
|
139
|
-
:port => @winrm_finder.winrm_host_port(),
|
140
|
-
:timeout_in_seconds => @machine.config.winrm.timeout,
|
141
|
-
:max_tries => @machine.config.winrm.max_tries
|
142
|
-
})
|
143
|
-
end
|
144
|
-
|
145
106
|
end #WinRM class
|
146
107
|
end
|
147
|
-
end
|
108
|
+
end
|
@@ -1,46 +1,49 @@
|
|
1
1
|
require 'log4r'
|
2
2
|
require_relative '../errors'
|
3
|
+
require_relative '../windows_machine'
|
3
4
|
|
4
5
|
module VagrantWindows
|
5
6
|
module Communication
|
6
7
|
class WinRMFinder
|
7
8
|
|
8
9
|
attr_reader :logger
|
9
|
-
attr_reader :
|
10
|
+
attr_reader :windows_machine
|
10
11
|
|
11
|
-
def initialize(
|
12
|
-
@
|
12
|
+
def initialize(windows_machine)
|
13
|
+
@windows_machine = windows_machine
|
13
14
|
@logger = Log4r::Logger.new("vagrant_windows::communication::winrmfinder")
|
14
15
|
end
|
15
16
|
|
17
|
+
# Finds the address of the Windows machine.
|
18
|
+
# Raises a Vagrant::Errors::SSHNotReady if WinRM is not responding yet.
|
19
|
+
#
|
20
|
+
# @return [String] The IP of the Windows machine
|
16
21
|
def winrm_host_address
|
17
22
|
# Get the SSH info for the machine, raise an exception if the
|
18
23
|
# provider is saying that the machine is not ready.
|
19
|
-
ssh_info = @
|
24
|
+
ssh_info = @windows_machine.ssh_info
|
20
25
|
raise VagrantWindows::Errors::WinRMNotReady if ssh_info.nil?
|
21
26
|
|
22
27
|
# if the configuration has a host value, that takes precedence
|
23
|
-
host = @
|
28
|
+
host = @windows_machine.winrm_config.host || ssh_info[:host]
|
24
29
|
@logger.info("WinRM host: #{host}")
|
25
30
|
host
|
26
31
|
end
|
27
32
|
|
33
|
+
# Finds the IP port of the Windows machine's WinRM service.
|
34
|
+
#
|
35
|
+
# @return [String] The port of the Windows machine's WinRM service
|
28
36
|
def winrm_host_port
|
29
|
-
expected_guest_port = @
|
37
|
+
expected_guest_port = @windows_machine.winrm_config.guest_port
|
30
38
|
@logger.debug("Searching for WinRM port: #{expected_guest_port.inspect}")
|
31
39
|
|
32
40
|
# Look for the forwarded port only by comparing the guest port
|
33
|
-
|
34
|
-
|
35
|
-
return hostport if guestport == expected_guest_port
|
36
|
-
end
|
37
|
-
rescue NoMethodError => e
|
38
|
-
# VMWare provider doesn't support read_forwarded_ports
|
39
|
-
@logger.debug(e.message)
|
41
|
+
@windows_machine.read_forwarded_ports().each do |_, _, hostport, guestport|
|
42
|
+
return hostport if guestport == expected_guest_port
|
40
43
|
end
|
41
44
|
|
42
45
|
# We tried, give up and use the configured port as-is
|
43
|
-
@
|
46
|
+
@windows_machine.winrm_config.port
|
44
47
|
end
|
45
48
|
|
46
49
|
end
|
@@ -56,6 +56,26 @@ module VagrantWindows
|
|
56
56
|
def wql(query)
|
57
57
|
execute_wql(query)
|
58
58
|
end
|
59
|
+
|
60
|
+
def upload(from, to)
|
61
|
+
@logger.debug("Uploading: #{from} to #{to}")
|
62
|
+
file_name = (cmd("echo %TEMP%\\winrm-upload-#{rand()}"))[:data][0][:stdout].chomp
|
63
|
+
powershell <<-EOH
|
64
|
+
if(Test-Path #{to}) {
|
65
|
+
rm #{to}
|
66
|
+
}
|
67
|
+
EOH
|
68
|
+
Base64.encode64(IO.binread(from)).gsub("\n",'').chars.to_a.each_slice(8000-file_name.size) do |chunk|
|
69
|
+
out = cmd("echo #{chunk.join} >> \"#{file_name}\"")
|
70
|
+
end
|
71
|
+
powershell <<-EOH
|
72
|
+
mkdir $([System.IO.Path]::GetDirectoryName(\"#{to}\"))
|
73
|
+
$base64_string = Get-Content \"#{file_name}\"
|
74
|
+
$bytes = [System.Convert]::FromBase64String($base64_string)
|
75
|
+
$new_file = [System.IO.Path]::GetFullPath(\"#{to}\")
|
76
|
+
[System.IO.File]::WriteAllBytes($new_file,$bytes)
|
77
|
+
EOH
|
78
|
+
end
|
59
79
|
|
60
80
|
protected
|
61
81
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative 'winrmshell'
|
2
|
+
require_relative 'winrmfinder'
|
3
|
+
|
4
|
+
module VagrantWindows
|
5
|
+
module Communication
|
6
|
+
|
7
|
+
# Factory class for generating new WinRMShell instances
|
8
|
+
class WinRMShellFactory
|
9
|
+
|
10
|
+
# @param [WindowsMachine] The Windows machine instance
|
11
|
+
# @param [WinRMFinder] The WinRMFinder instance
|
12
|
+
def initialize(windows_machine, winrm_finder)
|
13
|
+
@windows_machine = windows_machine
|
14
|
+
@winrm_finder = winrm_finder
|
15
|
+
end
|
16
|
+
|
17
|
+
# Creates a new WinRMShell instance
|
18
|
+
#
|
19
|
+
# @return [WinRMShell]
|
20
|
+
def create_winrm_shell()
|
21
|
+
WinRMShell.new(
|
22
|
+
@winrm_finder.winrm_host_address(),
|
23
|
+
@windows_machine.winrm_config.username,
|
24
|
+
@windows_machine.winrm_config.password,
|
25
|
+
{
|
26
|
+
:port => @winrm_finder.winrm_host_port(),
|
27
|
+
:timeout_in_seconds => @windows_machine.winrm_config.timeout,
|
28
|
+
:max_tries => @windows_machine.winrm_config.max_tries
|
29
|
+
})
|
30
|
+
end
|
31
|
+
|
32
|
+
end #WinShell class
|
33
|
+
end
|
34
|
+
end
|
@@ -3,6 +3,7 @@ require_relative '../../communication/guestnetwork'
|
|
3
3
|
require_relative '../../communication/winrmshell'
|
4
4
|
require_relative '../../errors'
|
5
5
|
require_relative '../../helper'
|
6
|
+
require_relative '../../windows_machine'
|
6
7
|
|
7
8
|
module VagrantWindows
|
8
9
|
module Guest
|
@@ -14,9 +15,10 @@ module VagrantWindows
|
|
14
15
|
def self.configure_networks(machine, networks)
|
15
16
|
@@logger.debug("networks: #{networks.inspect}")
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
windows_machine = VagrantWindows::WindowsMachine.new(machine)
|
19
|
+
guest_network = VagrantWindows::Communication::GuestNetwork.new(windows_machine.winrmshell)
|
20
|
+
unless windows_machine.is_vmware?()
|
21
|
+
vm_interface_map = create_vm_interface_map(windows_machine, guest_network)
|
20
22
|
end
|
21
23
|
|
22
24
|
networks.each do |network|
|
@@ -40,13 +42,13 @@ module VagrantWindows
|
|
40
42
|
raise WindowsError, "#{network_type} network type is not supported, try static or dhcp"
|
41
43
|
end
|
42
44
|
end
|
43
|
-
guest_network.set_all_networks_to_work() if
|
45
|
+
guest_network.set_all_networks_to_work() if windows_machine.windows_config.set_work_network
|
44
46
|
end
|
45
47
|
|
46
48
|
#{1=>{:name=>"Local Area Connection", :mac_address=>"0800275FAC5B", :interface_index=>"11", :index=>"7"}}
|
47
|
-
def self.create_vm_interface_map(
|
49
|
+
def self.create_vm_interface_map(windows_machine, guest_network)
|
48
50
|
vm_interface_map = {}
|
49
|
-
driver_mac_address =
|
51
|
+
driver_mac_address = windows_machine.read_mac_addresses.invert
|
50
52
|
@@logger.debug("mac addresses: #{driver_mac_address.inspect}")
|
51
53
|
guest_network.network_adapters().each do |nic|
|
52
54
|
@@logger.debug("nic: #{nic.inspect}")
|
@@ -3,6 +3,10 @@ module VagrantWindows
|
|
3
3
|
module Cap
|
4
4
|
class Halt
|
5
5
|
def self.halt(machine)
|
6
|
+
# Fix defect 129, if there's an existing scheduled reboot cancel it so shutdown succeeds
|
7
|
+
machine.communicate.execute("shutdown -a", :error_check => false)
|
8
|
+
|
9
|
+
# Force shutdown the machine now
|
6
10
|
machine.communicate.execute("shutdown /s /t 1 /c \"Vagrant Halt\" /f /d p:4:1")
|
7
11
|
|
8
12
|
# Wait until the VM's state is actually powered off. If this doesn't
|
@@ -12,11 +12,13 @@ module VagrantWindows
|
|
12
12
|
# Vagrant 1.1.x compatibility methods
|
13
13
|
# Implement the 1.1.x methods and call through to the new 1.2.x capabilities
|
14
14
|
|
15
|
+
attr_reader :windows_machine
|
15
16
|
attr_reader :machine
|
16
17
|
|
17
18
|
def initialize(machine = nil)
|
18
19
|
super(machine) unless machine == nil
|
19
20
|
@machine = machine
|
21
|
+
@windows_machine = ::VagrantWindows::WindowsMachine.new(machine)
|
20
22
|
end
|
21
23
|
|
22
24
|
def change_host_name(name)
|
@@ -32,7 +34,7 @@ module VagrantWindows
|
|
32
34
|
end
|
33
35
|
|
34
36
|
def mount_shared_folder(name, guestpath, options)
|
35
|
-
if
|
37
|
+
if @windows_machine.is_vmware?() then
|
36
38
|
VagrantWindows::Guest::Cap::MountSharedFolder.mount_vmware_shared_folder(
|
37
39
|
@machine, name, guestpath, options)
|
38
40
|
else
|
@@ -2,6 +2,8 @@ module VagrantWindows
|
|
2
2
|
module Helper
|
3
3
|
extend self
|
4
4
|
|
5
|
+
@@logger = Log4r::Logger.new("vagrant_windows::helper")
|
6
|
+
|
5
7
|
# Makes a path Windows guest friendly.
|
6
8
|
# Turns '/vagrant' into 'c:\vagrant'
|
7
9
|
#
|
@@ -22,12 +24,17 @@ module VagrantWindows
|
|
22
24
|
return shared_folder_name.gsub(/[\/\/]/,'_').sub(/^_/, '')
|
23
25
|
end
|
24
26
|
|
25
|
-
#
|
27
|
+
# Check to see if the guest is rebooting, if its rebooting then wait until its ready
|
26
28
|
#
|
27
|
-
# @
|
28
|
-
|
29
|
-
|
29
|
+
# @param [WindowsMachine] The windows machine instance
|
30
|
+
# @param [Int] The time in seconds to wait between checks
|
31
|
+
def wait_if_rebooting(windows_machine, wait_in_seconds=10)
|
32
|
+
@@logger.info('Checking guest reboot status')
|
33
|
+
while windows_machine.is_rebooting?
|
34
|
+
@@logger.debug('Guest is rebooting, waiting 10 seconds...')
|
35
|
+
sleep(wait_in_seconds)
|
36
|
+
end
|
30
37
|
end
|
31
|
-
|
38
|
+
|
32
39
|
end
|
33
40
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'tempfile'
|
2
2
|
require "#{Vagrant::source_root}/plugins/provisioners/chef/provisioner/chef_solo"
|
3
3
|
require_relative '../../../../../helper'
|
4
|
+
require_relative '../../../../../windows_machine'
|
4
5
|
|
5
6
|
module VagrantPlugins
|
6
7
|
module Chef
|
@@ -14,25 +15,16 @@ module VagrantPlugins
|
|
14
15
|
|
15
16
|
# This patch is needed until Vagrant supports chef on Windows guests
|
16
17
|
define_method(:run_chef_solo) do
|
17
|
-
is_windows ? run_chef_solo_on_windows() : run_chef_solo_on_linux.bind(self).()
|
18
|
+
is_windows? ? run_chef_solo_on_windows() : run_chef_solo_on_linux.bind(self).()
|
18
19
|
end
|
19
20
|
|
20
21
|
define_method(:provision) do
|
21
|
-
|
22
|
+
windows_machine = VagrantWindows::WindowsMachine.new(@machine)
|
23
|
+
wait_if_rebooting(windows_machine) if is_windows?
|
22
24
|
provision_on_linux.bind(self).()
|
23
25
|
end
|
24
26
|
|
25
|
-
def
|
26
|
-
# Check to see if the guest is rebooting, if its rebooting then wait until its ready
|
27
|
-
@logger.info('Checking guest reboot status')
|
28
|
-
|
29
|
-
while is_rebooting?(machine)
|
30
|
-
@logger.debug('Guest is rebooting, waiting 10 seconds...')
|
31
|
-
sleep(10)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def run_chef_solo_on_windows
|
27
|
+
def run_chef_solo_on_windows
|
36
28
|
# create cheftaskrun.ps1 that the scheduled task will invoke when run
|
37
29
|
render_file_and_upload("cheftaskrun.ps1", chef_script_options[:chef_task_run_ps1], :options => chef_script_options)
|
38
30
|
|
@@ -116,13 +108,8 @@ module VagrantPlugins
|
|
116
108
|
@chef_script_options
|
117
109
|
end
|
118
110
|
|
119
|
-
def
|
120
|
-
|
121
|
-
@machine.communicate.execute(reboot_detect_script, :error_check => false) != 0
|
122
|
-
end
|
123
|
-
|
124
|
-
def is_windows
|
125
|
-
@machine.config.vm.guest.eql? :windows
|
111
|
+
def is_windows?
|
112
|
+
VagrantWindows::WindowsMachine.is_windows?(@machine)
|
126
113
|
end
|
127
114
|
|
128
115
|
end # ChefSolo class
|
@@ -1,20 +1,31 @@
|
|
1
1
|
require "#{Vagrant::source_root}/plugins/provisioners/puppet/provisioner/puppet"
|
2
|
+
require_relative '../../../../../windows_machine'
|
3
|
+
require_relative '../../../../../helper'
|
2
4
|
|
3
5
|
module VagrantPlugins
|
4
6
|
module Puppet
|
5
7
|
module Provisioner
|
6
8
|
class Puppet < Vagrant.plugin("2", :provisioner)
|
9
|
+
|
10
|
+
include VagrantWindows::Helper
|
7
11
|
|
8
12
|
# This patch is needed until Vagrant supports Puppet on Windows guests
|
13
|
+
provision_on_linux = instance_method(:provision)
|
9
14
|
run_puppet_apply_on_linux = instance_method(:run_puppet_apply)
|
10
15
|
configure_on_linux = instance_method(:configure)
|
11
16
|
|
12
17
|
define_method(:run_puppet_apply) do
|
13
|
-
is_windows ? run_puppet_apply_on_windows() : run_puppet_apply_on_linux.bind(self).()
|
18
|
+
is_windows? ? run_puppet_apply_on_windows() : run_puppet_apply_on_linux.bind(self).()
|
14
19
|
end
|
15
20
|
|
16
21
|
define_method(:configure) do |root_config|
|
17
|
-
is_windows ? configure_on_windows(root_config) : configure_on_linux.bind(self).(root_config)
|
22
|
+
is_windows? ? configure_on_windows(root_config) : configure_on_linux.bind(self).(root_config)
|
23
|
+
end
|
24
|
+
|
25
|
+
define_method(:provision) do
|
26
|
+
windows_machine = VagrantWindows::WindowsMachine.new(@machine)
|
27
|
+
wait_if_rebooting(windows_machine) if is_windows?
|
28
|
+
provision_on_linux.bind(self).()
|
18
29
|
end
|
19
30
|
|
20
31
|
def run_puppet_apply_on_windows
|
@@ -108,8 +119,8 @@ module VagrantPlugins
|
|
108
119
|
end
|
109
120
|
end
|
110
121
|
|
111
|
-
def is_windows
|
112
|
-
@machine
|
122
|
+
def is_windows?
|
123
|
+
VagrantWindows::WindowsMachine.is_windows?(@machine)
|
113
124
|
end
|
114
125
|
|
115
126
|
end # Puppet class
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require "#{Vagrant::source_root}/plugins/provisioners/shell/provisioner"
|
2
2
|
require_relative '../../../../helper'
|
3
|
+
require_relative '../../../../windows_machine'
|
3
4
|
|
4
5
|
module VagrantPlugins
|
5
6
|
module Shell
|
@@ -11,13 +12,16 @@ module VagrantPlugins
|
|
11
12
|
provision_on_linux = instance_method(:provision)
|
12
13
|
|
13
14
|
define_method(:provision) do
|
14
|
-
is_windows ? provision_on_windows() : provision_on_linux.bind(self).()
|
15
|
+
VagrantWindows::WindowsMachine.is_windows?(@machine) ? provision_on_windows() : provision_on_linux.bind(self).()
|
15
16
|
end
|
16
|
-
|
17
|
+
|
17
18
|
def provision_on_windows
|
18
19
|
args = ""
|
19
20
|
args = " #{config.args}" if config.args
|
20
21
|
|
22
|
+
windows_machine = VagrantWindows::WindowsMachine.new(@machine)
|
23
|
+
wait_if_rebooting(windows_machine)
|
24
|
+
|
21
25
|
with_script_file do |path|
|
22
26
|
# Upload the script to the machine
|
23
27
|
@machine.communicate.tap do |comm|
|
@@ -80,10 +84,6 @@ module VagrantPlugins
|
|
80
84
|
end
|
81
85
|
end
|
82
86
|
|
83
|
-
def is_windows
|
84
|
-
@machine.config.vm.guest.eql? :windows
|
85
|
-
end
|
86
|
-
|
87
87
|
end # Provisioner class
|
88
88
|
end
|
89
89
|
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module VagrantWindows
|
2
|
+
|
3
|
+
# Provides a wrapper around the Vagrant machine object
|
4
|
+
class WindowsMachine
|
5
|
+
|
6
|
+
attr_reader :machine
|
7
|
+
|
8
|
+
# Returns true if the specifed Vagrant machine is a Windows guest, otherwise false.
|
9
|
+
#
|
10
|
+
# @param [Machine] The Vagrant machine object
|
11
|
+
# @return [Boolean]
|
12
|
+
def self.is_windows?(machine)
|
13
|
+
machine.config.vm.guest.eql? :windows
|
14
|
+
end
|
15
|
+
|
16
|
+
# @param [Machine] The Vagrant machine object
|
17
|
+
def initialize(machine)
|
18
|
+
@machine = machine
|
19
|
+
@logger = Log4r::Logger.new("vagrant_windows::windows_machine")
|
20
|
+
end
|
21
|
+
|
22
|
+
# Checks to see if the machine is using VMWare Fusion or Workstation.
|
23
|
+
#
|
24
|
+
# @return [Boolean]
|
25
|
+
def is_vmware?()
|
26
|
+
@machine.provider_name.to_s().start_with?('vmware')
|
27
|
+
end
|
28
|
+
|
29
|
+
# Checks to see if the machine is rebooting or has a scheduled reboot.
|
30
|
+
#
|
31
|
+
# @return [Boolean] True if rebooting
|
32
|
+
def is_rebooting?()
|
33
|
+
reboot_detect_script = VagrantWindows.load_script('reboot_detect.ps1')
|
34
|
+
@machine.communicate.execute(reboot_detect_script, :error_check => false) != 0
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns the active WinRMShell for the guest.
|
38
|
+
#
|
39
|
+
# @return [WinRMShell]
|
40
|
+
def winrmshell()
|
41
|
+
@machine.communicate.winrmshell
|
42
|
+
end
|
43
|
+
|
44
|
+
# Reads the machine's MAC addresses keyed by interface index.
|
45
|
+
# {1=>"0800273FAC5A", 2=>"08002757E68A"}
|
46
|
+
#
|
47
|
+
# @return [Hash]
|
48
|
+
def read_mac_addresses()
|
49
|
+
@machine.provider.driver.read_mac_addresses
|
50
|
+
end
|
51
|
+
|
52
|
+
# Returns a list of forwarded ports for a VM.
|
53
|
+
# NOTE: For VMWare this is currently unsupported.
|
54
|
+
#
|
55
|
+
# @return [Array<Array>]
|
56
|
+
def read_forwarded_ports()
|
57
|
+
is_vmware?() ? [] : @machine.provider.driver.read_forwarded_ports
|
58
|
+
end
|
59
|
+
|
60
|
+
# Returns the SSH config for this machine.
|
61
|
+
#
|
62
|
+
# @return [Hash]
|
63
|
+
def ssh_info()
|
64
|
+
@machine.ssh_info
|
65
|
+
end
|
66
|
+
|
67
|
+
def windows_config()
|
68
|
+
@machine.config.windows
|
69
|
+
end
|
70
|
+
|
71
|
+
def winrm_config()
|
72
|
+
@machine.config.winrm
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
@@ -5,7 +5,7 @@ describe VagrantWindows::Communication::GuestNetwork , :integration => true do
|
|
5
5
|
before(:all) do
|
6
6
|
# This test requires you already have a running Windows Server 2008 R2 Vagrant VM
|
7
7
|
# Not ideal, but you have to start somewhere
|
8
|
-
@shell = VagrantWindows::Communication::WinRMShell.new("
|
8
|
+
@shell = VagrantWindows::Communication::WinRMShell.new("127.0.0.1", "vagrant", "vagrant")
|
9
9
|
@guestnetwork = VagrantWindows::Communication::GuestNetwork.new(@shell)
|
10
10
|
end
|
11
11
|
|
@@ -35,23 +35,5 @@ describe VagrantWindows::Helper , :unit => true do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
end
|
38
|
-
|
39
|
-
describe "is_vmware" do
|
40
|
-
it "should be true for vmware_fusion" do
|
41
|
-
machine = stub(:provider_name => :vmware_fusion)
|
42
|
-
expect(@dummy.is_vmware(machine)).to be_true
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should be true for vmware_workstation" do
|
46
|
-
machine = stub(:provider_name => :vmware_workstation)
|
47
|
-
expect(@dummy.is_vmware(machine)).to be_true
|
48
|
-
end
|
49
|
-
|
50
|
-
it "should be false for virtual_box" do
|
51
|
-
machine = stub(:provider_name => :virtual_box)
|
52
|
-
expect(@dummy.is_vmware(machine)).to be_false
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
38
|
|
57
39
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'mocha/api'
|
3
|
+
require 'vagrant-windows/windows_machine'
|
4
|
+
|
5
|
+
describe VagrantWindows::WindowsMachine , :unit => true do
|
6
|
+
|
7
|
+
describe "is_vmware?" do
|
8
|
+
it "should be true for vmware_fusion" do
|
9
|
+
machine = stub(:provider_name => :vmware_fusion)
|
10
|
+
windows_machine = VagrantWindows::WindowsMachine.new(machine)
|
11
|
+
expect(windows_machine.is_vmware?()).to be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should be true for vmware_workstation" do
|
15
|
+
machine = stub(:provider_name => :vmware_workstation)
|
16
|
+
windows_machine = VagrantWindows::WindowsMachine.new(machine)
|
17
|
+
expect(windows_machine.is_vmware?()).to be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be false for virtual_box" do
|
21
|
+
machine = stub(:provider_name => :virtual_box)
|
22
|
+
windows_machine = VagrantWindows::WindowsMachine.new(machine)
|
23
|
+
expect(windows_machine.is_vmware?()).to be_false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "is_windows?" do
|
28
|
+
it "should return true when config vm guest is windows" do
|
29
|
+
vm = stub(:guest => :windows)
|
30
|
+
config = stub(:vm => vm)
|
31
|
+
machine = stub(:config => config)
|
32
|
+
expect(VagrantWindows::WindowsMachine.is_windows?(machine)).to be_true
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return false when config vm guest is not windows" do
|
36
|
+
vm = stub(:guest => :ubuntu)
|
37
|
+
config = stub(:vm => vm)
|
38
|
+
machine = stub(:config => config)
|
39
|
+
expect(VagrantWindows::WindowsMachine.is_windows?(machine)).to be_false
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -5,9 +5,8 @@ describe VagrantWindows::Communication::WinRMCommunicator, :integration => true
|
|
5
5
|
before(:all) do
|
6
6
|
# This test requires you already have a running Windows Server 2008 R2 Vagrant VM
|
7
7
|
# Not ideal, but you have to start somewhere
|
8
|
-
@shell = VagrantWindows::Communication::WinRMShell.new("localhost", "vagrant", "vagrant")
|
9
8
|
@communicator = VagrantWindows::Communication::WinRMCommunicator.new({})
|
10
|
-
@communicator.
|
9
|
+
@communicator.winrmshell = VagrantWindows::Communication::WinRMShell.new("127.0.0.1", "vagrant", "vagrant")
|
11
10
|
end
|
12
11
|
|
13
12
|
describe "execute" do
|
@@ -24,5 +23,25 @@ describe VagrantWindows::Communication::WinRMCommunicator, :integration => true
|
|
24
23
|
expect { @communicator.execute("exit 1", opts) }.to raise_error(VagrantWindows::Errors::WinRMInvalidShell)
|
25
24
|
end
|
26
25
|
end
|
26
|
+
|
27
|
+
describe "upload" do
|
28
|
+
it "should upload the file and overwrite it if it exists" do
|
29
|
+
test_file = Tempfile.new("uploadtest")
|
30
|
+
IO.write(test_file, "hello world")
|
31
|
+
@communicator.upload(test_file, "c:\\vagrantuploadtest.txt")
|
32
|
+
|
33
|
+
# ensure we can overwrite
|
34
|
+
IO.write(test_file, "goodbye cruel world")
|
35
|
+
@communicator.upload(test_file, "c:\\vagrantuploadtest.txt")
|
36
|
+
|
37
|
+
# get the uploaded file's contents to ensure it uploaded properly
|
38
|
+
uploaded_file_content = ''
|
39
|
+
@communicator.execute("cat c:\\vagrantuploadtest.txt", {}) do |type, line|
|
40
|
+
uploaded_file_content = uploaded_file_content + line
|
41
|
+
end
|
42
|
+
|
43
|
+
expect(uploaded_file_content.chomp).to eq("goodbye cruel world")
|
44
|
+
end
|
45
|
+
end
|
27
46
|
|
28
47
|
end
|
@@ -17,8 +17,7 @@ describe VagrantWindows::Communication::WinRMFinder, :unit => true do
|
|
17
17
|
# setup the winrm config to return nil for the host (i.e. the default)
|
18
18
|
winrm_config = VagrantWindows::Config::WinRM.new()
|
19
19
|
winrm_config.finalize!()
|
20
|
-
|
21
|
-
@machine.stubs(:config).returns(machine_config)
|
20
|
+
@machine.stubs(:winrm_config).returns(winrm_config)
|
22
21
|
|
23
22
|
# setup the machine ssh_info to return a 10.0.0.1
|
24
23
|
@machine.stubs(:ssh_info).returns({ :host => '10.0.0.1' })
|
@@ -31,8 +30,7 @@ describe VagrantWindows::Communication::WinRMFinder, :unit => true do
|
|
31
30
|
winrm_config = VagrantWindows::Config::WinRM.new()
|
32
31
|
winrm_config.host = '10.0.0.1'
|
33
32
|
winrm_config.finalize!()
|
34
|
-
|
35
|
-
@machine.stubs(:config).returns(machine_config)
|
33
|
+
@machine.stubs(:winrm_config).returns(winrm_config)
|
36
34
|
|
37
35
|
# setup the machine ssh_info to return a 10.0.0.1
|
38
36
|
@machine.stubs(:ssh_info).returns({ :host => '127.0.0.1' })
|
@@ -5,7 +5,7 @@ describe VagrantWindows::Communication::WinRMShell, :integration => true do
|
|
5
5
|
before(:all) do
|
6
6
|
# This test requires you already have a running Windows Server 2008 R2 Vagrant VM
|
7
7
|
# Not ideal, but you have to start somewhere
|
8
|
-
@shell = VagrantWindows::Communication::WinRMShell.new("
|
8
|
+
@shell = VagrantWindows::Communication::WinRMShell.new("127.0.0.1", "vagrant", "vagrant")
|
9
9
|
end
|
10
10
|
|
11
11
|
describe "powershell" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vagrant-windows
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.0.pre.
|
4
|
+
version: 1.3.0.pre.3
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-11-
|
13
|
+
date: 2013-11-04 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: winrm
|
@@ -163,6 +163,7 @@ files:
|
|
163
163
|
- lib/vagrant-windows/communication/winrmcommunicator.rb
|
164
164
|
- lib/vagrant-windows/communication/winrmfinder.rb
|
165
165
|
- lib/vagrant-windows/communication/winrmshell.rb
|
166
|
+
- lib/vagrant-windows/communication/winrmshell_factory.rb
|
166
167
|
- lib/vagrant-windows/config/windows.rb
|
167
168
|
- lib/vagrant-windows/config/winrm.rb
|
168
169
|
- lib/vagrant-windows/errors.rb
|
@@ -190,6 +191,7 @@ files:
|
|
190
191
|
- lib/vagrant-windows/scripts/set_work_network.ps1
|
191
192
|
- lib/vagrant-windows/scripts/winrs_v3_get_adapters.ps1
|
192
193
|
- lib/vagrant-windows/version.rb
|
194
|
+
- lib/vagrant-windows/windows_machine.rb
|
193
195
|
- lib/vagrant-windows.rb
|
194
196
|
- LICENSE
|
195
197
|
- locales/en.yml
|
@@ -200,6 +202,7 @@ files:
|
|
200
202
|
- spec/vagrant-windows/helper_spec.rb
|
201
203
|
- spec/vagrant-windows/mount_shared_folder_spec.rb
|
202
204
|
- spec/vagrant-windows/windows_config_spec.rb
|
205
|
+
- spec/vagrant-windows/windows_machine_spec.rb
|
203
206
|
- spec/vagrant-windows/winrm_config_spec.rb
|
204
207
|
- spec/vagrant-windows/winrmcommunicator_spec.rb
|
205
208
|
- spec/vagrant-windows/winrmfinder_spec.rb
|
@@ -225,7 +228,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
225
228
|
version: '0'
|
226
229
|
segments:
|
227
230
|
- 0
|
228
|
-
hash: -
|
231
|
+
hash: -432043001542177050
|
229
232
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
230
233
|
none: false
|
231
234
|
requirements:
|
@@ -245,6 +248,7 @@ test_files:
|
|
245
248
|
- spec/vagrant-windows/helper_spec.rb
|
246
249
|
- spec/vagrant-windows/mount_shared_folder_spec.rb
|
247
250
|
- spec/vagrant-windows/windows_config_spec.rb
|
251
|
+
- spec/vagrant-windows/windows_machine_spec.rb
|
248
252
|
- spec/vagrant-windows/winrm_config_spec.rb
|
249
253
|
- spec/vagrant-windows/winrmcommunicator_spec.rb
|
250
254
|
- spec/vagrant-windows/winrmfinder_spec.rb
|