vagrant-windows-domain 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32c0962897914ab1f2665b2ee83e436403d1c7d6
4
- data.tar.gz: 6b9f71576ad54801a1325ed828d8a04b614e186b
3
+ metadata.gz: 7a229ad007dcbe70866e50934f61fa5f64f43604
4
+ data.tar.gz: 9f8d84d07974fd3477df0668f6e33f917210c53f
5
5
  SHA512:
6
- metadata.gz: a2a5bba2574096eb6dd50802d44f87b1a20e94caee7f9191dfa686cef067c16b86ca4953c97d40e3203373eed09d5eaec89d28b77a4df6ca6a5f2af473dec6b7
7
- data.tar.gz: a15ef170cca8c7b9d8424d1e35d71d8447c3e49567fe9b74c36b6e69e31a99e7146f72dbcd9ac99679efc327a5dc19a824d4ec700e72f3c5ed6f24ef8d66f668
6
+ metadata.gz: 855602d71f7b086551907ebb97a4a4ea48ccea97838983a9c76cc8956cad4c76fbb6ce783c51cbbc629ea66275f7ae3b960615a50f29e2069af9eee3f1a36531
7
+ data.tar.gz: 9b694ca5053521836793f9a888ca25077a5c3f42c054c763a18ce929def348cbe9518cebda8f613f4292512b5f8acc4068acb80b60a32eeb0ebc67dabe52fa70
data/README.md CHANGED
@@ -30,7 +30,7 @@ config.vm.provision :windows_domain do |domain|
30
30
  #
31
31
  # Uses the Rename-Computer PowerShell command. ORRRR -NewName flag??
32
32
  # Specifies a new name for the computer in the new domain.
33
- domain.computer_name "myfandangledname"
33
+ domain.computer_name = "myfandangledname"
34
34
 
35
35
  # The Username to use when authenticating against the Domain.
36
36
  #
@@ -50,7 +50,7 @@ config.vm.provision :windows_domain do |domain|
50
50
  # The set of Advanced options to pass when joining the Domain.
51
51
  #
52
52
  # See (https://technet.microsoft.com/en-us/library/hh849798.aspx) for detail, these are generally not required.
53
- domain.join_options = { "--JoinReadOnly" => "" }
53
+ domain.join_options = [ "JoinReadOnly" ]
54
54
 
55
55
  # Organisational Unit path in AD.
56
56
  #
@@ -9,8 +9,8 @@ SCRIPT
9
9
  VAGRANTFILE_API_VERSION = "2"
10
10
 
11
11
  Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
12
-
13
- config.vm.box = "mfellows/windows2012r2"
12
+
13
+ config.vm.box = "windows2012r2"
14
14
  config.vm.guest = :windows
15
15
  config.vm.communicator = "winrm"
16
16
  config.vm.network :forwarded_port, guest: 5985, host: 5985, id: "winrm", auto_correct: true
@@ -27,7 +27,7 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
27
27
  #
28
28
  # Uses the Rename-Computer PowerShell command. ORRRR -NewName flag??
29
29
  # Specifies a new name for the computer in the new domain.
30
- domain.computer_name "myfandangledname"
30
+ domain.computer_name = "myfandangledname"
31
31
 
32
32
  # The Username to use when authenticating against the Domain.
33
33
  #
@@ -40,23 +40,23 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
40
40
  # join the computers to a new domain.
41
41
  domain.password = "iprobablyshouldntusethisfield"
42
42
 
43
- # The set of Advanced options to pass when joining the Domain.
43
+ # An array of advanced options to pass when joining the Domain.
44
44
  #
45
45
  # See (https://technet.microsoft.com/en-us/library/hh849798.aspx) for detail.
46
46
  # NOTE: If we user :computer_name from above this needs to be merged!!
47
- domain.join_options
47
+ # domain.join_options = ['Win9xUpgrade']
48
48
 
49
49
  # Organisational Unit path in AD.
50
50
  #
51
51
  # Specifies an organizational unit (OU) for the domain account.
52
52
  # Enter the full distinguished name of the OU in quotation marks.
53
53
  # The default value is the default OU for machine objects in the domain.
54
- domain.ou_path
54
+ # domain.ou_path = nil
55
55
 
56
56
  # Performs an unsecure join to the specified domain.
57
57
  #
