Class: VmShepherd::OpenstackManager

Inherits:
Object
  • Object
show all
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

Methods included from RetryHelper

#retry_until

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_environmentObject



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, vm_options)
  say "Uploading the image #{raw_file_path}"
  image = image_service.images.create(
    name:             vm_options[: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(vm_options[:flavor_name])
  network         = network_service.networks.find { |net| net.name == vm_options[:network_name] }
  security_groups = vm_options[:security_group_names]

  say('Launching an instance')
  server = service.servers.create(
    name:            vm_options[:name],
    flavor_ref:      flavor.id,
    image_ref:       image.id,
    key_name:        vm_options[:key_name],
    security_groups: security_groups,
    nics:            [
                       {net_id: network.id, v4_fixed_ip: vm_options[: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 == vm_options[: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(vm_options)
  say('Destroying Ops Manager instances')

  server = service.servers.find { |srv| srv.name == vm_options[:name] }
  if server
    say("Found running Ops Manager instance #{server.id}")
    server.destroy
    retry_until(retry_limit: 30) do
      say("  Waiting for #{vm_options[:name]} server to be destroyed")
      service.servers.find { |srv| srv.name == vm_options[:name] }.nil?
    end
    say('Ops Manager instance destroyed')
  end

  image = image_service.images.find { |img| img.name == vm_options[: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_serviceObject



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_serviceObject



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_environmentObject



116
117
# File 'lib/vm_shepherd/openstack_manager.rb', line 116

def prepare_environment
end

#serviceObject



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_serviceObject



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