Class: Kitchen::Driver::Openstack

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/driver/openstack.rb,
lib/kitchen/driver/openstack/volume.rb

Overview

This takes from the Base Class and creates the OpenStack driver. rubocop: disable Metrics/ClassLength

Defined Under Namespace

Classes: Volume

Constant Summary collapse

@@ip_pool_lock =
Mutex.new

Instance Method Summary collapse

Instance Method Details

#config_server_nameObject

Set the proper server name in the config



66
67
68
69
70
71
72
73
74
# File 'lib/kitchen/driver/openstack.rb', line 66

def config_server_name
  return if config[:server_name]

  config[:server_name] = if config[:server_name_prefix]
                           server_name_prefix(config[:server_name_prefix])
                         else
                           default_name
                         end
end

#create(state) ⇒ Object



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
# File 'lib/kitchen/driver/openstack.rb', line 76

def create(state)
  config_server_name
  if state[:server_id]
    info "#{config[:server_name]} (#{state[:server_id]}) already exists."
    return
  end
  disable_ssl_validation if config[:disable_ssl_validation]
  server = create_server
  state[:server_id] = server.id
  info "OpenStack instance with ID of <#{state[:server_id]}> is ready." # rubocop:disable Metrics/LineLength

  # this is due to the glance_caching issues. Annoying yes, but necessary.
  debug "Waiting for VM to be in ACTIVE state for a max time of:#{config[:glance_cache_wait_timeout]} seconds" # rubocop:disable Metrics/LineLength
  server.wait_for(config[:glance_cache_wait_timeout]) do
    sleep(1)
    ready?
  end

  if config[:floating_ip]
    attach_ip(server, config[:floating_ip])
  elsif config[:floating_ip_pool]
    attach_ip_from_pool(server, config[:floating_ip_pool])
  end
  state[:hostname] = get_ip(server)
  wait_for_server(state)
  add_ohai_hint(state)
rescue Fog::Errors::Error, Excon::Errors::Error => ex
  raise ActionFailed, ex.message
end

#destroy(state) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/kitchen/driver/openstack.rb', line 106

def destroy(state)
  return if state[:server_id].nil?

  disable_ssl_validation if config[:disable_ssl_validation]
  server = compute.servers.get(state[:server_id])

  unless server.nil?
    if config[:floating_ip_pool] && config[:allocate_floating_ip]
      info 'Retrieve the floating IP'
      pub, priv = get_public_private_ips(server)
      pub, = parse_ips(pub, priv)
      pub_ip = pub[config[:public_ip_order].to_i] || nil
      if pub_ip
        info "Retrieve the ID of floating IP <#{pub_ip}>"
        floating_ip_id = network.list_floating_ips(floating_ip_address: pub_ip) # rubocop:disable Metrics/LineLength
                                .body['floatingips'][0]['id']
        network.delete_floating_ip(floating_ip_id)
        info "OpenStack Floating IP <#{pub_ip}> released."
      end
    end
    server.destroy
  end
  info "OpenStack instance <#{state[:server_id]}> destroyed."
  state.delete(:server_id)
  state.delete(:hostname)
end