vagrant-windows-domain 1.1.7 → 1.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f5a4484f57b9b433e8299324a8cc574809763571
4
- data.tar.gz: d604d562237867e5c6e6ca019b51d4e6be7b5b5e
3
+ metadata.gz: 5b4d4875a6881aa9302b991ca98ca530ba0c2387
4
+ data.tar.gz: b43d80f3309032da02801d7ca249cc4b08671743
5
5
  SHA512:
6
- metadata.gz: 1c6519d627645442489e233fcf61def98cd363b53773f16d5f205ae03d071442655180317e53dd67bf8a3fd0dd78f795bac4d9fdfaef8888b4987019ca1a2031
7
- data.tar.gz: 2a10e956e23facd3e314b70daf2092a7bb0ea766ec3d6587b656c497c0393b9c3a0e19cc22710c29df391242311a5f217c8f1502df857ca3ca071cf40c49d34b
6
+ metadata.gz: 59121ad5e5524c081892f311c7a515443fd22520c906bff36dc027e00f0bb1f01e1e9e0aa42f7c65056c286401f294fdf7375ed3bbb0fde6985c397f95b81476
7
+ data.tar.gz: 8646823778f6fb96f14b0425908fdadcbfb0e8cf111ca9a2ca86eb905fef9d5b321e86e792fc5f2f7ae78862befb2f0b1b9cba416de56a45f6f882789c6a123c
@@ -10,8 +10,7 @@ VAGRANTFILE_API_VERSION = "2"
10
10
 
11
11
  Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
12
12
 
13
- config.vm.box = "talentsearch-api-1.0.2"
14
- # config.vm.box = "mfellows/windows2012r2"
13
+ config.vm.box = "mfellows/windows2012r2"
15
14
  config.vm.guest = :windows
16
15
  config.vm.communicator = "winrm"
17
16
  config.vm.network :forwarded_port, guest: 5985, host: 5985, id: "winrm", auto_correct: true
@@ -1,15 +1,32 @@
1
1
  require_relative '../provisioner'
2
- require 'pp'
3
2
 
4
3
  module VagrantPlugins
5
4
  module WindowsDomain
5
+ # Include the built-in modules so we can use them as top-level things.
6
+ include Vagrant::Action::Builtin
7
+
8
+ # Leave a domain on a `vagrant destroy`.
9
+ #
10
+ # This is an Action middleware component that will detect the state of the machine -
11
+ # and any corresponding configuration and act accordingly. This function accepts user input and
12
+ # will halt the execution of proceeding middleware if the user chooses to cancel.
13
+ #
14
+ # Also note that it will interfere with a `vagrant destroy` user input to avoid duplicate "are you sure.."
15
+ # messages.
6
16
  class LeaveDomain
7
17
  include VagrantPlugins::WindowsDomain
8
18
 
19
+ attr_accessor :machine
20
+ attr_accessor :config
21
+ attr_accessor :env
22
+ attr_accessor :app
23
+ attr_accessor :provisioner
24
+
9
25
  def initialize(app, env)
10
26
  @logger = Log4r::Logger.new("vagrant::provisioners::vagrant_windows_domain")
11
27
  @logger.debug("Initialising WindowsDomain plugin on destroy action")
12
28
  @app = app
29
+ @env = env
13
30
  @machine = env[:machine]
14
31
 
15
32
  @machine.config.vm.provisioners.each do |prov|
@@ -19,16 +36,40 @@ module VagrantPlugins
19
36
  @provisioner = VagrantPlugins::WindowsDomain::Provisioner.new(@machine, @config)
20
37
  end
21
38
 
39
+ # The middleware method that is invoked automatically by the Plugin ecosystem.
40
+ # Expected to call the next middleware component in the chain if action should proceed.
22
41
  def call(env)
