File: //lib/start-lib.sh
#!/usr/bin/env bash
## CREATES THE ENVIRONEMENT
export LANG=C
export LC_ALL=C
export PAAS_USER='hosting-user'
export APP_USER='hosting-app0'
export SYS_USER='hosting-admin'
export DB_USER='hosting-db'
export PHP_USER=${PAAS_USER}
export GITWEB_USER=${PAAS_USER}
export CUSTOMER_DIR='/srv/data'
export HOME="/home/${PAAS_USER}"
export LOCAL_DIR='/srv/run'
export PREFIX='/' # for npm
function do_as() {
# TODO: Improve this by using SMACK
local user="$1"
shift
/sbin/capsh --secbits=0x2f \
--user="$user" --caps="all=" \
-- -c 'exec "$0" "$@"' "$@"
# Last line protects arguments separation
}
# Parses the arguments common to all init scripts, and sets
# the corresponding variables, but only applies them
# on start-lib.sh . It is the responsability of the calling
# script to set global effects (like changing the gateway).
# The reason for that is to respect modularity.
function parse_arguments() {
while [ $# -gt 0 ]; do
case "$1" in
'--debug')
export NO_TTY_REDIRECTIONS=true
;;
'--rescue-grant-tables')
export RESCUE_GRANT_TABLES=true
;;
'--shell-start')
export SHELL_START=true
;;
'--')
shift
if [ $# -gt 1 ] && [ "$1" = "deploy" ]; then
VHOST=$(extract_vhost "$2")
export VHOST
export TREEISH="$3"
fi
break
;;
esac
shift
done
}
# Writes on its standard output the amount of available memory
# Requires /cgroup to be mounted
function get_cgroup_max_memory_mb() {
# Parses the path to isolate the cgroup name
local proc
read -r proc < /proc/self/cgroup
# Only keeps the last column
local cgroup_name=${proc##*:}
local mem_bytes
read -r mem_bytes < "/cgroup/$cgroup_name/memory.limit_in_bytes"
echo $(( mem_bytes / 1024 / 1024 ))
}
function logrotate_console_file() {
local console_file="${CUSTOMER_DIR}/var/log/boot/console.log"
local state_file=/dev/null
local logrotate_conf="/srv/admin/configs/logrotate/console.conf"
local logrotate_user=$SYS_USER
# this is error prone due to bad usage of delegations by the kernel it may
# fail if the console log file has been opened by eg. apache and then
# instance is restarted
set +e
[ -f "$console_file" ] && chmod 646 "$console_file"
do_as "$logrotate_user" /usr/sbin/logrotate -f -s "$state_file" \
"$logrotate_conf" > /dev/null
set -e
return 0
}
function go_console() {
if [[ -z "$NO_TTY_REDIRECTIONS" ]]; then
logrotate_console_file
exec < /dev/console
exec &> /dev/console
fi
}
function start_user_shell_on_tty() {
local user="$1"
local tty="$2"
if [[ -z "$NO_TTY_REDIRECTIONS" ]]; then
echo "Starting a shell for $user on $tty"
/usr/local/bin/start-shell /usr/local/bin/restart-shell "$user" < "/dev/$tty" >& "/dev/$tty"
fi
}
function extract_vhost() {
local reponame="$1"
reponame="${reponame#\'}"
reponame="${reponame%\'}"
echo "${reponame%.git}"
}
parse_json() {
JSON_FILE=$1
WWW_ENGINE=$(jq -er .language.name "${JSON_FILE}")
WWW_VERSION=$(jq -er .language.version "${JSON_FILE}")
DB_ENGINE=$(jq -er .database.name "${JSON_FILE}")
DB_VERSION=$(jq -er .database.version "${JSON_FILE}")
DB_PATH=$DB_ENGINE
case "${DB_ENGINE}" in
pgsql) DB_PATH="postgresql" ;;
esac
PATH="/usr/sbin:/usr/bin:/sbin/:/bin"
for path in "bin" "sbin" "usr/bin" "usr/sbin"
do
PATH="$PATH:/opt/${WWW_ENGINE}-${WWW_VERSION}/${path}"
PATH="$PATH:/opt/${DB_PATH}-${DB_VERSION}/${path}"
done
case "${DB_ENGINE}" in
mysql)
lib_path="/opt/mysql-${DB_VERSION}/lib/x86_64-linux-gnu" ;;
pgsql)
lib_path="/opt/postgresql-${DB_VERSION}/lib" ;;
esac
if [ -n "${lib_path-}" ]; then
if [ -n "${LD_LIBRARY_PATH-}" ]; then
LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${lib_path}"
else
LD_LIBRARY_PATH="${lib_path}"
fi
fi
export PATH
export WWW_ENGINE
export WWW_VERSION
export DB_ENGINE
export DB_VERSION
export LD_LIBRARY_PATH
}