Defined Type: docker::services

Defined in:
manifests/services.pp

Summary

define that managers a Docker services

Overview

Parameters:

  • ensure (Enum[present,absent]) (defaults to: 'present')

    This ensures that the service is present or not.

  • image (Optional[Variant[String,Array]]) (defaults to: undef)

    The Docker image to spwan the service from.

  • detach (Boolean) (defaults to: true)

    Exit immediately instead of waiting for the service to converge (default true)

  • env (Array) (defaults to: [])

    Set environment variables

  • label (Array) (defaults to: [])

    Service labels. This used as metdata to configure constraints etc.

  • publish (Optional[Variant[String,Array]]) (defaults to: undef)

    Publish port(s) as node ports.

  • replicas (Optional[Variant[String,Array]]) (defaults to: undef)

    Number of tasks (containers per service)

  • tty (Boolean) (defaults to: false)

    Allocate a pseudo-TTY

  • user (Optional[Variant[String,Array]]) (defaults to: undef)

    Username or UID (format: <name|uid>)

  • workdir (Optional[Variant[String,Array]]) (defaults to: undef)

    Working directory inside the container

  • extra_params (Array) (defaults to: [])

    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

  • update (Boolean) (defaults to: false)

    This changes the docker command to docker service update, you must pass a service name with this option

  • scale (Boolean) (defaults to: false)

    This changes the docker command to docker service scale, this can only be used with service name and replicas

  • host_socket (Optional[Variant[String,Array]]) (defaults to: undef)

    This will allow the service to connect to the host linux socket.

  • registry_mirror (Optional[Variant[String,Array]]) (defaults to: undef)

    This will allow the service to set a registry mirror.

  • mounts (Optional[Variant[String,Array]]) (defaults to: undef)

    Allows attaching filesystem mounts to the service (specified as an array)

  • networks (Optional[Array]) (defaults to: undef)

    Allows attaching the service to networks (specified as an array)

  • command (Optional[Variant[String,Array]]) (defaults to: undef)

    Command to run on the container

  • create (Boolean) (defaults to: true)
  • service_name (Optional[Variant[String,Array]]) (defaults to: undef)


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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'manifests/services.pp', line 66

define docker::services (
  Enum[present,absent]            $ensure          = 'present',
  Boolean                         $create          = true,
  Boolean                         $update          = false,
  Boolean                         $scale           = false,
  Boolean                         $detach          = true,
  Boolean                         $tty             = false,
  Array                           $env             = [],
  Array                           $label           = [],
  Array                           $extra_params    = [],
  Optional[Variant[String,Array]] $image           = undef,
  Optional[Variant[String,Array]] $service_name    = undef,
  Optional[Variant[String,Array]] $publish         = undef,
  Optional[Variant[String,Array]] $replicas        = undef,
  Optional[Variant[String,Array]] $user            = undef,
  Optional[Variant[String,Array]] $workdir         = undef,
  Optional[Variant[String,Array]] $host_socket     = undef,
  Optional[Variant[String,Array]] $registry_mirror = undef,
  Optional[Variant[String,Array]] $mounts          = undef,
  Optional[Array]                 $networks        = undef,
  Optional[Variant[String,Array]] $command         = 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 $facts['os']['family'] == 'windows' {
    $exec_environment = "PATH=${facts['docker_program_files_path']}/Docker/;${facts['docker_systemroot']}/System32/"
    $exec_path        = ["${facts['docker_program_files_path']}/Docker/",]
    $exec_provider    = 'powershell'
    $exec_timeout     = 3000
  } else {
    $exec_environment = 'HOME=/root'
    $exec_path        = ['/bin', '/usr/bin',]
    $exec_provider    = undef
    $exec_timeout     = 0
  }

  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,
        registry_mirror => $registry_mirror,
        mounts          => $mounts,
        networks        => $networks,
        command         => $command,
      }
    )

    $exec_create   = "${docker_command} create --name ${docker_service_create_flags}"
    $unless_create = "docker service ps ${service_name}"

    exec { "${title} docker service create":
      command     => $exec_create,
      environment => $exec_environment,
      path        => $exec_path,
      timeout     => $exec_timeout,
      provider    => $exec_provider,
      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,
        registry_mirror => $registry_mirror,
      }
    )

    $exec_update = "${docker_command} update ${docker_service_flags}"

    exec { "${title} docker service update":
      command     => $exec_update,
      environment => $exec_environment,
      path        => $exec_path,
      provider    => $exec_provider,
      timeout     => $exec_timeout,
    }
  }

  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 => $exec_environment,
      path        => $exec_path,
      timeout     => $exec_timeout,
      provider    => $exec_provider,
    }
  }

  if $ensure == 'absent' {
    exec { "${title} docker service remove":
      command  => "docker service rm ${service_name}",
      onlyif   => "docker service ps ${service_name}",
      path     => $exec_path,
      provider => $exec_provider,
      timeout  => $exec_timeout,
    }
  }
}