class Fog::Compute::Libvirt::Server

Attributes

iso_dir[RW]
The following attributes are only needed when creating a new vm

TODO: Add depreciation warning

iso_file[RW]
The following attributes are only needed when creating a new vm

TODO: Add depreciation warning

network_bridge_name[RW]
network_interface_type[RW]
network_nat_network[RW]
password[RW]
private_key[W]
private_key_path[W]
public_key[W]
public_key_path[W]
username[W]
volume_allocation[RW]
volume_capacity[RW]
volume_format_type[RW]
volume_name[RW]
volume_path[RW]
volume_pool_name[RW]
volume_template_name[RW]
xml[R]

Public Class Methods

new(attributes={} ) click to toggle source

Can be created by passing in :xml => "<xml to create domain/server>" or by providing :template_options => {

             :name => "", :cpus => 1, :memory_size => 256 , :volume_template
}
# File lib/fog/libvirt/models/compute/server.rb, line 48
def initialize(attributes={} )
  @xml = attributes.delete(:xml)
  verify_boot_order(attributes[:boot_order])
  super defaults.merge(attributes)
  initialize_nics
  initialize_volumes
end

Public Instance Methods

destroy(options={ :destroy_volumes => false}) click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 89
def destroy(options={ :destroy_volumes => false})
  poweroff unless stopped?
  connection.vm_action(uuid, :undefine)
  volumes.each { |vol| vol.destroy } if options[:destroy_volumes]
  true
end
disk_path() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 85
def disk_path
  volumes.first.path if volumes and volumes.first
end
halt() click to toggle source

alias methods

Alias for: poweroff
mac() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 81
def mac
  nics.first.mac if nics && nics.first
end
new?() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 56
def new?
  uuid.nil?
end
poweroff() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 100
def poweroff
  connection.vm_action(uuid, :destroy)
end
Also aliased as: halt
private_ip_address() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 134
def private_ip_address
  ip_address(:private)
end
private_key() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 147
def private_key
  @private_key ||= private_key_path && File.read(private_key_path)
end
private_key_path() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 142
def private_key_path
  @private_key_path ||= Fog.credentials[:private_key_path]
  @private_key_path &&= File.expand_path(@private_key_path)
end
public_ip_address() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 138
def public_ip_address
  ip_address(:public)
end
public_key() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 156
def public_key
  @public_key ||= public_key_path && File.read(public_key_path)
end
public_key_path() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 151
def public_key_path
  @public_key_path ||= Fog.credentials[:public_key_path]
  @public_key_path &&= File.expand_path(@public_key_path)
end
ready?() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 120
def ready?
  state == "running"
end
reboot() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 96
def reboot
  connection.vm_action(uuid, :reboot)
end
resume() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 108
def resume
  connection.vm_action(uuid, :resume)
end
save() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 60
def save
  raise Fog::Errors::Error.new('Saving an existing server may create a duplicate') unless new?
  create_or_clone_volume unless xml or @volumes
  @xml ||= to_xml
  self.id = (persistent ? connection.define_domain(xml) : connection.create_domain(xml)).uuid
  reload
rescue => e
  raise Fog::Errors::Error.new("Error saving the server: #{e}")
end
scp(local_path, remote_path, upload_options = {}) click to toggle source

Transfers a file

# File lib/fog/libvirt/models/compute/server.rb, line 181
def scp(local_path, remote_path, upload_options = {})
  requires :public_ip_address, :username

  scp_options = {}
  scp_options[:password] = password unless self.password.nil?
  scp_options[:key_data] = [private_key] if self.private_key
  scp_options[:proxy]= ssh_proxy unless self.ssh_proxy.nil?

  Fog::SCP.new(public_ip_address, username, scp_options).upload(local_path, remote_path, upload_options)
end
setup(credentials = {}) click to toggle source

Sets up a new key

# File lib/fog/libvirt/models/compute/server.rb, line 193
def setup(credentials = {})
  requires :public_key, :public_ip_address, :username

  credentials[:proxy]= ssh_proxy unless ssh_proxy.nil?
  credentials[:password] = password unless self.password.nil?
  credentails[:key_data] = [private_key] if self.private_key

  commands = [
    %Q{mkdir .ssh},
    #              %{passwd -l #{username}}, #Not sure if we need this here
    #              %{echo "#{Fog::JSON.encode(attributes)}" >> ~/attributes.json}
  ]
  if public_key
    commands << %Q{echo "#{public_key}" >> ~/.ssh/authorized_keys}
  end

  # wait for domain to be ready
  Timeout::timeout(360) do
    begin
      Timeout::timeout(8) do
        Fog::SSH.new(public_ip_address, username, credentials.merge(:timeout => 4)).run('pwd')
      end
    rescue Errno::ECONNREFUSED
      sleep(2)
      retry
    rescue Net::SSH::AuthenticationFailed, Timeout::Error
      retry
    end
  end
  Fog::SSH.new(public_ip_address, username, credentials).run(commands)
end
shutdown() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 104
def shutdown
  connection.vm_action(uuid, :shutdown)
end
Also aliased as: stop
ssh(commands) click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 160
def ssh(commands)
  requires :public_ip_address, :username

  #requires :password, :private_key
  ssh_options={}
  ssh_options[:password] = password unless password.nil?
  ssh_options[:key_data] = [private_key] if private_key
  ssh_options[:proxy]= ssh_proxy unless ssh_proxy.nil?

  Fog::SSH.new(public_ip_address, @username, ssh_options).run(commands)

end
ssh_proxy() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 173
def ssh_proxy
  # if this is a direct connection, we don't need a proxy to be set.
  return nil unless @connection.uri.ssh_enabled?
  user_string= connection.uri.user ? "-l #{connection.uri.user}" : ""
  Net::SSH::Proxy::Command.new("ssh #{user_string} #{connection.uri.host} nc %h %p")
end
start() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 74
def start
  return true if active?
  connection.vm_action(uuid, :create)
  reload
  true
end
stop() click to toggle source
Alias for: shutdown
stopped?() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 116
def stopped?
  state == "shutoff"
end
suspend() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 112
def suspend
  connection.vm_action(uuid, :suspend)
end
update_display(attrs = {}) click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 225
def update_display attrs = {}
  connection.update_display attrs.merge(:uuid => uuid)
  reload
end
username() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 70
def username
  @username ||= 'root'
end
vnc_port() click to toggle source

can't use deprecate method, as the value is part of the display hash

# File lib/fog/libvirt/models/compute/server.rb, line 231
def vnc_port
  Fog::Logger.deprecation("#{self.class} => #vnc_port is deprecated, use #display[:port] instead [light_black](#{caller.first})[/]")
  display[:port]
end
volumes() click to toggle source
# File lib/fog/libvirt/models/compute/server.rb, line 129
def volumes
  # lazy loading of volumes
  @volumes ||= (@volumes_path || []).map{|path| connection.volumes.all(:path => path).first }
end