Skip to content

Commit

Permalink
Replace 'docker-compose' with the docker-compose-plugin
Browse files Browse the repository at this point in the history
These changes include updates to the docker compose related
code and tests. They also resolve a small lint error in other
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

Run pdk validate -a to resolve lint problems

Use ensure_resource to manage the docker-compose-plugin

This is likely the expected behavior for this module

Use the package resource directly
  • Loading branch information
davidphay authored and nathanlcarlson committed Apr 16, 2024
1 parent 8f4b3d9 commit 97efd64
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 255 deletions.
30 changes: 14 additions & 16 deletions lib/puppet/provider/docker_compose/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

has_command(:docker, 'docker')

has_command(:dockercompose, 'docker-compose')

def set_tmpdir
return unless resource[:tmpdir]

Expand All @@ -28,8 +26,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 +74,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
130 changes: 27 additions & 103 deletions manifests/compose.pp
Original file line number Diff line number Diff line change
Expand Up @@ -7,118 +7,42 @@
# @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': {
package { 'docker-compose-plugin':
ensure => $package_ensure,
require => defined(bool2str($docker::use_upstream_package_source)) ? {
true => Apt::Source['docker'],
false => undef,
},
}
}

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': {
package { 'docker-compose-plugin':
ensure => $package_ensure,
require => defined(bool2str($docker::use_upstream_package_source)) ? {
true => Yumrepo['docker'],
false => undef,
},
}
}

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
41 changes: 2 additions & 39 deletions spec/classes/compose_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,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:password@proxy.example.org: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'
}
}

Expand All @@ -56,13 +33,7 @@
context title do
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']
'version' => defaults['compose_version']
}.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
Loading

0 comments on commit 97efd64

Please sign in to comment.