Chef 运行 bash 仅在其他 bash 块之后第一个 运行 块

Chef run bash block only after other bash block on first run

我正在使用 chef 来做这个:

我试图在前一个 运行 之后有一个 bash 块 运行,很简单,我使用通知。我还希望在第一个 运行 和第二个 运行 上进行锁定文件检查(有没有更好的方法来确保它只有 运行s 而前一个 bash 块 运行s?).

这是我目前的厨师代码:

if not File.exist? tsm_login_lock
  bash 'login_tsm' do
    user tableau_user
    cwd tableau_user_home
    code <<-EOH
      source /etc/profile.d/tableau_server.sh
      tsm login -u #{tableau_user} -p #{password}
      tsm settings import -f  /home/analytics/setting_file.json
      tsm pending-changes apply
      tsm licenses activate -k #{key}
      tsm register --file #{registration}
      tsm pending-changes apply
    EOH
    notifies :run, "bash[tsm_init]", :immediately
  end
 file tsm_login_lock do
   mode '0644'
   content 'tableau server stareted'
 end
end

if not File.exist? tsm_init_lock
  bash 'tsm_init' do
    user tableau_user
    cwd tableau_user_home
    code <<-EOH
      tsm initialize --start-server --request-timeout 1800
    EOH
    notifies :run, "bash[tsm_2]", :immediately
  end
  file tsm_init_lock do
    mode '0644'
    content 'tableau server initialized'
  end
end

您需要在此处结合使用几种方法:

  • 您要确保收到通知的资源不会 运行自行启动。所以,他们的动作应该设置为:nothing。这样他们就不会自己运行宁,然后有条件地再次通知运行。您定义为 subscribe 一部分的操作是它在收到通知时将采取的操作。
  • 您还希望确保仅当锁定文件实际上是 运行 时才创建锁定文件。因此,它们也应该设置为空,并通过 :create 操作得到通知。
  • 使用 Chef Guards 检查锁定文件是否存在。这样你仍然会看到资源被跳过(由于守卫)的特定输出,而不是一起被忽略。

使用您的代码的示例:

使用 not_if 守卫,这样如果 tsm_login_lock 变量定义的文件存在,资源将不会 运行。另外通知要创建的锁文件。

bash 'login_tsm' do
  user tableau_user
  cwd tableau_user_home
  code <<-EOH
    source /etc/profile.d/tableau_server.sh
    tsm login -u #{tableau_user} -p #{password}
    tsm settings import -f  /home/analytics/setting_file.json
    tsm pending-changes apply
    tsm licenses activate -k #{key}
    tsm register --file #{registration}
    tsm pending-changes apply
  EOH
  notifies :run, "bash[tsm_init]", :immediately
  notifies :create, "file[#{tsm_login_lock}]", :immediately
  not_if { ::File.exist?(tsm_login_lock) }
end

除非被它锁定的资源通知,否则让这个资源自己不做任何事情

file tsm_login_lock do
  mode '0644'
  content 'tableau server stareted'
  action :nothing
end

同样,该资源应该有一个 not_if 初始化锁文件保护。此外,它应该有一个默认的动作,因为它从登录资源接收到通知。最后,通知要创建的锁文件。

bash 'tsm_init' do
  user tableau_user
  cwd tableau_user_home
  code <<-EOH
    tsm initialize --start-server --request-timeout 1800
  EOH
  action :nothing
  not_if { ::File.exist?(tsm_init_lock) }
  notifies :run, "bash[tsm_2]", :immediately
  notifies :create, "file[#{tsm_init_lock}]", :immediately
end

让这个 init lock 文件资源自己不做任何事情,应该只由它锁定的资源通知

file tsm_init_lock do
  mode '0644'
  content 'tableau server initialized'
  action :nothing
end

最后,我强烈建议找出您认为成功登录 Tableau 和 init 的条件。问问你自己,如果你登录到服务器,你将如何检查这些。将这些验证用于守卫而不是锁定文件。通常,您希望在需要的地方使用守卫来确保资源是幂等的。查看上面关于守卫的 link 以了解有关守卫如何工作的完整详细信息。