From 43f89791f6e5361481795cd4f2a3e575876c3a60 Mon Sep 17 00:00:00 2001 From: yohan <783b8c87@scimetis.net> Date: Tue, 31 Mar 2020 16:03:59 +0200 Subject: [PATCH] Using systemd to mount volumes and start containers at boot. --- start_or_update.sh => create.sh | 27 +++++++++------------ docker-compose.yml | 1 - install.sh | 42 +++++++++++++++++++++++++++++++++ start.sh | 8 +++++++ volume_list | 2 ++ 5 files changed, 63 insertions(+), 17 deletions(-) rename start_or_update.sh => create.sh (67%) create mode 100755 install.sh create mode 100755 start.sh create mode 100644 volume_list diff --git a/start_or_update.sh b/create.sh similarity index 67% rename from start_or_update.sh rename to create.sh index 8051f1f..e85cf60 100755 --- a/start_or_update.sh +++ b/create.sh @@ -1,26 +1,21 @@ #!/bin/bash +#Absolute path to this script +SCRIPT=$(readlink -f $0) +#Absolute path this script is in +SCRIPTPATH=$(dirname $SCRIPT) + +cd $SCRIPTPATH + test -z $1 || HOST="_$1" test -z $2 || INSTANCE="_$2" test -f ~/secrets.tar.gz.enc || { echo "ERROR: ~/secrets.tar.gz.enc not found, exiting."; exit 1; } -openssl enc -aes-256-cbc -d -in ~/secrets.tar.gz.enc | tar -zxv --strip 2 secrets/docker-mysql-stack${HOST}${INSTANCE}/debian.cnf +openssl enc -aes-256-cbc -d -in ~/secrets.tar.gz.enc \ +| sudo tar -zxv --strip 2 secrets/docker-mysql-stack${HOST}${INSTANCE}/debian.cnf \ +|| { echo "Could not extract from secrets archive, exiting."; rm -f ~/secrets.tar.gz.enc; exit 1; } sudo chown root. debian.cnf test -f ~/openrc.sh || { echo "ERROR: ~/openrc.sh not found, exiting."; exit 1; } source ~/openrc.sh -INSTANCE=$(/home/yohan/env_py3/bin/openstack server show -c id --format value $(hostname)) -for VOLUME in mysql-server_data mysql-server_dumps -do - sudo mkdir -p /mnt/volumes/${VOLUME} - if ! mountpoint -q /mnt/volumes/${VOLUME} - then - VOLUME_ID=$(/home/yohan/env_py3/bin/openstack volume show ${VOLUME} -c id --format value) - test -e /dev/disk/by-id/*${VOLUME_ID:0:20} || nova volume-attach $INSTANCE $VOLUME_ID auto - sleep 3 - sudo mount /dev/disk/by-id/*${VOLUME_ID:0:20} /mnt/volumes/${VOLUME} - mountpoint -q /mnt/volumes/${VOLUME} || { echo "ERROR: could not mount /mnt/volumes/${VOLUME}, exiting."; exit 1; } - fi -done - export OS_REGION_NAME=GRA test -f ~/duplicity_password.sh || { echo "ERROR: ~/duplicity_password.sh not found, exiting."; exit 1; } source ~/duplicity_password.sh @@ -49,7 +44,7 @@ cd ~/build/docker-mysql; export VERSION_MYSQL=$(git show-ref --head| head -1 | c sudo docker build -t mysql-server:$VERSION_MYSQL ~/build/docker-mysql -sudo -E bash -c 'docker-compose up -d --force-recreate' +sudo -E bash -c 'docker-compose up --no-start --force-recreate' # --force-recreate is used to recreate container when a file has changed # We cannot remove the secrets files or restarting the container would become impossible # rm -f debian.cnf diff --git a/docker-compose.yml b/docker-compose.yml index a182db7..ab94f41 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,6 @@ services: image: mysql-server:$VERSION_MYSQL #build: "https://git.scimetis.net/yohan/docker-mysql.git" container_name: mysql-server - restart: always networks: - mysqlnet volumes: diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..a1e79a3 --- /dev/null +++ b/install.sh @@ -0,0 +1,42 @@ +#!/bin/bash +#Absolute path to this script +SCRIPT=$(readlink -f $0) +#Absolute path this script is in +SCRIPTPATH=$(dirname $SCRIPT) +STACK=$(basename "$SCRIPTPATH") + +for VOLUME in $(awk NF $SCRIPTPATH/volume_list | tr -d "[:blank:]") +do + systemctl enable mnt-cinder-volume@${VOLUME}.service +done + +cat << EOF > /etc/systemd/system/${STACK}.service +[Unit] +Description=Starting ${STACK} +After=network-online.target firewalld.service docker.service docker.socket +Wants=network-online.target docker.service +Requires=docker.socket +EOF + +for VOLUME in $(awk NF $SCRIPTPATH/volume_list | tr -d "[:blank:]") +do + cat << EOF >> /etc/systemd/system/${STACK}.service +After=mnt-cinder-volume@${VOLUME}.service +After=mnt-volumes-${VOLUME}.mount +Wants=mnt-cinder-volume@${VOLUME}.service +Requires=mnt-volumes-${VOLUME}.mount + +EOF +done + +cat << EOF >> /etc/systemd/system/${STACK}.service +[Service] +Type=oneshot +User=$SUDO_USER +ExecStart=$SCRIPTPATH/start.sh + +[Install] +WantedBy=multi-user.target +EOF +systemctl daemon-reload +systemctl enable ${STACK}.service diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..1f4bfdf --- /dev/null +++ b/start.sh @@ -0,0 +1,8 @@ +#!/bin/bash +#Absolute path to this script +SCRIPT=$(readlink -f $0) +#Absolute path this script is in +SCRIPTPATH=$(dirname $SCRIPT) + +cd $SCRIPTPATH +sudo docker-compose start diff --git a/volume_list b/volume_list new file mode 100644 index 0000000..6aaacc1 --- /dev/null +++ b/volume_list @@ -0,0 +1,2 @@ +mysql-server_data +mysql-server_dumps