Skip to content

Commit a032e52

Browse files
authored
Merge pull request #8903 from mhashizume/PA-11502/6.x/windows_url_package
(PUP-11502) Windows exe packages can be sourced from URL
2 parents a8a9ce9 + b6ccdcf commit a032e52

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

lib/puppet/provider/package/windows.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@
3030
has_feature :versionable
3131

3232
attr_accessor :package
33+
class << self
34+
attr_accessor :paths
35+
end
36+
37+
def self.post_resource_eval
38+
@paths.each do |path|
39+
begin
40+
Puppet::FileSystem.unlink(path)
41+
rescue => detail
42+
raise Puppet::Error.new(_("Error when unlinking %{path}: %{detail}") % { path: path ,detail: detail.message}, detail)
43+
end
44+
end if @paths
45+
end
3346

3447
# Return an array of provider instances
3548
def self.instances
@@ -64,7 +77,7 @@ def install
6477

6578
command = [installer.install_command(resource), install_options].flatten.compact.join(' ')
6679
working_dir = File.dirname(resource[:source])
67-
if !Puppet::FileSystem.exist?(working_dir) && resource[:source] =~ /\.msi"?\Z/i
80+
unless Puppet::FileSystem.exist?(working_dir)
6881
working_dir = nil
6982
end
7083
output = execute(command, :failonfail => false, :combine => true, :cwd => working_dir, :suppress_window => true)

lib/puppet/provider/package/windows/exe_package.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ class ExePackage < Puppet::Provider::Package::Windows::Package
1717
'WindowsInstaller',
1818
]
1919

20+
def self.register(path)
21+
Puppet::Type::Package::ProviderWindows.paths ||= []
22+
Puppet::Type::Package::ProviderWindows.paths << path
23+
end
24+
2025
# Return an instance of the package from the registry, or nil
2126
def self.from_registry(name, values)
2227
if valid?(name, values)
@@ -55,7 +60,31 @@ def match?(resource)
5560
end
5661

5762
def self.install_command(resource)
58-
munge(resource[:source])
63+
file_location = resource[:source]
64+
if file_location.start_with?('http://', 'https://')
65+
tempfile = Tempfile.new(['','.exe'])
66+
begin
67+
uri = URI(Puppet::Util.uri_encode(file_location))
68+
client = Puppet.runtime[:http]
69+
client.get(uri, options: { include_system_store: true }) do |response|
70+
raise Puppet::HTTP::ResponseError.new(response) unless response.success?
71+
72+
File.open(tempfile.path, 'wb') do |file|
73+
response.read_body do |data|
74+
file.write(data)
75+
end
76+
end
77+
end
78+
rescue => detail
79+
raise Puppet::Error.new(_("Error when installing %{package}: %{detail}") % { package: resource[:name] ,detail: detail.message}, detail)
80+
ensure
81+
self.register(tempfile.path)
82+
tempfile.close()
83+
file_location = tempfile.path
84+
end
85+
end
86+
87+
munge(file_location)
5988
end
6089

6190
def uninstall_command

lib/puppet/provider/package/windows/package.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def self.installer_class(resource)
6767
# REMIND: what about msp, etc
6868
MsiPackage
6969
when /\.exe"?\Z/i
70-
fail(_("The source does not exist: '%{source}'") % { source: resource[:source] }) unless Puppet::FileSystem.exist?(resource[:source])
70+
fail(_("The source does not exist: '%{source}'") % { source: resource[:source] }) unless
71+
Puppet::FileSystem.exist?(resource[:source]) || resource[:source].start_with?('http://', 'https://')
7172
ExePackage
7273
else
7374
fail(_("Don't know how to install '%{source}'") % { source: resource[:source] })

spec/unit/provider/package/windows/exe_package_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'spec_helper'
22
require 'puppet/provider/package/windows/exe_package'
3+
require 'puppet/provider/package/windows'
34

45
describe Puppet::Provider::Package::Windows::ExePackage do
56
let (:name) { 'Git version 1.7.11' }
@@ -71,10 +72,26 @@
7172

7273
context '#install_command' do
7374
it 'should install using the source' do
75+
allow(Puppet::FileSystem).to receive(:exist?).with(source).and_return(true)
7476
cmd = described_class.install_command({:source => source})
7577

7678
expect(cmd).to eq(source)
7779
end
80+
81+
it 'should raise error when URI is invalid' do
82+
web_source = 'https://www.t e s t.test/test.exe'
83+
84+
expect do
85+
described_class.install_command({:source => web_source, :name => name})
86+
end.to raise_error(Puppet::Error, /Error when installing #{name}:/)
87+
end
88+
89+
it 'should download package from source file before installing', if: Puppet::Util::Platform.windows? do
90+
web_source = 'https://www.test.test/test.exe'
91+
stub_request(:get, web_source).to_return(status: 200, body: 'package binaries')
92+
cmd = described_class.install_command({:source => web_source})
93+
expect(File.read(cmd)).to eq('package binaries')
94+
end
7895
end
7996

8097
context '#uninstall_command' do

0 commit comments

Comments
 (0)