nixos/mysql: reformat, move logical steps into variables

define commands like "waiting for the mysql socket to appear" or "setup
initial databases" in a let expression, so the main control flow becomes
more readable.
This commit is contained in:
Florian Klink 2019-05-17 00:12:20 +02:00
parent 50dda813e2
commit 25494cc193

View file

@ -318,14 +318,12 @@ in
pkgs.nettools pkgs.nettools
]; ];
preStart = preStart = ''
'' if ! test -e ${cfg.dataDir}/mysql; then
if ! test -e ${cfg.dataDir}/mysql; then ${mysql}/bin/mysql_install_db --defaults-file=/etc/my.cnf ${installOptions}
${mysql}/bin/mysql_install_db --defaults-file=/etc/my.cnf ${installOptions} touch /tmp/mysql_init
touch /tmp/mysql_init fi
fi '';
'';
serviceConfig = { serviceConfig = {
Type = if hasNotify then "notify" else "simple"; Type = if hasNotify then "notify" else "simple";
@ -336,50 +334,52 @@ in
ExecStart = "${mysql}/bin/mysqld --defaults-file=/etc/my.cnf ${mysqldOptions} $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION"; ExecStart = "${mysql}/bin/mysqld --defaults-file=/etc/my.cnf ${mysqldOptions} $_WSREP_NEW_CLUSTER $_WSREP_START_POSITION";
}; };
postStart = '' postStart =
${lib.optionalString (!hasNotify) '' let
# Wait until the MySQL server is available for use cmdWatchForMysqlSocket = ''
count=0 # Wait until the MySQL server is available for use
while [ ! -e /run/mysqld/mysqld.sock ] count=0
do while [ ! -e /run/mysqld/mysqld.sock ]
if [ $count -eq 30 ] do
then if [ $count -eq 30 ]
echo "Tried 30 times, giving up..." then
exit 1 echo "Tried 30 times, giving up..."
fi exit 1
fi
echo "MySQL daemon not yet started. Waiting for 1 second..." echo "MySQL daemon not yet started. Waiting for 1 second..."
count=$((count++)) count=$((count++))
sleep 1 sleep 1
done done
''} '';
cmdInitialDatabases = concatMapStrings (database: ''
# Create initial databases
if ! test -e "${cfg.dataDir}/${database.name}"; then
echo "Creating initial database: ${database.name}"
( echo 'create database `${database.name}`;'
${optionalString (database.schema != null) ''
echo 'use `${database.name}`;'
# TODO: this silently falls through if database.schema does not exist,
# we should catch this somehow and exit, but can't do it here because we're in a subshell.
if [ -f "${database.schema}" ]
then
cat ${database.schema}
elif [ -d "${database.schema}" ]
then
cat ${database.schema}/mysql-databases/*.sql
fi
''}
) | ${mysql}/bin/mysql -u root -N
fi
'') cfg.initialDatabases;
in
lib.optionalString (!hasNotify) cmdWatchForMysqlSocket + ''
if [ -f /tmp/mysql_init ] if [ -f /tmp/mysql_init ]
then then
${concatMapStrings (database: ${cmdInitialDatabases}
''
# Create initial databases
if ! test -e "${cfg.dataDir}/${database.name}"; then
echo "Creating initial database: ${database.name}"
( echo 'create database `${database.name}`;'
${optionalString (database.schema != null) ''
echo 'use `${database.name}`;'
# TODO: this silently falls through if database.schema does not exist,
# we should catch this somehow and exit, but can't do it here because we're in a subshell.
if [ -f "${database.schema}" ]
then
cat ${database.schema}
elif [ -d "${database.schema}" ]
then
cat ${database.schema}/mysql-databases/*.sql
fi
''}
) | ${mysql}/bin/mysql -u root -N
fi
'') cfg.initialDatabases}
${optionalString (cfg.replication.role == "master") ${optionalString (cfg.replication.role == "master")
'' ''
# Set up the replication master # Set up the replication master