def _do_package_core(self, func_path): zipfile, zip_file_name = self._zip_func(func_path) code_url = dict() if self.cos_bucket: CosClient().upload_file2cos(bucket=self.cos_bucket, file=zipfile.read(), key=zip_file_name) code_url["cos_bucket_name"] = self.cos_bucket code_url["cos_object_name"] = "/" + zip_file_name else: code_url["zip_file"] = os.path.join(os.getcwd(), zip_file_name) return code_url
def _do_package_core(self, func_path, namespace, func_name, region=None): zipfile, zip_file_name, zip_file_name_cos = self._zip_func(func_path, namespace, func_name) code_url = dict() if self.cos_bucket: CosClient(region).upload_file2cos(bucket=self.cos_bucket, file=zipfile.read(), key=zip_file_name_cos) code_url["cos_bucket_name"] = self.cos_bucket code_url["cos_object_name"] = "/" + zip_file_name_cos else: code_url["zip_file"] = os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name) return code_url
def _do_package_core(self, func_path, namespace, func_name, region=None): zipfile, zip_file_name, zip_file_name_cos = self._zip_func(func_path, namespace, func_name) code_url = dict() file_size = os.path.getsize(os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name)) Operation("Package name: %s, package size: %s kb" % (zip_file_name, str(file_size / 1000))).process() default_bucket_name = "" if UserConfig().using_cos.upper().startswith("TRUE"): using_cos = True default_bucket_name = "scf-deploy-" + region + "-" + str(UserConfig().appid) else: using_cos = False if self.without_cos: size_infor = self.file_size_infor(file_size) # size_infor = -1 if size_infor == -1: msg = 'Your package %s is too large and needs to be uploaded via COS.' % (zip_file_name) Operation(msg).warning() msg = 'You can use --cos-bucket BucketName to specify the bucket, or you can use the "scf configure set" to set the default to open the cos upload.' Operation(msg).warning() return elif size_infor == 0: Operation("Package %s size is over 8M, it is highly recommended that you upload using COS. " % ( zip_file_name)).information() Operation("Uploading this package without COS.").process() code_url["zip_file"] = os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name) Operation("%s Upload success" % (zip_file_name)).success() elif self.cos_bucket: bucket_name = self.cos_bucket + "-" + UserConfig().appid Operation("Uploading this package to COS, bucket_name: %s" % (bucket_name)).process() CosClient(region).upload_file2cos(bucket=self.cos_bucket, file=zipfile.read(), key=zip_file_name_cos) Operation("Upload success").success() code_url["cos_bucket_name"] = self.cos_bucket code_url["cos_object_name"] = "/" + zip_file_name_cos msg = "Upload function zip file '{}' to COS bucket '{}' success.".format(os.path.basename( \ code_url["cos_object_name"]), code_url["cos_bucket_name"]) Operation(msg).success() elif using_cos: Operation("By default, this package will be uploaded to COS.").information() Operation("Default COS-bucket: " + default_bucket_name).information() Operation( "If you don't want to upload the package to COS by default, you could change your configure!").information() cos_client = CosClient(region) Operation("Checking you COS Bucket: %s." % (default_bucket_name)).process() cos_bucket_status = cos_client.get_bucket(default_bucket_name) if cos_bucket_status == 0: # 未获得到bucket Operation("Creating default COS Bucket: " + default_bucket_name).process() create_status = cos_client.create_bucket(bucket=default_bucket_name) if create_status == True: cos_bucket_status = 1 Operation("Creating success. Cos Bucket name: " + default_bucket_name).success() else: Operation("Creating Cos Bucket: %s faild." % (default_bucket_name)).warning() cos_bucket_status = create_status try: if "<?xml" in str(create_status): error_code = re.findall("<Code>(.*?)</Code>", str(create_status))[0] error_message = re.findall("<Message>(.*?)</Message>", str(create_status))[0] Operation("COS client error code: %s, message: %s" % (error_code, error_message)).warning() except: pass if cos_bucket_status == 1: try: # 获取bucket正常,继续流程 file_data = zipfile.read() md5 = hashlib.md5(file_data).hexdigest() is_have = 0 try: object_list = cos_client.get_object_list( bucket=default_bucket_name, prefix=str(namespace) + "-" + str(func_name) ) if isinstance(object_list, dict) and 'Contents' in object_list: for eve_object in object_list["Contents"]: if md5 in eve_object["ETag"]: response = cos_client.copy_object( default_bucket_name, eve_object["Key"], zip_file_name_cos, ) is_have = 1 break except: pass if is_have == 0: Operation("Uploading to COS, bucket name: " + default_bucket_name).process() # 普通上传 cos_client.upload_file2cos( bucket=default_bucket_name, file=file_data, key=zip_file_name_cos ) # 分块上传 # cos_client.upload_file2cos2( # bucket=default_bucket_name, # file=os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name), # key=zip_file_name_cos, # md5=md5, # ) code_url["cos_bucket_name"] = default_bucket_name.replace("-" + UserConfig().appid, '') \ if default_bucket_name and default_bucket_name.endswith( "-" + UserConfig().appid) else default_bucket_name code_url["cos_object_name"] = "/" + zip_file_name_cos msg = "Upload function zip file '{}' to COS bucket '{}' success.".format(os.path.basename( \ code_url["cos_object_name"]), code_url["cos_bucket_name"]) Operation(msg).success() except Exception as e: cos_bucket_status = e # cos_bucket_status = 2 if cos_bucket_status not in (0, 1): size_infor = self.file_size_infor(file_size) if size_infor == -1: Operation("Upload Error.").warning() raise UploadFailed(str(e)) else: Operation("There are some exceptions and the process of uploading to COS is terminated!").warning() if len(str(cos_bucket_status)) > 3: Operation(str(cos_bucket_status)).warning() Operation("This package will be uploaded by TencentCloud Cloud API.").information() if size_infor == 0: msg = "Package size is over 8M, it is highly recommended that you upload using COS. " Operation(msg).information() Operation("Uploading this package.").process() code_url["zip_file"] = os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name) Operation("Upload success").success() else: msg = "If you want to increase the upload speed, you can configure using-cos with command:scf configure set" Operation(msg).information() size_infor = self.file_size_infor(file_size) if size_infor == -1: msg = 'Your package is too large and needs to be uploaded via COS.' Operation(msg).warning() msg = 'You can use --cos-bucket BucketName to specify the bucket, or you can use the "scf configure set" to set the default to open the cos upload.' Operation(msg).warning() raise UploadFailed("Upload faild") elif size_infor == 0: Operation("Package size is over 8M, it is highly recommended that you upload using COS. ").information() Operation("Uploading this package.").process() code_url["zip_file"] = os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name) Operation("Upload success").success() return code_url
def do_package(self): package_result = { "success": [], "faild": [] } for namespace in self.resource: for function in list(self.resource[namespace]): if function == tsmacro.Type: continue if self.function is not None and function != self.function: self.resource[namespace].pop(function) continue if self.history: cos_function = CosClient(self.region).get_object_list( bucket=self.bucket_name, prefix=str(namespace) + "-" + str(function) ) if isinstance(cos_function, dict) and 'Contents' in cos_function: rollback_dict = {} cos_function_list = cos_function['Contents'] if cos_function_list: msg = "[+] %s %s :Please select a historical deployment Number for the historical version deployment." % ( namespace, function) Operation(msg, fg="cyan").echo() function_number = 0 for eve_function in reversed(cos_function_list): function_number = function_number + 1 if function_number > 15: break else: function_name = eve_function["LastModified"].replace(".000Z", "").replace("T", " ") msg = " [%s] %s" % (function_number, text(function_name)) Operation(msg, fg="cyan").echo() rollback_dict[str(function_number)] = eve_function["Key"] number = click.prompt(Operation("Please input number(Like: 1)", fg="cyan").style()) if number not in rollback_dict: err_msg = "Please enter the version number correctly, for example the number 1." raise RollbackException(err_msg) else: code_url = { 'cos_bucket_name': "scf-deploy-" + self.region, 'cos_object_name': rollback_dict[number] } msg = "Select function zip file '{}' on COS bucket '{}' success.".format( os.path.basename(code_url["cos_object_name"]), code_url["cos_bucket_name"]) Operation(msg).success() else: err_msg = "The historical version is not queried. The deployment history version code only takes effect when you use using-cos." raise RollbackException(err_msg) else: err_msg = "The historical version is not queried. The deployment history version code only takes effect when you use using-cos." raise RollbackException(err_msg) else: template_path, template_name = os.path.split(self.template_file) code_uri = self.resource[namespace][function][tsmacro.Properties].get(tsmacro.CodeUri, "") function_path = os.path.join(template_path, code_uri) # print(function_path) code_url = self._do_package_core( # self.resource[namespace][function][tsmacro.Properties].get(tsmacro.CodeUri, ""), function_path, namespace, function, self.region ) try: if "cos_bucket_name" in code_url: bucket_name = code_url["cos_bucket_name"] object_name = code_url["cos_object_name"] self.resource[namespace][function][tsmacro.Properties]["CosBucketName"] = bucket_name self.resource[namespace][function][tsmacro.Properties]["CosObjectName"] = object_name package_result["success"].append((namespace, function)) elif "zip_file" in code_url: self.resource[namespace][function][tsmacro.Properties]["LocalZipFile"] = code_url["zip_file"] package_result["success"].append((namespace, function)) else: del self.resource[namespace][function] package_result["faild"].append((namespace, function)) except: package_result["faild"].append((namespace, function)) # print(package_result) return (package_result, self.resource)
def _do_package_core(self, func_path, namespace, func_name, region=None): zipfile, zip_file_name, zip_file_name_cos = self._zip_func( func_path, namespace, func_name) code_url = dict() file_size = os.path.getsize( os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name)) Operation("Package name: %s, package size: %s kb" % (zip_file_name, str(file_size / 1000))).process() default_bucket_name = "" if UserConfig().using_cos.startswith("True"): cos_bucket_status = True default_bucket_name = "scf-deploy-" + region + "-" + str( UserConfig().appid) else: cos_bucket_status = False if self.without_cos: self.file_size_infor(file_size) Operation("Uploading this package without COS.").process() code_url["zip_file"] = os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name) Operation("Upload success").success() elif self.cos_bucket: bucket_name = self.cos_bucket + "-" + UserConfig().appid Operation("Uploading this package to COS, bucket_name: %s" % (bucket_name)).process() CosClient(region).upload_file2cos(bucket=self.cos_bucket, file=zipfile.read(), key=zip_file_name_cos) Operation("Upload success").success() code_url["cos_bucket_name"] = self.cos_bucket code_url["cos_object_name"] = "/" + zip_file_name_cos msg = "Upload function zip file '{}' to COS bucket '{}' success.".format(os.path.basename( \ code_url["cos_object_name"]), code_url["cos_bucket_name"]) Operation(msg).success() elif cos_bucket_status: Operation("By default, this package will be uploaded to COS." ).information() Operation("Default COS-bucket: " + default_bucket_name).information() Operation("If you don't want to upload the package to COS by default, you could change your configure!") \ .information() # 根据region设置cos_client cos_client = CosClient(region) Operation("Checking you COS-bucket.").process() # 获取COS bucket cos_bucket_status = cos_client.get_bucket(default_bucket_name) if cos_bucket_status == -1: Operation("reating default COS-bucket: " + default_bucket_name).process() create_status = cos_client.create_bucket( bucket=default_bucket_name) if create_status == True: cos_bucket_status = 0 Operation("Creating success.").success() else: try: if "<?xml" in str(create_status): error_code = re.findall("<Code>(.*?)</Code>", str(create_status))[0] error_message = re.findall( "<Message>(.*?)</Message>", str(create_status))[0] Operation( "COS client error code: %s, message: %s" % (error_code, error_message)).warning() finally: cos_bucket_status = create_status Operation("Creating faild.").warning() if cos_bucket_status != 0: Operation( "There are some exceptions and the process of uploading to COS is terminated!" ).warning() Operation( "This package will be uploaded by TencentCloud Cloud API." ).information() Operation("Uploading this package.").process() code_url["zip_file"] = os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name) Operation("Upload success").success() else: # 获取bucket正常,继续流程 file_data = zipfile.read() md5 = hashlib.md5(file_data).hexdigest() is_have = 0 try: object_list = cos_client.get_object_list( bucket=default_bucket_name, prefix=str(namespace) + "-" + str(func_name)) if isinstance(object_list, dict) and 'Contents' in object_list: for eve_object in object_list["Contents"]: if md5 in eve_object["ETag"]: response = cos_client.copy_object( default_bucket_name, eve_object["Key"], zip_file_name_cos, ) is_have = 1 break except Exception as e: pass if is_have == 0: Operation("Uploading to COS, bucket_name:" + default_bucket_name).process() cos_client.upload_file2cos(bucket=default_bucket_name, file=file_data, key=zip_file_name_cos) # cos_client.upload_file2cos2( # bucket=default_bucket_name, # file=os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name), # key=zip_file_name_cos, # md5=md5, # ) code_url["cos_bucket_name"] = default_bucket_name.replace("-" + UserConfig().appid, '') \ if default_bucket_name and default_bucket_name.endswith( "-" + UserConfig().appid) else default_bucket_name code_url["cos_object_name"] = "/" + zip_file_name_cos msg = "Upload function zip file '{}' to COS bucket '{}' success.".format(os.path.basename( \ code_url["cos_object_name"]), code_url["cos_bucket_name"]) Operation(msg).success() else: Operation( \ "If you want to increase the upload speed, you can configure using-cos with command:scf configure set") \ .information() self.file_size_infor(file_size) Operation("Uploading this package.").process() code_url["zip_file"] = os.path.join(os.getcwd(), _BUILD_DIR, zip_file_name) Operation("Upload success").success() return code_url
def do_package(self): region = self.region for ns in self.resource: for func in list(self.resource[ns]): if func == tsmacro.Type: continue if self.function is not None and func != self.function: self.resource[ns].pop(func) continue if self.history: function_list = CosClient(self.region).get_object_list( bucket="scf-deploy-" + region, prefix=str(ns) + "-" + str(func)) if isinstance(function_list, dict) and 'Contents' in function_list: rollback_dict = {} function_list_data = function_list['Contents'] if function_list_data: click.secho( "[+] Please select a historical deployment Number for the historical version deployment.", fg="cyan") i = 0 for eve_obj in reversed(function_list_data): i = i + 1 if i > 15: break click.secho( " [%s] %s" % (i, text(eve_obj["LastModified"].replace( ".000Z", "").replace("T", " "))), fg="cyan") rollback_dict[str(i)] = eve_obj["Key"] number = click.prompt( click.style("Please input number(Like: 1)", fg="cyan")) if number not in rollback_dict: raise RollbackException( "Please enter the version number correctly, for example the number 1." ) else: code_url = { 'cos_bucket_name': "scf-deploy-" + region, 'cos_object_name': rollback_dict[number] } msg = "Select function zip file '{}' on COS bucket '{}' success.".format( os.path.basename( \ code_url["cos_object_name"]), code_url["cos_bucket_name"]) Operation(msg).success() else: raise RollbackException( "The historical version is not queried. The deployment history version code only takes effect when you use using-cos." ) else: raise RollbackException( "The historical version is not queried. The deployment history version code only takes effect when you use using-cos." ) else: code_url = self._do_package_core( self.resource[ns][func][tsmacro.Properties].get( tsmacro.CodeUri, ""), ns, func, self.region) if "cos_bucket_name" in code_url: self.resource[ns][func][tsmacro.Properties][ "CosBucketName"] = code_url["cos_bucket_name"] self.resource[ns][func][tsmacro.Properties][ "CosObjectName"] = code_url["cos_object_name"] elif "zip_file" in code_url: # if self.resource[ns][func][tsmacro.Properties][tsmacro.Runtime][0:].lower() in SERVICE_RUNTIME: # error = "Service just support cos to deploy, please set using-cos by 'scf configure set --using-cos y'" # raise UploadFailed(error) self.resource[ns][func][tsmacro.Properties][ "LocalZipFile"] = code_url["zip_file"] # click.secho("Generate resource '{}' success".format(self.resource), fg="green") return self.resource