Exemple #1
0
    def _do_deploy_core(self, func, func_name, func_ns, region, forced, skip_event=False):
        # check namespace exit, create namespace
        if self.namespace and self.namespace != func_ns:
            func_ns = self.namespace

        rep = ScfClient(region).get_ns(func_ns)
        if not rep:
            click.secho("{ns} not exists, create it now".format(ns=func_ns), fg="red")
            err = ScfClient(region).create_ns(func_ns)
            if err is not None:
                if sys.version_info[0] == 3:
                    s = err.get_message()
                else:
                    s = err.get_message().encode("UTF-8")
                click.secho("Create namespace '{name}' failure. Error: {e}.".format(
                    name=func_ns, e=s), fg="red")
                sys.exit(1)

        err = ScfClient(region).deploy_func(func, func_name, func_ns, forced)
        if err is not None:
            if sys.version_info[0] == 3:
                s = err.get_message()
            else:
                s = err.get_message().encode("UTF-8")
            
            err_msg = "Deploy function '{name}' failure, {e}.".format(name=func_name, e=s)

            if err.get_request_id():
                err_msg += ("\nRequestId: {}" .format(err.get_request_id().encode("UTF-8")))
            raise CloudAPIException(err_msg.decode("UTF-8"))
            

        click.secho("Deploy function '{name}' success".format(name=func_name), fg="green")
        if not skip_event:
            self._do_deploy_trigger(func, func_name, func_ns, region)
Exemple #2
0
 def _do_deploy_trigger(self, func, func_name, func_ns, region=None):
     proper = func.get(tsmacro.Properties, {})
     events = proper.get(tsmacro.Events, {})
     hasError = None
     for trigger in events:
         err = ScfClient(region).deploy_trigger(events[trigger], trigger,
                                                func_name, func_ns)
         if err is not None:
             hasError = err
             if sys.version_info[0] == 3:
                 s = err.get_message()
             else:
                 s = err.get_message().encode("UTF-8")
             if "Param error The path+method already exists under the service" not in str(
                     s):
                 if err.get_request_id():
                     Operation(
                         "Deploy trigger '{name}' failure. Error: {e}. RequestId: {id}"
                         .format(name=trigger, e=s,
                                 id=err.get_request_id())).warning()
                 else:
                     Operation(
                         "Deploy trigger '{name}' failure. Error: {e}.".
                         format(
                             name=trigger,
                             e=s,
                         )).warning()
             continue
         Operation("Deploy trigger '{name}' success".format(
             name=trigger)).success()
Exemple #3
0
    def _do_deploy_trigger(self, func, func_name, func_ns, region=None):
        proper = func.get(tsmacro.Properties, {})
        events = proper.get(tsmacro.Events, {})
        hasError = None
        for trigger in events:
            err = ScfClient(region).deploy_trigger(events[trigger], trigger,
                                                   func_name, func_ns)
            if err is not None:
                hasError = err
                if sys.version_info[0] == 3:
                    s = err.get_message()
                else:
                    s = err.get_message().encode("UTF-8")

                click.secho(
                    "Deploy trigger '{name}' failure. Error: {e}.".format(
                        name=trigger, e=s),
                    fg="red")
                if err.get_request_id():
                    click.secho("RequestId: {}".format(
                        err.get_request_id().encode("UTF-8")),
                                fg="red")
                continue
            click.secho("Deploy trigger '{name}' success".format(name=trigger),
                        fg="green")
        if hasError is not None:
            sys.exit(1)
Exemple #4
0
    def _do_deploy_core(self, func, func_name, func_ns, forced):
        err = ScfClient().deploy_func(func, func_name, func_ns, forced)
        if err is not None:
            if sys.version_info[0] == 3:
                s = err.get_message()
            else:
                s = err.get_message().encode("UTF-8")
            click.secho("Deploy function '{name}' failure. Error: {e}.".format(name=func_name,
                                                            e=s), fg="red")
            if err.get_request_id():
                click.secho("RequestId: {}".format(err.get_request_id().encode("UTF-8")), fg="red")
            return

        click.secho("Deploy function '{name}' success".format(name=func_name), fg="green")
        self._do_deploy_trigger(func, func_name, func_ns)
