Skip to content

Commit

Permalink
add VM startup script
Browse files Browse the repository at this point in the history
  • Loading branch information
vlewin committed Mar 31, 2014
1 parent 295108e commit f9018fb
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions jenkins/cloud_vm.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'openstack'
require 'fileutils'

module Cloud
class VM
class << self
def connection
if ENV['CLOUD_USER'] && ENV['CLOUD_PASS']
@@connection ||= OpenStack::Connection.create({
:username => ENV['CLOUD_USER'],
:api_key => ENV['CLOUD_PASS'],
:auth_method => "password",
:auth_url => "https://dashboardp2.cloud.suse.de:5000/v2.0",
:authtenant_name => "appliances",
:service_type => "compute"
})
else
puts "ERROR: Environment variable CLOUD_USER or CLOUD_PASS is not defined"
exit(1)
end
end

def start(name='SUSEConnect_testing')
image = connection.get_image('f29ec2c0-2483-4dae-af4d-01ddf7dd92c5')
flavor = connection.get_flavor(2)
address = connection.get_floating_ips.select{ |ip| ip.instance_id.nil? }.first || connection.create_floating_ip

puts "*** Creating new '#{name}' VM ..."
server = connection.create_server(:name => name, :imageRef => image.id, :flavorRef => flavor.id)

puts "*** Waiting for VM ..."
sleep(10) # Wait 10 seconds until machine gets initialized

puts "*** Attaching floating ip '#{address.ip}' to '#{name}' VM ..."
connection.attach_floating_ip({:server_id => server.id, :ip_id => address.id})

puts "*** Creating node configuration file #{address.ip}.json ..."
create_node_file(address.ip)

return server
end

def destroy(id)
s = connection.get_server id
s.delete!
end

def create_node_file(ip)
dir = File.join(File.expand_path(File.dirname(__FILE__)), '../kitchen/nodes')
src = File.join(dir, 'template.json')
dest = File.join(dir, "#{ip}.json")
FileUtils.cp(src, dest)
end
end
end
end






0 comments on commit f9018fb

Please sign in to comment.