| Current Path : /pkg/zweb/1.0/bin/ |
| Current File : //pkg/zweb/1.0/bin/zweb-php-ctl-2 |
#!/bin/bash
timestamp="$(date +'%Y-%m-%d %H:%M')"
echo "$timestamp start"
if ! echo $PATH | grep -q cmd; then
export PATH="/cmd:$PATH"
fi
cd /run/sv
while true; do
sleep 7
timestamp="$(date +'%Y-%m-%d %H:%M')"
users=$(ls /run/sv | grep -E '[a-z0-9]{1,10}-php' | cut -d '-' -f1)
for user in $users; do
conf="/srv/web/$user/etc/php"
mkdir -p $conf
temp="/srv/web/$user/tmp/php/conf"
mkdir -p $temp
svc="$user-php"
if id $user &> /dev/null; then
[[ -d $conf/ctl ]] || mkdir -p $conf/ctl
chown root:$user $conf/ctl
chmod 770 $conf/ctl
if [ -f $conf/ctl/version ]; then
chown $user:$user $conf/ctl/version
chmod 660 $conf/ctl/version
fi
else
echo "$timestamp user $user missing"
continue
fi
ver='none'
# begin with currently configured version
if [ -f $conf/version ]; then
ver=$(head -n 1 $conf/version)
etc_current="/pkg/php/$ver/etc"
for i in conf ini; do
if [ -f $etc_current/php.$i.base ]; then
cp $etc_current/php.$i.base $temp
fi
done
fi
ver_run=''
svc_stat=$(svctl status $svc);
if echo "$svc_stat" | grep -q "^$svc up"; then
sec=$(echo $svc_stat | cut -d ' ' -f5 | sed 's/s//;s/,//')
if [ $sec -lt 15 ]; then
echo "$timestamp $svc $ver up $sec seconds"
fi
ver_run="$ver"
ini_run="/pkg/php/$ver_run/etc/php.ini"
elif echo "$svc_stat" | grep -q "^$svc down"; then
if [ ! -f $svc/down ]; then
sec=$(echo $svc_stat | cut -d ' ' -f3 | sed 's/s//;s/,//')
if [ $sec -lt 15 ]; then
echo "$timestamp $svc $ver down $sec seconds"
fi
fi
fi
action='none'
# determine action, in order of precedence
for opt in start restart stop; do
if [ -f $conf/ctl/$opt ]; then
echo "$timestamp $svc $ver $opt requested"
action="$opt"
break
fi
done
# clean-up on-disk actions
for opt in start restart stop; do
[[ -f $conf/ctl/$opt ]] && rm $conf/ctl/$opt
done
if [ "$action" == 'none' ]; then
continue
fi
if [ "$action" == 'stop' ]; then
svctl down $svc > /dev/null 2>&1
# save for reboot
[[ -f $svc/down ]] || touch $svc/down
elif [ "$action" == 'restart' ] || [ "$action" == 'start' ]; then
[[ -f $svc/down ]] && rm $svc/down
ver_ctl=''
if [ -f "$conf/ctl/version" ]; then
ver_ctl=$(head -n 1 $conf/ctl/version)
fi
if [ "$ver_ctl" != "$ver" ]; then
# restart because the version changed
[[ "$action" == 'start' ]] && action='restart'
echo "$timestamp $svc $ver current"
if echo "$ver_ctl" | grep -Eq '^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,2}$'; then
etc_ctl="/pkg/php/$ver_ctl/etc"
if [ -f $etc_ctl/php.ini ]; then
ver="$ver_ctl"
echo "$timestamp $svc $ver_ctl change"
echo $ver_ctl > $conf/version
cp $etc_ctl/php.ini.base $temp
rm -f $conf/ctl/error
else
rm -f $conf/ctl/version
error="$timestamp $svc $ver_ctl unavailable"
echo "$error"
echo "$error" > $conf/ctl/error
fi
elif [ "$ver_ctl" != '' ]; then
error="$timestamp $svc $ver_ctl invalid"
echo "$error"
echo "$error" > $conf/ctl/error
cp $conf/version $conf/ctl/version
fi
fi
if [ -f $conf/ctl/module/redis/on ]; then
if find /pkg/php/$ver | grep -qE \.redis.so$; then
if [ -f /pkg/php/$ver/etc/php.ini.redis ]; then
cat /pkg/php/$ver/etc/php.ini.redis \
> $temp/php.ini.redis
fi
echo "$timestamp $svc $ver module redis enabled"
# if [ -f $conf/ctl/module/redis/sessions ]; then
# if [ -f /pkg/php/$ver/etc/php.conf.redis ]; then
# cat /pkg/php/$ver/etc/php.conf.redis \
# | sed "s/_USER_/$user/g" \
# >> $temp/php.conf.redis
# fi
# fi
else
echo "$timestamp $svc $ver module redis missing"
rm -f $temp/php.ini.redis
fi
elif grep -q 'redis.so' $conf/php.ini; then
echo "$timestamp $svc $ver module redis disabled"
rm -f $temp/php.ini.redis
fi
if [ -f $conf/ctl/module/opcache/on ]; then
echo "$timestamp $svc $ver module opcache enabled"
if find /pkg/php/$ver | grep -qE \.opcache.so$; then
opcache_dir="/srv/web/$user/tmp/php/opcache"
mkdir -p $opcache_dir
chown $user:$user $opcache_dir
if [ -f $conf/ctl/module/opcache/conf ]; then
echo "$timestamp $svc $ver module opcache conf found"
cat $conf/ctl/module/opcache/conf \
| grep -v 'opcache.file_cache' \
> $temp/php.ini.opcache
elif [ -f /pkg/php/$ver/etc/php.ini.opcache ]; then
echo "$timestamp $svc $ver module opcache conf created"
cat /pkg/php/$ver/etc/php.ini.opcache \
| grep -v 'opcache.file_cache' \
> $conf/ctl/module/opcache/conf
cat /pkg/php/$ver/etc/php.ini.opcache \
| grep -v 'opcache.file_cache' \
> $temp/php.ini.opcache
fi
echo "opcache.file_cache = /srv/web/$user/tmp/php/opcache" \
>> $temp/php.ini.opcache
else
echo "$timestamp $svc $ver opcache module missing"
fi
elif grep -q ^opcache $conf/php.ini; then
echo "$timestamp $svc $ver ini disable opcache"
rm -f $temp/php.ini.opcache
fi
# clear possible opcache
opcache="/srv/web/$user/tmp/php/opcache"
if cd $opcache > /dev/null 2>&1; then
if [ $(ls | wc -l) -gt 0 ]; then
rm -rf *
echo "$timestamp $svc $ver module opcache cache cleared"
fi
fi
chown -R $user:$user $conf/ctl
# begin building php.conf
echo "$timestamp $svc $ver conf begin"
cp /pkg/php/$ver/etc/php.conf \
| sed "s/_USER_/$user/g" \
> $conf/php.conf
# prepare admin value temp folders
for x in flag value; do
mkdir -p $temp/admin/$x
cd $temp/admin/$x
for var in *; do
[[ -f $var ]] && rm $var
done
done
# account specific admin values
for x in flag value; do
if cd $conf/admin/$x > /dev/null 2>&1; then
for var in *; do
val=$(head -n 1 $var)
echo "$timestamp $svc $ver conf admin set $x $var $val"
echo "$val" | sed "s/_USER_/$user/g" \
> $temp/admin/$x/$var
done
fi
done
# default admin values
for x in flag value; do
if cd /etc/zweb/php/admin/$x > /dev/null 2>&1; then
for var in *; do
# only add if not explicitly set
if [ ! -f $temp/admin/$x/$var ]; then
val=$(head -n 1 $var)
echo "$timestamp $svc $ver conf admin set $x $var $val"
echo "$val" | sed "s/_USER_/$user/g" \
> $temp/admin/$x/$var
fi
done
fi
done
# append admin values to php.conf
for x in flag value; do
if cd $temp/admin/$x > /dev/null 2>&1; then
for var in *; do
val=$(head -n 1 $var)
echo "php_admin_$x[$var] = $val" \
>> $temp/php.conf
done
fi
done
# append $conf/php.conf extras
for x in redis extra; do
if [ -f $temp/php.conf.$x ]; then
echo "$timestamp $svc $ver php.conf add $x"
cat $temp/php.conf.$x \
| sed "s/_USER_/$user/g" \
>> $conf/php.conf
fi
done
# clear php.ini
cat /dev/null > $conf/php.ini
# build $conf/php.ini from components
for ext in base opcache redis extra; do
if [ -f $temp/php.ini.$ext ]; then
echo "$timestamp $svc $ver php.ini enable $ext"
cat $temp/php.ini.$ext >> $conf/php.ini
fi
done
if [ "$action" == 'start' ]; then
svctl start $svc > /dev/null 2>&1
elif [ "$action" == 'restart' ]; then
svctl restart $svc > /dev/null 2>&1
fi
fi
echo "$timestamp $svc $ver $action complete"
sleep 3
# review service status
svc_stat=$(svctl status $svc);
if echo "$svc_stat" | grep -q "^$svc up"; then
sec=$(echo $svc_stat | cut -d ' ' -f5 | sed 's/s//;s/,//')
echo "$timestamp $svc $ver up $sec seconds"
echo "$timestamp $ver up" > $conf/ctl/status
elif echo "$svc_stat" | grep -q "^$svc down"; then
# down requested, so result as expected
if [ "$action" == 'stop' ]; then
sec=$(echo $svc_stat | cut -d ' ' -f3 | sed 's/s//;s/,//')
echo "$timestamp $svc $ver down $sec seconds"
echo "$timestamp $svc $ver down $sec seconds" > $conf/ctl/status
# uh-oh, we have a problem! rollback
elif [ "$ver_run" != '' ]; then
if [ -f $ini_run ]; then
echo $ver_run > $conf/version
echo $ver_run > $conf/ctl/version
echo $ver_ctl > $conf/ctl/version.error
cp $ini_run $conf
echo "$timestamp $svc $ver_run start"
svctl start $svc > /dev/null 2>&1
echo "$timestamp $ver_run rollback" > $conf/ctl/status
fi
fi
fi
done # end 'for user in $users; do'
done # end 'while true; do'