58
58
  # When this option is used username/password are not required
59
- domain.unsecure
59
+ domain.unsecure = false
60
60
  end
61
61
 
62
62
  # Confirm that this will run after the reload from the domain provisioner!
@@ -13,9 +13,7 @@ module VagrantPlugins
13
13
  attr_accessor :domain
14
14
 
15
15
  # The new Computer Name to use when joining the domain.
16
- #
17
- # Uses the Rename-Computer PowerShell command. ORRRR -NewName flag??
18
- # Specifies a new name for the computer in the new domain.
16
+ # Specifies a new name for the computer in the new domain. Uses the -NameName Option.
19
17
  attr_accessor :computer_name
20
18
 
21
19
  # The Username to use when authenticating against the Domain.
@@ -47,12 +45,6 @@ module VagrantPlugins
47
45
  # When this option is used username/password are not required
48
46
  attr_accessor :unsecure
49
47
 
50
- # The current Computer Name.
51
- #
52
- # Used to determine whether or not we need to rename the computer
53
- # on join. This parameter should not be manually set.
54
- attr_accessor :old_computer_name
55
-
56
48
  def initialize
57
49
  super
58
50
  @domain = UNSET_VALUE
@@ -77,7 +69,7 @@ module VagrantPlugins
77
69
  @computer_name = nil if @computer_name == UNSET_VALUE || @computer_name == ""
78
70
  @username = nil if @username == UNSET_VALUE || @username == ""
79
71
  @password = nil if @password == UNSET_VALUE || @password == ""
80
- @join_options = {} if @join_options == UNSET_VALUE
72
+ @join_options = [] if @join_options == UNSET_VALUE
81
73
  @ou_path = nil if @ou_path == UNSET_VALUE
82
74
  @unsecure = false if @unsecure == UNSET_VALUE
83
75
  end
@@ -88,9 +80,7 @@ module VagrantPlugins
88
80
  #
89
81
  # @param [Machine] The current {Machine}
90
82
  # @return [Hash] Any errors or {} if no errors found
91
- def validate(machine)
92
- @old_computer_name = get_guest_computer_name(machine)
93
-
83
+ def validate(machine)
94
84
  errors = _detected_errors
95
85
 
96
86
  # Need to supply one of them!
@@ -99,23 +89,7 @@ module VagrantPlugins
99
89
  end
100
90
 
101
91
  { "windows domain provisioner" => errors }
102
- end
103
-
104
- # Gets the Computer Name from the guest machine
105
- def get_guest_computer_name(machine)
106
- computerName = ""
107
- machine.communicate.shell.powershell("$env:COMPUTERNAME") do |type, data|
108
- if !data.chomp.empty?
109
- if [:stderr, :stdout].include?(type)
110
- color = type == :stdout ? :green : :red
111
- computerName = data.chomp
112
- @logger.info("Detected guest computer name: #{computerName}")
113
- end
114
- end
115
- end
116
-
117
- computerName
118
- end
92
+ end
119
93
  end
120
94
  end
121
95
  end
@@ -19,6 +19,14 @@ module VagrantPlugins
19
19
 
20
20
  # Default path for storing the transient script runner
21
21
  WINDOWS_DOMAIN_GUEST_RUNNER_PATH = "c:/tmp/vagrant-windows-domain-runner.ps1"
22
+
23
+ attr_accessor :restart_sleep_duration
24
+
25
+ # The current Computer Name.
26
+ #
27
+ # Used to determine whether or not we need to rename the computer
28
+ # on join. This parameter should not be manually set.
29
+ attr_accessor :old_computer_name
22
30
 
23
31
  # Constructs the Provisioner Plugin.
24
32
  #
@@ -29,6 +37,7 @@ module VagrantPlugins
29
37
  super
30
38
 
31
39
  @logger = Log4r::Logger.new("vagrant::provisioners::vagrant_windows_domain")
40
+ @restart_sleep_duration = 10
32
41
  end
33
42
 
34
43
  # Configures the Provisioner.
@@ -42,13 +51,19 @@ module VagrantPlugins
42
51
 
43
52
  # Run the Provisioner!
44
53
  def provision
54
+ @old_computer_name = get_guest_computer_name(machine)
55
+
45
56
  @machine.env.ui.say(:info, "Connecting guest machine to domain '#{config.domain}' with computer name '#{config.computer_name}'")
46
57
 
47
58
  set_credentials
48
59
 
49
- join_domain
60
+ result = join_domain
50
61
 
51
- restart_guest
62
+ remove_command_runner_script
63
+
64
+ if result
65
+ restart_guest
66
+ end
52
67
  end
53
68
 
54
69
  # Join the guest machine to a Windows Domain.
@@ -90,7 +105,6 @@ module VagrantPlugins
90
105
  def cleanup
91
106
  set_credentials
92
107
  leave_domain
93
-
94
108
  end
95
109
 
96
110
  # Restarts the Computer and waits
@@ -100,7 +114,7 @@ module VagrantPlugins
100
114
  options[:provision_ignore_sentinel] = false
101
115
  @machine.action(:reload, options)
102
116
  begin
103
- sleep 10
117
+ sleep @restart_sleep_duration
104
118
  end until @machine.communicate.ready?
105
119
  end
106
120
 
@@ -133,11 +147,43 @@ module VagrantPlugins
133
147
  username: @config.username,
134
148
  password: @config.password,
135
149
  domain: @config.domain,
136
- add_to_domain: add_to_domain
137
- # parameters: @config.join_options.map { |k,v| "#{k}" + (!v.nil? ? " \"#{v}\"": '') }.join(" ")
150
+ add_to_domain: add_to_domain,
151
+ unsecure: @config.unsecure,
152
+ parameters: generate_command_arguments(add_to_domain)
138
153
  })
139
154
  end
140
155
 
156
+ # Generates the argument list
157
+ def generate_command_arguments(add_to_domain=true)
158
+ params = {"-DomainName" => @config.domain }
159
+
160
+ if add_to_domain
161
+
162
+ if @config.unsecure
163
+ params["-Unsecure"] = nil
164
+ else
165
+ params["-Credential $credentials"] = nil
166
+ end
167
+
168
+ if @config.computer_name != nil && @config.computer_name != @old_computer_name
169
+ params["-NewName"] = "'#{@config.computer_name}'"
170
+ end
171
+
172
+ if @config.ou_path
173
+ params["-OUPath"] = "'#{@config.ou_path}'"
174
+ end
175
+ else
176
+ if !@config.unsecure
177
+ params["-UnjoinDomainCredential $credentials"] = nil
178
+ end
179
+ end
180
+
181
+ # Remove with unsecure
182
+ join_params = @config.join_options.map { |a| "#{a}" }.join(',')
183
+ params.map { |k,v| "#{k}" + (!v.nil? ? " #{v}": '') }.join(' ') + join_params
184
+
185
+ end
186
+
141
187
  # Writes the PowerShell runner script to a location on the guest.
142
188
  #
143
189
  # @param [String] script The PowerShell runner script.
@@ -157,7 +203,16 @@ module VagrantPlugins
157
203
  guest_script_path
158
204
  end
159
205
 
206
+ # Remove temporary run script as it may contain
207
+ # sensitive plain-text credentials.
208
+ def remove_command_runner_script
209
+ @machine.communicate.sudo("del #{WINDOWS_DOMAIN_GUEST_RUNNER_PATH}")
210
+ end
211
+
160
212
  # Runs the PowerShell script on the guest machine.
213
+ #
214
+ # Streams the output of the command to the UI
215
+ # @return [boolean] The result of the remote command
161
216
  def run_remote_command_runner(script_path)
162
217
  command = ". '#{script_path}'"
163
218
 
@@ -171,20 +226,39 @@ module VagrantPlugins
171
226
  shell: :powershell
172
227
  }
173
228
 
174
- @machine.communicate.sudo(command, opts) do |type, data|
229
+ result = @machine.communicate.sudo(command, opts) do |type, data|
175
230
  if !data.chomp.empty?
176
231
  if [:stderr, :stdout].include?(type)
177
232
  color = type == :stdout ? :green : :red
178
233
  @machine.ui.info(
179
234
  data.chomp,
180
- color: color, new_line: false, prefix: false)
235
+ color: color, new_line: false, prefix: false)
181
236
  end
182
237
  end
183
238
  end
239
+
240
+ result
184
241
  end
185
242
 
186
- # If on using WinRM, we can assume we are on Windows
243
+ # Gets the Computer Name from the guest machine
244
+ def get_guest_computer_name(machine)
245
+ computerName = ""
246
+ machine.communicate.shell.powershell("$env:COMPUTERNAME") do |type, data|
247
+ if !data.chomp.empty?
248
+ if [:stderr, :stdout].include?(type)
249
+ color = type == :stdout ? :green : :red
250
+ computerName = data.chomp
251
+ @logger.info("Detected guest computer name: #{computerName}")
252
+ end
253
+ end
254
+ end
255
+
256
+ computerName
257
+ end
258
+
259
+ # Is the guest Windows?
187
260
  def windows?
261
+ # If using WinRM, we can assume we are on Windows
188
262
  @machine.config.vm.communicator == :winrm
189
263
  end
190
264
 
@@ -1,9 +1,9 @@
1
+ <% if options[:username] != nil && options[:unsecure] != true %>
1
2
  $secpasswd = ConvertTo-SecureString "<%= options[:password] %>" -AsPlainText -Force
2
3
  $credentials = New-Object System.Management.Automation.PSCredential ("<%= options[:username] %>", $secpasswd)
3
-
4
+ <% end %>
4
5
  <% if options[:add_to_domain] === true %>
5
- echo "Add-Computer"
6
- Add-Computer -DomainName <%= options[:domain] %> -Credential $credentials -Verbose -Force #-WhatIf
6
+ Add-Computer <%= options[:parameters] %> -Verbose -Force
7
7
  <% else %>
8
- Remove-Computer -UnjoinDomainCredential $credentials -Verbose -Force
8
+ Remove-Computer <%= options[:parameters] %> -Verbose -Force
9
9
  <% end %>
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module WindowsDomain
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
@@ -77,11 +77,6 @@ describe VagrantPlugins::WindowsDomain::Config do
77
77
  assert_error("You must not supply a \"username\" and \"password\" if \"unsecure\" is set to true.")
78
78
  end
79
79
 
80
- it "should detect the current computers' name" do
81
- subject.validate(machine)
82
- expect(subject.old_computer_name).to eq("myoldcomputername")
83
- end
84
-
85
80
  end
86
81
 
87
82
  end
@@ -7,7 +7,7 @@ describe VagrantPlugins::WindowsDomain::Provisioner do
7
7
  include_context "unit"
8
8
 
9
9
  let(:root_path) { (Pathname.new(Dir.mktmpdir)).to_s }
10
- let(:ui) { Vagrant::UI::Silent.new }
10
+ let(:ui) { double("ui") }
11
11
  let(:machine) { double("machine", ui: ui) }
12
12
  let(:env) { double("environment", root_path: root_path, ui: ui) }
13
13
  let(:vm) { double ("vm") }
@@ -24,10 +24,10 @@ describe VagrantPlugins::WindowsDomain::Provisioner do
24
24
  before do
25
25
  allow(machine).to receive(:root_config).and_return(root_config)
26
26
  machine.stub(config: root_config, env: env)
27
-
27
+ allow(ui).to receive(:say).with(any_args)
28
28
  allow(machine).to receive(:communicate).and_return(communicator)
29
29
  allow(communicator).to receive(:shell).and_return(shell)
30
- allow(shell).to receive(:powershell).with("$env:COMPUTERNAME").and_yield(:stdout, "myoldcomputername")
30
+ allow(shell).to receive(:powershell).with("$env:COMPUTERNAME").and_yield(:stdout, "myoldcomputername")
31
31
  allow(root_config).to receive(:vm).and_return(vm)
32
32
  allow(vm).to receive(:communicator).and_return(:winrm)
33
33
  root_config.finalize!
@@ -50,148 +50,226 @@ describe VagrantPlugins::WindowsDomain::Provisioner do
50
50
  expect(communicator).to receive(:sudo).with("which Remove-Computer", {:error_class=>VagrantPlugins::WindowsDomain::WindowsDomainError, :error_key=>:binary_not_detected, :domain=>nil, :binary=>"Remove-Computer"})
51
51
  subject.configure(root_config)
52
52
  end
53
+
53
54
  end
54
55
 
55
56
  describe "provision" do
56
57
 
