config.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from flask import Blueprint, g
  4. from flask import request
  5. import jimit as ji
  6. import json
  7. from models import Config
  8. from models import Rules
  9. from models import Utils
  10. __author__ = 'James Iter'
  11. __date__ = '2017/3/21'
  12. __contact__ = 'james.iter.cn@gmail.com'
  13. __copyright__ = '(c) 2017 by James Iter.'
  14. blueprint = Blueprint(
  15. 'api_config',
  16. __name__,
  17. url_prefix='/api/config'
  18. )
  19. @Utils.dumps2response
  20. def r_create():
  21. args_rules = [
  22. Rules.JIMV_EDITION.value,
  23. Rules.STORAGE_MODE.value,
  24. Rules.DFS_VOLUME.value,
  25. Rules.STORAGE_PATH.value,
  26. Rules.VM_NETWORK.value,
  27. Rules.VM_MANAGE_NETWORK.value,
  28. Rules.START_IP.value,
  29. Rules.END_IP.value,
  30. Rules.START_VNC_PORT.value,
  31. Rules.NETMASK.value,
  32. Rules.GATEWAY.value,
  33. Rules.DNS1.value,
  34. Rules.DNS2.value
  35. ]
  36. config = Config()
  37. config.id = 1
  38. config.jimv_edition = int(request.json.get('jimv_edition', 0))
  39. config.storage_mode = int(request.json.get('storage_mode', 0))
  40. config.dfs_volume = request.json.get('dfs_volume', '')
  41. config.storage_path = request.json.get('storage_path')
  42. config.vm_network = request.json.get('vm_network')
  43. config.vm_manage_network = request.json.get('vm_manage_network')
  44. config.start_ip = request.json.get('start_ip')
  45. config.end_ip = request.json.get('end_ip')
  46. config.start_vnc_port = int(request.json.get('start_vnc_port'))
  47. config.netmask = request.json.get('netmask')
  48. config.gateway = request.json.get('gateway')
  49. config.dns1 = request.json.get('dns1')
  50. config.dns2 = request.json.get('dns2')
  51. try:
  52. ji.Check.previewing(args_rules, config.__dict__)
  53. ret = dict()
  54. ret['state'] = ji.Common.exchange_state(20000)
  55. if config.exist():
  56. ret['state'] = ji.Common.exchange_state(40901)
  57. return ret
  58. config.check_ip()
  59. config.generate_available_ip2set()
  60. config.generate_available_vnc_port()
  61. config.create()
  62. config.update_global_config()
  63. config.id = 1
  64. config.get()
  65. ret['data'] = config.__dict__
  66. return ret
  67. except ji.PreviewingError, e:
  68. return json.loads(e.message)
  69. @Utils.dumps2response
  70. def r_update():
  71. config = Config()
  72. args_rules = [
  73. ]
  74. if 'jimv_edition' in request.json:
  75. args_rules.append(
  76. Rules.JIMV_EDITION.value,
  77. )
  78. if 'storage_mode' in request.json:
  79. args_rules.append(
  80. Rules.STORAGE_MODE.value,
  81. )
  82. if 'dfs_volume' in request.json:
  83. args_rules.append(
  84. Rules.DFS_VOLUME.value,
  85. )
  86. if 'storage_path' in request.json:
  87. args_rules.append(
  88. Rules.STORAGE_PATH.value,
  89. )
  90. if 'vm_network' in request.json:
  91. args_rules.append(
  92. Rules.VM_NETWORK.value,
  93. )
  94. if 'vm_manage_network' in request.json:
  95. args_rules.append(
  96. Rules.VM_MANAGE_NETWORK.value,
  97. )
  98. if 'start_ip' in request.json:
  99. args_rules.append(
  100. Rules.START_IP.value,
  101. )
  102. if 'end_ip' in request.json:
  103. args_rules.append(
  104. Rules.END_IP.value,
  105. )
  106. if 'start_vnc_port' in request.json:
  107. args_rules.append(
  108. Rules.START_VNC_PORT.value,
  109. )
  110. if 'netmask' in request.json:
  111. args_rules.append(
  112. Rules.NETMASK.value,
  113. )
  114. if 'gateway' in request.json:
  115. args_rules.append(
  116. Rules.GATEWAY.value,
  117. )
  118. if 'dns1' in request.json:
  119. args_rules.append(
  120. Rules.DNS1.value,
  121. )
  122. if 'dns2' in request.json:
  123. args_rules.append(
  124. Rules.DNS2.value,
  125. )
  126. if args_rules.__len__() < 1:
  127. ret = dict()
  128. ret['state'] = ji.Common.exchange_state(20000)
  129. return ret
  130. try:
  131. config.id = 1
  132. ji.Check.previewing(args_rules, request.json)
  133. config.get()
  134. config.jimv_edition = request.json.get('jimv_edition', config.jimv_edition)
  135. config.storage_mode = request.json.get('storage_mode', config.storage_mode)
  136. config.dfs_volume = request.json.get('dfs_volume', config.dfs_volume)
  137. config.storage_path = request.json.get('storage_path', config.storage_path)
  138. config.vm_network = request.json.get('vm_network', config.vm_network)
  139. config.vm_manage_network = request.json.get('vm_manage_network', config.vm_manage_network)
  140. config.start_ip = request.json.get('start_ip', config.start_ip)
  141. config.end_ip = request.json.get('end_ip', config.end_ip)
  142. config.start_vnc_port = request.json.get('start_vnc_port', config.start_vnc_port)
  143. config.netmask = request.json.get('netmask', config.netmask)
  144. config.gateway = request.json.get('gateway', config.gateway)
  145. config.dns1 = request.json.get('dns1', config.dns1)
  146. config.dns2 = request.json.get('dns2', config.dns2)
  147. config.check_ip()
  148. config.generate_available_ip2set()
  149. config.generate_available_vnc_port()
  150. config.update()
  151. config.update_global_config()
  152. config.get()
  153. ret = dict()
  154. ret['state'] = ji.Common.exchange_state(20000)
  155. ret['data'] = config.__dict__
  156. return ret
  157. except ji.PreviewingError, e:
  158. return json.loads(e.message)
  159. @Utils.dumps2response
  160. def r_get():
  161. config = Config()
  162. try:
  163. config.id = 1
  164. config.get()
  165. ret = dict()
  166. ret['state'] = ji.Common.exchange_state(20000)
  167. ret['data'] = config.__dict__
  168. return ret
  169. except ji.PreviewingError, e:
  170. return json.loads(e.message)