【问题标题】:Chef run bash block only after other bash block on first run厨师仅在第一次运行其他 bash 块之后运行 bash 块
【发布时间】:2018-05-29 08:53:46
【问题描述】:

我正在使用厨师来执行此操作:

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

这是我目前的厨师代码:

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

【问题讨论】:

    标签: ruby bash chef-infra chef-recipe


    【解决方案1】:

    您需要在这里结合几种方法:

    • 您希望确保收到通知的资源不会自行运行。因此,他们的操作应设置为: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
    

    让这个初始化锁定文件资源自己什么都不做,应该只被它锁定的资源通知

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

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

    【讨论】:

    • 至于知道它是否成功,我不太确定 tableau 是如何工作的。我的任务是制作一本遵循与我们拥有的融合指南相同的步骤的食谱。我将询问这个案例的成功是什么样的。
    • 一切都好。只是一个建议。一切都会随着时间而改善。有时,让事情发挥作用是第一步,也是最重要的一步。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-30
    • 2015-09-13
    相关资源
    最近更新 更多