Skip to content

Commit 8476dfc

Browse files
authored
Merge pull request #823 from canihavethisone/main
Add tmpdir option to docker_compose
2 parents 3d01a5d + e6bffd5 commit 8476dfc

File tree

4 files changed

+95
-63
lines changed

4 files changed

+95
-63
lines changed

README.md

+66-63
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,20 @@ Docker provides a enterprise addition of the [Docker Engine](https://www.docker.
7474

7575
```puppet
7676
class { 'docker':
77-
docker_ee => true,
77+
docker_ee => true,
7878
docker_ee_source_location => 'https://<docker_ee_repo_url>',
79-
docker_ee_key_source => 'https://<docker_ee_key_source_url>',
80-
docker_ee_key_id => '<key id>',
79+
docker_ee_key_source => 'https://<docker_ee_key_source_url>',
80+
docker_ee_key_id => '<key id>',
8181
}
8282
```
8383

8484
To install Docker EE on RHEL/CentOS:
8585

8686
```puppet
8787
class { 'docker':
88-
docker_ee => true,
88+
docker_ee => true,
8989
docker_ee_source_location => 'https://<docker_ee_repo_url>',
90-
docker_ee_key_source => 'https://<docker_ee_key_source_url>',
90+
docker_ee_key_source => 'https://<docker_ee_key_source_url>',
9191
}
9292
```
9393

@@ -108,21 +108,21 @@ To use the CE packages, add the following code to the manifest file:
108108
```puppet
109109
class { 'docker':
110110
use_upstream_package_source => false,
111-
repo_opt => '',
111+
repo_opt => '',
112112
}
113113
```
114114

115115
By default, the Docker daemon binds to a unix socket at `/var/run/docker.sock`. To change this parameter and update the binding parameter to a tcp socket, add the following code to the manifest file:
116116

117117
```puppet
118118
class { 'docker':
119-
tcp_bind => ['tcp://127.0.0.1:2375'],
120-
socket_bind => 'unix:///var/run/docker.sock',
121-
ip_forward => true,
122-
iptables => true,
123-
ip_masq => true,
124-
bip => '192.168.1.1/24',
125-
fixed_cidr => '192.168.1.144/28',
119+
tcp_bind => ['tcp://127.0.0.1:2375'],
120+
socket_bind => 'unix:///var/run/docker.sock',
121+
ip_forward => true,
122+
iptables => true,
123+
ip_masq => true,
124+
bip => '192.168.1.1/24',
125+
fixed_cidr => '192.168.1.144/28',
126126
}
127127
```
128128

@@ -132,7 +132,7 @@ The default group ownership of the Unix control socket differs based on OS. For
132132

133133
```puppet
134134
class {'docker':
135-
socket_group => 'root',
135+
socket_group => 'root',
136136
socket_override => true,
137137
}
138138
```
@@ -153,11 +153,11 @@ When setting up TLS, upload the related files (CA certificate, server certificat
153153

154154
```puppet
155155
class { 'docker':
156-
tcp_bind => ['tcp://0.0.0.0:2376'],
157-
tls_enable => true,
158-
tls_cacert => '/etc/docker/tls/ca.pem',
159-
tls_cert => '/etc/docker/tls/cert.pem',
160-
tls_key => '/etc/docker/tls/key.pem',
156+
tcp_bind => ['tcp://0.0.0.0:2376'],
157+
tls_enable => true,
158+
tls_cacert => '/etc/docker/tls/ca.pem',
159+
tls_cert => '/etc/docker/tls/cert.pem',
160+
tls_key => '/etc/docker/tls/key.pem',
161161
}
162162
```
163163

@@ -177,7 +177,7 @@ To track the latest version of Docker, add the following code to the manifest fi
177177

178178
```puppet
179179
class { 'docker':
180-
version => 'latest',
180+
version => latest,
181181
}
182182
```
183183

@@ -477,18 +477,18 @@ To enable the restart of an unhealthy container, add the following code to the m
477477

478478
```puppet
479479
docker::run { 'helloworld':
480-
image => 'base',
481-
command => 'command',
482-
health_check_cmd => '<command_to_execute_to_check_your_containers_health>',
483-
restart_on_unhealthy => true,
480+
image => 'base',
481+
command => 'command',
482+
health_check_cmd => '<command_to_execute_to_check_your_containers_health>',
483+
restart_on_unhealthy => true,
484484
health_check_interval => '<time between running docker healthcheck>',
485485
```
486486

487487
To run command on Windows 2016 requires the `restart` parameter to be set:
488488

489489
```puppet
490490
docker::run { 'helloworld':
491-
image => 'microsoft/nanoserver',
491+
image => 'microsoft/nanoserver',
492492
command => 'ping 127.0.0.1 -t',
493493
restart => 'always'
494494
```
@@ -611,7 +611,7 @@ To install Docker Compose, add the following code to the manifest file:
611611

612612
```puppet
613613
class {'docker::compose':
614-
ensure => present,
614+
ensure => present,
615615
version => '1.9.0',
616616
}
617617
```
@@ -631,22 +631,25 @@ Specify the `file` resource to add a Compose file to the machine you have Puppet
631631
```puppet
632632
docker_compose { 'test':
633633
compose_files => ['/tmp/docker-compose.yml'],
634-
ensure => present,
634+
ensure => present,
635635
}
636636
```
637637

638638
Puppet automatically runs Compose because the relevant Compose services aren't running. If required, include additional options such as enabling experimental features and scaling rules.
639639

640-
In the example below, Puppet runs Compose when the number of containers specified for a service doesn't match the scale values.
640+
Additionally, the TMPDIR environment variable can optionally be set when docker_compose runs if you want Puppet to manage the environment variable within the scope of the resource. This is effective when noexec is set on the default /tmp dir, however you must ensure that the target directory exists as the resource will not create it.
641+
642+
In the example below, Puppet runs Compose when the number of containers specified for a service doesn't match the scale values. The optional tmpdir parameter is also specified.
641643

642644
```puppet
643645
docker_compose { 'test':
644646
compose_files => ['/tmp/docker-compose.yml'],
645-
ensure => present,
646-
scale => {
647+
ensure => present,
648+
scale => {
647649
'compose_test' => 2,
648650
},
649-
options => ['--x-networking']
651+
tmpdir => '/usr/local/share/tmp_docker',
652+
options => ['--x-networking']
650653
}
651654
```
652655

@@ -670,10 +673,10 @@ To deploy the stack, add the following code to the manifest file:
670673

671674
```puppet
672675
docker::stack { 'yourapp':
673-
ensure => present,
674-
stack_name => 'yourapp',
676+
ensure => present,
677+
stack_name => 'yourapp',
675678
compose_files => ['/tmp/docker-compose.yaml'],
676-
require => [Class['docker'], File['/tmp/docker-compose.yaml']],
679+
require => [Class['docker'], File['/tmp/docker-compose.yaml']],
677680
}
678681
```
679682

@@ -685,11 +688,11 @@ To deploy the stack, add the following code to the manifest file.
685688

686689
```puppet
687690
docker::stack { 'yourapp':
688-
ensure => present,
689-
stack_name => 'yourapp',
690-
compose_files => ['/tmp/docker-compose.yaml'],
691+
ensure => present,
692+
stack_name => 'yourapp',
693+
compose_files => ['/tmp/docker-compose.yaml'],
691694
with_registry_auth => true,
692-
require => [Class['docker'], File['/tmp/docker-compose.yaml']],
695+
require => [Class['docker'], File['/tmp/docker-compose.yaml']],
693696
}
694697
```
695698

@@ -698,8 +701,8 @@ To use the equivalent type and provider, use the following in your manifest file
698701
```puppet
699702
docker_stack { 'test':
700703
compose_files => ['/tmp/docker-compose.yml'],
701-
ensure => present,
702-
up_args => '--with-registry-auth',
704+
ensure => present,
705+
up_args => '--with-registry-auth',
703706
}
704707
```
705708

@@ -717,7 +720,7 @@ To install Docker Machine, add the following code to the manifest file:
717720

718721
```puppet
719722
class {'docker::machine':
720-
ensure => present,
723+
ensure => present,
721724
version => '1.16.1',
722725
}
723726
```
@@ -834,14 +837,14 @@ To create a Docker service, add the following code to the manifest file:
834837

835838
```puppet
836839
docker::services {'redis':
837-
create => true,
840+
create => true,
838841
service_name => 'redis',
839-
image => 'redis:latest',
840-
publish => '6379:639',
841-
replicas => '5',
842-
mounts => ['type=bind,source=/etc/my-redis.conf,target=/etc/redis/redis.conf,readonly'],
842+
image => 'redis:latest',
843+
publish => '6379:639',
844+
replicas => '5',
845+
mounts => ['type=bind,source=/etc/my-redis.conf,target=/etc/redis/redis.conf,readonly'],
843846
extra_params => ['--update-delay 1m', '--restart-window 30s'],
844-
command => ['redis-server', '--appendonly', 'yes'],
847+
command => ['redis-server', '--appendonly', 'yes'],
845848
}
846849
```
847850

@@ -851,10 +854,10 @@ To update the service, add the following code to the manifest file:
851854

852855
```puppet
853856
docker::services {'redis_update':
854-
create => false,
855-
update => true,
857+
create => false,
858+
update => true,
856859
service_name => 'redis',
857-
replicas => '3',
860+
replicas => '3',
858861
}
859862
```
860863

@@ -864,10 +867,10 @@ To scale a service, add the following code to the manifest file:
864867

865868
```puppet
866869
docker::services {'redis_scale':
867-
create => false,
868-
scale => true,
870+
create => false,
871+
scale => true,
869872
service_name => 'redis',
870-
replicas => '10',
873+
replicas => '10',
871874
}
872875
```
873876

@@ -877,8 +880,8 @@ To remove a service, add the following code to the manifest file:
877880

878881
```puppet
879882
docker::services {'redis':
880-
create => false,
881-
ensure => 'absent',
883+
create => false,
884+
ensure => 'absent',
882885
service_name => 'redis',
883886
}
884887
```
@@ -962,14 +965,14 @@ Within the context of a running container, the docker module supports arbitrary
962965

963966
```puppet
964967
docker::exec { 'cron_allow_root':
965-
detach => true,
966-
container => 'mycontainer',
967-
command => '/bin/echo root >> /usr/lib/cron/cron.allow',
968-
onlyif => 'running',
969-
tty => true,
970-
env => ['FOO=BAR', 'FOO2=BAR2'],
971-
unless => 'grep root /usr/lib/cron/cron.allow 2>/dev/null',
972-
refreshonly => true,
968+
detach => true,
969+
container => 'mycontainer',
970+
command => '/bin/echo root >> /usr/lib/cron/cron.allow',
971+
onlyif => 'running',
972+
tty => true,
973+
env => ['FOO=BAR', 'FOO2=BAR2'],
974+
unless => 'grep root /usr/lib/cron/cron.allow 2>/dev/null',
975+
refreshonly => true,
973976
}
974977
```
975978

lib/puppet/provider/docker_compose/ruby.rb

+10
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@
1717
environment(HOME: '/root')
1818
end
1919

20+
def set_tmpdir
21+
return unless resource[:tmpdir]
22+
# Check if the the tmpdir target exists
23+
Puppet.warning("#{resource[:tmpdir]} (defined as docker_compose tmpdir) does not exist") unless Dir.exist?(resource[:tmpdir])
24+
# Set TMPDIR environment variable only if defined among resources and exists
25+
ENV['TMPDIR'] = resource[:tmpdir] if Dir.exist?(resource[:tmpdir])
26+
end
27+
2028
def exists?
2129
Puppet.info("Checking for compose project #{name}")
2230
compose_services = {}
2331
compose_containers = []
2432

33+
set_tmpdir
34+
2535
# get merged config using docker-compose config
2636
args = [compose_files, '-p', name, 'config'].insert(3, resource[:options]).compact
2737
compose_output = YAML.safe_load(execute([command(:dockercompose)] + args, combine: false))

lib/puppet/type/docker_compose.rb

+15
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,19 @@ def refresh
4747
isnamevar
4848
desc 'The name of the project'
4949
end
50+
51+
newparam(:tmpdir) do
52+
desc "Override the temporary directory used by docker-compose.
53+
54+
This property is useful when the /tmp directory has been mounted
55+
with the noexec option. Or is otherwise being prevented It allows the module consumer to redirect
56+
docker-composes temporary files to a known directory.
57+
58+
The directory passed to this property must exist and be accessible
59+
by the user that is executing the puppet agent.
60+
"
61+
validate do |value|
62+
raise _('tmpdir should be a String') unless value.is_a? String
63+
end
64+
end
5065
end

spec/unit/lib/puppet/type/docker_compose_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@
4444
it 'requires scale to be a hash' do
4545
expect(compose).to require_hash_for('scale')
4646
end
47+
48+
it 'requires tmpdir to be a string' do
49+
expect(compose).to require_string_for('tmpdir')
50+
end
4751
end

0 commit comments

Comments
 (0)