Example #1
0
def get(**kwargs):
    '''
        \b
        Get your account parameters.
        \b
        Common usage:
            \b
            * Get the configured information
              $ scf configure get
        '''
    uc = UserConfig()

    def set_true(k):
        kwargs[k] = True

    bools = [v for k, v in kwargs.items()]
    if not reduce(lambda x, y: bool(x or y), bools):
        list(map(set_true, kwargs))
    attrs = uc.get_attrs(kwargs)
    msg = "{} config:".format(UserConfig.API)
    for attr in sorted(attrs):
        attr_value = attrs[attr]
        if attr == "secret-id":
            attr_value = "*" * 32 + attr_value[32:]
        elif attr == "secret-key":
            attr_value = "*" * 28 + attr_value[28:]
        msg += click.style("\n[-] ", fg="cyan") + click.style(
            "{} = {}".format(attr, attr_value), fg="cyan")
    Operation(msg.strip()).process()
Example #2
0
 def cmd(self):
     if self._debug_context.cmd is not None:
         return self._debug_context.cmd
     elif self._runtime.runtime == 'python3.6' and UserConfig().python3_path != 'None':
         return UserConfig().python3_path
     elif self._runtime.runtime == 'python2.7' and UserConfig().python2_path != 'None':
         return UserConfig().python2_path
     else:
         return self._runtime.cmd
Example #3
0
 def cmd(self):
     if self.debug_port is None:
         if self.runtime == 'python3.6' and UserConfig(
         ).python3_path != 'None':
             return UserConfig().python3_path
         elif self.runtime == 'python2.7' and UserConfig(
         ).python2_path != 'None':
             return UserConfig().python2_path
         else:
             return self.DEBUG_CMD[self.runtime]
     return None
Example #4
0
 def __init__(self, region=None):
     uc = UserConfig()
     if region is None:
         region = uc.region
     self._config = CosConfig(Secret_id=uc.secret_id, Secret_key=uc.secret_key,
                              Region=region, Appid=uc.appid)
     self._client = CosS3Client(self._config)
Example #5
0
def list_scf_role(region):
    try:
        uc = UserConfig()
        region = region if region else uc.region
        cred = credential.Credential(uc.secret_id, uc.secret_key)
        httpProfile = HttpProfile()
        httpProfile.endpoint = "cam.tencentcloudapi.com"
        clientProfile = ClientProfile()
        clientProfile.httpProfile = httpProfile
        client = cam_client.CamClient(cred, region, clientProfile)

        req = models.DescribeRoleListRequest()
        params = '{"Page":1,"Rp":200}'
        req.from_json_string(params)

        resp = client.DescribeRoleList(req)
        data = json.loads(resp.to_json_string())
        rolelist = []
        for role in data['List']:
            for state in json.loads(role['PolicyDocument'])['statement']:
                if 'scf.qcloud.com' in state['principal']['service']:
                    rolelist.append(str(role['RoleName']))
                    continue
        return rolelist
    except:
        return None
Example #6
0
    def show(region, namespace, name):
        if region and region not in REGIONS:
            raise ArgsException("region {r} not exists ,please select from{R}".format(r=region, R=REGIONS))
        if not region:
            region = UserConfig().region

        rep = ScfClient(region).get_ns(namespace)
        if not rep:
            raise NamespaceException("Region {r} not exist namespace {n}".format(r=region, n=namespace))

        functions = ScfClient(region).get_function(function_name=name, namespace=namespace)
        if not functions:
            raise FunctionNotFound("Region {r} namespace {n} not exist function {f}".format(r=region, n=namespace, f=name))
            return

        Operation("Region:%s" % (region)).process()
        Operation("Namespace:%s " % (namespace)).process()
        Operation("Function:%s " % (name)).process()
        testmodels = ScfClient(region).list_func_testmodel(functionName=name, namespace=namespace)
        if not testmodels:
            raise NamespaceException("This function not exist event".format(f=name))

        click.secho("%-20s %-20s %-20s" % ("TestmodelsName", "AddTime", "ModTime"))
        for testmodel in testmodels:
            res = ScfClient(region).get_func_testmodel(functionName=name, namespace=namespace, testModelName=testmodel)
            click.secho("%-20s %-20s %-20s" % (testmodel, res['CreatedTime'], res['ModifiedTime']))
        click.secho("\n")
