def RESTImportInfrastructure(): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: content_type = get_media_type('Content-Type') data = bottle.request.body.read().decode("utf-8") if content_type: if "application/json" not in content_type: return return_error(415, "Unsupported Media Type %s" % content_type) new_id = InfrastructureManager.ImportInfrastructure(data, auth) bottle.response.content_type = "text/uri-list" res = get_full_url('/infrastructures/%s' % new_id) return format_output(res, "text/uri-list", "uri") except InvaliddUserException as ex: return return_error(401, "Error Impporting Inf.: %s" % get_ex_error(ex)) except DisabledFunctionException as ex: return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Impporting Inf.") return return_error(400, "Error Impporting Inf.: %s" % get_ex_error(ex))
def RESTGetInfrastructureList(): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: flt = None if "filter" in bottle.request.params.keys(): flt = bottle.request.params.get("filter") inf_ids = InfrastructureManager.GetInfrastructureList(auth, flt) res = [] for inf_id in inf_ids: res.append(get_full_url('/infrastructures/%s' % inf_id)) return format_output(res, "text/uri-list", "uri-list", "uri") except InvaliddUserException as ex: return return_error(401, "Error Getting Inf. List: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Getting Inf. List") return return_error(400, "Error Getting Inf. List: %s" % get_ex_error(ex))
def RESTGetInfrastructureInfo(infid=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: vm_ids = InfrastructureManager.GetInfrastructureInfo(infid, auth) res = [] for vm_id in vm_ids: res.append( get_full_url('/infrastructures/' + str(infid) + '/vms/' + str(vm_id))) return format_output(res, "text/uri-list", "uri-list", "uri") except DeletedInfrastructureException as ex: return return_error(404, "Error Getting Inf. info: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error Getting Inf. info: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error Getting Inf. info: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Getting Inf. info") return return_error(400, "Error Getting Inf. info: %s" % get_ex_error(ex))
def RESTCreateInfrastructure(): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: content_type = get_media_type('Content-Type') radl_data = bottle.request.body.read().decode("utf-8") tosca_data = None async_call = False if "async" in bottle.request.params.keys(): str_ctxt = bottle.request.params.get("async").lower() if str_ctxt in ['yes', 'true', '1']: async_call = True elif str_ctxt in ['no', 'false', '0']: async_call = False else: return return_error(400, "Incorrect value in async parameter") if content_type: if "application/json" in content_type: radl_data = parse_radl_json(radl_data) elif "text/yaml" in content_type: tosca_data = Tosca(radl_data) _, radl_data = tosca_data.to_radl() elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type: content_type = "text/plain" else: return return_error(415, "Unsupported Media Type %s" % content_type) inf_id = InfrastructureManager.CreateInfrastructure( radl_data, auth, async_call) # Store the TOSCA document if tosca_data: sel_inf = InfrastructureManager.get_infrastructure(inf_id, auth) sel_inf.extra_info['TOSCA'] = tosca_data bottle.response.headers['InfID'] = inf_id bottle.response.content_type = "text/uri-list" res = get_full_url('/infrastructures/%s' % inf_id) return format_output(res, "text/uri-list", "uri") except InvaliddUserException as ex: return return_error(401, "Error Getting Inf. info: %s" % get_ex_error(ex)) except DisabledFunctionException as ex: return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Creating Inf.") return return_error(400, "Error Creating Inf.: %s" % get_ex_error(ex))
def RESTGeVersion(): try: from IM import __version__ as version return format_output(version, field_name="version") except Exception as ex: return return_error(400, "Error getting IM version: %s" % get_ex_error(ex))
def RESTReconfigureInfrastructure(infid=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: vm_list = None if "vm_list" in bottle.request.params.keys(): str_vm_list = bottle.request.params.get("vm_list") try: vm_list = [int(vm_id) for vm_id in str_vm_list.split(",")] except Exception: return return_error(400, "Incorrect vm_list format.") content_type = get_media_type('Content-Type') radl_data = bottle.request.body.read().decode("utf-8") if radl_data: if content_type: if "application/json" in content_type: radl_data = parse_radl_json(radl_data) elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type: content_type = "text/plain" else: return return_error( 415, "Unsupported Media Type %s" % content_type) else: radl_data = "" bottle.response.content_type = "text/plain" return InfrastructureManager.Reconfigure(infid, radl_data, auth, vm_list) except DeletedInfrastructureException as ex: return return_error( 404, "Error reconfiguring infrastructure: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error( 404, "Error reconfiguring infrastructure: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error( 403, "Error reconfiguring infrastructure: %s" % get_ex_error(ex)) except DisabledFunctionException as ex: return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error reconfiguring infrastructure") return return_error( 400, "Error reconfiguring infrastructure: %s" % get_ex_error(ex))
def _execute(self): try: res = self._call_function() self.set(res) return True except Exception as ex: logger.exception(self._error_mesage) self.set(get_ex_error(ex)) return False
def RESTGetVMInfo(infid=None, vmid=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: radl = InfrastructureManager.GetVMInfo(infid, vmid, auth) return format_output(radl, field_name="radl") except DeletedInfrastructureException as ex: return return_error(404, "Error Getting VM. info: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error Getting VM. info: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error Getting VM. info: %s" % get_ex_error(ex)) except DeletedVMException as ex: return return_error(404, "Error Getting VM. info: %s" % get_ex_error(ex)) except IncorrectVMException as ex: return return_error(404, "Error Getting VM. info: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Getting VM info") return return_error(400, "Error Getting VM info: %s" % get_ex_error(ex))
def RESTStopInfrastructure(infid=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: bottle.response.content_type = "text/plain" return InfrastructureManager.StopInfrastructure(infid, auth) except DeletedInfrastructureException as ex: return return_error( 404, "Error stopping infrastructure: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error( 404, "Error stopping infrastructure: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error( 403, "Error stopping infrastructure: %s" % get_ex_error(ex)) except DisabledFunctionException as ex: return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error stopping infrastructure") return return_error( 400, "Error stopping infrastructure: %s" % get_ex_error(ex))
def RESTCreateDiskSnapshot(infid=None, vmid=None, disknum=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: bottle.response.content_type = "text/plain" if "image_name" in bottle.request.params.keys(): image_name = bottle.request.params.get("image_name") else: return return_error(400, "Parameter image_name required.") if "auto_delete" in bottle.request.params.keys(): str_auto_delete = bottle.request.params.get("auto_delete").lower() if str_auto_delete in ['yes', 'true', '1']: auto_delete = True elif str_auto_delete in ['no', 'false', '0']: auto_delete = False else: return return_error( 400, "Incorrect value in auto_delete parameter") else: auto_delete = False return InfrastructureManager.CreateDiskSnapshot( infid, vmid, int(disknum), image_name, auto_delete, auth) except DeletedInfrastructureException as ex: return return_error(404, "Error creating snapshot: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error creating snapshot: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error creating snapshot: %s" % get_ex_error(ex)) except DeletedVMException as ex: return return_error(404, "Error creating snapshot: %s" % get_ex_error(ex)) except IncorrectVMException as ex: return return_error(404, "Error creating snapshot: %s" % get_ex_error(ex)) except DisabledFunctionException as ex: return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error creating snapshot") return return_error(400, "Error creating snapshot: %s" % get_ex_error(ex))
def RESTAlterVM(infid=None, vmid=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: content_type = get_media_type('Content-Type') radl_data = bottle.request.body.read().decode("utf-8") if content_type: if "application/json" in content_type: radl_data = parse_radl_json(radl_data) elif "text/yaml" in content_type: tosca_data = Tosca(radl_data) _, radl_data = tosca_data.to_radl() elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type: content_type = "text/plain" else: return return_error(415, "Unsupported Media Type %s" % content_type) vm_info = InfrastructureManager.AlterVM(infid, vmid, radl_data, auth) return format_output(vm_info, field_name="radl") except DeletedInfrastructureException as ex: return return_error(404, "Error modifying resources: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error modifying resources: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error modifying resources: %s" % get_ex_error(ex)) except DeletedVMException as ex: return return_error(404, "Error modifying resources: %s" % get_ex_error(ex)) except IncorrectVMException as ex: return return_error(404, "Error modifying resources: %s" % get_ex_error(ex)) except DisabledFunctionException as ex: return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error modifying resources") return return_error(400, "Error modifying resources: %s" % get_ex_error(ex))
def RESTRemoveResource(infid=None, vmid=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: context = True if "context" in bottle.request.params.keys(): str_ctxt = bottle.request.params.get("context").lower() if str_ctxt in ['yes', 'true', '1']: context = True elif str_ctxt in ['no', 'false', '0']: context = False else: return return_error(400, "Incorrect value in context parameter") InfrastructureManager.RemoveResource(infid, vmid, auth, context) bottle.response.content_type = "text/plain" return "" except DeletedInfrastructureException as ex: return return_error(404, "Error Removing resources: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error Removing resources: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error Removing resources: %s" % get_ex_error(ex)) except DeletedVMException as ex: return return_error(404, "Error Removing resources: %s" % get_ex_error(ex)) except IncorrectVMException as ex: return return_error(404, "Error Removing resources: %s" % get_ex_error(ex)) except DisabledFunctionException as ex: return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Removing resources") return return_error(400, "Error Removing resources: %s" % get_ex_error(ex))
def RESTDestroyInfrastructure(infid=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: force = False if "force" in bottle.request.params.keys(): str_force = bottle.request.params.get("force").lower() if str_force in ['yes', 'true', '1']: force = True elif str_force in ['no', 'false', '0']: force = False else: return return_error(400, "Incorrect value in force parameter") async_call = False if "async" in bottle.request.params.keys(): str_ctxt = bottle.request.params.get("async").lower() if str_ctxt in ['yes', 'true', '1']: async_call = True elif str_ctxt in ['no', 'false', '0']: async_call = False else: return return_error(400, "Incorrect value in async parameter") InfrastructureManager.DestroyInfrastructure(infid, auth, force, async_call) bottle.response.content_type = "text/plain" return "" except DeletedInfrastructureException as ex: return return_error(404, "Error Destroying Inf: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error Destroying Inf: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except IncorrectStateException as ex: return return_error(409, "Error Destroying Inf: %s" % get_ex_error(ex)) except DisabledFunctionException as ex: return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Destroying Inf") return return_error(400, "Error Destroying Inf: %s" % get_ex_error(ex))
def RESTAddResource(infid=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: context = True if "context" in bottle.request.params.keys(): str_ctxt = bottle.request.params.get("context").lower() if str_ctxt in ['yes', 'true', '1']: context = True elif str_ctxt in ['no', 'false', '0']: context = False else: return return_error(400, "Incorrect value in context parameter") content_type = get_media_type('Content-Type') radl_data = bottle.request.body.read().decode("utf-8") tosca_data = None remove_list = [] if content_type: if "application/json" in content_type: radl_data = parse_radl_json(radl_data) elif "text/yaml" in content_type: tosca_data = Tosca(radl_data) auth = InfrastructureManager.check_auth_data(auth) sel_inf = InfrastructureManager.get_infrastructure(infid, auth) # merge the current TOSCA with the new one if isinstance(sel_inf.extra_info['TOSCA'], Tosca): tosca_data = sel_inf.extra_info['TOSCA'].merge(tosca_data) remove_list, radl_data = tosca_data.to_radl(sel_inf) elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type: content_type = "text/plain" else: return return_error(415, "Unsupported Media Type %s" % content_type) if remove_list: InfrastructureManager.RemoveResource(infid, remove_list, auth, context) vm_ids = InfrastructureManager.AddResource(infid, radl_data, auth, context) # Replace the TOSCA document if tosca_data: sel_inf = InfrastructureManager.get_infrastructure(infid, auth) sel_inf.extra_info['TOSCA'] = tosca_data res = [] for vm_id in vm_ids: res.append( get_full_url("/infrastructures/" + str(infid) + "/vms/" + str(vm_id))) return format_output(res, "text/uri-list", "uri-list", "uri") except DeletedInfrastructureException as ex: return return_error(404, "Error Adding resources: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error Adding resources: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error Adding resources: %s" % get_ex_error(ex)) except DisabledFunctionException as ex: return return_error(403, "Error Destroying Inf: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Adding resources") return return_error(400, "Error Adding resources: %s" % get_ex_error(ex))
def RESTGetVMProperty(infid=None, vmid=None, prop=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: if prop == 'contmsg': info = InfrastructureManager.GetVMContMsg(infid, vmid, auth) elif prop == 'command': auth = InfrastructureManager.check_auth_data(auth) sel_inf = InfrastructureManager.get_infrastructure(infid, auth) step = 1 if "step" in bottle.request.params.keys(): step = int(bottle.request.params.get("step")) if step == 1: url = get_full_url('/infrastructures/' + str(infid) + '/vms/' + str(vmid) + '/command?step=2') auth = sel_inf.auth.getAuthInfo("InfrastructureManager")[0] if 'token' in auth: imauth = "token = %s" % auth['token'] else: imauth = "username = %s; password = %s" % ( auth['username'], auth['password']) command = ( 'curl --insecure -s -H "Authorization: type = InfrastructureManager; %s" ' '-H "Accept: text/plain" %s' % (imauth, url)) ps_command = "ps aux | grep -v grep | grep 'ssh -N -R'" info = """ res="wait" while [ "$res" == "wait" ] do res=`%s` if [ "$res" != "wait" ] then echo "$res" > /var/tmp/reverse_ssh.sh chmod a+x /var/tmp/reverse_ssh.sh /var/tmp/reverse_ssh.sh if [ "$res" != "true" ] then echo "*/1 * * * * root %s || /var/tmp/reverse_ssh.sh" > /etc/cron.d/reverse_ssh fi else sleep 20 fi done""" % (command, ps_command) logger.debug("Step 1 command: %s" % info) elif step == 2: sel_vm = None for vm in sel_inf.get_vm_list(): if vm.creation_im_id == int(vmid): sel_vm = vm break if not sel_vm: # it sometimes happen when the VM is in creation state logger.warn("Specified vmid in step2 is incorrect!!") info = "wait" else: ssh = sel_vm.get_ssh_ansible_master(retry=False) ssh_ok = False if ssh: ssh_ok = ssh.test_connectivity(time_out=2) if ssh_ok: # if it is the master do not make the ssh command if sel_inf.vm_master and int( vmid) == sel_inf.vm_master.creation_im_id: logger.debug( "Step 2: Is the master do no make ssh command." ) info = "true" else: # if this vm is connected with the master directly do not make it also if sel_vm.isConnectedWith(sel_inf.vm_master): logger.debug( "Step 2: Is connected with the master do no make ssh command." ) info = "true" else: info = sel_vm.get_ssh_command() else: info = "wait" logger.debug("Step 2 command for vm ID: %s is %s" % (vmid, info)) else: info = None else: info = InfrastructureManager.GetVMProperty(infid, vmid, prop, auth) if info is None: return return_error( 404, "Incorrect property %s for VM ID %s" % (prop, vmid)) else: return format_output(info, field_name=prop) except DeletedInfrastructureException as ex: return return_error( 404, "Error Getting VM. property: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error( 404, "Error Getting VM. property: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error( 403, "Error Getting VM. property: %s" % get_ex_error(ex)) except DeletedVMException as ex: return return_error( 404, "Error Getting VM. property: %s" % get_ex_error(ex)) except IncorrectVMException as ex: return return_error( 404, "Error Getting VM. property: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Getting VM property") return return_error(400, "Error Getting VM property: %s" % get_ex_error(ex))
def RESTGetInfrastructureProperty(infid=None, prop=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: if prop == "contmsg": headeronly = False if "headeronly" in bottle.request.params.keys(): str_headeronly = bottle.request.params.get( "headeronly").lower() if str_headeronly in ['yes', 'true', '1']: headeronly = True elif str_headeronly in ['no', 'false', '0']: headeronly = False else: return return_error( 400, "Incorrect value in headeronly parameter") res = InfrastructureManager.GetInfrastructureContMsg( infid, auth, headeronly) elif prop == "radl": res = InfrastructureManager.GetInfrastructureRADL(infid, auth) elif prop == "tosca": accept = get_media_type('Accept') if accept and "application/json" not in accept and "*/*" not in accept and "application/*" not in accept: return return_error( 415, "Unsupported Accept Media Types: %s" % accept) bottle.response.content_type = "application/json" auth = InfrastructureManager.check_auth_data(auth) sel_inf = InfrastructureManager.get_infrastructure(infid, auth) if "TOSCA" in sel_inf.extra_info: res = sel_inf.extra_info["TOSCA"].serialize() else: bottle.abort( 403, "'tosca' infrastructure property is not valid in this infrastructure" ) elif prop == "state": accept = get_media_type('Accept') if accept and "application/json" not in accept and "*/*" not in accept and "application/*" not in accept: return return_error( 415, "Unsupported Accept Media Types: %s" % accept) bottle.response.content_type = "application/json" res = InfrastructureManager.GetInfrastructureState(infid, auth) return format_output(res, default_type="application/json", field_name="state") elif prop == "outputs": accept = get_media_type('Accept') if accept and "application/json" not in accept and "*/*" not in accept and "application/*" not in accept: return return_error( 415, "Unsupported Accept Media Types: %s" % accept) bottle.response.content_type = "application/json" auth = InfrastructureManager.check_auth_data(auth) sel_inf = InfrastructureManager.get_infrastructure(infid, auth) if "TOSCA" in sel_inf.extra_info: res = sel_inf.extra_info["TOSCA"].get_outputs(sel_inf) else: bottle.abort( 403, "'outputs' infrastructure property is not valid in this infrastructure" ) return format_output(res, default_type="application/json", field_name="outputs") elif prop == "data": accept = get_media_type('Accept') if accept and "application/json" not in accept and "*/*" not in accept and "application/*" not in accept: return return_error( 415, "Unsupported Accept Media Types: %s" % accept) delete = False if "delete" in bottle.request.params.keys(): str_delete = bottle.request.params.get("delete").lower() if str_delete in ['yes', 'true', '1']: delete = True elif str_delete in ['no', 'false', '0']: delete = False else: return return_error(400, "Incorrect value in delete parameter") data = InfrastructureManager.ExportInfrastructure( infid, delete, auth) return format_output(data, default_type="application/json", field_name="data") else: return return_error(404, "Incorrect infrastructure property") return format_output(res, field_name=prop) except DeletedInfrastructureException as ex: return return_error(404, "Error Getting Inf. prop: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error Getting Inf. prop: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error Getting Inf. prop: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Getting Inf. prop") return return_error(400, "Error Getting Inf. prop: %s" % get_ex_error(ex))