57
- # before do
58
- # # allow(root_config).to receive(:vm).and_return(vm)
59
- # allow(machine).to receive(:root_config).and_return(root_config)
60
- # allow(machine).to receive(:env).and_return(env)
61
- # root_config.finalize!
62
- # root_config.validate(machine)
63
- # subject.configure(root_config)
64
- # machine.stub(config: root_config, env: env, communicate: communicator, guest: guest)
65
- # end
58
+ before do
59
+ allow(machine).to receive(:root_config).and_return(root_config)
60
+ machine.stub(config: root_config, env: env)
61
+ allow(ui).to receive(:say).with(any_args)
62
+ allow(machine).to receive(:communicate).and_return(communicator)
63
+ allow(communicator).to receive(:shell).and_return(shell)
64
+ allow(shell).to receive(:powershell).with("$env:COMPUTERNAME").and_yield(:stdout, "myoldcomputername")
65
+ allow(root_config).to receive(:vm).and_return(vm)
66
+ allow(vm).to receive(:communicator).and_return(:winrm)
67
+ expect(communicator).to receive(:sudo).with("which Add-Computer", {:error_class=>VagrantPlugins::WindowsDomain::WindowsDomainError, :error_key=>:binary_not_detected, :domain=>"foo.com", :binary=>"Add-Computer"})
68
+ expect(communicator).to receive(:sudo).with("which Remove-Computer", {:error_class=>VagrantPlugins::WindowsDomain::WindowsDomainError, :error_key=>:binary_not_detected, :domain=>"foo.com", :binary=>"Remove-Computer"})
66
69
 
67
- it "should join the domain" do
70
+ root_config.domain = "foo.com"
71
+ root_config.username = "username"
72
+ root_config.password = "password"
73
+
74
+ root_config.finalize!
75
+ root_config.validate(machine)
76
+ subject.configure(root_config)
77
+ end
68
78
 
79
+ it "should join the domain" do
80
+ allow(communicator).to receive(:upload)
81
+ allow(ui).to receive(:info)
82
+ expect(communicator).to receive(:sudo).with(". 'c:/tmp/vagrant-windows-domain-runner.ps1'", {:elevated=>true, :error_key=>:ssh_bad_exit_status_muted, :good_exit=>0, :shell=>:powershell}).and_return(true)
83
+ expect(communicator).to receive(:sudo).with("del c:/tmp/vagrant-windows-domain-runner.ps1")
84
+ expect(machine).to receive(:action). with(:reload, {:provision_ignore_sentinel=>false})
85
+ expect(communicator).to receive(:ready?).and_return(true)
86
+ subject.restart_sleep_duration = 0
87
+ subject.provision
88
+ expect(subject.old_computer_name).to eq("myoldcomputername")
69
89
  end
70
90
 
71
91
  it "should restart the machine on a successful domain join" do
92
+ allow(communicator).to receive(:upload)
93
+ allow(ui).to receive(:info)
94
+ expect(communicator).to receive(:sudo).with(". 'c:/tmp/vagrant-windows-domain-runner.ps1'", {:elevated=>true, :error_key=>:ssh_bad_exit_status_muted, :good_exit=>0, :shell=>:powershell}).and_return(true)
95
+ expect(communicator).to receive(:sudo).with("del c:/tmp/vagrant-windows-domain-runner.ps1")
96
+ expect(machine).to receive(:action). with(:reload, {:provision_ignore_sentinel=>false})
97
+ expect(communicator).to receive(:ready?).and_return(true)
98
+ subject.restart_sleep_duration = 0
99
+ subject.provision
100
+ end
72
101
 
102
+ it "should not restart the machine on a failed domain join attempt" do
103
+ allow(communicator).to receive(:upload)
104
+ allow(ui).to receive(:info)
105
+ expect(communicator).to receive(:sudo).with(". 'c:/tmp/vagrant-windows-domain-runner.ps1'", {:elevated=>true, :error_key=>:ssh_bad_exit_status_muted, :good_exit=>0, :shell=>:powershell}).and_return(false)
106
+ expect(communicator).to receive(:sudo).with("del c:/tmp/vagrant-windows-domain-runner.ps1")
107
+ expect(machine).to_not receive(:action). with(:reload, {:provision_ignore_sentinel=>false})
108
+ subject.restart_sleep_duration = 0
109
+ subject.provision
73
110
  end
74
111
 
