-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·141 lines (115 loc) · 3.29 KB
/
deploy.sh
File metadata and controls
executable file
·141 lines (115 loc) · 3.29 KB
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
#!/bin/bash
# Check if user is root
if (($EUID != 0)); then
echo "Please run this script as root."
exit
fi
# Check positional parameters
if (($# != 3)); then
echo "Usage: ./deploy.sh [SITE_NAME] [DOMAIN] [USER]"
exit
fi
# Check if user exists
if ! id "$3" &>/dev/null; then
echo "User $3 not found!"
exit
fi
# Setting up variables
site_name=$1
domain=$2
user_name=$3
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
# Get wsgi.py file location
wsgi_dir=$( dirname $( find $current_dir -name wsgi.py | tail -1 ) )
# If wsgi.py was not found
if [ -z "$wsgi_dir" ]; then
echo "Could not find wsgi.py"
exit
fi
# Otherwise get its relative path and switch "/" chars to "."
wsgi=$( echo ${wsgi_dir#"$current_dir/"} | tr "/" "." )
# Define file paths
sock_path="$current_dir/$site_name.sock"
venv_path="$current_dir/venv"
# Check python version
python_version=$(python3.8 --version 2>&1 | grep -Po '(?<=Python )(.+)')
if [[ "$python_version" < "3.7.0" ]]; then
python_version=$(python3.7 --version 2>&1 | grep -Po '(?<=Python )(.+)')
if [[ "$python_version" < "3.7.0" ]]; then
echo "Missing python version at least 3.7 Check the command 'python3 --version'"
exit
else
python="python3.7"
fi
else
python="python3.8"
fi
# Check pip version
pip_version=$(pip3 --version 2>&1 | tail -1 | grep -Po '(?<=pip )(.+)' | cut -f1 -d" ")
if [[ "$pip_version" < "19.0.0" ]]; then
echo "Missing pip version at least 19.0.0 Check the command 'pip3 --version'"
exit
else
pip="pip3"
fi
# Enter user mode
sudo -i -u $user_name /bin/sh << EOF
# Create virtual environment and install requirements
$python -m venv $venv_path
source $venv_path/bin/activate
$pip install -r $current_dir/requirements.txt
$pip install gunicorn
# Collect static data and migrate the database (just in case)
$python $current_dir/manage.py collectstatic
yes
$python $current_dir/manage.py migrate
# Exit user mode
EOF
# Generate gunicorn socket file
cat > /etc/systemd/system/$site_name.gunicorn.socket <<EOF
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=$sock_path
[Install]
WantedBy=sockets.target
EOF
# Generate gunicorn service file
cat > /etc/systemd/system/$site_name.gunicorn.service <<EOF
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=$user_name
Group=www-data
WorkingDirectory=$current_dir
ExecStart=$venv_path/bin/gunicorn --access-logfile - --workers 3 --bind unix:$sock_path $wsgi.wsgi:application
[Install]
WantedBy=multi-user.target
EOF
# Start and enable gunicorn socket and service
systemctl daemon-reload
systemctl start $site_name.gunicorn.socket
systemctl enable $site_name.gunicorn.socket
systemctl start $site_name.gunicorn.service
systemctl enable $site_name.gunicorn.socket
# Generate nginx config file
cat > /etc/nginx/sites-available/$site_name <<EOF
server {
listen 80;
listen [::]:80;
server_name $domain;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root $current_dir;
}
location / {
include proxy_params;
proxy_pass http://unix:$sock_path;
}
}
EOF
# Link nginx config file and reload it
ln -s /etc/nginx/sites-available/$site_name /etc/nginx/sites-enabled/
systemctl reload nginx
echo "Successfully deployed $site_name!"