class VagrantPlugins::Openstack::NovaClient

Constants

VM_STATES

Public Class Methods

new() click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 14
def initialize
  @logger = Log4r::Logger.new('vagrant_openstack::nova')
  @session = VagrantPlugins::Openstack.session
end

Public Instance Methods

add_floating_ip(env, server_id, floating_ip) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 139
def add_floating_ip(env, server_id, floating_ip)
  instance_exists do
    check_floating_ip(env, floating_ip)

    post(env, "#{@session.endpoints[:compute]}/servers/#{server_id}/action",
         { addFloatingIp: { address: floating_ip } }.to_json)
  end
end
allocate_floating_ip(env, pool) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 33
def allocate_floating_ip(env, pool)
  ips_json = post(env, "#{@session.endpoints[:compute]}/os-floating-ips",
                  {
                    pool: pool
                  }.to_json,
                  'X-Auth-Token' => @session.token,
                  :accept => :json,
                  :content_type => :json)
  floating_ip = JSON.parse(ips_json)['floating_ip']
  FloatingIP.new(floating_ip['ip'], floating_ip['pool'], floating_ip['instance_id'])
end
attach_volume(env, server_id, volume_id, device = nil) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 199
def attach_volume(env, server_id, volume_id, device = nil)
  instance_exists do
    attachment = post(env, "#{@session.endpoints[:compute]}/servers/#{server_id}/os-volume_attachments",
                      {
                        volumeAttachment: {
                          volumeId: volume_id,
                          device: device
                        }
                      }.to_json)
    JSON.parse(attachment)['volumeAttachment']
  end
end
check_assigned_floating_ip(env, server_id, floating_ip) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 148
def check_assigned_floating_ip(env, server_id, floating_ip)
  instance_exists do
    addresses = get_server_details(env, server_id)['addresses']
    addresses.each do |_, network|
      network.each do |network_detail|
        return true if network_detail['addr'] == floating_ip
      end
    end

    return false
  end
end
create_server(env, options) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 71
def create_server(env, options)
  server = {}.tap do |s|
    s['name'] = options[:name]
    if options[:image_ref].nil?
      s['block_device_mapping'] = [{ volume_id: options[:volume_boot][:id],
                                     device_name: options[:volume_boot][:device] }] if options[:volume_boot].key?(:id)
      s['block_device_mapping_v2'] = [{ boot_index: '0',
                                        volume_size: options[:volume_boot][:size],
                                        uuid: options[:volume_boot][:image],
                                        device_name: options[:volume_boot][:device],
                                        source_type: 'image',
                                        destination_type: 'volume',
                                        delete_on_termination: options[:volume_boot][:delete_on_destroy] }]\
                                        if options[:volume_boot].key?(:image)
    else
      s['imageRef'] = options[:image_ref]
    end
    s['flavorRef'] = options[:flavor_ref]
    s['key_name'] = options[:keypair] unless options[:keypair].nil?
    s['availability_zone'] = options[:availability_zone] unless options[:availability_zone].nil?
    s['security_groups'] = options[:security_groups] unless options[:security_groups].nil?
    s['user_data'] = Base64.encode64(options[:user_data]) unless options[:user_data].nil?
    s['metadata'] = options[:metadata] unless options[:metadata].nil?
    s['networks'] = options[:networks] unless options[:networks].nil? || options[:networks].empty?
  end
  object = { server: server }
  object['os:scheduler_hints'] = options[:scheduler_hints] unless options[:scheduler_hints].nil?
  server = post(env, "#{@session.endpoints[:compute]}/servers", object.to_json)
  JSON.parse(server)['server']['id']
end
create_snapshot(env, server_id, snapshot_name) click to toggle source

Create a named snapsot for a given VM

@param env [Hash] Vagrant action environment @param server_id [String] Server UUID @param snapshot_name [String]

@return [void]

# File lib/vagrant-openstack-provider/client/nova.rb, line 229
def create_snapshot(env, server_id, snapshot_name)
  instance_exists do
    post(
      env,
      "#{@session.endpoints[:compute]}/servers/#{server_id}/action",
      { createImage: {
        name: snapshot_name,
        metadata: { vagrant_snapshot: 'true' }
      } }.to_json)
  end
end
delete_keypair_if_vagrant(env, server_id) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 180
def delete_keypair_if_vagrant(env, server_id)
  instance_exists do
    keyname = get_server_details(env, server_id)['key_name']
    if keyname
      delete(env, "#{@session.endpoints[:compute]}/os-keypairs/#{keyname}") if keyname.start_with?('vagrant-generated-')
    end
  end
end
delete_server(env, server_id) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 102
def delete_server(env, server_id)
  instance_exists do
    delete(env, "#{@session.endpoints[:compute]}/servers/#{server_id}")
  end
end
delete_snapshot(env, snapshot_id) click to toggle source

Delete an identified snapshot

@param env [Hash] Vagrant action environment @param snapshot_id [String] Snapshot UUID

@return [void]

# File lib/vagrant-openstack-provider/client/nova.rb, line 247
def delete_snapshot(env, snapshot_id)
  delete(
    env,
    "#{@session.endpoints[:compute]}/images/#{snapshot_id}")
end
get_all_flavors(env) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 19
def get_all_flavors(env)
  flavors_json = get(env, "#{@session.endpoints[:compute]}/flavors/detail")
  JSON.parse(flavors_json)['flavors'].map do |fl|
    Flavor.new(fl['id'], fl['name'], fl['vcpus'], fl['ram'], fl['disk'])
  end
