Chef bash 执行 not_if 某些命令返回 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29099552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Chef bash execute not_if some command returns 1
提问by cardinal-gray
I need to checkout code in git repo & do mvn clean package
only if code in repo dir contains wrong branch. How can I do this with (or without) not_if in chef recipe?
我需要在 git repo 中签出代码,并且mvn clean package
仅当 repo 目录中的代码包含错误的分支时才执行。我如何在厨师食谱中使用(或不使用)not_if 来做到这一点?
bash "checkout_and_compile_if_wrong_version" do
user "develop"
group "develop"
cwd "/var/www/"
environment 'HOME' => "/home/develop"
code <<-EOH
cd /var/www/code_repo/
git checkout #{node['last_version']}
git pull
mvn clean package
EOH
not_if { ...condition... }
end
Maybe I can use for a condition something like check (in code_repo dir):
也许我可以使用类似检查的条件(在 code_repo 目录中):
[ "$(git branch |grep "*" |awk '{print }')" == #{node['last_version']} ] && echo 1
Thx in advance.
提前谢谢。
回答by Tensibai
Chef has a git resource, clearly best adapted to your use case.
Chef 有一个git resource,显然最适合您的用例。
I would do something like this (untested code):
我会做这样的事情(未经测试的代码):
execute "clean package" do
cwd "/var/www/code_repo/"
command "mvn clean package"
user "develop"
group "develop"
action :nothing
end
git "/var/www/code_repo/" do
repository "you_repo_url"
revision node['last_version']
user "develop"
group "develop"
action :sync
notifies :run,"execute[clean package]", :immediately
end
This will sync if a new commit is done or if node['last_version']
change and run mvn clean
only when there's a change. The HOME
env var is set by git resource so it does not need to be provided.
如果完成新的提交或node['last_version']
更改并mvn clean
仅在发生更改时运行,这将同步。环境HOME
变量由 git 资源设置,因此不需要提供。
Edit per comment:
每条评论编辑:
Quoting the not_if
or only_if
guard documentation before anything:
在任何事情之前引用not_if
或only_if
保护文档:
A string is executed as a shell command. If the command returns 0, the guard is applied. If the command returns any other value, then the guard attribute is not applied
字符串作为 shell 命令执行。如果命令返回 0,则应用保护。如果命令返回任何其他值,则不应用防护属性
recipe code:
配方代码:
bash "whatever" do
command "test"
only_if %Q{git branch | awk '/^[*] / { exit =="#{node['last_version']}"}'}, :cwd => "/var/www/code_repo"
end
Tricky part:
The bash would work only if the version is not equal to the node['last_version']
(guard printing 0 by the equality test within awk)
棘手的部分:只有当版本不等于node['last_version']
(awk 中的相等测试保护打印 0)时,bash 才会工作
I simplified the grep/awk/test part in a single awk script working on line starting with *
(the regex inside the / /
) and then telling awk to exit with the result of comparison between the second field (the actual git branch) and the chef attribute version. The %Q{ code and "quote test indide" }
is a ruby form to allow using quotes without having to escape them.
我在单个 awk 脚本中简化了 grep/awk/test 部分,从*
( 中的正则表达式/ /
)开始,然后根据第二个字段(实际的 git 分支)和厨师属性之间的比较结果告诉 awk 退出版本。这%Q{ code and "quote test indide" }
是一个红宝石形式,允许使用引号而不必转义它们。
If the comparison is true, do nothing, if it's false, run the bash script.
如果比较为真,则什么都不做,如果为假,则运行 bash 脚本。
I highly advise trying to use chef resources whenever possible and trying to replace bash script code with chef resources as much as possible. (It's easier to maintain and keep idempotent in time)
我强烈建议尽可能使用厨师资源,并尽可能用厨师资源替换 bash 脚本代码。(更容易维护并及时保持幂等)