forked from puppetlabs/puppetlabs-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.pp
177 lines (163 loc) · 5.07 KB
/
services.pp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# == Define: docker::services
#
# A define that managers a Docker services
#
# == Paramaters
#
# [*ensure*]
# This ensures that the service is present or not.
# Defaults to present
#
# [*image*]
# The Docker image to spwan the service from.
# Defualts to undef
#
# [*detach*]
# Exit immediately instead of waiting for the service to converge (default true)
# Defaults to true
#
# [*env*]
# Set environment variables
# Defaults to []
#
# [*label*]
# Service labels.
# This used as metdata to configure constraints etc.
# Defaults to []
#
# [*publish*]
# Publish a port as a node port.
# Defaults to undef
#
# [*replicas*]
# Number of tasks (containers per service)
# defaults to undef
#
# [*tty*]
# Allocate a pseudo-TTY
# Defaults to false
#
# [*user*]
# Username or UID (format: <name|uid>[:<group|gid>])
# Defaults to undef
#
# [*workdir*]
# Working directory inside the container
# Defaults to false
#
# [*extra_params*]
# Allows you to pass any other flag that the Docker service create supports.
# This must be passed as an array. See docker service create --help for all options
# defaults to []
#
# [*update*]
# This changes the docker command to
# docker service update, you must pass a service name with this option
#
# [*scale*]
# This changes the docker command to
# docker service scale, this can only be used with service name and
# replicas
#
# [*host_socket*]
# This will allow the service to connect to the host linux socket.
# defaults to undef
#
define docker::services(
Optional[Pattern[/^present$|^absent$/]] $ensure = 'present',
Optional[Boolean] $create = true,
Optional[Boolean] $update = false,
Optional[Boolean] $scale = false,
Optional[Boolean] $detach = true,
Optional[Boolean] $tty = false,
Optional[Array] $env = [],
Optional[Array] $label = [],
Optional[Array] $extra_params = [],
Variant[String,Array,Undef] $image = undef,
Variant[String,Array,Undef] $service_name = undef,
Variant[String,Array,Undef] $publish = undef,
Variant[String,Array,Undef] $replicas = undef,
Variant[String,Array,Undef] $user = undef,
Variant[String,Array,Undef] $workdir = undef,
Variant[String,Array,Undef] $host_socket = undef,
){
include docker::params
$docker_command = "${docker::params::docker_command} service"
if $ensure == 'absent' {
if $update {
fail('When removing a service you can not update it.')
}
if $scale {
fail('When removing a service you can not update it.')
}
}
if $create {
$docker_service_create_flags = docker_service_flags({
detach => $detach,
env => any2array($env),
service_name => $service_name,
label => any2array($label),
publish => $publish,
replicas => $replicas,
tty => $tty,
user => $user,
workdir => $workdir,
extra_params => any2array($extra_params),
image => $image,
host_socket => $host_socket,
})
$exec_create = "${docker_command} create --name ${docker_service_create_flags}"
$unless_create = "docker service ls | grep -w ${service_name}"
exec { "${title} docker service create":
command => $exec_create,
environment => 'HOME=/root',
path => ['/bin', '/usr/bin'],
timeout => 0,
unless => $unless_create,
}
}
if $update {
$docker_service_flags = docker_service_flags({
detach => $detach,
env => any2array($env),
service_name => $service_name,
label => any2array($label),
publish => $publish,
replicas => $replicas,
tty => $tty,
user => $user,
workdir => $workdir,
extra_params => any2array($extra_params),
image => $image,
host_socket => $host_socket,
})
$exec_update = "${docker_command} update ${docker_service_flags}"
exec { "${title} docker service update":
command => $exec_update,
environment => 'HOME=/root',
path => ['/bin', '/usr/bin'],
timeout => 0,
}
}
if $scale {
$docker_service_flags = docker_service_flags({
service_name => $service_name,
replicas => $replicas,
extra_params => any2array($extra_params),
})
$exec_scale = "${docker_command} scale ${service_name}=${replicas}"
exec { "${title} docker service scale":
command => $exec_scale,
environment => 'HOME=/root',
path => ['/bin', '/usr/bin'],
timeout => 0,
}
}
if $ensure == 'absent' {
exec { "${title} docker service remove":
command => "docker service rm ${service_name}",
onlyif => "docker service ls | grep -w ${service_name}",
path => ['/bin', '/usr/bin'],
}
}
}