скрипт для быстрой развертки django приложения. (еще сырой)

  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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/bin/bash
VERSION="django_app v0.1 (alpha)"
die(){
echo -e "\e[01;31m*\e[m $@" >&2
exit 1
}
ok() {
[[ ${VERBOSE} ]] && echo -e "\e[01;32m*\e[m $@"
}
usage() {
echo -e "Usage: $0 [OPTIONS] COMMAND PROJECT
\e[01;35mOPTIONS:\e[m
\e[01;36m-c\e[m CONFIG custom CONFIG file path
\e[01;36m-t\e[m TEMPLATE custom TEMPLATE file path
\e[01;36m-i\e[m IP custom IP addres
\e[01;36m-z\e[m ZONE custom ZONE, defaults is hostname
\e[01;36m-d\e[m DJANGO custom django packages path, defaults from python
\e[01;36m-p\e[m PROJECTDIR custom PROJECTDIR
\e[01;36m-T\e[m attach trac to http://\$PROJECT.\$ZONE/trac URL
\e[01;36m-v\e[m bee verbose
\e[01;36m-V\e[m print version
\e[01;35mCOMMAND:\e[m
\e[01;33mapache\e[m make apache vhost config and reload" >&2
exit 1
}
msg() {
echo -e "\e[01;33m*\e[m $@"
}
get_pub_ip(){
IP=$(host $(hostname) | awk '{print $4}' | { read ip; echo ${ip}; })
echo ${IP}
}
get_var() {
echo $(eval echo \${$1})
}
msg_opt() {
[[ ${VERBOSE} ]] && msg "Option $@ set to '$(get_var $@)'"
}
escape() {
CHARS='. /'
RES=$@
for c in ${CHARS}; do
E="s/\\${c}/\\\\\\${c}/g"
#echo $E
RES=$(echo "${RES}" | sed ${E})
done
echo ${RES}
}
validate_dir() {
if [[ -d $@ ]]; then
return 0
else
die "Directory \e[01;37m$@\e[m does not exists"
return 1
fi
}
validate_write() {
if [[ -w $@ ]]; then
return 0
else
die "Write to \e[01;37m$@\e[m not permitted"
return 1
fi
}
init_default() {
VERBOSE=1
TRAC='#'
IP=$(get_pub_ip)
ZONE=$(hostname)
DJANGO=$(echo 'import django; print "%s" % django.__path__[0]' | python)
DJANGO_WSGI_TEMPLATE="django.wsgi.tpl"
TRAC_WSGI_TEMPLATE="trac.wsgi.tpl"
VHOSTS="/etc/apache2/vhosts.d"
APACHE_TEMPLATE="apache.conf.tpl"
APACHE_USER="apache"
APACHE_GROUP="apache"
APACHE_RELOAD="/etc/init.d/apache2 reload"
TRACDIR="/var/lib/trac"
}
init_opts() {
errors=0
opts=""
while getopts 'vVTi:c:z:p:a:d:t:D:' OPTION $*; do
case ${OPTION} in
T) TRAC=''; opts="${opts} TRAC";;
i) IP=${OPTARG}; opts="${opts} IP";;
c) CONFIG=${OPTARG}; opts="${opts} CONFIG";;
D) DJANGO=${OPTARG}; opts="${opts} DJANGO";;
z) ZONE=${OPTARG};opts="${opts} ZONE";;
a) APACHE_TEMPLATE=${OPTARG};opts="${opts} APACHE_TEMPLATE";;
p) PROJECTDIR=${OPTARG};opts="${opts} PROJECTDIR";;
v) VERBOSE=0; opts="${opts} VERBOSE";;
V) echo ${VERSION}; exit 0;;
?) errors=$((${errors} + 1 )); die "Invalid option -${OPTARG}";;
esac
done
for o in ${opts}; do
msg_opt ${o}
done
[[ ${errors} -gt 0 ]] && return 1 || return 0
}
load_conf() {
if [[ -z ${CONFIG} ]]; then
CONFIG="django_app.conf"
else
[[ -f ${CONFIG} ]] || die "Config ${CONFIG} not found"
fi
[[ -f ${CONFIG} ]] && source ${CONFIG}
}
load_vars(){
[[ -z ${PROJECTSDIR} ]] && PROJECTSDIR="${HOME}/dev/django"
PROJECTDIR="${PROJECTSDIR}/${PROJECT}"
TRACENV="${TRACDIR}/${PROJECT}"
TRACEGG="${WWWDIR}/${PROJECT}/egg_cache"
}
process_tpl() {
#msg $#
#msg $@
[[ $# > 1 ]] || die "Bad parameters for template processing"
local TEMPLATE=$1
local TMP="${TEMPLATE}.tmp"
shift
#msg $*
local PARAMS=$*
cp "${TEMPLATE}" "${TMP}" || die "Failed backup template"
for PARAM in ${PARAMS}; do
local VAR=$(get_var ${PARAM})
local NEWVAR=$(escape ${VAR})
#msg "${PARAM} = ${VAR} -> ${NEWVAR}"
local E="s/%${PARAM}%/${NEWVAR}/g"
#echo "${VAR} -> ${NEWVAR} : ${E}"
#[[ ! -z ${NEWVAR} ]] &&
sed -i ${E} ${TMP}
done
}
tpl() {
[[ -s $1 ]] || die "$1 template doesn't exists or empty"
process_tpl $@ || die "Failed process template $1"
ok "Template $1 proccessed successfully"
}
apache_validate() {
validate_dir ${PROJECTDIR} && \
validate_dir ${DJANGO} && \
validate_dir ${WWWDIR} && \
validate_write ${WWWDIR} && \
validate_write ${VHOSTS} && \
return 0
}
#echo $#
main() {
[[ $# == 0 ]] && usage
#echo "$#"
init_default
load_conf
init_opts $* || die "Failed load options"
shift $((${OPTIND} - 1))
#set
#echo "$#"
if [[ $# != 2 ]]; then
usage
else
COMMAND=$1
PROJECT=$2
if [[ ${VERBOSE} ]]; then
msg "COMMAND set to ${COMMAND}"
msg "PROJECT set to ${PROJECT}"
fi
fi
load_vars
case ${COMMAND} in
apache) apache;;
wsgi) wsgi;;
*) die "Invalid command ${COMMAND}"; usage;;
esac
ok "[OK]"
}
apache() {
tpl ${APACHE_TEMPLATE} ZONE EMAIL WWWDIR PROJECT PROJECTDIR DJANGO TRAC
apache_validate || die "Apache validation failed"
ok "Apache params are valid"
local proj="${WWWDIR}/${PROJECT}"
if [[ ! -d "${proj}" ]]; then
mkdir "${proj}" || die "Failed create project dir in ${WWWDIR}"
fi
local dirs="htdocs cgi-bin logs egg_cache"
for dir in ${dirs}; do
if [[ ! -d "${proj}/${dir}" ]]; then
mkdir "${proj}/${dir}" || die "Failed create ${dir} in projecti dir ${proj}"
fi
done
ok "Project dirs has created"
chown -R ${APACHE_USER}.${APACHE_GROUP} "${proj}" || die "Failed change owner"
ok "Project dir owner has changed to ${APACHE_USER}.${APACHE_GROUP}"
cp "${APACHE_TEMPLATE}.tmp" "${VHOSTS}/${PROJECT}.conf" || die "Failed copy temp config to vhosts dir"
ok "Apache vhost config saved"
rm "${APACHE_TEMPLATE}.tmp"
${APACHE_RELOAD} || die "Failed reload Apache"
ok "Apache successfully reloaded"
}
wsgi(){
wsgi_django
[[ -z ${TRAC} ]] && wsgi_trac
}
wsgi_django() {
tpl ${DJANGO_WSGI_TEMPLATE} PROJECT DJANGO PROJECTDIR
local handler="${WWWDIR}/${PROJECT}/htdocs/index.wsgi"
cp "${DJANGO_WSGI_TEMPLATE}.tmp" "${handler}" || die "Failed copy Django WSGI handler"
chown ${APACHE_USER}.${APACHE_GROUP} "${handler}" || die "Failed change owner for Django WSGI handler"
ok "Django WSGI Handler owner has changed to ${APACHE_USER}.${APACHE_GROUP}"
chmod +x "${handler}" || die "Failed make executable Django WSGI handler"
rm "${DJANGO_WSGI_TEMPLATE}.tmp"
${APACHE_RELOAD} || die "Failed reload Apache"
ok "Django WSGI handler saved"
}
wsgi_trac(){
tpl ${TRAC_WSGI_TEMPLATE} TRACENV TRACEGG
local handler="${WWWDIR}/${PROJECT}/htdocs/trac.wsgi"
cp "${TRAC_WSGI_TEMPLATE}.tmp" "${handler}" || die "Failed copy Trac WSGI handler"
chown ${APACHE_USER}.${APACHE_GROUP} "${handler}" || die "Failed change owner for Trac WSGI handler"
ok "Trac WSGI handler owner has changed to ${APACHE_USER}.${APACHE_GROUP}"
chmod +x "${handler}" || die "Failed make executable Trac WSGI handler"
rm "${TRAC_WSGI_TEMPLATE}.tmp"
${APACHE_RELOAD} || die "Failed reload Apache"
ok "Trac WSGI handler saved"
}
main $*