Reputation:
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
Reputation: 5065
You'll want to combine a few methods here:
: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.:create
action.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
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
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