75
- it "should not attempt to join the domain if already on it" do
112
+ context "generate_command_arguments" do
76
113
 
77
- end
114
+ it "join with credentials if provided" do
115
+ args = subject.generate_command_arguments
116
+ puts args
117
+ end
118
+
119
+ it "not join with credentials if 'unsecure' option provided" do
120
+
121
+ end
78
122
 
79
- it "should authenticate with credentials if provided" do
123
+ it "remove with credentials if provided" do
80
124
 
125
+ end
126
+
127
+ it "remove join with credentials if 'unsecure' option provided" do
128
+
129
+ end
130
+
131
+ it "should rename the computer if the computer name is different" do
132
+
133
+ end
134
+
81
135
  end
82
136
 
83
137
  it "should prompt for credentials if not provided" do
84
-
138
+ root_config.username = nil
139
+ root_config.password = nil
140
+ expect(ui).to receive(:ask).with("Please enter your domain password (output will be hidden): ", {:echo=>false}).and_return("myusername")
141
+ expect(ui).to receive(:ask).with("Please enter your domain username: ")
142
+ subject.set_credentials
85
143
  end
86
144
 
87
- it "should remove any traces of credentials once provisioning has occurred" do
145
+ it "should not prompt for credentials if provided" do
146
+ expect(ui).to_not receive(:ask)
147
+ subject.set_credentials
148
+ end
88
149
 
150
+ it "should remove any traces of credentials once provisioning has occurred" do
151
+ expect(communicator).to receive(:sudo).with("del c:/tmp/vagrant-windows-domain-runner.ps1")
152
+ subject.remove_command_runner_script
89
153
  end
90
154
 
91
155
  end
92
156
 
93
157
  describe "cleanup" do
158
+ before do
159
+ allow(communicator).to receive(:shell).and_return(shell)
160
+ allow(shell).to receive(:powershell).and_yield(:stdout, "myoldcomputername")
161
+ end
162
+
163
+ it "should leave domain" do
164
+ allow(machine).to receive(:communicate).and_return(communicator)
165
+ expect(communicator).to receive(:upload)
166
+ expect(communicator).to receive(:sudo).with(". 'c:/tmp/vagrant-windows-domain-runner.ps1'", {:elevated=>true, :error_key=>:ssh_bad_exit_status_muted, :good_exit=>0, :shell=>:powershell}).and_return(true)
167
+ expect(ui).to receive(:info).with(any_args).once
168
+
169
+ result = subject.leave_domain
170
+ expect(result).to eq(true)
171
+ end
94
172
 
95
173
  it "should leave domain when a `vagrant destroy` is issued" do
174
+ allow(machine).to receive(:communicate).and_return(communicator)
175
+ expect(communicator).to receive(:upload)
176
+ expect(communicator).to receive(:sudo).with(". 'c:/tmp/vagrant-windows-domain-runner.ps1'", {:elevated=>true, :error_key=>:ssh_bad_exit_status_muted, :good_exit=>0, :shell=>:powershell})
177
+ expect(ui).to receive(:info).with(any_args).once
178
+
179
+ subject.cleanup
180
+ end
96
181
 
182
+ it "should ask for credentials when leaving domain when no credentials were provided" do
183
+ root_config.username = nil
184
+ root_config.password = nil
185
+ allow(machine).to receive(:communicate).and_return(communicator)
186
+ allow(machine).to receive(:env).and_return(env)
187
+ expect(communicator).to receive(:upload)
188
+ expect(communicator).to receive(:sudo).with(". 'c:/tmp/vagrant-windows-domain-runner.ps1'", {:elevated=>true, :error_key=>:ssh_bad_exit_status_muted, :good_exit=>0, :shell=>:powershell}).and_yield(:stdout, "deleted!")
189
+ expect(ui).to receive(:info).with(any_args).twice
190
+ expect(ui).to receive(:ask).with("Please enter your domain password (output will be hidden): ", {:echo=>false}).and_return("myusername")
191
+ expect(ui).to receive(:ask).with("Please enter your domain username: ")
192
+
193
+ subject.cleanup
97
194
  end
98
195
 
99
196
  end
100
197
 
