Defined Type: docker::run
- Defined in:
- manifests/run.pp
Overview
This define manages and runs a container based on the given docker image Derived from github.com/garethr/garethr-docker/blob/master/manifests/run.pp
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 |
# File 'manifests/run.pp', line 6
define docker::run (
String[1] $ensure = 'running',
Variant[Undef,String] $image = '',
String $command = '',
String $username = '',
Variant[Undef,String] $repository = $title,
Variant[Undef,String] $repository_tag = undef,
String $container_name = $title,
Pattern[/service|command/] $run_mode = 'command',
String $run_options = '',
String $service_prefix = 'docker-',
Boolean $remove_container_on_start = true,
Boolean $remove_container_on_stop = true,
Boolean $remove_volume_on_start = true,
Boolean $remove_volume_on_stop = true,
Variant[Undef,Array] $exec_environment = undef,
Variant[Boolean,Pattern[/on_failure/]] $exec_logoutput = 'on_failure',
Variant[Undef,String] $init_template = undef,
Boolean $mount_data_dir = true,
Boolean $mount_log_dir = true,
Hash $settings = { },
) {
include ::docker
$sanitised_title = $title
# $sanitised_title = regsubst($title, '[^0-9A-Za-z.\-]', '-', 'G')
$username_prefix = $username ? {
'' => $::docker::username ? {
'' => '',
default => "${::docker::username}/",
},
default => "${username}/",
}
$title_elements = split ($title, '::')
$app = $title_elements[0]
# $tp_app_settings = tp_lookup($app,'settings',$::docker::tinydata_module,'merge')
# $app_settings = $tp_app_settings + $settings
$real_image = $image ? {
'' => "${username_prefix}/${repository}:${repository_tag}",
default => $image,
}
if $run_mode == 'command' {
Exec {
path => '/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin',
timeout => 3000,
environment => $exec_environment,
logoutput => $exec_logoutput,
}
$cidfile = "/var/run/${service_prefix}${sanitised_title}.cid"
$exec_command = $ensure ? {
'running' => "docker run -d ${run_options} --name ${sanitised_title} --cidfile=${cidfile} ${real_image} ${command}",
'present' => "docker run -d ${run_options} --name ${sanitised_title} --cidfile=${cidfile} ${real_image} ${command}",
'stopped' => "docker stop ${sanitised_title}",
'absent' => "docker stop ${sanitised_title} ; docker rm ${sanitised_title}",
}
$exec_unless = $ensure ? {
'running' => "docker ps --no-trunc | grep `cat ${cidfile}`",
'present' => "docker ps --no-trunc | grep `cat ${cidfile}`",
'stopped' => "docker ps --no-trunc | grep `cat ${cidfile}` || true",
'absent' => "docker ps --no-trunc | grep `cat ${cidfile}` || true",
}
exec { "docker run ${sanitised_title}":
command => $exec_command,
unless => $exec_unless,
}
}
if $run_mode == 'service' {
$service_ensure = $ensure ? {
'absent' => 'stopped',
false => 'stopped',
default => 'running',
}
$service_enable = $ensure ? {
'absent' => false,
false => false,
default => true,
}
case $::docker::module_settings['init_system'] {
'upstart': {
$initscript_file_path = "/etc/init/${service_prefix}${sanitised_title}.conf"
$default_template = 'docker/run/upstart.erb'
$init_file_mode = '0644'
$service_provider = 'upstart'
}
'systemd': {
$initscript_file_path = "/etc/systemd/system/${service_prefix}${sanitised_title}.service"
$default_template = 'docker/run/systemd.erb'
$init_file_mode = '0644'
$service_provider = 'systemd'
}
'sysvinit': {
$initscript_file_path = "/etc/init.d/${service_prefix}${sanitised_title}"
$default_template = 'docker/run/sysvinit.erb'
$init_file_mode = '0755'
$service_provider = undef
}
}
file { $initscript_file_path:
ensure => $ensure,
content => template(pick($init_template,$default_template)),
mode => $init_file_mode,
notify => Service["docker-${app}"],
}
service { "${service_prefix}${sanitised_title}":
ensure => $service_ensure,
enable => $service_enable,
provider => $service_provider,
require => Service[$::docker::module_settings['service_name']],
}
}
}
|