how to make chef execute with a specific user and load its environment values
Asked Answered
P

2

9

Hi I am creating a WCS instance, for which i have to execute the create instance command using the wcs user (webadmin), its failing to connect to DB as its not able to get the required env variables.

so i put some sample code to check

I am using the below code

bash "wcs-create-instance" do
    user "webadmin"
    group "webspher"
    code <<-EOH        
        ###{node[:websphere][:wcs][:wcs_installLocation]}/bin/config_ant.sh -DinstanceName=#{node[:websphere][:wcs][:wcs_instance]} CreateInstance  
    whoami > /tmp/whoami
    env > /tmp/env              
EOH
    notifies :run, "bash[fix-permission]", :immediately 
    #This not_if is just temporary, a proper mechanism has to be implemented here to loop through all the WCS APars,
    #For the POC keeping it neat and simple such that this does not rerun on execution
    not_if {File.directory?("#{node[:websphere][:wcs][:wcs_installLocation]}/instances/#{node[:websphere][:wcs][:wcs_instance]}/starterstores")}
    #action :nothing
end

For whoami i am getting the user

webadmin

But for env i am getting the env of the user "root", its not sourcing the .bash_profile for the env variables. Any ideas

Positronium answered 12/7, 2013 at 11:55 Comment(0)
S
7

There is an environment attribute in the bash resource. Or you can source the .bash_profile in the script. That's one of the things you can do with bash (last example)

Sher answered 12/7, 2013 at 12:8 Comment(5)
tried to source but its not working, given there are quite a few environment variables to set, setting it while running the command in bash resource would be very cumbersome.Positronium
Seems that i was doing something wrong, the solution suggested by Paulo is the correct one, i did source the .bash_profile (this time properly) and it was working fine. Thanks againPositronium
@Positronium You mentioned you sourced .bash_profile properly. Can you tell me how? Even I am facing problem with sourcing bash_profileDrosophila
@VinayKadalagi follow the link which Paulo has suggested. bash 'foo' do code 'source /tmp/foo.sh' endPositronium
Or you can use this using the environment attribute execute "wcs-create-instance" do user node['was']['usr'] group node['was']['usr_grp'] cwd "#{node['wcs']['wcs_installLocation']}/bin" environment( 'ORACLE_HOME' => '#{node["ora_client"]["home"]}/product/#{node["ora_client"]["version"]}', 'TNS_ADMIN' => node["ora_client"]["tns_home"] ) command "./config_ant.sh -DinstanceName=#{node['wcs']['wcs_instance']} CreateInstance" not_if {File.exists?("#{node['wcs']['wcs_installLocation']}/instances/#{node['wcs']['wcs_instance']}/web/index.html")} endPositronium
F
-1

Seems like adding flags '-l' to tell bash to act as a login shell also does the trick.

bash 'do something' do
  code 'my_command'
  flags '-l'
end

Or using the execute block:

execute 'foo' do
  command 'bash -l -c "my_command"'
end
Fronnia answered 21/5, 2019 at 0:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.