23
- if @config
24
- @logger.debug("Configuration detected, triggering leave domain action")
25
- @provisioner.destroy
26
- else
27
- @logger.debug("No configuration detected, not leaving any domain")
28
- end
42
+
43
+ if @config and @config.include? "domain" and @config.domain != nil
44
+
45
+ if [:not_created].include? @machine.state.id
46
+ @logger.debug("Machine not created, nothing to do")
47
+ elsif [:running].include? @machine.state.id
48
+ answer = @machine.env.ui.ask("Are you sure you want to destroy this machine and disconnect from #{@config.domain}? (y/n)")
49
+ if answer.downcase == 'y'
50
+ env[:force_confirm_destroy] = true # Prevent the popup dialog again
51
+ @logger.debug("Valid configuration detected, triggering leave domain action")
52
+ @provisioner.destroy
53
+ end
54
+ else
55
+ @machine.env.ui.say(:warn, "Machine is currently not running. To properly leave the #{@config.domain} network the machine needs to be running and connected to the network in which it was provisioned. Please run `vagrant up` and then `vagrant destroy`.\n")
56
+ answer = @machine.env.ui.ask("Would you like to continue destroying this machine, leaving this machine orphaned in the '#{@config.domain}' network? (y/n)")
57
+ return unless answer.downcase == 'y' # Bail out of destroy and prevent middleware from continuing on
58
+
59
+ # OK, we're being naughty and letting the rest of the middleware do their things (i.e. destroy the machine, and such)
60
+
61
+ env[:force_confirm_destroy] = true # Prevent the popup dialog again
62
+ @logger.debug("Force destroying this machine and not leaving the domain #{@config.domain}")
63
+ @machine.env.ui.say(:warn, "Force destroying this machine and not leaving the domain #{@config.domain}. May FSM have mercy on your soul.")
64
+ end
65
+ else
66
+ @logger.debug("No configuration detected, not leaving any domain")
67
+ end
68
+
69
+ # Continue the rest of the middleware actions
29
70
  @app.call(env)
30
71
  end
31
72
 
32
73
  end
33
74
  end
34
- end
75
+ end
@@ -103,45 +103,6 @@ module VagrantPlugins
103
103
  # for any state related to the machine created by the provisioner
104
104
  # to be cleaned up.
105
105
  def destroy
106
- #
107
- # # Returns the state of this machine. The state is queried from the
108
- # # backing provider, so it can be any arbitrary symbol.
109
- # #
110
- # # @return [MachineState]
111
- # def state
112
- # result = @provider.state
113
- # raise Errors::MachineStateInvalid if !result.is_a?(MachineState)
114
-
115
- # # Update our state cache if we have a UUID and an entry in the
116
- # # master index.
117
- # uuid = index_uuid
118
- # if uuid
119
- # entry = @env.machine_index.get(uuid)
120
- # if entry
121
- # entry.state = result.short_description
122
- # @env.machine_index.set(entry)
123
- # @env.machine_index.release(entry)
124
- # end
125
- # end
126
-
127
- # result
128
- # end
129
-
130
-
131
- # Check state: if NOT running, prompt user to start the box and return false
132
-
133
-
134
-
135
- # Check state: if running, continue
136
-
137
-
138
-
139
- # Potentially ask if the user really wants to destroy first?
140
-
141
- # If we leave, set Force Key :force_confirm_destroy to true
142
- # -> We have already left domain!
143
-
144
-
145
106
  if @config && @config.include?("domain")
146
107
  set_credentials
147
108
  leave_domain
@@ -186,7 +147,7 @@ module VagrantPlugins
186
147
  def generate_command_runner_script(add_to_domain=true)
187
148
  path = File.expand_path("../templates/runner.ps1", __FILE__)
188
149
 
