-
Notifications
You must be signed in to change notification settings - Fork 0
/
fireitup.sh
96 lines (81 loc) · 3.18 KB
/
fireitup.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
#!/bin/sh
echo "Prior to executing the following script, please ensure that:"
echo "1. You have set back-end API url properly at:"
echo " - front-end: \$REACT_ENV_FILE: 'REACT_APP_API_URL' variable"
echo "2. PSQL db pass set inside \$DJANGO_CUSTOM_SETTINGS file is same with actual"
echo " db password, \$DB_PASS"
while true; do
read -p "Do you wish to proceed with this operation? [y/n]" yn
case $yn in
[Yy] ) break;;
* ) exit;;
esac
done
DIR=`git rev-parse --show-toplevel`
VENV_FOLDER="$DIR/.venv"
PIP_REQUIREMENTS="$DIR/deployment/requirements.txt"
DB_NAME="eloncharge"
DB_PASS="eloncharge"
DJANGO_CUSTOM_SETTINGS="$DIR/back-end/backend/settings_test.py"
DJANGO_SETTINGS_FILE="$DIR/back-end/backend/settings.py"
REACT_ENV_FILE="$DIR/front-end/.env"
SSL_CONFIGURATION="$DIR/deployment/ssl"
NGINX_SERVER_BLOCKS="$DIR/deployment/nginx/eloncharge"
### BUILD
echo "BUILD PHASE"
echo "##########################"
echo "Installing required packages.."
sudo apt -qq update -y
sudo apt -qq install postgresql python3 python3-pip python3-venv npm gunicorn nginx -y
echo "Setting up virtual environment.."
python3 -m venv $VENV_FOLDER
source $VENV_FOLDER/bin/activate
pip3 install -r $PIP_REQUIREMENTS > /dev/null
echo "Provision PSQL db.."
echo "- Drop previous instances of user/db, if existing"
sudo -u postgres dropdb "$DB_NAME" --if-exists
sudo -u postgres dropuser "$DB_NAME" --if-exists
echo "- Create $DB_NAME user and corresponding db"
sudo -u postgres psql -c "CREATE USER $DB_NAME WITH ENCRYPTED PASSWORD '$DB_PASS' SUPERUSER;"
sudo -u postgres createdb "$DB_NAME"
echo "- Grant all prigileges for db $DB_NAME to $DB_NAME"
sudo -u postgres psql -c "grant all privileges on database $DB_NAME to $DB_NAME;"
echo "Setting up backend.."
echo "- Import Django settings"
cp $DJANGO_CUSTOM_SETTINGS $DJANGO_SETTINGS_FILE
echo "- Run migrations"
cd $DIR/back-end
python3 manage.py migrate > /dev/null
echo "- Populate db with demo data. Check below for dummy user credentials."
python3 manage.py populatedb-demo
echo "Setting up frontend.."
cd $DIR/front-end
npm install --silent
echo "Setting up cli-client.."
cd $DIR/cli-client
python3 setup.py build > /dev/null
python3 setup.py install > /dev/null
echo "Setting up NGINX reverse proxy.."
echo "- Setting up self-signed TLS"
sudo cp -r $SSL_CONFIGURATION/* /etc/ssl
echo "- Set up NGINX server blocks"
sudo cp $NGINX_SERVER_BLOCKS /etc/nginx/sites-available/eloncharge
sudo ln -sn /etc/nginx/sites-available/eloncharge /etc/nginx/sites-enabled/eloncharge 2>/dev/null
sudo rm /etc/nginx/sites-enabled/default 2>/dev/null
echo "- Consume configuration"
sudo systemctl restart nginx
if ! grep -q "127.0.0.1 eloncharge.gr" /etc/hosts; then
echo "- One-off cheat to list webpage under eloncharge.gr"
sudo /bin/sh -c 'echo "127.0.0.1 eloncharge.gr" >> /etc/hosts'
fi
### DEPLOY
echo "DEPLOY PHASE"
echo "##########################"
echo "Fire up React server"
cd $DIR/front-end
nohup npm start &
echo "Fire up Django application through Gunicorn"
cd $DIR/back-end
nohup gunicorn --access-logfile - --workers 10 --bind 127.0.0.1:8000 backend.wsgi &
echo "Deployment was successful! You can now browse the application at" \
"https://eloncharge.com, enjoy!"