Example #7
0
def delete(region, namespace, name, force):
    '''
        \b
        Delete a SCF function.
        \b
        Common usage:
        \b
            * Delete a SCF function
              $ scf delete --name functionname --region ap-guangzhou --namespace default
    '''
    if name:

        if not region:
            region = UserConfig().region

        if region and region not in REGIONS:
            raise ArgsException("The region must in %s." % (", ".join(REGIONS)))

        Operation("Function Information:").process()
        Operation("  Region: %s" % (region)).process()
        Operation("  Namespace: %s" % (namespace)).process()
        Operation("  Function Name: %s" % (name)).process()

        if force:
            Delete.do_cli(region, namespace, name)
        else:
            Operation("This function's trigger will be deleted too").warning()
            result = click.prompt(click.style('[!] Are you sure delete this remote function? (y/n)', fg="magenta"))
            if result in ["y", "Y"]:
                Delete.do_cli(region, namespace, name)
            else:
                Operation("Delete operation has been canceled").warning()
    else:
        raise ArgsException("You must give a name, like: scf delete --name YourFunctionName! ")
Example #8
0
 def __init__(self):
     uc = UserConfig()
     self._cred = credential.Credential(secretId=uc.secret_id,
                                        secretKey=uc.secret_key)
     self._region = uc.region
     cp = ClientProfile("TC3-HMAC-SHA256")
     self._client = scf_client.ScfClient(self._cred, self._region, cp)
def report_use_infos():
    try:
        uc = UserConfig()
        data = {}
        data['appid'] = uc.appid
        data['region'] = uc.region

        version = platform.python_version()
        if version >= '3':
            from urllib.request import Request, urlopen
        else:
            from urllib2 import Request, urlopen

        try:
            socket.setdefaulttimeout(1)
            ssl._create_default_https_context = ssl._create_unverified_context

            url = 'https://service-qgphxt7y-1253970226.gz.apigw.tencentcs.com/release/scf_operation_report'
            postdata = json.dumps(data)

            if version >= '3':
                req = Request(url, data=postdata.encode("utf-8"))
            else:
                req = Request(url, data=postdata)
            res = urlopen(req)

        except Exception:
            pass
    except Exception as e:
        pass
 def echo(self):
     self.log(logs="INFO")
     if "--no-color" in sys.argv or "-nc" in sys.argv or UserConfig(
     ).section_map[UserConfig.OTHERS]['no_color'].startswith('True'):
         click.secho(
             u'%s' % self.format_message(),
             bold=self.bold,
             dim=self.dim,
             underline=self.underline,
             blink=self.blink,
             reverse=self.reverse,
             reset=self.reset,
             file=self.file,
             nl=self.nl,
             err=self.err,
             color=self.color,
         )
     else:
         click.secho(
             u'%s' % self.format_message(),
             fg=self.fg,
             bg=self.bg,
             bold=self.bold,
             dim=self.dim,
             underline=self.underline,
             blink=self.blink,
             reverse=self.reverse,
             reset=self.reset,
             file=self.file,
             nl=self.nl,
             err=self.err,
             color=self.color,
         )
Example #11
0
    def show(region, namespace):
        status = False
        if region and region not in REGIONS:
            raise ArgsException(
                "region {r} not exists ,please select from{R}".format(
                    r=region, R=REGIONS))
        if not region:
            region = UserConfig().region
            status = True

        rep = ScfClient(region).get_ns(namespace)
        if not rep:
            raise NamespaceException(
                "Region {r} not exist namespace {n}".format(r=region,
                                                            n=namespace))

        functions = ScfClient(region).list_function(namespace)
        if not functions:
            Operation("Region {r} namespace {n} not exist function".format(
                r=region, n=namespace)).warning()
            return

        Operation("Region:%s" % (region)).process()
        Operation("Namespace:%s " % (namespace)).process()
        Operation("%-20s %-15s %-20s %-20s %-60s" %
                  ("Runtime", "Status", "AddTime", "ModTime",
                   "FunctionName")).echo()
        for function in functions:
            Operation("%-20s %-24s %-20s %-20s %-60s" %
                      (function.Runtime, List.status(
                          function.Status), function.AddTime, function.ModTime,
                       function.FunctionName)).echo()
        if status:
            msg = "If you want to get a list of more functions, you can specify the region and namespace. Like: scf function list --region ap-shanghai --namespace default"
            Operation(msg).information()
Example #12
0
 def __init__(self):
     uc = UserConfig()
     self._cred = credential.Credential(secretId=uc.secret_id, secretKey=uc.secret_key)
     self._region = uc.region
     hp = HttpProfile(reqTimeout=ScfClient.CLOUD_API_REQ_TIMEOUT)
     cp = ClientProfile("TC3-HMAC-SHA256", hp)
     self._client = scf_client.ScfClient(self._cred, self._region, cp)
     self._client._sdkVersion = "TCFCLI"
Example #13
0
def set(**kwargs):
    def set_true(k):
        kwargs[k] = True

    uc = UserConfig()
    values = [v for k, v in kwargs.items()]
    if not reduce(lambda x, y: (bool(x) or bool(y)), values):
        list(map(set_true, kwargs))
        attrs = uc.get_attrs(kwargs)
        config = {}
        for attr in sorted(attrs):
            attr_value = attrs[attr]
            if attr == "secret-id":
                attr_value = "*" * 32 + attr_value[32:]
            elif attr == "secret-key":
                attr_value = "*" * 28 + attr_value[28:]
            v = click.prompt(text="TencentCloud {}({})".format(
                attr, attr_value),
                             default=attrs[attr],
                             show_default=False)
            config[attr] = v
        kwargs = config
    uc.set_attrs(kwargs)
    uc.flush()
    if not reduce(lambda x, y: (bool(x) or bool(y)), values):
        v = click.prompt(
            text="Allow report information to help us optimize scfcli(Y/n)",
            default="y",
            show_default=False)
        if v in ["y", "Y"]:
            client = ScfReportClient()
            client.report()
Example #14
0
    def do_cli(name, region, namespace, eventdata, logtype, invocationtype):
        if region and region not in REGIONS:
            raise ArgsException(
                "region {r} not exists ,please select from{R}".format(
                    r=region, R=REGIONS))
        if not region:
            region = UserConfig().region

        rep = ScfClient(region).get_ns(namespace)
        if not rep:
            raise NamespaceException(
                "Region {r} not exist namespace {n}".format(r=region,
                                                            n=namespace))

        functions = ScfClient(region).get_function(name, namespace)

        if not functions:
            Operation("Could not found this funtion").warning()
            Operation(
                "You could get function name by: scf function list").warning()
            Operation(
                "This command requires the --name option / -n shortcut. Usage: The function name"
            ).warning()
            return

        rep, invokeres = ScfClient(region).invoke_func(name, namespace,
                                                       eventdata,
                                                       invocationtype, logtype)

        if not rep:
            Operation("Invoke failed").warning()
            Operation(invokeres).exception()
            return
        else:
            invokeres = json.loads(invokeres)
            # print(invokeres)
            Operation('Invoke success\n\n'
                      'Response:%s\n\n'
                      'Output:\n%s\n\n'
                      'Summary:\n'
                      'FunctionRequestId:%s\n'
                      'Run Duration:%sms\n'
                      'Bill Duration:%sms\n'
                      'Usage memory:%sB\n\n' % (
                          invokeres['Result']['RetMsg'],
                          invokeres['Result']['Log'],
                          invokeres['Result']['FunctionRequestId'],
                          invokeres['Result']['Duration'],
                          invokeres['Result']['BillDuration'],
                          invokeres['Result']['MemUsage'],
                      )).success()

            if len(invokeres['Result']['Log']) > 4000:
                Operation(
                    'You could get more logs by: `scf logs -r %s -ns %s -n %s`'
                    % (region, namespace, name)).information()
Example #15
0
def get(**kwargs):
    uc = UserConfig()

    def set_true(k):
        kwargs[k] = True

    bools = [v for k, v in kwargs.items()]
    if not reduce(lambda x, y: bool(x or y), bools):
        list(map(set_true, kwargs))
    attrs = uc.get_attrs(kwargs)
    msg = "{} config:\n".format(UserConfig.API)
    for attr in sorted(attrs):
        attr_value = attrs[attr]
        if attr == "secret-id":
            attr_value = "*" * 32 + attr_value[32:]
        elif attr == "secret-key":
            attr_value = "*" * 28 + attr_value[28:]
        msg += "{} = {}\n".format(attr, attr_value)
    click.secho(msg.strip())
Example #16
0
    def check_params(self):
        if not self.template_file:
            raise TemplateNotFoundException("FAM Template Not Found. Missing option --template-file")
        if not os.path.isfile(self.template_file):
            raise TemplateNotFoundException("FAM Template Not Found, template-file Not Found")

        self.template_file = os.path.abspath(self.template_file)
        self.template_file_dir = os.path.dirname(os.path.abspath(self.template_file))

        uc = UserConfig()
        if self.cos_bucket and self.cos_bucket.endswith("-" + uc.appid):
            self.cos_bucket = self.cos_bucket.replace("-" + uc.appid, '')
