guest_xml.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from models import Config, Disk
  4. from models import Guest
  5. __author__ = 'James Iter'
  6. __date__ = '2017/3/25'
  7. __contact__ = 'james.iter.cn@gmail.com'
  8. __copyright__ = '(c) 2017 by James Iter.'
  9. class GuestXML(object):
  10. """
  11. <?xml version="1.0" encoding="utf-8"?>
  12. <domain type="kvm">
  13. <features>
  14. <acpi/>
  15. <apic/>
  16. </features>
  17. <name>10v022</name>
  18. <vcpu>16</vcpu>
  19. <memory unit="GiB">64</memory>
  20. <os>
  21. <boot dev="hd"/>
  22. <type arch="x86_64">hvm</type>
  23. <bootmenu timeout="3000" enable="yes"/>
  24. </os>
  25. <devices>
  26. <interface type='network'>
  27. <source network='net-br0'/>
  28. <model type='virtio'/>
  29. </interface>
  30. <disk type='network' device='disk'>
  31. <driver name='qemu' type='qcow2' cache='none'/>
  32. <source protocol='gluster' name='gv0/VMs/disk_pool/10v01251/L_10v01251.qcow2'>
  33. <host name='127.0.0.1' port='24007'/>
  34. </source>
  35. <target dev='vda' bus='virtio'/>
  36. </disk>
  37. <graphics passwd="000000" keymap="en-us" port="6002" type="vnc">
  38. <listen network="net-br0" type="network"/>
  39. </graphics>
  40. <serial type='pty'>
  41. <target port='0'/>
  42. </serial>
  43. <console type='pty'>
  44. <target type='serial' port='0'/>
  45. </console>
  46. </devices>
  47. </domain>
  48. """
  49. def __init__(self, guest=None, disk=None, config=None):
  50. assert isinstance(guest, Guest)
  51. assert isinstance(disk, Disk)
  52. assert isinstance(config, Config)
  53. self.guest = guest
  54. self.disk = disk
  55. self.config = config
  56. def get_domain(self):
  57. return """<?xml version="1.0" encoding="utf-8"?>
  58. <domain type="kvm">
  59. {0}
  60. {1}
  61. {2}
  62. {3}
  63. {4}
  64. {5}
  65. {6}
  66. </domain>
  67. """.format(self.get_features(), self.get_name(), self.get_uuid(), self.get_vcpu(), self.get_memory(),
  68. self.get_os(), self.get_devices())
  69. @staticmethod
  70. def get_features():
  71. return """
  72. <features>
  73. <acpi/>
  74. <apic/>
  75. </features>
  76. """
  77. def get_name(self):
  78. return """<name>{0}</name>""".format(self.guest.name)
  79. def get_uuid(self):
  80. return """<uuid>{0}</uuid>""".format(self.guest.uuid)
  81. def get_vcpu(self):
  82. return """<vcpu>{0}</vcpu>""".format(self.guest.cpu.__str__())
  83. def get_memory(self):
  84. return """<memory unit="GiB">{0}</memory>""".format(self.guest.memory.__str__())
  85. @staticmethod
  86. def get_os():
  87. return """
  88. <os>
  89. <boot dev="hd"/>
  90. <type arch="x86_64">hvm</type>
  91. <bootmenu timeout="3000" enable="yes"/>
  92. </os>
  93. """
  94. def get_devices(self):
  95. return """
  96. <devices>
  97. {0}
  98. {1}
  99. {2}
  100. {3}
  101. </devices>
  102. """.format(self.get_interface(), self.get_disk(), self.get_graphics(), self.get_console())
  103. def get_interface(self):
  104. return """
  105. <interface type='network'>
  106. <source network='{0}'/>
  107. <model type='virtio'/>
  108. </interface>
  109. """.format(self.guest.network)
  110. def get_disk(self):
  111. from initialize import dev_table
  112. return """
  113. <disk type='network' device='disk'>
  114. <driver name='qemu' type='{0}' cache='none'/>
  115. <source protocol='gluster' name='{1}/{2}'>
  116. <host name='127.0.0.1' port='24007'/>
  117. </source>
  118. <target dev='{3}' bus='virtio'/>
  119. </disk>
  120. """.format(self.disk.format, self.config.glusterfs_volume, self.disk.path, dev_table[self.disk.sequence])
  121. def get_graphics(self):
  122. return """
  123. <graphics passwd="{0}" keymap="en-us" port="{1}" type="vnc">
  124. <listen network="{2}" type="network"/>
  125. </graphics>
  126. """.format(self.guest.vnc_password, self.guest.vnc_port, self.guest.manage_network)
  127. @staticmethod
  128. def get_console():
  129. return """
  130. <serial type='pty'>
  131. <target port='0'/>
  132. </serial>
  133. <console type='pty'>
  134. <target type='serial' port='0'/>
  135. </console>
  136. """