Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from example42/dockerize
Added tp::dockerize define
- Loading branch information
Showing
5 changed files
with
179 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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, | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Dockerfile generated by tp::dockerize { '<%= @app %>': } | ||
FROM <%= @real_from %> | ||
<% if @maintainer -%> | ||
MAINTAINER <%= @maintainer %> | ||
<% end -%> | ||
<% if @os == 'debian' or @os == 'ubuntu' -%> | ||
<% if @settings['repo_url'] -%> | ||
RUN echo "deb <%= @settings['repo_url'] -%> <%= @settings['apt_repos'] %> <%= @settings['apt_release'] %>" > /etc/apt/sources.list.d/<%= @app %>.list | ||
<% if @settings['apt_key_server'] -%> | ||
RUN apt-key add --keyserver <%= @settings['apt_key_server'] %> --recv-key <%= @settings['key'] %> | ||
<% end -%> | ||
<% end -%> | ||
RUN apt-get update && \ | ||
apt-get install -y --no-install-recommends <%= @settings['package_name'] %> && \ | ||
rm -rf /var/lib/apt/lists/* | ||
<% end -%> | ||
<% if @os == 'redhat' or @os == 'centos' -%> | ||
<% if @settings['repo_url'] -%> | ||
RUN echo "[<%= @app %>]\nname=<%= @app %> repository\nbaseurl=<%= @settings['repo_url'] -%> > /etc/yum.repos.d/<%= @app %>.repo | ||
<% end -%> | ||
RUN yum install -y epel-release | ||
RUN yum install -y <%= @settings['package_name'] %> && yum clean all | ||
<% end -%> | ||
<% if @settings['tcp_port'] -%> | ||
EXPOSE <%= @settings['tcp_port'] %> | ||
<% end -%> | ||
<% if @settings['data_dir_path'] and @mount_data_dir -%> | ||
VOLUME <%= @settings['data_dir_path'] %> | ||
<% end -%> | ||
<% if @settings['log_dir_path'] and @mount_log_dir -%> | ||
VOLUME <%= @settings['log_dir_path'] %> | ||
<% end -%> | ||
ENTRYPOINT [ "<%= @settings['process_name'] %>"<% if @settings['process_extra_name'] -%> , "<%= @settings['process_extra_name'] -%>"<% end -%> ] | ||
<% if @settings['process_args'] -%> | ||
CMD [ <% comma = '' %><% @settings['process_args'].split.each do |ar| %><%= comma %>"<%= ar %>"<% comma = ',' %><% end -%> ] | ||
<% end -%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# <%= @app %> Docker instance | ||
|
||
description "<%= @app %> Docker instance" | ||
|
||
start on runlevel [2345] | ||
stop on runlevel [!2345] | ||
|
||
respawn | ||
respawn limit 10 5 | ||
umask 022 | ||
|
||
exec <%= @settings['process_name'] %> <% if @settings['process_args'] %><%= @settings['process_args'] %><% end %> | ||
|