Skip to content

Commit ece0e8d

Browse files
davidphaynathanlcarlson
authored andcommitted
Replace 'docker-compose' with the docker-compose-plugin
These changes include updates to the docker compose related code and tests. Install compose as plugin on linux Simplify compose install clean class compose_spec.rb Fix linting issue Since all usage of dockercompose is now replaced by docker, this line can be removed Fix syntax and indentation Use the package resource directly
1 parent 8f4b3d9 commit ece0e8d

File tree

5 files changed

+54
-247
lines changed

5 files changed

+54
-247
lines changed

lib/puppet/provider/docker_compose/ruby.rb

+14-16
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
has_command(:docker, 'docker')
1111

12-
has_command(:dockercompose, 'docker-compose')
13-
1412
def set_tmpdir
1513
return unless resource[:tmpdir]
1614

@@ -28,8 +26,8 @@ def exists?
2826
set_tmpdir
2927

3028
# get merged config using docker-compose config
31-
args = [compose_files, '-p', name, 'config'].insert(3, resource[:options]).compact
32-
compose_output = Puppet::Util::Yaml.safe_load(execute([command(:dockercompose)] + args, combine: false), [Symbol])
29+
args = ['compose', compose_files, '-p', name, 'config'].insert(3, resource[:options]).compact
30+
compose_output = Puppet::Util::Yaml.safe_load(execute([command(:docker)] + args, combine: false), [Symbol])
3331

