Example #1
0
    def __enter__(self):
        template_dict = Resources(
            Template.get_template_data(self._template_file)).to_json()
        resource = template_dict.get(model.RESOURCE, {})
        ns = None
        if self.namespace is not None:
            ns = resource.get(self.namespace, {})
        else:
            nss = list(resource.keys())
            if len(nss) == 1:
                ns = resource.get(nss[0], None)
        if not ns:
            raise InvokeContextException(
                "You must provide a namespace identifier,default is 'default'")
        if model.TYPE in ns:
            del ns[model.TYPE]
        self._template_dict = {model.RESOURCE: ns}
        self._function_provider = ScfFunctionProvider(
            template_dict=self._template_dict)
        self._env_vars = self._get_env_vars(self._env_vars_file)
        self._log_file_fp = self._get_log_file(self._log_file)
        self._debug_context = self._get_debug_context(self._debug_port,
                                                      self._debug_args,
                                                      self._debugger_path)

        self._check_docker()

        return self
Example #2
0
 def __init__(self, template_file, cos_bucket, output_template_file):
     self.template_file = template_file
     self.cos_bucket = cos_bucket
     self.output_template_file = output_template_file
     self.check_params()
     self.resource = Resources(
         Template.get_template_data(self.template_file))
Example #3
0
 def __enter__(self):
     template_dict = Resources(Template.get_template_data(self._template_file)).to_json()
     resource = template_dict.get(model.RESOURCE, {})
     func = self._get_function(self._get_namespace(resource))
     self._runtime = Runtime(func[model.PROPERTY])
     self._debug_context = DebugContext(self._debug_port, self._debug_argv, self._runtime.runtime)
     return self
Example #4
0
    def __enter__(self):
        template_dict = Resources(FuncContext().get_template_data(
            self._template_file)).to_json()
        resource = template_dict.get("Resources", {})
        ns = resource.get(self.namespace, {})
        if not ns:
            raise InvokeContextException(
                "You must provide a namespace identifier,default is 'default'")
        if ns.has_key("Type"):
            del ns["Type"]
        self._template_dict = {"Resources": ns}
        self._function_provider = ScfFunctionProvider(
            template_dict=self._template_dict)
        self._env_vars = self._get_env_vars(self._env_vars_file)
        self._log_file_fp = self._get_log_file(self._log_file)
        self._debug_context = self._get_debug_context(self._debug_port,
                                                      self._debug_args,
                                                      self._debugger_path)

        self._check_docker()

        return self
Example #5
0
 def __init__(self, template_file, forced=False):
     self.template_file = template_file
     self.check_params()
     self.resource = Resources(Context.get_template_data(self.template_file))
     self.forced = forced
Example #6
0
class Package(object):
    def __init__(self, template_file, cos_bucket, output_template_file):
        self.template_file = template_file
        self.cos_bucket = cos_bucket
        self.output_template_file = output_template_file
        self.check_params()
        self.resource = Resources(
            Template.get_template_data(self.template_file))

    def do_package(self):
        for ns_name, ns in vars(self.resource).items():
            for func_name, func in vars(ns).items():
                code_url = self._do_package_core(getattr(func, "CodeUri", ""))
                if "cos_bucket_name" in code_url:
                    setattr(func, "CosBucketName", code_url["cos_bucket_name"])
                    setattr(func, "CosObjectName", code_url["CosObjectName"])
                    click.secho(
                        "Upload function zip file '{}' to COS bucket '{}' success"
                        .format(os.path.basename(code_url["cos_object_name"]),
                                code_url["cos_bucket_name"]),
                        fg="green")
                elif "zip_file" in code_url:
                    setattr(func, "LocalZipFile", code_url["zip_file"])

        yaml_dump(self.resource.to_json(), self.output_template_file)
        click.secho("Generate deploy file '{}' success".format(
            self.output_template_file),
                    fg="green")

    def check_params(self):
        if not self.template_file:
            click.secho("FAM Template Not Found", fg="red")
            raise TemplateNotFoundException(
                "Missing option --template-file".format(self.template_file))

    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 _zip_func(self, func_path):
        buff = BytesIO()
        if not os.path.exists(func_path):
            raise ContextException(
                "Function file or path not found by CodeUri '{}'".format(
                    func_path))

        zip_file_name = str(uuid.uuid1()) + '.zip'
        cwd = os.getcwd()
        os.chdir(func_path)
        with ZipFile(buff, mode='w', compression=ZIP_DEFLATED) as zip_object:
            for current_path, sub_folders, files_name in os.walk(_CURRENT_DIR):
                for file in files_name:
                    zip_object.write(os.path.join(current_path, file))

        os.chdir(cwd)
        buff.seek(0)
        buff.name = zip_file_name

        # a temporary support for upload func from local zipfile
        with open(zip_file_name, 'wb') as f:
            f.write(buff.read())
            buff.seek(0)
        # click.secho("Compress function '{}' to zipfile '{}' success".format(func_config.name, zip_file_name))

        return buff, zip_file_name

    def _get_code_abs_path(self, code_uri):

        if not self._cwd or self._cwd == _CURRENT_DIR:
            self._cwd = os.getcwd()

        self._cwd = os.path.abspath(self._cwd)

        code_path = code_uri
        if not os.path.isabs(code_uri):
            code_path = os.path.normpath(os.path.join(self._cwd, code_path))

        return code_path

    def _get_cwd(self):
        cwd = os.path.dirname(os.path.abspath(self._template_path))
        return cwd
Example #7
0
def validate(template_file):
    '''
    validate a scf template.
    '''
    resource = Resources(Context.get_template_data(template_file))