guest.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import jimit as ji
  4. import json
  5. from filter import FilterFieldType
  6. from orm import ORM
  7. from status import GuestState, DiskState
  8. from database import Database as db
  9. from initialize import app
  10. __author__ = 'James Iter'
  11. __date__ = '2017/3/22'
  12. __contact__ = 'james.iter.cn@gmail.com'
  13. __copyright__ = '(c) 2017 by James Iter.'
  14. class Guest(ORM):
  15. _table_name = 'guest'
  16. _primary_key = 'id'
  17. def __init__(self):
  18. super(Guest, self).__init__()
  19. self.id = 0
  20. self.uuid = None
  21. self.label = None
  22. self.password = None
  23. self.remark = ''
  24. self.os_template_id = None
  25. self.create_time = ji.Common.tus()
  26. self.status = GuestState.no_state.value
  27. self.progress = 0
  28. self.on_host = ''
  29. self.cpu = None
  30. self.memory = None
  31. self.ip = None
  32. self.network = None
  33. self.manage_network = None
  34. self.vnc_port = None
  35. self.vnc_password = None
  36. self.xml = None
  37. @staticmethod
  38. def get_filter_keywords():
  39. return {
  40. 'id': FilterFieldType.INT.value,
  41. 'uuid': FilterFieldType.STR.value,
  42. 'label': FilterFieldType.STR.value,
  43. 'remark': FilterFieldType.STR.value,
  44. 'on_host': FilterFieldType.STR.value,
  45. 'ip': FilterFieldType.STR.value
  46. }
  47. @staticmethod
  48. def get_allow_update_keywords():
  49. return ['remark', 'cpu', 'memory', 'network', 'manage_network', 'vnc_password']
  50. @staticmethod
  51. def get_allow_content_search_keywords():
  52. return ['label', 'remark', 'on_host', 'ip']
  53. @staticmethod
  54. def emit_instruction(message):
  55. db.r.publish(app.config['instruction_channel'], message=message)
  56. def get_boot_jobs_key(self):
  57. return ':'.join([app.config['guest_boot_jobs'], self.uuid])
  58. def add_boot_jobs(self, boot_jobs_id):
  59. if not isinstance(boot_jobs_id, list):
  60. raise ValueError('The boot_jobs_id must be a list.')
  61. key = self.get_boot_jobs_key()
  62. db.r.sadd(key, *boot_jobs_id)
  63. db.r.expire(key, app.config['guest_boot_jobs_wait_time'])
  64. def get_boot_jobs(self):
  65. return db.r.ttl(self.get_boot_jobs_key()), list(db.r.smembers(self.get_boot_jobs_key()))
  66. def delete_boot_jobs(self, boot_jobs_id):
  67. if not isinstance(boot_jobs_id, list):
  68. raise ValueError('The boot_jobs_id must be a list.')
  69. key = self.get_boot_jobs_key()
  70. db.r.srem(key, *boot_jobs_id)
  71. # 如果集合下还有值,则更新启动作业有效时间
  72. if db.r.exists(key):
  73. db.r.expire(key, app.config['guest_boot_jobs_wait_time'])
  74. @staticmethod
  75. def get_uuids_of_all_had_boot_job():
  76. boot_job_keys = db.r.keys(pattern=app.config['guest_boot_jobs'] + '*')
  77. uuids = list()
  78. for boot_job_key in boot_job_keys:
  79. uuids.append(boot_job_key.split(':')[-1])
  80. return uuids
  81. @staticmethod
  82. def get_lightest_host():
  83. # 负载最小的宿主机
  84. lightest_host = None
  85. for k, v in db.r.hgetall(app.config['hosts_info']).items():
  86. v = json.loads(v)
  87. if lightest_host is None:
  88. lightest_host = v
  89. if float(lightest_host['system_load'][0]) / lightest_host['cpu'] > \
  90. float(v['system_load'][0]) / v['cpu']:
  91. lightest_host = v
  92. return lightest_host
  93. class Disk(ORM):
  94. _table_name = 'disk'
  95. _primary_key = 'id'
  96. def __init__(self):
  97. super(Disk, self).__init__()
  98. self.id = 0
  99. self.uuid = None
  100. self.remark = None
  101. self.path = None
  102. self.size = None
  103. self.sequence = None
  104. self.state = DiskState.pending.value
  105. self.on_host = ''
  106. self.format = 'qcow2'
  107. self.create_time = ji.Common.tus()
  108. self.guest_uuid = None
  109. self.iops = 0
  110. self.iops_rd = 0
  111. self.iops_wr = 0
  112. self.iops_max = 0
  113. self.iops_max_length = 0
  114. self.bps = 0
  115. self.bps_rd = 0
  116. self.bps_wr = 0
  117. self.bps_max = 0
  118. self.bps_max_length = 0
  119. def quota(self, config=None):
  120. from models import Config
  121. assert isinstance(config, Config)
  122. # 系统盘 IOPS 默认不计算增益
  123. if self.sequence != 0:
  124. self.iops = config.iops_base + config.iops_pre_unit * self.size
  125. else:
  126. self.iops = config.iops_base
  127. if self.iops > config.iops_cap:
  128. self.iops = config.iops_cap
  129. self.iops_max = config.iops_max
  130. self.iops_max_length = config.iops_max_length
  131. self.iops_rd = 0
  132. self.iops_wr = 0
  133. # 系统盘 BPS 默认不计算增益
  134. if self.sequence != 0:
  135. self.bps = config.bps_base + config.bps_pre_unit * self.size
  136. else:
  137. self.bps = config.bps_base
  138. if self.bps > config.bps_cap:
  139. self.bps = config.bps_cap
  140. self.bps_max = config.bps_max
  141. self.bps_max_length = config.bps_max_length
  142. self.bps_rd = 0
  143. self.bps_wr = 0
  144. @staticmethod
  145. def get_filter_keywords():
  146. return {
  147. 'id': FilterFieldType.INT.value,
  148. 'uuid': FilterFieldType.STR.value,
  149. 'remark': FilterFieldType.STR.value,
  150. 'size': FilterFieldType.INT.value,
  151. 'state': FilterFieldType.INT.value,
  152. 'sequence': FilterFieldType.INT.value,
  153. 'on_host': FilterFieldType.STR.value,
  154. 'guest_uuid': FilterFieldType.STR.value
  155. }
  156. @staticmethod
  157. def get_allow_update_keywords():
  158. return ['on_host', 'sequence', 'state', 'guest_uuid']
  159. @staticmethod
  160. def get_allow_content_search_keywords():
  161. return ['remark', 'size', 'guest_uuid', 'uuid', 'on_host']
  162. class GuestMigrateInfo(ORM):
  163. _table_name = 'guest_migrate_info'
  164. _primary_key = 'id'
  165. def __init__(self):
  166. super(GuestMigrateInfo, self).__init__()
  167. self.id = 0
  168. self.uuid = None
  169. self.type = None
  170. self.time_elapsed = None
  171. self.time_remaining = None
  172. self.data_total = None
  173. self.data_processed = None
  174. self.data_remaining = None
  175. self.mem_total = None
  176. self.mem_processed = None
  177. self.mem_remaining = None
  178. self.file_total = None
  179. self.file_processed = None
  180. self.file_remaining = None
  181. @staticmethod
  182. def get_filter_keywords():
  183. return {
  184. 'id': FilterFieldType.INT.value,
  185. 'uuid': FilterFieldType.STR.value
  186. }
  187. @staticmethod
  188. def get_allow_update_keywords():
  189. return []
  190. @staticmethod
  191. def get_allow_content_search_keywords():
  192. return []