如何防范最后一个区块的 chef-client 状态 运行;不要使用状态文件

How to guard from chef-client state of last block run; don't use state file

免责声明:对厨师来说还很陌生,我继承了一堆厨师食谱。下面的方法不是最理想的,但这是我现在必须使用的方法。请温柔点:) 另外,请耐心等待我描述我的需要。

请注意,我们使用的是 chef-client 11.16.4。目前,更新到 12.x 不是一个选项。

tl;博士 有没有办法从当前运行ning块的状态指定守卫:

...
only_if { this_block_did_something }
notifies :run, 'bash[deploy-custom-docker-container]', :immediately

好的....

把这段代码放在我继承的食谱中,需要稍微重构一下...

# The identities of the innocent have been changed for their 
# protection. Please ignore odd things in this example:

application app[:name] do
   path app[:deploy_path]
   enable_submodules true
   repository app[:repository]
   owner OWNER
   group GROUP
   symlinks({
       "file.py" => "path/file.py"
   })
   revision app[:branch]
   deploy_key data_bag_item('deployment_keys', 'keyname')['private_key']
end

link "/path/to/file.py" do
   to "/path/to/settings-%s.py" % [file]
end

# This is where I need some direction...I think.
# note that CMD is a valid constant and the custom docker 
# container does not follow any industry standard docker 
# conventions due to our strange use-case. So I had to resort
# using a bash block to call our custom start/stop/restart script

bash 'deploy-custom-docker-container' do
  code <<-EO
  #{CMD} restart
  EO

  # currently a subscribes but I've tried other methods which 
  # don't achieve what I'm trying to accomplish
  subscribes :run, 'application[%s]' % [app[:name]]
end

只要存储库有新代码要同步,application app[:name] 就会将源代码部署到目标节点上。 bash 块重新启动使用该代码的非常自定义和非行业标准的 docker 容器。

在目前的形式中,这是不受欢迎的,无论 application app[:name] 是否必须将代码部署到 git 存储库(IE 存储库是在目标节点上是最新的还是不是最新的)。我确定我可以创建一些代码来确定 repo 是否已更新,触摸 state/lock 文件,然后通过检查该锁定文件是否存在来 guard 执行 bash 块。对我来说,这将是实现我的目标的次优方式。最好的方法是使用厨师的更新状态作为设置守卫的方法。那可能吗??继续阅读...

换句话说,当 application app[:name] 在 chef-client 运行 时间内命中,并且 repo 已更新(因此部署在节点上)时,chef-client 报告步骤application app[:name] 正在部署新代码。如果 repo 不需要更新,chef-client 会愉快地跳过带有“(最新)”消息的块。如果 repo 需要更新,chef-client 会显示部署代码所采取的步骤。所以 chef-client 只知道代码块的状态 运行.

此外,我对我们环境中的主厨客户端 运行 的观察表明,如果我在 application app[:name] 中放置一个 notifies 块并不重要 [= =16=] 或使用 subscribes 方法(粘贴在上面); bash 块得到 运行 而不管 application app[:name] 的状态如何。我希望如果 application app[:name] 没有要执行的更新,那么 bash 块不会 运行.

我担心的是我将不得不使用状态文件来确定来自 application app[:name] 块的回购更新的状态。我宁愿只是从厨师的角度看 application app[:name] 块来防范 运行-time 的状态。

固定代码

正如 zts 所指出的,我的 action 有误或遗漏。以下代码是我能够解决我的问题的代码。

application app[:name] do
   ...

   notifies :run, 'link[%s]' % [filetolink], :immediately
end

link filetolink do
   to file

   notifies :run, 'bash[deploy-custom-docker-container]', :immediately
end

bash 'deploy-custom-docker-container' do
  code <<-EO
  #{CMD} restart
  EO

  action :nothing
end

这对我有用。

通知仅在通知资源发生变化时触发(反之,订阅仅在您订阅的资源发生变化时触发)。

bash 阻止 运行 与通知无关的原因是,默认情况下,bash 会阻止 运行。如果您只希望资源在收到通知时 运行,请确保包含 action :nothing.

即:

bash 'deploy-custom-docker-container' do
  code <<-EO
  #{CMD} restart
  EO

  action :nothing
  subscribes :run, 'application[%s]' % [app[:name]]
end