使用Puppet在Windows上获取临时目录

时间:2016-02-16 09:25:50

标签: windows puppet temp

我正在尝试使用download_file模块下载文件,并希望将其保存到临时目录。有没有内置的方法来使用Puppet执行此操作而不将其硬编码为C:\Windows\Temp

download_file { "Download Flyway" :
    url                   => 'https://bintray.com/artifact/download/business/maven/flyway-commandline-3.2.1-windows-x64.zip',
    destination_directory => 'C:\Windows\Temp'
}

1 个答案:

答案 0 :(得分:1)

您可以创建自定义事实来提供该信息(名称类似于modulename / lib / facter / module_temp_dir.rb):

Facter.add('module_temp_dir') do
  setcode do
    if Puppet::Util::Platform.windows?
        require 'win32/registry'

        value = nil
        begin
          # looking at current user may likely fail because it's likely going to be LocalSystem
          hive = Win32::Registry::HKEY_CURRENT_USER
          hive.open('Environment', Win32::Registry::KEY_READ | 0x100) do |reg|
            value = reg['TEMP']
          end
        rescue Win32::Registry::Error => e
          value = nil
        end

        if value.nil?
          begin
            hive = Win32::Registry::HKEY_LOCAL_MACHINE
            hive.open('SYSTEM\CurrentControlSet\Control\Session Manager\Environment', Win32::Registry::KEY_READ | 0x100) do |reg|
              value = reg['TEMP']
            end
          rescue Win32::Registry::Error => e
            value = nil
          end
        end
      end

      value
    end
  end
end

这种方法比简单地使用ENV['TEMP']更受欢迎,因为它不会被篡改,就像环境变量可能在运行时一样。

如果您不关心被篡改的值和/或您可能想在本地运行时更改它,您可以改为执行以下操作:

Facter.add('module_temp_dir') do
  setcode do
   ENV['TEMP']       
  end
end

作为自动提供部分/全部环境变量作为事实的后续行动,我提交了https://tickets.puppetlabs.com/browse/FACT-1346。我们可以快速推进提供更多重要系统提供的环境变量,在哪里提供所有这些变量,我们需要白名单功能,该功能目前被称为阻止该票证(https://tickets.puppetlabs.com/browse/FACT-718)。

参考:

https://github.com/chocolatey/puppet-chocolatey/blob/master/lib/facter/choco_install_path.rbhttps://github.com/chocolatey/puppet-chocolatey/blob/master/lib/puppet_x/chocolatey/chocolatey_install.rb