config.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import json
  4. from flask import Blueprint, render_template, request, abort
  5. import requests
  6. __author__ = 'James Iter'
  7. __date__ = '2017/8/29'
  8. __contact__ = 'james.iter.cn@gmail.com'
  9. __copyright__ = '(c) 2017 by James Iter.'
  10. blueprint = Blueprint(
  11. 'v_config',
  12. __name__,
  13. url_prefix='/config'
  14. )
  15. def show():
  16. host_url = request.host_url.rstrip('/')
  17. config_url = host_url + '/api/config'
  18. config_ret = requests.get(url=config_url, cookies=request.cookies)
  19. config_ret = json.loads(config_ret.content)
  20. # 检测 JimV 的配置是否已被初始化,只有未被初始化时,才展现初始化配置页面
  21. if config_ret['state']['code'] == '404':
  22. abort(404)
  23. return render_template('config_show.html', config_ret=config_ret)
  24. def create():
  25. host_url = request.host_url.rstrip('/')
  26. if request.method == 'POST':
  27. payload = {
  28. 'jimv_edition': int(request.form.get('jimv_edition', 0)),
  29. 'storage_mode': int(request.form.get('storage_mode')),
  30. 'dfs_volume': request.form.get('dfs_volume'),
  31. 'storage_path': request.form.get('storage_path'),
  32. 'vm_network': request.form.get('vm_network'),
  33. 'vm_manage_network': request.form.get('vm_manage_network'),
  34. 'start_ip': request.form.get('start_ip'),
  35. 'end_ip': request.form.get('end_ip'),
  36. 'start_vnc_port': int(request.form.get('start_vnc_port')),
  37. 'netmask': request.form.get('netmask'),
  38. 'gateway': request.form.get('gateway'),
  39. 'dns1': request.form.get('dns1'),
  40. 'dns2': request.form.get('dns2'),
  41. 'iops_base': int(request.form.get('iops_base', 1000)),
  42. 'iops_pre_unit': int(request.form.get('iops_pre_unit', 1)),
  43. 'iops_cap': int(request.form.get('iops_cap', 2000)),
  44. 'iops_max': int(request.form.get('iops_max', 3000)),
  45. 'iops_max_length': int(request.form.get('iops_max_length', 20)),
  46. # 200 MiB
  47. 'bps_base': int(request.form.get('bps_base', 1024 * 1024 * 200)),
  48. # 0.3 MiB
  49. 'bps_pre_unit': int(request.form.get('bps_pre_unit', 1024 * 1024 * 0.3)),
  50. # 500 MiB
  51. 'bps_cap': int(request.form.get('bps_cap', 1024 * 1024 * 500)),
  52. # 1 GiB
  53. 'bps_max': int(request.form.get('bps_max', 1024 * 1024 * 1024)),
  54. 'bps_max_length': int(request.form.get('bps_max_length', 10))
  55. }
  56. url = host_url + '/api/config'
  57. headers = {'content-type': 'application/json'}
  58. config_ret = requests.post(url, data=json.dumps(payload), headers=headers, cookies=request.cookies)
  59. config_ret = json.loads(config_ret.content)
  60. return render_template('success.html', go_back_url='/', timeout=5000, title='提交成功',
  61. message_title='初始化 JimV 请求已被接受',
  62. message='JimV 已被初始化。页面将在5秒钟后自动跳转到实例列表页面!')
  63. else:
  64. url = host_url + '/api/config'
  65. config_ret = requests.get(url=url, cookies=request.cookies)
  66. config_ret = json.loads(config_ret.content)
  67. # 检测 JimV 的配置是否已被初始化,只有未被初始化时,才展现初始化配置页面
  68. if config_ret['state']['code'] == '404':
  69. return render_template('config_init.html')
  70. else:
  71. abort(404)