Exemple #5
0
    def _do_deploy_core(self,
                        func,
                        func_name,
                        func_ns,
                        region,
                        forced,
                        skip_event=False):
        # check namespace exit, create namespace
        if self.namespace and self.namespace != func_ns:
            func_ns = self.namespace

        rep = ScfClient(region).get_ns(func_ns)
        if not rep:
            Operation(
                "{ns} not exists, create it now".format(ns=func_ns)).process()
            err = ScfClient(region).create_ns(func_ns)
            if err is not None:
                if sys.version_info[0] == 3:
                    s = err.get_message()
                else:
                    s = err.get_message().encode("UTF-8")
                raise NamespaceException(
                    "Create namespace '{name}' failure. Error: {e}.".format(
                        name=func_ns, e=s))

        err = ScfClient(region).deploy_func(func, func_name, func_ns, forced)
        if err is not None:
            # if sys.version_info[0] == 3:
            s = err.get_message()
            # else:
            #    s = err.get_message().encode("UTF-8")
            if sys.version_info[0] == 2 and isinstance(s, str):
                s = s.encode("utf8")
            err_msg = u"Deploy function '{name}' failure, {e}.".format(
                name=func_name, e=s)

            if err.get_request_id():
                err_msg += (u" RequestId: {}".format(err.get_request_id()))
            raise CloudAPIException(err_msg)

        Operation("Deploy function '{name}' success".format(
            name=func_name)).success()
        if not skip_event:
            self._do_deploy_trigger(func, func_name, func_ns, region)
Exemple #6
0
    def _do_deploy_trigger(self, func, func_name, func_ns):
        events = getattr(func, "Events", None)
        if events is None:
            return
        for name, trigger in vars(events).items():
            err = ScfClient().deploy_trigger(trigger, name, func_name, func_ns)
            if err is not None:
                if sys.version_info[0] == 3:
                    s = err.get_message()
                else:
                    s = err.get_message().encode("UTF-8")

                click.secho(
                    "Deploy trigger '{name}' failure. Error: {e}.".format(name=name,
                                                            e=s), fg="red")
                if err.get_request_id():
                    click.secho("RequestId: {}".format(err.get_request_id().encode("UTF-8")), fg="red")
                continue
            click.secho("Deploy trigger '{name}' success".format(name=name),fg="green")
Exemple #7
0
    def do_eve_trigger(self, region, events, trigger, func_name, func_ns):
        err = ScfClient(region).deploy_trigger(events[trigger], trigger, func_name, func_ns)
        if err is not None:
            if sys.version_info[0] == 3:
                s = err.get_message()
            else:
                s = err.get_message().encode("UTF-8")

            if err.get_request_id():
                Operation("Deploy {namespace} {function} trigger '{name}' failure. Error: {e}. RequestId: {id}".
                          format(namespace=func_ns, function=func_name, name=trigger, e=s,
                                 id=err.get_request_id())).warning()
            else:
                msg = "Deploy {namespace} {function} trigger '{name}' failure. Error: {e}.".format(namespace=func_ns,
                                                                                                   function=func_name,
                                                                                                   name=trigger, e=s, )
                Operation(msg).warning()
        else:
            Operation(
                "Deploy {namespace} {function} trigger '{name}' success".format(namespace=func_ns, function=func_name,
                                                                                name=trigger)).success()
