Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- from collections import OrderedDict
- from flask import Flask, jsonify, abort, make_response, request
- api_server = Flask(__name__)
- #@api_server.errorhandler(404)
- #def not_found(error):
- # return make_response(jsonify( { 'error': 'Not found' } ), 404)
- ############################
- # Create domain
- @api_server.route('/', methods = ['POST'])
- def create_task():
- if not request.json:
- abort(400)
- from powerkvm_functions import create_macaddr, dhcp_add, create_volume, create_domain, start_domain, get_domain_info, create_metadata, ConfigSectionMap
- domain_name = str(request.args['name'])
- image_class = str(request.json['class'])
- image_name = str(request.json['image'])
- username = str(request.json['username'])
- userpass = str(request.json['userpass'])
- rootpass = str(request.json['rootpass'])
- cpu_val = str(request.json['cpu'])
- mem_val = str(request.json['memory'])
- rootfs_size = str(int(request.json['disk']) * 1024)
- if request.json.get('iops'):
- disk_iops = str(request.json['iops'])
- else:
- disk_iops = ConfigSectionMap('api_configs')['disk_iops']
- if request.json.get('swap'):
- swapfs_size = str(int(request.json['swap']) * 1024)
- else:
- swapfs_size = None
- if request.json.get('temp'):
- tempfs_size = str(int(request.json['temp']) * 1024)
- else:
- tempfs_size = None
- # check is domain already exists
- domain_exists = get_domain_info(domain_name)
- if domain_exists is None:
- mac_addr = create_macaddr()
- dhcp_add(domain_name, mac_addr)
- volume_obj = create_volume('new', image_name, rootfs_size, swapfs_size, tempfs_size)
- create_metadata(image_class, domain_name, username, userpass, rootpass, volume_obj)
- #create_metadata(image_class, domain_name, username, userpass, rootpass, volume_obj[2], swapfs_size)
- create_domain(domain_name, cpu_val, mem_val, mac_addr, volume_obj, disk_iops)
- #if request.json.get('temp'):
- # tempdisk_size = str(int(request.json['temp']) * 1024)
- # tempdisk_name = volume_obj[0].name().replace('_rootfs', '')
- # create_tempdisk(domain_name, tempdisk_name, tempdisk_size)
- start_domain(domain_name)
- status = {domain_name: []}
- status[domain_name] = OrderedDict(get_domain_info(domain_name))
- return jsonify( status )
- else:
- abort(403)
- ############################
- # Delete domain/volume
- @api_server.route('/', methods = ['DELETE'])
- def destroy_task():
- from powerkvm_functions import domain_status, stop_domain, dhcp_del, get_domain_volumes, delete_volume, delete_domain, volume_in_use
- if request.args.get('name'):
- domain_name = request.args['name']
- if domain_status(domain_name) == 'running':
- stop_domain(domain_name)
- dhcp_del(domain_name)
- volumes = get_domain_volumes(domain_name)
- delete_domain_status = delete_domain(domain_name)
- for x in range(len(volumes)):
- if not volume_in_use(volumes[x]):
- delete_volume('local', volumes[x])
- status = {
- 'domain remove': (delete_domain_status)
- }
- elif request.args.get('volume'):
- volume_name = request.args['volume']
- delete_volume_status = delete_volume('repo', volume_name)
- status = {
- 'volume remove': (delete_volume_status)
- }
- return jsonify(status)
- ############################
- # Stop/Start/Save/Upload
- @api_server.route('/', methods = ['PUT'])
- def action_task():
- 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
- if request.args.get('name'):
- domain_name = request.args['name']
- if request.args['action'] == 'stop':
- if (domain_status(domain_name) == 'running'):
- stop_domain(domain_name)
- status = {}
- status[domain_name] = OrderedDict(get_domain_info(domain_name))
- elif request.args['action'] == 'start':
- if (domain_status(domain_name) == 'stopped'):
- start_domain(domain_name)
- status = {}
- status[domain_name] = OrderedDict(get_domain_info(domain_name))
- elif request.args['action'] == 'save':
- if (domain_status(domain_name) == 'running'):
- #stop_domain(domain_name)
- managedsave_domain(domain_name)
- volume_obj = get_domain_volumes(domain_name)[0]
- volume_size = int(volume_obj.info()[1]) / 1024 / 1024
- saved_volume_obj = create_volume('save', volume_obj, 0, 0, 0)
- start_domain(domain_name)
- status = {}
- status[saved_volume_obj.name().replace('_rootfs', '')] = OrderedDict(get_volume_info(saved_volume_obj.name(), 'hypervisor', 1))
- elif request.args.get('volume'):
- volume_name = request.args['volume']
- if request.args['action'] == 'upload':
- status = { 'status': upload_volume(volume_name) }
- return jsonify(status)
- ###########################
- # Get info
- @api_server.route('/', methods = ['GET'])
- def info_task():
- from powerkvm_functions import get_all_domains, get_domain_info, get_all_volumes, get_volume_info
- if request.args.get('name'):
- domain_name = request.args['name']
- if domain_name == 'all':
- status = get_all_domains()
- else:
- domain_exists = get_domain_info(domain_name)
- if domain_exists is None:
- abort(404)
- else:
- status = {domain_name: []}
- status[domain_name] = OrderedDict(get_domain_info(domain_name))
- elif request.args.get('volume'):
- location = request.args['location']
- volume_name = request.args['volume']
- if volume_name == 'all':
- status = get_all_volumes(request.args['location'])
- else:
- status = {volume_name: []}
- status[volume_name] = OrderedDict(get_volume_info(volume_name + '_rootfs', request.args['location'], 1))
- return jsonify(status)
- # to run Flask's built-in stand-alone
- # web server uncomment the following
- # then execute this file
- #if __name__ == '__main__':
- # api_server.run(debug = True, host='0.0.0.0')
- #else:
- # sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement