【问题标题】:Is there any way to see the output of a shell script when launched through make, for SAM通过make启动时,有什么方法可以查看shell脚本的输出,对于SAM
【发布时间】:2021-02-18 00:03:58
【问题描述】:

我正在尝试构建一个多层 Python SAM(AWS 无服务器应用程序模型)应用程序。

没什么花哨的,但它有一些依赖项,使用pipenv 管理。

SAM 有一个sam build 命令,它在幕后使用make。 我有一个看起来像这样的Makefile

# Empty build target for "sam build". Why?
# 1. "sam build Layers" works
# 2. "sam build" for the main function doesn't (it tries to use pip and crashes)
# With this empty target the main build is skipped and only the layers are built, which is enough.
build-Function: ;

build-Layers:
    bash pip-install.sh "$(ARTIFACTS_DIR)"

pip-install.sh 看起来像这样:

#!/bin/bash
ARTIFACTS_DIR="${1}"

set -eu

# Detect Cygwin: https://stackoverflow.com/a/20691293/1320143
uname="$(uname -s)"
if [[ "${uname}" =~ "Cygwin" ]]
then
    # AWS SAM for Windows passes a Windows path Cygwin tools (make, etc.) don't understand.
    ARTIFACTS_DIR="$(cygpath -u "${ARTIFACTS_DIR}")"
fi

PROJECT_PATH="$(echo "${ARTIFACTS_DIR}" | sed 's@/.aws-sam.*@@')"
PIPFILE_PATH="$(echo "${ARTIFACTS_DIR}" | sed 's@/.aws-sam.*@/../Pipfile.lock@' | xargs readlink -f)"

mkdir -p "${ARTIFACTS_DIR}/python"
# We're using pipenv, which does not use requirements.txt.
# requirements.txt is used by SAM to automatically package and deploy layers (dependencies).
# To avoid dependency/version duplication, we extract the info from the pipenv info.

jq -r '.default | to_entries[] | .key + .value.version' "${PIPFILE_PATH}" > requirements.txt

# For debugging purposes.
ls -lha

python -m pip install -r "${PROJECT_PATH}/requirements.txt" -t "${ARTIFACTS_DIR}/python"

重要的是这个项目使用pipenv来管理依赖,而不是pip,SAM使用make作为构建工具,但是之后它只有pip扩展。这个脚本基本上创建了一个pip理解的依赖列表,然后pip将所有依赖下载到一个文件夹中,然后打包成一个大层。

主要问题是调试。

我已经看到由于某些(恕我直言)原因make 隐藏了它启动的 shell 脚本的输出。据说make可以被骗显示出来,用这个:See output of shell script in Makefile

我已经尝试过了,但它不起作用。我运行sam build Layers,我只看到sam 输出。我试过--debug,同样的事情。

有没有办法显示该输出?我希望能够在make/sam build 上下文中调试脚本,因为我不明白我遇到的一些错误????

【问题讨论】:

  • "make" 不会隐藏它启动的 shell 脚本的输出。如果输出没有显示,它已被重定向到别处。
  • ... 这将是正在使用的 makefile 或运行 make 的程序的函数。
  • 嗯,这就是我在这里的原因,这不是严格意义上的make 问题,它也是sam build 问题。 makefile 不会重定向任何内容,它启动的脚本也不会重定向。所以在我看来一定是sam build,我只是想看看有没有办法避免这种情况。
  • @Jens 那么你如何解释这个答案呢?查看投票最多的答案:stackoverflow.com/a/11021891/1320143 根据测试,make 绝对不会显示脚本的命令输出,也不会发生任何重定向。你必须不遗余力地改变它,并将输出 redirect 到 stderr 才能真正看到它。
  • @oblio 解释是$(shell xxx) 不是制造的东西。这是一个 GNU 制造的东西,专门设计用于扩展为命令的输出。根据定义,这不能去终端。

标签: python makefile aws-lambda aws-serverless aws-lambda-layers


【解决方案1】:

隐藏输出的不是make,而是sam (issue #2006)。当make 失败(返回任何不同于 0 的退出代码)时,它只打印 STDERR。您可以使用 shell 重定向将 STDOUT 重定向到 STDERR:

my-rule:
    npm run build-my-lambda 1>&2

【讨论】:

    【解决方案2】:

    在 re:Invent 2020 上,AWS 宣布 Lambda 现在可以使用 Docker 映像。

    与其混合使用 SAM 提供的 Make 和专有设置来构建 Lambda 层,只需使用标准 Dockerfile 即可节省大量时间?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 2021-11-04
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 2017-04-07
      相关资源
      最近更新 更多