From d133dcadabc1ffd8222a606c75e8f27a48a1f789 Mon Sep 17 00:00:00 2001 From: yohan <783b8c87@scimetis.net> Date: Tue, 30 Jan 2024 11:26:25 +0100 Subject: [PATCH] Initial commit. --- create.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 31 +++++++++++++++++++++++++ install.sh | 24 ++++++++++++++++++++ start.sh | 8 +++++++ vars | 4 ++++ 5 files changed, 123 insertions(+) create mode 100755 create.sh create mode 100644 docker-compose.yml create mode 100755 install.sh create mode 100755 start.sh create mode 100644 vars diff --git a/create.sh b/create.sh new file mode 100755 index 0000000..0caebea --- /dev/null +++ b/create.sh @@ -0,0 +1,56 @@ +#!/bin/bash +#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" +source vars + +sudo docker-compose down +sudo rm -rf "${RUN_DIRECTORY}" +sudo mkdir -p "${RUN_DIRECTORY}" +sudo chmod a+rx "${RUN_DIRECTORY}" + +cd "${RUN_DIRECTORY}" +test -z $1 || HOST="_$1" +test -z $2 || INSTANCE="_$2" +test -z $KEY && { echo "KEY is not defined."; exit 1; } + +if ! test -f ~/secrets.tar.gz.enc +then + curl -o ~/secrets.tar.gz.enc "https://${CLOUD_SERVER}/s/${KEY}/download?path=%2F&files=secrets.tar.gz.enc" + if ! test -f ~/secrets.tar.gz.enc + then + echo "ERROR: ~/secrets.tar.gz.enc not found, exiting." + exit 1 + fi +fi + +openssl enc -aes-256-cbc -d -in ~/secrets.tar.gz.enc \ +| sudo tar -zxv --strip 2 secrets/${STACK}${HOST}${INSTANCE}/conf.yml \ +|| { echo "Could not extract from secrets archive, exiting."; rm -f ~/secrets.tar.gz.enc; exit 1; } +sudo chown root. conf.yml + +cd "$SCRIPTPATH" +for NETWORK in metricsnet mysqlnet +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 + +IMAGE="store-metrics-in-db" +REPO="docker-store-metrics-in-db" +unset VERSION_STORE_METRICS_IN_DB +export VERSION_STORE_METRICS_IN_DB=$(git ls-remote https://${GIT_SERVER}/yohan/${REPO}.git| head -1 | cut -f 1|cut -c -10) + +mkdir -p ~/build +git clone https://${GIT_SERVER}/yohan/${REPO}.git ~/build/${REPO} +sudo docker build -t ${IMAGE}:$VERSION_STORE_METRICS_IN_DB ~/build/${REPO} + +sudo -E bash -c 'docker-compose up --no-start --force-recreate' + +rm -rf ~/build diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..ac2359e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,31 @@ +version: "2.1" + +services: + + store-metrics-in-db: + container_name: store-metrics-in-db + image: store-metrics-in-db:$VERSION_STORE_METRICS_IN_DB + networks: + - mysqlnet + - metricsnet + ports: + - "3001:3001/tcp" + expose: + - "3001/tcp" + environment: + FLASK_APP: api.py + #FLASK_ENV: development + FLASK_ENV: production + FLASK_RUN_HOST: "0.0.0.0" + FLASK_RUN_PORT: 3001 + volumes: + - /mnt/volumes/store-metrics-in-db/data/migrations:/root/migrations:z + - ${RUN_DIRECTORY}/conf.yml:/root/conf.yml + +networks: + + mysqlnet: + external: true + + metricsnet: + external: true diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..144561f --- /dev/null +++ b/install.sh @@ -0,0 +1,24 @@ +#!/bin/bash +#Absolute path to this script +SCRIPT=$(readlink -f $0) +#Absolute path this script is in +SCRIPTPATH=$(dirname $SCRIPT) +STACK=$(basename "$SCRIPTPATH") + +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 + +[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/vars b/vars new file mode 100644 index 0000000..ee32044 --- /dev/null +++ b/vars @@ -0,0 +1,4 @@ +#!/bin/bash +export CLOUD_SERVER=cloud.scimetis.net +export GIT_SERVER=git.scimetis.net +