def get(self, request): client = uus.client() result, stat_info = client.list_hosts() return { 'items': result, 'stat_info': stat_info, }
def get(self, request, host_id): try: client = uus.client() result = client.show_host_power_capping(host_id) return result except Exception as e: result = {"status": "failed", "msg": str(e)} return result
def delete(self, request, switch_id, pmswitch_id): try: client = uus.client() client.delete_switch(switch_id) client.delete_portmapping_switches(pmswitch_id) result = {"status": "success", "msg": "success"} return result except Exception as e: result = {"status": "failed", "msg": str(e)} return result
def patch(self, request, switch_id, pmswitch_id): try: mapping_info = request.DATA.get("port_mapping") client = uus.client() client.update_portmapping_nodes(pmswitch_id, mapping_info) result = {"status": "success", "msg": "success"} return result except Exception as e: result = {"status": "failed", "msg": str(e)} return result
def delete(self, request, host_id): try: client = uus.client() if client.delete_host(host_id): result = {"status": "success", "msg": "success"} else: result = {"status": "failed", "msg": "Delete failed "} return result except Exception as e: result = {"status": "failed", "msg": str(e)} return result
def post(self, request): halist = request.DATA.get('halist') if halist is None: result = {"status": "failed", "msg": "No node is selected"} return result try: client = uus.client() result = client.set_computeha(halist) return result except Exception as e: result = {"status": "failed", "msg": str(e)} return result
def post(self, request, host_id): newCap = request.DATA.get('newCap') if newCap is None: result = {"status": "failed", "msg": "newCap is NULL"} return result try: client = uus.client() result = client.set_host_power_capping(host_id, newCap) return result except Exception as e: result = {"status": "failed", "msg": str(e)} return result
def post(self, request, host_id): interval = request.DATA.get('interval') duration = request.DATA.get('duration') if interval is None or duration is None: result = {"status": "failed", "msg": "NULL interval or duration"} return result try: client = uus.client() result = client.show_host_power_history(host_id, interval, duration) return result except Exception as e: result = {"status": "failed", "msg": str(e)} return result
def post(self, request, host_id): userid = request.DATA.get('userid') passowrd = request.DATA.get('password') if userid is None or passowrd is None: result = {"status": "failed", "msg": "NULL userid or password"} return result try: client = uus.client() if client.auth_imm(host_id, userid, passowrd): result = {"status": "success", "msg": "success"} else: result = {"status": "failed", "msg": "auth failed"} return result except Exception as e: result = {"status": "failed", "msg": str(e)} return result
def get(self, request, switch_id, pmswitch_id): LOG.info('Switch get switch_id = %s,pmswitch_id = %s' % (switch_id, pmswitch_id)) client = uus.client() switch_result = client.show_switch(switch_id) pm_result = client.show_portmapping_switch(pmswitch_id) if pm_result == {}: return pm_result result_data = switch_result.copy() result_data['pmswitch_id'] = pm_result['uuid'] result_data['username'] = pm_result['username'] result_data['ssh_port'] = pm_result['ssh_port'] result_data['os_type'] = pm_result['os_type'] if pm_result.has_key('rest_tcp_port'): result_data['rest_tcp_port'] = pm_result['rest_tcp_port'] result_data['protocol'] = pm_result['protocol'] return result_data
def put(self, request): try: obsvtime = request.DATA['obsv'] locktime = request.DATA['loct'] loctimes = request.DATA['locc'] except KeyError as e: raise rest_utils.AjaxError(400, _("Post Data Error: %s" % unicode(e))) try: client = uus.client() client.refresh_security_setting(obsvtime, locktime, loctimes) result = {"status": "success", "msg": "success"} return result except Exception as e: # result = {"status": "success", "msg": "success"} result = {"status": "failed", "msg": str(e)} return result
def put(self, request, switch_id, pmswitch_id): try: username = request.DATA['username'] password = request.DATA['password'] except KeyError as e: raise rest_utils.AjaxError(400, _("Post Data Error: %s" % unicode(e))) try: client = uus.client() client.update_switch_user(switch_id, username=username, password=password) client.update_portmapping_switch(pmswitch_id, username=username, password=password) result = {"status": "success", "msg": "success"} return result except Exception as e: result = {"status": "failed", "msg": str(e)} return result
def post(self, request, host_id): operation = request.DATA.get('operation', 'none') opcode_dict = { 'poweroff': 0, 'poweron': 1, 'reboot': 2, } opcode = opcode_dict.get(operation, -1) if opcode < 0: result = {"status": "failed", "msg": "Invalid host operation "} return result try: client = uus.client() if client.change_power_state(host_id, opcode): result = {"status": "success", "msg": "success"} else: result = {"status": "failed", "msg": "Invalid host operation "} return result except Exception as e: result = {"status": "failed", "msg": str(e)} return result
def post(self, request): # Example of request data: # { # "switch_ip": "10.240.253.201", # "username": "******", # "ssh_port": "830", # "os_type": "cnos", # "password": "******", # "protocol" :"rest" # "rest_tcp_port":"8090" # } try: switch_ip = request.DATA['switch_ip'] ssh_port = request.DATA['ssh_port'] username = request.DATA['username'] password = request.DATA['password'] os_type = request.DATA['os_type'] protocol = request.DATA['protocol'] client = uus.client() return_data = client.switch_discovery(switch_ip, username=username, password=password, os_type=os_type) client.add_portmapping_switch( switch_ip, ssh_port, username=username, password=password, os_type=os_type, protocol=protocol, rest_tcp_port=return_data.get("rest_tcp_port")) result = {"status": "success", "msg": "success"} return result except Exception as e: result = {"status": "failed", "msg": str(e)} return result
def get(self, request): client = uus.client() switch_result = client.list_switches() pm_result = client.list_portmapping_switches() pm_index = dict() for pm_switch in pm_result: switch_ip = pm_switch['switch_ip'] pm_index[switch_ip] = pm_switch result_data = [] for endpoint in switch_result: switch = endpoint.copy() switch_ip = switch['switch_ip'] pm_switch = pm_index[switch_ip] switch['pmswitch_id'] = pm_switch['uuid'] switch['username'] = pm_switch['username'] switch['ssh_port'] = pm_switch['ssh_port'] switch['os_type'] = pm_switch['os_type'] switch['protocol'] = pm_switch['protocol'] result_data.append(switch) return { 'items': result_data, }
def delete(self, request, switch_id, pmswitch_id): try: param_nodes = request.GET.get('nodes') LOG.info('Param_nodes = %s' % (param_nodes)) if param_nodes is None: raise rest_utils.AjaxError(400, _("Parameter 'nodes' required")) mapping_info = dict() delete_nodes = param_nodes.split(' ') LOG.info('Switch get switch_id = %s' % (delete_nodes)) for node in delete_nodes: mapping_info[node] = "" client = uus.client() client.delete_portmapping_nodes(pmswitch_id, mapping_info) result = {"status": "success", "msg": "success"} return result except Exception as e: result = {"status": "failed", "msg": str(e)} return result
def get(self, request, switch_id, pmswitch_id): client = uus.client() result = client.list_portmapping_nodes(pmswitch_id) return {'port_mapping': result}
def get(self, request, host_id): client = uus.client() result = client.show_host(host_id) return result
def get(self, request): client = uus.client() # result = client.showall_upgradestatus() result = self.getData(request) return {'items': result}
def get(self, request): client = uus.client() result = client.list_computeha() return result
def getFile(self, request, vdata): # return {'status': 'success', 'msg': "success"} try: if isinstance(vdata, TemporaryUploadedFile): # Hack to fool Django, so we can keep file open in the new thread. vdata.file.close_called = True if isinstance(vdata, InMemoryUploadedFile): # Clone a new file for InMemeoryUploadedFile. # Because the old one will be closed by Django. vdata = SimpleUploadedFile(vdata.name, vdata.read(), vdata.content_type) dataFilename = vdata.name with open('/tmp/' + dataFilename, 'wb+') as destination: for chunk in vdata.chunks(): destination.write(chunk) # todo,add code to handle stuff after upload, kick off # destination.close() folder_str = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) status, output = commands.getstatusoutput( ("cd /tmp;mkdir -p %s;unzip %s -d %s") % (folder_str, dataFilename, folder_str)) if os.path.exists('/tmp/%s/upgrade.zip' % folder_str) == False or os.path.exists( '/tmp/%s/signature' % folder_str) == False: # messages.error(request,_('Failed to validated the upgrade package! Please check if the package is a valid Lenovo upgrade package!')) status, output = commands.getstatusoutput( "rm -rf /tmp/%s;rm -rf /tmp/%s" % (folder_str, dataFilename)) msg = 'Failed to validated the upgrade package! Please check if the package is a valid Lenovo upgrade package!' return {'status': 'failed', 'msg': msg} if request.DATA['fileAction'] == 'unchecked': if os.path.exists('/tmp/%s/version.txt' % folder_str) == False: status, output = commands.getstatusoutput( "rm -rf /tmp/%s;rm -rf /tmp/%s" % (folder_str, dataFilename)) msg = 'Failed to validated the upgrade package! Please check if the package contains a version.txt file!' return {'status': 'failed', 'msg': msg} else: with open('/tmp/%s/version.txt' % folder_str) as versionFile: data = {"UPGRADE_VERSION": "", "PATCH_ID": ""} for line in versionFile.readlines(): props = line.split("=") if props[0] in data: data[props[0]] = props[1].strip("\r\n") status, output = commands.getstatusoutput( "rm -rf /tmp/%s;rm -rf /tmp/%s" % (folder_str, dataFilename)) client = uus.client() LOG.info("data = %s" % data) up_info = client.get_upgradestatus(data["PATCH_ID"]) LOG.info("upinfo = %s" % up_info) retstr = "PREVISION =" + up_info + " U" + "PGRADEVERSION =" + data[ "UPGRADE_VERSION"] if len(data["UPGRADE_VERSION"].strip()) > 0: return {'status': 'success', 'msg': retstr} else: msg = 'Failed to validated the upgrade package! Please specific a version in the version.txt file!' return {'status': 'failed', 'msg': msg} if os.path.exists('/etc/hwmgmt/public.pem') == False: # messages.error(request,_('Failed to validated the upgrade package! Please quickly contact with our department of server.')) status, output = commands.getstatusoutput( "rm -rf /tmp/%s;rm -rf /tmp/%s" % (folder_str, dataFilename)) msg = 'Failed to validated the upgrade package! Please quickly contact with our department of server.' return {'status': 'failed', 'msg': msg} with open('/etc/hwmgmt/public.pem') as f, open('/tmp/%s/upgrade.zip' % folder_str) as message, \ open('/tmp/%s/signature' % folder_str) as signFile,open('/tmp/%s/version.txt' % folder_str) as versionFile: key = f.read() rsakey = RSA.importKey(key) verifier = Signature.new(rsakey) digest = SHA.new() digest.update(message.read()) is_verify = verifier.verify(digest, base64.b64decode(signFile.read())) data = {"UPGRADE_VERSION": "", "PATCH_ID": ""} for line in versionFile.readlines(): props = line.split("=") if props[0] in data: data[props[0]] = props[1].strip("\r\n") if is_verify == False: # messages.error(request,_('Failed to validated the upgrade package! Please check if the package is a valid Lenovo upgrade package!')) status, output = commands.getstatusoutput( "rm -rf /tmp/%s;rm -rf /tmp/%s" % (folder_str, dataFilename)) msg = 'Failed to validated the upgrade package! Please check if the package is a valid Lenovo upgrade package!' return {'status': 'failed', 'msg': msg} status, output = commands.getstatusoutput( "cd /tmp/%s;unzip upgrade.zip ;chmod +x /tmp/%s/*" % (folder_str, folder_str)) execute_str = "/tmp/" + folder_str + "/upgrade.yml" #leverage uus to handle backend upgrade procedure, since it require root auth while we are running in as horizon user client = uus.client() return_code = 0 try: with open('/tmp/%s/version.txt' % folder_str) as versionFile: data = {} for line in versionFile.readlines(): props = line.split("=") data[props[0]] = props[1].strip("\r\n") data['workdir'] = folder_str return_code = client.startupgrade(execute_str, data) if return_code == 20301: raise Exception msg = 'File uploaded successfully.' return {'status': 'success', 'msg': msg} except Exception as e: # exceptions.handle(request,_('The package can NOT work.')) if return_code == 20301: msg = "The package has already patched!" else: msg = 'The package can NOT work.' return {'status': 'failed', 'msg': msg} # messages.success(request, # _('Patch file %s upload success.') % # vdata.name) except Exception as e: msg = 'Unable to upload patch file' # TODO(nikunj2512): Fix this once it is fixed in glance client # exceptions.handle(request, msg) return {'status': 'failed', 'msg': msg}