0

we set a Chef runlist for a node composed by, let's say, recipe A and then recipe B. In recipe A we execute a command resource that launch a script.sh, which export the $PATH variable on the target machine with a custom path.

Next, when recipe B is executed, it looks for an executable to be launched and we want that it can execute it looking into the paths contained into $PATH. Unfortunately the $PATH variable during recipe B loses the modifies made during recipe A.

The context is: on a machine we have a JVM in a specific path (that is NOT in $PATH). With recipe A we launch a script.sh that export the JVM bin path into $PATH. Then in recipe B we use a java executable (let's say, jar) and so we want that it is executing looking into the path we exported before in $PATH. But it doesn't work.

We want that the JVM path is set temporarily into $PATH just to let recipe B execute. I suppose we cannot use any of Chef environment variable to achieve that, since in a distributed scenario the JVM path can vary from machine to machine.

Thanks a lot.

UPDATE:

Recipe A

mydir = "/path/to/jvm"
script_name = "script.sh"
sh = File.join(mydir,script_name).gsub("/","\/")
script_str = ". " + sh

# Launch script.sh in order to export Java to PATH
execute "ExecuteScript" do
  command script_str
  user "myuser"
  group "mygroup"
  only_if {File.exists?(File.join(mydir,script_name))}
end

Unfortunately I cannot publish Recipe B, but keep in mind that it is not editable and it simply execute "java" or "jar" (looking for them in $PATH).

UPDATE 2

I solved this issue setting in recipe A the following:

javapath = "/path/to/jvm/bin"
ENV['PATH'] = "#{ENV['PATH']}:" + javapath

In that way I set the environment variable $PATH for the duration of the Ruby execution. This makes recipe B able to see that variable, and so executing the required bin (jar in my case). I don't know if that is an elegant solution, but it worked for me.

giglio91
  • 53
  • 1
  • 8
  • Even if the recipe is not editable, its resources can be `rewind`, to add a `env` parameter to an `execute` resource for example. without details on what is in recipe B it's impossible to give a correct advice, maybe you can try modifying chef ruby's `ENV['PATH']` to add your jvm path, so the child processes will inherit it, but this smells a brittle code at end. – Tensibai Nov 17 '16 at 09:48
  • [related](http://stackoverflow.com/questions/25295439/reload-environment-variables-path-from-chef-recipes-client?rq=1) and [also related](http://stackoverflow.com/questions/6284517/how-can-you-use-a-chef-recipe-to-set-an-environment-variable?rq=1) with info for the `magic_shell` cookbook. – Tensibai Nov 17 '16 at 09:51

2 Answers2

0

execute a command resource that launch a script.sh, which export the $PATH variable on the target machine with a custom path.

This export is only available within the execute resource, so for your script and anything it calls, it is discarded as soon as the resource ends.

Your description sounds like you're doing it wrong at first, but without more details it's hard to give a correct advice for your use case.

Tensibai
  • 15,557
  • 1
  • 37
  • 57
  • Yeah, I supposed the scope of the export was limited to the resource or at most the recipe A. So, supposing we can't edit the recipe B, I think there is no way to export that JVM dir in order to let recipe B correctly execute any java executable from that dir (looking into $PATH), right? – giglio91 Nov 17 '16 at 09:04
  • @giglio91 There's plenty of way, that's why I say it's hard to give a correct advice without more insight on what your real use case is/how you're using it. Show your recipes and maybe we will be able to give a better way to achieve the goal. – Tensibai Nov 17 '16 at 09:07
0

Environment variables are inherited from parent to child when a process forks, but they can't go from child to parent. When Chef runs your external command, any env var changes will be scoped to that process. You can use a script resource to run a longer block of shell code in a single process so it can share env vars.

coderanger
  • 52,400
  • 4
  • 52
  • 75