lib/kitchen/driver/cloudstack.rb in kitchen-cloudstack-0.1.0 vs lib/kitchen/driver/cloudstack.rb in kitchen-cloudstack-0.1.1

- old
+ new

@@ -87,11 +87,11 @@ jobid = server['deployvirtualmachineresponse'].fetch('jobid') info("CloudStack instance <#{state[:server_id]}> created.") debug("Job ID #{jobid}") server_start = compute.query_async_job_result('jobid'=>jobid) while server_start['queryasyncjobresultresponse'].fetch('jobstatus') == 0 - print "." + print ". " sleep(10) server_start = compute.query_async_job_result('jobid'=>jobid) debug("Server_Start: #{server_start} \n") end if server_start['queryasyncjobresultresponse'].fetch('jobstatus') == 2 @@ -100,35 +100,48 @@ end if server_start['queryasyncjobresultresponse'].fetch('jobstatus') == 1 server_info = server_start['queryasyncjobresultresponse']['jobresult']['virtualmachine'] debug(server_info) - puts "\n(server ready)" - if (server_info.fetch('passwordenabled') == true) + print "(server ready)" + + #Check first if the API response has a keypair. I should probably just make this check the config, but this + #should save against typos in the config file. + + if (!server_info.fetch('keypair').nil?) + state[:hostname] = server_info.fetch('nic').first.fetch('ipaddress') + info("SSH for #{state[:hostname]} with SSH Key #{server_info.fetch('keypair')}.") + if File.exist?("./#{server_info.fetch('keypair')}.pem") + keypair = "./#{server_info.fetch('keypair')}.pem" + elsif File.exist?("~/#{server_info.fetch('keypair')}.pem") + keypair = "~/#{server_info.fetch('keypair')}.pem" + elsif File.exist?("~/.ssh/#{server_info.fetch('keypair')}.pem") + keypair = "~/.ssh/#{server_info.fetch('keypair')}.pem" + else + error("Cannot find PEM file specified.") + end + + ssh = Fog::SSH.new(state[:hostname], config[:username], {:keys => keypair}) + debug(state[:hostname]) + debug(config[:username]) + debug(keypair) + deploy_private_key(state[:hostname], ssh) + elsif (server_info.fetch('keypair').nil? && server_info.fetch('passwordenabled') == true) password = server_info.fetch('password') state[:hostname] = server_info.fetch('nic').first.fetch('ipaddress') + # Print out IP and password so you can record it if you want. info("Password for #{config[:username]} at #{state[:hostname]} is #{password}") ssh = Fog::SSH.new(state[:hostname], config[:username], {:password => password}) debug(state[:hostname]) debug(config[:username]) debug(password) - tcp_test_ssh(state[:hostname]) -# Installing SSH keys is consistently failing. Not sure why. - if !(config[:public_key_path].nil?) - pub_key = open(config[:public_key_path]).read - # Wait a few moments for the OS to run the cloud-setup-sshkey/password scripts - sleep(30) - ssh.run([ - %{mkdir .ssh}, - %{echo "#{pub_key}" >> ~/.ssh/authorized_keys} - ]) - end - info("(ssh ready)") + deploy_private_key(state[:hostname], ssh) + elsif (server_info.fetch('keypair').nil? && server_info.fetch('passwordenabled') == false) + state[:hostname] = server_info.fetch('nic').first.fetch('ipaddress') + info("No SSH key specified nor is this a password enabled template. You will have to manually copy your SSH public key to #{state[:hostname]} to use this Kitchen.") end - end - end def destroy(state) return if state[:server_id].nil? @@ -138,11 +151,12 @@ state.delete(:server_id) state.delete(:hostname) end def tcp_test_ssh(hostname) - print(".") + # Ripped unceremoniously from knife-cloudstack-fog as I was having issues with the wait_for_sshd() function. + print(". ") tcp_socket = TCPSocket.new(hostname, 22) readable = IO.select([tcp_socket], nil, nil, 5) if readable debug("\nsshd accepting connections on #{hostname}, banner is #{tcp_socket.gets}\n") yield @@ -165,9 +179,29 @@ rescue Errno::ENETUNREACH sleep 30 false ensure tcp_socket && tcp_socket.close + end + + def deploy_private_key(hostname, ssh) + debug("Deploying private key to #{hostname} using connection #{ssh}") + tcp_test_ssh(hostname) + sync_time = 45 + if (config[:cloudstack_sync_time]) + debug("Setting sync time to #{config[:cloudstack_sync_time]}") + sync_time = config[:cloudstack_sync_time] + end + if !(config[:public_key_path].nil?) + pub_key = open(config[:public_key_path]).read + # Wait a few moments for the OS to run the cloud-setup-password scripts + sleep(sync_time) + ssh.run([ + %{mkdir .ssh}, + %{echo "#{pub_key}" >> ~/.ssh/authorized_keys} + ]) + + end end end end end