Skip to content

Commit

Permalink
M #-: Add supervisord services definition (#269)
Browse files Browse the repository at this point in the history
* M #-: Add supervisord services definition

Currently only for CentOS 8 but there should be needed only few changes
to make it work on other systems: crond, apache/httpd, mysql/mariadb...

The rest is pretty much system agnostic.

Signed-off-by: Petr Ospalý <[email protected]>

* M #-: Adjust supervisor directories

Co-authored-by: Vlastimil Holer <[email protected]>
  • Loading branch information
Petr Ospalý and Vlastimil Holer authored Sep 30, 2020
1 parent 9e65e44 commit 6fb4b2a
Show file tree
Hide file tree
Showing 38 changed files with 1,047 additions and 0 deletions.
3 changes: 3 additions & 0 deletions share/pkgs/services/supervisor/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
These definitions are for the Supervisor (http://supervisord.org) to manage
services within container, where systemd is not available. They are not
expected to be a general purpose services and avoid using them!
149 changes: 149 additions & 0 deletions share/pkgs/services/supervisor/centos8/scripts/lib/functions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#!/bin/sh

# here are shared functions for all supervised services

msg()
(
echo "[SUPERVISOR]: ${SUPERVISOR_PROCESS_NAME}: $*"
)

err()
(
echo "[SUPERVISOR] [!] ERROR: ${SUPERVISOR_PROCESS_NAME}: $*"
)

is_running()
(
_status=$(LANG=C supervisorctl status "$1" | awk '{print $2}')

case "$_status" in
RUNNING)
return 0
;;
esac

return 1
)

check_pidfile()
(
if [ -f "$1" ] ; then
_pid=$(cat "$1")
else
return 1
fi

if ! kill -0 ${_pid} ; then
return 1
fi

return 0
)

wait_for_oned()
(
TIMEOUT="${TIMEOUT:-120}"

while [ "$TIMEOUT" -gt 0 ] ; do
if oneuser list -x \
--endpoint "http://${OPENNEBULA_ONED_HOSTNAME}:${OPENNEBULA_ONED_APIPORT}/RPC2" \
> /dev/null 2>&1 \
;
then
return 0
fi

TIMEOUT=$(( TIMEOUT - 1 ))
sleep 1
done

return 1
)

wait_for_memcached()
(
TIMEOUT="${TIMEOUT:-120}"

while [ "$TIMEOUT" -gt 0 ] ; do
if echo stats | nc "${OPENNEBULA_MEMCACHED_HOSTNAME}" 11211 \
> /dev/null 2>&1 \
;
then
return 0
fi

TIMEOUT=$(( TIMEOUT - 1 ))
sleep 1
done

return 1
)

wait_for_ssh_agent()
(
TIMEOUT="${TIMEOUT:-120}"

while [ "$TIMEOUT" -gt 0 ] ; do
if [ -e ${SSH_AUTH_SOCK} ] ; then
return 0
fi

TIMEOUT=$(( TIMEOUT - 1 ))
sleep 1
done

return 1
)

wait_for_mysqld()
(
TIMEOUT="${TIMEOUT:-120}"

while [ "$TIMEOUT" -gt 0 ] ; do
if check_pidfile /var/run/mariadb/mariadb.pid ; then
return 0
fi

TIMEOUT=$(( TIMEOUT - 1 ))
sleep 1
done

return 1
)

wait_for_opennebula_db()
(
TIMEOUT="${TIMEOUT:-120}"

while [ "$TIMEOUT" -gt 0 ] ; do
if mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -D "$MYSQL_DATABASE" \
-u "$MYSQL_USER" -p"$MYSQL_PASSWORD" \
-e 'exit' \
;
then
return 0
fi

TIMEOUT=$(( TIMEOUT - 1 ))
sleep 1s
done

return 1
)

wait_for_file()
(
TIMEOUT="${TIMEOUT:-120}"

while [ "$TIMEOUT" -gt 0 ] ; do
if [ -f "$1" ] ; then
return 0
fi

TIMEOUT=$(( TIMEOUT - 1 ))
sleep 1
done

return 1
)

26 changes: 26 additions & 0 deletions share/pkgs/services/supervisor/centos8/scripts/memcached.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

set -e

#
# functions
#

. /usr/share/one/supervisor/scripts/lib/functions.sh

#
# run service
#

for envfile in \
/etc/sysconfig/memcached \
;
do
if [ -f "$envfile" ] ; then
. "$envfile"
fi
done

msg "Service started!"
exec /usr/bin/memcached -p ${PORT} -u ${USER} -m ${CACHESIZE} -c ${MAXCONN} $OPTIONS

124 changes: 124 additions & 0 deletions share/pkgs/services/supervisor/centos8/scripts/mysqld-configure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#!/bin/sh

