-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·147 lines (132 loc) · 4.19 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
#!/bin/bash
# Clarify what OS is it
if [[ -e /etc/redhat-release ]]; then
release="centos"
elif [[ -e /etc/debian_version ]]; then
release="debian"
else
echo; echo "Error: unknown OS!"; echo
exit 1
fi
# Check arguments number
if [ ${#} -gt 3 ]; then
echo; echo "Too many arguments!"; echo
exit 1
elif [ ${#} -lt 1 ]; then
echo; echo "Please specify webserver (nginx or apache)"; echo
exit 1
fi
webserver="${1}"
homedir="/usr/gm/htpass"
# Return error in case of invalid webserver argument
if [ ${webserver} != 'nginx' -a ${webserver} != 'apache' ]; then
echo; echo "Unknown webserver! Please specify either nginx or apache"; echo
exit 1
# Specify correct conf file for furter substitution
elif [ ${webserver} == 'apache' ]; then
if [ ${release} == 'centos' ]; then
webserver="httpd"
else
webserver="apache2"
fi
conf="${homedir}/apache/htpass.conf"
else
conf="${homedir}/nginx/htpass.conf"
fi
# Check if chosen webserver is installed
if ! [ -n "$(which ${webserver} 2>/dev/null|grep -E "(/\w+)+/${webserver}")" ]; then
echo; echo "Please install ${webserver} and try again"; echo
exit 1
fi
cd ${homedir}
# Add user htpass, so Apache can launch our virtualhost
if id -u htpass > /dev/null; then
echo; echo "Htpass user exists"; echo
else
if useradd htpass -m -b /home -s /bin/bash -U; then
echo; echo "Htpass user created"; echo
else
echo; echo "Failed to create Htpass user"; echo
exit 1
fi
fi
# Provide valid configuration file
rm -f /etc/${webserver}/sites-{available,enabled}/htpass.conf
rm -rf ${homedir}/env/
if cp ${conf} /etc/${webserver}/sites-available/; then
# Enable configuration file
if ln -s /etc/${webserver}/sites-available/htpass.conf /etc/${webserver}/sites-enabled/; then
echo; echo "Configuration files placed"; echo
else
echo; echo "Failed to place configuration files"; echo
exit 1
fi
fi
# Install virtualenv
if pip install virtualenv; then
# Create vitrualenv for our app
if virtualenv ${homedir}/env; then
# Install dependencies under virtualenv
if source ${homedir}/env/bin/activate; then
if pip install Flask uwsgi && deactivate; then
echo; echo "Virtual environment set"; echo
else
echo; echo "Failed to install pip dependencies"; echo
exit 1
fi
else
echo; echo "Failed to activate virtualenv"; echo
exit 1
fi
else
echo; echo "Failed to create virtualenv for Htpass"; echo
exit 1
fi
else
echo; echo "Failed to install virtualenv"; echo
exit 1
fi
# Provide init script in case of nginx
if [ ${webserver} == 'nginx' ]; then
#stop htpass-init
#rm -f /etc/init/htpass-init.conf
/etc/init.d/htpass-init stop &> /dev/null
rm -f /etc/init.d/htpass-init
#if cp ${homedir}/nginx/htpass-init.conf /etc/init/htpass-init.conf; then
if cp ${homedir}/htpass-init /etc/init.d/; then
chmod +x /etc/init.d/htpass-init
if [ -n "$(grep "nginx" /etc/passwd)" ]; then
nginx_group="nginx"
elif [ -n "$(grep "www-data" /etc/passwd)" ]; then
nginx_group="www-data"
else
echo
echo "No Nginx group found, are you sure you have Nginx installed?"
exit 1
fi
if chown -R htpass:${nginx_group} ${homedir}/; then
if /etc/init.d/htpass-init start; then
echo; echo "Upstart script for uWSGI set and launched"; echo
else
echo; echo "Failed to set upstart script for uWSGI"; echo
exit 1
fi
else
echo; echo "Failed to give correct permissions on ${homedir} directory"; echo
exit 1
fi
else
echo; echo "Failed to copy init script to /etc/init/"; echo
exit 1
fi
nginx -t &&\
service nginx reload &&\
echo && echo "Nginx reloaded, ready to serve" ||\
echo "Failed to reload Nginx"
echo
else
apachectl configtest &&\
service ${webserver} reload &&\
echo && echo "Apache reloaded, ready to serve" ||\
echo "Failed to reload Apache"
fi