Skip to content

Commit db5b154

Browse files
davidphaynathanlcarlson
authored andcommitted
Simplify compose install
1 parent 6117530 commit db5b154

File tree

3 files changed

+27
-232
lines changed

3 files changed

+27
-232
lines changed

manifests/compose.pp

Lines changed: 15 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -7,141 +7,30 @@
77
# @param version
88
# The version of Docker Compose to install.
99
#
10-
# @param install_path
11-
# The path where to install Docker Compose.
12-
#
13-
# @param symlink_name
14-
# The name of the symlink created pointing to the actual docker-compose binary
15-
# This allows use of own docker-compose wrapper scripts for the times it's
16-
# necessary to set certain things before running the docker-compose binary
17-
#
18-
# @param proxy
19-
# Proxy to use for downloading Docker Compose.
20-
#
21-
# @param base_url
22-
# The base url for installation
23-
# This allows use of a mirror that follows the same layout as the
24-
# official repository
25-
#
26-
# @param raw_url
27-
# Override the raw URL for installation
28-
# The default is to build a URL from baseurl. If rawurl is set, the caller is
29-
# responsible for ensuring the URL points to the correct version and
30-
# architecture.
31-
#
32-
# @param curl_ensure
33-
# Whether or not the curl package is ensured by this module.
34-
#
3510
class docker::compose (
36-
Enum[present,absent] $ensure = present,
37-
Optional[String] $version = $docker::params::compose_version,
38-
Optional[String] $install_path = $docker::params::compose_install_path,
39-
Optional[String] $symlink_name = $docker::params::compose_symlink_name,
40-
Optional[Pattern['^((http[s]?)?:\/\/)?([^:^@]+:[^:^@]+@|)([\da-z\.-]+)\.([\da-z\.]{2,6})(:[\d])?([\/\w \.-]*)*\/?$']] $proxy = undef,
41-
Optional[String] $base_url = $docker::params::compose_base_url,
42-
Optional[String] $raw_url = undef,
43-
Optional[Boolean] $curl_ensure = $docker::params::curl_ensure,
11+
Enum[present,absent] $ensure = present,
12+
Optional[String] $version = $docker::params::compose_version,
4413
) inherits docker::params {
4514

46-
if $facts['kernel'] == 'Linux' {
15+
if $docker::manage_package {
16+
if $version and $ensure != 'absent' {
17+
$package_ensure = $version
18+
} else {
19+
$package_ensure = $ensure
20+
}
21+
4722
case $facts['os']['family'] {
4823
'Debian': {
49-
ensure_packages('docker-compose-plugin', { ensure => pick($version,$ensure), require => defined(bool2str($docker::use_upstream_package_source)) ? { true => Apt::Source['docker'], false => undef } }) #lint:ignore:140chars
24+
ensure_packages('docker-compose-plugin', { ensure => $package_ensure, require => defined(bool2str($docker::use_upstream_package_source)) ? { true => Apt::Source['docker'], false => undef } }) #lint:ignore:140chars
5025
}
5126
'RedHat': {
52-
ensure_packages('docker-compose-plugin', { ensure => pick($version,$ensure), require => defined(bool2str($docker::use_upstream_package_source)) ? { true => Yumrepo['docker'], false => undef } }) #lint:ignore:140chars lint:ignore:unquoted_string_in_selector
27+
ensure_packages('docker-compose-plugin', { ensure => $package_ensure, require => defined(bool2str($docker::use_upstream_package_source)) ? { true => Yumrepo['docker'], false => undef } }) #lint:ignore:140chars lint:ignore:unquoted_string_in_selector
5328
}
54-
default: {}
55-
}
56-
} else {
57-
# mac & Windows OS
58-
if $facts['os']['family'] == 'windows' {
59-
$file_extension = '.exe'
60-
$file_owner = 'Administrator'
61-
} else {
62-
$file_extension = ''
63-
$file_owner = 'root'
64-
}
65-
66-
$docker_compose_location = "${install_path}/${symlink_name}${file_extension}"
67-
68-
if $version == 'latest' {
69-
$version = "2.20.2"
70-
}
71-
72-
$docker_compose_location_versioned = "${install_path}/docker-compose-${version}${file_extension}"
73-
74-
if $ensure == 'present' {
75-
if $raw_url != undef {
76-
$docker_compose_url = $raw_url
77-
} else {
78-
if $version =~ /2[.]\d+[.]\d+$/ {
79-
$version_prepath = 'v'
80-
}
81-
82-
$docker_compose_url = "${base_url}/${version_prepath}${version}/docker-compose-${facts['kernel']}-${facts['os']['hardware']}${file_extension}"
83-
}
84-
85-
if $proxy != undef {
86-
$proxy_opt = "--proxy ${proxy}"
87-
} else {
88-
$proxy_opt = ''
29+
'Windows': {
30+
fail('Docker compose is installed with docker machine on Windows')
8931
}
90-
91-
if $facts['os']['family'] == 'windows' {
92-
$docker_download_command = "if (Invoke-WebRequest ${docker_compose_url} ${proxy_opt} -UseBasicParsing -OutFile \"${docker_compose_location_versioned}\") { exit 0 } else { exit 1}" # lint:ignore:140chars
93-
94-
$parameters = {
95-
'proxy' => $proxy,
96-
'docker_compose_url' => $docker_compose_url,
97-
'docker_compose_location_versioned' => $docker_compose_location_versioned,
98-
}
99-
100-
exec { "Install Docker Compose ${version}":
101-
command => epp('docker/windows/download_docker_compose.ps1.epp', $parameters),
102-
provider => powershell,
103-
creates => $docker_compose_location_versioned,
104-
}
105-
106-
file { $docker_compose_location:
107-
ensure => 'link',
108-
target => $docker_compose_location_versioned,
109-
require => Exec["Install Docker Compose ${version}"],
110-
}
111-
} else {
112-
if $curl_ensure {
113-
stdlib::ensure_packages(['curl'])
114-
}
115-
116-
exec { "Install Docker Compose ${version}":
117-
path => '/usr/bin/',
118-
cwd => '/tmp',
119-
command => "curl -s -S -L ${proxy_opt} ${docker_compose_url} -o ${docker_compose_location_versioned}",
120-
creates => $docker_compose_location_versioned,
121-
require => Package['curl'],
122-
}
123-
124-
file { $docker_compose_location_versioned:
125-
owner => $file_owner,
126-
mode => '0755',
127-
seltype => 'container_runtime_exec_t',
128-
require => Exec["Install Docker Compose ${version}"],
129-
}
130-
131-
file { $docker_compose_location:
132-
ensure => 'link',
133-
target => $docker_compose_location_versioned,
134-
require => File[$docker_compose_location_versioned],
135-
}
136-
}
137-
138-
} else {
139-
file { $docker_compose_location_versioned:
140-
ensure => absent,
141-
}
142-
143-
file { $docker_compose_location:
144-
ensure => absent,
32+
default: {
33+
fail('This module only works on Debian, RedHat or Windows.')
14534
}
14635
}
14736
}

manifests/params.pp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@
4747
$dns = undef
4848
$dns_search = undef
4949
$proxy = undef
50-
$compose_base_url = 'https://github.com/docker/compose/releases/download'
51-
$compose_symlink_name = 'docker-compose'
52-
$compose_version = 'latest'
53-
$compose_install_path = undef
50+
$compose_version = undef
5451
$no_proxy = undef
5552
$execdriver = undef
5653
$storage_driver = undef
@@ -92,7 +89,6 @@
9289
$docker_command = 'docker'
9390

9491
if ($facts['os']['family'] == 'windows') {
95-
$compose_install_path = "${facts['docker_program_files_path']}/Docker"
9692
$docker_ee_package_name = 'Docker'
9793
$machine_install_path = "${facts['docker_program_files_path']}/Docker"
9894
$tls_cacert = "${facts['docker_program_data_path']}/docker/certs.d/ca.pem"

spec/shared_examples/compose.rb

Lines changed: 11 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -3,110 +3,20 @@
33
shared_examples 'compose' do |_params, _facts|
44
ensure_value = _params['ensure']
55
version = _params['version']
6-
install_path = _params['install_path']
7-
symlink_name = _params['symlink_name']
8-
proxy = _params['proxy']
9-
base_url = _params['base_url']
10-
raw_url = _params['raw_url']
11-
curl_ensure = _params['curl_ensure']
126

7+
if _params['manage_package']
8+
ensure_value = if _params['version'] != :undef && _params['ensure'] != 'absent'
9+
_params['version']
10+
else
11+
_params['ensure']
12+
end
1313

14-
if _facts[:kernel] == 'Linux'
1514
case _facts['os']['family']
16-
when 'Debian'
17-
18-
when 'RedHat'
19-
20-
end
21-
22-
else
23-
if _facts[:os]['family'] == 'windows'
24-
file_owner = 'Administrator'
25-
file_extension = '.exe'
26-
else
27-
file_owner = 'root'
28-
file_extension = ''
29-
end
30-
31-
docker_compose_location = "#{install_path}/#{symlink_name}#{file_extension}"
32-
33-
if version == 'latest'
34-
version = "2.20.2"
35-
end
36-
37-
if ensure == 'present'
38-
docker_compose_url = if raw_url == :undef
39-
"#{base_url}/#{version}/docker-compose-#{_facts[:kernel]}-x86_64#{file_extension}"
40-
else
41-
raw_url
42-
end
43-
44-
proxy_opt = if proxy == :undef
45-
''
46-
else
47-
"--proxy #{proxy}"
48-
end
49-
50-
if _facts[:os]['family'] == 'windows'
51-
docker_download_command = "if (Invoke-WebRequest #{docker_compose_url} #{proxy_opt} -UseBasicParsing -OutFile \"#{docker_compose_location_versioned}\") { exit 0 } else { exit 1 }"
52-
53-
it {
54-
expect(subject).to contain_exec("Install Docker Compose #{version}").with(
55-
'provider' => 'powershell',
56-
'creates' => docker_compose_location_versioned,
57-
)
58-
59-
expect(subject).to contain_file(docker_compose_location).with(
60-
'ensure' => 'link',
61-
'target' => docker_compose_location_versioned,
62-
).that_requires(
63-
"Exec[Install Docker Compose #{version}]",
15+
when 'Debian', 'RedHat'
16+
it {
17+
is_expected.to contain_package('docker-compose-plugin').with(
18+
ensure: ensure_value,
6419
)
65-
}
66-
else
67-
if curl_ensure
68-
it {
69-
expect(subject).to contain_package('curl')
70-
}
71-
end
72-
73-
it {
74-
expect(subject).to contain_exec("Install Docker Compose #{version}").with(
75-
'path' => '/usr/bin/',
76-
'cwd' => '/tmp',
77-
'command' => "curl -s -S -L #{proxy_opt} #{docker_compose_url} -o #{docker_compose_location_versioned}",
78-
'creates' => docker_compose_location_versioned,
79-
).that_requires(
80-
'Package[curl]',
81-
)
82-
83-
expect(subject).to contain_file(docker_compose_location_versioned).with(
84-
'owner' => file_owner,
85-
'mode' => '0755',
86-
).that_requires(
87-
"Exec[Install Docker Compose #{version}]",
88-
)
89-
90-
expect(subject).to contain_file(docker_compose_location).with(
91-
'ensure' => 'link',
92-
'target' => docker_compose_location_versioned,
93-
).that_requires(
94-
"File[#{docker_compose_location_versioned}]",
95-
)
96-
}
97-
end
98-
99-
end
100-
101-
else
102-
it {
103-
expect(subject).to contain_file(docker_compose_location_versioned).with(
104-
'ensure' => 'absent',
105-
)
106-
107-
expect(subject).to contain_file(docker_compose_location).with(
108-
'ensure' => 'absent',
109-
)
110-
}
20+
}
11121
end
11222
end

0 commit comments

Comments
 (0)