Chef - 如何检查 bash 块 运行 是否成功并在第二个实例中忽略

Chef - How to check if a bash block ran successfully and to ignore at the second instances

我的厨师食谱包含以下 bash 方块 -

bash "run_bash" do
user node[:value1][:user]
group node[:value1][:group]
cwd node[:value1][:install_dir] + '/tools/bin'
code <<-EOH
export MW_HOME=#{node[:value1][:mw_install_dir]}
export JAVA_HOME=#{node[:value1][:install_dir]}
export ORACLE_HOME=#{node[:value1][:install_dir]}
/usr/bin/expect -c 'spawn ./idmConfigTool.sh -configOAM input_file=#{Chef::Config[:file_cache_path]}/config.props
expect "Enter ID Store Bind DN password : "
send "#{OrcladminPassword}\r"
expect "Enter User Password for IDSTORE_PWD_SOFTWAREUSER: "
send "#{LDAPPassword }\r"
expect "Confirm User Password for IDSTORE_PWD_APPSOFTWAREUSER: "
send "#{AppLDAPPassword }\r"
expect "Enter User Password for IDSTORE_PWD_APP2ADMINUSER: "
send "#{App2AdminPassword }\r"
expect "Confirm User Password for IDSTORE_PWD_APP3ADMINUSER: "
send "#{App3AdminPassword}\r"
sleep 240
expect eof'
EOH

我正在尝试 运行 与主厨 solo.So 一起制作此食谱,但此食谱会 运行。

我需要一个解决方案来使这个 bash 块成为幂等的。有 not_if、only_if 等守卫语句。但我无法在这里找到实现这些守卫的方法。

我想到了一个解决方案,一旦这个 bash 块 运行 成功,我可以找到时间戳并将其与下一个 运行 时的当前时间戳进行比较正在发生。如果那些不匹配,我可以忽略 运行宁 it.This 只是我认为的一轮蛮力工作。

请针对此场景提供更好的优化方案。

我会使用模板 notifications:

your_cookbook/templates/default/script.erb:

#!/bin/bash
export MW_HOME=<%= node[:value1][:mw_install_dir] %>
export JAVA_HOME=<%= node[:value1][:install_dir] %>
export ORACLE_HOME=<%= node[:value1][:install_dir]}
/usr/bin/expect -c 'spawn ./idmConfigTool.sh -configOAM input_file=<%=
Chef::Config[:file_cache_path] %>/config.props
expect "Enter ID Store Bind DN password : "
send "<%= @OrcladminPassword %>\r"
expect "Enter User Password for IDSTORE_PWD_SOFTWAREUSER: "
send "<%= @LDAPPassword %>\r"
expect "Confirm User Password for IDSTORE_PWD_APPSOFTWAREUSER: "
send "<%= @AppLDAPPassword %>\r"
expect "Enter User Password for IDSTORE_PWD_APP2ADMINUSER: "
send "<%= @App2AdminPassword %>\r"
expect "Confirm User Password for IDSTORE_PWD_APP3ADMINUSER: "
send "<%= @App3AdminPassword %>\r"
sleep 240
expect eof

you_cookbook/recipes/default.rb:

execute 'my_installer' do
  command '/usr/local/bin/my_script'
  action :noting # For it not to run on each converge by default
end

template '/usr/local/bin/my_script' do
  source 'script.erb'
  mode '0600'
  variables(
    'OrcladminPassword' => <how you set the value>,
    'LDAPPassword' => <...>,
    'AppLDAPPassword' => <...>,
    'App2AdminPassword' => <...>,
    'App3AdminPassword' => <...>
  )
  notifies :run,execute[my_installer]
end

您可以在通知行的末尾添加一个 , :immediately 以在模板已呈现后触发执行。

这样chef只会在模板改变的时候触发execute资源

这没有考虑到您的安装可能无法运行并且厨师不会在这种情况下重试的情况,安装将保持失败。

总而言之,我会与产品编辑器合作,开发一种软​​件包来避免这种脆弱的安装。