user9753902
user9753902

Reputation:

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

I am using chef to do this:

I am trying to have a bash block run only after the previous one runs, simple enough, I use notify. I also want there to be a lock file check on the initial run and the second (is there a better way to make sure it only runs when the previous bash block runs?).

Here is my current chef code:

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

Upvotes: 1

Views: 499

Answers (1)

Brandon Miller
Brandon Miller

Reputation: 5065

You'll want to combine a few methods here:

  • You want to make sure resources that are notified aren't also running on their own. So, their actions should be set to :nothing. That way they aren't running on their own, then being notified to run again conditionally. The action you define as part of your subscribe is the action it will take when notified.
  • You also want to be sure that the lock files are only created if the resource they lock is actually run. So, they should also be set to nothing and be notified with the :create action.
  • Use Chef Guards to check for existence of the lock files. This way you'll still see specific output that the resource was skipped (due to guard), rather then ignored all together.

Example using your code:

Use a not_if guard so the resource will not run if the file defined by the tsm_login_lock variable exists. Additionally notify the lock file to be created.

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

Have this resource do nothing on its own unless notified by the resource it's locking

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

Similarly, this resource should have a not_if guard for the init lock file. In addition it should have a default action of nothing since it receives a notification from the login resource. Finally, notify it's lock file to be created.

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

Have this init lock file resource do nothing on it's own, should only be notified by the resource it locks

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

Finally, I'd highly recommend finding out what you'd consider a successful login for Tableau and for init. Ask yourself how you'd check these if you logged in to the server. Use these verifications for guards instead of lock files. In general you want to use guards where ever needed to be sure resources are idempotent. Review the link above on guards for complete details on how guards work.

Upvotes: 1

Related Questions