Using systemd to mount volumes and start containers at boot.

This commit is contained in:
yohan 2020-03-31 17:05:18 +02:00
parent 68974a9717
commit 0edc9c7230
5 changed files with 61 additions and 16 deletions

View File

@ -1,4 +1,11 @@
#!/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 reverse-proxy
do
sudo docker network inspect ${NETWORK} &> /dev/null && continue
@ -9,20 +16,6 @@ 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 reverse-proxy_conf reverse-proxy_conf_enabled reverse-proxy_letsencrypt
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
@ -52,6 +45,6 @@ cd ~/build/docker-reverse-proxy; export VERSION_PROXY=$(git show-ref --head| hea
sudo docker build -t reverse-proxy:$VERSION_PROXY ~/build/docker-reverse-proxy
sudo -E bash -c 'docker-compose up -d --force-recreate'
sudo -E bash -c 'docker-compose up --no-start --force-recreate'
rm -rf ~/build

View File

@ -5,7 +5,6 @@ services:
reverse-proxy:
image: reverse-proxy:$VERSION_PROXY
#build: "https://git.scimetis.net/yohan/docker-reverse-proxy.git"
restart: always
ports:
- 80:80/tcp
- 443:443/tcp

42
install.sh Executable file
View File

@ -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

8
start.sh Executable file
View File

@ -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

3
volume_list Normal file
View File

@ -0,0 +1,3 @@
reverse-proxy_conf
reverse-proxy_conf_enabled
reverse-proxy_letsencrypt