forked from BorderlessNomad/DataPlay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.sh
156 lines (117 loc) · 4.06 KB
/
frontend.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
145
146
147
148
149
150
151
152
153
154
155
156
#!/bin/bash
# This is setup script for Frontend instance.
set -ex
if [ "$(id -u)" != "0" ]; then
echo >&2 "Error: This script must be run as user 'root'";
exit 1
fi
DEST="/home/ubuntu/www"
APP="dataplay"
WWW="www-src"
APP_HOST=$(ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}')
APP_PORT="80"
APP_TYPE="gamification"
LOADBALANCER_HOST=$(ss-get --timeout 360 loadbalancer.hostname)
LOADBALANCER_REQUEST_PORT="80"
LOADBALANCER_API_PORT="1937"
# DOMAIN="dataplay.playgen.com"
DOMAIN="${LOADBALANCER_HOST}:${LOADBALANCER_REQUEST_PORT}"
timestamp () {
date +"%F %T,%3N"
}
setuphost () {
HOSTNAME=$(hostname)
HOSTLOCAL="127.0.1.1"
echo "$HOSTLOCAL $HOSTNAME" >> /etc/hosts
}
export_variables () {
echo "export DP_LOADBALANCER_HOST=$LOADBALANCER_HOST" >> /etc/profile.d/dataplay.sh
echo "export DP_LOADBALANCER_REQUEST_PORT=$LOADBALANCER_REQUEST_PORT" >> /etc/profile.d/dataplay.sh
echo "export DP_LOADBALANCER_API_PORT=$LOADBALANCER_API_PORT" >> /etc/profile.d/dataplay.sh
echo "export DP_DOMAIN=$DOMAIN" >> /etc/profile.d/dataplay.sh
. /etc/profile
su - ubuntu -c ". /etc/profile"
}
install_nginx () {
URL="https://raw.githubusercontent.com"
USER="playgenhub"
REPO="DataPlay"
BRANCH="master"
SOURCE="$URL/$USER/$REPO/$BRANCH"
mkdir -p $DEST/$APP/$WWW/dist
apt-add-repository -y ppa:nginx/stable
apt-get --allow-unauthenticated update
apt-get install -y --allow-unauthenticated nginx-full
unixts="$(date +'%Y%m%d%H%M%S')"
keyword="<filesystem>"
destination="$DEST/$APP/$WWW/dist"
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default.$unixts
wget --retry-connrefused --waitretry=1 --read-timeout=20 --timeout=15 -t 0 -N $SOURCE/tools/deployment/app/nginx.default -O /etc/nginx/sites-available/default
sed -i 's,'"$keyword"','"$destination"',g' /etc/nginx/sites-available/default
chown ubuntu:www-data $DEST/$APP/$WWW
/etc/init.d/nginx reload
}
download_app () {
URL="https://codeload.github.com"
USER="playgenhub"
REPO="DataPlay"
BRANCH="master"
SOURCE="$URL/$USER/$REPO"
cd $DEST
echo "Fetching latest ZIP"
wget --retry-connrefused --waitretry=1 --read-timeout=20 --timeout=15 -t 0 -N $SOURCE/zip/$BRANCH -O $BRANCH.zip
echo "Extracting from $BRANCH.zip"
unzip -oq $BRANCH.zip
if [ -d $APP ]; then
rm -r $APP
fi
mkdir -p $APP
echo "Moving files from $REPO-$BRANCH/ to $APP"
mv -f $REPO-$BRANCH/* $APP
cd $APP
}
init_frontend () {
sed -i "s/localhost:3000/$DOMAIN/g" $DEST/$APP/$WWW/dist/scripts/*.js
}
configure_frontend () {
sed -i "s/localhost:3000/$DOMAIN/g" $DEST/$APP/$WWW/app/scripts/app.coffee
command -v grunt >/dev/null 2>&1 || { echo >&2 "Error: Command 'grunt' not found!"; exit 1; }
command -v coffee >/dev/null 2>&1 || { echo >&2 "Error: 'coffee' is not installed!"; exit 1; }
command -v bower >/dev/null 2>&1 || { echo >&2 "Error: 'bower' is not installed!"; exit 1; }
}
build_frontend () {
npm install -d
bower install
grunt build
}
inform_loadbalancer () {
retries=0
until curl -H "Content-Type: application/json" -X POST -d "{\"ip\":\"$APP_HOST:$APP_PORT\"}" http://$LOADBALANCER_HOST:$LOADBALANCER_API_PORT/$APP_TYPE; do
echo "[$(timestamp)] Load Balancer is not up yet, retry... [$(( retries++ ))]"
sleep 5
done
}
setup_service_script () {
DEPLOYMENT="tools/deployment"
SERVICE="frontend.service.sh"
cp $DEST/$APP/$DEPLOYMENT/app/$SERVICE $DEST/$SERVICE
chmod +x $DEST/$SERVICE
}
echo "[$(timestamp)] ---- 1. Setup Host ----"
setuphost
echo "[$(timestamp)] ---- 2. Export Variables ----"
export_variables
echo "[$(timestamp)] ---- 3. Install Nginx ----"
install_nginx
echo "[$(timestamp)] ---- 4. Download Application ----"
download_app
# We either Init frontend which is quicker and doesn't install any extra libraries
# or do configure and build which is very time consuming process due to lots of node.js libraries
echo "[$(timestamp)] ---- 5. Init Frotnend ----"
init_frontend
echo "[$(timestamp)] ---- 6. Inform Load Balancer (Add) ----"
inform_loadbalancer
echo "[$(timestamp)] ---- 7. Setup Service Script ----"
setup_service_script
echo "[$(timestamp)] ---- Completed ----"
exit 0