Skip to content

Commit

Permalink
Merge pull request #76 from lejmr/1.3
Browse files Browse the repository at this point in the history
1.3
  • Loading branch information
lejmr authored Aug 8, 2020
2 parents b8972ba + 9ec583d commit 80c72af
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 9 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# iRedMail Docker Container #

iRedMail allows to deploy an OPEN SOURCE, FULLY FLEDGED, FULL-FEATURED mail server in several minutes, for free. If several minutes is long time then this docker container can reduce the deployment time and help you to get a mail server in matter seconds.
iRedMail allows deployment of an OPEN SOURCE, FULLY FLEDGED, FULL-FEATURED mail server in several minutes, for free. If several minutes is long time then this docker container can reduce the deployment time and help you to get a mail server in the matter of seconds.

Current version of container uses MySQL for accounts saving. In the future the LDAP can be used, so pull requests are welcome. Container contains all components (Postfix, Dovecot, Fail2ban, ClamAV, Roundcube, and SoGo) and MySQL server. The hostname of the mail server can be set using the normal docker methods (```docker run -h <host>``` or setting 'hostname' in a docker compose file). In order to customize the container several environmental variables are allowed:
The current version of container uses MySQL for accounts saving. In the future the LDAP can be used, so pull requests are welcome. Container contains all components (Postfix, Dovecot, Fail2ban, ClamAV, Roundcube, and SoGo) and MySQL server. The hostname of the mail server can be set using the normal docker methods (```docker run -h <host>``` or setting 'hostname' in a docker compose file). In order to customize the container several environmental variables are allowed:

