forked from puppetlabs/puppetlabs-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswarm.pp
148 lines (136 loc) · 4.28 KB
/
swarm.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
# == Define: docker::swarm
#
# A define that managers a Docker Swarm Mode cluster
#
# == Paramaters
#
# [*ensure*]
# This ensures that the cluster is present or not.
# Defaults to present
# Note this forcefully removes a node from the cluster. Make sure all worker nodes
# have been removed before managers
#
# [*init*]
# This creates the first worker node for a new cluster.
# Set init to true to create a new cluster
# Defaults to false
#
# [*join*]
# This adds either a worker or manger node to the cluster.
# The role of the node is defined by the join token.
# Set to true to join the cluster
# Defaults to false
#
# [*advertise_addr*]
# The address that your node will advertise to the cluster for raft.
# On multihomed servers this flag must be passed
# Defaults to undef
#
# [*autolock*]
# Enable manager autolocking (requiring an unlock key to start a stopped manager)
# Defaults to undef
#
# [*cert_expiry*]
# Validity period for node certificates (ns|us|ms|s|m|h) (default 2160h0m0s)
# defaults to undef
#
# [*dispatcher_heartbeat*]
# Dispatcher heartbeat period (ns|us|ms|s|m|h) (default 5s)
# Defaults to undef
#
# [*external_ca*]
# Specifications of one or more certificate signing endpoints
# Defaults to undef
#
# [*force_new_cluster*]
# Force create a new cluster from current state
# Defaults to false
#
# [*listen_addr*]
# The address that your node will listen to the cluster for raft.
# On multihomed servers this flag must be passed
# Defaults to undef
#
# [*max_snapshots*]
# Number of additional Raft snapshots to retain
# Defaults to undef
#
# [*snapshot_interval*]
# Number of log entries between Raft snapshots (default 10000)
# Defaults to undef
#
# [*token*]
# The authentication token to join the cluster. The token also defines the type of
# node (worker or manager)
# Defaults to undef
#
# [*manager_ip*]
# The ip address of a manager node to join the cluster.
# Defaults to undef
#
define docker::swarm(
Optional[Pattern[/^present$|^absent$/]] $ensure = 'present',
Optional[Boolean] $init = false,
Optional[Boolean] $join = false,
Optional[String] $advertise_addr = undef,
Optional[Boolean] $autolock = false,
Optional[String] $cert_expiry = undef,
Optional[String] $dispatcher_heartbeat = undef,
Optional[String] $external_ca = undef,
Optional[Boolean] $force_new_cluster = false,
Optional[String] $listen_addr = undef,
Optional[String] $max_snapshots = undef,
Optional[String] $snapshot_interval = undef,
Optional[String] $token = undef,
Optional[String] $manager_ip = undef,
){
include docker::params
$docker_command = "${docker::params::docker_command} swarm"
if $init {
$docker_swarm_init_flags = docker_swarm_init_flags({
init => $init,
advertise_addr => $advertise_addr,
autolock => $autolock,
cert_expiry => $cert_expiry,
dispatcher_heartbeat => $dispatcher_heartbeat,
external_ca => $external_ca,
force_new_cluster => $force_new_cluster,
listen_addr => $listen_addr,
max_snapshots => $max_snapshots,
snapshot_interval => $snapshot_interval,
})
$exec_init = "${docker_command} ${docker_swarm_init_flags}"
$unless_init = 'docker info | grep -w "Swarm: active"'
exec { 'Swarm init':
command => $exec_init,
environment => 'HOME=/root',
path => ['/bin', '/usr/bin'],
timeout => 0,
unless => $unless_init,
}
}
if $join {
$docker_swarm_join_flags = docker_swarm_join_flags({
join => $join,
advertise_addr => $advertise_addr,
listen_addr => $listen_addr,
token => $token,
})
$exec_join = "${docker_command} ${docker_swarm_join_flags} ${manager_ip}"
$unless_join = 'docker info | grep -w "Swarm: active"'
exec { 'Swarm join':
command => $exec_join,
environment => 'HOME=/root',
path => ['/bin', '/usr/bin'],
timeout => 0,
unless => $unless_join,
}
}
if $ensure == 'absent' {
exec { 'Leave swarm':
command => 'docker swarm leave --force',
onlyif => 'docker info | grep -w "Swarm: active"',
path => ['/bin', '/usr/bin'],
}
}
}