3432
containers = docker([
3533
'ps',
@@ -76,32 +74,32 @@ def get_image(service_name, compose_services)
7674

7775
def create
7876
Puppet.info("Running compose project #{name}")
79-
args = [compose_files, '-p', name, 'up', '-d', '--remove-orphans'].insert(3, resource[:options]).insert(5, resource[:up_args]).compact
80-
dockercompose(args)
77+
args = ['compose', compose_files, '-p', name, 'up', '-d', '--remove-orphans'].insert(3, resource[:options]).insert(5, resource[:up_args]).compact
78+
docker(args)
8179
return unless resource[:scale]
8280

8381
instructions = resource[:scale].map { |k, v| "#{k}=#{v}" }
8482
Puppet.info("Scaling compose project #{name}: #{instructions.join(' ')}")
85-
args = [compose_files, '-p', name, 'scale'].insert(3, resource[:options]).compact + instructions
86-
dockercompose(args)
83+
args = ['compose', compose_files, '-p', name, 'scale'].insert(3, resource[:options]).compact + instructions
84+
docker(args)
8785
end
8886

8987
def destroy
9088
Puppet.info("Removing all containers for compose project #{name}")
91-
kill_args = [compose_files, '-p', name, 'kill'].insert(3, resource[:options]).compact
92-
dockercompose(kill_args)
93-
rm_args = [compose_files, '-p', name, 'rm', '--force', '-v'].insert(3, resource[:options]).compact
94-
dockercompose(rm_args)
89+
kill_args = ['compose', compose_files, '-p', name, 'kill'].insert(3, resource[:options]).compact
90+
docker(kill_args)
91+
rm_args = ['compose', compose_files, '-p', name, 'rm', '--force', '-v'].insert(3, resource[:options]).compact
92+
docker(rm_args)
9593
end
9694

9795
def restart
9896
return unless exists?
9997

10098
Puppet.info("Rebuilding and Restarting all containers for compose project #{name}")
101-
kill_args = [compose_files, '-p', name, 'kill'].insert(3, resource[:options]).compact
102-
dockercompose(kill_args)
103-
build_args = [compose_files, '-p', name, 'build'].insert(3, resource[:options]).compact
104-
dockercompose(build_args)
99+
kill_args = ['compose', compose_files, '-p', name, 'kill'].insert(3, resource[:options]).compact
100+
docker(kill_args)
101+
build_args = ['compose', compose_files, '-p', name, 'build'].insert(3, resource[:options]).compact
102+
docker(build_args)
105103
create
106104
end
107105

manifests/compose.pp

+27-103
Original file line numberDiff line numberDiff line change
@@ -7,118 +7,42 @@
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 {
45-
if $facts['os']['family'] == 'windows' {
46-
$file_extension = '.exe'
47-
$file_owner = 'Administrator'
48-
} else {
49-
$file_extension = ''
50-
$file_owner = 'root'
51-
}
52-
53-
$docker_compose_location = "${install_path}/${symlink_name}${file_extension}"
54-
$docker_compose_location_versioned = "${install_path}/docker-compose-${version}${file_extension}"
55-
56-
if $ensure == 'present' {
57-
if $raw_url != undef {
58-
$docker_compose_url = $raw_url
59-
} else {
60-
$docker_compose_url = "${base_url}/${version}/docker-compose-${facts['kernel']}-${facts['os']['hardware']}${file_extension}"
61-
}
62-
63-
if $proxy != undef {
64-
$proxy_opt = "--proxy ${proxy}"
14+
if $docker::manage_package {
15+
if $version and $ensure != 'absent' {
16+
$package_ensure = $version
6517
} else {
66-
$proxy_opt = ''
18+
$package_ensure = $ensure
6719
}
6820

69-
if $facts['os']['family'] == 'windows' {
70-
$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
71-
72-
$parameters = {
73-
'proxy' => $proxy,
74-
'docker_compose_url' => $docker_compose_url,
75-
'docker_compose_location_versioned' => $docker_compose_location_versioned,
21+
case $facts['os']['family'] {
22+
'Debian': {
23+
package { 'docker-compose-plugin':
24+
ensure => $package_ensure,
25+
require => defined(bool2str($docker::use_upstream_package_source)) ? {
26+
true => Apt::Source['docker'],
27+
false => undef,
28+
},
29+
}
7630
}
77-
78-
exec { "Install Docker Compose ${version}":
79-
command => epp('docker/windows/download_docker_compose.ps1.epp', $parameters),
80-
provider => powershell,
81-
creates => $docker_compose_location_versioned,
82-
}
83-
84-
file { $docker_compose_location:
85-
ensure => 'link',
86-
target => $docker_compose_location_versioned,
87-
require => Exec["Install Docker Compose ${version}"],
88-
}
89-
} else {
90-
if $curl_ensure {
91-
stdlib::ensure_packages(['curl'])
92-
}
93-
94-
exec { "Install Docker Compose ${version}":
95-
path => '/usr/bin/',
96-
cwd => '/tmp',
97-
command => "curl -s -S -L ${proxy_opt} ${docker_compose_url} -o ${docker_compose_location_versioned}",
98-
creates => $docker_compose_location_versioned,
99-
require => Package['curl'],
31+
'RedHat': {
32+
package { 'docker-compose-plugin':
33+
ensure => $package_ensure,
34+
require => defined(bool2str($docker::use_upstream_package_source)) ? {
35+
true => Yumrepo['docker'],
36+
false => undef,
37+
},
38+
}
10039
}
101-
102-
file { $docker_compose_location_versioned:
103-
owner => $file_owner,
104-
mode => '0755',
105-
seltype => 'container_runtime_exec_t',
106-
require => Exec["Install Docker Compose ${version}"],
40+
'Windows': {
41+
fail('Docker compose is installed with docker machine on Windows')
10742
}
108-
109-
file { $docker_compose_location:
110-
ensure => 'link',
111-
target => $docker_compose_location_versioned,
112-
require => File[$docker_compose_location_versioned],
43+
default: {
44+
fail('This module only works on Debian, RedHat or Windows.')
11345
}
11446
}
115-
} else {
116-
file { $docker_compose_location_versioned:
117-
ensure => absent,
118-
}
119-
120-
file { $docker_compose_location:
121-
ensure => absent,
122-
}
12347
}
12448
}

manifests/params.pp

+1-6
Original file line numberDiff line numberDiff line change
@@ -47,8 +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'
50+
$compose_version = undef
5251
$no_proxy = undef
5352
$execdriver = undef
5453
$storage_driver = undef
@@ -90,16 +89,12 @@
9089
$docker_command = 'docker'
9190

9291
if ($facts['os']['family'] == 'windows') {
93-
$compose_install_path = "${facts['docker_program_files_path']}/Docker"
94-
$compose_version = '1.29.2'
9592
$docker_ee_package_name = 'Docker'
9693
$machine_install_path = "${facts['docker_program_files_path']}/Docker"
9794
$tls_cacert = "${facts['docker_program_data_path']}/docker/certs.d/ca.pem"
9895
$tls_cert = "${facts['docker_program_data_path']}/docker/certs.d/server-cert.pem"
9996
$tls_key = "${facts['docker_program_data_path']}/docker/certs.d/server-key.pem"
10097
} else {
101-
$compose_install_path = '/usr/local/bin'
102-
$compose_version = '1.29.2'
10398
$docker_ee_package_name = 'docker-ee'
10499
$machine_install_path = '/usr/local/bin'
105100
$tls_cacert = '/etc/docker/tls/ca.pem'

spec/classes/compose_spec.rb

+2-39
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,6 @@
99
},
1010
'with version => 1.7.0' => {
1111
'version' => '1.7.0'
12-
},
13-
'when proxy is provided' => {
14-
'version' => '1.7.0',
15-
'proxy' => 'http://proxy.example.org:3128/'
16-
},
17-
'when proxy is not a http proxy' => {
18-
'proxy' => 'this is not a URL'
19-
},
20-
'when proxy contains username and password' => {
21-
'version' => '1.7.0',
22-
'proxy' => 'http://user:[email protected]:3128/'
23-
},
24-
'when proxy IP is provided' => {
25-
'version' => '1.7.0',
26-
'proxy' => 'http://10.10.10.10:3128/'
27-
},
28-
'when base_url is provided' => {
29-
'version' => '1.7.0',
30-
'base_url' => 'http://example.org'
31-
},
32-
'when raw_url is provided' => {
33-
'version' => '1.7.0',
34-
'raw_url' => 'http://example.org'
3512
}
3613
}
3714

@@ -56,13 +33,7 @@
5633
context title do
5734
params = {
5835
'ensure' => 'present',
59-
'version' => defaults['compose_version'],
60-
'install_path' => defaults['compose_install_path'],
61-
'symlink_name' => defaults['compose_symlink_name'],
62-
'proxy' => :undef,
63-
'base_url' => defaults['compose_base_url'],
64-
'raw_url' => :undef,
65-
'curl_ensure' => defaults['curl_ensure']
36+
'version' => defaults['compose_version']
6637
}.merge(local_params)
6738

6839
let(:facts) do
@@ -73,15 +44,7 @@
7344
params
7445
end
7546

76-
if title == 'when proxy is not a http proxy'
77-
it 'raises an error for invalid proxy URL' do
78-
expect(subject).to compile.and_raise_error(
79-
%r{parameter 'proxy' expects an undef value or a match for Pattern},
80-
)
81-
end
82-
else
83-
include_examples 'compose', params, facts
84-
end
47+
include_examples 'compose', params, facts
8548
end
8649
end
8750
end

0 commit comments

Comments
 (0)