Skip to content

Commit

Permalink
Feature/93 run as service (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayashbaral authored Jan 15, 2021
1 parent fbd6ea2 commit 52e8209
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -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
sudo systemctl daemon-reload
sudo systemctl enable todo.service
sudo systemctl start todo
39 changes: 0 additions & 39 deletions todo-app/aws/3-tier-app/packer/scripts/todo-installer.sh

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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`}}"
]
}
Expand Down
47 changes: 47 additions & 0 deletions todo-app/aws/3-tier-app/packer/todo/scripts/installer.sh
Original file line number Diff line number Diff line change
@@ -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

10 changes: 10 additions & 0 deletions todo-app/aws/3-tier-app/packer/todo/services/app.service
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions todo-app/aws/3-tier-app/packer/todo/services/bootstrap.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 52e8209

Please sign in to comment.