config/proc.sh in traquitana-0.0.20 vs config/proc.sh in traquitana-0.0.21
- old
+ new
@@ -1,95 +1,269 @@
+#
+# Show a message
+# msg(message, verbose, newline)
+#
function msg() {
local str="$1"
- local verbose="$2"
- local newline="$3"
- if [ "$verbose" != "true" ]; then
- return 1
- fi
- if [ "$newline" == "true" ]; then
- echo "$str"
+
+ echo "$str" >> $logfile
+ echo -e "$str"
+}
+
+#
+# Show usage
+#
+function usage() {
+ echo "Usage:"
+ echo ""
+ echo "proc.sh <traq directory> <id> <verbose>"
+ echo ""
+ echo "-h Show this message"
+ echo "-o Show gem dir owner"
+ echo "-r Show gem dir owner, asking RVM"
+ echo "-g Show gem dir owner, asking Rubygems"
+ echo "-i Show gems provider (Rubygems, RVM, etc)"
+ echo "-d Show gems dir"
+}
+
+#
+# Gem dir
+#
+function gem_dir() {
+ local provider=$(gem_provider)
+ if [ -z "${provider}" ]; then
+ echo ""
else
- echo -n "$str"
+ echo $(${provider}_gemdir)
fi
}
-# force the production enviroment
-export RAILS_ENV=production
+#
+# RVM gem dir
+#
+function rvm_gemdir() {
+ echo $(rvm gemdir)
+}
-# move to the correct directory
-cd $1/..
+#
+# Rubygems gem dir
+#
+function rubygems_gemdir() {
+ echo $(gem environment | grep INSTALLATION | cut -f2- -d:)
+}
-# verbose mode
-verbose="$3"
+#
+# Return the RVM gems dir owner
+#
+function rvm_owner() {
+ echo $(stat --printf=%U $(rvm_gemdir))
+}
-# make a copy of the old contents
-msg "Making a safety copy of the old contents on traq/$2 ... " "$verbose" "false"
-zip -q traq/$2.safe.zip `cat traq/$2.list` &> /dev/null
-msg "done." "$verbose" "true"
+#
+# Return the gems dir owner
+#
+function rubygems_owner() {
+ echo $(stat --printf=%U $(rubygems_gemdir))
+}
-# check the current Gemfile checksum
-old_gemfile_md5=$(md5sum Gemfile 2> /dev/null | cut -f1 -d' ')
+#
+# Gems provider
+#
+function gem_provider() {
+ if [ -n "$(which rvm)" ]; then
+ echo "rvm"
+ elif [ -n "$(which gem)" ]; then
+ echo "rubygems"
+ else
+ echo ""
+ fi
+}
-# install the new files
-msg "Unzipping $2.zip ... " "true" "false"
-unzip -o traq/$2.zip &> /dev/null
-msg "done." "true" "true"
+#
+# Find the current gem owner
+# If not using RVM, returns the owner of the gem directory
+#
+function gemdir_owner() {
+ local provider=$(gem_provider)
+ if [ -z "${provider}" ]; then
+ echo $(whoami)
+ else
+ echo $(${provider}_owner)
+ fi
+}
-# check the new Gemfile checksum
-new_gemfile_md5=$(md5sum Gemfile 2> /dev/null | cut -f1 -d' ')
+#
+# Move to the app directory
+#
+function cd_app_dir() {
+ msg "Moving to ${dir} directory ..."
+ cd $1/..
+}
-# if the current Gemfile is different, run bundle install
-if [ -f Gemfile -a "$old_gemfile_md5" != "$new_gemfile_md5" ]; then
+#
+# Make a copy of the old contents
+#
+function safe_copy() {
+ msg "Making a safety copy of the old contents on traq/$1.safe.zip ... "
+ zip -q traq/$1.safe.zip `cat traq/$1.list` &> /dev/null
+}
- if [ -f Gemfile.lock ]; then # if there is a Gemfile.lock file ...
- if [ -w Gemfile.lock ]; then # ... and it is writable
- msg "Running bundle install ..." "true" "true"
- bundle install
- else
- msg "Gemfile.lock exists but it is not writable, not running bundle install." "true" "true"
- fi
+#
+# Install the new files
+#
+function install_new_files() {
+ msg "Unzipping $1.zip ... "
+ unzip -o traq/$1.zip &> /dev/null
+}
+
+#
+# Create database if needed
+#
+function createdb() {
+ msg "Creating database if needed ..."
+ bundle exec rake db:create &> /dev/null
+}
+
+#
+# Run migrations if needed
+#
+function migrate() {
+ migrations=$(grep "^db/migrate" traq/${config_id}.list)
+ if [ -n "$migrations" ]; then
+ msg "Running migrations ... "
+ bundle exec rake db:migrate 2> /dev/null
fi
+}
- if [ ! -f Gemfile.lock ]; then # if there is not a Gemfile.lock file ...
- if [ -w . ]; then # ... and current dir is writable
- msg "Running bundle install ..." "true" "true"
+#
+# Precompile assets if needed
+#
+function assets() {
+ if [ -d app/assets ]; then
+ msg "Compiling assets ... "
+ bundle exec rake assets:precompile 2> /dev/null
+ fi
+}
+
+#
+# Change file permissions on public dir
+#
+function permissions() {
+ if [ -d public ]; then
+ msg "Changing file permissions on public to 0755 ... "
+ chmod -R 0755 public/*
+ fi
+}
+
+#
+# Fix current gems, running bundle
+#
+function fix_gems() {
+ msg "Fixing gems ..."
+ local basedir=$(gem_dir | cut -f1-3 -d/)
+ local owner=$(gemdir_owner)
+ local curdir=$(pwd)
+ local curuser=$(whoami)
+ msg "Gem dir owner is \e[1m${owner}\e[0m"
+
+ # if gemdir owner and current user is root, try to install gems system wide
+ if [ "${owner}" == "root" -a "${curuser}" == "root" ]; then
+ msg "Performing a \e[1msystem wide gem install using root\e[0m"
+ bundle install
+ # install gems on rvm system path or vendor/bundle
+ else
+ # if gemdir is the current user dir, install there
+ if [ "${basedir}" == "/home/${owner}" ]; then
+ msg "Performing a \e[1mlocal gem install on home dir\e[0m"
bundle install
+ # if user is not root and gemdir is not the home dir, install on vendor
else
- msg "Gemfile.lock does not exists but current dir is not writable, not running bundle install" "true" "true"
+ msg "Performing a \e[1mlocal gem install on vendor/bundle\e[0m"
+ bundle install --path vendor/bundle
fi
fi
-fi
+}
-# run migrations if needed
-migrations=$(grep "^db/migrate" traq/$2.list)
-if [ -n "$migrations" ]; then
- msg "Running migrations ... " "true" "true"
- bundle exec rake db:migrate 2> /dev/null
- msg "Migrations done." "true" "true"
-fi
+#
+# Make a sanity check to see if all the tools needed are available
+#
+function sanity_check() {
+ if [ -z "$(which unzip)" ]; then
+ msg "\e[31mThere's no \e[1munzip\e[0;31m tool installed on the server. Please install it before proceeding again\e[0m"
+ exit 2
+ fi
+}
-# precompile assets if needed
-if [ -d app/assets ]; then
- msg "Compiling assets ... " "$verbose" "false"
- bundle exec rake assets:precompile 2> /dev/null
- msg "done." "$verbose" "true"
-fi
+# force the production enviroment
+export RAILS_ENV=production
-# change file permissions on public dir
-if [ -d public ]; then
- msg "Changing file permissions on public to 0755 ... " "$verbose" "false"
- chmod -R 0755 public/*
- msg "done." "$verbose" "true"
-fi
+config_dir="$1" # config dir
+config_id="$2" # config id
+verbose="$3" # verbose mode
+newline="true" # default newline on messages
+logfile="/tmp/traq$$.log" # log file
+# sanity check
+sanity_check
+
+# parse command line options
+while getopts "horgid" OPTION
+do
+ case ${OPTION} in
+ h)
+ usage
+ exit 1
+ ;;
+ o)
+ echo "Gem dir owner is: $(gemdir_owner)"
+ exit 1
+ ;;
+ r)
+ echo "RVM gem dir owner is: $(rvm_owner)"
+ exit 1
+ ;;
+ g)
+ echo "Ruby gems dir owner is: $(rubygems_owner)"
+ exit 1
+ ;;
+ i)
+ echo "Gems provider is $(gem_provider)"
+ exit 1
+ ;;
+ d)
+ echo "Gems dir is $(gem_dir)"
+ exit 1
+ ;;
+ *)
+ usage
+ exit 1
+ ;;
+ esac
+done
+
+msg "Log file is ${logfile}"
+
+# move to the correct directory
+dir="${1}"
+cd_app_dir "${dir}"
+safe_copy "${config_id}"
+
+# here is where things happens on the server
+install_new_files "${config_id}"
+fix_gems
+createdb
+migrate
+assets
+permissions
+
# restart server
if [ -x ./traq/server.sh -a -f ./traq/server.sh ]; then
./traq/server.sh
-fi
+fi
# extra configs
if [ -x ./traq/extra.sh -a -f ./traq/extra.sh ]; then
./traq/extra.sh
fi
# erase file
-rm traq/$2.zip
+rm traq/${config_id}.zip