os_template_image.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from flask import Blueprint
  4. from flask import request
  5. import jimit as ji
  6. import json
  7. from api.base import Base
  8. from models import OSTemplateImage, OSTemplateProfile, Host, Config, OSTemplateImageKind, Guest
  9. from models import Rules
  10. from models import Utils
  11. __author__ = 'James Iter'
  12. __date__ = '2018/2/4'
  13. __contact__ = 'james.iter.cn@gmail.com'
  14. __copyright__ = '(c) 2018 by James Iter.'
  15. blueprint = Blueprint(
  16. 'api_os_template_image',
  17. __name__,
  18. url_prefix='/api/os_template_image'
  19. )
  20. blueprints = Blueprint(
  21. 'api_os_templates_image',
  22. __name__,
  23. url_prefix='/api/os_templates_image'
  24. )
  25. os_template_image_base = Base(the_class=OSTemplateImage, the_blueprint=blueprint, the_blueprints=blueprints)
  26. @Utils.dumps2response
  27. def r_create():
  28. os_template_image = OSTemplateImage()
  29. args_rules = [
  30. Rules.LABEL.value,
  31. Rules.DESCRIPTION.value,
  32. Rules.PATH.value,
  33. Rules.LOGO.value,
  34. Rules.OS_TEMPLATE_PROFILE_ID_EXT.value,
  35. Rules.ACTIVE.value,
  36. Rules.OS_TEMPLATE_IMAGE_KIND.value
  37. ]
  38. os_template_image.label = request.json.get('label')
  39. os_template_image.description = request.json.get('description')
  40. os_template_image.path = request.json.get('path')
  41. os_template_image.logo = request.json.get('logo')
  42. os_template_image.active = bool(int(request.json.get('active', 1)))
  43. os_template_image.os_template_profile_id = request.json.get('os_template_profile_id')
  44. os_template_image.kind = request.json.get('kind')
  45. os_template_image.progress = 100
  46. try:
  47. ji.Check.previewing(args_rules, os_template_image.__dict__)
  48. ret = dict()
  49. ret['state'] = ji.Common.exchange_state(20000)
  50. if os_template_image.exist_by('path'):
  51. ret['state'] = ji.Common.exchange_state(40901)
  52. ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', os_template_image.path])
  53. return ret
  54. os_template_profile = OSTemplateProfile()
  55. os_template_profile.id = os_template_image.os_template_profile_id
  56. if not os_template_profile.exist():
  57. ret['state'] = ji.Common.exchange_state(40401)
  58. ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], u': 操作系统模板描述文件ID: ',
  59. os_template_image.os_template_profile_id.__str__()])
  60. return ret
  61. os_template_image.create()
  62. os_template_image.get_by('path')
  63. ret['data'] = os_template_image.__dict__
  64. return ret
  65. except ji.PreviewingError, e:
  66. return json.loads(e.message)
  67. @Utils.dumps2response
  68. def r_update(_id):
  69. os_template_image = OSTemplateImage()
  70. args_rules = [
  71. Rules.ID.value
  72. ]
  73. if 'label' in request.json:
  74. args_rules.append(
  75. Rules.LABEL.value,
  76. )
  77. if 'description' in request.json:
  78. args_rules.append(
  79. Rules.DESCRIPTION.value,
  80. )
  81. if 'path' in request.json:
  82. args_rules.append(
  83. Rules.PATH.value,
  84. )
  85. if 'active' in request.json:
  86. args_rules.append(
  87. Rules.ACTIVE.value,
  88. )
  89. if 'logo' in request.json:
  90. args_rules.append(
  91. Rules.LOGO.value,
  92. )
  93. if 'os_template_profile_id' in request.json:
  94. args_rules.append(
  95. Rules.OS_TEMPLATE_PROFILE_ID_EXT.value,
  96. )
  97. if 'kind' in request.json:
  98. args_rules.append(
  99. Rules.OS_TEMPLATE_IMAGE_KIND.value,
  100. )
  101. if args_rules.__len__() < 2:
  102. ret = dict()
  103. ret['state'] = ji.Common.exchange_state(20000)
  104. return ret
  105. request.json['id'] = _id
  106. try:
  107. ji.Check.previewing(args_rules, request.json)
  108. os_template_image.id = request.json.get('id')
  109. os_template_image.get()
  110. os_template_image.label = request.json.get('label', os_template_image.label)
  111. os_template_image.description = request.json.get('description', os_template_image.description)
  112. os_template_image.path = request.json.get('path', os_template_image.path)
  113. os_template_image.active = request.json.get('active', os_template_image.active)
  114. os_template_image.logo = request.json.get('logo', os_template_image.logo)
  115. os_template_image.os_template_profile_id = \
  116. request.json.get('os_template_profile_id', os_template_image.os_template_profile_id)
  117. os_template_image.kind = request.json.get('kind', os_template_image.kind)
  118. os_template_image.update()
  119. os_template_image.get()
  120. ret = dict()
  121. ret['state'] = ji.Common.exchange_state(20000)
  122. ret['data'] = os_template_image.__dict__
  123. return ret
  124. except ji.PreviewingError, e:
  125. return json.loads(e.message)
  126. @Utils.dumps2response
  127. def r_delete(ids):
  128. ret = dict()
  129. ret['state'] = ji.Common.exchange_state(20000)
  130. config = Config()
  131. config.id = 1
  132. config.get()
  133. # 取全部活着的 hosts
  134. available_hosts = Host.get_available_hosts(nonrandom=None)
  135. if available_hosts.__len__() == 0:
  136. ret['state'] = ji.Common.exchange_state(50351)
  137. return ret
  138. chosen_host = available_hosts[0]
  139. node_id = chosen_host['node_id']
  140. os_template_image = OSTemplateImage()
  141. # TODO: 加入对,是否有被 Guest 引用的判断
  142. for _id in ids.split(','):
  143. os_template_image.id = _id
  144. os_template_image.get()
  145. for _id in ids.split(','):
  146. os_template_image.id = _id
  147. os_template_image.get()
  148. # 暂时不支持从计算节点上,删除公共镜像
  149. if os_template_image.kind == OSTemplateImageKind.public.value:
  150. os_template_image.delete()
  151. continue
  152. elif os_template_image.kind == OSTemplateImageKind.custom.value:
  153. os_template_image.progress = 254
  154. message = {
  155. '_object': 'os_template_image',
  156. 'action': 'delete',
  157. 'storage_mode': config.storage_mode,
  158. 'dfs_volume': config.dfs_volume,
  159. 'template_path': os_template_image.path,
  160. # uuid 这里没有实际意义,仅仅是为了迁就 JimV-C 的个命令格式
  161. 'uuid': None,
  162. 'node_id': node_id,
  163. 'os_template_image_id': os_template_image.id,
  164. 'passback_parameters': {'id': os_template_image.id}
  165. }
  166. Utils.emit_instruction(message=json.dumps(message))
  167. os_template_image.update()
  168. return ret
  169. @Utils.dumps2response
  170. def r_get(ids):
  171. return os_template_image_base.get(ids=ids, ids_rule=Rules.IDS.value, by_field='id')
  172. @Utils.dumps2response
  173. def r_get_by_filter():
  174. ret = os_template_image_base.get_by_filter()
  175. if '200' != ret['state']['code']:
  176. return ret
  177. os_template_images_id = list()
  178. for os_template_image in ret['data']:
  179. os_template_image_id = os_template_image['id']
  180. if os_template_image_id not in os_template_images_id:
  181. os_template_images_id.append(os_template_image_id)
  182. rows, _ = Guest.get_by_filter(filter_str=':'.join(['os_template_image_id', 'in',
  183. ','.join(_id.__str__() for _id in os_template_images_id)]))
  184. os_template_image_guest_mapping = dict()
  185. for guest in rows:
  186. os_template_image_id = guest['os_template_image_id']
  187. if os_template_image_id not in os_template_image_guest_mapping:
  188. os_template_image_guest_mapping[os_template_image_id] = list()
  189. os_template_image_guest_mapping[os_template_image_id].append(guest)
  190. for i, os_template_image in enumerate(ret['data']):
  191. if os_template_image['id'] in os_template_image_guest_mapping:
  192. ret['data'][i]['guests'] = os_template_image_guest_mapping[os_template_image['id']]
  193. return ret
  194. @Utils.dumps2response
  195. def r_content_search():
  196. return os_template_image_base.content_search()