Advertisement
Guest User

Untitled

a guest
Aug 4th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from collections import OrderedDict
  4. from flask import Flask, jsonify, abort, make_response, request
  5.  
  6. api_server = Flask(__name__)
  7.  
  8. #@api_server.errorhandler(404)
  9. #def not_found(error):
  10. # return make_response(jsonify( { 'error': 'Not found' } ), 404)
  11.  
  12. ############################
  13. # Create domain
  14. @api_server.route('/', methods = ['POST'])
  15. def create_task():
  16. if not request.json:
  17. abort(400)
  18.  
  19. from powerkvm_functions import create_macaddr, dhcp_add, create_volume, create_domain, start_domain, get_domain_info, create_metadata, ConfigSectionMap
  20.  
  21. domain_name = str(request.args['name'])
  22. image_class = str(request.json['class'])
  23. image_name = str(request.json['image'])
  24. username = str(request.json['username'])
  25. userpass = str(request.json['userpass'])
  26. rootpass = str(request.json['rootpass'])
  27. cpu_val = str(request.json['cpu'])
  28. mem_val = str(request.json['memory'])
  29. rootfs_size = str(int(request.json['disk']) * 1024)
  30. if request.json.get('iops'):
  31. disk_iops = str(request.json['iops'])
  32. else:
  33. disk_iops = ConfigSectionMap('api_configs')['disk_iops']
  34. if request.json.get('swap'):
  35. swapfs_size = str(int(request.json['swap']) * 1024)
  36. else:
  37. swapfs_size = None
  38. if request.json.get('temp'):
  39. tempfs_size = str(int(request.json['temp']) * 1024)
  40. else:
  41. tempfs_size = None
  42. # check is domain already exists
  43. domain_exists = get_domain_info(domain_name)
  44. if domain_exists is None:
  45. mac_addr = create_macaddr()
  46. dhcp_add(domain_name, mac_addr)
  47. volume_obj = create_volume('new', image_name, rootfs_size, swapfs_size, tempfs_size)
  48. create_metadata(image_class, domain_name, username, userpass, rootpass, volume_obj)
  49. #create_metadata(image_class, domain_name, username, userpass, rootpass, volume_obj[2], swapfs_size)
  50. create_domain(domain_name, cpu_val, mem_val, mac_addr, volume_obj, disk_iops)
  51. #if request.json.get('temp'):
  52. # tempdisk_size = str(int(request.json['temp']) * 1024)
  53. # tempdisk_name = volume_obj[0].name().replace('_rootfs', '')
  54. # create_tempdisk(domain_name, tempdisk_name, tempdisk_size)
  55. start_domain(domain_name)
  56. status = {domain_name: []}
  57. status[domain_name] = OrderedDict(get_domain_info(domain_name))
  58. return jsonify( status )
  59. else:
  60. abort(403)
  61.  
  62. ############################
  63. # Delete domain/volume
  64. @api_server.route('/', methods = ['DELETE'])
  65. def destroy_task():
  66.  
  67. from powerkvm_functions import domain_status, stop_domain, dhcp_del, get_domain_volumes, delete_volume, delete_domain, volume_in_use
  68.  
  69. if request.args.get('name'):
  70. domain_name = request.args['name']
  71. if domain_status(domain_name) == 'running':
  72. stop_domain(domain_name)
  73. dhcp_del(domain_name)
  74. volumes = get_domain_volumes(domain_name)
  75. delete_domain_status = delete_domain(domain_name)
  76. for x in range(len(volumes)):
  77. if not volume_in_use(volumes[x]):
  78. delete_volume('local', volumes[x])
  79. status = {
  80. 'domain remove': (delete_domain_status)
  81. }
  82. elif request.args.get('volume'):
  83. volume_name = request.args['volume']
  84. delete_volume_status = delete_volume('repo', volume_name)
  85. status = {
  86. 'volume remove': (delete_volume_status)
  87. }
  88. return jsonify(status)
  89.  
  90. ############################
  91. # Stop/Start/Save/Upload
  92. @api_server.route('/', methods = ['PUT'])
  93. def action_task():
  94.  
  95. from powerkvm_functions import domain_status, get_domain_info, get_domain_volumes, get_volume_info, create_volume, upload_volume, stop_domain, start_domain, managedsave_domain
  96.  
  97. if request.args.get('name'):
  98. domain_name = request.args['name']
  99. if request.args['action'] == 'stop':
  100. if (domain_status(domain_name) == 'running'):
  101. stop_domain(domain_name)
  102. status = {}
  103. status[domain_name] = OrderedDict(get_domain_info(domain_name))
  104. elif request.args['action'] == 'start':
  105. if (domain_status(domain_name) == 'stopped'):
  106. start_domain(domain_name)
  107. status = {}
  108. status[domain_name] = OrderedDict(get_domain_info(domain_name))
  109. elif request.args['action'] == 'save':
  110. if (domain_status(domain_name) == 'running'):
  111. #stop_domain(domain_name)
  112. managedsave_domain(domain_name)
  113. volume_obj = get_domain_volumes(domain_name)[0]
  114. volume_size = int(volume_obj.info()[1]) / 1024 / 1024
  115. saved_volume_obj = create_volume('save', volume_obj, 0, 0, 0)
  116. start_domain(domain_name)
  117. status = {}
  118. status[saved_volume_obj.name().replace('_rootfs', '')] = OrderedDict(get_volume_info(saved_volume_obj.name(), 'hypervisor', 1))
  119. elif request.args.get('volume'):
  120. volume_name = request.args['volume']
  121. if request.args['action'] == 'upload':
  122. status = { 'status': upload_volume(volume_name) }
  123. return jsonify(status)
  124.  
  125. ###########################
  126. # Get info
  127. @api_server.route('/', methods = ['GET'])
  128. def info_task():
  129.  
  130. from powerkvm_functions import get_all_domains, get_domain_info, get_all_volumes, get_volume_info
  131.  
  132. if request.args.get('name'):
  133. domain_name = request.args['name']
  134. if domain_name == 'all':
  135. status = get_all_domains()
  136. else:
  137. domain_exists = get_domain_info(domain_name)
  138. if domain_exists is None:
  139. abort(404)
  140. else:
  141. status = {domain_name: []}
  142. status[domain_name] = OrderedDict(get_domain_info(domain_name))
  143. elif request.args.get('volume'):
  144. location = request.args['location']
  145. volume_name = request.args['volume']
  146. if volume_name == 'all':
  147. status = get_all_volumes(request.args['location'])
  148. else:
  149. status = {volume_name: []}
  150. status[volume_name] = OrderedDict(get_volume_info(volume_name + '_rootfs', request.args['location'], 1))
  151. return jsonify(status)
  152.  
  153. # to run Flask's built-in stand-alone
  154. # web server uncomment the following
  155. # then execute this file
  156.  
  157. #if __name__ == '__main__':
  158. # api_server.run(debug = True, host='0.0.0.0')
  159. #else:
  160. # sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement