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 = <<-XML
      <domain type='qemu'>
        <name>#{options['name']}</name>
        <memory>#{options['memory']}</memory>
        <currentMemory>#{options['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 + options['image'] + '.img'}'/>
            <target dev='hda'/>
          </disk>
          <input type='mouse' bus='ps2'/> 
          #{"<graphics type='vnc' port='#{options['vnc']['port']}' listen='#{options['vnc']['ip']}'/>" if options.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='#{options['vnc']['port']}' listen='#{options['vnc']['ip']}'/>" if options.has_key?('vnc') }"
    puts e
    @response = { action: 'vm creation', status: 'ERROR' }
  ensure
    conn.close
  end

  puts @response
  @response.to_json
end

.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