* MYSQL_ROOT_PASSWORD - Root password for MySQL server installation
* POSTMASTER_PASSWORD - Initial password for postmaster@DOMAIN. Password can be generated according to [wiki](http://www.iredmail.org/docs/reset.user.password.html). ({PLAIN}password)
Expand Down Expand Up @@ -32,4 +32,31 @@ docker run -p 80:80 -p 443:443 \
```

## Upgrade from version 1.0 or above

The iRedMail container gained automatic database schema migration with version 1.3 of the iRedMail container. What does it mean? It means upgrades should be smooth, and one wont no longer need to care about studying [release notes](https://docs.iredmail.org/iredmail.releases.html).

If you are running and older version of the container the automatic upgrade needs to be activated by installation of control table in vmail database using the following steps.

```
-- Switch to vmail database
use vmail
-- Create version tracking table
CREATE TABLE IF NOT EXISTS `versions` (
`component` varchar(120) NOT NULL,
`version` varchar(20) NOT NULL,
PRIMARY KEY(`component`)
);
-- Insert initial line representing installed version
INSERT INTO versions VALUES('iredmail', 'YOUR_CURRENT_VERSION');
```

The next step is just to upgrade the container version.

### Notes for contributors

When a new version of iRedMail gets released and I am not providing the upgrade. Feel free to open a pull request with migration stored in `mysql/static_files/opt/iredmail/migrations/DATABASE`. The file name should follow this schema:

```INDEX_IREDMAILVERSION__SHORTDESCRIPTION.sql```
4 changes: 2 additions & 2 deletions mysql/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM centos:7
MAINTAINER Miloš Kozák <milos.kozak@lejmr.com>

# Suporting software versions
ARG IREDMAIL_VERSION=1.1
ARG IREDMAIL_VERSION=1.3.1
ARG GOSU_VERSION=1.12

# Default values changable at startup
Expand Down Expand Up @@ -40,7 +40,7 @@ RUN yum install -y mariadb-server openssl \
AUTO_CLEANUP_REPLACE_MYSQL_CONFIG=y \
AUTO_CLEANUP_RESTART_POSTFIX=n \
bash iRedMail.sh \
&& yum install -y supervisor \
&& yum install -y supervisor MySQL-python python-webpy \
# Remove all dependencies and all caches
&& mkdir dumps \
&& for d in amavisd iredadmin iredapd roundcubemail sogo vmail mysql; do mysqldump $d | gzip -c > dumps/${d}.sql.gz; done \
Expand Down
3 changes: 3 additions & 0 deletions mysql/static_files/etc/supervisord.d/mariadb-server.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
[program:mariadb-server]
startsecs = 0
autorestart = true
stdout_logfile=/dev/fd/1
stdout_logfile_maxbytes=0
command=/services/mariadb-server.sh



# TODO: group restart seems to be problematic
[group:db-server]
programs=mariadb-server
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE msgs MODIFY COLUMN subject VARBINARY(255) NOT NULL DEFAULT '';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- https://docs.iredmail.org/upgrade.iredmail.1.2.1-1.3.html#mysqlmariadb-backend-special
CREATE INDEX msgs_idx_time_iso ON msgs (time_iso);
2 changes: 2 additions & 0 deletions mysql/static_files/services/iredadmin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

# Update MySQL password
. /opt/iredmail/.cv
DOMAIN=$(hostname -d)
sed -i "s/TEMP_IREDADMIN_DB_PASSWD/$IREDADMIN_DB_PASSWD/" /opt/www/iredadmin/settings.py
sed -i "s/TEMP_IREDAPD_DB_PASSWD/$IREDAPD_DB_PASSWD/" /opt/www/iredadmin/settings.py
sed -i "s/TEMP_VMAIL_DB_ADMIN_PASSWD/$VMAIL_DB_ADMIN_PASSWD/" /opt/www/iredadmin/settings.py
sed -i "s/DOMAIN/${DOMAIN}/g" /opt/www/iredadmin/settings.py

# Starting procedure based on SystemD service:
# /lib/systemd/system/iredadmin.service
Expand Down
2 changes: 1 addition & 1 deletion mysql/static_files/services/iredapd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ if [ -f ${PIDFILE} ]; then
fi

# Start iredapd
python ${BINPATH}
python3 ${BINPATH}


while [ ! -f /var/run/iredapd.pid ]
Expand Down
111 changes: 107 additions & 4 deletions mysql/static_files/services/mariadb-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,42 @@
export HOME="/root"
export USER="root"


# https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash
vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}

# Load global variables
. /opt/iredmail/conf/global

# Remove default .my.cnf file
test /root/.my.cnf && rm /root/.my.cnf* -f

Expand Down Expand Up @@ -107,8 +143,19 @@ EOF
echo "UPDATE mailbox SET password='${POSTMASTER_PASSWORD}' WHERE username='postmaster@${DOMAIN}';" | mysql vmail
fi

# Stop temporary instance
mysqladmin shutdown
# Create versioning table
cat << EOF | mysql vmail
-- CREATE TABLE
CREATE TABLE versions (
`component` varchar(120) NOT NULL,
`version` varchar(20) NOT NULL,
PRIMARY KEY(`component`)
);
-- INSERT iredmail version
INSERT INTO versions VALUES('iredmail', '${PROG_VERSION}');
EOF

else
### Update passwords for technical accounts
echo "*** Updating password credentials"
Expand Down Expand Up @@ -137,11 +184,67 @@ EOF
SET PASSWORD FOR 'iredapd'@'localhost' = PASSWORD('$IREDAPD_DB_PASSWD');
FLUSH PRIVILEGES;
EOF
fi

# Perform database update
# Make sure version table is installed and retrive current version from DB. This check is useful for
# upgrades from older versions where versions table is not available. In such table is installed while
# current iredmail version is inserted. Having an older versions, one can simply update the version in
# versions table and restart iredmail container. This way all migrations get applied
cat << EOF | mysql vmail
-- CREATE TABLE if not exists
CREATE TABLE IF NOT EXISTS \`versions\` (
\`component\` varchar(120) NOT NULL,
\`version\` varchar(20) NOT NULL,
PRIMARY KEY(\`component\`)
);
-- Insert initial version
INSERT IGNORE INTO versions VALUES('iredmail', '${PROG_VERSION}');
EOF

# Get iredmail's version off database (if empty)
CUR_VERSION=`mysql vmail -NB -e "SELECT version from versions WHERE component = 'iredmail';"`
echo "*** Current database schema version as of ${CUR_VERSION}"

# Install db migratios in case versions table returns current version. Otherwise
# nothing happens. Empty variable can only happen when versions table is just
# installed.
if [ "x$CUR_VERSION" != "x" ]; then

echo "*** Installing database migrations"

# stop temporary instance
mysqladmin shutdown
# Install all migrations of /opt/iredmail/migrations
for db in `ls /opt/iredmail/migrations/`; do

# Walk through all migration files
for migration in `ls /opt/iredmail/migrations/${db}/*_*__*.sql | sort -n`; do
fn=`basename ${migration}`
ver=`echo ${fn} | awk -F_ '{print $2}'`

# Compare versions
vercomp $ver $CUR_VERSION
if [ "x$?" == "x1" ]; then
# This migration script holds newer migration than current version, so installing
echo "** Installing ${migration}"

# Install migration file
mysql ${db} < ${migration}
fi
done
done
fi

# Update version in migration table
cat << EOF | mysql vmail
UPDATE versions
SET component = 'iredmail',
version = '${PROG_VERSION}'
WHERE component = 'iredmail';
EOF

# stop temporary instance
mysqladmin shutdown

# Normal database server start
echo "*** Starting MySQL database.."
# exec mysqld_safe
Expand Down

0 comments on commit 80c72af

Please sign in to comment.