189
- script = Vagrant::Util::TemplateRenderer.render(path, options: {
150
+ Vagrant::Util::TemplateRenderer.render(path, options: {
190
151
  config: @config,
191
152
  username: @config.username,
192
153
  password: @config.password,
@@ -259,30 +220,26 @@ module VagrantPlugins
259
220
  # Streams the output of the command to the UI
260
221
  # @return [boolean] The result of the remote command
261
222
  def run_remote_command_runner(script_path)
262
- command = ". '#{script_path}'"
263
-
264
223
  @machine.ui.info(I18n.t(
265
224
  "vagrant_windows_domain.running"))
266
225
 
267
- opts = {
268
- elevated: true,
269
- error_key: :ssh_bad_exit_status_muted,
270
- good_exit: 0,
271
- shell: :powershell
272
- }
273
-
274
- result = @machine.communicate.sudo(command, opts) do |type, data|
226
+ # A bit of an ugly dance, but this is how we get neat, colourised output and exit codes from a Powershell run
227
+ last_type = nil
228
+ new_line = ""
229
+ error = false
230
+ machine.communicate.shell.powershell("powershell -ExecutionPolicy Bypass -OutputFormat Text -file #{script_path}") do |type, data|
275
231
  if !data.chomp.empty?
232
+ error = true if type == :stderr
276
233
  if [:stderr, :stdout].include?(type)
277
234
  color = type == :stdout ? :green : :red
278
- @machine.ui.info(
279
- data.chomp,
280
- color: color, new_line: false, prefix: false)
235
+ new_line = "\r\n" if last_type != nil and last_type != type
236
+ last_type = type
237
+ @machine.ui.info( new_line + data.chomp, color: color, new_line: false, prefix: false)
281
238
  end
282
239
  end
283
240
  end
284
241
 
285
- result == 0
242
+ error == false
286
243
  end
287
244
 
288
245
  # Gets the Computer Name from the guest machine
@@ -291,7 +248,6 @@ module VagrantPlugins
291
248
  machine.communicate.shell.powershell("$env:COMPUTERNAME") do |type, data|
292
249
  if !data.chomp.empty?
293
250
  if [:stderr, :stdout].include?(type)
294
- color = type == :stdout ? :green : :red
295
251
  computerName = data.chomp
296
252
  @logger.info("Detected guest computer name: #{computerName}")
297
253
  end
@@ -309,4 +265,4 @@ module VagrantPlugins
309
265
 
310
266
  end
311
267
  end
312
- end
268
+ end
@@ -1,5 +1,5 @@
1
1
  module Vagrant
2
2
  module WindowsDomain
3
- VERSION = "1.1.7"
3
+ VERSION = "1.1.8"
4
4
  end
5
5
  end
@@ -0,0 +1,123 @@
1
+ require 'spec_helper'
2
+ require 'vagrant-windows-domain/provisioner'
3
+ require 'vagrant-windows-domain/action/leave_domain'
4
+ require 'vagrant-windows-domain/config'
5
+ require 'base'
6
+
7
+ describe VagrantPlugins::WindowsDomain::LeaveDomain do
8
+
9
+ include_context "unit"
10
+ let(:instance) { described_class.new }
11
+
12
+ let(:root_path) { (Pathname.new(Dir.mktmpdir)).to_s }
13
+ let(:domain) { "foo.com" }
14
+ let(:ui) { double("ui") }
15
+ let(:app) { double("app") }
16
+ let(:communicator) { double ("communicator") }
17
+ let(:shell) { double ("shell") }
18
+ let(:powershell) { double ("powershell") }
19
+ let(:guest) { double ("guest") }
20
+ let(:configuration_file) { "manifests/MyWebsite.ps1" }
21
+ let(:module_path) { ["foo/modules", "foo/modules2"] }
22
+ let(:config) { VagrantPlugins::WindowsDomain::Config.new }
23
+ let(:vm) { double("vm", provisioners: [double("prov", config: config)]) }
24
+ let(:root_config) { double("root_config", vm: vm) }
25
+ let(:env) { {:ui => ui, :machine => machine} }
26
+ let(:machine) { double("machine", ui: ui, id: "1234", config: root_config) }
27
+ let(:provisioner) { double("provisioner") }
28
+ subject { described_class.new app, env }
29
+
30
+ describe "call" do
31
+
32
+ before do
33
+ allow(machine).to receive(:communicate).and_return(communicator)
34
+ allow(env).to receive(:machine).and_return(machine)
35
+ allow(communicator).to receive(:shell).and_return(shell)
36
+ allow(shell).to receive(:powershell).and_yield(:stdout, "myoldcomputername")
37
+ config.domain = domain
38
+ allow(env).to receive(:machine).and_return(machine)
39
+ allow(machine).to receive(:env).and_return(env)
40
+ allow(env).to receive(:ui).and_return(ui)
41
+ subject.provisioner = provisioner
42
+ end
43
+
44
+ context "when no configuration exists for the machine" do
45
+ it "should pass control to the next middleware Action" do
46
+ config.domain = nil
47
+ expect(app).to receive(:call).with(env)
48
+ subject.call(env)
49
+ end
50
+ end
51
+
52
+ context "when machine is running" do
53
+ it "should prompt the user if they would like to destroy & d/c the machine" do
54
+ state = double("state", id: :running)
55
+ expect(provisioner).to receive(:destroy)
56
+
57
+ expect(app).to receive(:call).with(env)
58
+ expect(ui).to receive(:ask).with("Are you sure you want to destroy this machine and disconnect from #{domain}? (y/n)").and_return("y")
59
+ expect(machine).to receive(:state).and_return(state).twice
60
+ subject.call(env)
61
+
62
+ end
63
+ end
64
+
65
+ context "when machine is :paused, :saved or :poweroff" do
66
+ it "should prompt the user if they would like to force destroy the machine" do
67
+ state = double("state", id: :poweroff)
68
+ provisioner = double("provisioner")
69
+ subject.provisioner = provisioner
70
+
71
+ expect(ui).to receive(:say).with(:warn, "Machine is currently not running. To properly leave the #{domain} network the machine needs to be running and connected to the network in which it was provisioned. Please run `vagrant up` and then `vagrant destroy`.\n")
72
+ expect(ui).to receive(:ask).with("Would you like to continue destroying this machine, leaving this machine orphaned in the '#{domain}' network? (y/n)").and_return("y")
73
+ expect(ui).to receive(:say).with(:warn, "Force destroying this machine and not leaving the domain foo.com. May FSM have mercy on your soul.")
74
+ expect(machine).to receive(:state).and_return(state).twice
75
+ expect(app).to receive(:call).with(env)
76
+ # Can't call destroy on a non-running machine
77
+ expect(provisioner).to_not receive(:destroy)
78
+
79
+ subject.call(env)
80
+
81
+ expect(env[:force_confirm_destroy]).to be(true)
82
+ end
83
+
84
+ it "should not pass on to middleware if user declines force destroy" do
85
+ state = double("state", id: :poweroff)
86
+ provisioner = double("provisioner")
87
+ subject.provisioner = provisioner
88
+
89
+ expect(ui).to receive(:say).with(:warn, "Machine is currently not running. To properly leave the #{domain} network the machine needs to be running and connected to the network in which it was provisioned. Please run `vagrant up` and then `vagrant destroy`.\n")
90
+ expect(ui).to receive(:ask).with("Would you like to continue destroying this machine, leaving this machine orphaned in the '#{domain}' network? (y/n)").and_return("n")
91
+ expect(machine).to receive(:state).and_return(state).twice
92
+ expect(app).to_not receive(:call).with(env)
93
+ expect(provisioner).to_not receive(:destroy)
94
+ expect(env[:force_confirm_destroy]).to be(nil)
95
+
96
+ subject.call(env)
97
+
98
+ end
99
+ end
100
+
101
+ context "when machine is :not_created" do
102
+ it "should pass control to the next middleware action" do
103
+ state = double("state", id: :not_created)
104
+ expect(machine).to receive(:state).and_return(state)
105
+
106
+ expect(app).to receive(:call).with(env)
107
+ subject.call(env)
108
+
109
+ end
110
+ end
111
+ end
112
+
113
+
114
+ describe "initialize" do
115
+
116
+ its("env") { should eq(env) }
117
+ its("app") { should eq(app) }
118
+ its("config") { should eq(config) }
119
+ its("machine") { should eq(machine) }
120
+
121
+ end
122
+
123
+ end
@@ -73,7 +73,7 @@ describe VagrantPlugins::WindowsDomain::Provisioner do
73
73
  it "should join the domain" do
74
74
  allow(communicator).to receive(:upload)
75
75
  allow(ui).to receive(:info)
76
- 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(0)
76
+ expect(shell).to receive(:powershell).with("powershell -ExecutionPolicy Bypass -OutputFormat Text -file c:/tmp/vagrant-windows-domain-runner.ps1")
77
77
  expect(communicator).to receive(:sudo).with("del c:/tmp/vagrant-windows-domain-runner.ps1")
78
78
  expect(machine).to receive(:action). with(:reload, {:provision_ignore_sentinel=>false})
79
79
  expect(communicator).to receive(:ready?).and_return(true)
@@ -85,7 +85,7 @@ describe VagrantPlugins::WindowsDomain::Provisioner do
85
85
  it "should restart the machine on a successful domain join" do
86
86
  allow(communicator).to receive(:upload)
87
87
  allow(ui).to receive(:info)
88
- 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(0)
88
+ expect(shell).to receive(:powershell).with("powershell -ExecutionPolicy Bypass -OutputFormat Text -file c:/tmp/vagrant-windows-domain-runner.ps1")
89
89
  expect(communicator).to receive(:sudo).with("del c:/tmp/vagrant-windows-domain-runner.ps1")
90
90
  expect(machine).to receive(:action). with(:reload, {:provision_ignore_sentinel=>false})
91
91
  expect(communicator).to receive(:ready?).and_return(true)
@@ -95,8 +95,9 @@ describe VagrantPlugins::WindowsDomain::Provisioner do
95
95
 
96
96
  it "should not restart the machine on a failed domain join attempt" do
97
97
  allow(communicator).to receive(:upload)
98
- allow(ui).to receive(:info)
99
- 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)
98
+ allow(ui).to receive(:info).with("some exception thrown!", {:color=>:red, :new_line=>false, :prefix=>false}) # Red output on error please!
99
+ allow(ui).to receive(:info).with("\"Running Windows Domain Provisioner\"")
100
+ expect(shell).to receive(:powershell).with("powershell -ExecutionPolicy Bypass -OutputFormat Text -file c:/tmp/vagrant-windows-domain-runner.ps1").and_yield(:stderr, "some exception thrown!")
100
101
  expect(communicator).to receive(:sudo).with("del c:/tmp/vagrant-windows-domain-runner.ps1")
101
102
  expect(machine).to_not receive(:action). with(:reload, {:provision_ignore_sentinel=>false})
102
103
  subject.restart_sleep_duration = 0
@@ -166,8 +167,9 @@ describe VagrantPlugins::WindowsDomain::Provisioner do
166
167
  it "should leave domain" do
167
168
  allow(machine).to receive(:communicate).and_return(communicator)
168
169
  expect(communicator).to receive(:upload)
169
- 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(0)
170
- expect(ui).to receive(:info).with(any_args).once
170
+ expect(shell).to receive(:powershell).with("powershell -ExecutionPolicy Bypass -OutputFormat Text -file c:/tmp/vagrant-windows-domain-runner.ps1").and_yield(:stdout, "deleted")
171
+ expect(ui).to receive(:info).with("\"Running Windows Domain Provisioner\"")
172
+ expect(ui).to receive(:info).with("deleted", {:color=>:green, :new_line=>false, :prefix=>false})
171
173
 
172
174
  result = subject.leave_domain
173
175
  expect(result).to eq(true)
@@ -176,8 +178,9 @@ describe VagrantPlugins::WindowsDomain::Provisioner do
176
178
  it "should leave domain when a `vagrant destroy` is issued" do
177
179
  allow(machine).to receive(:communicate).and_return(communicator)
178
180
  expect(communicator).to receive(:upload)
179
- 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})
180
- expect(ui).to receive(:info).with(any_args).once
181
+ expect(shell).to receive(:powershell).with("powershell -ExecutionPolicy Bypass -OutputFormat Text -file c:/tmp/vagrant-windows-domain-runner.ps1").and_yield(:stdout, "deleted")
182
+ expect(ui).to receive(:info).with("\"Running Windows Domain Provisioner\"")
183
+ expect(ui).to receive(:info).with("deleted", {:color=>:green, :new_line=>false, :prefix=>false})
181
184
 
182
185
  expect(ui).to_not receive(:say)
183
186
  expect(ui).to_not receive(:ask)
@@ -199,7 +202,7 @@ describe VagrantPlugins::WindowsDomain::Provisioner do
199
202
  allow(machine).to receive(:communicate).and_return(communicator)
200
203
  allow(machine).to receive(:env).and_return(env)
201
204
  expect(communicator).to receive(:upload)
202
- 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!")
205
+ expect(shell).to receive(:powershell).with("powershell -ExecutionPolicy Bypass -OutputFormat Text -file c:/tmp/vagrant-windows-domain-runner.ps1").and_yield(:stdout, "deleted")
203
206
  expect(ui).to receive(:info).with(any_args).twice
204
207
  expect(ui).to receive(:ask).with("Please enter your domain password (output will be hidden): ", {:echo=>false}).and_return("myusername")
205
208
  expect(ui).to receive(:ask).with("Please enter your domain username: ")
@@ -285,4 +288,4 @@ Add-Computer -DomainName foo.com -Credential $credentials -NewName 'mynewcompute
285
288
  end
286
289
 
287
290
  end
288
- end
291
+ 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.1.7
4
+ version: 1.1.8
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-02-11 00:00:00.000000000 Z
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -175,6 +175,7 @@ files:
175
175
  - lib/vagrant-windows-domain/version.rb
176
176
  - spec/base.rb
177
177
  - spec/provisioner/config_spec.rb
178
+ - spec/provisioner/leave_domain_spec.rb
178
179
  - spec/provisioner/provisioner_spec.rb
179
180
  - spec/spec_helper.rb
180
181
  - vagrant-windows-domain.gemspec
@@ -198,12 +199,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
199
  version: '0'
199
200
  requirements: []
200
201
  rubyforge_project:
201
- rubygems_version: 2.4.1
202
+ rubygems_version: 2.0.14
202
203
  signing_key:
203
204
  specification_version: 4
204
205
  summary: Windows Domain Provisioner for Vagrant
205
206
  test_files:
206
207
  - spec/base.rb
207
208
  - spec/provisioner/config_spec.rb
209
+ - spec/provisioner/leave_domain_spec.rb
208
210
  - spec/provisioner/provisioner_spec.rb
209
211
  - spec/spec_helper.rb