Example #17
0
def daily_task():
    try:
        version = platform.python_version()

        if version >= '3':
            import urllib.request as openurl
        else:
            import urllib2 as openurl

        socket.setdefaulttimeout(1.2)
        ssl._create_default_https_context = ssl._create_unverified_context

        try:

            uc = UserConfig()

            now_time = time.strftime("%Y-%m-%d")  # day
            version_time = uc.version_time
            allow_report = uc.section_map[UserConfig.OTHERS]['allow_report']
            #print allow_report
            if now_time != version_time:
                url = "https://service-qgphxt7y-1253970226.gz.apigw.tencentcs.com/release/client_daily_task"
                post_data = None
                if allow_report.upper() == 'TRUE':
                    statistics = StatisticsConfigure()
                    statistics.read_data()
                    post_data = statistics.get_data()
                    statistics.delete_data()
                # print post_data
                if not post_data:
                    post_data = {}
                post_data["appid"] = uc.appid
                post_data["version"] = __version__
                post_data = json.dumps(post_data) if post_data else "{}"
                request = openurl.Request(
                    data=post_data.encode("utf-8")
                    if version >= '3' else post_data,
                    url=url) if version >= '3' else openurl.Request(
                        url, data=post_data)
                # print openurl.urlopen(request).read()
                response = json.loads(
                    json.loads(
                        openurl.urlopen(request).read().decode("utf-8")))
                release_version = response["version"]
                message = response["message"]
                release_version_list = release_version.split(".")
                local_version_list = __version__.split(".")
                for i in range(0, len(release_version_list)):
                    if int(release_version_list[i]) > int(
                            local_version_list[i]):
                        output_msg(__version__, release_version, message)
                        break
                uc.set_attrs({'version_time': now_time})
                uc.flush()
        except Exception as e:
            # print e
            pass

    except Exception as e:
        pass
Example #18
0
def deploy(template_file, cos_bucket, name, namespace, region, forced,
           skip_event, without_cos, history, update_event):
    '''
        \b
        Scf cli completes the function package deployment through the deploy subcommand. The scf command line tool deploys the code package, function configuration, and other information specified in the configuration file to the cloud or updates the functions of the cloud according to the specified function template configuration file.
        \b
        The execution of the scf deploy command is based on the function template configuration file. For the description and writing of the specific template file, please refer to the template file description.
            * https://cloud.tencent.com/document/product/583/33454
        \b
        Common usage:
            \b
            * Deploy the package
              $ scf deploy
            \b
            * Package the configuration file, and specify the COS bucket as "temp-code-1253970226"
              $ scf deploy --cos-bucket temp-code-1253970226
            \b
            * Deploy history package
              $ scf deploy --history
            \b
            * Upgrade the function and urgrade events
              $ scf deploy -f -ue
    '''

    if region and region not in REGIONS:
        raise ArgsException("The region must in %s." % (", ".join(REGIONS)))
    region = region if region else UserConfig().region
    package = Package(template_file, cos_bucket, name, region, namespace,
                      without_cos, history)
    resource = package.do_package()

    if resource == None:
        return

    if name and "'%s'" % str(name) not in str(resource):
        raise DeployException(
            "Couldn't find the function in YAML, please add this function in YAML."
        )
    else:
        deploy = Deploy(resource, namespace, region, forced, skip_event,
                        update_event)
        deploy.do_deploy()
        Operation("Deploy success").success()

        # delete package dir
        try:
            shutil.rmtree(_BUILD_DIR)
        except Exception as e:
            pass
    def __init__(self, region=None, period=60):
        uc = UserConfig()
        self._cred = credential.Credential(secretId=uc.secret_id,
                                           secretKey=uc.secret_key)
        if region is None:
            self._region = uc.region
        else:
            self._region = region

        self.period = period
        hp = HttpProfile(reqTimeout=MonitorClient.CLOUD_API_REQ_TIMEOUT)
        cp = ClientProfile("TC3-HMAC-SHA256", hp)
        self._client = monitor_client.MonitorClient(self._cred, self._region,
                                                    cp)
        self._client._sdkVersion = "TCFCLI"
