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
115
116
117
118
119
120
121
122
# File 'lib/vm_shepherd/openstack_manager.rb', line 73

def clean_environment
  say("Destroying #{service.servers.size} instances:")
  retry_until(retry_limit: 2) do
    begin
      service.servers.each do |server|
        server.volumes.each do |volume|
          say("  Detaching volume #{volume.id} from server #{server.id}")
          server.detach_volume(volume.id)
          volume.wait_for { volume.ready? }
        end

        say("  Destroying instance #{server.id}")
        server.destroy
      end

      retry_until(retry_limit: 15) 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|
        next if image.status == 'deleted'
        say("  Destroying image #{image.id}")
        image.destroy
      end

      say("Destroying #{service.volumes.size} volumes:")
      service.volumes.each do |volume|
        say("  Destroying volume #{volume.id}; current status: [#{volume.status}]")
        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
    rescue VmShepherd::RetryHelper::RetryLimitExceeded, Fog::Errors::TimeoutError => e
      say("Going to retry to cleanup again. First attempt raised #{e.class}: #{e.message}")
    end
  end
end

#connection_optionsObject



127
128
129
130
131
# File 'lib/vm_shepherd/openstack_manager.rb', line 127

def connection_options
  {
    :ssl_verify_peer => false
  }
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]} #{Time.now}",
    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_service.images.each do |image|
    next unless /^#{vm_options[:name]} \d+/ =~ image.name && image.status != 'deleted'
    say("Found an Ops Manager image for env [#{vm_options[:name]}]: [#{image.id}][#{image.name}]")
    image.destroy
    say('Ops Manager image destroyed')
  end
  say('Done destroying Ops Manager instances')
end

#image_serviceObject



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/vm_shepherd/openstack_manager.rb', line 144

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',
    connection_options: connection_options,
  )
end

#network_serviceObject



156
157
158
159
160
161
162
163
164
165
# File 'lib/vm_shepherd/openstack_manager.rb', line 156

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,
    connection_options: connection_options,
  )
end

#prepare_environmentObject



124
125
# File 'lib/vm_shepherd/openstack_manager.rb', line 124

def prepare_environment
end

#serviceObject



133
134
135
136
137
138
139
140
141
142
# File 'lib/vm_shepherd/openstack_manager.rb', line 133

def service
  @service ||= Fog::Compute.new(
    provider: 'openstack',
    openstack_auth_url: auth_url,
    openstack_username: username,
    openstack_tenant: tenant,
    openstack_api_key: api_key,
    connection_options: connection_options,
  )
end

#storage_serviceObject



167
168
169
170
171
172
173
174
175
176
# File 'lib/vm_shepherd/openstack_manager.rb', line 167

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,
    connection_options: connection_options,
  )
end