windows - 为当前 chef-client 运行设置环境变量

标签 windows powershell chef-infra chef-recipe

我正在尝试使用 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/

相关文章:

powershell - 在Powershell中重新加载修改后的类

java - Chef 对已部署应用程序的健全性检查

ruby - 如何使用 include_recipe 检查 Recipe 是否已被包含?

c# - 如何读取故障转储文件(我有源代码和 .pdb 文件)

windows - XCOPY 是通过 Windows 文件系统复制文件夹/文件的最快工具吗?

c++ - Win32 : How to hide 3rd party windows in taskbar by hWnd

powershell - 如何设置 Jenkins 在 Powershell 脚本中使用 Knife

C# Windows 窗体在安装后不打开默认浏览器

powershell - 在事件目录中获取不属于ANY组的用户的列表

powershell - 如何使用 Powershell 从文本文件中删除回车符?