Example #20
0
def change(userid):
    '''
        \b
        Change your user.
        \b
        Common usage:
            \b
            * Configure change your user
              $ scf configure change
    '''

    uc = UserConfig()

    userlist = uc.get_all_user()
    userlist = sorted(userlist)

    # 如果没有传入编号,则进入交互
    if not userid:
        curruser = uc.section_map[UserConfig.OTHERS]['curr_user']
        Operation('Your current user is %s' % curruser).process()
        Operation('%-10s %-15s %-15s %-15s %-15s %-10s' %
                  ('UserId', 'AppId', 'region', 'secret_id', 'secret_key',
                   'using_cos')).process()
        for user in userlist:
            userinfo = uc.get_user_info(user)
            secret_id = ("*" * 3 + userinfo['secret_id'][32:]
                         ) if userinfo['secret_id'] != 'None' else 'None'
            secret_key = ("*" * 3 + userinfo['secret_key'][28:]
                          ) if userinfo['secret_key'] != 'None' else 'None'
            Operation(
                '%-10s %-15s %-15s %-15s %-15s %-10s' %
                (user.strip('USER_'), userinfo['appid'], userinfo['region'],
                 secret_id, secret_key, userinfo['using_cos'][:5])).process()

        v = click.prompt(text="Please choice UserId to change",
                         show_default=False)

    v = userid if userid else v
    if ('USER_' + v) in userlist:
        uc.changeuser(('USER_' + v))
        uc.flush()
        Operation('Your current user has switched to %s' %
                  ('USER_' + v)).success()
    else:
        Operation('error No').warning()
Example #21
0
def check_version():
    try:
        version = platform.python_version()

        if version >= '3':
            import urllib.request
        else:
            import urllib2

        socket.setdefaulttimeout(1)
        ssl._create_default_https_context = ssl._create_unverified_context

        try:

            uc = UserConfig()

            # this_time = time.strftime("%W") # week
            this_time = time.strftime("%Y-%m-%d")  # day
            that_time = uc.version_time

            if this_time != that_time:
                url = "https://github.com/tencentyun/scfcli/blob/master/tcfcli/cmds/cli/__init__.py"
                temp_data = urllib.request.urlopen(
                    url) if version >= '3' else urllib2.urlopen(url)
                temp_data = temp_data.read().decode("utf-8")
                regex_str = '<span class="pl-pds">&#39;</span>(.*?)<span class="pl-pds">&#39;</span></span>'
                r_version = re.findall(regex_str, temp_data)[0]
                version_list = __version__.split(".")
                r_version_list = r_version.split(".")
                for i in range(0, len(version_list)):
                    if r_version_list[i] > version_list[i]:
                        uc.set_attrs({'version_time': this_time})
                        uc.flush()
                        click.secho(
                            click.style(
                                """    ----------------------------------------------------
    |                  Upgrade reminder                |
    | Latest version:%s   ,  Your version:   %s |
    | If you want to upgrade, you can use the command: |
    |""" % (r_version, __version__),
                                fg="green") +
                            click.style(
                                "                 pip install -U scf               ",
                                fg="yellow") + click.style('''|
    ----------------------------------------------------''',
                                                           fg="green"))
                        break
        except Exception as e:
            pass
    except Exception as e:
        pass
Example #22
0
    def update_event_data(region, namespace, name, eventdatalist, forced):
        region = region if region else UserConfig().region

        if region not in REGIONS:
            raise ArgsException(
                "region {r} not exists ,please select from{R}".format(
                    r=region, R=REGIONS))

        if not ScfClient(region).get_ns(namespace):
            raise NamespaceException(
                "Region {r} not exist namespace {n}".format(r=region,
                                                            n=namespace))

        if not ScfClient(region).get_function(function_name=name,
                                              namespace=namespace):
            raise FunctionNotFound(
                "Region {r} namespace {n} not exists function {f}".format(
                    r=region, n=namespace, f=name))

        Operation("Region:%s" % (region)).process()
        Operation("Namespace:%s " % (namespace)).process()
        Operation("Function:%s " % (name)).process()

        flag = False
        for eventdata in eventdatalist:
            if Update.do_deploy_testmodel(functionName=name,
                                          namespace=namespace,
                                          region=region,
                                          forced=forced,
                                          event=list(eventdata.values())[0],
                                          event_name=list(
                                              eventdata.keys())[0]):
                flag = True
        if flag:
            Operation(
                "If you want to cover remote eventdata.You can use command with option -f"
            ).exception()
Example #23
0
def check_version():
    try:
        version = platform.python_version()

        if version >= '3':
            import urllib.request
        else:
            import urllib2

        socket.setdefaulttimeout(1)
        ssl._create_default_https_context = ssl._create_unverified_context

        try:

            uc = UserConfig()

            # this_time = time.strftime("%W") # week
            this_time = time.strftime("%Y-%m-%d")  # day
            that_time = uc.version_time

            if this_time != that_time:
                url = "https://service-qgphxt7y-1253970226.gz.apigw.tencentcs.com/test/check_version"
                temp_data = urllib.request.urlopen(url) if version >= '3' else urllib2.urlopen(url)
                r_version = temp_data.read().decode("utf-8")[1:-1]
                print(r_version)
                r_version_list = r_version.split(".")
                version_list = __version__.split(".")
                for i in range(0, len(version_list)):
                    if int(r_version_list[i]) > int(version_list[i]):
                        click.secho(click.style("""    ----------------------------------------------------
    |                  Upgrade reminder                |
    | Latest version:%7s  , Your version: %7s |
    | If you want to upgrade, you can use the command: |
    |""" % (r_version, __version__), fg="green") +
                                    click.style("                 pip install -U scf               ", fg="yellow") +
                                    click.style('''|
    ----------------------------------------------------''', fg="green"))
                        break
                uc.set_attrs({'version_time': this_time})
                uc.flush()
        except Exception as e:
            pass
    except Exception as e:
        pass
 def style(self):
     if "--no-color" in sys.argv or "-nc" in sys.argv or UserConfig(
     ).section_map[UserConfig.OTHERS]['no_color'].startswith('True'):
         return click.style(
             u'%s' % self.format_message(),
             bold=self.bold,
             dim=self.dim,
             underline=self.underline,
             blink=self.blink,
             reverse=self.reverse,
             reset=self.reset,
         )
     else:
         return click.style(
             u'%s' % self.format_message(),
             fg=self.fg,
             bg=self.bg,
             bold=self.bold,
             dim=self.dim,
             underline=self.underline,
             blink=self.blink,
             reverse=self.reverse,
             reset=self.reset,
         )
 def new_style(self, msg, bg=None, fg=None):
     if "--no-color" in sys.argv or "-nc" in sys.argv or UserConfig(
     ).section_map[UserConfig.OTHERS]['no_color'].upper() == 'TRUE':
         return click.style(u'%s' % msg)
     else:
         return click.style(u'%s' % msg, bg=bg, fg=fg)
Example #26
0
    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
Example #27
0
    def do_cli(region, namespace, name, event, output_dir, forced):
        Get.checkpath(output_dir)
        if region and region not in REGIONS:
            raise ArgsException(
                "region {r} not exists ,please select from{R}".format(
                    r=region, R=REGIONS))
        if not region:
            region = UserConfig().region

        rep = ScfClient(region).get_ns(namespace)
        if not rep:
            raise NamespaceException(
                "Region {r} not exist namespace {n}".format(r=region,
                                                            n=namespace))

        functions = ScfClient(region).get_function(function_name=name,
                                                   namespace=namespace)
        if not functions:
            raise FunctionNotFound(
                "Region {r} namespace {n} not exist function {f}".format(
                    r=region, n=namespace, f=name))

        testmodelvaluelist = {}
        if event:
            testmodelvalue = ScfClient(region).get_func_testmodel(
                functionName=name, namespace=namespace, testModelName=event)
            if not testmodelvalue:
                raise NamespaceException(
                    "This function {f} not exist event {e} ".format(f=name,
                                                                    e=event))
            testmodelvaluelist[event] = testmodelvalue
        else:
            for testmodel in ScfClient(region).list_func_testmodel(
                    functionName=name, namespace=namespace):
                testmodelvalue = ScfClient(region).get_func_testmodel(
                    functionName=name,
                    namespace=namespace,
                    testModelName=testmodel)
                testmodelvaluelist[testmodel] = testmodelvalue

        flag = False
        for testmodel in testmodelvaluelist:
            testmodelfilename = testmodel + '.json'
            testmodelfilepath = os.path.join(output_dir, testmodelfilename)
            if os.path.exists(testmodelfilepath) and os.path.isfile(
                    testmodelfilepath) and not forced:
                Operation('Event-data: {%s} exists in local.' %
                          (testmodelfilepath)).exception()
                flag = True
                continue
            try:
                with open(testmodelfilepath, 'w') as f:
                    Operation('Downloading event-data: {%s} ...' %
                              (testmodel)).process()
                    f.write(testmodelvaluelist[testmodel]['TestModelValue'])
                    Operation('Download event-data: {%s} success' %
                              (testmodel)).success()
            except:
                Operation('Download event-data: {%s} failed' %
                          (testmodel)).exception()
        if flag:
            Operation(
                'If you want to cover local eventdata.You can use commond with option -f'
            ).exception()
