guest.py 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import copy
  4. from math import ceil
  5. from IPy import IP
  6. import requests
  7. import json
  8. from uuid import uuid4
  9. import random
  10. import time
  11. import jimit as ji
  12. from flask import Blueprint, url_for, request
  13. from jimvc.api.base import Base
  14. from jimvc.models.initialize import dev_table
  15. from jimvc.models import app_config
  16. from jimvc.models import GuestState
  17. from jimvc.models import Service
  18. from jimvc.models import IPPool
  19. from jimvc.models import ReservedIP
  20. from jimvc.models import DiskState, Host
  21. from jimvc.models import Database as db
  22. from jimvc.models import Config
  23. from jimvc.models import Disk
  24. from jimvc.models import Rules
  25. from jimvc.models import Utils
  26. from jimvc.models import Guest
  27. from jimvc.models import OSTemplateImage
  28. from jimvc.models import OSTemplateProfile
  29. from jimvc.models import OSTemplateInitializeOperate
  30. from jimvc.models import GuestXML
  31. from jimvc.models import SSHKeyGuestMapping
  32. from jimvc.models import SSHKey
  33. from jimvc.models import Snapshot
  34. from jimvc.models import status
  35. __author__ = 'James Iter'
  36. __date__ = '2017/3/22'
  37. __contact__ = 'james.iter.cn@gmail.com'
  38. __copyright__ = '(c) 2017 by James Iter.'
  39. blueprint = Blueprint(
  40. 'api_guest',
  41. __name__,
  42. url_prefix='/api/guest'
  43. )
  44. blueprints = Blueprint(
  45. 'api_guests',
  46. __name__,
  47. url_prefix='/api/guests'
  48. )
  49. guest_base = Base(the_class=Guest, the_blueprint=blueprint, the_blueprints=blueprints)
  50. os_template_image_base = Base(the_class=OSTemplateImage, the_blueprint=blueprint, the_blueprints=blueprints)
  51. os_template_profile_base = Base(the_class=OSTemplateProfile, the_blueprint=blueprint, the_blueprints=blueprints)
  52. @Utils.dumps2response
  53. def r_create():
  54. args_rules = [
  55. Rules.CPU.value,
  56. Rules.MEMORY.value,
  57. Rules.BANDWIDTH.value,
  58. Rules.BANDWIDTH_UNIT.value,
  59. Rules.OS_TEMPLATE_IMAGE_ID.value,
  60. Rules.QUANTITY.value,
  61. Rules.REMARK.value,
  62. Rules.PASSWORD.value,
  63. Rules.LEASE_TERM.value
  64. ]
  65. if 'node_id' in request.json:
  66. args_rules.append(
  67. Rules.NODE_ID.value
  68. )
  69. if 'ssh_keys_id' in request.json:
  70. args_rules.append(
  71. Rules.SSH_KEYS_ID.value
  72. )
  73. if 'service_id' in request.json:
  74. args_rules.append(
  75. Rules.SERVICE_ID.value
  76. )
  77. if 'autostart' in request.json:
  78. args_rules.append(
  79. Rules.AUTOSTART.value
  80. )
  81. try:
  82. ret = dict()
  83. ret['state'] = ji.Common.exchange_state(20000)
  84. ji.Check.previewing(args_rules, request.json)
  85. config = Config()
  86. config.id = 1
  87. config.get()
  88. os_template_image = OSTemplateImage()
  89. os_template_profile = OSTemplateProfile()
  90. os_template_image.id = request.json.get('os_template_image_id')
  91. if not os_template_image.exist():
  92. ret['state'] = ji.Common.exchange_state(40450)
  93. ret['state']['sub']['zh-cn'] = ''.join([ret['state']['sub']['zh-cn'], ': ', os_template_image.id.__str__()])
  94. return ret
  95. os_template_image.get()
  96. os_template_profile.id = os_template_image.os_template_profile_id
  97. os_template_profile.get()
  98. os_template_initialize_operates, os_template_initialize_operates_count = \
  99. OSTemplateInitializeOperate.get_by_filter(
  100. filter_str='os_template_initialize_operate_set_id:eq:' +
  101. os_template_profile.os_template_initialize_operate_set_id.__str__())
  102. node_id = request.json.get('node_id', None)
  103. # 默认只取可随机分配虚拟机的 hosts
  104. available_hosts = Host.get_available_hosts(nonrandom=False)
  105. # 当指定了 host 时,取全部活着的 hosts
  106. if node_id is not None:
  107. available_hosts = Host.get_available_hosts(nonrandom=None)
  108. if available_hosts.__len__() == 0:
  109. ret['state'] = ji.Common.exchange_state(50351)
  110. return ret
  111. available_hosts_mapping_by_node_id = dict()
  112. for host in available_hosts:
  113. if host['node_id'] not in available_hosts_mapping_by_node_id:
  114. available_hosts_mapping_by_node_id[host['node_id']] = host
  115. if node_id is not None and node_id not in available_hosts_mapping_by_node_id:
  116. ret['state'] = ji.Common.exchange_state(50351)
  117. return ret
  118. ssh_keys_id = request.json.get('ssh_keys_id', list())
  119. ssh_keys = list()
  120. ssh_key_guest_mapping = SSHKeyGuestMapping()
  121. if ssh_keys_id.__len__() > 0:
  122. rows, _ = SSHKey.get_by_filter(
  123. filter_str=':'.join(['id', 'in', ','.join(_id.__str__() for _id in ssh_keys_id)]))
  124. for row in rows:
  125. ssh_keys.append(row['public_key'])
  126. # 确保目标 服务组 存在
  127. service = Service()
  128. service.id = request.json.get('service_id', 1)
  129. service.get()
  130. bandwidth = request.json.get('bandwidth')
  131. bandwidth_unit = request.json.get('bandwidth_unit')
  132. if bandwidth_unit == 'k':
  133. bandwidth = bandwidth * 1000
  134. elif bandwidth_unit == 'm':
  135. bandwidth = bandwidth * 1000 ** 2
  136. elif bandwidth_unit == 'g':
  137. bandwidth = bandwidth * 1000 ** 3
  138. else:
  139. ret = dict()
  140. ret['state'] = ji.Common.exchange_state(41203)
  141. raise ji.PreviewingError(json.dumps(ret, ensure_ascii=False))
  142. # http://man7.org/linux/man-pages/man8/tc.8.html
  143. # 如果带宽大于 tc 所控最大速率,则置其为无限带宽
  144. # 34359738360 等于 tc 最大可控字节速率,换算出的比特位
  145. if bandwidth > 34359738360:
  146. bandwidth = 0
  147. quantity = request.json.get('quantity')
  148. occupied_ips = list()
  149. occupied_vnc_ports = list()
  150. rows, count = Guest.get_all()
  151. for row in rows:
  152. occupied_ips.append(row['ip'])
  153. occupied_vnc_ports.append(row['vnc_port'])
  154. rows, count = ReservedIP.get_all()
  155. for row in rows:
  156. occupied_ips.append(row['ip'])
  157. rows, count = IPPool.get_by_filter(filter_str=':'.join(['activity', 'eq', '1']))
  158. if count < 1:
  159. ret['state'] = ji.Common.exchange_state(50350)
  160. return ret
  161. ip_pool = IPPool()
  162. ip_pool.id = rows[0]['id']
  163. ip_pool.get()
  164. guest_ip_generator = ip_pool.ip_generator(occupied_ips=occupied_ips)
  165. guest_vnc_port_generator = ip_pool.vnc_port_generator(occupied_vnc_ports=occupied_vnc_ports)
  166. while quantity:
  167. quantity -= 1
  168. guest = Guest()
  169. guest.uuid = uuid4().__str__()
  170. guest.cpu = request.json.get('cpu')
  171. # 虚拟机内存单位,模板生成方法中已置其为GiB
  172. guest.memory = request.json.get('memory')
  173. guest.bandwidth = bandwidth
  174. guest.os_template_image_id = request.json.get('os_template_image_id')
  175. guest.label = ji.Common.generate_random_code(length=8)
  176. guest.remark = request.json.get('remark', '')
  177. guest.autostart = request.json.get('autostart', False)
  178. guest.password = request.json.get('password')
  179. if guest.password is None or guest.password.__len__() < 1:
  180. guest.password = ji.Common.generate_random_code(length=16)
  181. guest.ip = guest_ip_generator.next()
  182. guest.vnc_port = guest_vnc_port_generator.next()
  183. guest.network = config.vm_network
  184. guest.manage_network = config.vm_manage_network
  185. guest.vnc_password = ji.Common.generate_random_code(length=16)
  186. disk = Disk()
  187. disk.uuid = guest.uuid
  188. disk.remark = guest.label.__str__() + '_SystemImage'
  189. disk.format = 'qcow2'
  190. disk.sequence = 0
  191. disk.size = 0
  192. disk.path = config.storage_path + '/' + disk.uuid + '.' + disk.format
  193. disk.guest_uuid = ''
  194. # disk.node_id 由 guest 事件处理机更新。涉及迁移时,其所属 node_id 会变更。参见 @models/event_processory.py:111 附近。
  195. disk.node_id = 0
  196. disk.quota(config=config)
  197. disk.create()
  198. if node_id is None:
  199. # 在可用计算节点中平均分配任务
  200. chosen_host = available_hosts[quantity % available_hosts.__len__()]
  201. else:
  202. chosen_host = available_hosts_mapping_by_node_id[node_id]
  203. guest.node_id = chosen_host['node_id']
  204. guest.service_id = service.id
  205. guest_xml = GuestXML(host=chosen_host, guest=guest, disk=disk, config=config,
  206. os_type=os_template_profile.os_type)
  207. guest.xml = guest_xml.get_domain()
  208. guest.node_id = int(guest.node_id)
  209. guest.create()
  210. ssh_key_guest_mapping.guest_uuid = guest.uuid
  211. if ssh_keys_id.__len__() > 0:
  212. for ssh_key_id in ssh_keys_id:
  213. ssh_key_guest_mapping.ssh_key_id = ssh_key_id
  214. ssh_key_guest_mapping.create()
  215. if os_template_profile.os_distro == 'coreos':
  216. ip_pool.netmask = IP(guest.ip).make_net(ip_pool.netmask).prefixlen().__str__()
  217. # 替换占位符为有效内容
  218. _os_template_initialize_operates = copy.deepcopy(os_template_initialize_operates)
  219. for k, v in enumerate(_os_template_initialize_operates):
  220. _os_template_initialize_operates[k]['content'] = v['content'].replace('{IP}', guest.ip).\
  221. replace('{HOSTNAME}', guest.label). \
  222. replace('{PASSWORD}', guest.password). \
  223. replace('{NETMASK}', ip_pool.netmask).\
  224. replace('{GATEWAY}', ip_pool.gateway).\
  225. replace('{DNS1}', ip_pool.dns1).\
  226. replace('{DNS2}', ip_pool.dns2). \
  227. replace('{SSH-KEY}', '\n'.join(ssh_keys))
  228. _os_template_initialize_operates[k]['command'] = v['command'].replace('{IP}', guest.ip). \
  229. replace('{HOSTNAME}', guest.label). \
  230. replace('{PASSWORD}', guest.password). \
  231. replace('{NETMASK}', ip_pool.netmask). \
  232. replace('{GATEWAY}', ip_pool.gateway). \
  233. replace('{DNS1}', ip_pool.dns1). \
  234. replace('{DNS2}', ip_pool.dns2). \
  235. replace('{SSH-KEY}', '\n'.join(ssh_keys))
  236. message = {
  237. '_object': 'guest',
  238. 'action': 'create',
  239. 'uuid': guest.uuid,
  240. 'storage_mode': config.storage_mode,
  241. 'dfs_volume': config.dfs_volume,
  242. 'node_id': guest.node_id,
  243. 'autostart': guest.autostart,
  244. 'name': guest.label,
  245. 'template_path': os_template_image.path,
  246. 'os_type': os_template_profile.os_type,
  247. 'disks': [disk.__dict__],
  248. 'xml': guest_xml.get_domain(),
  249. 'os_template_initialize_operates': _os_template_initialize_operates,
  250. 'passback_parameters': {}
  251. }
  252. Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False))
  253. return ret
  254. except ji.PreviewingError, e:
  255. return json.loads(e.message)
  256. @Utils.dumps2response
  257. def r_autostart(uuids, autostart):
  258. args_rules = [
  259. Rules.UUIDS.value,
  260. Rules.AUTOSTART.value
  261. ]
  262. if str(autostart).lower() in ['false', '0']:
  263. autostart = False
  264. else:
  265. autostart = True
  266. try:
  267. ji.Check.previewing(args_rules, {'uuids': uuids, 'autostart': autostart})
  268. guest = Guest()
  269. for uuid in uuids.split(','):
  270. guest.uuid = uuid
  271. guest.get_by('uuid')
  272. for uuid in uuids.split(','):
  273. guest.uuid = uuid
  274. guest.get_by('uuid')
  275. message = {
  276. '_object': 'guest',
  277. 'action': 'autostart',
  278. 'uuid': uuid,
  279. 'node_id': guest.node_id,
  280. 'autostart': autostart,
  281. 'passback_parameters': {'autostart': autostart}
  282. }
  283. Utils.emit_instruction(message=json.dumps(message))
  284. ret = dict()
  285. ret['state'] = ji.Common.exchange_state(20000)
  286. return ret
  287. except ji.PreviewingError, e:
  288. return json.loads(e.message)
  289. @Utils.dumps2response
  290. def r_reboot(uuids):
  291. args_rules = [
  292. Rules.UUIDS.value
  293. ]
  294. try:
  295. ji.Check.previewing(args_rules, {'uuids': uuids})
  296. guest = Guest()
  297. for uuid in uuids.split(','):
  298. guest.uuid = uuid
  299. guest.get_by('uuid')
  300. for uuid in uuids.split(','):
  301. guest.uuid = uuid
  302. guest.get_by('uuid')
  303. message = {
  304. '_object': 'guest',
  305. 'action': 'reboot',
  306. 'uuid': uuid,
  307. 'node_id': guest.node_id
  308. }
  309. Utils.emit_instruction(message=json.dumps(message))
  310. ret = dict()
  311. ret['state'] = ji.Common.exchange_state(20000)
  312. return ret
  313. except ji.PreviewingError, e:
  314. return json.loads(e.message)
  315. @Utils.dumps2response
  316. def r_force_reboot(uuids):
  317. args_rules = [
  318. Rules.UUIDS.value
  319. ]
  320. try:
  321. ji.Check.previewing(args_rules, {'uuids': uuids})
  322. guest = Guest()
  323. for uuid in uuids.split(','):
  324. guest.uuid = uuid
  325. guest.get_by('uuid')
  326. for uuid in uuids.split(','):
  327. guest.uuid = uuid
  328. guest.get_by('uuid')
  329. disks, _ = Disk.get_by_filter(filter_str=':'.join(['guest_uuid', 'eq', guest.uuid]))
  330. message = {
  331. '_object': 'guest',
  332. 'action': 'force_reboot',
  333. 'uuid': uuid,
  334. 'node_id': guest.node_id,
  335. 'disks': disks
  336. }
  337. Utils.emit_instruction(message=json.dumps(message))
  338. ret = dict()
  339. ret['state'] = ji.Common.exchange_state(20000)
  340. return ret
  341. except ji.PreviewingError, e:
  342. return json.loads(e.message)
  343. @Utils.dumps2response
  344. def r_shutdown(uuids):
  345. args_rules = [
  346. Rules.UUIDS.value
  347. ]
  348. try:
  349. ji.Check.previewing(args_rules, {'uuids': uuids})
  350. guest = Guest()
  351. for uuid in uuids.split(','):
  352. guest.uuid = uuid
  353. guest.get_by('uuid')
  354. for uuid in uuids.split(','):
  355. guest.uuid = uuid
  356. guest.get_by('uuid')
  357. message = {
  358. '_object': 'guest',
  359. 'action': 'shutdown',
  360. 'uuid': uuid,
  361. 'node_id': guest.node_id
  362. }
  363. Utils.emit_instruction(message=json.dumps(message))
  364. ret = dict()
  365. ret['state'] = ji.Common.exchange_state(20000)
  366. return ret
  367. except ji.PreviewingError, e:
  368. return json.loads(e.message)
  369. @Utils.dumps2response
  370. def r_force_shutdown(uuids):
  371. args_rules = [
  372. Rules.UUIDS.value
  373. ]
  374. try:
  375. ji.Check.previewing(args_rules, {'uuids': uuids})
  376. guest = Guest()
  377. for uuid in uuids.split(','):
  378. guest.uuid = uuid
  379. guest.get_by('uuid')
  380. for uuid in uuids.split(','):
  381. guest.uuid = uuid
  382. guest.get_by('uuid')
  383. message = {
  384. '_object': 'guest',
  385. 'action': 'force_shutdown',
  386. 'uuid': uuid,
  387. 'node_id': guest.node_id
  388. }
  389. Utils.emit_instruction(message=json.dumps(message))
  390. ret = dict()
  391. ret['state'] = ji.Common.exchange_state(20000)
  392. return ret
  393. except ji.PreviewingError, e:
  394. return json.loads(e.message)
  395. @Utils.dumps2response
  396. def r_boot(uuids):
  397. # TODO: 做好关系依赖判断,比如boot不可以对suspend的实例操作。
  398. args_rules = [
  399. Rules.UUIDS.value
  400. ]
  401. try:
  402. ji.Check.previewing(args_rules, {'uuids': uuids})
  403. guest = Guest()
  404. for uuid in uuids.split(','):
  405. guest.uuid = uuid
  406. guest.get_by('uuid')
  407. config = Config()
  408. config.id = 1
  409. config.get()
  410. for uuid in uuids.split(','):
  411. guest.uuid = uuid
  412. guest.get_by('uuid')
  413. disks, _ = Disk.get_by_filter(filter_str=':'.join(['guest_uuid', 'eq', guest.uuid]))
  414. message = {
  415. '_object': 'guest',
  416. 'action': 'boot',
  417. 'uuid': uuid,
  418. 'node_id': guest.node_id,
  419. 'passback_parameters': {},
  420. 'disks': disks
  421. }
  422. Utils.emit_instruction(message=json.dumps(message))
  423. ret = dict()
  424. ret['state'] = ji.Common.exchange_state(20000)
  425. return ret
  426. except ji.PreviewingError, e:
  427. return json.loads(e.message)
  428. @Utils.dumps2response
  429. def r_suspend(uuids):
  430. args_rules = [
  431. Rules.UUIDS.value
  432. ]
  433. try:
  434. ji.Check.previewing(args_rules, {'uuids': uuids})
  435. guest = Guest()
  436. for uuid in uuids.split(','):
  437. guest.uuid = uuid
  438. guest.get_by('uuid')
  439. for uuid in uuids.split(','):
  440. guest.uuid = uuid
  441. guest.get_by('uuid')
  442. message = {
  443. '_object': 'guest',
  444. 'action': 'suspend',
  445. 'uuid': uuid,
  446. 'node_id': guest.node_id
  447. }
  448. Utils.emit_instruction(message=json.dumps(message))
  449. ret = dict()
  450. ret['state'] = ji.Common.exchange_state(20000)
  451. return ret
  452. except ji.PreviewingError, e:
  453. return json.loads(e.message)
  454. @Utils.dumps2response
  455. def r_resume(uuids):
  456. args_rules = [
  457. Rules.UUIDS.value
  458. ]
  459. try:
  460. ji.Check.previewing(args_rules, {'uuids': uuids})
  461. guest = Guest()
  462. for uuid in uuids.split(','):
  463. guest.uuid = uuid
  464. guest.get_by('uuid')
  465. for uuid in uuids.split(','):
  466. guest.uuid = uuid
  467. guest.get_by('uuid')
  468. message = {
  469. '_object': 'guest',
  470. 'action': 'resume',
  471. 'uuid': uuid,
  472. 'node_id': guest.node_id
  473. }
  474. Utils.emit_instruction(message=json.dumps(message))
  475. ret = dict()
  476. ret['state'] = ji.Common.exchange_state(20000)
  477. return ret
  478. except ji.PreviewingError, e:
  479. return json.loads(e.message)
  480. @Utils.dumps2response
  481. def r_delete(uuids):
  482. args_rules = [
  483. Rules.UUIDS.value
  484. ]
  485. # TODO: 加入是否删除使用的数据磁盘开关,如果为True,则顺便删除使用的磁盘。否则解除该磁盘被使用的状态。
  486. try:
  487. ji.Check.previewing(args_rules, {'uuids': uuids})
  488. guest = Guest()
  489. # 检测所指定的 UUDIs 实例都存在
  490. for uuid in uuids.split(','):
  491. guest.uuid = uuid
  492. guest.get_by('uuid')
  493. config = Config()
  494. config.id = 1
  495. config.get()
  496. # 执行删除操作
  497. for uuid in uuids.split(','):
  498. guest.uuid = uuid
  499. guest.get_by('uuid')
  500. message = {
  501. '_object': 'guest',
  502. 'action': 'delete',
  503. 'uuid': uuid,
  504. 'storage_mode': config.storage_mode,
  505. 'dfs_volume': config.dfs_volume,
  506. 'node_id': guest.node_id
  507. }
  508. Utils.emit_instruction(message=json.dumps(message))
  509. # 删除创建失败的 Guest
  510. if guest.status == status.GuestState.dirty.value:
  511. disk = Disk()
  512. disk.uuid = guest.uuid
  513. disk.get_by('uuid')
  514. if disk.state == status.DiskState.pending.value:
  515. disk.delete()
  516. guest.delete()
  517. SSHKeyGuestMapping.delete_by_filter(filter_str=':'.join(['guest_uuid', 'eq', guest.uuid]))
  518. ret = dict()
  519. ret['state'] = ji.Common.exchange_state(20000)
  520. return ret
  521. except ji.PreviewingError, e:
  522. return json.loads(e.message)
  523. @Utils.dumps2response
  524. def r_attach_disk(uuid, disk_uuid):
  525. args_rules = [
  526. Rules.UUID.value,
  527. Rules.DISK_UUID.value
  528. ]
  529. try:
  530. ji.Check.previewing(args_rules, {'uuid': uuid, 'disk_uuid': disk_uuid})
  531. guest = Guest()
  532. guest.uuid = uuid
  533. guest.get_by('uuid')
  534. disk = Disk()
  535. disk.uuid = disk_uuid
  536. disk.get_by('uuid')
  537. config = Config()
  538. config.id = 1
  539. config.get()
  540. ret = dict()
  541. ret['state'] = ji.Common.exchange_state(20000)
  542. # 判断欲挂载的磁盘是否空闲
  543. if disk.guest_uuid.__len__() > 0 or disk.state != DiskState.idle.value:
  544. ret['state'] = ji.Common.exchange_state(41258)
  545. return ret
  546. # 判断 Guest 是否处于可用状态
  547. if guest.status in (status.GuestState.no_state.value, status.GuestState.dirty.value):
  548. ret['state'] = ji.Common.exchange_state(41259)
  549. return ret
  550. # 判断 Guest 与 磁盘是否在同一宿主机上
  551. if config.storage_mode in [status.StorageMode.local.value, status.StorageMode.shared_mount.value]:
  552. if guest.node_id != disk.node_id:
  553. ret['state'] = ji.Common.exchange_state(41260)
  554. return ret
  555. # 通过检测未被使用的序列,来确定当前磁盘在目标 Guest 身上的序列
  556. disk.guest_uuid = guest.uuid
  557. disks, count = disk.get_by_filter(filter_str='guest_uuid:in:' + guest.uuid)
  558. already_used_sequence = list()
  559. for _disk in disks:
  560. already_used_sequence.append(_disk['sequence'])
  561. for sequence in range(0, dev_table.__len__()):
  562. if sequence not in already_used_sequence:
  563. disk.sequence = sequence
  564. break
  565. disk.state = DiskState.mounting.value
  566. guest_xml = GuestXML(guest=guest, disk=disk, config=config)
  567. message = {
  568. '_object': 'guest',
  569. 'action': 'attach_disk',
  570. 'uuid': uuid,
  571. 'node_id': guest.node_id,
  572. 'xml': guest_xml.get_disk(),
  573. 'passback_parameters': {'disk_uuid': disk.uuid, 'sequence': disk.sequence},
  574. 'disks': [disk.__dict__]
  575. }
  576. Utils.emit_instruction(message=json.dumps(message))
  577. disk.update()
  578. return ret
  579. except ji.PreviewingError, e:
  580. return json.loads(e.message)
  581. @Utils.dumps2response
  582. def r_detach_disk(disk_uuid):
  583. args_rules = [
  584. Rules.DISK_UUID.value
  585. ]
  586. try:
  587. ji.Check.previewing(args_rules, {'disk_uuid': disk_uuid})
  588. disk = Disk()
  589. disk.uuid = disk_uuid
  590. disk.get_by('uuid')
  591. ret = dict()
  592. ret['state'] = ji.Common.exchange_state(20000)
  593. if disk.state != DiskState.mounted.value or disk.sequence == 0:
  594. # 表示未被任何实例使用,已被分离
  595. # 序列为 0 的表示实例系统盘,系统盘不可以被分离
  596. # TODO: 系统盘单独范围其它状态
  597. return ret
  598. guest = Guest()
  599. guest.uuid = disk.guest_uuid
  600. guest.get_by('uuid')
  601. # 判断 Guest 是否处于可用状态
  602. if guest.status in (status.GuestState.no_state.value, status.GuestState.dirty.value):
  603. ret['state'] = ji.Common.exchange_state(41259)
  604. return ret
  605. config = Config()
  606. config.id = 1
  607. config.get()
  608. guest_xml = GuestXML(guest=guest, disk=disk, config=config)
  609. message = {
  610. '_object': 'guest',
  611. 'action': 'detach_disk',
  612. 'uuid': disk.guest_uuid,
  613. 'node_id': guest.node_id,
  614. 'xml': guest_xml.get_disk(),
  615. 'passback_parameters': {'disk_uuid': disk.uuid}
  616. }
  617. Utils.emit_instruction(message=json.dumps(message))
  618. disk.state = DiskState.unloading.value
  619. disk.update()
  620. return ret
  621. except ji.PreviewingError, e:
  622. return json.loads(e.message)
  623. @Utils.dumps2response
  624. def r_migrate(uuids, node_id):
  625. args_rules = [
  626. Rules.UUIDS.value,
  627. Rules.NODE_ID.value
  628. ]
  629. try:
  630. ji.Check.previewing(args_rules, {'uuids': uuids, 'node_id': node_id})
  631. ret = dict()
  632. ret['state'] = ji.Common.exchange_state(20000)
  633. config = Config()
  634. config.id = 1
  635. config.get()
  636. # 取全部活着的 hosts
  637. available_hosts = Host.get_available_hosts(nonrandom=None)
  638. if available_hosts.__len__() == 0:
  639. ret['state'] = ji.Common.exchange_state(50351)
  640. return ret
  641. available_hosts_mapping_by_node_id = dict()
  642. for host in available_hosts:
  643. if host['node_id'] not in available_hosts_mapping_by_node_id:
  644. available_hosts_mapping_by_node_id[host['node_id']] = host
  645. dst_ip = available_hosts_mapping_by_node_id[node_id]['interfaces'][config.vm_manage_network]['ip']
  646. guest = Guest()
  647. for uuid in uuids.split(','):
  648. guest.uuid = uuid
  649. guest.get_by('uuid')
  650. for uuid in uuids.split(','):
  651. guest.uuid = uuid
  652. guest.get_by('uuid')
  653. # 忽略宕机计算节点 上面的 虚拟机 迁移请求
  654. # 忽略目标计算节点 等于 当前所在 计算节点 的虚拟机 迁移请求
  655. if guest.node_id.__str__() not in available_hosts_mapping_by_node_id or guest.node_id.__str__() == node_id:
  656. continue
  657. message = {
  658. '_object': 'guest',
  659. 'action': 'migrate',
  660. 'uuid': uuid,
  661. 'node_id': guest.node_id,
  662. 'storage_mode': config.storage_mode,
  663. 'duri': 'qemu+ssh://' + dst_ip + '/system'
  664. }
  665. Utils.emit_instruction(message=json.dumps(message))
  666. return ret
  667. except ji.PreviewingError, e:
  668. return json.loads(e.message)
  669. @Utils.dumps2response
  670. def r_get(uuids):
  671. ret = guest_base.get(ids=uuids, ids_rule=Rules.UUIDS.value, by_field='uuid')
  672. if '200' != ret['state']['code']:
  673. return ret
  674. rows, _ = SSHKeyGuestMapping.get_by_filter(filter_str=':'.join(['guest_uuid', 'in', uuids]))
  675. guest_uuid_ssh_key_id_mapping = dict()
  676. ssh_keys_id = list()
  677. for row in rows:
  678. if row['ssh_key_id'] not in ssh_keys_id:
  679. ssh_keys_id.append(row['ssh_key_id'].__str__())
  680. if row['guest_uuid'] not in guest_uuid_ssh_key_id_mapping:
  681. guest_uuid_ssh_key_id_mapping[row['guest_uuid']] = list()
  682. guest_uuid_ssh_key_id_mapping[row['guest_uuid']].append(row['ssh_key_id'])
  683. rows, _ = SSHKey.get_by_filter(filter_str=':'.join(['id', 'in', ','.join(ssh_keys_id)]))
  684. ssh_key_id_mapping = dict()
  685. for row in rows:
  686. row['url'] = url_for('v_ssh_keys.show')
  687. ssh_key_id_mapping[row['id']] = row
  688. hosts_url = url_for('api_hosts.r_get_by_filter', _external=True)
  689. hosts_ret = requests.get(url=hosts_url, cookies=request.cookies)
  690. hosts_ret = json.loads(hosts_ret.content)
  691. hosts_mapping_by_node_id = dict()
  692. for host in hosts_ret['data']:
  693. hosts_mapping_by_node_id[int(host['node_id'])] = host
  694. if -1 == uuids.find(','):
  695. if 'ssh_keys' not in ret['data']:
  696. ret['data']['ssh_keys'] = list()
  697. if ret['data']['uuid'] in guest_uuid_ssh_key_id_mapping:
  698. for ssh_key_id in guest_uuid_ssh_key_id_mapping[ret['data']['uuid']]:
  699. if ssh_key_id not in ssh_key_id_mapping:
  700. continue
  701. ret['data']['ssh_keys'].append(ssh_key_id_mapping[ssh_key_id])
  702. if not hosts_mapping_by_node_id[ret['data']['node_id']]['alive']:
  703. ret['data']['status'] = GuestState.no_state.value
  704. else:
  705. for i, guest in enumerate(ret['data']):
  706. if 'ssh_keys' not in ret['data'][i]:
  707. ret['data'][i]['ssh_keys'] = list()
  708. if ret['data'][i]['uuid'] in guest_uuid_ssh_key_id_mapping:
  709. for ssh_key_id in guest_uuid_ssh_key_id_mapping[ret['data'][i]['uuid']]:
  710. if ssh_key_id not in ssh_key_id_mapping:
  711. continue
  712. ret['data'][i]['ssh_keys'].append(ssh_key_id_mapping[ssh_key_id])
  713. if not hosts_mapping_by_node_id[ret['data'][i]['node_id']]['alive']:
  714. ret['data'][i]['status'] = GuestState.no_state.value
  715. return ret
  716. def exchange_guest_os_templates_logo(os_templates_image_mapping_by_id=None, os_templates_profile_mapping_by_id=None,
  717. os_template_image_id=None):
  718. assert isinstance(os_templates_image_mapping_by_id, dict)
  719. assert isinstance(os_templates_profile_mapping_by_id, dict)
  720. assert isinstance(os_template_image_id, int)
  721. if os_templates_image_mapping_by_id[os_template_image_id]['logo'] == "":
  722. logo = os_templates_profile_mapping_by_id[os_templates_image_mapping_by_id[os_template_image_id]['os_template_profile_id']]['icon']
  723. else:
  724. logo = os_templates_image_mapping_by_id[os_template_image_id]['logo']
  725. label = os_templates_image_mapping_by_id[os_template_image_id]['label']
  726. return logo, label
  727. def format_guest_status(_status, progress):
  728. from jimvc.models import GuestState
  729. color = 'FF645B'
  730. icon = 'glyph-icon icon-bolt'
  731. desc = '未知状态'
  732. if _status == GuestState.booting.value:
  733. color = '00BBBB'
  734. icon = 'glyph-icon icon-circle'
  735. desc = '启动中'
  736. elif _status == GuestState.running.value:
  737. color = '00BB00'
  738. icon = 'glyph-icon icon-circle'
  739. desc = '运行中'
  740. elif _status == GuestState.creating.value:
  741. color = 'FFC543'
  742. icon = 'glyph-icon icon-spinner'
  743. desc = ' '.join(['创建中', str(progress) + '%'])
  744. elif _status == GuestState.blocked.value:
  745. color = '3D4245'
  746. icon = 'glyph-icon icon-minus-square'
  747. desc = '被阻塞'
  748. elif _status == GuestState.paused.value:
  749. color = 'B7B904'
  750. icon = 'glyph-icon icon-pause'
  751. desc = '暂停'
  752. elif _status == GuestState.shutdown.value:
  753. color = '4E5356'
  754. icon = 'glyph-icon icon-terminal'
  755. desc = '关闭'
  756. elif _status == GuestState.shutoff.value:
  757. color = 'FFC543'
  758. icon = 'glyph-icon icon-plug'
  759. desc = '断电'
  760. elif _status == GuestState.crashed.value:
  761. color = '9E2927'
  762. icon = 'glyph-icon icon-question'
  763. desc = '已崩溃'
  764. elif _status == GuestState.pm_suspended.value:
  765. color = 'FCFF07'
  766. icon = 'glyph-icon icon-anchor'
  767. desc = '悬挂'
  768. elif _status == GuestState.migrating.value:
  769. color = '1CF5E7'
  770. icon = 'glyph-icon icon-space-shuttle'
  771. desc = '迁移中'
  772. elif _status == GuestState.dirty.value:
  773. color = 'FF0707'
  774. icon = 'glyph-icon icon-remove'
  775. desc = '创建失败,待清理'
  776. else:
  777. pass
  778. return '<span class="{icon}" style="color: #{color};">&nbsp;&nbsp;{desc}</span>'.format(
  779. icon=icon, color=color, desc=desc)
  780. def exchange_guest_bandwidth(bandwidth=None):
  781. assert isinstance(bandwidth, int)
  782. if bandwidth == 0:
  783. bandwidth = '<span style="font-size: 16px;" title="无限带宽">&nbsp;∞</span>'
  784. elif 0 < bandwidth < 1000 ** 2:
  785. bandwidth = str(bandwidth // 1000) + ' Kbps'
  786. elif 1000 ** 2 <= bandwidth < 1000 ** 3:
  787. bandwidth = str(bandwidth // 1000 ** 2) + ' Mbps'
  788. else:
  789. bandwidth = str(bandwidth // 1000 ** 3) + ' Gbps'
  790. return bandwidth
  791. @Utils.dumps2response
  792. def r_get_by_filter():
  793. ret = guest_base.get_by_filter()
  794. uuids = list()
  795. for guest in ret['data']:
  796. uuids.append(guest['uuid'])
  797. rows, _ = SSHKeyGuestMapping.get_by_filter(filter_str=':'.join(['guest_uuid', 'in', ','.join(uuids)]))
  798. guest_uuid_ssh_key_id_mapping = dict()
  799. ssh_keys_id = list()
  800. for row in rows:
  801. if row['ssh_key_id'] not in ssh_keys_id:
  802. ssh_keys_id.append(row['ssh_key_id'].__str__())
  803. if row['guest_uuid'] not in guest_uuid_ssh_key_id_mapping:
  804. guest_uuid_ssh_key_id_mapping[row['guest_uuid']] = list()
  805. guest_uuid_ssh_key_id_mapping[row['guest_uuid']].append(row['ssh_key_id'])
  806. rows, _ = SSHKey.get_by_filter(filter_str=':'.join(['id', 'in', ','.join(ssh_keys_id)]))
  807. ssh_key_id_mapping = dict()
  808. for row in rows:
  809. row['url'] = url_for('v_ssh_keys.show')
  810. ssh_key_id_mapping[row['id']] = row
  811. rows, _ = Snapshot.get_by_filter(filter_str=':'.join(['guest_uuid', 'in', ','.join(uuids)]))
  812. snapshots_guest_uuid_mapping = dict()
  813. for row in rows:
  814. guest_uuid = row['guest_uuid']
  815. if guest_uuid not in snapshots_guest_uuid_mapping:
  816. snapshots_guest_uuid_mapping[guest_uuid] = list()
  817. snapshots_guest_uuid_mapping[guest_uuid].append(row)
  818. hosts_url = url_for('api_hosts.r_get_by_filter', _external=True)
  819. hosts_ret = requests.get(url=hosts_url, cookies=request.cookies)
  820. hosts_ret = json.loads(hosts_ret.content)
  821. hosts_mapping_by_node_id = dict()
  822. for host in hosts_ret['data']:
  823. hosts_mapping_by_node_id[int(host['node_id'])] = host
  824. os_templates_image, _ = OSTemplateImage.get_by_filter()
  825. os_templates_image_mapping_by_id = dict()
  826. for os_template_image in os_templates_image:
  827. os_templates_image_mapping_by_id[os_template_image['id']] = os_template_image
  828. os_templates_profile, _ = OSTemplateProfile.get_by_filter()
  829. os_templates_profile_mapping_by_id = dict()
  830. for os_template_profile in os_templates_profile:
  831. os_templates_profile_mapping_by_id[os_template_profile['id']] = os_template_profile
  832. for i, guest in enumerate(ret['data']):
  833. guest_uuid = ret['data'][i]['uuid']
  834. if 'ssh_keys' not in ret['data'][i]:
  835. ret['data'][i]['ssh_keys'] = list()
  836. if guest_uuid in guest_uuid_ssh_key_id_mapping:
  837. for ssh_key_id in guest_uuid_ssh_key_id_mapping[guest_uuid]:
  838. if ssh_key_id not in ssh_key_id_mapping:
  839. continue
  840. ret['data'][i]['ssh_keys'].append(ssh_key_id_mapping[ssh_key_id])
  841. if 'snapshot' not in ret['data'][i]:
  842. ret['data'][i]['snapshot'] = {
  843. 'creatable': True,
  844. 'mapping': list()
  845. }
  846. if guest_uuid in snapshots_guest_uuid_mapping:
  847. ret['data'][i]['snapshot']['mapping'] = snapshots_guest_uuid_mapping[guest_uuid]
  848. for snapshot in snapshots_guest_uuid_mapping[guest_uuid]:
  849. if snapshot['progress'] == 100:
  850. continue
  851. else:
  852. ret['data'][i]['snapshot']['creatable'] = False
  853. if not hosts_mapping_by_node_id[ret['data'][i]['node_id']]['alive']:
  854. ret['data'][i]['status'] = GuestState.no_state.value
  855. ret['data'][i]['hostname'] = hosts_mapping_by_node_id[guest['node_id']]['hostname']
  856. ret['data'][i]['html'] = dict()
  857. ret['data'][i]['html']['logo'], ret['data'][i]['html']['os_template_label'] = exchange_guest_os_templates_logo(
  858. os_templates_image_mapping_by_id=os_templates_image_mapping_by_id,
  859. os_templates_profile_mapping_by_id=os_templates_profile_mapping_by_id,
  860. os_template_image_id=guest['os_template_image_id'])
  861. ret['data'][i]['html']['status'] = format_guest_status(_status=guest['status'], progress=guest['progress'])
  862. ret['data'][i]['html']['bandwidth'] = exchange_guest_bandwidth(bandwidth=guest['bandwidth'])
  863. return ret
  864. @Utils.dumps2response
  865. def r_content_search():
  866. ret = guest_base.content_search()
  867. uuids = list()
  868. for guest in ret['data']:
  869. uuids.append(guest['uuid'])
  870. rows, _ = SSHKeyGuestMapping.get_by_filter(filter_str=':'.join(['guest_uuid', 'in', ','.join(uuids)]))
  871. guest_uuid_ssh_key_id_mapping = dict()
  872. ssh_keys_id = list()
  873. for row in rows:
  874. if row['ssh_key_id'] not in ssh_keys_id:
  875. ssh_keys_id.append(row['ssh_key_id'].__str__())
  876. if row['guest_uuid'] not in guest_uuid_ssh_key_id_mapping:
  877. guest_uuid_ssh_key_id_mapping[row['guest_uuid']] = list()
  878. guest_uuid_ssh_key_id_mapping[row['guest_uuid']].append(row['ssh_key_id'])
  879. rows, _ = SSHKey.get_by_filter(filter_str=':'.join(['id', 'in', ','.join(ssh_keys_id)]))
  880. ssh_key_id_mapping = dict()
  881. for row in rows:
  882. row['url'] = url_for('v_ssh_keys.show')
  883. ssh_key_id_mapping[row['id']] = row
  884. rows, _ = Snapshot.get_by_filter(filter_str=':'.join(['guest_uuid', 'in', ','.join(uuids)]))
  885. snapshots_guest_uuid_mapping = dict()
  886. for row in rows:
  887. guest_uuid = row['guest_uuid']
  888. if guest_uuid not in snapshots_guest_uuid_mapping:
  889. snapshots_guest_uuid_mapping[guest_uuid] = list()
  890. snapshots_guest_uuid_mapping[guest_uuid].append(row)
  891. hosts_url = url_for('api_hosts.r_get_by_filter', _external=True)
  892. hosts_ret = requests.get(url=hosts_url, cookies=request.cookies)
  893. hosts_ret = json.loads(hosts_ret.content)
  894. hosts_mapping_by_node_id = dict()
  895. for host in hosts_ret['data']:
  896. hosts_mapping_by_node_id[int(host['node_id'])] = host
  897. os_templates_image, _ = OSTemplateImage.get_by_filter()
  898. os_templates_image_mapping_by_id = dict()
  899. for os_template_image in os_templates_image:
  900. os_templates_image_mapping_by_id[os_template_image['id']] = os_template_image
  901. os_templates_profile, _ = OSTemplateProfile.get_by_filter()
  902. os_templates_profile_mapping_by_id = dict()
  903. for os_template_profile in os_templates_profile:
  904. os_templates_profile_mapping_by_id[os_template_profile['id']] = os_template_profile
  905. for i, guest in enumerate(ret['data']):
  906. guest_uuid = ret['data'][i]['uuid']
  907. if 'ssh_keys' not in ret['data'][i]:
  908. ret['data'][i]['ssh_keys'] = list()
  909. if guest_uuid in guest_uuid_ssh_key_id_mapping:
  910. for ssh_key_id in guest_uuid_ssh_key_id_mapping[guest_uuid]:
  911. if ssh_key_id not in ssh_key_id_mapping:
  912. continue
  913. ret['data'][i]['ssh_keys'].append(ssh_key_id_mapping[ssh_key_id])
  914. if 'snapshot' not in ret['data'][i]:
  915. ret['data'][i]['snapshot'] = {
  916. 'creatable': True,
  917. 'mapping': list()
  918. }
  919. if guest_uuid in snapshots_guest_uuid_mapping:
  920. ret['data'][i]['snapshot']['mapping'] = snapshots_guest_uuid_mapping[guest_uuid]
  921. for snapshot in snapshots_guest_uuid_mapping[guest_uuid]:
  922. if snapshot['progress'] == 100:
  923. continue
  924. else:
  925. ret['data'][i]['snapshot']['creatable'] = False
  926. if not hosts_mapping_by_node_id[ret['data'][i]['node_id']]['alive']:
  927. ret['data'][i]['status'] = GuestState.no_state.value
  928. ret['data'][i]['hostname'] = hosts_mapping_by_node_id[guest['node_id']]['hostname']
  929. ret['data'][i]['html'] = dict()
  930. ret['data'][i]['html']['logo'], ret['data'][i]['html']['os_template_label'] = exchange_guest_os_templates_logo(
  931. os_templates_image_mapping_by_id=os_templates_image_mapping_by_id,
  932. os_templates_profile_mapping_by_id=os_templates_profile_mapping_by_id,
  933. os_template_image_id=guest['os_template_image_id'])
  934. ret['data'][i]['html']['status'] = format_guest_status(_status=guest['status'], progress=guest['progress'])
  935. ret['data'][i]['html']['bandwidth'] = exchange_guest_bandwidth(bandwidth=guest['bandwidth'])
  936. return ret
  937. @Utils.dumps2response
  938. def r_distribute_count():
  939. from jimvc.models import Guest
  940. rows, count = Guest.get_all()
  941. ret = dict()
  942. ret['state'] = ji.Common.exchange_state(20000)
  943. ret['data'] = {
  944. 'os_template_image_id': dict(),
  945. 'status': dict(),
  946. 'node_id': dict(),
  947. 'cpu_memory': dict(),
  948. 'cpu': 0,
  949. 'memory': 0,
  950. 'guests': rows.__len__()
  951. }
  952. for guest in rows:
  953. if guest['os_template_image_id'] not in ret['data']['os_template_image_id']:
  954. ret['data']['os_template_image_id'][guest['os_template_image_id']] = 0
  955. if guest['status'] not in ret['data']['status']:
  956. ret['data']['status'][guest['status']] = 0
  957. if guest['node_id'] not in ret['data']['node_id']:
  958. ret['data']['node_id'][guest['node_id']] = 0
  959. cpu_memory = '_'.join([str(guest['cpu']), str(guest['memory'])])
  960. if cpu_memory not in ret['data']['cpu_memory']:
  961. ret['data']['cpu_memory'][cpu_memory] = 0
  962. ret['data']['os_template_image_id'][guest['os_template_image_id']] += 1
  963. ret['data']['status'][guest['status']] += 1
  964. ret['data']['node_id'][guest['node_id']] += 1
  965. ret['data']['cpu_memory'][cpu_memory] += 1
  966. ret['data']['cpu'] += guest['cpu']
  967. ret['data']['memory'] += guest['memory']
  968. return ret
  969. @Utils.dumps2response
  970. def r_update(uuids):
  971. ret = dict()
  972. ret['state'] = ji.Common.exchange_state(20000)
  973. ret['data'] = list()
  974. args_rules = [
  975. Rules.UUIDS.value
  976. ]
  977. if 'remark' in request.json:
  978. args_rules.append(
  979. Rules.REMARK.value,
  980. )
  981. if args_rules.__len__() < 2:
  982. return ret
  983. request.json['uuids'] = uuids
  984. try:
  985. ji.Check.previewing(args_rules, request.json)
  986. guest = Guest()
  987. # 检测所指定的 UUDIs 实例都存在
  988. for uuid in uuids.split(','):
  989. guest.uuid = uuid
  990. guest.get_by('uuid')
  991. for uuid in uuids.split(','):
  992. guest.uuid = uuid
  993. guest.get_by('uuid')
  994. guest.remark = request.json.get('remark', guest.remark)
  995. guest.update()
  996. guest.get()
  997. ret['data'].append(guest.__dict__)
  998. return ret
  999. except ji.PreviewingError, e:
  1000. return json.loads(e.message)
  1001. @Utils.dumps2response
  1002. def r_revise_ip(uuid, ip):
  1003. ret = dict()
  1004. ret['state'] = ji.Common.exchange_state(20000)
  1005. args_rules = [
  1006. Rules.UUID.value,
  1007. Rules.IP.value
  1008. ]
  1009. try:
  1010. ji.Check.previewing(args_rules, {'uuid': uuid, 'ip': ip})
  1011. guest = Guest()
  1012. guest.uuid = uuid
  1013. guest.get_by('uuid')
  1014. guest.ip = ip
  1015. guest.update()
  1016. guest.get()
  1017. ret['data'] = guest.__dict__
  1018. return ret
  1019. except ji.PreviewingError, e:
  1020. return json.loads(e.message)
  1021. @Utils.dumps2response
  1022. def r_reset_password(uuids, password):
  1023. args_rules = [
  1024. Rules.UUIDS.value,
  1025. Rules.PASSWORD.value
  1026. ]
  1027. try:
  1028. ji.Check.previewing(args_rules, {'uuids': uuids, 'password': password})
  1029. guest = Guest()
  1030. os_template_image = OSTemplateImage()
  1031. os_template_profile = OSTemplateProfile()
  1032. # 检测所指定的 UUDIs 实例都存在
  1033. for uuid in uuids.split(','):
  1034. guest.uuid = uuid
  1035. guest.get_by('uuid')
  1036. for uuid in uuids.split(','):
  1037. guest.uuid = uuid
  1038. guest.get_by('uuid')
  1039. os_template_image.id = guest.os_template_image_id
  1040. os_template_image.get()
  1041. os_template_profile.id = os_template_image.os_template_profile_id
  1042. os_template_profile.get()
  1043. user = 'root'
  1044. if os_template_profile.os_type == 'windows':
  1045. user = 'administrator'
  1046. # guest.password 由 guest 事件处理机更新。参见 @models/event_processory.py:189 附近。
  1047. message = {
  1048. '_object': 'guest',
  1049. 'action': 'reset_password',
  1050. 'uuid': guest.uuid,
  1051. 'node_id': guest.node_id,
  1052. 'os_type': os_template_profile.os_type,
  1053. 'user': user,
  1054. 'password': password,
  1055. 'passback_parameters': {'password': password}
  1056. }
  1057. Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False))
  1058. ret = dict()
  1059. ret['state'] = ji.Common.exchange_state(20000)
  1060. return ret
  1061. except ji.PreviewingError, e:
  1062. return json.loads(e.message)
  1063. @Utils.dumps2response
  1064. def r_allocate_bandwidth(uuids, bandwidth, bandwidth_unit):
  1065. args_rules = [
  1066. Rules.UUIDS.value,
  1067. Rules.BANDWIDTH_IN_URL.value,
  1068. Rules.BANDWIDTH_UNIT.value,
  1069. ]
  1070. try:
  1071. ji.Check.previewing(args_rules, {'uuids': uuids, 'bandwidth': bandwidth, 'bandwidth_unit': bandwidth_unit})
  1072. ret = dict()
  1073. ret['state'] = ji.Common.exchange_state(20000)
  1074. bandwidth = int(bandwidth)
  1075. if bandwidth_unit == 'k':
  1076. bandwidth = bandwidth * 1000
  1077. elif bandwidth_unit == 'm':
  1078. bandwidth = bandwidth * 1000 ** 2
  1079. elif bandwidth_unit == 'g':
  1080. bandwidth = bandwidth * 1000 ** 3
  1081. else:
  1082. ret['state'] = ji.Common.exchange_state(41203)
  1083. return ret
  1084. # http://man7.org/linux/man-pages/man8/tc.8.html
  1085. # 如果带宽大于 tc 所控最大速率,则置其为无限带宽
  1086. # 34359738360 等于 tc 最大可控字节速率,换算出的比特位
  1087. if bandwidth > 34359738360:
  1088. bandwidth = 0
  1089. guest = Guest()
  1090. # 检测所指定的 UUDIs 实例都存在
  1091. for uuid in uuids.split(','):
  1092. guest.uuid = uuid
  1093. guest.get_by('uuid')
  1094. for uuid in uuids.split(','):
  1095. guest.uuid = uuid
  1096. guest.get_by('uuid')
  1097. guest.bandwidth = bandwidth
  1098. message = {
  1099. '_object': 'guest',
  1100. 'action': 'allocate_bandwidth',
  1101. 'uuid': guest.uuid,
  1102. 'node_id': guest.node_id,
  1103. 'bandwidth': guest.bandwidth,
  1104. 'passback_parameters': {'bandwidth': guest.bandwidth}
  1105. }
  1106. Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False))
  1107. return ret
  1108. except ji.PreviewingError, e:
  1109. return json.loads(e.message)
  1110. @Utils.dumps2response
  1111. def r_adjust_ability(uuids, cpu, memory):
  1112. args_rules = [
  1113. Rules.UUIDS.value,
  1114. Rules.CPU.value,
  1115. Rules.MEMORY.value,
  1116. ]
  1117. try:
  1118. ret = dict()
  1119. ret['state'] = ji.Common.exchange_state(20000)
  1120. cpu = int(cpu)
  1121. memory = int(memory)
  1122. ji.Check.previewing(args_rules, {'uuids': uuids, 'cpu': cpu, 'memory': memory})
  1123. not_ready_yet_of_guests = list()
  1124. guest = Guest()
  1125. # 检测所指定的 UUDIs 实例都存在。且状态都为可以操作状态(即关闭状态)。
  1126. for uuid in uuids.split(','):
  1127. guest.uuid = uuid
  1128. guest.get_by('uuid')
  1129. if guest.status != status.GuestState.shutoff.value:
  1130. not_ready_yet_of_guests.append(guest.__dict__)
  1131. if not_ready_yet_of_guests.__len__() > 0:
  1132. ret['state'] = ji.Common.exchange_state(41261)
  1133. ret['data'] = not_ready_yet_of_guests
  1134. return ret
  1135. for uuid in uuids.split(','):
  1136. guest.uuid = uuid
  1137. guest.get_by('uuid')
  1138. guest.cpu = cpu
  1139. guest.memory = memory
  1140. message = {
  1141. '_object': 'guest',
  1142. 'action': 'adjust_ability',
  1143. 'uuid': guest.uuid,
  1144. 'node_id': guest.node_id,
  1145. 'cpu': guest.cpu,
  1146. 'memory': guest.memory,
  1147. 'passback_parameters': {'cpu': guest.cpu, 'memory': guest.memory}
  1148. }
  1149. Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False))
  1150. return ret
  1151. except ji.PreviewingError, e:
  1152. return json.loads(e.message)
  1153. @Utils.dumps2response
  1154. def r_change_prepared_by(uuids, service_id):
  1155. ret = dict()
  1156. ret['state'] = ji.Common.exchange_state(20000)
  1157. ret['data'] = list()
  1158. args_rules = [
  1159. Rules.UUIDS.value,
  1160. Rules.SERVICE_ID_IN_URL.value
  1161. ]
  1162. try:
  1163. ji.Check.previewing(args_rules, {'uuids': uuids, 'service_id': service_id})
  1164. guest = Guest()
  1165. # 检测所指定的 UUDIs 实例都存在
  1166. for uuid in uuids.split(','):
  1167. guest.uuid = uuid
  1168. guest.get_by('uuid')
  1169. for uuid in uuids.split(','):
  1170. guest.uuid = uuid
  1171. guest.get_by('uuid')
  1172. guest.service_id = int(service_id)
  1173. guest.update()
  1174. guest.get()
  1175. ret['data'].append(guest.__dict__)
  1176. return ret
  1177. except ji.PreviewingError, e:
  1178. return json.loads(e.message)
  1179. @Utils.dumps2response
  1180. def r_refresh_guest_state():
  1181. try:
  1182. ret = dict()
  1183. ret['state'] = ji.Common.exchange_state(20000)
  1184. # 取全部活着的 hosts
  1185. available_hosts = Host.get_available_hosts(nonrandom=None)
  1186. if available_hosts.__len__() == 0:
  1187. ret['state'] = ji.Common.exchange_state(50351)
  1188. return ret
  1189. for host in available_hosts:
  1190. message = {
  1191. '_object': 'global',
  1192. 'action': 'refresh_guest_state',
  1193. 'node_id': host['node_id']
  1194. }
  1195. Utils.emit_instruction(message=json.dumps(message, ensure_ascii=False))
  1196. except ji.PreviewingError, e:
  1197. return json.loads(e.message)
  1198. @Utils.dumps2response
  1199. def r_show():
  1200. args = list()
  1201. page = int(request.args.get('page', 1))
  1202. page_size = int(request.args.get('page_size', 20))
  1203. keyword = request.args.get('keyword', None)
  1204. if page is not None:
  1205. args.append('page=' + page.__str__())
  1206. if page_size is not None:
  1207. args.append('page_size=' + page_size.__str__())
  1208. if keyword is not None:
  1209. args.append('keyword=' + keyword.__str__())
  1210. hosts_url = url_for('api_hosts.r_get_by_filter', _external=True)
  1211. guests_url = url_for('api_guests.r_get_by_filter', _external=True)
  1212. if keyword is not None:
  1213. guests_url = url_for('api_guests.r_content_search', _external=True)
  1214. if args.__len__() > 0:
  1215. guests_url = guests_url + '?' + '&'.join(args)
  1216. hosts_ret = requests.get(url=hosts_url, cookies=request.cookies)
  1217. hosts_ret = json.loads(hosts_ret.content)
  1218. hosts_mapping_by_node_id = dict()
  1219. for host in hosts_ret['data']:
  1220. hosts_mapping_by_node_id[int(host['node_id'])] = host
  1221. guests_ret = requests.get(url=guests_url, cookies=request.cookies)
  1222. guests_ret = json.loads(guests_ret.content)
  1223. os_templates_image, _ = OSTemplateImage.get_by_filter()
  1224. os_templates_image_mapping_by_id = dict()
  1225. for os_template_image in os_templates_image:
  1226. os_templates_image_mapping_by_id[os_template_image['id']] = os_template_image
  1227. os_templates_profile, _ = OSTemplateProfile.get_by_filter()
  1228. os_templates_profile_mapping_by_id = dict()
  1229. for os_template_profile in os_templates_profile:
  1230. os_templates_profile_mapping_by_id[os_template_profile['id']] = os_template_profile
  1231. last_page = int(ceil(guests_ret['paging']['total'] / float(page_size)))
  1232. page_length = 5
  1233. pages = list()
  1234. if page < int(ceil(page_length / 2.0)):
  1235. for i in range(1, page_length + 1):
  1236. pages.append(i)
  1237. if i == last_page or last_page == 0:
  1238. break
  1239. elif last_page - page < page_length / 2:
  1240. for i in range(last_page - page_length + 1, last_page + 1):
  1241. if i < 1:
  1242. continue
  1243. pages.append(i)
  1244. else:
  1245. for i in range(page - page_length / 2, page + int(ceil(page_length / 2.0))):
  1246. pages.append(i)
  1247. if i == last_page or last_page == 0:
  1248. break
  1249. ret = dict()
  1250. ret['state'] = ji.Common.exchange_state(20000)
  1251. ret['data'] = {
  1252. 'guests': guests_ret['data'],
  1253. 'os_templates_image_mapping_by_id': os_templates_image_mapping_by_id,
  1254. 'os_templates_profile_mapping_by_id': os_templates_profile_mapping_by_id,
  1255. 'hosts_mapping_by_node_id': hosts_mapping_by_node_id,
  1256. 'paging': guests_ret['paging'],
  1257. 'page': page,
  1258. 'page_size': page_size,
  1259. 'keyword': keyword,
  1260. 'pages': pages,
  1261. 'last_page': last_page
  1262. }
  1263. return ret
  1264. @Utils.dumps2response
  1265. def r_vnc(uuid):
  1266. guest_ret = guest_base.get(ids=uuid, ids_rule=Rules.UUID.value, by_field='uuid')
  1267. if '200' != guest_ret['state']['code']:
  1268. return guest_ret
  1269. hosts_url = url_for('api_hosts.r_get_by_filter', _external=True)
  1270. hosts_ret = requests.get(url=hosts_url, cookies=request.cookies)
  1271. hosts_ret = json.loads(hosts_ret.content)
  1272. hosts_mapping_by_node_id = dict()
  1273. for host in hosts_ret['data']:
  1274. hosts_mapping_by_node_id[int(host['node_id'])] = host
  1275. port = random.randrange(50000, 60000)
  1276. while True:
  1277. if not Utils.port_is_opened(port=port):
  1278. break
  1279. port = random.randrange(50000, 60000)
  1280. payload = {'listen_port': port, 'target_host': hosts_mapping_by_node_id[guest_ret['data']['node_id']]['hostname'],
  1281. 'target_port': guest_ret['data']['vnc_port']}
  1282. db.r.rpush(app_config['ipc_queue'], json.dumps(payload, ensure_ascii=False))
  1283. time.sleep(1)
  1284. ret = dict()
  1285. ret['state'] = ji.Common.exchange_state(20000)
  1286. ret['data'] = {
  1287. 'port': port,
  1288. 'vnc_password': guest_ret['data']['vnc_password']
  1289. }
  1290. return ret
  1291. @Utils.dumps2response
  1292. def r_detail(uuid):
  1293. hosts_url = url_for('api_hosts.r_get_by_filter', _external=True)
  1294. hosts_ret = requests.get(url=hosts_url, cookies=request.cookies)
  1295. hosts_ret = json.loads(hosts_ret.content)
  1296. hosts_mapping_by_node_id = dict()
  1297. for host in hosts_ret['data']:
  1298. hosts_mapping_by_node_id[int(host['node_id'])] = host
  1299. guest = Guest()
  1300. guest.uuid = uuid
  1301. guest.get_by(field='uuid')
  1302. guest.ssh_keys = list()
  1303. rows, _ = SSHKeyGuestMapping.get_by_filter(filter_str=':'.join(['guest_uuid', 'in', guest.uuid]))
  1304. ssh_keys_id = list()
  1305. for row in rows:
  1306. if row['ssh_key_id'] not in ssh_keys_id:
  1307. ssh_keys_id.append(row['ssh_key_id'].__str__())
  1308. rows, _ = SSHKey.get_by_filter(filter_str=':'.join(['id', 'in', ','.join(ssh_keys_id)]))
  1309. for row in rows:
  1310. row['url'] = url_for('v_ssh_keys.show')
  1311. if row['id'].__str__() not in ssh_keys_id:
  1312. continue
  1313. guest.ssh_keys.append(row)
  1314. os_template_image = OSTemplateImage()
  1315. os_template_image.id = guest.os_template_image_id.__str__()
  1316. os_template_image.get()
  1317. os_template_profiles, _ = OSTemplateProfile.get_by_filter()
  1318. os_templates_profile_mapping_by_id = dict()
  1319. for os_template_profile in os_template_profiles:
  1320. os_templates_profile_mapping_by_id[os_template_profile['id']] = os_template_profile
  1321. disks_url = url_for('api_disks.r_get_by_filter', filter='guest_uuid:in:' + guest.uuid, _external=True)
  1322. disks_ret = requests.get(url=disks_url, cookies=request.cookies)
  1323. disks = json.loads(disks_ret.content)['data']
  1324. if not hosts_mapping_by_node_id[guest.node_id]['alive']:
  1325. guest.status = GuestState.no_state.value
  1326. config = Config()
  1327. config.id = 1
  1328. config.get()
  1329. ret = dict()
  1330. ret['state'] = ji.Common.exchange_state(20000)
  1331. ret['data'] = {
  1332. 'uuid': uuid,
  1333. 'guest': guest.__dict__,
  1334. 'os_template_image': os_template_image.__dict__,
  1335. 'os_templates_profile_mapping_by_id': os_templates_profile_mapping_by_id,
  1336. 'hosts_mapping_by_node_id': hosts_mapping_by_node_id,
  1337. 'disks': disks,
  1338. 'config': config.__dict__
  1339. }
  1340. return ret