Class: Fog::Libvirt::Compute::Server
- Inherits:
-
Compute::Server
- Object
- Compute::Server
- Fog::Libvirt::Compute::Server
- Includes:
- Util
- Defined in:
- lib/fog/libvirt/models/compute/server.rb
Instance Attribute Summary collapse
-
#iso_dir ⇒ Object
The following attributes are only needed when creating a new vm TODO: Add depreciation warning.
-
#iso_file ⇒ Object
The following attributes are only needed when creating a new vm TODO: Add depreciation warning.
-
#network_bridge_name ⇒ Object
Returns the value of attribute network_bridge_name.
-
#network_interface_type ⇒ Object
Returns the value of attribute network_interface_type.
-
#network_nat_network ⇒ Object
Returns the value of attribute network_nat_network.
-
#password ⇒ Object
Returns the value of attribute password.
-
#user_data ⇒ Object
Returns the value of attribute user_data.
-
#volume_allocation ⇒ Object
Returns the value of attribute volume_allocation.
-
#volume_capacity ⇒ Object
Returns the value of attribute volume_capacity.
-
#volume_format_type ⇒ Object
Returns the value of attribute volume_format_type.
-
#volume_name ⇒ Object
Returns the value of attribute volume_name.
-
#volume_path ⇒ Object
Returns the value of attribute volume_path.
-
#volume_pool_name ⇒ Object
Returns the value of attribute volume_pool_name.
-
#volume_template_name ⇒ Object
Returns the value of attribute volume_template_name.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Instance Method Summary collapse
- #cloud_init_volume_name ⇒ Object
- #create_user_data_iso ⇒ Object
- #destroy(options = { :destroy_volumes => false, :flags => ::Libvirt::Domain::UNDEFINE_NVRAM }) ⇒ Object
- #disk_path ⇒ Object
- #generate_config_iso(user_data, &blk) ⇒ Object
- #generate_config_iso_in_dir(dir_path, user_data, &blk) ⇒ Object
-
#initialize(attributes = {}) ⇒ Server
constructor
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 }.
- #mac ⇒ Object
- #new? ⇒ Boolean
- #poweroff ⇒ Object (also: #halt)
- #private_ip_address ⇒ Object
- #public_ip_address ⇒ Object
- #ready? ⇒ Boolean
- #reboot ⇒ Object
- #resume ⇒ Object
- #save ⇒ Object
-
#scp(local_path, remote_path, upload_options = {}) ⇒ Object
Transfers a file.
-
#setup(credentials = {}) ⇒ Object
Sets up a new key.
- #shutdown ⇒ Object (also: #stop)
- #ssh(commands) ⇒ Object
- #ssh_proxy ⇒ Object
- #start ⇒ Object
- #stopped? ⇒ Boolean
- #suspend ⇒ Object
-
#to_xml ⇒ Object
rubocop:disable Metrics.
- #update_autostart(value) ⇒ Object
- #update_display(attrs = {}) ⇒ Object
-
#vnc_port ⇒ Object
can’t use deprecate method, as the value is part of the display hash.
- #volumes ⇒ Object
Methods included from Util
#randomized_name, #xml_element, #xml_elements
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
}
55 56 57 58 59 60 61 62 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 55 def initialize(attributes={} ) @xml = attributes.delete(:xml) verify_boot_order(attributes[:boot_order]) super defaults.merge(attributes) initialize_nics initialize_volumes @user_data = attributes.delete(:user_data) end |
Instance Attribute Details
#iso_dir ⇒ Object
The following attributes are only needed when creating a new vm TODO: Add depreciation warning
44 45 46 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 44 def iso_dir @iso_dir end |
#iso_file ⇒ Object
The following attributes are only needed when creating a new vm TODO: Add depreciation warning
44 45 46 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 44 def iso_file @iso_file end |
#network_bridge_name ⇒ Object
Returns the value of attribute network_bridge_name.
45 46 47 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 45 def network_bridge_name @network_bridge_name end |
#network_interface_type ⇒ Object
Returns the value of attribute network_interface_type.
45 46 47 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 45 def network_interface_type @network_interface_type end |
#network_nat_network ⇒ Object
Returns the value of attribute network_nat_network.
45 46 47 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 45 def network_nat_network @network_nat_network end |
#password ⇒ Object
Returns the value of attribute password.
47 48 49 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 47 def password @password end |
#user_data ⇒ Object
Returns the value of attribute user_data.
48 49 50 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 48 def user_data @user_data end |
#volume_allocation ⇒ Object
Returns the value of attribute volume_allocation.
46 47 48 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 46 def volume_allocation @volume_allocation end |
#volume_capacity ⇒ Object
Returns the value of attribute volume_capacity.
46 47 48 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 46 def volume_capacity @volume_capacity end |
#volume_format_type ⇒ Object
Returns the value of attribute volume_format_type.
46 47 48 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 46 def volume_format_type @volume_format_type end |
#volume_name ⇒ Object
Returns the value of attribute volume_name.
46 47 48 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 46 def volume_name @volume_name end |
#volume_path ⇒ Object
Returns the value of attribute volume_path.
46 47 48 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 46 def volume_path @volume_path end |
#volume_pool_name ⇒ Object
Returns the value of attribute volume_pool_name.
46 47 48 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 46 def volume_pool_name @volume_pool_name end |
#volume_template_name ⇒ Object
Returns the value of attribute volume_template_name.
46 47 48 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 46 def volume_template_name @volume_template_name end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
10 11 12 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 10 def xml @xml end |
Instance Method Details
#cloud_init_volume_name ⇒ Object
276 277 278 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 276 def cloud_init_volume_name "#{name}-cloud-init.iso" end |
#create_user_data_iso ⇒ Object
267 268 269 270 271 272 273 274 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 267 def create_user_data_iso generate_config_iso(user_data) do |iso| vol = service.volumes.create(:name => cloud_init_volume_name, :capacity => "#{File.size(iso)}b", :allocation => "0G") vol.upload_image(iso) @iso_file = cloud_init_volume_name @iso_dir = File.dirname(vol.path) if vol.path end end |
#destroy(options = { :destroy_volumes => false, :flags => ::Libvirt::Domain::UNDEFINE_NVRAM }) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 98 def destroy(={ :destroy_volumes => false, :flags => ::Libvirt::Domain::UNDEFINE_NVRAM }) poweroff unless stopped? flags = .fetch(:flags, ::Libvirt::Domain::UNDEFINE_NVRAM) if flags.zero? service.vm_action(uuid, :undefine) else # the test driver doesn't support UNDEFINE_NVRAM if service.uri.driver == 'test' flags ^= ::Libvirt::Domain::UNDEFINE_NVRAM end service.vm_action(uuid, :undefine, flags) end volumes.each { |vol| vol.destroy } if [:destroy_volumes] true end |
#disk_path ⇒ Object
94 95 96 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 94 def disk_path volumes.first.path if volumes and volumes.first end |
#generate_config_iso(user_data, &blk) ⇒ Object
250 251 252 253 254 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 250 def generate_config_iso(user_data, &blk) Dir.mktmpdir('config') do |wd| generate_config_iso_in_dir(wd, user_data, &blk) end end |
#generate_config_iso_in_dir(dir_path, user_data, &blk) ⇒ Object
256 257 258 259 260 261 262 263 264 265 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 256 def generate_config_iso_in_dir(dir_path, user_data, &blk) FileUtils.touch(File.join(dir_path, "meta-data")) File.open(File.join(dir_path, 'user-data'), 'w') { |f| f.write user_data } isofile = Tempfile.new(['init', '.iso']).path unless system("genisoimage -output #{isofile} -volid cidata -joliet -rock #{File.join(dir_path, 'user-data')} #{File.join(dir_path, 'meta-data')}") raise Fog::Errors::Error.new("Couldn't generate cloud-init iso disk with genisoimage.") end blk.call(isofile) end |
#mac ⇒ Object
90 91 92 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 90 def mac nics&.first&.mac end |
#new? ⇒ Boolean
64 65 66 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 64 def new? uuid.nil? end |
#poweroff ⇒ Object Also known as: halt
120 121 122 123 124 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 120 def poweroff action_status = service.vm_action(uuid, :destroy) reload action_status end |
#private_ip_address ⇒ Object
163 164 165 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 163 def private_ip_address ip_address(:private) end |
#public_ip_address ⇒ Object
167 168 169 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 167 def public_ip_address ip_address(:public) end |
#ready? ⇒ Boolean
148 149 150 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 148 def ready? state == "running" end |
#reboot ⇒ Object
114 115 116 117 118 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 114 def reboot action_status = service.vm_action(uuid, :reboot) reload action_status end |
#resume ⇒ Object
132 133 134 135 136 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 132 def resume action_status = service.vm_action(uuid, :resume) reload action_status end |
#save ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 68 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 create_user_data_iso if user_data @xml ||= to_xml self.id = (persistent ? service.define_domain(xml) : service.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
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 195 def scp(local_path, remote_path, = {}) requires :ssh_ip_address, :username = {} [:password] = password unless self.password.nil? [:key_data] = [private_key] if self.private_key [:proxy]= ssh_proxy unless self.ssh_proxy.nil? Fog::SCP.new(ssh_ip_address, username, ).upload(local_path, remote_path, ) end |
#setup(credentials = {}) ⇒ Object
Sets up a new key
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 207 def setup(credentials = {}) requires :public_key, :ssh_ip_address, :username credentials[:proxy]= ssh_proxy unless ssh_proxy.nil? credentials[:password] = password unless self.password.nil? credentials[: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(ssh_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(ssh_ip_address, username, credentials).run(commands) end |
#shutdown ⇒ Object Also known as: stop
126 127 128 129 130 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 126 def shutdown action_status = service.vm_action(uuid, :shutdown) reload action_status end |
#ssh(commands) ⇒ Object
171 172 173 174 175 176 177 178 179 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 171 def ssh(commands) requires :ssh_ip_address, :username ={} [:password] = password unless password.nil? [:proxy]= ssh_proxy unless ssh_proxy.nil? super(commands, ) end |
#ssh_proxy ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 181 def ssh_proxy begin require 'net/ssh/proxy/command' rescue LoadError Fog::Logger.warning("'net/ssh' missing, please install and try again.") exit(1) end # if this is a direct connection, we don't need a proxy to be set. return nil unless connection.uri.ssh_enabled? user_string= service.uri.user ? "-l #{service.uri.user}" : "" Net::SSH::Proxy::Command.new("ssh #{user_string} #{service.uri.host} nc %h %p") end |
#start ⇒ Object
79 80 81 82 83 84 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 79 def start return true if active? action_status = service.vm_action(uuid, :create) reload action_status end |
#stopped? ⇒ Boolean
144 145 146 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 144 def stopped? state == "shutoff" end |
#suspend ⇒ Object
138 139 140 141 142 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 138 def suspend action_status = service.vm_action(uuid, :suspend) reload action_status end |
#to_xml ⇒ Object
rubocop:disable Metrics
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 281 def to_xml builder = Nokogiri::XML::Builder.new do |xml| xml.domain(:type => domain_type) do xml.name(name) xml.memory(memory_size) if hugepages xml.memoryBacking do xml.hugepages end end xml.vcpu(cpus) = {} [:firmware] = firmware if firmware == 'efi' xml.os(**) do type = xml.type(os_type, :arch => arch) type[:machine] = "q35" if ["i686", "x86_64"].include?(arch) boot_order.each do |dev| xml.boot(:dev => dev) end loader_attributes&.each do |key, value| xml.loader(key => value) end if firmware == "efi" && firmware_features&.any? xml.firmware do firmware_features.each_pair do |key, value| xml.feature(:name => key, :enabled => value) end end end end xml.features do xml.acpi xml.apic end unless cpu.empty? if cpu.dig(:model, :name) xml.cpu do xml.model(cpu.dig(:model, :name), :fallback => cpu.dig(:model, :fallback) || "allow") end else xml.cpu( :mode => cpu.fetch(:mode, "host-passthrough"), :check => cpu.fetch(:check, "none"), :migratable => cpu.fetch(:migratable, "on") ) end end xml.clock(:offset => "utc") do xml.timer(:name => "rtc", :tickpolicy => "catchup") xml.timer(:name => "pit", :tickpolicy => "delay") xml.timer(:name => "hpet", :present => "no") end xml.devices do ceph_args = read_ceph_args volumes.each_with_index do |volume, index| target_device = "vd#{('a'..'z').to_a[index]}" if ceph_args && volume.pool_name.include?(ceph_args["libvirt_ceph_pool"]) xml.disk(:type => "network", :device => "disk") do xml.driver(:name => "qemu", :type => volume.format_type, :cache => "writeback", :discard => "unmap") xml.source(:protocol => "rbd", :name => volume.path) ceph_args["monitor"]&.split(",")&.each do |monitor| xml.host(:name => monitor, :port => ceph_args["port"]) end xml.auth(:username => ceph_args["auth_username"]) do if ceph_args.key?("auth_uuid") xml.secret(:type => "ceph", :uuid => ceph_args["auth_uuid"]) else xml.secret(:type => "ceph", :usage => ceph_args["auth_usage"]) end end xml.target(:dev => target_device, :bus => ceph_args["bus_type"] == "virtio" ? "virtio" : "scsi") end else is_block = volume.path.start_with?("/dev/") xml.disk(:type => is_block ? "block" : "file", :device => "disk") do xml.driver(:name => "qemu", :type => volume.format_type) if is_block xml.source(:dev => volume.path) else xml.source(:file => volume.path) end xml.target(:dev => target_device, :bus => "virtio") end end end if iso_file xml.disk(:type => "file", :device => "cdrom") do xml.driver(:name => "qemu", :type => "raw") xml.source(:file => "#{iso_dir}/#{iso_file}") xml.target(:dev => "sda", :bus => "scsi") xml.readonly xml.address(:type => "drive", :controller => 0, :bus => 0, :unit => 0) end end nics.each do |nic| xml.interface(:type => nic.type) do xml.mac(:address => nic.mac) if nic.mac if nic.type == "bridge" xml.source(:bridge => nic.bridge) else xml.source(:network => nic.network) end xml.model(:type => nic.model) end end if guest_agent xml.channel(:type => "unix") do xml.target(:type => "virtio", :name => "org.qemu.guest_agent.0") end end xml.rng(:model => "virtio") do xml.backend(virtio_rng[:backend_path], :model => virtio_rng.fetch(:backend_model, "random")) end if arch == "s390x" xml.controller(:type => "scsi", :index => "0", :model => "virtio-scsi") xml.console(:type => "pty") do xml.target(:type => "sclp") end xml.memballoon(:model => "virtio") else xml.serial(:type => "pty") do xml.target(:port => 0) end xml.console(:type => "pty") do xml.target(:port => 0) end xml.input(:type => "tablet", :bus => "usb") xml.input(:type => "mouse", :bus => "ps2") graphics = xml.graphics(:type => display[:type]) if display[:port].empty? graphics.port = display[:port] graphics.autoport = "no" else graphics.port = -1 graphics.autoport = "yes" end graphics.listen = display[:listen] unless display[:listen].empty? graphics.passwd = display[:password] if display[:password] && !display[:password].empty? xml.video do xml.model(video) end end end end end builder.to_xml end |
#update_autostart(value) ⇒ Object
86 87 88 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 86 def update_autostart(value) service.update_autostart(uuid, value) end |
#update_display(attrs = {}) ⇒ Object
239 240 241 242 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 239 def update_display attrs = {} service.update_display attrs.merge(:uuid => uuid) reload end |
#vnc_port ⇒ Object
can’t use deprecate method, as the value is part of the display hash
245 246 247 248 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 245 def vnc_port Fog::Logger.deprecation("#{self.class} => #vnc_port is deprecated, use #display[:port] instead [light_black](#{caller.first})[/]") display[:port] end |
#volumes ⇒ Object
158 159 160 161 |
# File 'lib/fog/libvirt/models/compute/server.rb', line 158 def volumes # lazy loading of volumes @volumes ||= (@volumes_path || []).map{ |path| service.volumes.all(:path => path).first }.compact end |