From 52e8209985b25b3a7942908bc2898c3d656d1b73 Mon Sep 17 00:00:00 2001 From: Ayash Kant Baral <20314867+ayashbaral@users.noreply.github.com> Date: Fri, 15 Jan 2021 15:59:44 +0530 Subject: [PATCH] Feature/93 run as service (#97) --- .../todo/app/templates/deployment.tpl | 14 ++++-- .../packer/scripts/todo-installer.sh | 39 --------------- .../packer/{todo.json => todo/app.json} | 10 ++-- .../packer/todo/scripts/installer.sh | 47 +++++++++++++++++++ .../packer/todo/services/app.service | 10 ++++ .../packer/todo/services/bootstrap.sh | 2 + 6 files changed, 71 insertions(+), 51 deletions(-) delete mode 100644 todo-app/aws/3-tier-app/packer/scripts/todo-installer.sh rename todo-app/aws/3-tier-app/packer/{todo.json => todo/app.json} (82%) create mode 100644 todo-app/aws/3-tier-app/packer/todo/scripts/installer.sh create mode 100644 todo-app/aws/3-tier-app/packer/todo/services/app.service create mode 100644 todo-app/aws/3-tier-app/packer/todo/services/bootstrap.sh diff --git a/todo-app/aws/3-tier-app/lab/services/todo/app/templates/deployment.tpl b/todo-app/aws/3-tier-app/lab/services/todo/app/templates/deployment.tpl index b63209c6..9f065fd4 100644 --- a/todo-app/aws/3-tier-app/lab/services/todo/app/templates/deployment.tpl +++ b/todo-app/aws/3-tier-app/lab/services/todo/app/templates/deployment.tpl @@ -1,9 +1,13 @@ #!/bin/bash -x -export MYSQL_HOST='${MYSQL_HOST}' -export MYSQL_DB_NAME='${MYSQL_DB_NAME}' -export MYSQL_USER='${MYSQL_USER}' -export MYSQL_PASSWORD='${MYSQL_PASSWORD}' +# Export mysql environment variables +systemctl set-environment MYSQL_HOST=${MYSQL_HOST} +systemctl set-environment MYSQL_DB_NAME=${MYSQL_DB_NAME} +systemctl set-environment MYSQL_USER=${MYSQL_USER} +systemctl set-environment MYSQL_PASSWORD=${MYSQL_PASSWORD} + # bootstrap todo app -java -server -Dfile.encoding=utf-8 -XX:+ExitOnOutOfMemoryError -Djava.security.egd=file:/dev/./urandom -Duser.timezone=UTC -jar /opt/app.jar \ No newline at end of file +sudo systemctl daemon-reload +sudo systemctl enable todo.service +sudo systemctl start todo \ No newline at end of file diff --git a/todo-app/aws/3-tier-app/packer/scripts/todo-installer.sh b/todo-app/aws/3-tier-app/packer/scripts/todo-installer.sh deleted file mode 100644 index 90222b7f..00000000 --- a/todo-app/aws/3-tier-app/packer/scripts/todo-installer.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash -set -x - -# Install necessary dependencies -sudo DEBIAN_FRONTEND=noninteractive apt-get -y install software-properties-common -sudo DEBIAN_FRONTEND=noninteractive apt-add-repository universe -sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" update -sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade -sudo apt-get -y -qq install curl git openjdk-11-jdk maven apt-transport-https ca-certificates - -# Setup sudo to allow no-password sudo for "$USER_GROUP" group and adding "$USER" user -sudo groupadd -r $USER_GROUP -sudo useradd -m -s /bin/bash $USER -sudo usermod -a -G $USER_GROUP $USER -sudo cp /etc/sudoers /etc/sudoers.orig -echo "$USER ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/$USER - -# Installing SSH key -sudo mkdir -p /home/$USER/.ssh -sudo chmod 700 /home/$USER/.ssh -sudo cp /tmp/todo.pub /home/$USER/.ssh/authorized_keys -sudo chmod 600 /home/$USER/.ssh/authorized_keys -sudo chown -R $USER /home/$USER/.ssh -sudo usermod --shell /bin/bash $USER - -# Create JAVA_HOME for $USER & download the todo-app from github - -sudo -H -i -u $USER -- env bash << EOF -whoami -echo ~$USER -cd /home/$USER -export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/ -export PATH=$PATH:$JAVA_HOME/bin -git clone https://github.com/Raghav2211/psi-lab.git -cd psi-lab/todo-app -mvn clean package -sudo cp target/psi-todo-${APP_VERSION}.jar /opt/app.jar -EOF - diff --git a/todo-app/aws/3-tier-app/packer/todo.json b/todo-app/aws/3-tier-app/packer/todo/app.json similarity index 82% rename from todo-app/aws/3-tier-app/packer/todo.json rename to todo-app/aws/3-tier-app/packer/todo/app.json index 6c9ebb09..4ee349ed 100644 --- a/todo-app/aws/3-tier-app/packer/todo.json +++ b/todo-app/aws/3-tier-app/packer/todo/app.json @@ -3,8 +3,6 @@ "aws_access_key": "{{env `AWS_ACCESS_KEY`}}", "aws_secret_key": "{{env `AWS_SECRET_KEY`}}", "aws_region": "{{env `AWS_REGION`}}", - "ssh_user" : "{{env `SSH_USER`}}", - "ssh_user_group" : "{{env `SSH_USER_GROUP`}}", "app_version" : "{{env `APP_VERSION`}}" }, "builders": [ @@ -39,15 +37,13 @@ "provisioners": [ { "type": "file", - "source": "todo.pub", - "destination": "/tmp/todo.pub" + "sources": "todo.pub,services/bootstrap.sh,services/app.service", + "destination": "/tmp/" }, { "type": "shell", - "script": "scripts/todo-installer.sh", + "script": "scripts/installer.sh", "environment_vars": [ - "USER_GROUP={{user `ssh_user_group`}}", - "USER={{user `ssh_user`}}", "APP_VERSION={{user `app_version`}}" ] } diff --git a/todo-app/aws/3-tier-app/packer/todo/scripts/installer.sh b/todo-app/aws/3-tier-app/packer/todo/scripts/installer.sh new file mode 100644 index 00000000..ba1f8b0c --- /dev/null +++ b/todo-app/aws/3-tier-app/packer/todo/scripts/installer.sh @@ -0,0 +1,47 @@ +#!/bin/bash +set -x + +# Install necessary dependencies +sudo DEBIAN_FRONTEND=noninteractive apt-get -y install software-properties-common +sudo DEBIAN_FRONTEND=noninteractive apt-add-repository universe +sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" update +sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade +sudo apt-get -y -qq install curl git openjdk-11-jdk maven apt-transport-https ca-certificates + +# Setup sudo to allow no-password sudo for "psi" group and adding "todo" user +sudo groupadd -r psi +sudo useradd -m -s /bin/bash todo +sudo usermod -a -G psi todo +sudo cp /etc/sudoers /etc/sudoers.orig +echo "todo ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/todo + +# Installing SSH key +sudo mkdir -p /home/todo/.ssh +sudo chmod 700 /home/todo/.ssh +sudo cp /tmp/todo.pub /home/todo/.ssh/authorized_keys +sudo chmod 600 /home/todo/.ssh/authorized_keys +sudo chown -R todo /home/todo/.ssh +sudo usermod --shell /bin/bash todo + + + +# Create JAVA_HOME for todo & download the todo-app from github + +sudo -H -i -u todo -- env bash << EOF +whoami +echo ~todo +cd /home/todo +export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64/ +export PATH=$PATH:$JAVA_HOME/bin +git clone https://github.com/Raghav2211/psi-lab.git +cd psi-lab/todo-app +mvn clean package -DskipTests +sudo mkdir /opt/todo +sudo cp target/psi-todo-${APP_VERSION}.jar /opt/todo/app.jar +sudo cp /tmp/bootstrap.sh /opt/todo/bootstrap.sh +sudo chmod 744 /opt/todo/bootstrap.sh +sudo cp /tmp/app.service /etc/systemd/system/todo.service +cd ../.. +rm -r psi-lab +EOF + diff --git a/todo-app/aws/3-tier-app/packer/todo/services/app.service b/todo-app/aws/3-tier-app/packer/todo/services/app.service new file mode 100644 index 00000000..50c7b370 --- /dev/null +++ b/todo-app/aws/3-tier-app/packer/todo/services/app.service @@ -0,0 +1,10 @@ +[Unit] +Description=Todo Application as a Service +[Service] +User=todo +#path to the executable bash script which executes todo app +ExecStart=/bin/bash /opt/todo/bootstrap.sh +SuccessExitStatus=143 +TimeoutStopSec=10 +Restart=on-failure +RestartSec=5 diff --git a/todo-app/aws/3-tier-app/packer/todo/services/bootstrap.sh b/todo-app/aws/3-tier-app/packer/todo/services/bootstrap.sh new file mode 100644 index 00000000..e125a842 --- /dev/null +++ b/todo-app/aws/3-tier-app/packer/todo/services/bootstrap.sh @@ -0,0 +1,2 @@ +#!/bin/sh +java -server -Dfile.encoding=utf-8 -XX:+ExitOnOutOfMemoryError -Djava.security.egd=file:/dev/./urandom -Duser.timezone=UTC -jar /opt/todo/app.jar \ No newline at end of file