docker/.bin/friendica

279 lines
7.2 KiB
Bash

#!/bin/sh
set -eu
SMTP=${SMTP:-localhost}
SMTP_FROM=${SMTP_FROM:-no-reply}
SOURCEDIR=${SOURCEDIR:-/usr/src}
WORKDIR=${WORKDIR:-/var/www/html}
SMTP_TLS=${SMTP_TLS:-}
SMTP_STARTTLS=${SMTP_STARTTLS:-}
SMTP_AUTH_USER=${SMTP_AUTH_USER:-}
SMTP_AUTH_PASS=${SMTP_AUTH_PASS:-}
SMTP_AUTH_METHOD=${SMTP_AUTH_METHOD:-}
VERBOSE=1
for arg; do
case "$arg" in
-q|--quit)
if [ "$VERBOSE" -eq 2 ]; then
echo 'You cannot use verbose and quiet at the same time'
exit 1
fi
VERBOSE=0
break
;;
-v|--verbose)
if [ "$VERBOSE" -eq 0 ]; then
echo 'You cannot use verbose and quiet at the same time'
exit 1
fi
VERBOSE=2
break
;;
esac
done
# run an command with the www-data user
run_as() {
if [ "$(id -u)" -eq 0 ]; then
su - www-data -s /bin/sh -c "$1"
else
sh -c "$1"
fi
}
# log event
log() {
currVerb=1
if [ "$#" -eq 2 ]; then
currVerb=$2
fi
if [ "$VERBOSE" -ge "$currVerb" ]; then
echo "$1"
fi
}
# checks if the the first parameter is greater than the second parameter
version_greater() {
[ "$(printf '%s\n' "$@" | sort -t '.' -n -k1,1 -k2,2 | head -n 1)" != "$1" ]
}
# clones the whole develop branch (Friendica and Addons)
clone_develop() {
dir="${1:-$SOURCEDIR}"
friendica="${2:-$FRIENDICA_VERSION}"
addons="${3:-$FRIENDICA_ADDONS}"
friendica_git=$friendica
addons_git=$addons
if echo "$friendica" | grep -Eq '^.*\-dev'; then
friendica_git="develop"
fi
if echo "$addons" | grep -Eq '^.*\-dev'; then
addons_git="develop"
fi
log 'Cloning Friendica '\'$friendica_git\'' with Addons '\'$addons_git\'' into '\'$dir\'
# Removing the whole directory first
rm -fr $dir/friendica
sh -c "git clone -q -b ${friendica_git} https://github.com/friendica/friendica ${dir}/friendica"
mkdir $dir/friendica/addon
sh -c "git clone -q -b ${addons_git} https://github.com/friendica/friendica-addons ${dir}/friendica/addon"
}
# help of this shell script
friendica_help() {
echo "Usage: friendica <command> [<args>]"
echo ""
echo "Commands:"
echo " console Executes an command in the Friendica console"
echo " composer Executes the composer.phar executable for Friendica"
echo " install Installs Friendica"
echo " update Updates Friendica"
exit 0
}
check_database() {
TERM=dumb php -- <<'EOPHP'
<?php
// database might not exist, so let's wait for it (just to be safe)
$stderr = fopen('php://stderr', 'w');
require_once '/usr/src/config/htconfig.php';
$connection = "mysql:host=".$db_host.";dbname=".$db_data;
$maxTries = 10;
$connected = false;
do {
try {
// This docker image is using the PDO library
$mysql = @new PDO($connection, $db_user, $db_pass);
$connected = true;
} catch (PDOException $e) {
fwrite($stderr, "\nMySQL Connection Error: " . $e . "\n");
$connected = false;
--$maxTries;
if ($maxTries <= 0) {
exit(1);
}
sleep(3);
}
} while (!$connected);
unset($db_user, $db_pass, $db_host, $db_data, $port, $socket);
?>
EOPHP
}
# executes the Friendica console
console() {
if [ -f $WORKDIR/bin/console.php ]; then
cd $WORKDIR
php $WORKDIR/bin/console.php "$@"
fi
}
# executes the composer.phar binary of Friendica
composer() {
if [ -f $WORKDIR/bin/composer.phar ]; then
run_as "cd $WORKDIR;$WORKDIR/bin/composer.phar $@ -d $WORKDIR"
elif [ -f $SOURCEDIR/friendica/bin/composer.phar ]; then
cd $SOURCEDIR/friendica
$SOURCEDIR/friendica/bin/composer.phar "$@" -d $SOURCEDIR/friendica
fi
}
copy_sources() {
installed_version="0.0.0.0"
if [ -f ${WORKDIR}/VERSION ]; then
installed_version="$(cat ${WORKDIR}/VERSION)"
fi
if echo "$FRIENDICA_VERSION" | grep -Eq '^.*(\-dev|-rc)'; then
clone_develop
fi
image_version="0.0.0.0"
if [ -f $SOURCEDIR/friendica/VERSION ]; then
image_version="$(cat $SOURCEDIR/friendica/VERSION)"
else
# no given installation and not using the developer branch => nothing to do
log 'Friendica command '\'$1\'' failed, because of no version found', 0
exit 1;
fi
if version_greater "$installed_version" "$image_version"; then
log 'Can'\''t copy Friendica sources because the version of the data ($installed_version) is higher than the docker image ('$image_version')', 0
exit 1;
fi
if version_greater "$image_version" "$installed_version" ||
echo "$image_version" | grep -Eq '^.*\-dev|-rc'; then
if [ "$(id -u)" -eq 0 ]; then
rsync_options="-rlDog --chown=www-data:root"
else
rsync_options="-rlD"
fi
log 'Copying Friendica sources ('$image_version') from '\'$SOURCEDIR'/friendica'\'' to '\'$WORKDIR\'
rsync $rsync_options --delete --exclude='.git' --exclude='photo' --exclude='proxy' --exclude='.htconfig.php' --exclude='home.*' $SOURCEDIR/friendica/ $WORKDIR/
if [ -f $WORKDIR/view/smarty3 ]; then
chmod -R 777 $WORKDIR/view/smarty3
fi
# the stable packages already have the whole vendor stuff in their images
if echo "$FRIENDICA_VERSION" | grep -Eq '^.*(\-dev|-rc)'; then
composer install
fi
fi
}
# install Friendica
install() {
if [ -f ${WORKDIR}/VERSION ]; then
# If there is a given installation of Friendica and we should not update it => exit
# We have to explicit update Friendica to avoid breaking something
return
fi
log 'Installing Friendica'
copy_sources
if [ ! -f ${WORKDIR}/.htconfig.php ] &&
[ -f ${SOURCEDIR}/config/htconfig.php ] &&
[ "$AUTOINSTALL" = "true" ]; then
if check_database; then
run_as "cp ${SOURCEDIR}/config/htconfig.php ${WORKDIR}/.htconfig.php"
console autoinstall -f .htconfig.php
# TODO Workaround because of a strange permission issue
rm -fr ${WORKDIR}/view/smarty3/compiled
fi
fi
}
update() {
if [ ! -f ${WORKDIR}/VERSION ]; then
# We want to update a given installation
# if there is no installation, exit
return
fi
log 'Upgrading Friendica'
copy_sources
console dbstructure update
}
configmail() {
if [ "$SMTP" = "localhost" ]; then
# SMTP is a required setting
# do nothing if it is "localhost" (= not changed)
return
fi
sed -i "s/:root:/:${SITENAME}:/g" /etc/passwd
sed -i "s/Linux\ User/${SITENAME}/g" /etc/passwd
# add possible mail-senders
echo "www-data:$SMTP_FROM@$HOSTNAME:$SMTP" >> /etc/ssmtp/revaliases
echo "root:$SMTP_FROM@$HOSTNAME:$SMTP" >> /etc/ssmtp/revaliases
# replace ssmtp.conf settings
cat << EOF > /etc/ssmtp/ssmtp.conf
# /etc/ssmtp/ssmtp.conf
root=$SMTP_FROM@$HOSTNAME
hostname=$HOSTNAME
mailhub=$SMTP
FromLineOverride=YES
EOF
[ -z "$SMTP_TLS" ] || echo "UseTLS=$SMTP_TLS" >> /etc/ssmtp/ssmtp.conf
[ -z "$SMTP_STARTTLS" ] || echo "UseSTARTTLS=$SMTP_STARTTLS" >> /etc/ssmtp/ssmtp.conf
[ -z "$SMTP_AUTH_USER" ] || echo "AuthUser=$SMTP_AUTH_USER" >> /etc/ssmtp/ssmtp.conf
[ -z "$SMTP_AUTH_PASS" ] || echo "AuthPass=$SMTP_AUTH_PASS" >> /etc/ssmtp/ssmtp.conf
[ -z "$SMTP_AUTH_METHOD" ] || echo "AuthMethod=$SMTP_AUTH_METHOD" >> /etc/ssmtp/ssmtp.conf
}
if [ "$#" -eq 0 ]; then
friendica_help
exit 0
fi
case "$1" in
install) shift; install $@;;
update) shift; update $@ ;;
console) shift; console $@ ;;
composer) shift; composer $@ ;;
configmail) shift; configmail $@ ;;
*) friendica_help ;;
esac