![](/img/trans.png)
[英]How can I programmatically tell if a binary file on a website (e.g. image) has changed without downloading it?
[英]How to fetch a remote file (e.g. from Github) in a Puppet file resource?
file { 'leiningen':
path => '/home/vagrant/bin/lein',
ensure => 'file',
mode => 'a+x',
source => 'https://raw.github.com/technomancy/leiningen/stable/bin/lein',
}
是我的主意,但Puppet不知道http://
。 puppet://
有什么問題puppet://
我錯過了?
或者如果沒有,有沒有辦法以聲明方式首先獲取文件然后將其用作本地源?
在Puppet 4.4之前,根據http://docs.puppetlabs.com/references/latest/type.html#file ,文件源只接受puppet://或file:// URIs。
從Puppet 4.4+起, 您的原始代碼將成為可能 。
如果您使用的是舊版本,那么在不拉下整個Git存儲庫的情況下實現您想要做的事情的一種方法是使用exec資源來獲取文件。
exec{'retrieve_leiningen':
command => "/usr/bin/wget -q https://raw.github.com/technomancy/leiningen/stable/bin/lein -O /home/vagrant/bin/lein",
creates => "/home/vagrant/bin/lein",
}
file{'/home/vagrant/bin/lein':
mode => 0755,
require => Exec["retrieve_leiningen"],
}
雖然exec的使用有點不受歡迎,但它可以有效地用於創建自己的類型。 例如,您可以獲取上面的代碼段並創建自己的資源類型。
define remote_file($remote_location=undef, $mode='0644'){
exec{"retrieve_${title}":
command => "/usr/bin/wget -q ${remote_location} -O ${title}",
creates => $title,
}
file{$title:
mode => $mode,
require => Exec["retrieve_${title}"],
}
}
remote_file{'/home/vagrant/bin/lein':
remote_location => 'https://raw.github.com/technomancy/leiningen/stable/bin/lein',
mode => '0755',
}
當您引用GitHub存儲庫時,我會使用Puppetlabs vcsrepo模塊 ,這將提供能夠反饋更改或只是保持最新的額外好處。 您可以使用Puppet Forge安裝模塊
sudo puppet module install puppetlabs/vcsrepo
然后,您只需聲明存儲庫並使用文件鏈接將文件准確放置在您想要的位置。
vcsrepo { '/opt/leiningen':
ensure => present,
provider => git,
source => 'https://github.com/technomancy/leiningen.git',
revision => 'stable',
}
file { "/usr/local/bin/lein": # or wherever you want the file to be
ensure => symlink,
target => '/opt/leiningen/bin/lein',
}
請注意, revision
參數可用於指定修訂,標記或(如此處所示)分支。
顯然你可以省略文件聲明,只需更新你的PATH以包含/opt/leiningen/bin/
。
我喜歡maestrodev-wget模塊。 它可以在Puppetlabs Forge上找到 。
安裝很簡單。 我使用vagrant很多,我有一個'bootstrap.sh'文件,其中包括:
puppet module install maestrodev-wget
然后就是這樣的事情:
include wget
wget::fetch { "download the jdk7 file":
source => 'https://a_path_to_our_internal_artifact_repo/oracle/jdk7...',
destination => '/tmp/jdk7...',
timeout => 0,
verbose => true,
nocheckcertificate => true,
}
然后我就像正常一樣使用我班級中的文件。 我添加了nocheckcertificate標志,因為我從我們的本地https repo中取出,我經常忘記該標志。
作者還制作了一個非常棒的maven模塊,也可用於從工件存儲庫中獲取文件。
雖然Puppet 4.4和更新版本支持從http源檢索基本文件,但內置支持非常非常基本。 使用Puppet Forge中的實用程序類型可以最好地解決文件檢索問題。
有幾個選擇。 適用於所有平台的高質量認可模塊是lwf / remote_file 。 這是作為本機Puppet類型實現的,而不是作為Exec包裝器實現的。
用法示例:
remote_file { '/path/to/your/file':
ensure => present,
source => 'https://example.com/file.tar.gz',
checksum => 'd41d8cd98f00b204e9800998ecf8427e'
proxy_host => '192.168.12.40',
proxy_port => 3128,
}
支持的其他功能包括傳遞控制SSL驗證要求的HTTP標頭,以及使用基本身份驗證用戶名/密碼。
對於Windows puppet用戶,您可以使用Powershell模塊和本機Powershell Invoke-WebRequest
命令。
exec { 'C:/home/vagrant/bin/lein':
command => 'Invoke-WebRequest "https://raw.github.com/technomancy/leiningen/stable/bin/lein" -OutFile "C:/home/vagrant/bin/lein"',
provider => powershell,
creates => 'C:/home/vagrant/bin/lein'
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.