Example #28
0
def deploy(template_file, cos_bucket, name, namespace, region, forced, skip_event, without_cos, history, update_event,
           no_color):
    '''
        \b
        Scf cli completes the function package deployment through the deploy subcommand. The scf command line tool deploys the code package, function configuration, and other information specified in the configuration file to the cloud or updates the functions of the cloud according to the specified function template configuration file.
        \b
        The execution of the scf deploy command is based on the function template configuration file. For the description and writing of the specific template file, please refer to the template file description.
            * https://cloud.tencent.com/document/product/583/33454
        \b
        Common usage:
            \b
            * Deploy the package
              $ scf deploy
            \b
            * Package the configuration file, and specify the COS bucket as "temp-code-1253970226"
              $ scf deploy --cos-bucket temp-code-1253970226
            \b
            * Deploy history package
              $ scf deploy --history
            \b
            * Upgrade the function and urgrade events
              $ scf deploy -f -ue
    '''

    # time1 = time.time()
    if region and region not in REGIONS:
        raise ArgsException("The region must in %s." % (", ".join(REGIONS)))
    region = region if region else UserConfig().region

    # package
    Operation("Package begin").begin()

    package = Package(template_file, cos_bucket, name, region, namespace, without_cos, history)
    package_result, resource = package.do_package()

    try:
        Operation("Package result:").process()
        Operation("  Success: %d" % (len(package_result["success"]))).out_infor()
        for eve_success in package_result["success"]:
            Operation("    Namespace: %s\tFunction: %s" % (eve_success[0], eve_success[1])).out_infor()
        Operation("  Faild: %d" % (len(package_result["faild"]))).out_infor()
        for eve_success in package_result["faild"]:
            Operation("    Namespace: %s\tFunction: %s" % (eve_success[0], eve_success[1])).out_infor()
    except Exception as e:
        # print(e)
        pass

    if resource == None or len(package_result["success"]) == 0:
        raise PackageException("The number of deployable packages is 0, and the deployment is terminated.")

    Operation("Package end").success()

    # deploy
    if name and "'%s'" % str(name) not in str(resource):
        raise DeployException("Couldn't find the function in YAML, please add this function in YAML.")
    else:
        Operation("Deploy begin").begin()
        deploy = Deploy(resource, namespace, region, forced, skip_event, update_event)
        deploy.do_deploy()
        Operation("Deploy end").success()

        # delete package dir
        try:
            shutil.rmtree(_BUILD_DIR)
        except Exception as e:
            pass
Example #29
0
def set(**kwargs):
    '''
        \b
        Configure your account parameters.
        \b
        Common usage:
            \b
            * Configure your account parameters
              $ scf configure set
            \b
            * Modify a configuration item
              $ scf configure set --region ap-shanghai
    '''
    def set_true(k):
        kwargs[k] = True

    uc = UserConfig()

    using_cos_true = "True (By default, it is deployed by COS.)"
    using_cos_false = "False (By default, it isn't deployed by COS.)"

    if "region" in kwargs and kwargs["region"]:
        if kwargs["region"] not in REGIONS:
            Operation("The region must in %s." %
                      (", ".join(REGIONS))).warning()
            kwargs["region"] = uc.region
            return

    if "using_cos" in kwargs and kwargs["using_cos"]:
        kwargs["using_cos"] = using_cos_false if kwargs["using_cos"] not in [
            "y", "Y"
        ] else using_cos_true

    if "allow_report" in kwargs and kwargs["allow_report"]:
        kwargs["allow_report"] = 'False' if kwargs["allow_report"] not in [
            "y", "Y"
        ] else 'True'
        if kwargs["allow_report"] == "True":
            Operation(
                '当前已开启数据收集,详情请参考 https://cloud.tencent.com/document/product/583/37766'
            ).out_infor()

    if "no_color" in kwargs and kwargs["no_color"]:
        kwargs["no_color"] = 'False' if kwargs["no_color"] not in [
            "y", "Y"
        ] else 'True'

    values = [v for k, v in kwargs.items()]
    config = {}
    if not reduce(lambda x, y: (bool(x) or bool(y)), values):
        list(map(set_true, kwargs))
        attrs = uc.get_attrs(kwargs)
        skip_attr = {
            'using_cos', 'python2_path', 'python3_path', 'no_color',
            'allow_report'
        }
        for attr in sorted(attrs):
            if attr not in skip_attr:
                while True:
                    attr_value = attrs[attr]
                    if attr == "secret_id":
                        attr_value = "*" * 32 + attr_value[32:]
                    elif attr == "secret_key":
                        attr_value = "*" * 28 + attr_value[28:]

                    v = click.prompt(text="TencentCloud {}({})".format(
                        attr.replace('_', '-'), attr_value),
                                     default=attrs[attr],
                                     show_default=False)
                    config[attr] = v

                    if attr != "region":
                        break
                    else:
                        if v in REGIONS:
                            break
                        else:
                            Operation("The region must in %s." %
                                      (", ".join(REGIONS))).warning()

        #
        v1 = click.prompt(
            text=("Show the command information without color(cur:%s). (y/n)")
            % attrs["no_color"][:5],
            default="y"
            if str(attrs["no_color"]).upper().startswith("TRUE") else "n",
            show_default=False)
        if v1:
            config["no_color"] = "False" if v1 not in ["y", "Y"] else "True"
        else:
            config["no_color"] = attrs["no_color"]

        v2 = click.prompt(text=(
            "Deploy SCF function by COS, it will be faster(cur:%s).  (y/n)") %
                          attrs["using_cos"][:5],
                          default="y"
                          if str(attrs["using_cos"]).upper().startswith("TRUE")
                          else "n",
                          show_default=False)
        if v2:
            config["using_cos"] = using_cos_false if v2 not in [
                "y", "Y"
            ] else using_cos_true
        else:
            config["using_cos"] = attrs["using_cos"]

        #v3 = click.prompt(text=("Allow report information to help us optimize scfcli(cur:%s). (y/n)") % attrs["allow_report"][:5],
        #                  default="y" if str(attrs["allow_report"]).upper().startswith("TRUE") else "n",
        #                  show_default=False)
        #if v3:
        #    config["allow_report"] = "False" if v3 not in ["y", "Y"] else "True"
        #else:
        #    config["allow_report"] = attrs["allow_report"]

        # if uc.section_map[UserConfig.OTHERS]['allow_report'].upper() == 'TRUE':
        #     Operation('当前已开启数据收集,详情请参考 https://cloud.tencent.com/document/product/583/37766').out_infor()

        kwargs = config

    uc.set_attrs(kwargs)
    uc.flush()
