guest.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. def get_boot_jobs_key(self):
  54. return ':'.join([app.config['guest_boot_jobs'], self.uuid])
  55. def add_boot_jobs(self, boot_jobs_id):
  56. if not isinstance(boot_jobs_id, list):
  57. raise ValueError('The boot_jobs_id must be a list.')
  58. key = self.get_boot_jobs_key()
  59. db.r.sadd(key, *boot_jobs_id)
  60. db.r.expire(key, app.config['guest_boot_jobs_wait_time'])
  61. def get_boot_jobs(self):
  62. return db.r.ttl(self.get_boot_jobs_key()), list(db.r.smembers(self.get_boot_jobs_key()))
  63. def delete_boot_jobs(self, boot_jobs_id):
  64. if not isinstance(boot_jobs_id, list):
  65. raise ValueError('The boot_jobs_id must be a list.')
  66. key = self.get_boot_jobs_key()
  67. db.r.srem(key, *boot_jobs_id)
  68. # 如果集合下还有值,则更新启动作业有效时间
  69. if db.r.exists(key):
  70. db.r.expire(key, app.config['guest_boot_jobs_wait_time'])
  71. @staticmethod
  72. def get_uuids_of_all_had_boot_job():
  73. boot_job_keys = db.r.keys(pattern=app.config['guest_boot_jobs'] + '*')
  74. uuids = list()
  75. for boot_job_key in boot_job_keys:
  76. uuids.append(boot_job_key.split(':')[-1])
  77. return uuids
  78. @staticmethod
  79. def get_lightest_host():
  80. # 负载最小的宿主机
  81. lightest_host = None
  82. for k, v in db.r.hgetall(app.config['hosts_info']).items():
  83. v = json.loads(v)
  84. if lightest_host is None:
  85. lightest_host = v
  86. if float(lightest_host['system_load'][0]) / lightest_host['cpu'] > \
  87. float(v['system_load'][0]) / v['cpu']:
  88. lightest_host = v
  89. return lightest_host
  90. class Disk(ORM):
  91. _table_name = 'disk'
  92. _primary_key = 'id'
  93. def __init__(self):
  94. super(Disk, self).__init__()
  95. self.id = 0
  96. self.uuid = None
  97. self.remark = None
  98. self.path = None
  99. self.size = None
  100. self.sequence = None
  101. self.state = DiskState.pending.value
  102. self.on_host = ''
  103. self.format = 'qcow2'
  104. self.create_time = ji.Common.tus()
  105. self.guest_uuid = None
  106. self.iops = 0
  107. self.iops_rd = 0
  108. self.iops_wr = 0
  109. self.iops_max = 0
  110. self.iops_max_length = 0
  111. self.bps = 0
  112. self.bps_rd = 0
  113. self.bps_wr = 0
  114. self.bps_max = 0
  115. self.bps_max_length = 0
  116. def quota(self, config=None):
  117. from models import Config
  118. assert isinstance(config, Config)
  119. # 系统盘 IOPS 默认不计算增益
  120. if self.sequence != 0:
  121. self.iops = config.iops_base + config.iops_pre_unit * self.size
  122. else:
  123. self.iops = config.iops_base
  124. if self.iops > config.iops_cap:
  125. self.iops = config.iops_cap
  126. self.iops_max = config.iops_max
  127. self.iops_max_length = config.iops_max_length
  128. self.iops_rd = 0
  129. self.iops_wr = 0
  130. # 系统盘 BPS 默认不计算增益
  131. if self.sequence != 0:
  132. self.bps = config.bps_base + config.bps_pre_unit * self.size
  133. else:
  134. self.bps = config.bps_base
  135. if self.bps > config.bps_cap:
  136. self.bps = config.bps_cap
  137. self.bps_max = config.bps_max
  138. self.bps_max_length = config.bps_max_length
  139. self.bps_rd = 0
  140. self.bps_wr = 0
  141. @staticmethod
  142. def get_filter_keywords():
  143. return {
  144. 'id': FilterFieldType.INT.value,
  145. 'uuid': FilterFieldType.STR.value,
  146. 'remark': FilterFieldType.STR.value,
  147. 'size': FilterFieldType.INT.value,
  148. 'state': FilterFieldType.INT.value,
  149. 'sequence': FilterFieldType.INT.value,
  150. 'on_host': FilterFieldType.STR.value,
  151. 'guest_uuid': FilterFieldType.STR.value
  152. }
  153. @staticmethod
  154. def get_allow_update_keywords():
  155. return ['on_host', 'sequence', 'state', 'guest_uuid']
  156. @staticmethod
  157. def get_allow_content_search_keywords():
  158. return ['remark', 'size', 'guest_uuid', 'uuid', 'on_host']
  159. class GuestMigrateInfo(ORM):
  160. _table_name = 'guest_migrate_info'
  161. _primary_key = 'id'
  162. def __init__(self):
  163. super(GuestMigrateInfo, self).__init__()
  164. self.id = 0
  165. self.uuid = None
  166. self.type = None
  167. self.time_elapsed = None
  168. self.time_remaining = None
  169. self.data_total = None
  170. self.data_processed = None
  171. self.data_remaining = None
  172. self.mem_total = None
  173. self.mem_processed = None
  174. self.mem_remaining = None
  175. self.file_total = None
  176. self.file_processed = None
  177. self.file_remaining = None
  178. @staticmethod
  179. def get_filter_keywords():
  180. return {
  181. 'id': FilterFieldType.INT.value,
  182. 'uuid': FilterFieldType.STR.value
  183. }
  184. @staticmethod
  185. def get_allow_update_keywords():
  186. return []
  187. @staticmethod
  188. def get_allow_content_search_keywords():
  189. return []