Defined Type: docker::image

Defined in:
manifests/image.pp

Summary

Module to install an up-to-date version of a Docker image from the registry

Overview

Parameters:

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

    Whether you want the image present or absent.

  • image (Optional[Pattern[/^[\S]*$/]]) (defaults to: $title)

    If you want the name of the image to be different from the name of the puppet resource you can pass a value here.

  • image_tag (Optional[String]) (defaults to: undef)

    If you want a specific tag of the image to be installed

  • image_digest (Optional[String]) (defaults to: undef)

    If you want a specific content digest of the image to be installed

  • docker_file (Optional[String]) (defaults to: undef)

    If you want to add a docker image from specific docker file

  • docker_tar (Optional[String]) (defaults to: undef)

    If you want to load a docker image from specific docker tar

  • force (Boolean) (defaults to: false)
  • docker_dir (Optional[String]) (defaults to: undef)


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
178
179
180
181
182
183
184
185
# File 'manifests/image.pp', line 28

define docker::image (
  Enum[present,absent,latest]   $ensure       = 'present',
  Optional[Pattern[/^[\S]*$/]]  $image        = $title,
  Optional[String]              $image_tag    = undef,
  Optional[String]              $image_digest = undef,
  Boolean                       $force        = false,
  Optional[String]              $docker_file  = undef,
  Optional[String]              $docker_dir   = undef,
  Optional[String]              $docker_tar   = undef,
) {
  include docker::params

  $docker_command = $docker::params::docker_command

  if $facts['os']['family'] == 'windows' {
    $update_docker_image_template = 'docker/windows/update_docker_image.ps1.epp'
    $update_docker_image_path     = "${facts['docker_user_temp_path']}/update_docker_image.ps1"
    $exec_environment             = "PATH=${facts['docker_program_files_path']}/Docker/"
    $exec_timeout                 = 3000
    $update_docker_image_owner    = undef
    $exec_path                    = ["${facts['docker_program_files_path']}/Docker/",]
    $exec_provider                = 'powershell'
  } else {
    $update_docker_image_template = 'docker/update_docker_image.sh.epp'
    $update_docker_image_path     = '/usr/local/bin/update_docker_image.sh'
    $update_docker_image_owner    = 'root'
    $exec_environment             = 'HOME=/root'
    $exec_path                    = ['/bin', '/usr/bin',]
    $exec_timeout                 = 0
    $exec_provider                = undef
  }

  $parameters = {
    'docker_command' => $docker_command,
  }
  # Wrapper used to ensure images are up to date
  ensure_resource('file', $update_docker_image_path,
    {
      ensure  => $docker::params::ensure,
      owner   => $update_docker_image_owner,
      group   => $update_docker_image_owner,
      mode    => '0555',
      content => epp($update_docker_image_template, $parameters),
    }
  )

  if ($docker_file) and ($docker_tar) {
    fail('docker::image must not have both $docker_file and $docker_tar set')
  }

  if ($docker_dir) and ($docker_tar) {
    fail('docker::image must not have both $docker_dir and $docker_tar set')
  }

  if ($image_digest) and ($docker_file) {
    fail('docker::image must not have both $image_digest and $docker_file set')
  }

  if ($image_digest) and ($docker_dir) {
    fail('docker::image must not have both $image_digest and $docker_dir set')
  }

  if ($image_digest) and ($docker_tar) {
    fail('docker::image must not have both $image_digest and $docker_tar set')
  }

  if $force {
    $image_force = '-f '
  } else {
    $image_force = ''
  }

  if $image_tag {
    $image_arg     = "${image}:${image_tag}"
    $image_remove  = "${docker_command} rmi ${image_force}${image}:${image_tag}"
    $image_find    = "${docker_command} images -q ${image}:${image_tag}"
  } elsif $image_digest {
    $image_arg     = "${image}@${image_digest}"
    $image_remove  = "${docker_command} rmi ${image_force}${image}:${image_digest}"
    $image_find    = "${docker_command} images -q ${image}@${image_digest}"
  } else {
    $image_arg     = $image
    $image_remove  = "${docker_command} rmi ${image_force}${image}"
    $image_find    = "${docker_command} images -q ${image}"
  }

  if $facts['os']['family'] == 'windows' {
    $_image_find = "If (-not (${image_find}) ) { Exit 1 }"
  } else {
    $_image_find = "${image_find} | grep ."
  }

  if ($docker_dir) and ($docker_file) {
    $image_install = "${docker_command} build -t ${image_arg} -f ${docker_file} ${docker_dir}"
  } elsif $docker_dir {
    $image_install = "${docker_command} build -t ${image_arg} ${docker_dir}"
  } elsif $docker_file {
    if $facts['os']['family'] == windows {
      $image_install = "Get-Content ${docker_file} -Raw | ${docker_command} build -t ${image_arg} -"
    } else {
      $image_install = "${docker_command} build -t ${image_arg} - < ${docker_file}"
    }
  } elsif $docker_tar {
    $image_install = "${docker_command} load -i ${docker_tar}"
  } else {
    if $facts['os']['family'] == 'windows' {
      $image_install = "& ${update_docker_image_path} -DockerImage ${image_arg}"
    } else {
      $image_install = "${update_docker_image_path} ${image_arg}"
    }
  }

  if $ensure == 'absent' {
    exec { $image_remove:
      path        => $exec_path,
      environment => $exec_environment,
      onlyif      => $_image_find,
      provider    => $exec_provider,
      timeout     => $exec_timeout,
      logoutput   => true,
    }
  } elsif $ensure == 'latest' or $image_tag == 'latest' or $force {
    notify { "Check if image ${image_arg} is in-sync":
      noop => false,
    }
    ~> exec { $image_install:
      environment => $exec_environment,
      path        => $exec_path,
      timeout     => $exec_timeout,
      returns     => ['0', '2'],
      require     => File[$update_docker_image_path],
      provider    => $exec_provider,
      logoutput   => true,
    }
    ~> exec { "echo 'Update of ${image_arg} complete'":
      environment => $exec_environment,
      path        => $exec_path,
      timeout     => $exec_timeout,
      require     => File[$update_docker_image_path],
      provider    => $exec_provider,
      logoutput   => true,
      refreshonly => true,
    }
  } elsif $ensure == 'present' {
    exec { $image_install:
      unless      => $_image_find,
      environment => $exec_environment,
      path        => $exec_path,
      timeout     => $exec_timeout,
      returns     => ['0', '2'],
      require     => File[$update_docker_image_path],
      provider    => $exec_provider,
      logoutput   => true,
    }
  }

  Docker::Image <| title == $title |>
}