set -e

# give up after two minutes
TIMEOUT=120

#
# functions
#

. /usr/share/one/supervisor/scripts/lib/functions.sh

is_root_password_unset()
(
_check=$(mysql -u root -s -N -e 'select CURRENT_USER();')
case "$_check" in
root@*)
return 0
;;
*)
return 1
;;
esac

return 1
)

is_root_password_valid()
(
_check=$(mysql -u root -p"${MYSQL_ROOT_PASSWORD}" -s -N -e 'select CURRENT_USER();')
case "$_check" in
root@*)
return 0
;;
*)
return 1
;;
esac

return 1
)

#
# run service
#

# we are talking locally and this pollutes our env.
unset MYSQL_HOST
unset MYSQL_PORT

# wait for mysqld
msg "Wait for mysqld process..."
if ! wait_for_mysqld ; then
err "Timeout!"
exit 1
fi

msg "Start configuration - mysqld is running"

# create password, user and database if requested

# root password
if [ -n "$MYSQL_ROOT_PASSWORD" ] ; then
msg "Setup root password"
if is_root_password_unset ; then
mysql -u root <<EOF
SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED;
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('${MYSQL_ROOT_PASSWORD}');
FLUSH PRIVILEGES;
EOF
else
if ! is_root_password_valid ; then
# TODO: support the change of root password?
err "The root password was already set and differs - ABORT"
exit 1
fi
fi
fi

# create user and database
if [ -n "$MYSQL_USER" ] \
&& [ -n "$MYSQL_PASSWORD" ] \
&& [ -n "$MYSQL_DATABASE" ] ;
then
msg "Setup the mysql database and its user"

mysql -u root -p"${MYSQL_ROOT_PASSWORD}" <<EOF
CREATE DATABASE IF NOT EXISTS ${MYSQL_DATABASE};
GRANT ALL PRIVILEGES on ${MYSQL_DATABASE}.* to '${MYSQL_USER}'@'%' identified by '${MYSQL_PASSWORD}';
FLUSH PRIVILEGES;
EOF
fi

# secure the mysql installation
msg "Secure the installation"
LANG=C expect -f - <<EOF
set timeout 10
spawn mysql_secure_installation
expect "Enter current password for root (enter for none):"
send "${MYSQL_ROOT_PASSWORD}\n"
expect "Set root password?"
send "n\n"
expect "Remove anonymous users?"
send "Y\n"
expect "Disallow root login remotely?"
send "Y\n"
expect "Remove test database and access to it?"
send "Y\n"
expect "Reload privilege tables now?"
send "Y\n"
expect eof
EOF

# TODO: either this or dealing with a service in EXITED status
msg "Service finished! (entered infinity sleep)"
exec /bin/sleep infinity
34 changes: 34 additions & 0 deletions share/pkgs/services/supervisor/centos8/scripts/mysqld-upgrade.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh

set -e

# give up after two minutes
TIMEOUT=120

#
# functions
#

. /usr/share/one/supervisor/scripts/lib/functions.sh

#
# run service
#

# we are talking locally and this pollutes our env.
unset MYSQL_HOST
unset MYSQL_PORT

# wait for mysqld
msg "Wait for mysqld process..."
if ! wait_for_mysqld ; then
err "Timeout!"
exit 1
fi

msg "Try to upgrade the database - mysqld is running"
/usr/libexec/mysql-check-upgrade

# TODO: either this or dealing with a service in EXITED status
msg "Service finished! (entered infinity sleep)"
exec /bin/sleep infinity
46 changes: 46 additions & 0 deletions share/pkgs/services/supervisor/centos8/scripts/mysqld.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/sh

set -e

# give up after two minutes
TIMEOUT=120

#
# functions
#

. /usr/share/one/supervisor/scripts/lib/functions.sh

#
# run service
#

# we are talking locally and this pollutes our env.
unset MYSQL_HOST
unset MYSQL_PORT

msg "Check socket and initialize the database directory"
/usr/libexec/mysql-check-socket
/usr/libexec/mysql-prepare-db-dir

# emulate ExecStartPost from systemd service unit
msg "Setup upgrade and configure post-exec steps"
for _sv in \
mysqld-upgrade \
mysqld-configure \
;
do
if is_running "$_sv" ; then
supervisorctl stop "$_sv"
fi
done

# the following "ExecStartPost" services will wait until the pidfile creation
rm -f /var/run/mariadb/mariadb.pid
supervisorctl start mysqld-upgrade
supervisorctl start mysqld-configure

# Note: we set --basedir to prevent probes that might trigger SELinux alarms,
# per bug #547485
msg "Service started!"
exec /usr/libexec/mysqld --basedir=/usr
Loading

0 comments on commit 6fb4b2a

Please sign in to comment.