Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docker compose #902

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions lib/puppet/provider/docker_compose/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

has_command(:docker, 'docker')

has_command(:dockercompose, 'docker-compose')
has_command(:dockercompose, 'docker')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all usage of dockercompose is now replaced by docker, this line can be removed.


def set_tmpdir
return unless resource[:tmpdir]
Expand All @@ -28,8 +28,8 @@ def exists?
set_tmpdir

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

containers = docker([
'ps',
Expand Down Expand Up @@ -76,32 +76,32 @@ def get_image(service_name, compose_services)

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

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

def destroy
Puppet.info("Removing all containers for compose project #{name}")
kill_args = [compose_files, '-p', name, 'kill'].insert(3, resource[:options]).compact
dockercompose(kill_args)
rm_args = [compose_files, '-p', name, 'rm', '--force', '-v'].insert(3, resource[:options]).compact
dockercompose(rm_args)
kill_args = ['compose', compose_files, '-p', name, 'kill'].insert(3, resource[:options]).compact
docker(kill_args)
rm_args = ['compose', compose_files, '-p', name, 'rm', '--force', '-v'].insert(3, resource[:options]).compact
docker(rm_args)
end

def restart
return unless exists?

Puppet.info("Rebuilding and Restarting all containers for compose project #{name}")
kill_args = [compose_files, '-p', name, 'kill'].insert(3, resource[:options]).compact
dockercompose(kill_args)
build_args = [compose_files, '-p', name, 'build'].insert(3, resource[:options]).compact
dockercompose(build_args)
kill_args = ['compose', compose_files, '-p', name, 'kill'].insert(3, resource[:options]).compact
docker(kill_args)
build_args = ['compose', compose_files, '-p', name, 'build'].insert(3, resource[:options]).compact
docker(build_args)
create
end

Expand Down
118 changes: 15 additions & 103 deletions manifests/compose.pp
Original file line number Diff line number Diff line change
Expand Up @@ -7,118 +7,30 @@
# @param version
# The version of Docker Compose to install.
#
# @param install_path
# The path where to install Docker Compose.
#
# @param symlink_name
# The name of the symlink created pointing to the actual docker-compose binary
# This allows use of own docker-compose wrapper scripts for the times it's
# necessary to set certain things before running the docker-compose binary
#
# @param proxy
# Proxy to use for downloading Docker Compose.
#
# @param base_url
# The base url for installation
# This allows use of a mirror that follows the same layout as the
# official repository
#
# @param raw_url
# Override the raw URL for installation
# The default is to build a URL from baseurl. If rawurl is set, the caller is
# responsible for ensuring the URL points to the correct version and
# architecture.
#
# @param curl_ensure
# Whether or not the curl package is ensured by this module.
#
class docker::compose (
Enum[present,absent] $ensure = present,
Optional[String] $version = $docker::params::compose_version,
Optional[String] $install_path = $docker::params::compose_install_path,
Optional[String] $symlink_name = $docker::params::compose_symlink_name,
Optional[Pattern['^((http[s]?)?:\/\/)?([^:^@]+:[^:^@]+@|)([\da-z\.-]+)\.([\da-z\.]{2,6})(:[\d])?([\/\w \.-]*)*\/?$']] $proxy = undef,
Optional[String] $base_url = $docker::params::compose_base_url,
Optional[String] $raw_url = undef,
Optional[Boolean] $curl_ensure = $docker::params::curl_ensure,
Enum[present,absent] $ensure = present,
Optional[String] $version = $docker::params::compose_version,
) inherits docker::params {
if $facts['os']['family'] == 'windows' {
$file_extension = '.exe'
$file_owner = 'Administrator'
} else {
$file_extension = ''
$file_owner = 'root'
}

$docker_compose_location = "${install_path}/${symlink_name}${file_extension}"
$docker_compose_location_versioned = "${install_path}/docker-compose-${version}${file_extension}"

if $ensure == 'present' {
if $raw_url != undef {
$docker_compose_url = $raw_url
} else {
$docker_compose_url = "${base_url}/${version}/docker-compose-${facts['kernel']}-${facts['os']['hardware']}${file_extension}"
}

if $proxy != undef {
$proxy_opt = "--proxy ${proxy}"
if $docker::manage_package {
if $version and $ensure != 'absent' {
$package_ensure = $version
} else {
$proxy_opt = ''
$package_ensure = $ensure
}

if $facts['os']['family'] == 'windows' {
$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

$parameters = {
'proxy' => $proxy,
'docker_compose_url' => $docker_compose_url,
'docker_compose_location_versioned' => $docker_compose_location_versioned,
case $facts['os']['family'] {
'Debian': {
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
}

exec { "Install Docker Compose ${version}":
command => epp('docker/windows/download_docker_compose.ps1.epp', $parameters),
provider => powershell,
creates => $docker_compose_location_versioned,
}

file { $docker_compose_location:
ensure => 'link',
target => $docker_compose_location_versioned,
require => Exec["Install Docker Compose ${version}"],
}
} else {
if $curl_ensure {
stdlib::ensure_packages(['curl'])
}

exec { "Install Docker Compose ${version}":
path => '/usr/bin/',
cwd => '/tmp',
command => "curl -s -S -L ${proxy_opt} ${docker_compose_url} -o ${docker_compose_location_versioned}",
creates => $docker_compose_location_versioned,
require => Package['curl'],
'RedHat': {
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
}

file { $docker_compose_location_versioned:
owner => $file_owner,
mode => '0755',
seltype => 'container_runtime_exec_t',
require => Exec["Install Docker Compose ${version}"],
'Windows': {
fail('Docker compose is installed with docker machine on Windows')
}

file { $docker_compose_location:
ensure => 'link',
target => $docker_compose_location_versioned,
require => File[$docker_compose_location_versioned],
default: {
fail('This module only works on Debian, RedHat or Windows.')
}
}
} else {
file { $docker_compose_location_versioned:
ensure => absent,
}

file { $docker_compose_location:
ensure => absent,
}
}
}
7 changes: 1 addition & 6 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
$dns = undef
$dns_search = undef
$proxy = undef
$compose_base_url = 'https://github.com/docker/compose/releases/download'
$compose_symlink_name = 'docker-compose'
$compose_version = undef
$no_proxy = undef
$execdriver = undef
$storage_driver = undef
Expand Down Expand Up @@ -90,16 +89,12 @@
$docker_command = 'docker'

if ($facts['os']['family'] == 'windows') {
$compose_install_path = "${facts['docker_program_files_path']}/Docker"
$compose_version = '1.29.2'
$docker_ee_package_name = 'Docker'
$machine_install_path = "${facts['docker_program_files_path']}/Docker"
$tls_cacert = "${facts['docker_program_data_path']}/docker/certs.d/ca.pem"
$tls_cert = "${facts['docker_program_data_path']}/docker/certs.d/server-cert.pem"
$tls_key = "${facts['docker_program_data_path']}/docker/certs.d/server-key.pem"
} else {
$compose_install_path = '/usr/local/bin'
$compose_version = '1.29.2'
$docker_ee_package_name = 'docker-ee'
$machine_install_path = '/usr/local/bin'
$tls_cacert = '/etc/docker/tls/ca.pem'
Expand Down
39 changes: 1 addition & 38 deletions spec/classes/compose_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,6 @@
'with version => 1.7.0' => {
'version' => '1.7.0'
},
'when proxy is provided' => {
'version' => '1.7.0',
'proxy' => 'http://proxy.example.org:3128/'
},
'when proxy is not a http proxy' => {
'proxy' => 'this is not a URL'
},
'when proxy contains username and password' => {
'version' => '1.7.0',
'proxy' => 'http://user:[email protected]:3128/'
},
'when proxy IP is provided' => {
'version' => '1.7.0',
'proxy' => 'http://10.10.10.10:3128/'
},
'when base_url is provided' => {
'version' => '1.7.0',
'base_url' => 'http://example.org'
},
'when raw_url is provided' => {
'version' => '1.7.0',
'raw_url' => 'http://example.org'
}
}

describe 'docker::compose', type: :class do
Expand All @@ -57,12 +34,6 @@
params = {
'ensure' => 'present',
'version' => defaults['compose_version'],
'install_path' => defaults['compose_install_path'],
'symlink_name' => defaults['compose_symlink_name'],
'proxy' => :undef,
'base_url' => defaults['compose_base_url'],
'raw_url' => :undef,
'curl_ensure' => defaults['curl_ensure']
}.merge(local_params)

let(:facts) do
Expand All @@ -73,15 +44,7 @@
params
end

if title == 'when proxy is not a http proxy'
it 'raises an error for invalid proxy URL' do
expect(subject).to compile.and_raise_error(
%r{parameter 'proxy' expects an undef value or a match for Pattern},
)
end
else
include_examples 'compose', params, facts
end
include_examples 'compose', params, facts
end
end
end
Expand Down
99 changes: 12 additions & 87 deletions spec/shared_examples/compose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,95 +3,20 @@
shared_examples 'compose' do |_params, _facts|
ensure_value = _params['ensure']
version = _params['version']
install_path = _params['install_path']
symlink_name = _params['symlink_name']
proxy = _params['proxy']
base_url = _params['base_url']
raw_url = _params['raw_url']
curl_ensure = _params['curl_ensure']

if _facts[:os]['family'] == 'windows'
file_extension = '.exe'
file_owner = 'Administrator'
else
file_extension = ''
file_owner = 'root'
end

docker_compose_location = "#{install_path}/#{symlink_name}#{file_extension}"
docker_compose_location_versioned = "#{install_path}/docker-compose-#{version}#{file_extension}"

if ensure_value == 'present'
docker_compose_url = if raw_url == :undef
"#{base_url}/#{version}/docker-compose-#{_facts[:kernel]}-x86_64#{file_extension}"
else
raw_url
end

proxy_opt = if proxy == :undef
''
else
"--proxy #{proxy}"
end

if _facts[:os]['family'] == 'windows'
docker_download_command = "if (Invoke-WebRequest #{docker_compose_url} #{proxy_opt} -UseBasicParsing -OutFile \"#{docker_compose_location_versioned}\") { exit 0 } else { exit 1 }"

it {
expect(subject).to contain_exec("Install Docker Compose #{version}").with(
'provider' => 'powershell',
'creates' => docker_compose_location_versioned,
)

expect(subject).to contain_file(docker_compose_location).with(
'ensure' => 'link',
'target' => docker_compose_location_versioned,
).that_requires(
"Exec[Install Docker Compose #{version}]",
)
}
else
if curl_ensure
it {
expect(subject).to contain_package('curl')
}
end
if _params['manage_package']
ensure_value = if _params['version'] != :undef && _params['ensure'] != 'absent'
_params['version']
else
_params['ensure']
end

case _facts['os']['family']
when 'Debian', 'RedHat'
it {
expect(subject).to contain_exec("Install Docker Compose #{version}").with(
'path' => '/usr/bin/',
'cwd' => '/tmp',
'command' => "curl -s -S -L #{proxy_opt} #{docker_compose_url} -o #{docker_compose_location_versioned}",
'creates' => docker_compose_location_versioned,
).that_requires(
'Package[curl]',
)

expect(subject).to contain_file(docker_compose_location_versioned).with(
'owner' => file_owner,
'mode' => '0755',
).that_requires(
"Exec[Install Docker Compose #{version}]",
)

expect(subject).to contain_file(docker_compose_location).with(
'ensure' => 'link',
'target' => docker_compose_location_versioned,
).that_requires(
"File[#{docker_compose_location_versioned}]",
)
}
end
else

it {
expect(subject).to contain_file(docker_compose_location_versioned).with(
'ensure' => 'absent',
)

expect(subject).to contain_file(docker_compose_location).with(
'ensure' => 'absent',
)
}
is_expected.to contain_package('docker-compose-plugin').with(
ensure: ensure_value,
)
}
end
end