-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
|
||
|
||
|
||
|