end
get_all_floating_ips(env) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 26
def get_all_floating_ips(env)
  ips_json = get(env, "#{@session.endpoints[:compute]}/os-floating-ips",
                 'X-Auth-Token' => @session.token,
                 :accept => :json)
  JSON.parse(ips_json)['floating_ips'].map { |n| FloatingIP.new(n['ip'], n['pool'], n['instance_id']) }
end
get_all_images(env, headers = {}) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 45
def get_all_images(env, headers = {})
  images_json = get(env, "#{@session.endpoints[:compute]}/images/detail", headers)
  JSON.parse(images_json)['images'].map do |fl|
    Image.new(
      fl['id'],
      fl['name'],
      'unknown',
      nil,
      fl['minRam'],
      fl['minDisk'],
      fl['metadata']
    )
  end
end
get_floating_ip_pools(env) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 189
def get_floating_ip_pools(env)
  floating_ips = get(env, "#{@session.endpoints[:compute]}/os-floating-ip-pools")
  JSON.parse(floating_ips)['floating_ip_pools']
end
get_floating_ips(env) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 194
def get_floating_ips(env)
  floating_ips = get(env, "#{@session.endpoints[:compute]}/os-floating-ips")
  JSON.parse(floating_ips)['floating_ips']
end
get_image_details(env, image_id) click to toggle source

Get detailed information about an image

@param env [Hash] Vagrant action environment @param image_id [String] Image UUID

@return [Hash]

# File lib/vagrant-openstack-provider/client/nova.rb, line 66
def get_image_details(env, image_id)
  image_json = get(env, "#{@session.endpoints[:compute]}/images/#{image_id}")
  JSON.parse(image_json)['image']
end
get_server_details(env, server_id) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 132
def get_server_details(env, server_id)
  instance_exists do
    server_details = get(env, "#{@session.endpoints[:compute]}/servers/#{server_id}")
    JSON.parse(server_details)['server']
  end
end
import_keypair(env, public_key) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 161
def import_keypair(env, public_key)
  keyname = "vagrant-generated-#{Kernel.rand(36**8).to_s(36)}"

  key_details = post(env, "#{@session.endpoints[:compute]}/os-keypairs",
                     { keypair:
                       {
                         name: keyname,
                         public_key: public_key
                       }
                     }.to_json)
  JSON.parse(key_details)['keypair']['name']
end
import_keypair_from_file(env, public_key_path) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 174
def import_keypair_from_file(env, public_key_path)
  fail "File specified in public_key_path #{public_key_path} doesn't exist" unless File.exist?(public_key_path)
  file = File.open(public_key_path)
  import_keypair(env, file.read)
end
list_snapshots(env, server_id) click to toggle source

List snapshot images associated with a particular server

@param env [Hash] Vagrant action environment @param server_id [String] Server UUID

@return [Array<VagrantPlugins::Openstack::Domain::Image>]

# File lib/vagrant-openstack-provider/client/nova.rb, line 218
def list_snapshots(env, server_id)
  get_all_images(env, params: { server: server_id })
end
restore_snapshot(env, server_id, snapshot_id) click to toggle source

Restore a VM to an identified snapshot

@param env [Hash] Vagrant action environment @param server_id [String] Server UUID @param snapshot_id [String] Snapshot UUID

@return [void]

# File lib/vagrant-openstack-provider/client/nova.rb, line 260
def restore_snapshot(env, server_id, snapshot_id)
  instance_exists do
    post(
      env,
      "#{@session.endpoints[:compute]}/servers/#{server_id}/action",
      { rebuild: { imageRef: snapshot_id } }.to_json)
  end
end
resume_server(env, server_id) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 114
def resume_server(env, server_id)
  instance_exists do
    change_server_state(env, server_id, :resume)
  end
end
start_server(env, server_id) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 126
def start_server(env, server_id)
  instance_exists do
    change_server_state(env, server_id, :start)
  end
end
stop_server(env, server_id) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 120
def stop_server(env, server_id)
  instance_exists do
    change_server_state(env, server_id, :stop)
  end
end
suspend_server(env, server_id) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 108
def suspend_server(env, server_id)
  instance_exists do
    change_server_state(env, server_id, :suspend)
  end
end

Private Instance Methods

change_server_state(env, server_id, new_state) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 286
def change_server_state(env, server_id, new_state)
  post(env, "#{@session.endpoints[:compute]}/servers/#{server_id}/action",
       { :"#{VM_STATES[new_state.to_sym]}" => nil }.to_json)
end
check_floating_ip(env, floating_ip) click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 291
def check_floating_ip(env, floating_ip)
  ip_details = get(env, "#{@session.endpoints[:compute]}/os-floating-ips")

  JSON.parse(ip_details)['floating_ips'].each do |ip|
    next unless ip['ip'] == floating_ip
    return if ip['instance_id'].nil?
    fail Errors::FloatingIPAlreadyAssigned, floating_ip: floating_ip
  end
  fail Errors::FloatingIPNotAvailable, floating_ip: floating_ip
end
instance_exists() { || ... } click to toggle source
# File lib/vagrant-openstack-provider/client/nova.rb, line 279
def instance_exists
  return yield
rescue Errors::VagrantOpenstackError => e
  raise Errors::InstanceNotFound if e.extra_data[:code] == 404
  raise e
end