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 4961c65..77243a8 100755 --- a/centos-ruby-builder/bin/start +++ b/centos-ruby-builder/bin/start @@ -3,14 +3,28 @@ 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 - # 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 + ;; + s) + download_assemble_scripts "${OPTARG}" + ;; + d) + exec /bin/bash + ;; + esac +done + # Detect the presence of $stdin and restore the artifacts # To restore the artifacts, you have to run the builder as: # @@ -21,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/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 diff --git a/centos-ruby-builder/etc/helpers b/centos-ruby-builder/etc/helpers index 325d1df..6142040 100644 --- a/centos-ruby-builder/etc/helpers +++ b/centos-ruby-builder/etc/helpers @@ -18,7 +18,45 @@ 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 } + +# 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" + if ! curl --silent -L "${url}/${script_name}" > ${HOME}/bin/${script_name}; then + echo "Warning: Unable to download '${url}/${script_name}' ($?)" + 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" + download_script "$1" "save-artifacts" + 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' +}