Skip to content

Instantly share code, notes, and snippets.

@b
Created February 11, 2009 19:55
Show Gist options
  • Save b/62204 to your computer and use it in GitHub Desktop.
Save b/62204 to your computer and use it in GitHub Desktop.
{
"_content": {
"devices": {
"_content": [
{
"emulator": {
"_content": "\/usr\/bin\/kvm"
}
},
{
"disk": {
"_content": {
"source": {
"_attributes": {
"file": "\/srv\/kvm\/dev-ots02\/disk0.qcow2"
}
},
"target": {
"_attributes": {
"bus": "ide",
"dev": "hda"
}
}
},
"_attributes": {
"device": "disk",
"type": "file"
}
}
},
{
"interface": {
"_content": {
"mac": {
"_attributes": {
"address": "52:54:00:15:e9:80"
}
},
"model": {
"_attributes": {
"type": "virtio"
}
},
"source": {
"_attributes": {
"bridge": "br0.2"
}
},
"target": {
"_attributes": {
"dev": "vnet5"
}
}
},
"_attributes": {
"type": "bridge"
}
}
},
{
"interface": {
"_content": {
"mac": {
"_attributes": {
"address": "52:54:00:d0:56:73"
}
},
"model": {
"_attributes": {
"type": "virtio"
}
},
"source": {
"_attributes": {
"bridge": "br0.14"
}
},
"target": {
"_attributes": {
"dev": "vnet6"
}
}
},
"_attributes": {
"type": "bridge"
}
}
},
{
"serial": {
"_content": {
"source": {
"_attributes": {
"path": "\/dev\/pts\/5"
}
},
"target": {
"_attributes": {
"port": "0"
}
}
},
"_attributes": {
"type": "pty"
}
}
},
{
"console": {
"_content": {
"source": {
"_attributes": {
"path": "\/dev\/pts\/5"
}
},
"target": {
"_attributes": {
"port": "0"
}
}
},
"_attributes": {
"tty": "\/dev\/pts\/5",
"type": "pty"
}
}
},
"input",
"graphics"
]
},
"vcpu": {
"_content": "1"
},
"name": {
"_content": "dev-ots02"
},
"memory": {
"_content": "2048000"
},
"clock": {
"_attributes": {
"offset": "utc"
}
},
"os": {
"_content": {
"type": {
"_content": "hvm"
},
"boot": {
"_attributes": {
"dev": "hd"
}
}
}
},
"uuid": {
"_content": "46a3694d-605e-84f4-1411-d3a49d67cbdf"
},
"on_crash": {
"_content": "destroy"
},
"features": {
"_content": [
"acpi"
]
},
"currentMemory": {
"_content": "2048000"
},
"on_reboot": {
"_content": "restart"
},
"on_poweroff": {
"_content": "destroy"
}
},
"_attributes": {
"type": "kvm",
"id": "4"
}
}
<domain type='kvm' id='4'>
<name>dev-ots02</name>
<uuid>46a3694d-605e-84f4-1411-d3a49d67cbdf</uuid>
<memory>2048000</memory>
<currentMemory>2048000</currentMemory>
<vcpu>1</vcpu>
<os>
<type>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>
<emulator>/usr/bin/kvm</emulator>
<disk type='file' device='disk'>
<source file='/srv/kvm/dev-ots02/disk0.qcow2'/>
<target dev='hda' bus='ide'/>
</disk>
<interface type='bridge'>
<mac address='52:54:00:15:e9:80'/>
<source bridge='br0.2'/>
<target dev='vnet5'/>
<model type='virtio'/>
</interface>
<interface type='bridge'>
<mac address='52:54:00:d0:56:73'/>
<source bridge='br0.14'/>
<target dev='vnet6'/>
<model type='virtio'/>
</interface>
<serial type='pty'>
<source path='/dev/pts/5'/>
<target port='0'/>
</serial>
<console type='pty' tty='/dev/pts/5'>
<source path='/dev/pts/5'/>
<target port='0'/>
</console>
<input type='mouse' bus='ps2'/>
<graphics type='vnc' port='5902' listen='127.0.0.1'/>
</devices>
</domain>
def parse_xml_desc(xml)
# consumes the xml_desc blob from libvirt
# produces an intermediate form composed of ruby hashes and arrays
h = {}
h["_attributes"] = n.attributes if n.attributes.keys.length > 0
case n.children.length
when 0..1
h["_content"] = n.content if n.content.length > 0
h = nil unless (h["_content"] || h["_attributes"])
else
if %{ features devices }.include?(n.name)
h["_content"] = n.children.collect do |c|
next if c.name.eql?("text")
(c.children.length == 0 ? c.name : { c.name => parse_xml_desc(c) }) }).compact
end
else
h["_content"] = {}
n.children.each do |c|
next if c.name.eql?("text")
h["_content"][c.name] = parse_xml_desc(c)
end
end
end
h
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment