Class: Haze::VM

Inherits:
Object
  • Object
show all
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(options)
    path = '/tmp/'

    dom_xml = "      <domain type='qemu'>\n        <name>\#{options['name']}</name>\n        <memory>\#{options['memory']}</memory>\n        <currentMemory>\#{options['memory']}</currentMemory>\n        <vcpu>1</vcpu>\n        <os>\n          <type arch='i686' machine='pc'>hvm</type>\n          <boot dev='hd'/>\n        </os>\n        <features>\n          <acpi/>\n        </features>\n        <clock offset='utc'/>\n        <on_poweroff>destroy</on_poweroff>\n        <on_reboot>restart</on_reboot>\n        <on_crash>destroy</on_crash>\n        <devices>\n          <disk type='file' device='disk'>\n            <source file='\#{path + options['image'] + '.img'}'/>\n            <target dev='hda'/>\n          </disk>\n          <input type='mouse' bus='ps2'/> \n          \#{\"<graphics type='vnc' port='\#{options['vnc']['port']}' listen='\#{options['vnc']['ip']}'/>\" if options.has_key?('vnc') }\n        </devices>\n      </domain>\n    XML\n  puts dom_xml\n  begin\n    conn = Libvirt::open('qemu:///system')\n    dom = conn.create_domain_xml(dom_xml)\n    @response = { action: 'vm creation', status: 'OK' }\n  rescue Exception => e\n    puts \"\#{\"<graphics type='vnc' port='\#{options['vnc']['port']}' listen='\#{options['vnc']['ip']}'/>\" if options.has_key?('vnc') }\"\n    puts e\n    @response = { action: 'vm creation', status: 'ERROR' }\n  ensure\n    conn.close\n  end\n\n  puts @response\n  @response.to_json\nend\n"

.listObject



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(options)
  begin
    puts options
    conn = Libvirt::open('qemu:///system')
    conn.lookup_domain_by_id(options['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