Ejemplo n.º 1
0
    def test_must_resolve_absolute_codeuri(self, codeuri):

        expected = codeuri  # CodeUri must be return as is for absolute paths

        actual = resolve_code_path(None, codeuri)
        self.assertEquals(expected, actual)
        self.assertTrue(os.path.isabs(actual), "Result must be an absolute path")
Ejemplo n.º 2
0
    def _get_invoke_config(self, function):
        """
        Returns invoke configuration to pass to Lambda Runtime to invoke the given function

        :param samcli.commands.local.lib.provider.Function function: Lambda function to generate the configuration for
        :return samcli.local.lambdafn.config.FunctionConfig: Function configuration to pass to Lambda runtime
        """

        env_vars = self._make_env_vars(function)
        code_abs_path = resolve_code_path(self.cwd, function.codeuri)

        LOG.debug("Resolved absolute path to code is %s", code_abs_path)

        function_timeout = function.timeout

        # The Runtime container handles timeout inside the container. When debugging with short timeouts, this can
        # cause the container execution to stop. When in debug mode, we set the timeout in the container to a max 10
        # hours. This will ensure the container doesn't unexpectedly stop while debugging function code
        if self.is_debugging():
            function_timeout = self.MAX_DEBUG_TIMEOUT

        return FunctionConfig(name=function.name,
                              runtime=function.runtime,
                              handler=function.handler,
                              code_abs_path=code_abs_path,
                              layers=function.layers,
                              memory=function.memory,
                              timeout=function_timeout,
                              env_vars=env_vars)
Ejemplo n.º 3
0
    def test_must_resolve_present_cwd(self, cwd_path):
        codeuri = self.relative_codeuri
        expected = os.path.normpath(os.path.join(self.os_cwd, codeuri))

        actual = resolve_code_path(cwd_path, codeuri)
        self.assertEquals(expected, actual)
        self.assertTrue(os.path.isabs(actual), "Result must be an absolute path")
Ejemplo n.º 4
0
    def test_must_resolve_relative_codeuri(self, codeuri):

        expected = os.path.normpath(os.path.join(self.cwd, codeuri))

        actual = resolve_code_path(self.cwd, codeuri)
        self.assertEquals(expected, actual)
        self.assertTrue(os.path.isabs(actual), "Result must be an absolute path")
Ejemplo n.º 5
0
    def _get_invoke_config(self, function):
        """
        Returns invoke configuration to pass to Lambda Runtime to invoke the given function

        :param samcli.commands.local.lib.provider.Function function: Lambda function to generate the configuration for
        :return samcli.local.lambdafn.config.FunctionConfig: Function configuration to pass to Lambda runtime
        """

        env_vars = self._make_env_vars(function)
        code_abs_path = resolve_code_path(self.cwd, function.codeuri)

        LOG.debug("Resolved absolute path to code is %s", code_abs_path)

        function_timeout = function.timeout

        # The Runtime container handles timeout inside the container. When debugging with short timeouts, this can
        # cause the container execution to stop. When in debug mode, we set the timeout in the container to a max 10
        # hours. This will ensure the container doesn't unexpectedly stop while debugging function code
        if self.is_debugging():
            function_timeout = self.MAX_DEBUG_TIMEOUT

        return FunctionConfig(name=function.name,
                              runtime=function.runtime,
                              handler=function.handler,
                              code_abs_path=code_abs_path,
                              layers=function.layers,
                              memory=function.memory,
                              timeout=function_timeout,
                              env_vars=env_vars)
Ejemplo n.º 6
0
    def download(self, layer: LayerVersion, force=False) -> LayerVersion:
        """
        Download a given layer to the local cache.

        Parameters
        ----------
        layer samcli.commands.local.lib.provider.Layer
            Layer representing the layer to be downloaded.
        force bool
            True to download the layer even if it exists already on the system

        Returns
        -------
        Path
            Path object that represents where the layer is download to
        """
        if layer.is_defined_within_template:
            LOG.info("%s is a local Layer in the template", layer.name)
            # the template file containing the layer might not be in the same directory as root template file
            # therefore we need to join the path of template directory and codeuri in case codeuri is a relative path.
            try:
                stack = next(stack for stack in self._stacks
                             if stack.stack_path == layer.stack_path)
            except StopIteration as ex:
                raise RuntimeError(
                    f"Cannot find stack that matches layer's stack_path {layer.stack_path}"
                ) from ex

            codeuri = (layer.codeuri
                       if os.path.isabs(layer.codeuri) else os.path.normpath(
                           os.path.join(os.path.dirname(stack.location),
                                        layer.codeuri)))
            layer.codeuri = resolve_code_path(self.cwd, codeuri)
            return layer

        layer_path = Path(self.layer_cache).resolve().joinpath(layer.name)
        is_layer_downloaded = self._is_layer_cached(layer_path)
        layer.codeuri = str(layer_path)

        if is_layer_downloaded and not force:
            LOG.info("%s is already cached. Skipping download", layer.arn)
            return layer

        layer_zip_path = layer.codeuri + ".zip"
        layer_zip_uri = self._fetch_layer_uri(layer)
        unzip_from_uri(
            layer_zip_uri,
            layer_zip_path,
            unzip_output_dir=layer.codeuri,
            progressbar_label="Downloading {}".format(layer.layer_arn),
        )

        return layer
