From 0f71299ebdf165076a9405b88d6d65c866c63b84 Mon Sep 17 00:00:00 2001 From: Override-6 Date: Sun, 4 Dec 2022 05:46:11 +0100 Subject: [PATCH] Drone CI setup, UTs, delivery and deployment now OP on development server --- drone/deliver.sh | 45 ++++++++++++++++++++++++++++++++++++++------- drone/deploy.sh | 29 +++++++++++++++++++++-------- drone/start.sh | 3 +++ 3 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 drone/start.sh diff --git a/drone/deliver.sh b/drone/deliver.sh index 77c9b82..44accf1 100644 --- a/drone/deliver.sh +++ b/drone/deliver.sh @@ -1,12 +1,43 @@ #!/usr/bin/env bash -apt update && apt install openjdk-11-jdk sftp -y +echo "installing packages..." +yum update > /dev/null +yum -y install openssh-clients openssh-client > /dev/null +echo "done." -PATH="$PATH:." +rm -rf /root/.ssh +mkdir -p /root/.ssh +echo "$SSH_PRIVATE_KEY" > /root/.ssh/id_rsa +echo "$SSH_PUBLIC_KEY" > /root/.ssh/id_rsa.pub +chmod 0600 /root/.ssh/* +chmod 700 /root/.ssh +ls -lsh /root/.ssh +ls -ld /root/.ssh +pwd -gradlew :shadowJar +case "$1" in + "dev") + USER=maxime + IP=92.132.18.192 + DIR=server/TBasket/backend + ;; + "production") + USER=palafour + IP=193.49.118.205 + DIR=TBasket/backend + ;; + "") + echo "first argument is missing" >&2 + exit 1 + ;; + *) + echo "branch $1 is unable to perform delivery, authorized branches are 'dev and production' for delivery." >&2 + echo "delivery step skipped" + exit 0 +esac -mkdir -p ~/.ssh -echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa -echo "$SSH_PUBLIC_KEY" > ~/.ssh/id_rsa.pub -sftp "$USER@$IP:tbasket/" <<< $'put build/libs/server-all.jar drone/deploy.sh' \ No newline at end of file +./gradlew :shadowJar + +echo "making delivery onto '$USER@$IP:$DIR'" +scp -o "StrictHostKeyChecking no" "build/libs/server-all.jar" "drone/deploy.sh" "drone/start.sh" "$USER@$IP:$DIR/" +echo "chmod 700 $DIR/*; $DIR/deploy.sh" | ssh -o "StrictHostKeyChecking no" $USER@$IP diff --git a/drone/deploy.sh b/drone/deploy.sh index f1e2b5f..52c3b27 100644 --- a/drone/deploy.sh +++ b/drone/deploy.sh @@ -1,19 +1,32 @@ +#!/usr/bin/env bash +DIR=$(readlink -e "$(dirname "$0")") +PROD_SERVER_JAR_NAME="server-prod.jar" +NEW_SERVER_JAR_NAME="server-all.jar" -SERVER_JAR_NAME="server-all.jar" - -OLD_PID=$(ps -aux | grep "-jar $SERVER_JAR_NAME" | tr -s " " | cut -d " " -f2) - +OLD_PID=$(ps -aux | tr -s " " | grep -E "\bjava -jar $PROD_SERVER_JAR_NAME\b" | cut -d " " -f2) #if $OLD_PID is not empty but isn't a number, something went wrong -if [ "$OLD_PID" ] && ! grep -E -q "^[0-9]+$"; then +if [ "$OLD_PID" ] && ! echo "$OLD_PID" | grep -E -q "^[0-9]+$"; then echo "error, unable to retrieve old server pid: $OLD_PID" >&2 exit 2 fi - if [ "$OLD_PID" ]; then #will cause the old server to gracefully shutdown echo "shutting down old server version ..." - kill SIGQUIT "$OLD_PID" - wait "$OLD_PID" + kill -s SIGQUIT "$OLD_PID" + while ! kill -s 0 "$OLD_PID"; do sleep 1; done #sleep until process ends +fi + +rm "$DIR/$PROD_SERVER_JAR_NAME" +mv "$DIR/$NEW_SERVER_JAR_NAME" "$DIR/$PROD_SERVER_JAR_NAME" + +SCREEN="basket" + +# create screen if not exists +if ! screen -ls | grep -q -E "\b[0-9]+\.$SCREEN\b"; then + screen -S "$SCREEN" -d -m fi + +screen -d -r "$SCREEN" -X stuff $"$DIR/start.sh\n" +echo "server is started into $SCREEN screen." diff --git a/drone/start.sh b/drone/start.sh new file mode 100644 index 0000000..3373e87 --- /dev/null +++ b/drone/start.sh @@ -0,0 +1,3 @@ + +echo "starting server" +java -jar "server-prod.jar"