Exemple #8
0
    def _do_deploy_core(self, func, func_name, func_ns, region, forced, success_list, faild_list, skip_event=False):
        # check role exit
        try:
            role = func.get(tsmacro.Properties, {}).get(tsmacro.Role)
            if role:
                rolelist = list_scf_role(region)
                if rolelist == None:
                    Operation("Get Role list error").warning()
                    func[tsmacro.Properties][tsmacro.Role] = None
                elif role not in rolelist:
                    Operation("%s not exists in remote scf role list" % (role)).warning()
                    if len(rolelist):
                        Operation("You can choose from %s " % (str(rolelist))).warning()
                    func[tsmacro.Properties][tsmacro.Role] = None
            # check namespace exit, create namespace
            if self.namespace and self.namespace != func_ns:
                func_ns = self.namespace

            function_data = Function(region, func_ns, func_name, self.resources).get_function_trigger()
            trigger_release = None
            if function_data:
                now_runtime = function_data[0]
                trigger_release = function_data[1]

                if func['Properties']['Runtime'] != now_runtime:
                    err_msg = "RUNTIME in YAML does not match RUNTIME on the RELEASE (release: %s)" % now_runtime
                    echo(
                        Operation("[x]", bg="red").style() + Operation(u' %s' % text(err_msg), fg="red").style(),
                        file=get_text_stderr()
                    )
                    exit(1)
                    # raise DeployException(err_msg)

            rep = ScfClient(region).get_ns(func_ns)
            if not rep:
                Operation("{ns} not exists, create it now".format(ns=func_ns)).process()
                err = ScfClient(region).create_ns(func_ns)
                if err is not None:
                    if sys.version_info[0] == 3:
                        s = err.get_message()
                    else:
                        s = err.get_message().encode("UTF-8")
                    err_msg = "Create namespace '{name}' failure. Error: {e}.".format(name=func_ns, e=s)
                    echo(
                        Operation("[x]", bg="red").style() + Operation(u' %s' % text(err_msg), fg="red").style(),
                        file=get_text_stderr()
                    )
                    exit(1)
                    # raise NamespaceException()

            deploy_result = ScfClient(region).deploy_func(func, func_name, func_ns, forced)

            if deploy_result == 1:
                Operation("{ns} {name} already exists, update it now".format(ns=func_ns, name=func_name)).process()
                deploy_result = ScfClient(region).update_config(func, func_name, func_ns)
                if deploy_result == True:
                    deploy_result = ScfClient(region).update_code(func, func_name, func_ns)
                    deploy_result = 0 if deploy_result == True else deploy_result

            if deploy_result == 0:
                Operation("Deploy function '{name}' success".format(name=func_name)).success()
                if not skip_event:
                    self._do_deploy_trigger(func, func_name, func_ns, region, trigger_release)
                success_list.append(func_name)
                return

            elif deploy_result == 2:
                faild_list.append(func_name)
                Operation(
                    "%s %s: You can add -f to update the function when it already exists. Example : scf deploy -f" % (
                        func_ns, func_name)).warning()
                err_msg = "%s %s: The function already exists." % (func_ns, func_name)
                echo(
                    Operation("[x]", bg="red").style() + Operation(u' %s' % text(err_msg), fg="red").style(),
                    file=get_text_stderr()
                )

                exit(1)
                # raise CloudAPIException("The function already exists.")

            if deploy_result != None:
                faild_list.append(func_name)
                err = deploy_result
                s = err.get_message()
                if sys.version_info[0] == 2 and isinstance(s, str):
                    s = s.encode("utf8")
                err_msg = u"Deploy function '{name}' failure, {e}.".format(name=func_name, e=s)

                if err.get_request_id():
                    err_msg += (u" RequestId: {}".format(err.get_request_id()))
                echo(
                    Operation("[x]", bg="red").style() + Operation(u' %s' % text(err_msg), fg="red").style(),
                    file=get_text_stderr()
                )
                exit(1)
                # raise CloudAPIException(err_msg)
        except Exception as e:
            faild_list.append(func_name)
            echo(
                Operation("[x]", bg="red").style() + Operation(u' %s' % text(str(e)), fg="red").style(),
                file=get_text_stderr()
            )
            exit(1)
