Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
comanage-registry-docker/comanage-registry-mailman/core/docker-entrypoint.sh
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
227 lines (189 sloc)
6.65 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# GNU Mailman 3 Core for COmanage Registry Dockerfile entrypoint | |
# | |
# This bash script borrows heavily from a script by Abhilash Raj for | |
# GNU Mailman 3. See https://github.com/maxking/docker-mailman | |
# | |
# Portions licensed to the University Corporation for Advanced Internet | |
# Development, Inc. ("UCAID") under one or more contributor license agreements. | |
# See the NOTICE file distributed with this work for additional information | |
# regarding copyright ownership. | |
# | |
# UCAID licenses this file to you under the Apache License, Version 2.0 | |
# (the "License"); you may not use this file except in compliance with the | |
# License. You may obtain a copy of the License at: | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
set -e | |
function wait_for_postgres () { | |
# Check if the postgres database is up and accepting connections before | |
# moving forward. | |
# TODO: Use python's psycopg2 module to do this in python instead of | |
# installing postgres-client in the image. | |
until psql $MAILMAN_DATABASE_URL -c '\l'; do | |
>&2 echo "Postgres is unavailable - sleeping" | |
sleep 1 | |
done | |
>&2 echo "Postgres is up - continuing" | |
} | |
function wait_for_mysql () { | |
# Check if MySQL is up and accepting connections. | |
HOSTNAME=$(python3 -c "from urllib.parse import urlparse; o = urlparse('$MAILMAN_DATABASE_URL'); print(o.hostname);") | |
until mysqladmin ping --host "$HOSTNAME" --silent; do | |
>&2 echo "MySQL is unavailable - sleeping" | |
sleep 1 | |
done | |
>&2 echo "MySQL is up - continuing" | |
} | |
# Configuration details that may be injected through environment | |
# variables or the contents of files. | |
injectable_config_vars=( | |
HYPERKITTY_API_KEY | |
MAILMAN_DATABASE_URL | |
MAILMAN_REST_PASSWORD | |
) | |
# If the file associated with a configuration variable is present then | |
# read the value from it into the appropriate variable. | |
for config_var in "${injectable_config_vars[@]}" | |
do | |
eval file_name=\$"${config_var}_FILE"; | |
if [ -e "$file_name" ]; then | |
declare "${config_var}"=`cat $file_name` | |
fi | |
done | |
# Empty the config file. | |
echo "# This file is autogenerated at container startup." > /etc/mailman.cfg | |
# Check if $MM_HOSTNAME is set, if not, set it to the value returned by | |
# `hostname -i` command to set it to whatever IP address is assigned to the | |
# container. | |
if [[ ! -v MM_HOSTNAME ]]; then | |
export MM_HOSTNAME=`hostname -i` | |
fi | |
if [[ ! -v SMTP_HOST ]]; then | |
export SMTP_HOST='172.19.199.1' | |
fi | |
if [[ ! -v SMTP_PORT ]]; then | |
export SMTP_PORT=25 | |
fi | |
# Check if REST port, username, and password are set, if not, set them | |
# to default values. | |
if [[ ! -v MAILMAN_REST_PORT ]]; then | |
export MAILMAN_REST_PORT='8001' | |
fi | |
if [[ ! -v MAILMAN_REST_USER ]]; then | |
export MAILMAN_REST_USER='restadmin' | |
fi | |
if [[ ! -v MAILMAN_REST_PASSWORD ]]; then | |
export MAILMAN_REST_PASSWORD='restpass' | |
fi | |
function setup_database () { | |
if [[ ! -v MAILMAN_DATABASE_URL ]] | |
then | |
echo "Environemnt variable MAILMAN_DATABASE_URL should be defined..." | |
exit 1 | |
fi | |
# Translate mysql:// urls to mysql+mysql:// backend: | |
if [[ "$MAILMAN_DATABASE_URL" == mysql://* ]]; then | |
MAILMAN_DATABASE_URL="mysql+pymysql://${MAILMAN_DATABASE_URL:8}" | |
echo "Database URL was automatically rewritten to: $MAILMAN_DATABASE_URL" | |
fi | |
# If MAILMAN_DATABASE_CLASS is not set, guess it for common databases: | |
if [ -z "$MAILMAN_DATABASE_CLASS" ]; then | |
if [[ ("$MAILMAN_DATABASE_URL" == mysql:*) || | |
("$MAILMAN_DATABASE_URL" == mysql+*) ]]; then | |
MAILMAN_DATABASE_CLASS=mailman.database.mysql.MySQLDatabase | |
fi | |
if [[ ("$MAILMAN_DATABASE_URL" == postgres:*) || | |
("$MAILMAN_DATABASE_URL" == postgres+*) ]]; then | |
MAILMAN_DATABASE_CLASS=mailman.database.postgresql.PostgreSQLDatabase | |
fi | |
fi | |
cat >> /etc/mailman.cfg <<EOF | |
[database] | |
class: $MAILMAN_DATABASE_CLASS | |
url: $MAILMAN_DATABASE_URL | |
EOF | |
} | |
# Check if $MAILMAN_DATABASE_URL is defined, if not, use a standard sqlite database. | |
# | |
# If the $MAILMAN_DATABASE_URL is defined and is postgres, check if it is available | |
# yet. Do not start the container before the postgresql boots up. | |
# | |
# TODO: If the $MAILMAN_DATABASE_URL is defined and is mysql, check if the database is | |
# available before the container boots up. | |
# | |
# TODO: Check the database type and detect if it is up based on that. For now, | |
# assume that postgres is being used if MAILMAN_DATABASE_URL is defined. | |
if [[ ! -v MAILMAN_DATABASE_URL ]]; then | |
echo "MAILMAN_DATABASE_URL is not defined. Using sqlite database..." | |
else | |
setup_database | |
fi | |
if [[ "$MAILMAN_DATABASE_TYPE" = 'postgres' ]] | |
then | |
wait_for_postgres | |
elif [[ "$MAILMAN_DATABASE_TYPE" = 'mysql' ]] | |
then | |
wait_for_mysql | |
fi | |
# Generate a basic mailman.cfg. | |
cat >> /etc/mailman.cfg <<EOF | |
[mta] | |
incoming: mailman.mta.postfix.LMTP | |
outgoing: mailman.mta.deliver.deliver | |
lmtp_host: $MM_HOSTNAME | |
lmtp_port: 8024 | |
smtp_host: $SMTP_HOST | |
smtp_port: $SMTP_PORT | |
configuration: /etc/postfix-mailman.cfg | |
[runner.retry] | |
sleep_time: 10s | |
[webservice] | |
hostname: $MM_HOSTNAME | |
port: $MAILMAN_REST_PORT | |
admin_user: $MAILMAN_REST_USER | |
admin_pass: $MAILMAN_REST_PASSWORD | |
[archiver.hyperkitty] | |
class: mailman_hyperkitty.Archiver | |
enable: yes | |
configuration: /etc/mailman-hyperkitty.cfg | |
EOF | |
# Generate a basic configuration to use postfix. | |
cat > /etc/postfix-mailman.cfg <<EOF | |
[postfix] | |
transport_file_type: regex | |
# While in regex mode, postmap_command is never used, a placeholder | |
# is added here so that it doesn't break anything. | |
postmap_command: true | |
EOF | |
if [[ -e /opt/mailman/mailman-extra.cfg ]] | |
then | |
echo "Found configuration file at /opt/mailman/mailman-extra.cfg" | |
cat /opt/mailman/mailman-extra.cfg >> /etc/mailman.cfg | |
fi | |
if [[ ! -v HYPERKITTY_API_KEY ]]; then | |
echo "HYPERKITTY_API_KEY not defined, please set this environment variable..." | |
echo "exiting..." | |
exit 1 | |
fi | |
if [[ ! -v HYPERKITTY_URL ]]; then | |
echo "HYPERKITTY_URL not set, using the default value of http://mailman-web:8000/hyperkitty" | |
export HYPERKITTY_URL="http://mailman-web:8000/hyperkitty/" | |
fi | |
# Generate a basic mailman-hyperkitty.cfg. | |
cat > /etc/mailman-hyperkitty.cfg <<EOF | |
[general] | |
base_url: $HYPERKITTY_URL | |
api_key: $HYPERKITTY_API_KEY | |
EOF | |
# Generate the LMTP files for postfix if needed. | |
mailman aliases | |
# Now chown the places where mailman wants to write stuff. | |
chown -R mailman /opt/mailman | |
exec su-exec mailman "$@" |