简体   繁体   中英

Set environment variables for current chef-client run

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

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

穿上你的client.rbsolo.rb

ENV['VAR'] = 'value'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM