docker-cron-global-stack/start_or_update.sh

52 lines
1.9 KiB
Bash
Raw Normal View History

2020-02-26 17:11:28 +00:00
#!/bin/bash
2024-03-03 16:58:17 +00:00
#Absolute path to this script
SCRIPT=$(readlink -f $0)
#Absolute path this script is in
SCRIPTPATH=$(dirname $SCRIPT)
STACK=$(basename "$SCRIPTPATH")
export RUN_DIRECTORY="/usr/local/docker-mounted-files/${STACK}"
cd "$SCRIPTPATH"
2020-08-08 15:40:54 +00:00
source vars
2024-03-03 16:58:17 +00:00
sudo mkdir -p "${RUN_DIRECTORY}"
sudo chmod a+rx "${RUN_DIRECTORY}"
sudo cp config "${RUN_DIRECTORY}"
cd "${RUN_DIRECTORY}"
2020-03-21 16:28:28 +00:00
test -z $KEY && { echo "KEY is not defined."; exit 1; }
2020-02-26 17:11:28 +00:00
test -z $1 || HOST="_$1"
test -z $2 || INSTANCE="_$2"
if ! test -f ~/secrets.tar.gz.enc
then
2020-08-08 15:40:54 +00:00
curl -o ~/secrets.tar.gz.enc "https://${CLOUD_SERVER}/s/${KEY}/download?path=%2F&files=secrets.tar.gz.enc"
2020-02-26 17:11:28 +00:00
if ! test -f ~/secrets.tar.gz.enc
then
echo "ERROR: ~/secrets.tar.gz.enc not found, exiting."
exit 1
fi
fi
2020-03-27 20:03:30 +00:00
openssl enc -aes-256-cbc -d -in ~/secrets.tar.gz.enc \
| sudo tar -zxv --strip 2 secrets/docker-cron-global-stack${HOST}${INSTANCE}/crontab.yaml \
secrets/docker-cron-global-stack${HOST}${INSTANCE}/scripts \
secrets/bootstrap/id_rsa secrets/bootstrap/id_rsa.pub \
2020-03-21 16:28:28 +00:00
|| { echo "Could not extract from secrets archive, exiting."; rm -f ~/secrets.tar.gz.enc; exit 1; }
2020-03-27 20:03:30 +00:00
sudo chown root. crontab.yaml id_rsa id_rsa.pub config scripts
2020-03-21 16:07:19 +00:00
sudo chmod 644 crontab.yaml
2020-03-27 20:03:30 +00:00
sudo chmod 400 id_rsa id_rsa.pub config
2020-02-26 17:11:28 +00:00
2024-03-03 16:58:17 +00:00
cd "$SCRIPTPATH"
2020-02-26 17:11:28 +00:00
unset VERSION_CRON
2020-08-08 15:40:54 +00:00
export VERSION_CRON=$(git ls-remote https://${GIT_SERVER}/yohan/docker-cron.git| head -1 | cut -f 1|cut -c -10)
2020-02-26 17:11:28 +00:00
rm -rf ~/build
mkdir -p ~/build
2020-08-08 15:40:54 +00:00
git clone https://${GIT_SERVER}/yohan/docker-cron.git ~/build/docker-cron
2020-02-26 17:11:28 +00:00
sudo docker build -t cron:$VERSION_CRON ~/build/docker-cron
sudo -E bash -c 'docker-compose up -d --force-recreate'
# --force-recreate is used to recreate container when crontab file has changed
# We cannot remove the secrets files or restarting the container would become impossible
# rm -f crontab
rm -rf ~/build