Example #30
0
def set(**kwargs):
    '''
        \b
        Configure your account parameters.
        \b
        Common usage:
            \b
            * Configure your account parameters
              $ scf configure set
            \b
            * Modify a configuration item
              $ scf configure set --region ap-shanghai
    '''
    def set_true(k):
        kwargs[k] = True

    uc = UserConfig()

    using_cos_true = "False (By default, it isn't deployed by COS.)"
    using_cos_false = "True (By default, it is deployed by COS.)"

    if "region" in kwargs and kwargs["region"]:
        if kwargs["region"] not in REGIONS:
            Operation("The region must in %s." %
                      (", ".join(REGIONS))).warning()
            kwargs["region"] = uc.region
            return

    if "using_cos" in kwargs and kwargs["using_cos"]:
        kwargs["using_cos"] = using_cos_true if kwargs["using_cos"] not in [
            "y", "Y"
        ] else using_cos_false

    values = [v for k, v in kwargs.items()]
    if not reduce(lambda x, y: (bool(x) or bool(y)), values):
        list(map(set_true, kwargs))
        attrs = uc.get_attrs(kwargs)
        config = {}
        for attr in sorted(attrs):
            if attr != "using-cos":
                while True:
                    attr_value = attrs[attr]
                    if attr == "secret-id":
                        attr_value = "*" * 32 + attr_value[32:]
                    elif attr == "secret-key":
                        attr_value = "*" * 28 + attr_value[28:]

                    v = click.prompt(text="TencentCloud {}({})".format(
                        attr, attr_value),
                                     default=attrs[attr],
                                     show_default=False)
                    config[attr] = v

                    if attr != "region":
                        break
                    else:
                        if v in REGIONS:
                            break
                        else:
                            Operation("The region must in %s." %
                                      (", ".join(REGIONS))).warning()

        v = click.prompt(
            text="Deploy SCF function by COS, it will be faster. (y/n)",
            default="y" if str(attrs["using-cos"]).startswith("True") else "n",
            show_default=False)

        if v:
            config["using_cos"] = using_cos_true if v not in [
                "y", "Y"
            ] else using_cos_false
        else:
            config["using_cos"] = attrs["using-cos"]

        kwargs = config

    uc.set_attrs(kwargs)
    uc.flush()

    if not reduce(lambda x, y: (bool(x) or bool(y)), values):
        v = click.prompt(
            text="Allow report information to help us optimize scfcli. (y/n)",
            default="y",
            show_default=False)
        if v in ["y", "Y"]:
            client = ScfReportClient()
            client.report()