From b1243bab2d3b968a48d052d3293c44b433e88b5a Mon Sep 17 00:00:00 2001 From: Michal Fojtik Date: Fri, 9 May 2014 13:31:42 +0200 Subject: [PATCH 1/3] Allow users of self-contained builder specify alternate location of assemble scripts --- centos-ruby-builder/bin/start | 17 +++++++++++++---- centos-ruby-builder/etc/helpers | 20 +++++++++++++++++++- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/centos-ruby-builder/bin/start b/centos-ruby-builder/bin/start index 4961c65..151ccb9 100755 --- a/centos-ruby-builder/bin/start +++ b/centos-ruby-builder/bin/start @@ -3,10 +3,19 @@ source ${HOME}/etc/sdk source ${HOME}/etc/helpers -# Allow users to inspect/debug the builder image itself, by using: -# $ docker run -i -t openshift/centos-ruby-builder --debug -# -[ "$1" == "--debug" ] && exec /bin/bash +while getopts “ht:r:p:v” opt; do + case $opt in + h) + print_usage_and_exit + ;; + s) + download_assemble_scripts "${OPTARG}" + ;; + d) + exec /bin/bash + ;; + esac +done # If the /tmp/src is empty, then print usage and exit. (dir_is_empty /tmp/src) && print_usage_and_exit diff --git a/centos-ruby-builder/etc/helpers b/centos-ruby-builder/etc/helpers index 325d1df..826ebaa 100644 --- a/centos-ruby-builder/etc/helpers +++ b/centos-ruby-builder/etc/helpers @@ -18,7 +18,25 @@ function print_usage_and_exit() { echo echo "Other options:" echo - echo " --debug Drop to the shell instead of build." + echo " -s Point builder to remote directory with assemble scripts" + echo " -h Print this usage" + echo " -d Drop to the shell instead of build." echo exit 0 } + +function download_script() { + local url="$1" + local script_name="$2" + if ! curl --silent -L "${url}/${script_name}" > ${HOME}/bin/${script_name}; then + echo "Warning: Unable to download '${url}/${script_name}' ($?)" + fi +} + +function download_assemble_scripts() { + # FIXME: Rename 'prepare' to 'assemble' at some point. + download_script "$1" "prepare" + download_script "$1" "save-artifacts" + download_script "$1" "restore-artifacts" + download_script "$1" "run" +} From 9cfb5e4d58f7bf2685ec03375d5d2ed79cc7cb05 Mon Sep 17 00:00:00 2001 From: Michal Fojtik Date: Fri, 9 May 2014 13:32:43 +0200 Subject: [PATCH 2/3] Added example script to exercise the self-contained builder --- centos-ruby-builder/contrib/perf-test.sh | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 centos-ruby-builder/contrib/perf-test.sh diff --git a/centos-ruby-builder/contrib/perf-test.sh b/centos-ruby-builder/contrib/perf-test.sh new file mode 100755 index 0000000..432ec00 --- /dev/null +++ b/centos-ruby-builder/contrib/perf-test.sh @@ -0,0 +1,34 @@ +#!/bin/bash -e + +mkdir $(pwd)/.test + +test_app_name="test-app-$(date +%s)" + +git clone https://github.com/mfojtik/sinatra-app-example .test/app +cd .test/app + +function run_clean_build() { + docker run --cidfile="app.cid" -v $(pwd):/tmp/src openshift/centos-ruby-builder + docker commit $(cat app.cid) $test_app_name + rm -f app.cid +} + +function run_inc_build() { + docker run --rm --entrypoint="/opt/ruby/bin/save-artifacts" $test_app_name > ../archive.tgz + docker run --cidfile="app.cid" -i -v $(pwd):/tmp/src openshift/centos-ruby-builder < ../archive.tgz + docker commit $(cat app.cid) $test_app_name + rm -f app.cid +} + +time run_clean_build +echo "gem 'nokogiri'" >> Gemfile +bundle install +time run_inc_build +echo "gem 'therubyracer'" >> Gemfile +bundle install +time run_inc_build +echo "gem 'rails'" >> Gemfile +bundle install +time run_inc_build + +cd ../.. && rm -rf .test From b46538068d2a0dfab120ea35c1f42dbfea671505 Mon Sep 17 00:00:00 2001 From: Michal Fojtik Date: Fri, 9 May 2014 14:37:48 +0200 Subject: [PATCH 3/3] Added copy_assemble_files function and some minor tweaks in comments. --- centos-ruby-builder/bin/restore-artifacts | 10 ++++++--- centos-ruby-builder/bin/save-artifacts | 10 +++++---- centos-ruby-builder/bin/start | 25 ++++++++++++++++------- centos-ruby-builder/etc/helpers | 20 ++++++++++++++++++ 4 files changed, 51 insertions(+), 14 deletions(-) diff --git a/centos-ruby-builder/bin/restore-artifacts b/centos-ruby-builder/bin/restore-artifacts index 29ddf60..dbd8249 100755 --- a/centos-ruby-builder/bin/restore-artifacts +++ b/centos-ruby-builder/bin/restore-artifacts @@ -1,5 +1,9 @@ #!/bin/bash -cd $HOME - -exec tar -xzf - +# Reads the archive with artifacts from STDIN and extract it +# into the $HOME (/opt/ruby) folder. +# +# NOTE: Docker needs to be run with --interactive (-i) option to route +# the stdin properly. +# +cd $HOME && exec tar -xzf - diff --git a/centos-ruby-builder/bin/save-artifacts b/centos-ruby-builder/bin/save-artifacts index 10adb98..9d9573a 100755 --- a/centos-ruby-builder/bin/save-artifacts +++ b/centos-ruby-builder/bin/save-artifacts @@ -1,7 +1,9 @@ #!/bin/bash - -cd $HOME - +# +# Create the archive with files/folders you would like to +# preserve for the next build. Then the archive is streamed into +# STDOUT. +# # List the files and directories you would like to archive. # -exec tar -czf - {bundle,gems} +cd $HOME && exec tar -czf - {bundle,gems} diff --git a/centos-ruby-builder/bin/start b/centos-ruby-builder/bin/start index 151ccb9..77243a8 100755 --- a/centos-ruby-builder/bin/start +++ b/centos-ruby-builder/bin/start @@ -3,7 +3,15 @@ source ${HOME}/etc/sdk source ${HOME}/etc/helpers -while getopts “ht:r:p:v” opt; do +# If the /tmp/src is empty, then print usage and exit. +(dir_is_empty /tmp/src) && print_usage_and_exit + +# If the directory with sources contain '.sti' folder, then look for the +# prepare/run/etc. scripts there and replace the 'base' image scripts. +# +(! dir_is_empty /tmp/src/.sti ) && copy_assemble_scripts + +while getopts “h:s:d” opt; do case $opt in h) print_usage_and_exit @@ -17,9 +25,6 @@ while getopts “ht:r:p:v” opt; do esac done -# If the /tmp/src is empty, then print usage and exit. -(dir_is_empty /tmp/src) && print_usage_and_exit - # Detect the presence of $stdin and restore the artifacts # To restore the artifacts, you have to run the builder as: # @@ -30,12 +35,18 @@ if [ -p /dev/stdin ]; then cat | ${HOME}/bin/restore-artifacts fi -# Build the ruby application first +# Prepare the Ruby application source code which includes: +# +# * (optional) Restore artifacts (bundle/vendor) from the previous build +# * Installing all rubygems dependencies using Bundler +# * (optional) Complilation of the Rails assets (rake assets:precompile) # ${HOME}/bin/prepare -# Replace this script with the application runner at the end of the build. -# echo "---> Build successful. Commit this image with: 'docker commit ${HOSTNAME} ruby-app'" +# Once the Ruby application sources are prepared, replace +# this script with the 'run' script that launch the application +# using Ruby application server (puma or rackup by default). +# cp -f ${HOME}/bin/run ${HOME}/bin/start && exit diff --git a/centos-ruby-builder/etc/helpers b/centos-ruby-builder/etc/helpers index 826ebaa..6142040 100644 --- a/centos-ruby-builder/etc/helpers +++ b/centos-ruby-builder/etc/helpers @@ -25,6 +25,12 @@ function print_usage_and_exit() { exit 0 } +# Functions to support downloading/replacing the base image scripts. +# Users could override them by: +# +# 1) Adding them into GIT_SOURCE/.sti/ folder +# 1) Using '-s URL' on the builder command line + function download_script() { local url="$1" local script_name="$2" @@ -33,6 +39,13 @@ function download_script() { fi } +function copy_script() { + local $script_name="$1" + [ ! -f "/tmp/src/.sti/${script_name}"] && return + cp -f "/tmp/src/.sti/${script_name}" "${HOME}/bin/${script_name}" && \ + chmod +x "${HOME}/bin/${script_name}" +} + function download_assemble_scripts() { # FIXME: Rename 'prepare' to 'assemble' at some point. download_script "$1" "prepare" @@ -40,3 +53,10 @@ function download_assemble_scripts() { download_script "$1" "restore-artifacts" download_script "$1" "run" } + +function copy_assemble_scripts() { + copy_script 'prepare' + copy_script 'save-artifacts' + copy_script 'restore-artifacts' + copy_script 'run' +}