The following attributes are only needed when creating a new vm
TODO: Add depreciation warning
The following attributes are only needed when creating a new vm
TODO: Add depreciation warning
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 47 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
# File lib/fog/libvirt/models/compute/server.rb, line 254 def cloud_init_volume_name "#{name}-cloud-init.iso" end
# File lib/fog/libvirt/models/compute/server.rb, line 245 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
# File lib/fog/libvirt/models/compute/server.rb, line 86 def destroy(options={ :destroy_volumes => false}) poweroff unless stopped? service.vm_action(uuid, :undefine) volumes.each { |vol| vol.destroy } if options[:destroy_volumes] true end
# File lib/fog/libvirt/models/compute/server.rb, line 82 def disk_path volumes.first.path if volumes and volumes.first end
# File lib/fog/libvirt/models/compute/server.rb, line 228 def generate_config_iso(user_data, &blk) Dir.mktmpdir('config') do |wd| generate_config_iso_in_dir(wd, user_data, &blk) end end
# File lib/fog/libvirt/models/compute/server.rb, line 234 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
# File lib/fog/libvirt/models/compute/server.rb, line 78 def mac nics.first.mac if nics && nics.first end
# File lib/fog/libvirt/models/compute/server.rb, line 56 def new? uuid.nil? end
# File lib/fog/libvirt/models/compute/server.rb, line 99 def poweroff action_status = service.vm_action(uuid, :destroy) reload action_status end
# File lib/fog/libvirt/models/compute/server.rb, line 141 def private_ip_address ip_address(:private) end
# File lib/fog/libvirt/models/compute/server.rb, line 145 def public_ip_address ip_address(:public) end
# File lib/fog/libvirt/models/compute/server.rb, line 127 def ready? state == "running" end
# File lib/fog/libvirt/models/compute/server.rb, line 93 def reboot action_status = service.vm_action(uuid, :reboot) reload action_status end
# File lib/fog/libvirt/models/compute/server.rb, line 111 def resume action_status = service.vm_action(uuid, :resume) reload action_status end
# 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 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
Transfers a file
# File lib/fog/libvirt/models/compute/server.rb, line 173 def scp(local_path, remote_path, upload_options = {}) requires :ssh_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(ssh_ip_address, username, scp_options).upload(local_path, remote_path, upload_options) end
Sets up a new key
# File lib/fog/libvirt/models/compute/server.rb, line 185 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 = [ %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(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
# File lib/fog/libvirt/models/compute/server.rb, line 105 def shutdown action_status = service.vm_action(uuid, :shutdown) reload action_status end
# File lib/fog/libvirt/models/compute/server.rb, line 149 def ssh(commands) requires :ssh_ip_address, :username ssh_options={} ssh_options[:password] = password unless password.nil? ssh_options[:proxy]= ssh_proxy unless ssh_proxy.nil? super(commands, ssh_options) end
# File lib/fog/libvirt/models/compute/server.rb, line 159 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
# File lib/fog/libvirt/models/compute/server.rb, line 71 def start return true if active? action_status = service.vm_action(uuid, :create) reload action_status end
# File lib/fog/libvirt/models/compute/server.rb, line 123 def stopped? state == "shutoff" end
# File lib/fog/libvirt/models/compute/server.rb, line 117 def suspend action_status = service.vm_action(uuid, :suspend) reload action_status end
# File lib/fog/libvirt/models/compute/server.rb, line 217 def update_display attrs = {} service.update_display attrs.merge(:uuid => uuid) reload end
can’t use deprecate method, as the value is part of the display hash
# File lib/fog/libvirt/models/compute/server.rb, line 223 def vnc_port Fog::Logger.deprecation("#{self.class} => #vnc_port is deprecated, use #display[:port] instead [light_black](#{caller.first})[/]") display[:port] end
# File lib/fog/libvirt/models/compute/server.rb, line 136 def volumes # lazy loading of volumes @volumes ||= (@volumes_path || []).map{|path| service.volumes.all(:path => path).first } end