繁体   English   中英

Bash脚本在终端中正常运行,但在Chef中运行的相同脚本失败

[英]Bash script works fine in terminal but same script run in Chef fails

我正在运行Chef-client 11.12.8。 我编写了一个bash脚本,其中包含以下行:

aws s3 ls s3://my-bucket/prod-builds/

我只是使用awscli工具从S3存储桶中获取文件列表。 当我在终端(Ubuntu 14.04 LTS)上手动运行此命令时,得到以下输出:

2014-10-10 21:27:50   42081271 aragorn-38d5afe23f1fa09a991c4cdbcb8461ed8709d15f.tgz

但是当chef-client使用printf语句在同一行执行bash脚本时,我看到bash脚本仅输出:

2014-10-10

看来命令输出中空格的存在使Chef-client感到困惑。

这是我的厨师食谱中执行bash脚本的那一行:

execute "Download latest build" do
    command "bash /path/to/my/script"
    creates "/file/that/gets/created"
    user "aragorn"
    action :run
end

知道我该如何解决吗?

更新:根据要求,这是我的脚本的一部分:

if [ -z "$gitCommitId" ]; then
        # Take a filename like "aragorn-38d5afe23f1fa09a991c4cdbcb8461ed8709d15f.tgz" and extract the commitId from it
        # Note that we take the topmost result
        gitCommitId=$(aws s3 ls s3://my-bucket/prod-builds/ | sort -r | head -2 | sed -n '1!p' | awk -F "aragorn-" '{print $2}' | cut -d'.' -f 1)
fi

我已经确认执行了这一行(使用输出的printf语句),但是奇怪的是,当我执行命令aws s3 ls s3://my-bucket/prod-builds/ | sort -r | head -2 | sed -n '1!p' | awk -F "aragorn-" '{print $2}' | cut -d'.' -f 1 aws s3 ls s3://my-bucket/prod-builds/ | sort -r | head -2 | sed -n '1!p' | awk -F "aragorn-" '{print $2}' | cut -d'.' -f 1 aws s3 ls s3://my-bucket/prod-builds/ | sort -r | head -2 | sed -n '1!p' | awk -F "aragorn-" '{print $2}' | cut -d'.' -f 1直接在我的终端中(或从我的终端手动运行脚本),它工作正常。 但是当从厨师gitCommitId运行时, gitCommitId设置为gitCommitId

更新#2:我最后写了一个简单的bash脚本来测试Chef和Terminal之间的差异。 我确认Chef run从调用aws s3 ls s3://my-bucket/prod-builds输出的结果与从终端调用它时输出的结果不同。 具体而言,它在终端呼叫中存在的S3存储桶中保留了一个文件夹。 我仍然无法解释这一点。

我通过使用以下行来结束操作,该行通过确保仅显示构建而不显示文件夹来提供一致的结果:

aws s3 ls s3://my-bucket/prod-builds/ | grep aragorn | sort -r | head -1 | cut -d'-' -f 4 | cut -d'.' -f 1

特别感谢@TejayCardon和@StephenC为我指出正确的方向!

该问题几乎肯定会在脚本中发生,因此您需要找出脚本失败的原因。

我将从将command属性更改为

    command "bash -x /path/to/my/script > /tmp/some_file 2>&1"

查看bash正在执行的命令。


看来命令输出中空格的存在使Chef-client感到困惑。

您在问题中没有向我们显示的任何内容都支持该结论。 (而且这不太可能execute资源不会对输出执行任何操作,除非(也许)将其记录下来。)

我怀疑问题出在脚本,或者它运行的命令之一....,或者是关于环境变量,当前目录等的假设。


UPDATE

因此问题似乎出在以下管道中:

   aws s3 ls s3://my-bucket/prod-builds/ | \
       sort -r | \ 
       head -2 | \
       sed -n '1!p' | \
       awk -F "aragorn-" '{print $2}' | \
       cut -d'.' -f 1

我将首先查看管道中的第一个命令是否在stdout上产生任何输出。 (我怀疑问题是尚未设置aws命令所需的环境变量。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM