4

I'm trying to use chef to install OpenJDK, as well as downoad Eclipse and install a few plugins using p2 director on a Windows 2008 node. OpenJDK installs and I set my environment variables JAVA_HOME and add it to the path. However, this change does not take affect until I close and re-open PowerShell. The chef-client run needs these in the current session to run the eclipse p2 director. Is there any way to do this so that I can run chef-client only once?

In my recipe for installing openJDK I included:

env "JAVA_HOME" do
  value 'C:\\Program Files\\Zulu\\zulu-8'
end

env "path" do
  delim ";"
  value '%JAVA_HOME%\\bin'
  action :modify
end

#For Command Prompt
execute "setPathCMD" do
  command "set PATH=#{node['java']['path']}\\bin;%PATH%"
end
#For PowerShell
powershell_script "setPathPS" do
  code <<-EOH
  $env:Path="#{node['java']['path']}\\bin;$env:Path"
  EOH
end

ENV['Path'] += ";C:\\Program Files\\Zulu\\zulu-8\\bin"

And in the recipe for installing the eclipse plugins I have:

if not node['eclipse']['plugins'].empty?
  node['eclipse']['plugins'].each do |plugin_group|
    repo, id = plugin_group.first
    execute "eclipse plugin install" do
      command "#{node['eclipse']['unzip_location']}/eclipse/eclipse.exe -application org.eclipse.equinox.p2.director -noSplash -repository #{repo} -installIUs #{id}"
      action :run
    end
  end
end
jsmartt
  • 1,404
  • 1
  • 15
  • 22
  • possible duplicate of [How can you use a Chef recipe to set an environment variable?](http://stackoverflow.com/questions/6284517/how-can-you-use-a-chef-recipe-to-set-an-environment-variable) – sethvargo Aug 19 '14 at 16:34
  • @sethvargo Good point in general, but are you sure that the magic_shell_cookbook works in a powershell environment? – hek2mgl Aug 19 '14 at 18:30
  • @sethvargo Had a look at the code, it will not work on Windows – hek2mgl Aug 19 '14 at 18:43
  • You need to adapt the code for Windows. I'm sure the maintainer of the cookbook would accept a PR. – sethvargo Aug 19 '14 at 19:28
  • I may be wrong but don't you need to set `ENV['JAVA_HOME']` too in your openJDK recipe for the current (and child) process to know it ? – Tensibai Aug 20 '14 at 07:58
  • @Tensibai, in order to use it, yes, but just for the current process, I put the full path to java directly into the path. I don't have anything that uses JAVA_HOME – jsmartt Aug 22 '14 at 22:11
  • I had though eclipse needed a JAVA_HOME env variable to be launched... – Tensibai Aug 23 '14 at 06:43
  • please select the answer that worked for you – kamal Nov 02 '17 at 10:02

2 Answers2

1

Try using setx:

execute 'set java_home' do
  command "setx -m JAVA_HOME \"C:\\Program Files\\Zulu\\zulu-8\""
  only_if { ENV['JAVA_HOME'] != 'C:\\Program Files\\Zulu\\zulu-8' }
end

# Set JAVA_HOME for this process
ENV['JAVA_HOME'] = 'C:\\Program Files\\Zulu\\zulu-8'

# do something similar for path...

Adapted from the visualstudio cookbook for enabling NuGet package restore: https://github.com/daptiv/visualstudio/blob/master/recipes/nuget.rb

Mike Robinet
  • 823
  • 6
  • 19
0

Put on your client.rb or solo.rb:

ENV['VAR'] = 'value'
brauliobo
  • 5,843
  • 4
  • 29
  • 34