厨师食谱执行顺序

时间:2014-08-22 12:37:02

标签: chef chef-recipe silent-installer

以下是我用于在Windows服务器上复制和安装软件的配方。

batch "Get installed software list" do
  code "wmic /output:C:\\InstallList.txt product get name,version"
end

if File.read('C:/InstallList.txt', mode: 'r:BOM|UTF-16:UTF-8') =~ /.NET Framework 4/
  print "Dotnet framework 4 is already installed."
else
  remote_file 'c:/repo/dotNetFx40_Full_x86_x64.exe' do
    source 'file:////10.132.17.53/e$/CHEFREPO/dotNetFx40_Full_x86_x64.exe'
  end
  batch "Install Dotnet" do
    cwd 'c:/repo/'
    code <<-EOH
        dotNetFx40_Full_x86_x64.exe  #{node['mycloud_dotnet']['passive']} #{node['mycloud_dotnet']['CEIPconsent']} #{node['mycloud_dotnet']['chainingpackage']} #{node['mycloud_dotnet']['createlayout']} #{node['mycloud_dotnet']['lcid']} #{node['mycloud_dotnet']['log']} #{node['mycloud_dotnet']['msioptions']} #{node['mycloud_dotnet']['norestart']} #{node['mycloud_dotnet']['promptrestart']} #{node['mycloud_dotnet']['quit']} #{node['mycloud_dotnet']['repair']} #{node['mycloud_dotnet']['serialdownload']} #{node['mycloud_dotnet']['uninstall']} #{node['mycloud_dotnet']['parameterfolder']} #{node['mycloud_dotnet']['NoSetUpVersionCheck']} #{node['mycloud_dotnet']['uninstallpatch']} 
        del dotNetFx40_Full_x86_x64.exe

    EOH
    not_if {File.read('c:/InstallList.txt', mode: 'r:BOM|UTF-16:UTF-8') =~ /.NET Framework 4/}
  end
end

批处理资源创建了一个文本文件,其中包含已安装软件的列表。然后我读取此文件以检查软件是否已安装。它是,我打印一条消息。此外,配方进入我的其他部分,我从远程存储库下载设置,然后使用静默安装进行安装。 但是在客户端运行时,我收到如下错误:

[2014-08-22T05:26:38-07:00] FATAL: Errno::ENOENT: No such file or directory - C:
/InstallList.txt

在执行第一批File.read之前,我的客户端以某种方式在if块中执行resource("Get installed Software List")代码。任何可能出错的想法和相同的解决方法。

1 个答案:

答案 0 :(得分:3)

是的,您的if代码在编译阶段执行,而您的batch资源在汇聚阶段执行,请参阅details here

您可以通过在编译时运行batch资源来实现您的目标,如:

batch "Get installed software list" do
  code "wmic /output:C:\\InstallList.txt product get name,version"
  action :nothing
end.run_action(:run)

在您的情况下,在not_if块中包含您的代码并删除if / else部分是另一种选择 即:

remote_file 'c:/repo/dotNetFx40_Full_x86_x64.exe' do
  source 'file:////10.132.17.53/e$/CHEFREPO/dotNetFx40_Full_x86_x64.exe'
end

batch "Install Dotnet" do
  cwd 'c:/repo/'
  code <<-EOH
    dotNetFx40_Full_x86_x64.exe  #{node['mycloud_dotnet']['passive']} #                 {node['mycloud_dotnet']['CEIPconsent']} #{node['mycloud_dotnet']['chainingpackage']} #{node['mycloud_dotnet']['createlayout']} #{node['mycloud_dotnet']['lcid']} #{node['mycloud_dotnet']['log']} #{node['mycloud_dotnet']['msioptions']} #{node['mycloud_dotnet']['norestart']} #{node['mycloud_dotnet']['promptrestart']} #{node['mycloud_dotnet']['quit']} #{node['mycloud_dotnet']['repair']} #{node['mycloud_dotnet']['serialdownload']} #{node['mycloud_dotnet']['uninstall']} #{node['mycloud_dotnet']['parameterfolder']} #{node['mycloud_dotnet']['NoSetUpVersionCheck']} #{node['mycloud_dotnet']['uninstallpatch']} 
    del dotNetFx40_Full_x86_x64.exe
  EOH
  not_if { `wmic /output:C:\\InstallList.txt product get name,version` =~ /.NET Framework 4/ }
end

远程文件可能有一个警卫,以避免每次下载:

not_if { File::exists(...) }