Skip to content

Commit

Permalink
Merge pull request #18 from example42/dockerize
Browse files Browse the repository at this point in the history
Added tp::dockerize define
  • Loading branch information
alvagante committed Mar 30, 2016
2 parents f5acbad + f0a0988 commit 0147669
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 2 deletions.
127 changes: 127 additions & 0 deletions manifests/dockerize.pp
@@ -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,
}
}

}
3 changes: 2 additions & 1 deletion manifests/install.pp
Expand Up @@ -122,7 +122,8 @@

# Automatic repo management
if $auto_repo == true
and $settings[repo_url] {
and $settings[repo_url]
or $settings[yum_mirrorlist] {
$repo_enabled = $ensure ? {
'absent' => false,
false => false,
Expand Down
2 changes: 1 addition & 1 deletion spec/functions/tp_lookup_spec.rb
Expand Up @@ -10,7 +10,7 @@
end

it "should raise a ParseError if there is less than 3 arguments" do
expect { scope.function_tp_lookup(["1","2"]) }.to( raise_error(Puppet::ParseError))
expect { scope.function_tp_lookup(["1","2"]) }.to( raise_error(Puppet::DevError))
end

context "with test params" do
Expand Down
36 changes: 36 additions & 0 deletions templates/dockerize/Dockerfile.erb
@@ -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 -%>
13 changes: 13 additions & 0 deletions templates/dockerize/init.erb
@@ -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 %>

0 comments on commit 0147669

Please sign in to comment.