Class: VmShepherd::OpenstackManager

Inherits:
Object
  • Object
show all
Defined in:
lib/vm_shepherd/openstack_manager.rb

Instance Method Summary collapse

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, vm_options)
  say "Uploading the image #{qcow2_file_path}"
  image = image_service.images.create(
    name: vm_options[: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(vm_options[:min_disk_size])
  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



47
48
49
50
51
52
53
54
55
56
# File 'lib/vm_shepherd/openstack_manager.rb', line 47

def destroy(vm_options)
  ip = service.addresses.find { |address| address.ip == vm_options[: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_serviceObject



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_serviceObject



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

#serviceObject



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