101
- # describe "Powershell runner script" do
102
- # before do
103
- # # Prevent counters messing with output in tests
104
- # Vagrant::Util::Counter.class_eval do
105
- # def get_and_update_counter(name=nil) 1 end
106
- # end
107
-
108
- # allow(machine).to receive(:root_config).and_return(root_config)
109
- # root_config.configuration_file = configuration_file
110
- # machine.stub(config: root_config, env: env)
111
- # root_config.module_path = module_path
112
- # root_config.configuration_file = configuration_file
113
- # root_config.finalize!
114
- # root_config.validate(machine)
115
- # subject.configure(root_config)
116
-
117
- # end
118
-
119
- # context "with default parameters" do
120
- # it "should generate a valid powershell command" do
121
- # script = subject.generate_dsc_runner_script
122
- # expect_script = "#
123
- # # DSC Runner.
124
- # #
125
- # # Bootstraps the DSC environment, sets up configuration data
126
- # # and runs the DSC Configuration.
127
- # #
128
- # #
129
-
130
- # # Set the local PowerShell Module environment path
131
- # $absoluteModulePaths = [string]::Join(\";\", (\"/tmp/vagrant-windows-domain-1/modules-0;/tmp/vagrant-windows-domain-1/modules-1\".Split(\";\") | ForEach-Object { $_ | Resolve-Path }))
132
-
133
- # echo \"Adding to path: $absoluteModulePaths\"
134
- # $env:PSModulePath=\"$absoluteModulePaths;${env:PSModulePath}\"
135
- # (\"/tmp/vagrant-windows-domain-1/modules-0;/tmp/vagrant-windows-domain-1/modules-1\".Split(\";\") | ForEach-Object { gci -Recurse $_ | ForEach-Object { Unblock-File $_.FullName} })
136
-
137
- # $script = $(Join-Path \"/tmp/vagrant-windows-domain-1\" \"manifests/MyWebsite.ps1\" -Resolve)
138
- # echo \"PSModulePath Configured: ${env:PSModulePath}\"
139
- # echo \"Running Configuration file: ${script}\"
140
-
141
- # # Generate the MOF file, only if a MOF path not already provided.
142
- # # Import the Manifest
143
- # . $script
144
-
145
- # cd \"/tmp/vagrant-windows-domain-1\"
146
- # $StagingPath = $(Join-Path \"/tmp/vagrant-windows-domain-1\" \"staging\")
147
- # $response = MyWebsite -OutputPath $StagingPath 4>&1 5>&1 | Out-String
148
-
149
- # # Start a DSC Configuration run
150
- # $response += Start-DscConfiguration -Force -Wait -Verbose -Path $StagingPath 4>&1 5>&1 | Out-String
151
- # $response"
152
-
153
- # expect(script).to eq(expect_script)
154
- # end
155
- # end
156
-
157
- # end
158
-
159
- # describe "write DSC Runner script" do
160
- # it "should upload the customised DSC runner to the guest" do
161
- # script = "myscript"
162
- # path = "/local/runner/path"
163
- # guest_path = "c:/tmp/vagrant-windows-domain-runner.ps1"
164
- # machine.stub(config: root_config, env: env, communicate: communicator)
165
- # file = double("file")
166
- # allow(file).to receive(:path).and_return(path)
167
- # allow(Tempfile).to receive(:new) { file }
168
- # expect(file).to receive(:write).with(script)
169
- # expect(file).to receive(:fsync)
170
- # expect(file).to receive(:close).exactly(2).times
171
- # expect(file).to receive(:unlink)
172
- # expect(communicator).to receive(:upload).with(path, guest_path)
173
- # res = subject.write_dsc_runner_script(script)
174
- # expect(res.to_s).to eq(guest_path)
175
- # end
176
- # end
177
-
178
- # describe "Apply DSC" do
179
- # it "should invoke the DSC Runner and notify the User of provisioning status" do
180
- # expect(ui).to receive(:info).with(any_args).once
181
- # expect(ui).to receive(:info).with("provisioned!", {color: :green, new_line: false, prefix: false}).once
182
- # allow(machine).to receive(:communicate).and_return(communicator)
183
- # expect(communicator).to receive(:sudo).with('. ' + "'c:/tmp/vagrant-windows-domain-runner.ps1'",{:elevated=>true, :error_key=>:ssh_bad_exit_status_muted, :good_exit=>0, :shell=>:powershell}).and_yield(:stdout, "provisioned!")
184
-
185
- # subject.run_dsc_apply
186
- # end
187
-
188
- # it "should show error output in red" do
189
- # expect(ui).to receive(:info).with(any_args).once
190
- # expect(ui).to receive(:info).with("provisioned!", {color: :red, new_line: false, prefix: false}).once
191
- # allow(machine).to receive(:communicate).and_return(communicator)
192
- # expect(communicator).to receive(:sudo).with('. ' + "'c:/tmp/vagrant-windows-domain-runner.ps1'",{:elevated=>true, :error_key=>:ssh_bad_exit_status_muted, :good_exit=>0, :shell=>:powershell}).and_yield(:stderr, "provisioned!")
193
-
194
- # subject.run_dsc_apply
195
- # end
196
- # end
198
+ describe "Powershell runner script" do
199
+ before do
200
+ allow(machine).to receive(:root_config).and_return(root_config)
201
+ machine.stub(config: root_config, env: env)
202
+ allow(ui).to receive(:say).with(any_args)
203
+ allow(machine).to receive(:communicate).and_return(communicator)
204
+ allow(communicator).to receive(:shell).and_return(shell)
205
+ allow(shell).to receive(:powershell).with("$env:COMPUTERNAME").and_yield(:stdout, "myoldcomputername")
206
+ allow(root_config).to receive(:vm).and_return(vm)
207
+ allow(vm).to receive(:communicator).and_return(:winrm)
208
+ root_config.domain = "foo.com"
209
+ root_config.username = "username"
210
+ root_config.password = "password"
211
+ root_config.finalize!
212
+ root_config.validate(machine)
213
+ end
214
+
215
+ context "with credentials provided" do
216
+
217
+ it "should generate a valid powershell command to add the computer to a domain" do
218
+ script = subject.generate_command_runner_script
219
+ expect_script =
220
+ %Q{$secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force
221
+ $credentials = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)
222
+ Add-Computer -DomainName foo.com -Credential $credentials -Verbose -Force
223
+ }
224
+ expect(script).to eq(expect_script)
225
+ end
226
+
227
+ it "should generate a valid powershell command to remove the computer from a domain" do
228
+ script = subject.generate_command_runner_script(false)
229
+ expect_script =
230
+ %Q{$secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force
231
+ $credentials = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)
232
+ Remove-Computer -DomainName foo.com -UnjoinDomainCredential $credentials -Verbose -Force
233
+ }
234
+ expect(script).to eq(expect_script)
235
+ end
236
+
237
+ context "with join options" do
238
+ it "should rename the computer on join" do
239
+
240
+ root_config.computer_name = "mynewcomputername"
241
+ root_config.ou_path = "OU=testOU,DC=domain,DC=Domain,DC=com"
242
+ root_config.finalize!
243
+ root_config.validate(machine)
244
+
245
+ script = subject.generate_command_runner_script
246
+ expect_script =
247
+ %Q{$secpasswd = ConvertTo-SecureString "password" -AsPlainText -Force
248
+ $credentials = New-Object System.Management.Automation.PSCredential ("username", $secpasswd)
249
+ Add-Computer -DomainName foo.com -Credential $credentials -NewName 'mynewcomputername' -OUPath 'OU=testOU,DC=domain,DC=Domain,DC=com' -Verbose -Force
250
+ }
251
+ expect(script).to eq(expect_script)
252
+ end
253
+ end
254
+ end
255
+
256
+ context "with 'unsecure' parameter provided" do
257
+ before do
258
+ root_config.unsecure = true
259
+ end
260
+
261
+ it "should generate a valid powershell command to add the computer to a domain" do
262
+ script = subject.generate_command_runner_script.strip
263
+ expect_script = "Add-Computer -DomainName foo.com -Unsecure -Verbose -Force"
264
+ expect(script).to eq(expect_script)
265
+ end
266
+
267
+ it "should generate a valid powershell command to remove the computer from a domain" do
268
+ script = subject.generate_command_runner_script(false).strip
269
+ expect_script = "Remove-Computer -DomainName foo.com -Verbose -Force"
270
+ expect(script).to eq(expect_script)
271
+ end
272
+ end
273
+
274
+ end
197
275
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-windows-domain
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Fellows
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-30 00:00:00.000000000 Z
11
+ date: 2015-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake