From 103e752bb7943686222020cea36008b0382a458c Mon Sep 17 00:00:00 2001 From: yohan <783b8c87@scimetis.net> Date: Tue, 31 Mar 2020 16:52:16 +0200 Subject: [PATCH] Using systemd to mount volumes and start containers at boot. --- create.sh | 17 +++++++++++++++++ docker-compose.yml | 3 +-- install.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ start.sh | 8 ++++++++ start_or_update.sh | 26 -------------------------- volume_list | 1 + 6 files changed, 69 insertions(+), 28 deletions(-) create mode 100755 create.sh create mode 100755 install.sh create mode 100755 start.sh delete mode 100755 start_or_update.sh create mode 100644 volume_list diff --git a/create.sh b/create.sh new file mode 100755 index 0000000..0c59be3 --- /dev/null +++ b/create.sh @@ -0,0 +1,17 @@ +#!/bin/bash +#Absolute path to this script +SCRIPT=$(readlink -f $0) +#Absolute path this script is in +SCRIPTPATH=$(dirname $SCRIPT) + +cd $SCRIPTPATH + +for NETWORK in mailnet mysqlnet reverse-proxy +do + sudo docker network inspect ${NETWORK} &> /dev/null && continue + sudo docker network create ${NETWORK} + sudo docker network inspect ${NETWORK} &> /dev/null || \ + { echo "ERROR: could not create network ${NETWORK}, exiting."; exit 1; } +done + +sudo -E bash -c 'docker-compose up --no-start --build' diff --git a/docker-compose.yml b/docker-compose.yml index cd56648..51991ea 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,9 +4,8 @@ services: nextcloud: container_name: nextcloud-full - image: nextcloud:17.0.3 + image: nextcloud:18.0.3 build: ./docker/.examples/dockerfiles/full/apache/ - restart: unless-stopped networks: - reverse-proxy - mysqlnet 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/start_or_update.sh b/start_or_update.sh deleted file mode 100755 index d62feaf..0000000 --- a/start_or_update.sh +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/bash -for NETWORK in mailnet mysqlnet reverse-proxy -do - sudo docker network inspect ${NETWORK} &> /dev/null && continue - sudo docker network create ${NETWORK} - sudo docker network inspect ${NETWORK} &> /dev/null || \ - { echo "ERROR: could not create network ${NETWORK}, exiting."; exit 1; } -done - -test -f ~/openrc.sh || { echo "ERROR: ~/openrc.sh not found, exiting."; exit 1; } -source ~/openrc.sh -INSTANCE=$(~/env_py3/bin/openstack server show -c id --format value $(hostname)) -for VOLUME in nextcloud -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 - -sudo -E bash -c 'docker-compose up -d --build' diff --git a/volume_list b/volume_list new file mode 100644 index 0000000..705c7f8 --- /dev/null +++ b/volume_list @@ -0,0 +1 @@ +nextcloud