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