guest.py 59 KB

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