Exemple #9
0
    def _do_deploy_trigger(self,
                           func,
                           func_name,
                           func_ns,
                           region=None,
                           trigger_release=None):

        proper = func.get(tsmacro.Properties, {})
        events = proper.get(tsmacro.Events, {})
        hasError = None

        for trigger in events:
            trigger_status = True
            msg = "Deploy %s trigger '%s' failure, this trigger has been created." % (
                events[trigger]['Type'], trigger)
            if trigger_release:
                try:
                    event_type = str(events[trigger]['Type']).lower()
                    temp_trigger = events[trigger].copy()

                    if event_type == "timer":
                        temp_trigger['TriggerName'] = trigger

                    for eve_event in trigger_release[str(
                            events[trigger]['Type']).lower()]:
                        eve_event_infor = eve_event.copy()
                        eve_event_infor.pop("TriggerDesc", eve_event_infor)
                        change_infor = False
                        tproperty = temp_trigger['Properties']
                        eproperty = eve_event['Properties']

                        if event_type == "timer":
                            if temp_trigger['TriggerName'] == eve_event[
                                    'TriggerName']:
                                change_infor = True
                        elif event_type == "apigw":
                            if tproperty['ServiceId'] == eproperty[
                                    'ServiceId'] and tproperty[
                                        'StageName'] == eproperty[
                                            'StageName'] and tproperty[
                                                'HttpMethod'] == eproperty[
                                                    'HttpMethod']:
                                eve_event_infor.pop("TriggerName")
                                change_infor = True
                        elif event_type == "ckafka":
                            if tproperty['Name'] + "-" + eproperty[
                                    'Topic'] == tproperty[
                                        'Name'] + "-" + eproperty['Topic']:
                                eve_event_infor.pop("TriggerName")
                                change_infor = True
                        elif event_type == "cmq":
                            if tproperty['Name'] == eproperty['Name']:
                                eve_event_infor.pop("TriggerName")
                                change_infor = True
                        elif event_type == "cos":
                            if tproperty['Bucket'] == eproperty[
                                    'Bucket'] and tproperty[
                                        'Events'] == eproperty[
                                            'Events'] and tproperty[
                                                'Filter'] == eproperty[
                                                    'Filter']:
                                eve_event_infor.pop("TriggerName")
                                change_infor = True

                        if change_infor:
                            if temp_trigger == eve_event_infor:
                                trigger_status = False
                                Operation(msg).warning()

                            else:
                                if self.update_event:
                                    self.trigger_upgrade_message(
                                        temp_trigger, eve_event_infor)
                                    eve_event["Properties"].pop(
                                        "Enable", eve_event["Properties"])
                                    err = ScfClient(region).remove_trigger(
                                        eve_event, func_name, func_ns)
                                    if not err:
                                        Operation(
                                            "The trigger is being redeployed."
                                        ).warning()
                                    else:
                                        trigger_status = False
                                        err_msg = "The redeployment trigger failed. Please manually delete the trigger and redeploy."
                                        Operation(err_msg).warning()
                                else:
                                    trigger_status = False
                                    self.trigger_upgrade_message(
                                        temp_trigger, eve_event_infor)
                            break
                except Exception as e:
                    print(e)
                    pass

                # if temp_trigger in trigger_release[str(events[trigger]['Type']).lower()]:
                #     trigger_status = False
                #     Operation(msg).warning()

            if trigger_status == True:
                err = ScfClient(region).deploy_trigger(events[trigger],
                                                       trigger, func_name,
                                                       func_ns)
                if err is not None:
                    hasError = err
                    if sys.version_info[0] == 3:
                        s = err.get_message()
                    else:
                        s = err.get_message().encode("UTF-8")

                    if err.get_request_id():
                        Operation(
                            "Deploy trigger '{name}' failure. Error: {e}. RequestId: {id}"
                            .format(name=trigger, e=s,
                                    id=err.get_request_id())).warning()
                    else:
                        msg = "Deploy trigger '{name}' failure. Error: {e}.".format(
                            name=trigger,
                            e=s,
                        )
                        Operation(msg).warning()
                Operation("Deploy trigger '{name}' success".format(
                    name=trigger)).success()
Exemple #10
0
    def _do_deploy_core(self,
                        func,
                        func_name,
                        func_ns,
                        region,
                        forced,
                        skip_event=False):
        # check namespace exit, create namespace
        if self.namespace and self.namespace != func_ns:
            func_ns = self.namespace

        function_data = Function(region, func_ns, func_name,
                                 self.resources).get_function_trigger()
        trigger_release = None
        if function_data:
            now_runtime = function_data[0]
            trigger_release = function_data[1]

            if func['Properties']['Runtime'] != now_runtime:
                err_msg = "RUNTIME in YAML does not match RUNTIME on the RELEASE (release: %s)" % now_runtime
                raise DeployException(err_msg)

        rep = ScfClient(region).get_ns(func_ns)
        if not rep:
            Operation(
                "{ns} not exists, create it now".format(ns=func_ns)).process()
            err = ScfClient(region).create_ns(func_ns)
            if err is not None:
                if sys.version_info[0] == 3:
                    s = err.get_message()
                else:
                    s = err.get_message().encode("UTF-8")
                raise NamespaceException(
                    "Create namespace '{name}' failure. Error: {e}.".format(
                        name=func_ns, e=s))

        deploy_result = ScfClient(region).deploy_func(func, func_name, func_ns,
                                                      forced)

        if deploy_result == 1:
            Operation("{ns} {name} already exists, update it now".format(
                ns=func_ns, name=func_name)).process()
            deploy_result = ScfClient(region).update_config(
                func, func_name, func_ns)
            if deploy_result == True:
                deploy_result = ScfClient(region).update_code(
                    func, func_name, func_ns)
                deploy_result = 0 if deploy_result == True else deploy_result

        if deploy_result == 0:
            Operation("Deploy function '{name}' success".format(
                name=func_name)).success()
            if not skip_event:
                self._do_deploy_trigger(func, func_name, func_ns, region,
                                        trigger_release)
            return

        elif deploy_result == 2:
            Operation(
                "You can add -f to update the function when it already exists. Example : scf deploy -f"
            ).warning()
            raise CloudAPIException("The function already exists.")

        if deploy_result != None:
            err = deploy_result
            s = err.get_message()
            if sys.version_info[0] == 2 and isinstance(s, str):
                s = s.encode("utf8")
            err_msg = u"Deploy function '{name}' failure, {e}.".format(
                name=func_name, e=s)

            if err.get_request_id():
                err_msg += (u" RequestId: {}".format(err.get_request_id()))

            raise CloudAPIException(err_msg)