How to Have Chef Reload Global Path

How to have Chef reload global PATH

Short answer: No. Environment variables are inherited from the parent. A child process cannot change its parent's environment variables.

Long answer: When leveraging signals (or other forms of IPC), you could signal the parent to reload environment variables from a file.

If you have a file ~/my_environment containing the following:

export FOO="bar"

Execute the following code in your bash:

function reload_environment { source ~/my_environment; }
trap reload_environment SIGHUP
reload_environment

Run echo $FOO and see it outputs bar

Now, when another processes sends a HUP signal to this bash process, it'll reload the environment variables in ~/my_environment by running the reload_environment function.

Lets give it a try. Check what's the bash's PID by running

echo $$

Next, change the content of ~/my_environment to:

export FOO="fish"

Then, send a SIGHUP to the bash's PID:

kill -HUP 12133

If everything went well, running echo $FOO on that bash session would yield fish

For your case, you can use inotify to send the HUP signal to bash when ~/my_environment changes. If you're running chef-client from bash, you could send a HUP signal to your parent PID.

Chef cookbook - reload PATH

I'm not 100% sure you can update the PATH variable for future chef runs, but you can set it up manually using the environment attribute within the execute stanza. This can also be used on other Resources as well. See: http://docs.opscode.com/chef/resources.html#execute

From the Chef Docs,

environment
A hash of environment variables: {"ENV_VARIABLE"=>"VALUE"}.
(These environment variables must exist for a command to execute successfully.)
Default value: nil.

Run a command which requires an environment variable

execute "slapadd" do
command "slapadd < /tmp/something.ldif"
creates "/var/lib/slapd/uid.bdb"
action :run
environment ({'HOME' => '/home/myhome'})
end

How to restart a service only if the :upgrade option of the package resource is used

Use a notification

package 'foo' do
notifies :restart, 'service[foo]'
end

Problema with nvm_install and chef-client first run

As mentioned on IRC, you need to handle this in each subshell you run from Chef. A better solution is probably to use poise-javascript to manage versions within Chef.

In chef, how do I update environment attributes from a recipe?

UPDATED to allow sorting by time added.

you need to use search. Rather than use an attribute to determine the list, use an attribute to indicate a given node belongs in the list.

node.set['include_me'] = Time.now

Then search for all nodes that have set this, sort them, and get their fqdn:

sorted = search(:node, 'include_me:*').sort_by { |node| node['include_me'] }
hostnames = sorted.map{ |node| node.fqdn }

This will give you an array of fqdns hostnames that is sorted by the added date. One caveat, it will only include nodes that have finished their run. So if you have nodes building in parallel, they will not see one another. Furthermore, the current node will not show up on its first run. So you'll need a little more logic to search for the current node and then add it to the list if it is missing.



Related Topics



Leave a reply



Submit