guest_xml.py 4.1 KB

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