-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.sh
executable file
·170 lines (145 loc) · 3.83 KB
/
start.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
#!/bin/bash
# filename: start.sh
# Author: lufei
# Date: 20200429 14:57:16
inst_needed() {
echo $su_pass | su - root -c "yum install platform-python-devel \
nginx mariadb-server mariadb-connector-c-devel gcc -y "
$pip install --user Django django-ratelimit-backend \
django-tinymce uWSGI mysqlclient
}
gen_files() {
local ng_tmp=/tmp/nginx.tmp
local my_tmp=/tmp/mysql.tmp
cat > $ng_tmp << eof
# blog.conf
upstream django {
server unix://$socket;
}
server {
listen $port;
server_name $server_name;
charset utf-8;
client_max_body_size 75M;
location /static {
alias $blog_root/home/static;
}
location / {
uwsgi_pass django;
client_body_temp_path $nginx_temp_path;
include $blog_root/blog/uwsgi_params;
}
}
eof
cat > $my_tmp << eof
[client]
database = $db_name
user = $db_user
password = $db_pass
default-character-set = utf8
eof
echo $su_pass | su - root -c "mv $ng_tmp $nginx_cnf_file"
echo $su_pass | su - root -c "mv $my_tmp $mysql_cnf_file"
}
customise_setting() {
echo "set ALLOWED_HOSTS to $server_name"
sed -i "s/ALLOWED_HOSTS\(.*\)/ALLOWED_HOSTS = [u\"$server_name\"]/g" \
$blog_root/blog/settings.py
echo "set DEBUG to False"
sed -i 's/DEBUG\(.*\)/DEBUG = False/g' $blog_root/blog/settings.py
echo "set DATABASES to db_mysql"
sed -i 's/DATABASES\(.*\)/DATABASES = db_mysql/g' \
$blog_root/blog/settings.py
}
start_blog() {
DATE=`date +%Y%m%d`
cd $blog_root
/home/xx01/.local/bin/uwsgi --ini $blog_root/blog.ini --daemonize \
$blog_root/log/uwsgi-${DATE}.log
}
start_server() {
echo $su_pass | su - root -c "systemctl start nginx"
echo $su_pass | su - root -c "systemctl start mariadb"
start_blog
}
restart() {
DATE=`date +%Y%m%d`
killall -9 uwsgi
start_blog
}
set_env() {
echo $su_pass | su - root -c "setsebool -P httpd_read_user_content 1"
echo $su_pass | su - root -c "firewall-cmd --add-service=http --permanent; \
firewall-cmd --reload"
}
backup() {
DATE=`date +%Y%m%d`
mysqldump --defaults-file=$mysql_cnf_file --databases $db_name \
> /tmp/${db_name}-${DATE}.sql
}
send() {
backup
local remail=$1
local msg="backup for $server_name."
echo $msg | mail -s "Today's Backup -- from $server_name" -a \
/tmp/${db_name}-${DATE}.sql $remail
## unfinished ..
}
deploy() {
read -s -p "Enter root password: " su_pass
read -s -p "Enter db password: " db_pass
inst_needed
gen_files
customise_setting
set_env
start_blog
}
help() {
local execution=$0
cat << eof
Usage:
$execution OPTION
OPTIONS:
deploy deploy from minimal OS
help show help
restart restart blog
send <emailaddr> send backup email, e.g. $execution send lufy@lufy.org
...
eof
}
main() {
blog_root=`pwd`
pip=pip3
mysql_cnf_file=/etc/blog.cnf
db_name=blog
db_user=blogadmin
nginx_cnf_file=/etc/nginx/conf.d/blog.conf
socket=$blog_root/socket/blog.sock
port=80
server_name=lufy.org
## nginx cannot access tmp directory in user home
nginx_temp_path=/var/lib/nginx/tmp/
local op=${1--h}
shift
if [ $op == "-h" ]; then
op=help
fi
$op $@
}
if [ $0 == "./start.sh" ]; then
main $@
fi
## issues
# port issue
# nginx cannot access socket file in user home
## mysql operations
# create user
# create database
# grant user
## then
# ./manage.py makemigrations
# ./manage.py migrate
# ln /home/xx01/.local/lib/python3.6/site-packages/django/contrib/admin/static/admin/ home/static/ -s
# ln /home/xx01/.local/lib/python3.6/site-packages/tinymce/static/django_tinymce/ -s home/static/
# ln /home/xx01/.local/lib/python3.6/site-packages/tinymce/static/tinymce/ -s home/static/
# set user username in /etc/nginx/nginx.conf