Permalink
Fetching contributors…
Cannot retrieve contributors at this time
executable file 96 lines (81 sloc) 3.24 KB
#!/bin/sh
root_option_file="$SNAP_DATA/mysql/root.ini"
owncloud_password_file="$SNAP_DATA/mysql/owncloud_password"
mysqld_pid_file_path=$SNAP_DATA/mysql/`hostname`.pid
mysql_socket_file_path=$SNAP_DATA/mysql/mysql.sock
new_install=false
# Make sure the database is initialized (this is safe to run if already
# initialized)
mysqld --initialize-insecure --basedir="$SNAP" --datadir="$SNAP_DATA/mysql" --lc-messages-dir="$SNAP/share"
# If the above command succeeded, it means this is a new installation.
if [ $? -eq 0 ]; then
new_install=true
fi
# Start mysql
$SNAP/support-files/mysql.server start
# Initialize new installation if necessary.
if [ $new_install = true ]; then
# Generate a password for the root mysql user.
echo -n "Generating root mysql password... "
root_password=$(cat /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c64)
echo "done."
# Generate a password for the owncloud mysql user.
echo -n "Generating owncloud mysql password... "
owncloud_password=$(cat /dev/urandom | tr -dc _A-Z-a-z-0-9 | head -c64)
echo "done."
# Save root user information
echo "[client]" >> $root_option_file
echo "socket=$mysql_socket_file_path" >> $root_option_file
echo "user=root" >> $root_option_file
chmod 600 $root_option_file
# Now set everything up in one step:
# 1) Set the root user's password
# 2) Create the owncloud user
# 3) Create the owncloud database
# 4) Grant the owncloud user privileges on the owncloud database
echo -n "Setting up users and owncloud database... "
mysql --defaults-file=$root_option_file <<SQL
ALTER USER 'root'@'localhost' IDENTIFIED BY '$root_password';
CREATE USER 'owncloud'@'localhost' IDENTIFIED BY '$owncloud_password';
CREATE DATABASE owncloud;
GRANT ALL PRIVILEGES ON owncloud.* TO 'owncloud'@'localhost' IDENTIFIED BY '$owncloud_password';
SQL
if [ $? -eq 0 ]; then
echo "done."
else
echo "Failed to initialize-- reverting..."
$SNAP/support-files/mysql.server stop
rm -rf $SNAP_DATA/mysql/*
fi
# Now the root mysql user has a password. Save that as well.
echo "password=$root_password" >> $root_option_file
fi
# Wait here until mysql is running
echo "Waiting for server..."
while [ ! -f "$mysqld_pid_file_path" -o ! -S "$mysql_socket_file_path" ]; do
sleep 1
done
# Check and upgrade mysql tables if necessary. This will return 0 if the upgrade
# succeeded, in which case we need to restart mysql.
echo "Checking/upgrading mysql tables if necessary..."
mysql_upgrade --defaults-file=$root_option_file
if [ $? -eq 0 ]; then
echo "Restarting mysql server after upgrade..."
$SNAP/support-files/mysql.server restart
echo "Waiting for server to come back after upgrade..."
while [ ! -f "$mysqld_pid_file_path" -o ! -S "$mysql_socket_file_path" ]; do
sleep 1
done
fi
# If this was a new installation, wait until the server is all up and running
# before saving off the owncloud user's password. This way the presence of the
# file can be used as a signal that mysql is ready to be used.
if [ $new_install = true ]; then
echo "$owncloud_password" > $owncloud_password_file
fi
# Wait here until mysql exits (turn a forking service into simple). This is
# only needed for Ubuntu Core 15.04, as 16.04 supports forking services.
mysqld_pid=$(cat "$mysqld_pid_file_path")
while kill -0 $mysqld_pid 2>/dev/null; do
sleep 1
done