Class: Fog::Compute::Libvirt::Server

Inherits:
Server show all
Includes:
Fog::Compute::LibvirtUtil
Defined in:
lib/fog/libvirt/models/compute/server.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#collection, #connection

Instance Method Summary collapse

Methods included from Fog::Compute::LibvirtUtil

#randomized_name, #to_xml, #xml_element, #xml_elements

Methods inherited from Server

#scp_download, #ssh_port, #sshable?

Methods inherited from Model

#inspect, #reload, #symbolize_keys, #to_json, #wait_for

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #requires, #requires_one

Constructor Details

#initialize(attributes = {}) ⇒ Server

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
}


48
49
50
51
52
53
54
# 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

Instance Attribute Details

#iso_dirObject

The following attributes are only needed when creating a new vm TODO: Add depreciation warning



37
38
39
# File 'lib/fog/libvirt/models/compute/server.rb', line 37

def iso_dir
  @iso_dir
end

#iso_fileObject

The following attributes are only needed when creating a new vm TODO: Add depreciation warning



37
38
39
# File 'lib/fog/libvirt/models/compute/server.rb', line 37

def iso_file
  @iso_file
end

#network_bridge_nameObject

Returns the value of attribute network_bridge_name.



38
39
40
# File 'lib/fog/libvirt/models/compute/server.rb', line 38

def network_bridge_name
  @network_bridge_name
end

#network_interface_typeObject

Returns the value of attribute network_interface_type.



38
39
40
# File 'lib/fog/libvirt/models/compute/server.rb', line 38

def network_interface_type
  @network_interface_type
end

#network_nat_networkObject

Returns the value of attribute network_nat_network.



38
39
40
# File 'lib/fog/libvirt/models/compute/server.rb', line 38

def network_nat_network
  @network_nat_network
end

#passwordObject

Returns the value of attribute password.



40
41
42
# File 'lib/fog/libvirt/models/compute/server.rb', line 40

def password
  @password
end

#private_keyObject



147
148
149
# 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_pathObject



142
143
144
145
# 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_keyObject



156
157
158
# 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_pathObject



151
152
153
154
# 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

#usernameObject



70
71
72
# File 'lib/fog/libvirt/models/compute/server.rb', line 70

def username
  @username ||= 'root'
end

#volume_allocationObject

Returns the value of attribute volume_allocation.



39
40
41
# File 'lib/fog/libvirt/models/compute/server.rb', line 39

def volume_allocation
  @volume_allocation
end

#volume_capacityObject

Returns the value of attribute volume_capacity.



39
40
41
# File 'lib/fog/libvirt/models/compute/server.rb', line 39

def volume_capacity
  @volume_capacity
end

#volume_format_typeObject

Returns the value of attribute volume_format_type.



39
40
41
# File 'lib/fog/libvirt/models/compute/server.rb', line 39

def volume_format_type
  @volume_format_type
end

#volume_nameObject

Returns the value of attribute volume_name.



39
40
41
# File 'lib/fog/libvirt/models/compute/server.rb', line 39

def volume_name
  @volume_name
end

#volume_pathObject

Returns the value of attribute volume_path.



39
40
41
# File 'lib/fog/libvirt/models/compute/server.rb', line 39

def volume_path
  @volume_path
end

#volume_pool_nameObject

Returns the value of attribute volume_pool_name.



39
40
41
# File 'lib/fog/libvirt/models/compute/server.rb', line 39

def volume_pool_name
  @volume_pool_name
end

#volume_template_nameObject

Returns the value of attribute volume_template_name.



39
40
41
# File 'lib/fog/libvirt/models/compute/server.rb', line 39

def volume_template_name
  @volume_template_name
end

#xmlObject (readonly)

Returns the value of attribute xml.



12
13
14
# File 'lib/fog/libvirt/models/compute/server.rb', line 12

def xml
  @xml
end

Instance Method Details

#destroy(options = { :destroy_volumes => false}) ⇒ Object



89
90
91
92
93
94
# 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_pathObject



85
86
87
# File 'lib/fog/libvirt/models/compute/server.rb', line 85

def disk_path
  volumes.first.path if volumes and volumes.first
end

#macObject



81
82
83
# File 'lib/fog/libvirt/models/compute/server.rb', line 81

def mac
  nics.first.mac if nics && nics.first
end

#new?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/fog/libvirt/models/compute/server.rb', line 56

def new?
  uuid.nil?
end

#poweroffObject Also known as: halt



100
101
102
# File 'lib/fog/libvirt/models/compute/server.rb', line 100

def poweroff
  connection.vm_action(uuid, :destroy)
end

#private_ip_addressObject



134
135
136
# File 'lib/fog/libvirt/models/compute/server.rb', line 134

def private_ip_address
  ip_address(:private)
end

#public_ip_addressObject



138
139
140
# File 'lib/fog/libvirt/models/compute/server.rb', line 138

def public_ip_address
  ip_address(:public)
end

#ready?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/fog/libvirt/models/compute/server.rb', line 120

def ready?
  state == "running"
end

#rebootObject



96
97
98
# File 'lib/fog/libvirt/models/compute/server.rb', line 96

def reboot
  connection.vm_action(uuid, :reboot)
end

#resumeObject



108
109
110
# File 'lib/fog/libvirt/models/compute/server.rb', line 108

def resume
  connection.vm_action(uuid, :resume)
end

#saveObject



60
61
62
63
64
65
66
67
68
# 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 = {}) ⇒ Object

Transfers a file



181
182
183
184
185
186
187
188
189
190
# 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 = {}) ⇒ Object

Sets up a new key



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# 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 = [
    %{mkdir .ssh},
    #              %{passwd -l #{username}}, #Not sure if we need this here
    #              %{echo "#{Fog::JSON.encode(attributes)}" >> ~/attributes.json}
  ]
  if public_key
    commands << %{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

#shutdownObject Also known as: stop



104
105
106
# File 'lib/fog/libvirt/models/compute/server.rb', line 104

def shutdown
  connection.vm_action(uuid, :shutdown)
end

#ssh(commands) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
# 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_proxyObject



173
174
175
176
177
178
# 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

#startObject



74
75
76
77
78
79
# File 'lib/fog/libvirt/models/compute/server.rb', line 74

def start
  return true if active?
  connection.vm_action(uuid, :create)
  reload
  true
end

#stopped?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/fog/libvirt/models/compute/server.rb', line 116

def stopped?
  state == "shutoff"
end

#suspendObject



112
113
114
# File 'lib/fog/libvirt/models/compute/server.rb', line 112

def suspend
  connection.vm_action(uuid, :suspend)
end

#update_display(attrs = {}) ⇒ Object



225
226
227
228
# File 'lib/fog/libvirt/models/compute/server.rb', line 225

def update_display attrs = {}
  connection.update_display attrs.merge(:uuid => uuid)
  reload
end

#vnc_portObject

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



231
232
233
234
# 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

#volumesObject



129
130
131
132
# 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