def snapshot_seq(self, sasuri, sasuri_index, meta_data): result = None snapshot_error = SnapshotError() snapshot_info_indexer = SnapshotInfoIndexerObj(sasuri_index, False, None, None) if (sasuri is None): self.logger.log("Failed to do the snapshot because sasuri is none", False, 'Error') snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri try: sasuri_obj = urlparser.urlparse(sasuri) if (sasuri_obj is None or sasuri_obj.hostname is None): self.logger.log("Failed to parse the sasuri", False, 'Error') snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri else: body_content = '' headers = {} headers["Content-Length"] = '0' if (meta_data is not None): for meta in meta_data: key = meta['Key'] value = meta['Value'] headers["x-ms-meta-" + key] = value self.logger.log(str(headers)) http_util = HttpUtil(self.logger) sasuri_obj = urlparser.urlparse(sasuri + '&comp=snapshot') self.logger.log("start calling the snapshot rest api") # initiate http call for blob-snapshot and get http response result, httpResp, errMsg, responseBody = http_util.HttpCallGetResponse( 'PUT', sasuri_obj, body_content, headers=headers, responseBodyRequired=True) self.logger.log("responseBody: " + responseBody) if (result == CommonVariables.success and httpResp != None): # retrieve snapshot information from http response snapshot_info_indexer, snapshot_error, message = self.httpresponse_get_snapshot_info( httpResp, sasuri_index, sasuri, responseBody) self.logger.log( ' httpresponse_get_snapshot_info message: ' + str(message)) else: # HttpCall failed self.logger.log(" snapshot HttpCallGetResponse failed ") self.logger.log(str(errMsg)) snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri except Exception as e: errorMsg = "Failed to do the snapshot with error: %s, stack trace: %s" % ( str(e), traceback.format_exc()) self.logger.log(errorMsg, False, 'Error') snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri return snapshot_error, snapshot_info_indexer
def put_page_update(self, pageContent, blobUri, pageBlobIndex): http_util = HttpUtil(self.hutil) sasuri_obj = urlparse.urlparse(blobUri + '&comp=page') headers = {} headers["x-ms-page-write"] = 'update' headers["x-ms-range"] = 'bytes={0}-{1}'.format(pageBlobIndex, pageBlobIndex + len(pageContent) - 1) headers["Content-Length"] = len(str(pageContent)) result = http_util.Call(method = 'PUT', sasuri_obj = sasuri_obj, data = pageContent, headers = headers, fallback_to_curl = True) return result
def put_page_clear(self, blobUri, pageBlobIndex, clearLength): http_util = HttpUtil(self.hutil) sasuri_obj = urlparse.urlparse(blobUri + '&comp=page') headers = {} headers["x-ms-page-write"] = 'clear' headers["x-ms-range"] = 'bytes={0}-{1}'.format(pageBlobIndex, pageBlobIndex + clearLength - 1) headers["Content-Length"] = 0 result = http_util.Call(method = 'PUT', sasuri_obj = sasuri_obj, data = None, headers = headers, fallback_to_curl = True) return result
def pre_snapshot(self, paras, taskId): statusCode = 555 if (self.presnapshoturi is None): self.logger.log( "Failed to do the snapshot because presnapshoturi is none", False, 'Error') all_failed = True try: presnapshoturi_obj = urlparser.urlparse(self.presnapshoturi) if (presnapshoturi_obj is None or presnapshoturi_obj.hostname is None): self.logger.log("Failed to parse the presnapshoturi", False, 'Error') all_failed = True else: headers = {} headers['Backup'] = 'true' headers['Content-type'] = 'application/json' hostPreSnapshotRequestBodyObj = HostSnapshotObjects.HostPreSnapshotRequestBody( taskId, paras.snapshotTaskToken) body_content = json.dumps(hostPreSnapshotRequestBodyObj, cls=HandlerUtil.ComplexEncoder) self.logger.log('Headers : ' + str(headers)) self.logger.log('Host Request body : ' + str(body_content)) http_util = HttpUtil(self.logger) self.logger.log("start calling the presnapshot rest api") # initiate http call for blob-snapshot and get http response result, httpResp, errMsg, responseBody = http_util.HttpCallGetResponse( 'POST', presnapshoturi_obj, body_content, headers=headers, responseBodyRequired=True, isHttpCall=True) self.logger.log("presnapshot responseBody: " + responseBody) if (httpResp != None): statusCode = httpResp.status HandlerUtil.HandlerUtility.add_to_telemetery_data( "hostStatusCodePreSnapshot", str(statusCode)) if (httpResp.status == 500 and not responseBody.startswith("{ \"error\"")): self.logger.log("BHS is not runnning on host machine") statusCode = 556 HandlerUtil.HandlerUtility.add_to_telemetery_data( "hostStatusCodePreSnapshot", str(statusCode)) else: # HttpCall failed statusCode = 555 self.logger.log("presnapshot Hitting wrong WireServer IP") HandlerUtil.HandlerUtility.add_to_telemetery_data( "hostStatusCodePreSnapshot", str(statusCode)) except Exception as e: errorMsg = "Failed to do the pre snapshot in host with error: %s, stack trace: %s" % ( str(e), traceback.format_exc()) self.logger.log(errorMsg, False, 'Error') return statusCode
def create_kek_secret(self, Passphrase, KeyVaultURL, KeyEncryptionKeyURL, AADClientID, KeyEncryptionAlgorithm, AADClientSecret, DiskEncryptionKeyFileName): try: self.logger.log("start creating kek secret") passphrase_encoded = base64.standard_b64encode(Passphrase) keys_uri = self.urljoin(KeyVaultURL, "keys") http_util = HttpUtil(self.logger) headers = {} result = http_util.Call(method='GET', http_uri=keys_uri, data=None, headers=headers) http_util.connection.close() """ get the access token """ self.logger.log("getting the access token.") bearerHeader = result.getheader("www-authenticate") authorize_uri = self.get_authorize_uri(bearerHeader) if (authorize_uri is None): self.logger.log("the authorize uri is None") return None access_token = self.get_access_token(authorize_uri, AADClientID, AADClientSecret) if (access_token is None): self.logger.log("the access token is None") return None """ we should skip encrypting the passphrase if the KeyVaultURL and KeyEncryptionKeyURL is empty """ if (KeyEncryptionKeyURL is None or KeyEncryptionKeyURL == ""): secret_value = passphrase_encoded else: secret_value = self.encrypt_passphrase( access_token, passphrase_encoded, KeyVaultURL, KeyEncryptionKeyURL, AADClientID, KeyEncryptionAlgorithm, AADClientSecret) if (secret_value is None): self.logger.log("secret value is None") return None secret_id = self.create_secret(access_token, KeyVaultURL, secret_value, KeyEncryptionAlgorithm, DiskEncryptionKeyFileName) return secret_id except Exception as e: self.logger.log( "Failed to create_kek_secret with error: {0}, stack trace: {1}" .format(e, traceback.format_exc())) return None
def snapshot(self, sasuri, sasuri_index, meta_data, snapshot_result_error, snapshot_info_indexer_queue, global_logger, global_error_logger): temp_logger='' error_logger='' snapshot_error = SnapshotError() snapshot_info_indexer = SnapshotInfoIndexerObj(sasuri_index, False, None, None) if(sasuri is None): error_logger = error_logger + str(datetime.datetime.now()) + " Failed to do the snapshot because sasuri is none " snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri try: sasuri_obj = urlparser.urlparse(sasuri) if(sasuri_obj is None or sasuri_obj.hostname is None): error_logger = error_logger + str(datetime.datetime.now()) + " Failed to parse the sasuri " snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri else: start_time = datetime.datetime.utcnow() body_content = '' headers = {} headers["Content-Length"] = '0' if(meta_data is not None): for meta in meta_data: key = meta['Key'] value = meta['Value'] headers["x-ms-meta-" + key] = value temp_logger = temp_logger + str(headers) http_util = HttpUtil(self.logger) sasuri_obj = urlparser.urlparse(sasuri + '&comp=snapshot') temp_logger = temp_logger + str(datetime.datetime.now()) + ' start calling the snapshot rest api. ' # initiate http call for blob-snapshot and get http response result, httpResp, errMsg, responseBody = http_util.HttpCallGetResponse('PUT', sasuri_obj, body_content, headers = headers) self.logger.log('Response Body : ' + str(responseBody)) if(result == CommonVariables.success and httpResp != None): # retrieve snapshot information from http response snapshot_info_indexer, snapshot_error, message = self.httpresponse_get_snapshot_info(httpResp, sasuri_index, sasuri) temp_logger = temp_logger + str(datetime.datetime.now()) + ' httpresponse_get_snapshot_info message: ' + str(message) else: # HttpCall failed error_logger = error_logger + str(datetime.datetime.now()) + " snapshot HttpCallGetResponse failed " error_logger = error_logger + str(datetime.datetime.now()) + str(errMsg) snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri end_time = datetime.datetime.utcnow() time_taken=end_time-start_time temp_logger = temp_logger + str(datetime.datetime.now()) + ' time taken for snapshot ' + str(time_taken) except Exception as e: errorMsg = " Failed to do the snapshot with error: %s, stack trace: %s" % (str(e), traceback.format_exc()) error_logger = error_logger + str(datetime.datetime.now()) + errorMsg snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri temp_logger=temp_logger + str(datetime.datetime.now()) + ' snapshot ends..' global_logger.put(temp_logger) global_error_logger.put(error_logger) snapshot_result_error.put(snapshot_error) snapshot_info_indexer_queue.put(snapshot_info_indexer)
class FridgeFreezer: def __init__(self,token,haID, url): self.token = "Bearer " + token self.haID = haID self.url = url self.headers = {"Accept":"application/vnd.bsh.sdk.v1+json","Content-Type":"application/json","Authorization": self.token} self.httputil = HttpUtil() self.logger = logModule() def getStatus(self): url = self.url + self.haID + "/status" self.logger.debug(url) duration = self.httputil.Get(url,self.headers) if duration is not -1: self.logger.info("Get Status in "+ str(duration)+ " seconds") else: self.logger.error("Fail to operate HA due to unknown reason") def changeTemperature(self): program = "" randomPick = random.randint(0,3) if randomPick is 0: settingKey = "Refrigeration.FridgeFreezer.Setting.SetpointTemperatureFreezer" Temperature = random.randint(-24,-16) body = {"data":{"key":"Refrigeration.FridgeFreezer.Setting.SetpointTemperatureFreezer","value":Temperature,"unit":"°C"}} program = "Freezer Temperature" elif randomPick is 1: settingKey = "Refrigeration.FridgeFreezer.Setting.SetpointTemperatureRefrigerator" Temperature = random.randint(2,8) body = {"data":{"key":"Refrigeration.FridgeFreezer.Setting.SetpointTemperatureRefridgerator","value":Temperature,"unit":"°C"}} program = "Refrigerator Temperature" elif randomPick is 2: settingKey = "Refrigeration.FridgeFreezer.Setting.SuperModeFreezer" body = {"data":{"key":"Refrigeration.FridgeFreezer.Setting.SuperModeFreezer","value":True}} program = "Super Mode Freezer" elif randomPick is 3: settingKey = "Refrigeration.FridgeFreezer.Setting.SuperModeRefrigerator" body = {"data":{"key":"Refrigeration.FridgeFreezer.Setting.SuperModeRefrigerator","value":True}} program = "Super Mode Refridgerator" url = self.url + self.haID + "/settings/" + settingKey self.logger.debug(url) bodyJson = json.dumps(body) duration = self.httputil.Put(url,bodyJson,self.headers) if duration is not -1: self.logger.info(program + " starts in " + str(duration) + " seconds") else: self.logger.error("Failed to operation HA due to unknown reason") def stopProgram(self): url = self.url + self.haID + "/programs/active" duration = self.httputil.Delete(url,self.headers) if duration is not -1: print ("program stops in "+ str(duration)+ " seconds") else: print ("Failed to operate HA due to unknown reason")
def __init__(self, token, haID, url): self.token = "Bearer " + token self.haID = haID self.url = url self.headers = { "Accept": "application/vnd.bsh.sdk.v1+json", "Content-Type": "application/json", "Authorization": self.token } self.httputil = HttpUtil() self.logger = logModule()
def snapshotall(self, paras, freezer, g_fsfreeze_on, taskId): result = None blob_snapshot_info_array = [] all_failed = True is_inconsistent = False unable_to_sleep = False meta_data = paras.backup_metadata if(self.snapshoturi is None): self.logger.log("Failed to do the snapshot because snapshoturi is none",False,'Error') all_failed = True try: snapshoturi_obj = urlparser.urlparse(self.snapshoturi) if(snapshoturi_obj is None or snapshoturi_obj.hostname is None): self.logger.log("Failed to parse the snapshoturi",False,'Error') all_failed = True else: diskIds = [] body_content = '' headers = {} headers['Backup'] = 'true' headers['Content-type'] = 'application/json' hostDoSnapshotRequestBodyObj = HostSnapshotObjects.HostDoSnapshotRequestBody(taskId, diskIds, paras.snapshotTaskToken, meta_data) body_content = json.dumps(hostDoSnapshotRequestBodyObj, cls = HandlerUtil.ComplexEncoder) self.logger.log('Headers : ' + str(headers)) self.logger.log('Host Request body : ' + str(body_content)) http_util = HttpUtil(self.logger) self.logger.log("start calling the snapshot rest api") # initiate http call for blob-snapshot and get http response result, httpResp, errMsg,responseBody = http_util.HttpCallGetResponse('POST', snapshoturi_obj, body_content, headers = headers, responseBodyRequired = True, isHttpCall = True) self.logger.log("dosnapshot responseBody: " + responseBody) if(httpResp != None): HandlerUtil.HandlerUtility.add_to_telemetery_data("hotStatusCodeDoSnapshot", str(httpResp.status)) if(int(httpResp.status) == 200 or int(httpResp.status) == 201): blob_snapshot_info_array, all_failed = self.get_snapshot_info(responseBody) if(httpResp.status == 500 and not responseBody.startswith("{ \"error\"")): HandlerUtil.HandlerUtility.add_to_telemetery_data("hostStatusCodeDoSnapshot", str(556)) else: # HttpCall failed HandlerUtil.HandlerUtility.add_to_telemetery_data("hostStatusCodeDoSnapshot", str(555)) self.logger.log("presnapshot Hitting wrong WireServer IP") #performing thaw if g_fsfreeze_on : time_before_thaw = datetime.datetime.now() thaw_result, unable_to_sleep = freezer.thaw_safe() time_after_thaw = datetime.datetime.now() HandlerUtil.HandlerUtility.add_to_telemetery_data("ThawTime", str(time_after_thaw-time_before_thaw)) self.logger.log('T:S thaw result ' + str(thaw_result)) if(thaw_result is not None and len(thaw_result.errors) > 0): is_inconsistent = True except Exception as e: errorMsg = "Failed to do the snapshot in host with error: %s, stack trace: %s" % (str(e), traceback.format_exc()) self.logger.log(errorMsg, False, 'Error') all_failed = True return blob_snapshot_info_array, all_failed, is_inconsistent, unable_to_sleep
class Washer: def __init__(self,token,haID, url): self.token = "Bearer " + token self.haID = haID self.url = url self.headers = {"Accept":"application/vnd.bsh.sdk.v1+json","Content-Type":"application/json","Authorization": self.token} self.httputil = HttpUtil() self.logger = logModule() def getStatus(self): url = self.url + self.haID + "/status" self.logger.debug(url) duration = self.httputil.Get(url,self.headers) if duration is not -1: self.logger.info("Get Status in "+ str(duration) + " seconds") else: self.logger.error("Fail to operate HA due to unknown reason") def putProgram(self): program = "" randomPick = (random.randint(0,20)%5) if randomPick is 0: body = {"data":{"key":"LaundryCare.Washer.Program.Cotton","options":[{"key":"LaundryCare.Washer.Option.Temperature","value":"LaundryCare.Washer.EnumType.Temperature.GC30"},{"key":"LaundryCare.Washer.Option.SpinSpeed","value":"LaundryCare.Washer.EnumType.SpinSpeed.RPM800"}]}} program = "Cotton" elif randomPick is 1: body = {"data":{"key":"LaundryCare.Washer.Program.EasyCare","options":[{"key":"LaundryCare.Washer.Option.Temperature","value":"LaundryCare.Washer.EnumType.Temperature.GC30"},{"key":"LaundryCare.Washer.Option.SpinSpeed","value":"LaundryCare.Washer.EnumType.SpinSpeed.RPM800"}]}} program = "EasyCare" elif randomPick is 2: body = {"data":{"key":"LaundryCare.Washer.Program.Mix","options":[{"key":"LaundryCare.Washer.Option.Temperature","value":"LaundryCare.Washer.EnumType.Temperature.GC30"},{"key":"LaundryCare.Washer.Option.SpinSpeed","value":"LaundryCare.Washer.EnumType.SpinSpeed.RPM800"}]}} program = "Mix" elif randomPick is 3: body = {"data":{"key":"LaundryCare.Washer.Program.Wool","options":[{"key":"LaundryCare.Washer.Option.Temperature","value":"LaundryCare.Washer.EnumType.Temperature.GC30"},{"key":"LaundryCare.Washer.Option.SpinSpeed","value":"LaundryCare.Washer.EnumType.SpinSpeed.RPM800"}]}} program = "Wool" elif randomPick is 4: body = {"data":{"key":"LaundryCare.Washer.Program.DelicatesSilk","options":[{"key":"LaundryCare.Washer.Option.Temperature","value":"LaundryCare.Washer.EnumType.Temperature.GC30"},{"key":"LaundryCare.Washer.Option.SpinSpeed","value":"LaundryCare.Washer.EnumType.SpinSpeed.RPM800"}]}} program = "Delicates Silk" url = self.url + self.haID + "/programs/active" self.logger.debug(url) bodyJson = json.dumps(body) duration = self.httputil.Put(url,bodyJson,self.headers) if duration is not -1: self.logger.info(program + " starts in " + str(duration)+ " seconds") else: self.logger.error("Failed to operation HA due to unknown reason") def stopProgram(self): url = self.url + self.haID + "/programs/active" duration = self.httputil.Delete(url,self.headers) if duration is not -1: self.logger.info("program stops in "+ str(duration)+ " seconds") else: self.logger.error("Failed to operate HA due to unknown reason")
def get_access_token(self, KeyVaultResourceName, AuthorizeUri, AADClientID, AADClientCertThumbprint, AADClientSecret): if not AADClientSecret and not AADClientCertThumbprint: raise Exception( "Neither AADClientSecret nor AADClientCertThumbprint were specified" ) if AADClientSecret and AADClientCertThumbprint: raise Exception( "Both AADClientSecret nor AADClientCertThumbprint were specified" ) if AADClientCertThumbprint: try: import adal except: raise Exception("adal library is not available on the VM") import waagent prv_path = os.path.join(waagent.LibDir, AADClientCertThumbprint.upper() + '.prv') prv_data = waagent.GetFileContents(prv_path) context = adal.AuthenticationContext(AuthorizeUri) result_json = context.acquire_token_with_client_certificate( KeyVaultResourceName, AADClientID, prv_data, AADClientCertThumbprint) access_token = result_json["accessToken"] return access_token token_uri = AuthorizeUri + "/oauth2/token" request_content = "resource=" + urllib.quote( KeyVaultResourceName ) + "&client_id=" + AADClientID + "&client_secret=" + urllib.quote( AADClientSecret) + "&grant_type=client_credentials" headers = {} http_util = HttpUtil(self.logger) result = http_util.Call(method='POST', http_uri=token_uri, data=request_content, headers=headers) self.logger.log("{0} {1}".format(result.status, result.getheaders())) result_content = result.read() if result.status != httplib.OK and result.status != httplib.ACCEPTED: self.logger.log(str(result_content)) return None http_util.connection.close() result_json = json.loads(result_content) access_token = result_json["access_token"] return access_token
def snapshot(self, sasuri, meta_data, snapshot_result_error, global_logger, global_error_logger): result = None temp_logger = '' error_logger = '' snapshot_error = SnapshotError() if (sasuri is None): error_logger = error_logger + " Failed to do the snapshot because sasuri is none " snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri try: sasuri_obj = urlparse.urlparse(sasuri) if (sasuri_obj is None or sasuri_obj.hostname is None): error_logger = error_logger + " Failed to parse the sasuri " snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri else: start_time = datetime.datetime.utcnow() body_content = '' headers = {} headers["Content-Length"] = '0' for meta in meta_data: key = meta['Key'] value = meta['Value'] headers["x-ms-meta-" + key] = value self.logger.log(str(headers)) http_util = HttpUtil(self.logger) sasuri_obj = urlparse.urlparse(sasuri + '&comp=snapshot') temp_logger = temp_logger + 'start calling the snapshot rest api. ' result = http_util.Call('PUT', sasuri_obj, body_content, headers=headers) temp_logger = temp_logger + ' snapshot api returned: {0} '.format( result) end_time = datetime.datetime.utcnow() time_taken = end_time - start_time temp_logger = temp_logger + ' time taken for snapshot ' + str( time_taken) if (result != CommonVariables.success): snapshot_error.errorcode = result snapshot_error.sasuri = sasuri except Exception as e: errorMsg = " Failed to do the snapshot with error: %s, stack trace: %s" % ( str(e), traceback.format_exc()) error_logger = error_logger + errorMsg snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri temp_logger = temp_logger + ' snapshot ends..' global_logger.put(temp_logger) global_error_logger.put(error_logger) snapshot_result_error.put(snapshot_error)
class Oven: def __init__(self,token,haID, url): self.token = "Bearer " + token self.haID = haID self.url = url self.headers = {"Accept":"application/vnd.bsh.sdk.v1+json","Content-Type":"application/json","Authorization": self.token} self.httputil = HttpUtil() self.logger = logModule() def getStatus(self): url = self.url + self.haID + "/status" self.logger.debug(url) duration = self.httputil.Get(url,self.headers) if duration is not -1: self.logger.info("Get Status in "+ str(duration) + " seconds") else: self.logger.error("Fail to operate HA due to unknown reason") def putProgram(self): program = "" randomPick = (random.randint(0,20)%3) if randomPick is 0: body = {"data":{"key":"Cooking.Oven.Program.HeatingMode.PreHeating","options":[{"key":"Cooking.Oven.Option.SetpointTemperature","value":230,"unit":"°C"},{"key":"BSH.Common.Option.Duration","value":1200,"unit":"seconds"}]}} program = "PreHeating" elif randomPick is 1: body = {"data":{"key":"Cooking.Oven.Program.HeatingMode.HotAir","options":[{"key":"Cooking.Oven.Option.SetpointTemperature","value":230,"unit":"°C"},{"key":"BSH.Common.Option.Duration","value":1200,"unit":"seconds"}]}} program = "HotAir" elif randomPick is 2: body = {"data":{"key":"Cooking.Oven.Program.HeatingMode.TopBottomHeating","options":[{"key":"Cooking.Oven.Option.SetpointTemperature","value":230,"unit":"°C"},{"key":"BSH.Common.Option.Duration","value":1200,"unit":"seconds"}]}} program = "TopBottomHeating" elif randomPick is 3: body = {"data":{"key":"Cooking.Oven.Program.HeatingMode.PizzaSetting","options":[{"key":"Cooking.Oven.Option.SetpointTemperature","value":230,"unit":"°C"},{"key":"BSH.Common.Option.Duration","value":1200,"unit":"seconds"}]}} program = "Eco 50 degree" url = self.url + self.haID + "/programs/active" self.logger.debug(url) bodyJson = json.dumps(body) duration = self.httputil.Put(url,bodyJson,self.headers) if duration is not -1: self.logger.info(program + " starts in " + str(duration) + " seconds") else: self.logger.error("Failed to operation HA due to unknown reason") def stopProgram(self): url = self.url + self.haID + "/programs/active" duration = self.httputil.Delete(url,self.headers) if duration is not -1: self.logger.info("program stops in "+ str(duration)+ " seconds") else: self.logger.error("Failed to operate HA due to unknown reason")
def doFreezeSnapshot(self): run_result = CommonVariables.success run_status = 'success' all_failed = False unable_to_sleep = False """ Do Not remove below HttpUtil object creation. This is to ensure HttpUtil singleton object is created before freeze.""" http_util = HttpUtil(self.logger) if(self.takeSnapshotFrom == CommonVariables.onlyGuest): run_result, run_status, blob_snapshot_info_array, all_failed, all_snapshots_failed, unable_to_sleep, is_inconsistent = self.takeSnapshotFromGuest() elif(self.takeSnapshotFrom == CommonVariables.firstGuestThenHost): run_result, run_status, blob_snapshot_info_array, all_failed, unable_to_sleep, is_inconsistent = self.takeSnapshotFromFirstGuestThenHost() elif(self.takeSnapshotFrom == CommonVariables.firstHostThenGuest): run_result, run_status, blob_snapshot_info_array, all_failed, unable_to_sleep, is_inconsistent = self.takeSnapshotFromFirstHostThenGuest() elif(self.takeSnapshotFrom == CommonVariables.onlyHost): run_result, run_status, blob_snapshot_info_array, all_failed, unable_to_sleep, is_inconsistent = self.takeSnapshotFromOnlyHost() else : self.logger.log('Snapshot method did not match any listed type, taking firstHostThenGuest as default') run_result, run_status, blob_snapshot_info_array, all_failed, unable_to_sleep, is_inconsistent = self.takeSnapshotFromFirstHostThenGuest() self.logger.log('doFreezeSnapshot : run_result - {0} run_status - {1} all_failed - {2} unable_to_sleep - {3} is_inconsistent - {4} values post snapshot'.format(str(run_result), str(run_status), str(all_failed), str(unable_to_sleep), str(is_inconsistent))) if (run_result == CommonVariables.success): run_result, run_status = self.updateErrorCode(blob_snapshot_info_array, all_failed, unable_to_sleep, is_inconsistent) snapshot_info_array = self.update_snapshotinfoarray(blob_snapshot_info_array) if not (run_result == CommonVariables.success): self.hutil.SetExtErrorCode(self.extensionErrorCode) return run_result, run_status, snapshot_info_array
def ClearPageBlob(self, blobUri): if (blobUri is not None): retry_times = 3 while (retry_times > 0): try: http_util = HttpUtil(self.hutil) sasuri_obj = urlparse.urlparse(blobUri + '&comp=page') # Get Blob-properties to know content-length blobProperties = self.GetBlobProperties(blobUri) contentLength = int(blobProperties.contentLength) # Clear Pages if (contentLength > 0): result = self.put_page_clear(blobUri, 0, contentLength) if (result == CommonVariables.success): self.hutil.log( "ClearPageBlob: page-blob cleared succesfully") retry_times = 0 else: self.hutil.log( "ClearPageBlob: page-blob failed to clear") else: self.hutil.log( "ClearPageBlob: page-blob contentLength is 0") retry_times = 0 except Exception as e: self.hutil.log( "ClearPageBlob: Failed to clear to page-blob with error: %s, stack trace: %s" % (str(e), traceback.format_exc())) self.hutil.log("ClearPageBlob: retry times is " + str(retry_times)) retry_times = retry_times - 1 else: self.hutil.log("ClearPageBlob: bloburi is None")
def CheckHttpConnectivity(self): print "-------------------------------" while HttpUtil.CheckConnectivity(self.pCTX.connectivityCheckUrl, 1) == False: time.sleep(1) self.workerRet = 3 print "-------------------------------" return
def create_secret(self, AccessToken, KeyVaultURL, secret_value, KeyEncryptionAlgorithm, DiskEncryptionKeyFileName): """ create secret api https://msdn.microsoft.com/en-us/library/azure/dn903618.aspx https://mykeyvault.vault.azure.net/secrets/{secret-name}?api-version={api-version} """ try: secret_name = str(uuid.uuid4()) secret_keyvault_uri = self.urljoin(KeyVaultURL, "secrets", secret_name) self.logger.log( "secret_keyvault_uri is: {0} and keyvault_uri is:{1}".format( secret_keyvault_uri, KeyVaultURL)) if (KeyEncryptionAlgorithm is None): request_content = '{{"value":"{0}","attributes":{{"enabled":"true"}},"tags":{{"DiskEncryptionKeyFileName":"{1}"}}}}'\ .format(str(secret_value),DiskEncryptionKeyFileName) else: request_content = '{{"value":"{0}","attributes":{{"enabled":"true"}},"tags":{{"DiskEncryptionKeyEncryptionAlgorithm":"{1}","DiskEncryptionKeyFileName":"{2}"}}}}'\ .format(str(secret_value),KeyEncryptionAlgorithm,DiskEncryptionKeyFileName) http_util = HttpUtil(self.logger) headers = {} headers["Content-Type"] = "application/json" headers["Authorization"] = "Bearer " + AccessToken result = http_util.Call(method='PUT', http_uri=secret_keyvault_uri + '?api-version=' + self.api_version, data=request_content, headers=headers) self.logger.log("{0} {1}".format(result.status, result.getheaders())) result_content = result.read() self.logger.log("result_content is {0}".format(result_content)) result_json = json.loads(result_content) secret_id = result_json["id"] http_util.connection.close() if (result.status != httplib.OK and result.status != httplib.ACCEPTED): self.logger.log("the result status failed.") return None return secret_id except Exception as e: self.logger.log( "Failed to create_secret with error: {0}, stack trace: {1}". format(e, traceback.format_exc())) return None
def get_access_token(self,AuthorizeUri,AADClientID,AADClientSecret): keyvault_resource_name = "https://vault.azure.net" token_uri = AuthorizeUri + "/oauth2/token" request_content = "resource=" + urllib.quote(keyvault_resource_name) + "&client_id=" + AADClientID + "&client_secret=" + urllib.quote(AADClientSecret) + "&grant_type=client_credentials" headers = {} http_util = HttpUtil(self.logger) result = http_util.Call(method='POST', http_uri=token_uri, data=request_content, headers=headers) self.logger.log("{0} {1}".format(result.status, result.getheaders())) result_content = result.read() if(result.status != httplib.OK and result.status != httplib.ACCEPTED): self.logger.log(str(result_content)) return CommonVariables.create_encryption_secret_failed http_util.connection.close() result_json = json.loads(result_content) access_token = result_json["access_token"] return access_token
def GetBlobMetadata(self, blobUri): blobProperties = None if (blobUri is not None): try: http_util = HttpUtil(self.logger) sasuri_obj = urlparser.urlparse(blobUri + '&comp=metadata') headers = {} result, httpResp, errMsg = http_util.HttpCallGetResponse( 'GET', sasuri_obj, None, headers=headers) self.logger.log( "FS : GetBlobMetadata: HttpCallGetResponse : result :" + str(result) + ", errMsg :" + str(errMsg)) blobProperties = self.httpresponse_parse_metadata(httpResp) except Exception as e: self.logger.log( "FS : GetBlobMetadata: Failed to get blob properties with error: %s, stack trace: %s" % (str(e), traceback.format_exc())) return blobProperties
def try_resize_page_blob(self, blobUri, size): isSuccessful = False if (size % 512 == 0): try: http_util = HttpUtil(self.hutil) sasuri_obj = urlparse.urlparse(blobUri + '&comp=properties') headers = {} headers["x-ms-blob-content-length"] = size headers["Content-Length"] = size result = http_util.Call(method = 'PUT', sasuri_obj = sasuri_obj, data = None, headers = headers, fallback_to_curl = True) if(result == CommonVariables.success): isSuccessful = True else: self.hutil.log("try_resize_page_blob: page-blob resize failed, size :"+str(size)+", result :"+str(result)) except Exception as e: self.hutil.log("try_resize_page_blob: failed to resize page-blob with error: %s, stack trace: %s" % (str(e), traceback.format_exc())) else: self.hutil.log("try_resize_page_blob: invalid size : " + str(size)) return isSuccessful
def GetBlobProperties(self, blobUri): blobProperties = None if(blobUri is not None): retry_times = 3 while(retry_times > 0): try: http_util = HttpUtil(self.hutil) sasuri_obj = urlparse.urlparse(blobUri) headers = {} result, httpResp, errMsg = http_util.HttpCallGetResponse('GET', sasuri_obj, None, headers = headers) self.hutil.log("GetBlobProperties: HttpCallGetResponse : result :" + str(result) + ", errMsg :" + str(errMsg)) blobProperties = self.httpresponse_get_blob_properties(httpResp) self.hutil.log("GetBlobProperties: blobProperties :" + str(blobProperties)) retry_times = 0 except Exception as e: self.hutil.log("GetBlobProperties: Failed to get blob properties with error: %s, stack trace: %s" % (str(e), traceback.format_exc())) self.hutil.log("GetBlobProperties: retry times is " + str(retry_times)) retry_times = retry_times - 1 return blobProperties
def on_downloadItems(self, obj): logger.info('on_downloadItems') for item in obj.get('downloadItems'): if(item.get('savename') and item.get('url')): ret = HttpUtil.download(item) if ret and type(ret) == dict: item.update(ret) else: logger.error('downloadItem error!') self.emit('downloadItemsFinished', obj) logger.info('on_downloadItems finished')
def get_access_token(self, KeyVaultResourceName, AuthorizeUri, AADClientID, AADClientCertThumbprint, AADClientSecret): if not AADClientSecret and not AADClientCertThumbprint: raise ValueError( "Missing Credentials. Either AADClientSecret or AADClientCertThumbprint must be specified" ) if AADClientSecret and AADClientCertThumbprint: raise ValueError( "Both AADClientSecret and AADClientCertThumbprint were supplied, when only one of these was expected." ) if AADClientCertThumbprint: return self.get_access_token_with_certificate( KeyVaultResourceName, AuthorizeUri, AADClientID, AADClientCertThumbprint) else: # retrieve access token directly, adal library not required token_uri = AuthorizeUri + "/oauth2/token" request_content = "resource=" + urllib.quote( KeyVaultResourceName ) + "&client_id=" + AADClientID + "&client_secret=" + urllib.quote( AADClientSecret) + "&grant_type=client_credentials" headers = {} http_util = HttpUtil(self.logger) result = http_util.Call(method='POST', http_uri=token_uri, data=request_content, headers=headers) self.logger.log("{0} {1}".format(result.status, result.getheaders())) result_content = result.read() if result.status != httplib.OK and result.status != httplib.ACCEPTED: self.logger.log(str(result_content)) return None http_util.connection.close() result_json = json.loads(result_content) access_token = result_json["access_token"] return access_token
def snapshot(self, sasuri, meta_data): result = None snapshot_error = SnapshotError() if (sasuri is None): self.logger.log("Failed to do the snapshot because sasuri is none", False, 'Error') snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri try: sasuri_obj = urlparse.urlparse(sasuri) if (sasuri_obj is None or sasuri_obj.hostname is None): self.logger.log("Failed to parse the sasuri", False, 'Error') snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri else: body_content = '' headers = {} headers["Content-Length"] = '0' for meta in meta_data: key = meta['Key'] value = meta['Value'] headers["x-ms-meta-" + key] = value self.logger.log(str(headers)) http_util = HttpUtil(self.logger) sasuri_obj = urlparse.urlparse(sasuri + '&comp=snapshot') self.logger.log("start calling the snapshot rest api") result = http_util.Call('PUT', sasuri_obj, body_content, headers=headers) self.logger.log("snapshot api returned: {0}".format(result)) if (result != CommonVariables.success): snapshot_error.errorcode = result snapshot_error.sasuri = sasuri except Exception as e: errorMsg = "Failed to do the snapshot with error: %s, stack trace: %s" % ( str(e), traceback.format_exc()) self.logger.log(errorMsg, False, 'Error') snapshot_error.errorcode = CommonVariables.error snapshot_error.sasuri = sasuri return snapshot_error
def main(): failNum = 0 global g_HttpUtil g_HttpUtil = HttpUtil(GetProxy()) for account in GenerateAccount(): ret = Register(account) if ret == None: print 'failed:', account['nickName'] failNum += 1 #consecutive failure 5 times,berak if failNum >= 5: proxy = GetProxy() if proxy == None: break g_HttpUtil = HttpUtil(proxy) failNum = 0 continue print 'success:', ret['nickName'] RecordSuccess(ret) failNum = 0 time.sleep(1)
def encrypt_passphrase(self, AccessToken, Passphrase, KeyVaultURL, KeyEncryptionKeyURL, AADClientID, KeyEncryptionAlgorithm, AADClientSecret): try: """ wrap our passphrase using the encryption key api ref for wrapkey: https://msdn.microsoft.com/en-us/library/azure/dn878066.aspx """ self.logger.log("encrypting the secret using key: " + KeyEncryptionKeyURL) request_content = '{"alg":"' + str( KeyEncryptionAlgorithm) + '","value":"' + str( Passphrase) + '"}' headers = {} headers["Content-Type"] = "application/json" headers["Authorization"] = "Bearer " + str(AccessToken) relative_path = KeyEncryptionKeyURL + "/wrapkey" + '?api-version=' + self.api_version http_util = HttpUtil(self.logger) result = http_util.Call(method='POST', http_uri=relative_path, data=request_content, headers=headers) result_content = result.read() self.logger.log("result_content is: {0}".format(result_content)) self.logger.log("{0} {1}".format(result.status, result.getheaders())) if result.status != httplib.OK and result.status != httplib.ACCEPTED: return None http_util.connection.close() result_json = json.loads(result_content) secret_value = result_json[u'value'] return secret_value except Exception as e: self.logger.log( "Failed to encrypt_passphrase with error: {0}, stack trace: %s" .format(e, traceback.format_exc())) return None
def GetApiInfo(self): apiUrl = self.pCTX.infoApiUrl savePath = "/tmp/WanemApiInfo.json" print "-------------------------------" while HttpUtil.Get(apiUrl, savePath, 1) == False: time.sleep(1) file = open(savePath) dat = json.load(file) file.close() #pprint(dat) self.pCTX.apiStatus = dat["status"]["maintStatus"] self.workerRet = 3 print "-------------------------------" return
def WriteBlockBlob(self, msg, blobUri, blobProperties): retry_times = 3 while (retry_times > 0): try: # get the blob type if (blobUri is not None): http_util = HttpUtil(self.hutil) sasuri_obj = urlparse.urlparse(blobUri) headers = {} headers["x-ms-blob-type"] = 'BlockBlob' self.hutil.log(str(headers)) result = http_util.Call(method='PUT', sasuri_obj=sasuri_obj, data=msg, headers=headers, fallback_to_curl=True) if (result == CommonVariables.success): self.hutil.log("blob written succesfully") retry_times = 0 else: self.hutil.log("blob failed to write") HandlerUtil.HandlerUtility.add_to_telemetery_data( CommonVariables.statusBlobUploadError, "true") else: self.hutil.log("bloburi is None") retry_times = 0 HandlerUtil.HandlerUtility.add_to_telemetery_data( CommonVariables.statusBlobUploadError, "true") except Exception as e: HandlerUtil.HandlerUtility.add_to_telemetery_data( CommonVariables.statusBlobUploadError, "true") self.hutil.log( "Failed to committing the log with error: %s, stack trace: %s" % (str(e), traceback.format_exc())) self.hutil.log("retry times is " + str(retry_times)) retry_times = retry_times - 1
def encrypt_passphrase(self,AccessToken,Passphrase, KeyVaultURL, KeyEncryptionKeyURL, AADClientID, KeyEncryptionAlgorithm, AADClientSecret): try: """ api for encrypt use key is https://msdn.microsoft.com/en-us/library/azure/dn878060.aspx get the key information, to get the key id, so we can use that key to do encryption https://mykeyvault.vault.azure.net/keys/{key-name}?api-version={api-version} https://msdn.microsoft.com/en-us/library/azure/dn878080.aspx """ self.logger.log("getting the info of the key.") http_util = HttpUtil(self.logger) headers = {} headers["Authorization"] = "Bearer " + AccessToken result = http_util.Call(method='GET', http_uri=KeyEncryptionKeyURL + '?api-version=' + self.api_version, data=None, headers=headers) self.logger.log("{0} {1}".format(result.status, result.getheaders())) if(result.status != httplib.OK and result.status != httplib.ACCEPTED): return None result_content = result.read() self.logger.log("result_content is {0}".format(result_content)) http_util.connection.close() result_json = json.loads(result_content) key_id = result_json["key"]["kid"] """ encrypt our passphrase using the encryption key api for encrypt use key is https://msdn.microsoft.com/en-us/library/azure/dn878060.aspx """ self.logger.log("encrypting the secret using key: " + str(key_id)) request_content = '{"alg":"' + str(KeyEncryptionAlgorithm) + '","value":"' + str(Passphrase) + '"}' headers = {} headers["Content-Type"] = "application/json" headers["Authorization"] = "Bearer " + str(AccessToken) relative_path=key_id+"/encrypt" + '?api-version=' + self.api_version http_util = HttpUtil(self.logger) result = http_util.Call(method='POST',http_uri=relative_path,data=request_content,headers=headers) result_content = result.read() self.logger.log("result_content is: {0}".format(result_content)) self.logger.log("{0} {1}".format(result.status, result.getheaders())) if(result.status != httplib.OK and result.status != httplib.ACCEPTED): return None http_util.connection.close() result_json = json.loads(result_content) secret_value = result_json[u'value'] return secret_value except Exception as e: self.logger.log("Failed to encrypt_passphrase with error: {0}, stack trace: %s".format(e, traceback.format_exc())) return None
def WritePageBlob(self, message, blobUri): if (blobUri is not None): retry_times = 3 while (retry_times > 0): msg = message try: PAGE_SIZE_BYTES = 512 PAGE_UPLOAD_LIMIT_BYTES = 4194304 # 4 MB STATUS_BLOB_LIMIT_BYTES = 10485760 # 10 MB http_util = HttpUtil(self.hutil) sasuri_obj = urlparse.urlparse(blobUri + '&comp=page') # Get Blob-properties to know content-length blobProperties = self.GetBlobProperties(blobUri) blobContentLength = int(blobProperties.contentLength) self.hutil.log("WritePageBlob: contentLength:" + str(blobContentLength)) maxMsgLen = STATUS_BLOB_LIMIT_BYTES if (blobContentLength > STATUS_BLOB_LIMIT_BYTES): maxMsgLen = blobContentLength msgLen = len(msg) self.hutil.log("WritePageBlob: msg length:" + str(msgLen)) if (len(msg) > maxMsgLen): msg = msg[msgLen - maxMsgLen:msgLen] msgLen = len(msg) self.hutil.log( "WritePageBlob: msg length after aligning to maxMsgLen:" + str(msgLen)) if ((msgLen % PAGE_SIZE_BYTES) != 0): # Add padding to message to make its legth multiple of 512 paddedLen = msgLen + (512 - (msgLen % PAGE_SIZE_BYTES)) msg = msg.ljust(paddedLen) msgLen = len(msg) self.hutil.log( "WritePageBlob: msg length after aligning to page-size(512):" + str(msgLen)) if (blobContentLength < msgLen): # Try to resize blob to increase its size isSuccessful = self.try_resize_page_blob( blobUri, msgLen) if (isSuccessful == True): self.hutil.log( "WritePageBlob: page-blob resized successfully new size(blobContentLength):" + str(msgLen)) blobContentLength = msgLen else: self.hutil.log( "WritePageBlob: page-blob resize failed") if (msgLen > blobContentLength): msg = msg[msgLen - blobContentLength:msgLen] msgLen = len(msg) self.hutil.log( "WritePageBlob: msg length after aligning to blobContentLength:" + str(msgLen)) # Write Pages result = CommonVariables.error bytes_sent = 0 while (bytes_sent < msgLen): bytes_remaining = msgLen - bytes_sent pageContent = None if (bytes_remaining > PAGE_UPLOAD_LIMIT_BYTES): # more than 4 MB pageContent = msg[bytes_sent:bytes_sent + PAGE_UPLOAD_LIMIT_BYTES] else: pageContent = msg[bytes_sent:msgLen] self.hutil.log("WritePageBlob: pageContentLen:" + str(len(pageContent))) result = self.put_page_update(pageContent, blobUri, bytes_sent) if (result == CommonVariables.success): self.hutil.log( "WritePageBlob: page written succesfully") else: self.hutil.log( "WritePageBlob: page failed to write") break bytes_sent = bytes_sent + len(pageContent) if (result == CommonVariables.success): self.hutil.log( "WritePageBlob: page-blob written succesfully") retry_times = 0 else: self.hutil.log( "WritePageBlob: page-blob failed to write") except Exception as e: self.hutil.log( "WritePageBlob: Failed to write to page-blob with error: %s, stack trace: %s" % (str(e), traceback.format_exc())) self.hutil.log("WritePageBlob: retry times is " + str(retry_times)) retry_times = retry_times - 1 else: self.hutil.log("WritePageBlob: bloburi is None")
from HttpUtil import HttpUtil from lxml import etree import urllib2 from urllib2 import urlopen from urllib import urlencode import time import random #proxy_handler = urllib2.ProxyHandler({'http': 'http://211.167.112.16:82'}) #opener = urllib2.build_opener(proxy_handler) #urllib2.install_opener(opener) g_HttpUtil = HttpUtil() g_SuccessFile = 'result.txt' g_ProxyFile = 'proxy.txt' g_ProxyIt = 0 g_ProxyList = [] with open(g_ProxyFile) as f: lines = f.readlines() g_ProxyList = [{'http': 'http://' + line.strip()} for line in lines] def randomStr(l): ret = '' for i in xrange(l): ret += chr(ord('a') + random.randint(0, 25)) return ret def GenerateAccount():