Class: VmShepherd::OpenstackManager
- Inherits:
-
Object
- Object
- VmShepherd::OpenstackManager
- Includes:
- RetryHelper
- Defined in:
- lib/vm_shepherd/openstack_manager.rb
Constant Summary
Constants included from RetryHelper
RetryHelper::RETRY_INTERVAL, RetryHelper::RETRY_LIMIT
Instance Method Summary collapse
- #clean_environment ⇒ Object
- #deploy(raw_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
- #prepare_environment ⇒ Object
- #service ⇒ Object
- #storage_service ⇒ Object
Methods included from RetryHelper
Constructor Details
#initialize(auth_url:, username:, api_key:, tenant:) ⇒ OpenstackManager
Returns a new instance of OpenstackManager.
8 9 10 11 12 13 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 8 def initialize(auth_url:, username:, api_key:, tenant:) @auth_url = auth_url @username = username @api_key = api_key @tenant = tenant end |
Instance Method Details
#clean_environment ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 73 def clean_environment say("Destroying #{service.servers.size} instances:") service.servers.each do |server| server.volumes.each do |volume| say(" Detaching volume #{volume.id} from server #{server.id}") server.detach_volume(volume.id) end say(" Destroying instance #{server.id}") server.destroy end retry_until(retry_limit: 30) do server_count = service.servers.count say(" Waiting for #{server_count} servers to be destroyed") server_count == 0 end private_images = image_service.images.reject(&:is_public) say("Destroying #{private_images.size} images:") private_images.each do |image| say(" Destroying image #{image.id}") image.destroy end say("Destroying #{service.volumes.size} volumes:") service.volumes.each do |volume| say(" Destroying volume #{volume.id}") volume.wait_for { volume.ready? } volume.destroy end say("Destroying contents of #{storage_service.directories.size} containers:") storage_service.directories.each do |directory| say(" Destroying #{directory.files.size} files from #{directory.key}") directory.files.each do |file| say(" Destroying file #{file.key}") file.destroy end end end |
#deploy(raw_file_path, vm_options) ⇒ Object
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 46 47 48 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 15 def deploy(raw_file_path, ) say "Uploading the image #{raw_file_path}" image = image_service.images.create( name: [:name], size: File.size(raw_file_path), disk_format: 'raw', container_format: 'bare', location: raw_file_path, ) say 'Finished uploading the image' flavor = find_flavor([:flavor_name]) 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
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 50 def destroy() say('Destroying Ops Manager instances') server = service.servers.find { |srv| srv.name == [:name] } if server say("Found running Ops Manager instance #{server.id}") server.destroy retry_until(retry_limit: 30) do say(" Waiting for #{[:name]} server to be destroyed") service.servers.find { |srv| srv.name == [:name] }.nil? end say('Ops Manager instance destroyed') end image = image_service.images.find { |img| img.name == [:name] } if image say("Found Ops Manager image #{image.id}") image.destroy say('Ops Manager image destroyed') end say('Done destroying Ops Manager instances') end |
#image_service ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 129 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
140 141 142 143 144 145 146 147 148 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 140 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, ) end |
#prepare_environment ⇒ Object
116 117 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 116 def prepare_environment end |
#service ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 119 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 |
#storage_service ⇒ Object
150 151 152 153 154 155 156 157 158 |
# File 'lib/vm_shepherd/openstack_manager.rb', line 150 def storage_service @network_service ||= Fog::Storage.new( provider: 'openstack', openstack_auth_url: auth_url, openstack_username: username, openstack_tenant: tenant, openstack_api_key: api_key, ) end |