-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
Β·163 lines (132 loc) Β· 4.8 KB
/
deploy.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
#!/bin/sh
# Deployment script
# Execute as normal user, it will ask for permissions as needed
# Configure the following variables
DIR=`dirname $0`
DIR=`realpath $DIR`
# Set to "yes" to setup for development server
# Any other value sets up for release
DEBUG="yes"
# Virtual environment folder
VENV_FOLDER=".venv"
VENV="$DIR/.venv"
PIP="$VENV/bin/pip"
PYTHON="$VENV/bin/python"
VIRTUALENV_EXE="~/.local/bin/virtualenv"
SYSTEM_PYTHON3_EXE='python3.6'
# 100% secure
# CAREFUL: will destroy previous db and user with this name
# Don't change if you dont know what you are doing
DB_NAME="asoures"
DB_PASS="asoures"
# default backend superuser
SU_USER="asoures"
SU_EMAIL="asoures@asoures.gr"
SU_PASS="asoures"
# Skip steps if already done, set to "yes"
SKIP_PACKAGE_INSTALLATION="no"
SKIP_NPM_INSTALL="no"
SKIP_NPM_BUILD="yes"
SKIP_PGRES_CREATE_DB_AND_USER="no"
SKIP_PGRES_DISABLE_FORCED_SSL="no"
SKIP_SETUP_VENV="no"
SKIP_UPDATE_HOSTS="no"
SKIP_CREATE_SUPERUSER="no"
if [ "x$SKIP_PACKAGE_INSTALLATION" != "xyes" ] ; then
echo "Installing packages..."
sudo apt-get update -y
sudo apt-get install postgresql postgresql-contrib postgis python3 python3-pip apache2 apache2-dev libapache2-mod-wsgi nodejs npm -y
fi
if [ "x$SKIP_NPM_INSTALL" != "xyes" ]; then
echo "Installing node_modules"
cd "$DIR/project/client" && npm install
fi
if [ "x$SKIP_NPM_BUILD" != "xyes" ]; then
echo "Building front-end"
cd "$DIR/project/client" && npm run build
fi
if [ "x$SKIP_PGRES_CREATE_DB_AND_USER" != "xyes" ]; then
echo "Creating database"
sudo -u postgres dropdb "$DB_NAME" --if-exists
sudo -u postgres dropuser "$DB_NAME" --if-exists
sudo userdel "$DB_NAME"
# create postgres role
sudo -u postgres psql -c "CREATE USER $DB_NAME WITH PASSWORD '$DB_PASS' SUPERUSER;"
sudo -u postgres createdb "$DB_NAME"
# create normal user
sudo useradd -M -s /usr/sbin/nologin "$DB_NAME"
echo "$DB_PASS" >> "$DIR/.tmp"
echo "$DB_PASS" >> "$DIR/.tmp"
sudo passwd "$DB_NAME" < "$DIR/.tmp"
sudo -- sh -c "echo '[User]\nSystemAccount=true' > '/var/lib/AccountsService/users/$DB_NAME'"
rm "$DIR/.tmp"
fi
if [ "x$SKIP_PGRES_DISABLE_FORCED_SSL" != "xyes" ]; then
echo "Disabling postgresql forced SSL option"
postgresql_conf="/etc/postgresql/10/main/postgresql.conf"
if [ -f "$postgresql_conf" ]; then
echo "Config file: $postgresql_conf"
sudo cp "$postgresql_conf" "${postgresql_conf}.bak"
cat "$postgresql_conf" | sed "s,ssl\s*=\s*on,ssl = off,g" > "$DIR/.conf"
sudo cp "$DIR/.conf" $postgresql_conf
rm "$DIR/.conf"
echo "Updated successfully"
else
echo "WARNING: postgresql.conf not found, manually edit and set option 'ssl = off'"
fi
fi
[ -e "$DIR/asoures.egg-info" ] && echo "Removing old asoures.egg-info" && rm -rf "$DIR/asoures.egg-info"
if [ "x$SKIP_SETUP_VENV" != "xyes" ]; then
echo "Installing virtualenv"
pip3 install --user virtualenv > /dev/null
echo "Setting up python virtual environment in $VENV"
[ -e "$VENV" ] && rm -rf "$VENV" # trust me
mkdir -p "$VENV"
if [ ! -e $VIRTUALENV_EXE ]; then
VIRTUALENV_EXE=`which virtualenv`
fi
echo "Using virtualenv executable: '$VIRTUALENV_EXE'"
"$VIRTUALENV_EXE" --python="$SYSTEM_PYTHON3_EXE" "$VENV" > /dev/null
echo "Installing django and other dependencies"
cd "$DIR" && "$PIP" install -e .[dev] > /dev/null
fi
echo -n "Updating environment file "
echo "DATABASE_URL=postgis://$DB_NAME:$DB_PASS@localhost/$DB_NAME" > "$DIR/.env"
if [ "x$DEBUG" != "xyes" ]; then
echo "(release)"
echo "DEBUG=false" >> "$DIR/.env"
echo "SECRET_KEY=asdfhqweughkjhbkjgiuqwerhfbwergtqwiureyt" >> "$DIR/.env"
echo "ALLOWED_HOSTS=*" >> "$DIR/.env"
else
echo "(debug)"
echo "DEBUG=true" >> "$DIR/.env"
fi
echo "Running migrations"
cd "$DIR" && $PYTHON manage.py migrate > /dev/null
if [ "x$SKIP_UPDATE_HOSTS" != "xyes" ]; then
echo "Updating hosts"
x=`grep "^\s*127.0.0.1\s\s*asoures\.gr\s*$" /etc/hosts`
if [ "x$x" = "x" ]; then
sudo -- sh -c "echo '\n\n127.0.0.1 asoures.gr' >> /etc/hosts"
else
echo "no changes needed in /etc/hosts"
fi
fi
if [ "x$SKIP_CREATE_SUPERUSER" != "xyes" ]; then
# based on https://stackoverflow.com/questions/6244382/how-to-automate-createsuperuser-on-django
echo "Creating superuser $SU_USER"
$PYTHON "$DIR/manage.py" addroot "$SU_USER" "$SU_EMAIL" "$SU_PASS"
fi
echo "
-----------------
Done. Next Steps:
-----------------
Populate database:
$ python manage.py populatedb 100 # add 100 shops and products to db
Development servers:
$ cd project/client && npm run start (ReactJS)
$ python manage.py runserver 8000 (Django)
Start HTTP or HTTPS server for Django and ReactJS build:
$ python manage.py runhttps 8443
$ python manage.py runhttp 8000
"