Class: Haze::VM
- Inherits:
-
Object
- Object
- Haze::VM
- Defined in:
- lib/haze/vm.rb
Class Method Summary collapse
Class Method Details
.create(options) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/haze/vm.rb', line 9 def self.create() path = '/tmp/' dom_xml = <<-XML <domain type='qemu'> <name>#{['name']}</name> <memory>#{['memory']}</memory> <currentMemory>#{['memory']}</currentMemory> <vcpu>1</vcpu> <os> <type arch='i686' machine='pc'>hvm</type> <boot dev='hd'/> </os> <features> <acpi/> </features> <clock offset='utc'/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <devices> <disk type='file' device='disk'> <source file='#{path + ['image'] + '.img'}'/> <target dev='hda'/> </disk> <input type='mouse' bus='ps2'/> #{"<graphics type='vnc' port='#{['vnc']['port']}' listen='#{['vnc']['ip']}'/>" if .has_key?('vnc') } </devices> </domain> XML puts dom_xml begin conn = Libvirt::open('qemu:///system') dom = conn.create_domain_xml(dom_xml) @response = { action: 'vm creation', status: 'OK' } rescue Exception => e puts "#{"<graphics type='vnc' port='#{['vnc']['port']}' listen='#{['vnc']['ip']}'/>" if .has_key?('vnc') }" puts e @response = { action: 'vm creation', status: 'ERROR' } ensure conn.close end puts @response @response.to_json end |
.list ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/haze/vm.rb', line 72 def self.list conn = Libvirt::open('qemu:///system') doms = conn.list_domains.map do |id| dom = conn.lookup_domain_by_id(id) { id: id, name: dom.name, cpu_time: dom.info.cpu_time, vcpus: dom.get_vcpus.size, status: dom.state ? "running" : "paused", memory: dom.max_memory, type: dom.os_type } end conn.close { action:'vm listing', data: { vms: doms } }.to_json end |
.remove(options) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/haze/vm.rb', line 56 def self.remove() begin puts conn = Libvirt::open('qemu:///system') conn.lookup_domain_by_id(['id']).destroy response = { action: 'vm removal', status: 'OK'} rescue Exception => e puts e response = { action: 'vm removal', status: 'ERROR'} ensure conn.close end response.to_json end |