Skip to content

Instantly share code, notes, and snippets.

@b
Created February 16, 2009 22:04
Show Gist options
  • Save b/65401 to your computer and use it in GitHub Desktop.
Save b/65401 to your computer and use it in GitHub Desktop.
pure evil.rb transforms the pretty form into the intermediate form for xml generation
{
"domain": {
"_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": {
"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"
}
}
},
{
"serial": {
"_content": [
{
"source": {
"_attributes": {
"service": "2445",
"mode": "bind",
"host": "0.0.0.0"
}
}
},
{
"source": {
"_attributes": {
"service": "2445",
"mode": "connect",
"host": "0.0.0.0"
}
}
},
{
"target": {
"_attributes": {
"port": "1"
}
}
}
],
"_attributes": {
"type": "udp"
}
}
},
{
"console": {
"_content": [
{
"source": {
"_attributes": {
"path": "\/dev\/pts\/5"
}
}
},
{
"target": {
"_attributes": {
"port": "0"
}
}
}
],
"_attributes": {
"tty": "\/dev\/pts\/5",
"type": "pty"
}
}
},
{
"input": {
"_attributes": {
"bus": "ps2",
"type": "mouse"
}
}
},
{
"graphics": {
"_attributes": {
"type": "vnc",
"port": "5902",
"listen": "127.0.0.1"
}
}
},
{
"hostdev": {
"_content": [
{
"source": {
"_content": [
{
"product": {
"_attributes": {
"id": "0xbeef"
}
}
},
{
"vendor": {
"_attributes": {
"id": "0x1234"
}
}
}
]
}
}
],
"_attributes": {
"mode": "subsystem",
"type": "usb"
}
}
},
{
"hostdev": {
"_content": [
{
"source": {
"_content": [
{
"address": {
"_content": "bus='0x06' slot='0x02' function='0x0'"
}
}
]
}
}
],
"_attributes": {
"mode": "subsystem",
"type": "pci"
}
}
}
]
}
},
{
"vcpu": {
"_content": "1"
}
},
{
"name": {
"_content": "dev-ots02"
}
},
{
"memory": {
"_content": "2048000"
}
},
{
"clock": {
"_attributes": {
"offset": "utc"
}
}
},
{
"os": {
"_content": [
{
"loader": {
"_content": "\/usr\/lib\/xen\/boot\/hvmloader"
}
},
{
"type": {
"_content": "hvm"
}
},
{
"boot": {
"_attributes": {
"dev": "hd"
}
}
}
]
}
},
{
"uuid": {
"_content": "46a3694d-605e-84f4-1411-d3a49d67cbdf"
}
},
{
"on_crash": {
"_content": "destroy"
}
},
{
"features": {
"_content": [
{
"acpi": {
"_content": [
]
}
}
]
}
},
{
"currentMemory": {
"_content": "2048000"
}
},
{
"on_reboot": {
"_content": "restart"
}
},
{
"on_poweroff": {
"_content": "destroy"
}
}
],
"_attributes": {
"type": "kvm",
"id": "4"
}
}
}
{
"domain": {
"devices": [
{
"emulator": "\/usr\/bin\/kvm"
},
{
"disk": {
"device": "disk",
"type": "file",
"source": {
"file": "\/srv\/kvm\/dev-ots02\/disk0.qcow2"
},
"target": {
"bus": "ide",
"dev": "hda"
}
}
},
{
"interface": {
"type": "bridge",
"mac": {
"address": "52:54:00:15:e9:80"
},
"model": {
"type": "virtio"
},
"source": {
"bridge": "br0.2"
},
"target": {
"dev": "vnet5"
}
}
},
{
"interface": {
"type": "bridge",
"mac": {
"address": "52:54:00:d0:56:73"
},
"model": {
"type": "virtio"
},
"source": {
"bridge": "br0.14"
},
"target": {
"dev": "vnet6"
}
}
},
{
"serial": {
"type": "pty",
"source": {
"path": "\/dev\/pts\/5"
},
"target": {
"port": "0"
}
}
},
{
"serial": [
{
"source": {
"service": "2445",
"mode": "bind",
"host": "0.0.0.0"
}
},
{
"source": {
"service": "2445",
"mode": "connect",
"host": "0.0.0.0"
}
},
{
"target": {
"port": "1"
}
},
{
"type": "udp"
}
]
},
{
"console": {
"tty": "\/dev\/pts\/5",
"type": "pty",
"source": {
"path": "\/dev\/pts\/5"
},
"target": {
"port": "0"
}
}
},
{
"input": {
"bus": "ps2",
"type": "mouse"
}
},
{
"graphics": {
"type": "vnc",
"port": "5902",
"listen": "127.0.0.1"
}
},
{
"hostdev": {
"mode": "subsystem",
"type": "usb",
"source": {
"product": {
"id": "0xbeef"
},
"vendor": {
"id": "0x1234"
}
}
}
},
{
"hostdev": {
"mode": "subsystem",
"type": "pci",
"source": {
"address": "bus='0x06' slot='0x02' function='0x0'"
}
}
}
],
"vcpu": "1",
"name": "dev-ots02",
"memory": "2048000",
"clock": {
"offset": "utc"
},
"type": "kvm",
"os": {
"loader": "\/usr\/lib\/xen\/boot\/hvmloader",
"type": "hvm",
"boot": {
"dev": "hd"
}
},
"id": "4",
"uuid": "46a3694d-605e-84f4-1411-d3a49d67cbdf",
"on_crash": "destroy",
"features": {
"acpi": {
}
},
"currentMemory": "2048000",
"on_reboot": "restart",
"on_poweroff": "destroy"
}
}
def exp_dom(hash)
exp_dom_r(hash.keys.first, hash.values.first)
end
def exp_dom_r(key, hash)
case key
when "domain", "disk", "interface", "hostdev", "console"
h = { key => { "_attributes" => hash.inject({}) do |memo, kv|
memo[kv[0]] = kv[1] if %{ type id mode tty }.include?(kv[0])
memo
end
}
}
h[key].merge!(
{ "_content" => (hash.keys.map do |k|
exp_dom_r(k, hash[k]) unless %{ type id mode tty }.include?(k)
end).compact
}
)
return h
when "acpi", "name", "uuid", "memory",
"currentMemory", "vcpu", "on_poweroff",
"on_reboot", "on_crash", "emulator",
"loader", "bootloader", "bootloader_args",
"kernel", "initrd", "cmdline", "address"
return { key => { "_content" => (hash.length > 0 ? hash : []) } }
when "clock", "script", "target", "model",
"vendor", "product", "mac", "input",
"graphics", "path", "port", "boot",
"driver", "wiremode", "mode"
return { key => { "_attributes" => hash } }
when "os", "features"
return { key => { "_content" => (hash.keys.map do |k|
exp_dom_r(k, hash[k])
end).compact
}
}
when "serial"
if hash.is_a?(Array)
h = { key => { "_attributes" => hash.inject({}) do |memo, kv|
memo[kv.keys.first] = kv.values.first if %{ type id mode tty service host }.include?(kv.keys.first)
memo
end
}
}
h[key].merge!(
{ "_content" => (hash.map do |k|
exp_dom_r(k.keys.first, k.values.first) unless %{ type id mode tty service host }.include?(k.keys.first)
end).compact
}
)
return h
else
h = { key => { "_attributes" => hash.inject({}) do |memo, kv|
memo[kv[0]] = kv[1] if %{ type id mode tty service host }.include?(kv[0])
memo
end
}
}
h[key].merge!(
{ "_content" => (hash.keys.map do |k|
exp_dom_r(k, hash[k]) unless %{ type id mode tty service host }.include?(k)
end).compact
}
)
return h
end
when "devices"
if hash.is_a?(Array)
return { key => { "_content" => (hash.map do |k|
exp_dom_r(k.keys.first, k.values.first)
end).compact
}
}
else
return { key => { "_content" => (hash.keys.map do |k|
exp_dom_r(k, hash[k])
end).compact
}
}
end
else
return { key => { "_content" => hash } } if key.eql?("type")
if key.eql?("source")
if (hash.keys & ['product', 'vendor', 'address']).length > 0
return { key => { "_content" => (hash.keys.map do |k|
exp_dom_r(k, hash[k])
end).compact
}
}
else
return { key => { "_attributes" => hash } }
end
end
end
end
<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>
<loader>/usr/lib/xen/boot/hvmloader</loader>
<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>
<serial type="udp">
<source mode="bind" host="0.0.0.0" service="2445"/>
<source mode="connect" host="0.0.0.0" service="2445"/>
<target port="1"/>
</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'/>
<hostdev mode='subsystem' type='usb'>
<source>
<vendor id='0x1234'/>
<product id='0xbeef'/>
</source>
</hostdev>
<hostdev mode='subsystem' type='pci'>
<source>
<address>bus='0x06' slot='0x02' function='0x0'</address>
</source>
</hostdev>
</devices>
</domain>
<domain type="kvm" id="4">
<devices>
<emulator>/usr/bin/kvm</emulator>
<disk type="file">
<source file="/srv/kvm/dev-ots02/disk0.qcow2"/>
<target bus="ide" dev="hda"/>
</disk>
<interface type="bridge">
<mac address="52:54:00:15:e9:80"/>
<model type="virtio"/>
<source bridge="br0.2"/>
<target dev="vnet5"/>
</interface>
<interface type="bridge">
<mac address="52:54:00:d0:56:73"/>
<model type="virtio"/>
<source bridge="br0.14"/>
<target dev="vnet6"/>
</interface>
<serial type="pty">
<source path="/dev/pts/5"/>
<target port="0"/>
</serial>
<serial type="udp">
<source service="2445" mode="bind" host="0.0.0.0"/>
<source service="2445" mode="connect" host="0.0.0.0"/>
<target port="1"/>
</serial>
<console tty="/dev/pts/5" type="pty">
<source path="/dev/pts/5"/>
<target port="0"/>
</console>
<input bus="ps2" type="mouse"/>
<graphics type="vnc" port="5902" listen="127.0.0.1"/>
<hostdev mode="subsystem" type="usb">
<source>
<product id="0xbeef"/>
<vendor id="0x1234"/>
</source>
</hostdev>
<hostdev mode="subsystem" type="pci">
<source>
<address>bus='0x06' slot='0x02' function='0x0'</address>
</source>
</hostdev>
</devices>
<vcpu>1</vcpu>
<name>dev-ots02</name>
<memory>2048000</memory>
<clock offset="utc"/>
<os>
<loader>/usr/lib/xen/boot/hvmloader</loader>
<type>hvm</type>
<boot dev="hd"/>
</os>
<uuid>46a3694d-605e-84f4-1411-d3a49d67cbdf</uuid>
<on_crash>destroy</on_crash>
<features>
<acpi/>
</features>
<currentMemory>2048000</currentMemory>
<on_reboot>restart</on_reboot>
<on_poweroff>destroy</on_poweroff>
</domain>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment