Class: VmShepherd::OpenstackManager
- Inherits:
-
Object
- Object
- VmShepherd::OpenstackManager
- Defined in:
- lib/vm_shepherd/openstack_manager.rb
Instance Method Summary collapse
- #deploy(qcow2_file_path, vm_options) ⇒ Object
- #destroy(vm_options) ⇒ Object
- #image_service ⇒ Object
-
#initialize(auth_url:, username:, api_key:, tenant:) ⇒ OpenstackManager
constructor
A new instance of OpenstackManager.
- #network_service ⇒ Object
- #service ⇒ Object
Constructor Details
#initialize(auth_url:, username:, api_key:, tenant:) ⇒ OpenstackManager
Returns a new instance of OpenstackManager.
5 6 7 8 9 10 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 5 def initialize(auth_url:, username:, api_key:, tenant:) @auth_url = auth_url @username = username @api_key = api_key @tenant = tenant end |
Instance Method Details
#deploy(qcow2_file_path, vm_options) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 12 def deploy(qcow2_file_path, ) say "Uploading the image #{qcow2_file_path}" image = image_service.images.create( name: [:name], size: File.size(qcow2_file_path), disk_format: 'qcow2', container_format: 'bare', location: qcow2_file_path, ) say 'Finished uploading the image' flavor = find_flavor([:min_disk_size]) network = network_service.networks.find { |net| net.name == [:network_name] } security_groups = [:security_group_names] say('Launching an instance') server = service.servers.create( name: [:name], flavor_ref: flavor.id, image_ref: image.id, key_name: [:key_name], security_groups: security_groups, nics: [ { net_id: network.id, v4_fixed_ip: [:private_ip] } ], ) server.wait_for { ready? } say('Finished launching an instance') say('Assigning a Public IP to the instance') ip = service.addresses.find { |address| address.instance_id.nil? && address.ip == [:public_ip] } ip.server = server say('Finished assigning a Public IP to the instance') end |
#destroy(vm_options) ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 47 def destroy() ip = service.addresses.find { |address| address.ip == [:public_ip] } server = service.servers.get(ip.instance_id) if server image = image_service.images.get(server.image['id']) server.destroy image.destroy end end |
#image_service ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 68 def image_service @image_service ||= Fog::Image.new( provider: 'openstack', openstack_auth_url: auth_url, openstack_username: username, openstack_tenant: tenant, openstack_api_key: api_key, openstack_endpoint_type: 'publicURL', ) end |
#network_service ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 79 def network_service @network_service ||= Fog::Network.new( provider: 'openstack', openstack_auth_url: auth_url, openstack_username: username, openstack_tenant: tenant, openstack_api_key: api_key, openstack_endpoint_type: 'publicURL', ) end |
#service ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 58 def service @service ||= Fog::Compute.new( provider: 'openstack', openstack_auth_url: auth_url, openstack_username: username, openstack_tenant: tenant, openstack_api_key: api_key, ) end |