2017-03-02 11:23:23 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2018-06-08 09:11:03 +02:00
|
|
|
function array_by_comma { local IFS=","; echo "$*"; }
|
2017-03-02 11:23:23 +01:00
|
|
|
|
2017-05-08 15:35:24 +02:00
|
|
|
# Wait for containers
|
2018-10-11 11:53:22 +02:00
|
|
|
while ! mysqladmin status --socket=/var/run/mysqld/mysqld.sock -u${DBUSER} -p${DBPASS} --silent; do
|
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-29 22:01:23 +02:00
|
|
|
echo "Waiting for SQL..."
|
2017-05-08 15:35:24 +02:00
|
|
|
sleep 2
|
|
|
|
done
|
|
|
|
|
2018-07-04 11:41:43 +02:00
|
|
|
until [[ $(redis-cli -h redis-mailcow PING) == "PONG" ]]; do
|
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-29 22:01:23 +02:00
|
|
|
echo "Waiting for Redis..."
|
2017-05-08 15:35:24 +02:00
|
|
|
sleep 2
|
|
|
|
done
|
|
|
|
|
2018-12-10 13:22:25 +01:00
|
|
|
# Set a default release format
|
|
|
|
|
2018-12-06 16:49:14 +01:00
|
|
|
if [[ -z $(redis-cli --raw -h redis-mailcow GET Q_RELEASE_FORMAT) ]]; then
|
|
|
|
redis-cli --raw -h redis-mailcow SET Q_RELEASE_FORMAT raw
|
|
|
|
fi
|
|
|
|
|
2019-06-01 21:22:58 +02:00
|
|
|
# Set max age of q items - if unset
|
|
|
|
|
|
|
|
if [[ -z $(redis-cli --raw -h redis-mailcow GET Q_MAX_AGE) ]]; then
|
|
|
|
redis-cli --raw -h redis-mailcow SET Q_MAX_AGE 365
|
|
|
|
fi
|
|
|
|
|
2018-12-10 13:22:25 +01:00
|
|
|
# Check of mysql_upgrade
|
|
|
|
|
|
|
|
CONTAINER_ID=
|
2019-03-12 23:24:22 +01:00
|
|
|
until [[ ! -z "${CONTAINER_ID}" ]] && [[ "${CONTAINER_ID}" =~ ^[[:alnum:]]*$ ]]; do
|
2019-07-06 11:46:05 +02:00
|
|
|
CONTAINER_ID=$(curl --silent --insecure https://dockerapi/containers/json | jq -r ".[] | {name: .Config.Labels[\"com.docker.compose.service\"], id: .Id}" 2> /dev/null | jq -rc "select( .name | tostring | contains(\"mysql-mailcow\")) | .id" 2> /dev/null)
|
2019-03-12 23:24:22 +01:00
|
|
|
done
|
|
|
|
echo "MySQL @ ${CONTAINER_ID}"
|
2019-09-23 21:42:47 +02:00
|
|
|
SQL_LOOP_C=0
|
|
|
|
SQL_CHANGED=0
|
|
|
|
until [[ ${SQL_UPGRADE_STATUS} == 'success' ]]; do
|
|
|
|
if [ ${SQL_LOOP_C} -gt 4 ]; then
|
|
|
|
echo "Tried to upgrade MySQL and failed, giving up after ${SQL_LOOP_C} retries and starting container (oops, not good)"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
SQL_FULL_UPGRADE_RETURN=$(curl --silent --insecure -XPOST https://dockerapi/containers/${CONTAINER_ID}/exec -d '{"cmd":"system", "task":"mysql_upgrade"}' --silent -H 'Content-type: application/json')
|
|
|
|
SQL_UPGRADE_STATUS=$(echo ${SQL_FULL_UPGRADE_RETURN} | jq -r .type)
|
|
|
|
SQL_LOOP_C=$((SQL_LOOP_C+1))
|
|
|
|
echo "SQL upgrade iteration #${SQL_LOOP_C}"
|
|
|
|
if [[ ${SQL_UPGRADE_STATUS} == 'warning' ]]; then
|
|
|
|
SQL_CHANGED=1
|
|
|
|
echo "MySQL applied an upgrade, debug output:"
|
|
|
|
echo ${SQL_FULL_UPGRADE_RETURN}
|
|
|
|
sleep 3
|
2019-03-12 23:24:22 +01:00
|
|
|
while ! mysqladmin status --socket=/var/run/mysqld/mysqld.sock -u${DBUSER} -p${DBPASS} --silent; do
|
2019-09-23 21:42:47 +02:00
|
|
|
echo "Waiting for SQL to return, please wait"
|
2019-03-12 23:24:22 +01:00
|
|
|
sleep 2
|
|
|
|
done
|
2019-09-23 21:42:47 +02:00
|
|
|
continue
|
|
|
|
elif [[ ${SQL_UPGRADE_STATUS} == 'success' ]]; then
|
|
|
|
echo "MySQL is up-to-date - debug output:"
|
|
|
|
echo ${SQL_FULL_UPGRADE_RETURN}
|
|
|
|
else
|
2019-09-25 12:53:14 +02:00
|
|
|
echo "No valid reponse for mysql_upgrade was received, debug output:"
|
|
|
|
echo ${SQL_FULL_UPGRADE_RETURN}
|
2019-09-23 21:42:47 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
# doing post-installation stuff, if SQL was upgraded
|
|
|
|
if [ ${SQL_CHANGED} -eq 1 ]; then
|
|
|
|
POSTFIX=($(curl --silent --insecure https://dockerapi/containers/json | jq -r '.[] | {name: .Config.Labels["com.docker.compose.service"], id: .Id}' | jq -rc 'select( .name | tostring | contains("postfix-mailcow")) | .id' | tr "\n" " "))
|
|
|
|
if [[ -z ${POSTFIX} ]]; then
|
|
|
|
echo "Could not determine Postfix container ID, skipping Postfix restart."
|
|
|
|
else
|
|
|
|
echo "Restarting Postfix"
|
|
|
|
curl -X POST --silent --insecure https://dockerapi/containers/${POSTFIX}/restart | jq -r '.msg'
|
|
|
|
echo "Sleeping 5 seconds..."
|
|
|
|
sleep 5
|
2018-12-10 13:22:25 +01:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2018-10-11 11:53:22 +02:00
|
|
|
# Trigger db init
|
|
|
|
echo "Running DB init..."
|
|
|
|
php -c /usr/local/etc/php -f /web/inc/init_db.inc.php
|
|
|
|
|
2019-09-23 21:42:47 +02:00
|
|
|
# Recreating domain map
|
|
|
|
echo "Rebuilding domain map in Redis..."
|
2017-05-08 15:35:24 +02:00
|
|
|
declare -a DOMAIN_ARR
|
2019-09-23 21:42:47 +02:00
|
|
|
redis-cli -h redis-mailcow DEL DOMAIN_MAP > /dev/null
|
2017-05-08 15:35:24 +02:00
|
|
|
while read line
|
|
|
|
do
|
|
|
|
DOMAIN_ARR+=("$line")
|
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-29 22:01:23 +02:00
|
|
|
done < <(mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SELECT domain FROM domain" -Bs)
|
2018-01-17 15:23:33 +01:00
|
|
|
while read line
|
|
|
|
do
|
|
|
|
DOMAIN_ARR+=("$line")
|
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-29 22:01:23 +02:00
|
|
|
done < <(mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} -e "SELECT alias_domain FROM alias_domain" -Bs)
|
2018-01-17 15:23:33 +01:00
|
|
|
|
2017-05-08 15:35:24 +02:00
|
|
|
if [[ ! -z ${DOMAIN_ARR} ]]; then
|
|
|
|
for domain in "${DOMAIN_ARR[@]}"; do
|
2019-09-23 21:42:47 +02:00
|
|
|
redis-cli -h redis-mailcow HSET DOMAIN_MAP ${domain} 1 > /dev/null
|
2017-05-08 15:35:24 +02:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2018-06-08 09:11:03 +02:00
|
|
|
# Set API options if env vars are not empty
|
|
|
|
|
2018-06-10 14:30:30 +02:00
|
|
|
if [[ ${API_ALLOW_FROM} != "invalid" ]] && \
|
|
|
|
[[ ${API_KEY} != "invalid" ]] && \
|
|
|
|
[[ ! -z ${API_KEY} ]] && \
|
|
|
|
[[ ! -z ${API_ALLOW_FROM} ]]; then
|
2018-06-08 09:11:03 +02:00
|
|
|
IFS=',' read -r -a API_ALLOW_FROM_ARR <<< "${API_ALLOW_FROM}"
|
|
|
|
declare -a VALIDATED_API_ALLOW_FROM_ARR
|
|
|
|
REGEX_IP6='^([0-9a-fA-F]{0,4}:){1,7}[0-9a-fA-F]{0,4}$'
|
|
|
|
REGEX_IP4='^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$'
|
|
|
|
|
|
|
|
for IP in "${API_ALLOW_FROM_ARR[@]}"; do
|
|
|
|
if [[ ${IP} =~ ${REGEX_IP6} ]] || [[ ${IP} =~ ${REGEX_IP4} ]]; then
|
|
|
|
VALIDATED_API_ALLOW_FROM_ARR+=("${IP}")
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
VALIDATED_IPS=$(array_by_comma ${VALIDATED_API_ALLOW_FROM_ARR[*]})
|
|
|
|
if [[ ! -z ${VALIDATED_IPS} ]]; then
|
[Docker API] Use TLS encryption for communication with "on-the-fly" created key paris (non-exposed)
[Docker API] Create pipe to pass Rspamd UI worker password
[Dovecot] Pull Spamassassin ruleset to be read by Rspamd (MANY THANKS to Peer Heinlein!)
[Dovecot] Garbage collector for deleted maildirs (set keep time via MAILDIR_GC_TIME which defaults to 1440 minutes)
[Web] Flush memcached after mailbox item changes, fixes #1808
[Web] Fix duplicate IDs, fixes #1792
[Compose] Use SQL sockets
[PHP-FPM] Update APCu and Redis libs
[Dovecot] Encrypt maildir with global key pair in crypt-vol-1 (BACKUP!), also fixes #1791
[Web] Fix deletion of spam aliases
[Helper] Add "crypt" to backup script
[Helper] Override file for external SQL socket (not supported!)
[Compose] New images for Rspamd, PHP-FPM, SOGo, Dovecot, Docker API, Watchdog, ACME, Postfix
2018-09-29 22:01:23 +02:00
|
|
|
mysql --socket=/var/run/mysqld/mysqld.sock -u ${DBUSER} -p${DBPASS} ${DBNAME} << EOF
|
2018-10-11 11:53:22 +02:00
|
|
|
DELETE FROM api;
|
|
|
|
INSERT INTO api (api_key, active, allow_from) VALUES ("${API_KEY}", "1", "${VALIDATED_IPS}");
|
2018-06-08 09:11:03 +02:00
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2017-03-02 11:23:23 +01:00
|
|
|
exec "$@"
|