Ejemplo n.º 7
0
    def get_invoke_config(self, function: Function) -> FunctionConfig:
        """
        Returns invoke configuration to pass to Lambda Runtime to invoke the given function

        :param samcli.commands.local.lib.provider.Function function: Lambda function to generate the configuration for
        :return samcli.local.lambdafn.config.FunctionConfig: Function configuration to pass to Lambda runtime
        """

        env_vars = self._make_env_vars(function)
        code_abs_path = None
        if function.packagetype == ZIP:
            # the template file containing the function might not be in the same directory as root template file
            # therefore we need to join the path of template directory and codeuri in case codeuri is a relative path.
            try:
                stack = next(stack for stack in self.provider.stacks
                             if stack.stack_path == function.stack_path)
            except StopIteration as ex:
                raise RuntimeError(
                    f"Cannot find stack that matches function's stack_path {function.stack_path}"
                ) from ex

            # Note(xinhol): function.codeuri might be None here, we might want to add some check here.
            codeuri = (
                function.codeuri
                if os.path.isabs(function.codeuri)  # type: ignore
                else os.path.join(os.path.dirname(stack.location),
                                  function.codeuri)  # type: ignore
            )
            code_abs_path = resolve_code_path(self.cwd, codeuri)
            LOG.debug("Resolved absolute path to code is %s", code_abs_path)

        function_timeout = function.timeout

        # The Runtime container handles timeout inside the container. When debugging with short timeouts, this can
        # cause the container execution to stop. When in debug mode, we set the timeout in the container to a max 10
        # hours. This will ensure the container doesn't unexpectedly stop while debugging function code
        if self.is_debugging():
            function_timeout = self.MAX_DEBUG_TIMEOUT

        return FunctionConfig(
            name=function.name,
            runtime=function.runtime,
            handler=function.handler,
            imageuri=function.imageuri,
            imageconfig=function.imageconfig,
            packagetype=function.packagetype,
            code_abs_path=code_abs_path,
            layers=function.layers,
            memory=function.memory,
            timeout=function_timeout,
            env_vars=env_vars,
        )
Ejemplo n.º 8
0
    def download(self, layer, force=False):
        """
        Download a given layer to the local cache.

        Parameters
        ----------
        layer samcli.commands.local.lib.provider.Layer
            Layer representing the layer to be downloaded.
        force bool
            True to download the layer even if it exists already on the system

        Returns
        -------
        Path
            Path object that represents where the layer is download to
        """
        if layer.is_defined_within_template:
            LOG.info("%s is a local Layer in the template", layer.name)
            layer.codeuri = resolve_code_path(self.cwd, layer.codeuri)
            return layer

        LayerDownloader._create_cache(self.layer_cache)

        # disabling no-member due to https://github.com/PyCQA/pylint/issues/1660
        layer_path = Path(self.layer_cache).joinpath(layer.name).resolve()  # pylint: disable=no-member
        is_layer_downloaded = self._is_layer_cached(layer_path)
        layer.codeuri = str(layer_path)

        if is_layer_downloaded and not force:
            LOG.info("%s is already cached. Skipping download", layer.arn)
            return layer

        layer_zip_path = layer.codeuri + '.zip'
        layer_zip_uri = self._fetch_layer_uri(layer)
        unzip_from_uri(layer_zip_uri,
                       layer_zip_path,
                       unzip_output_dir=layer.codeuri,
                       progressbar_label='Downloading {}'.format(
                           layer.layer_arn))

        return layer
Ejemplo n.º 9
0
    def download(self, layer: LayerVersion, force=False) -> LayerVersion:
        """
        Download a given layer to the local cache.

        Parameters
        ----------
        layer samcli.commands.local.lib.provider.Layer
            Layer representing the layer to be downloaded.
        force bool
            True to download the layer even if it exists already on the system

        Returns
        -------
        Path
            Path object that represents where the layer is download to
        """
        if layer.is_defined_within_template:
            LOG.info("%s is a local Layer in the template", layer.name)
            layer.codeuri = resolve_code_path(self.cwd, layer.codeuri)
            return layer

        layer_path = Path(self.layer_cache).resolve().joinpath(layer.name)
        is_layer_downloaded = self._is_layer_cached(layer_path)
        layer.codeuri = str(layer_path)

        if is_layer_downloaded and not force:
            LOG.info("%s is already cached. Skipping download", layer.arn)
            return layer

        layer_zip_path = layer.codeuri + ".zip"
        layer_zip_uri = self._fetch_layer_uri(layer)
        unzip_from_uri(
            layer_zip_uri,
            layer_zip_path,
            unzip_output_dir=layer.codeuri,
            progressbar_label="Downloading {}".format(layer.layer_arn),
        )

        return layer