-
Notifications
You must be signed in to change notification settings - Fork 0
/
installServer.sh
322 lines (262 loc) · 8.65 KB
/
installServer.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#! /bin/bash
#####################################################################
# COMMAND LINE INPUT
#####################################################################
if [ -z "$1" ]; then
echo "It was not specified server ip"
exit 1
fi
if [ -z "$2" ]; then
echo "It was not specified database name"
exit 1
fi
if [ -z "$3" ]; then
echo "It was not specified postgres user name"
exit 1
fi
if [ -z "$4" ]; then
echo "It was not specified postgres user password"
exit 1
fi
if [ -z "$5" ]; then
echo "It was not specified linux user name"
exit 1
fi
if [ -z "$6" ]; then
echo "It was not specified django secret key"
exit 1
fi
SERVER_IP=$1
DATABASE_NAME=$2
POSTGRES_USER=$3
POSTGRES_PASS=$4
LINUX_USER_NAME=$5
DJANGO_SECRET_KEY=$6
# name of github repository
REPOSITORY_NAME="osirisWebPlatform"
GITHUB_URL="https://github.com/felipinbombin/osirisWebPlatform.git"
#####################################################################
# SETUP
#####################################################################
SCRIPT="$(readlink "$0")"
INSTALLER_FOLDER="$(dirname "$SCRIPT")"
if [ ! -d "$INSTALLER_FOLDER" ]; then
echo "failed to retrieve the installer folder path"
exit 1
fi
unset SCRIPT
# move to installation folder
cd "$INSTALLER_FOLDER"
#####################################################################
# USER CONFIGURATION
#####################################################################
# stores the current path
if id "$LINUX_USER_NAME" >/dev/null 2>&1; then
echo "User $LINUX_USER_NAME already exists.. skipping"
else
echo "User $LINUX_USER_NAME does not exists.. CREATING!"
adduser "$LINUX_USER_NAME"
fi
PROJECT_PATH=/home/"$LINUX_USER_NAME"
PROJECT_DIR="$PROJECT_PATH"/"$REPOSITORY_NAME"
# virtual environment
VIRTUAL_ENV_NAME="myenv"
VIRTUAL_ENV_DIR="$PROJECT_DIR"/"$VIRTUAL_ENV_NAME"
#####################################################################
# CONFIGURATION
#####################################################################
clone_project=false
install_packages=false
postgresql_configuration=false
project_configuration=false
apache_configuration=false
#####################################################################
# CLONE PROJECT
#####################################################################
if $clone_project; then
apt-get install --yes git
cd "$PROJECT_PATH"
# clone project from git
echo ""
echo "----"
echo "Clone project from gitHub"
echo "----"
echo ""
DO_CLONE=true
if [ -d "$REPOSITORY_NAME" ]; then
echo ""
echo "$REPOSITORY_NAME repository already exists."
read -p "Do you want to remove it and clone it again? [Y/n]: " -n 1 -r
echo # (optional) move to a new line
DO_CLONE=false
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Removing repository '$REPOSITORY_NAME' at: $(pwd)"
rm -rf "$REPOSITORY_NAME"
DO_CLONE=true
fi
fi
if "$DO_CLONE" ; then
git clone "$GITHUB_URL"
chown -R "$LINUX_USER_NAME":"$LINUX_USER_NAME" "$PROJECT_PATH"
fi
# move to installation folder
cd "$INSTALLER_FOLDER"
fi
#####################################################################
# REQUIREMENTS
#####################################################################
if $install_packages; then
apt-get update
apt-get upgrade
# install dependencies
# install postgres
apt-get --yes install postgresql postgresql-contrib
# install apache
apt-get install --yes apache2 libapache2-mod-wsgi-py3
# install python and pip
apt-get --yes install python-pip python-dev libpq-dev
# upgrade pip
pip install -U pip
# install npm
apt-get --yes install nodejs
apt-get --yes install npm
ln -s /usr/bin/nodejs /usr/bin/node
# install bower
npm install -g bower
bower install --allow-root
pip install virtualenv
cd "$PROJECT_DIR"
# create virtual env
sudo -u "$LINUX_USER_NAME" virtualenv "$VIRTUAL_ENV_NAME"
# activate virtualenv
source "$VIRTUAL_ENV_DIR"/bin/activate
# install requirements
pip install -r requirements.txt
# move to installation folder
cd "$INSTALLER_FOLDER"
fi
#####################################################################
# POSTGRESQL
#####################################################################
if $postgresql_configuration; then
echo ----
echo ----
echo "Postgresql"
echo ----
echo ----
CREATE_DATABASE=true
DATABASE_EXISTS=$(sudo -Hiu postgres psql -lqt | cut -d \| -f 1 | grep -w "$DATABASE_NAME")
if [ "$DATABASE_EXISTS" ]; then
echo ""
echo "The database $DATABASE_NAME already exists."
read -p "Do you want to remove it and create it again? [Y/n]: " -n 1 -r
echo # (optional) move to a new line
CREATE_DATABASE=false
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Removing database $DATABASE_NAME..."
sudo -Hiu postgres psql -c "DROP DATABASE $DATABASE_NAME;"
CREATE_DATABASE=true
fi
fi
if "$CREATE_DATABASE" ; then
# change config of psql
cd "$INSTALLER_FOLDER"
# create user and database
POSTGRES_TEMPLATE_FILE=./template_postgresqlConfig.sql
POSTGRES_FINAL_FILE=./postgresqlConfig.sql
# copy the template
cp "$POSTGRES_TEMPLATE_FILE" "$POSTGRES_FINAL_FILE"
# change parameters
sed -i -e 's/<DATABASE>/'"$DATABASE_NAME"'/g' "$POSTGRES_FINAL_FILE"
sed -i -e 's/<USER>/'"$POSTGRES_USER"'/g' "$POSTGRES_FINAL_FILE"
sed -i -e 's/<PASSWORD>/'"$POSTGRES_PASS"'/g' "$POSTGRES_FINAL_FILE"
# postgres user has to be owner of the file and folder that contain the file
CURRENT_OWNER=$(stat -c '%U' .)
# change owner to let postgres user exec file
chown postgres:postgres "$INSTALLER_FOLDER"/postgresqlConfig.sql
chown postgres:postgres "$INSTALLER_FOLDER"
sudo -u postgres psql -f "$POSTGRES_FINAL_FILE"
rm "$POSTGRES_FINAL_FILE"
chown "$CURRENT_OWNER":"$CURRENT_OWNER" "$INSTALLER_FOLDER"
fi
echo ----
echo ----
echo "Postgresql ready"
echo ----
echo ----
fi
#####################################################################
# SETUP DJANGO APP
#####################################################################
if $project_configuration; then
echo ----
echo ----
echo "Project configuration"
echo ----
echo ----
# configure wsgi
cd "$INSTALLER_FOLDER"
python wsgiConfig.py "$PROJECT_DIR" "$REPOSITORY_NAME"
SETTING_PATH="$PROJECT_DIR"/"$REPOSITORY_NAME"
KEYS_PATH="$SETTING_PATH"/keys
# set secret_key variable
SECRET_KEY_FILE="$KEYS_PATH"/secret_key.py
# change parameter
echo "SECRET_KEY=\"""$DJANGO_SECRET_KEY""\"" > "$SECRET_KEY_FILE"
DATABASE_CONFIG_TEMPLATE=./template_database.py
DATABASE_CONFIG_FILE="$KEYS_PATH"/database.py
cp "$DATABASE_CONFIG_TEMPLATE" "$DATABASE_CONFIG_FILE"
# change parameter
sed -i -e 's/<DATABASE>/'"$DATABASE_NAME"'/g' "$DATABASE_CONFIG_FILE"
sed -i -e 's/<USER>/'"$POSTGRES_USER"'/g' "$DATABASE_CONFIG_FILE"
sed -i -e 's/<PASSWORD>/'"$POSTGRES_PASS"'/g' "$DATABASE_CONFIG_FILE"
# create folder used by loggers if not exist
LOG_DIR="$PROJECT_DIR"/"$REPOSITORY_NAME"/logs
sudo -u "$LINUX_USER_NAME" mkdir -p "$LOG_DIR"
touch "$LOG_DIR"/file.log
chmod 777 "$LOG_DIR"/file.log
# add ip to allowed_hosts list
sed -i -e 's/ALLOWED_HOSTS = \[\]/ALLOWED_HOSTS = [u'"'$SERVER_IP'"']/g' "$SETTING_PATH"/settings.py
# update database models and static files
source "$VIRTUAL_ENV_DIR"/bin/activate
python "$PROJECT_DIR"/manage.py migrate
python "$PROJECT_DIR"/manage.py collectstatic
python "$PROJECT_DIR"/manage.py loaddata models possibleQueue
# create super user
echo "creating super user to manage web system ..."
python "$PROJECT_DIR"/manage.py createsuperuser
# run tests
coverage run --source='.' manage.py test
echo ----
echo ----
echo "Project configuration ready"
echo ----
echo ----
fi
#####################################################################
# APACHE CONFIGURATION
#####################################################################
if $apache_configuration; then
echo ----
echo ----
echo "Apache configuration"
echo ----
echo ----
# configure apache 2.4
cd "$INSTALLER_FOLDER"
configApache="osiris_web_platform.conf"
python configApache.py "$PROJECT_PATH" "$REPOSITORY_NAME" "$VIRTUAL_ENV_NAME" "$configApache"
a2dissite 000-default.conf
a2ensite "$configApache"
sudo service apache2 restart
echo ----
echo ----
echo "Apache configuration ready"
echo ----
echo ----
fi
cd "$INSTALLER_FOLDER"
echo "Installation ready."
echo "To check that its all ok, enter to $SERVER_IP/admin"