我正在尝试使用 chef 安装 OpenJDK,以及下载 Eclipse 并在 Windows 2008 节点上使用 p2 director 安装一些插件。安装 OpenJDK 并设置环境变量 JAVA_HOME 并将其添加到路径中。 但是,在我关闭并重新打开 PowerShell 之前,此更改不会生效。 chef-client run 在当前 session 中需要这些来运行 eclipse p2 director。有什么方法可以让我只运行一次 chef-client 吗?
在我安装 openJDK 的秘诀中,我包括:
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"
在我安装 eclipse 插件的方法中:
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
最佳答案
尝试使用 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...
改编自用于启用 NuGet 包还原的 visualstudio 说明书: https://github.com/daptiv/visualstudio/blob/master/recipes/nuget.rb
关于windows - 为当前 chef-client 运行设置环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25387655/