|
| 1 | +#!/usr/bin/env bash |
| 2 | +# create environment variables |
| 3 | +createEnv(){ |
| 4 | + sudo echo " |
| 5 | + export SITE=davidessien.com |
| 6 | + export GIT_REPO=https://github.com/andela/selene-ah-frontend.git |
| 7 | + export SITES_AVAILABLE=/etc/nginx/sites-available |
| 8 | + export SITES_ENABLED=/etc/nginx/sites-enabled |
| 9 | + export SITES_ENABLED_CONFIG=/etc/nginx/sites-enabled/selene |
| 10 | + export SITES_AVAILABLE_CONFIG=/etc/nginx/sites-available/selene |
| 11 | + export REPOSITORY_FOLDER=selene-ah-frontend |
| 12 | + |
| 13 | + export GREEN='\033[0;32m' |
| 14 | + export RED='\033[0;31m' |
| 15 | + " > .env |
| 16 | + |
| 17 | + # add enviroment variables to OS |
| 18 | + source .env |
| 19 | +} |
| 20 | + |
| 21 | +# Ouput messages to terminal |
| 22 | +output(){ |
| 23 | + echo -e "$2 ################################ $1 ################################## $(tput sgr0)" |
| 24 | +} |
| 25 | + |
| 26 | +# Install node.js |
| 27 | +installNode(){ |
| 28 | + output "installing node.js" $GREEN |
| 29 | + sudo apt-get update |
| 30 | + curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh |
| 31 | + sudo bash nodesource_setup.sh |
| 32 | + sudo apt-get install -y nodejs |
| 33 | + output "Node.js installed successfully" $GREEN |
| 34 | +} |
| 35 | + |
| 36 | +# Clone the repository |
| 37 | +cloneRepository(){ |
| 38 | + output "Checking if repository exists..." $GREEN |
| 39 | + if [ ! -d selene-ah-frontend ] |
| 40 | + then |
| 41 | + output "Cloning repository..." $GREEN |
| 42 | + git clone -b aws-deploy https://github.com/andela/selene-ah-frontend.git |
| 43 | + else |
| 44 | + output "Repository already exists..." $RED |
| 45 | + output "Removing repository..." $GREEN |
| 46 | + sudo rm -r selene-ah-frontend |
| 47 | + output "Cloning repository..." $GREEN |
| 48 | + git clone -b aws-deploy https://github.com/andela/selene-ah-frontend.git |
| 49 | + fi |
| 50 | + output "Repository cloned successfully" $GREEN |
| 51 | +} |
| 52 | + |
| 53 | +# Setup the project |
| 54 | +setupProject(){ |
| 55 | + output "installing node modules" $GREEN |
| 56 | + cd selene-ah-frontend |
| 57 | + sudo npm install -y |
| 58 | + sudo npm audit fix --force |
| 59 | + sudo npm run build |
| 60 | + output "successfully installed node modules" $GREEN |
| 61 | +} |
| 62 | + |
| 63 | +# Setup nginx |
| 64 | +setupNginx(){ |
| 65 | + output "installing nginx" $GREEN |
| 66 | + # Install nginx |
| 67 | + sudo apt-get install nginx -y |
| 68 | + |
| 69 | + output "setting up reverse proxy" $GREEN |
| 70 | + # Setup reverse proxy with nginx |
| 71 | + nginxScript="server { |
| 72 | + listen 80; |
| 73 | + server_name $SITE "www.$SITE"; |
| 74 | +
|
| 75 | + location / { |
| 76 | + proxy_pass http://127.0.0.1:8080; |
| 77 | + } |
| 78 | + }" |
| 79 | + |
| 80 | + # Remove the default nginx proxy script |
| 81 | + if [ -f $SITES_AVAILABLE/default ]; then |
| 82 | + sudo rm $SITES_AVAILABLE/default |
| 83 | + fi |
| 84 | + |
| 85 | + if [ -f $SITES_ENABLED/default ]; then |
| 86 | + sudo rm $SITES_ENABLED/default |
| 87 | + fi |
| 88 | + |
| 89 | + # Create an nginx reverse proxy script |
| 90 | + sudo echo ${nginxScript} >> ${SITES_AVAILABLE_CONFIG} |
| 91 | + |
| 92 | + # Create a symlink for the sites enabled and the sites available script |
| 93 | + sudo ln -s $SITES_AVAILABLE_CONFIG $SITES_ENABLED_CONFIG |
| 94 | + |
| 95 | + sudo service nginx restart |
| 96 | + |
| 97 | + output "successfully setup nginx" $GREEN |
| 98 | +} |
| 99 | + |
| 100 | +setupSSL(){ |
| 101 | + output "installing and setting up SSL" $GREEN |
| 102 | + site=davidessien.com |
| 103 | + |
| 104 | + sudo apt-get update -y |
| 105 | + |
| 106 | + # Get and install the SSL certificate |
| 107 | + sudo apt-get install software-properties-common -y |
| 108 | + sudo add-apt-repository ppa:certbot/certbot -y |
| 109 | + sudo apt-get update -y |
| 110 | + sudo apt-get install python-certbot-nginx -y |
| 111 | + |
| 112 | + # Configure the ngix proxy file to use the SSL certificate |
| 113 | + sudo certbot --nginx -n --agree-tos --email $email --redirect --expand -d $site -d "www.$site" |
| 114 | + |
| 115 | + output "successfuly setup SSL" $GREEN |
| 116 | +} |
| 117 | + |
| 118 | +# Create a service to run the app in the backgroud using systemctl |
| 119 | +createAppService(){ |
| 120 | + output "Creating a service for the app..." $GREEN |
| 121 | + sudo bash -c "cat > /etc/systemd/system/selene.service <<EOF |
| 122 | + [Unit] |
| 123 | + Description=Selene ah Service - Service to start the selene-ah frontend app |
| 124 | + After=network.target |
| 125 | +
|
| 126 | + [Service] |
| 127 | + ExecStart=/usr/bin/node /home/ubuntu/selene-ah-frontend/server.js |
| 128 | + Restart=on-failure |
| 129 | + Type=simple |
| 130 | + User=ubuntu |
| 131 | +
|
| 132 | + [Install] |
| 133 | + WantedBy=multi-user.target" |
| 134 | +} |
| 135 | + |
| 136 | +# start the app using the service that has been created |
| 137 | +startAppService(){ |
| 138 | + output "Starting the app service..." $GREEN |
| 139 | + sudo systemctl daemon-reload |
| 140 | + sudo systemctl start selene.service |
| 141 | + sudo systemctl enable selene.service |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +# Function to deploy the project |
| 146 | +main(){ |
| 147 | + createEnv |
| 148 | + setupNginx |
| 149 | + installNode |
| 150 | + cloneRepository |
| 151 | + setupProject |
| 152 | + setupSSL |
| 153 | + createAppService |
| 154 | + startAppService |
| 155 | + output "Project deployed" $GREEN |
| 156 | +} |
| 157 | + |
| 158 | +main |
0 commit comments