config.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import json
  4. from flask import Blueprint, render_template, url_for, request
  5. import requests
  6. from math import ceil
  7. import re
  8. __author__ = 'James Iter'
  9. __date__ = '2017/8/29'
  10. __contact__ = 'james.iter.cn@gmail.com'
  11. __copyright__ = '(c) 2017 by James Iter.'
  12. blueprint = Blueprint(
  13. 'v_config',
  14. __name__,
  15. url_prefix='/config'
  16. )
  17. blueprints = Blueprint(
  18. 'v_configs',
  19. __name__,
  20. url_prefix='/configs'
  21. )
  22. def show():
  23. args = list()
  24. page = int(request.args.get('page', 1))
  25. page_size = int(request.args.get('page_size', 10))
  26. keyword = request.args.get('keyword', None)
  27. resource_path = request.path
  28. if page is not None:
  29. args.append('page=' + page.__str__())
  30. if page_size is not None:
  31. args.append('page_size=' + page_size.__str__())
  32. if keyword is not None:
  33. args.append('keyword=' + keyword.__str__())
  34. host_url = request.host_url.rstrip('/')
  35. guests_url = host_url + url_for('api_guests.r_get_by_filter')
  36. if keyword is not None:
  37. guests_url = host_url + url_for('api_guests.r_content_search')
  38. os_templates_url = host_url + url_for('api_os_templates.r_get_by_filter')
  39. if args.__len__() > 0:
  40. guests_url = guests_url + '?' + '&'.join(args)
  41. guests_ret = requests.get(url=guests_url)
  42. guests_ret = json.loads(guests_ret.content)
  43. os_templates_ret = requests.get(url=os_templates_url)
  44. os_templates_ret = json.loads(os_templates_ret.content)
  45. os_templates_mapping_by_id = dict()
  46. for os_template in os_templates_ret['data']:
  47. os_templates_mapping_by_id[os_template['id']] = os_template
  48. guests_uuid = list()
  49. for guest in guests_ret['data']:
  50. guests_uuid.append(guest['uuid'])
  51. guests_boot_jobs_ret = {'data': dict()}
  52. if guests_uuid.__len__() > 0:
  53. # 获取指定 Guest 的启动作业 ID
  54. guests_boot_jobs_url = host_url + url_for('api_guests.r_get_boot_jobs', uuids=','.join(guests_uuid))
  55. guests_boot_jobs_ret = requests.get(url=guests_boot_jobs_url)
  56. guests_boot_jobs_ret = json.loads(guests_boot_jobs_ret.content)
  57. # 统一单个、多个的返回JSON格式
  58. if guests_uuid.__len__() == 1:
  59. guests_boot_jobs_ret['data'] = {guests_uuid[0]: guests_boot_jobs_ret['data']}
  60. last_page = int(ceil(guests_ret['paging']['total'] / float(page_size)))
  61. page_length = 5
  62. pages = list()
  63. if page < int(ceil(page_length / 2.0)):
  64. for i in range(1, page_length + 1):
  65. pages.append(i)
  66. if i == last_page:
  67. break
  68. elif last_page - page < page_length / 2:
  69. for i in range(last_page - page_length + 1, last_page + 1):
  70. if i < 1:
  71. continue
  72. pages.append(i)
  73. else:
  74. for i in range(page - page_length / 2, page + int(ceil(page_length / 2.0))):
  75. pages.append(i)
  76. if i == last_page:
  77. break
  78. return render_template('config_show.html', guests_ret=guests_ret, resource_path=resource_path,
  79. os_templates_mapping_by_id=os_templates_mapping_by_id,
  80. guests_boot_jobs_ret=guests_boot_jobs_ret, page=page,
  81. page_size=page_size, keyword=keyword, pages=pages, last_page=last_page)
  82. def create():
  83. host_url = request.host_url.rstrip('/')
  84. if request.method == 'POST':
  85. ability = request.form.get('ability')
  86. os_template_id = request.form.get('os_template_id')
  87. quantity = request.form.get('quantity')
  88. password = request.form.get('password')
  89. remark = request.form.get('remark')
  90. if not isinstance(ability, basestring):
  91. pass
  92. m = re.search('^(\d)c(\d)g$', ability.lower())
  93. if m is None:
  94. pass
  95. cpu = m.groups()[0]
  96. memory = m.groups()[1]
  97. payload = {
  98. "cpu": int(cpu),
  99. "memory": int(memory),
  100. "os_template_id": int(os_template_id),
  101. "quantity": int(quantity),
  102. "remark": remark,
  103. "password": password,
  104. "lease_term": 100
  105. }
  106. url = host_url + '/api/guest'
  107. headers = {'content-type': 'application/json'}
  108. r = requests.post(url, data=json.dumps(payload), headers=headers)
  109. j_r = json.loads(r.content)
  110. return render_template('success.html', go_back_url='/', timeout=5000, title='提交成功',
  111. message_title='初始化 JimV 请求已被接受',
  112. message='JimV 已被初始化。页面将在5秒钟后自动跳转到实例列表页面!')
  113. else:
  114. return render_template('config_init.html')
  115. def success():
  116. return render_template('success.html', go_back_url='/', timeout=5000, title='提交成功',
  117. message_title='初始化 JimV 的请求已被接受',
  118. message='JimV 已被初始化。页面将在5秒钟后自动跳转到实例列表页面!')