Skip to content

Commit 3b307fe

Browse files
committed
Support VM password on config + WinRM loop bug
1 parent 6ed3055 commit 3b307fe

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

lib/vagrant-cloudstack/action/read_winrm_info.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,13 @@ def read_winrm_info(cloudstack, machine)
4343
# the username via winrm_info ... yet ;-)
4444

4545
# Read password from file into domain_config
46-
if domain_config.vm_password.nil?
47-
vmcredentials_file = machine.data_dir.join("vmcredentials")
48-
if vmcredentials_file.file?
49-
vmcredentials_password = nil
50-
File.read(vmcredentials_file).each_line do |line|
51-
vmcredentials_password = line.strip
52-
end
53-
domain_config.vm_password = vmcredentials_password
46+
vmcredentials_file = machine.data_dir.join("vmcredentials")
47+
if vmcredentials_file.file?
48+
vmcredentials_password = nil
49+
File.read(vmcredentials_file).each_line do |line|
50+
vmcredentials_password = line.strip
5451
end
52+
domain_config.vm_password = vmcredentials_password
5553
end
5654

5755
transport_info = transport_info.merge({

lib/vagrant-cloudstack/action/run_instance.rb

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ def call(env)
3737

3838
store_volumes
3939

40-
store_password
40+
password = wait_for_password
41+
42+
store_password(password)
4143

4244
configure_networking
4345

@@ -381,6 +383,14 @@ def get_communicator_short_name(communicator)
381383
communicator_short_names[communicator.class.name]
382384
end
383385

386+
def get_communicator_connect_attempts(communicator)
387+
communicator_connect_attempts = {
388+
'VagrantPlugins::CommunicatorSSH::Communicator' => 40,
389+
'VagrantPlugins::CommunicatorWinRM::Communicator' => 1
390+
}
391+
communicator_connect_attempts[communicator.class.name]
392+
end
393+
384394
def evaluate_pf_private_port
385395
return unless @domain_config.pf_private_port.nil?
386396

@@ -756,13 +766,14 @@ def generate_ssh_keypair(keyname, account = nil, domainid = nil, projectid = nil
756766
@domain_config.keypair = sshkeypair['name']
757767
end
758768

759-
def store_password
769+
def wait_for_password
760770
password = nil
761-
if @server.password_enabled and @server.respond_to?('job_id')
771+
772+
if @server.respond_to?('job_id')
762773
server_job_result = @env[:cloudstack_compute].query_async_job_result({:jobid => @server.job_id})
763774
if server_job_result.nil?
764-
@env[:ui].warn(' -- Failed to retrieve job_result for retrieving the password')
765-
return
775+
@env[:ui].warn(' -- Failed to retrieve job_result')
776+
raise 'ERROR -- Failed to retrieve job_result'
766777
end
767778

768779
while true
@@ -776,15 +787,28 @@ def store_password
776787
end
777788

778789
@env[:ui].info("Password of virtualmachine: #{password}")
779-
# Set the password on the current communicator
780-
@domain_config.vm_password = password
790+
end
781791

782-
# Save password to file
783-
vmcredentials_file = @env[:machine].data_dir.join('vmcredentials')
784-
vmcredentials_file.open('w') do |f|
785-
f.write("#{password}")
786-
end
792+
password
793+
end
794+
795+
def store_password(password)
796+
unless @server.password_enabled
797+
@env[:ui].info("VM is not password-enabled. Using virtualmachine password from config")
798+
if @domain_config.vm_password.nil?
799+
fail "No vm_password configured but VM is not password enabled either!"
800+
end
801+
password = @domain_config.vm_password
787802
end
803+
804+
# Save password to file
805+
vmcredentials_file = @env[:machine].data_dir.join('vmcredentials')
806+
vmcredentials_file.open('w') do |f|
807+
f.write("#{password}")
808+
end
809+
810+
# Set the password on the current communicator
811+
@domain_config.vm_password = password
788812
end
789813

790814
def store_volumes
@@ -829,15 +853,18 @@ def wait_for_communicator_ready
829853
@env[:metrics]['instance_ssh_time'] = Util::Timer.time do
830854
# Wait for communicator to be ready.
831855
communicator_short_name = get_communicator_short_name(@env[:machine].communicate)
856+
communicator_connect_attempts = get_communicator_connect_attempts(@env[:machine].communicate)
832857
@env[:ui].info(
833858
I18n.t('vagrant_cloudstack.waiting_for_communicator',
834859
communicator: communicator_short_name.to_s.upcase)
835860
)
836-
while true
861+
connection_attempt = 0
862+
while connection_attempt<communicator_connect_attempts
837863
# If we're interrupted then just back out
838864
break if @env[:interrupted]
839865
break if @env[:machine].communicate.ready?
840866
sleep 2
867+
connection_attempt = connection_attempt + 1
841868
end
842869
end
843870
@logger.info("Time for SSH ready: #{@env[:metrics]['instance_ssh_time']}")

0 commit comments

Comments
 (0)