-
Notifications
You must be signed in to change notification settings - Fork 21
/
dockerize.pp
127 lines (106 loc) · 3.82 KB
/
dockerize.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
# @define tp::dockerize
#
# This define dockerizes an application.
# It can:
# - Create a dockerfile based on tinydata (default: false)
# - Build the relevant image (default:false)
# - Push the image to Docker Hub (default:false)
# - Run the image from the Docker Hub (default:true)
#
define tp::dockerize (
String[1] $ensure = 'present',
Variant[Undef,String] $template = 'tp/dockerize/Dockerfile.erb',
Variant[Undef,String] $init_template = 'tp/dockerize/init.erb',
String[1] $workdir = '/var/tmp',
String[1] $username = 'example42',
String[1] $os = downcase($::operatingsystem),
String[1] $osversion = $::operatingsystemmajrelease,
Variant[Undef,String] $maintainer = undef,
Variant[Undef,String] $from = undef,
Variant[Undef,String] $repository = undef,
Variant[Undef,String] $repository_tag = undef,
Boolean $run = true,
Boolean $create = false,
Boolean $build = false,
Boolean $push = false,
Variant[Undef,Array] $exec_environment = undef,
String $build_options = '',
Boolean $mount_data_dir = true,
Boolean $mount_log_dir = true,
Hash $settings_hash = {},
String[1] $data_module = 'tinydata',
) {
# Settings evaluation
$app = $title
$tp_settings = tp_lookup($app,'settings',$data_module,'merge')
$settings = $tp_settings + $settings_hash
$real_repository = $repository ? {
undef => $app,
default => $repository,
}
$real_repository_tag = $repository_tag ? {
undef => "${os}-${osversion}",
default => $repository_tag,
}
$real_from = $from ? {
undef => "${os}:${osversion}",
default => $from,
}
$basedir_path = "${workdir}/${username}/${os}/${osversion}/${app}"
Exec {
path => '/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin',
timeout => 3000,
}
# Dockerfile creation
if $create {
exec { "mkdir -p ${basedir_path}":
creates => $basedir_path,
} ->
file { "${basedir_path}/Dockerfile":
ensure => $ensure,
content => template($template),
}
}
# Image build
if $build and $ensure == 'present' {
exec { "docker build ${build_options} -t ${username}/${real_repository}:${real_repository_tag} .":
cwd => $basedir_path,
subscribe => File["${basedir_path}/Dockerfile"],
environment => $exec_environment,
}
}
# Image upload to Docker Hub
if $push and $ensure == 'present' {
exec { "docker push ${username}/${real_repository}:${real_repository_tag}":
subscribe => Exec["docker build ${build_options} -t ${username}/${real_repository}:${real_repository_tag} ."],
environment => $exec_environment,
}
}
# Image run
if $run {
$service_ensure = $ensure ? {
'absent' => 'stopped',
false => 'stopped',
default => $settings[service_ensure],
}
$service_enable = $ensure ? {
'absent' => false,
false => false,
default => $settings[service_enable],
}
exec { "docker pull ${username}/${real_repository}:${real_repository_tag}":
unless => "docker images | grep ${username}/${real_repository} | grep ${real_repository_tag}",
environment => $exec_environment,
}
file { "/etc/init/docker-${app}":
ensure => $ensure,
content => template($init_template),
mode => '0755',
notify => Service["docker-${app}"],
}
service { "docker-${app}":
ensure => $service_ensure,
enable => $service_enable,
}
}
}