-
Notifications
You must be signed in to change notification settings - Fork 1
/
install_django.sh
executable file
·300 lines (243 loc) · 7.12 KB
/
install_django.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env bash
SCRIPT_NAME=$0
CANONICAL_URL="http://dl.dropbox.com/u/53973/install_django.sh"
usage() {
cat <<EOF
Usage: $SCRIPT_NAME OPTIONS
Application:
-n NAME set the NAME of the application - letters, numbers and underscore
-d DISTRIBUTION the URL to the tgz distribution of the application
Database:
-H DATABASE_HOST the database host
-D DATABASE_NAME the database name to use. Defaults to \${NAME}
-U DATABASE_USER the database user to use. Default to \${NAME}
-P DATABASE_PASSWORD the database password
Web:
-w WEBSERVER which webserver to use. apache only at the moment
-u SITE_NAME the SiteName to use in Apache. Defaults to \${NAME}.com
-s STATIC_URL the url for static assets. Defaults to /static/
Local:
-u LOCAL_USER name of the unix user to create. Defaults to \${NAME}
-p PROJECT_ROOT name of the project root. Defaults to /home/\${LOCAL_USER}/\${NAME}
EOF
}
die() {
message=$1
error_code=$2
echo "$SCRIPT_NAME: $message" 1>&2
usage
exit $error_code
}
get_options() {
while getopts "hn:d:H:D:U:P:w:u:s:u:p:" opt; do
case "$opt" in
h)
usage
exit 0
;;
n)
export NAME="$OPTARG"
;;
d)
export DISTRIBUTION="$OPTARG"
;;
H)
export DATABASE_HOST="$OPTARG"
;;
D)
export DATABASE_NAME="$OPTARG"
;;
U)
export DATABASE_USER="$OPTARG"
;;
P)
export DATABASE_PASSWORD="$OPTARG"
;;
w)
export WEBSERVER="$OPTARG"
;;
u)
export SITE_NAME="$OPTARG"
;;
s)
export STATIC_URL="$OPTARG"
;;
u)
export LOCAL_USER="$OPTARG"="$OPTARG"
;;
p)
export PROJECT_ROOT="$OPTARG"
;;
[?])
die "unknown option $opt" 10
;;
esac
done
}
handle_defaults() {
if [ -z "$NAME" ]; then
die "NAME is required" 1
fi
if [ -z "$DISTRIBUTION" ]; then
die "DISTRIBUTION is required" 2
fi
if [ -z "$DATABASE_NAME" ]; then
export DATABASE_NAME="$NAME"
fi
if [ -z "$DATABASE_HOST" ]; then
export DATABASE_HOST="localhost"
fi
if [ -z "$DATABASE_USER" ]; then
export DATABASE_USER="$NAME"
fi
if [ -z "$DATABASE_PASSWORD" ]; then
export DATABASE_PASSWORD=`head -c 100 /dev/urandom | md5sum | awk '{print $1}'`
fi
if [ -z "$WEBSERVER" ]; then
export WEBSERVER="apache"
fi
if [ -z "$SITE_NAME" ]; then
export SITE_NAME="${NAME}.com"
fi
if [ -z "$STATIC_URL" ]; then
export STATIC_URL="/static/"
fi
if [ -z "$ADMIN_EMAIL" ]; then
export ADMIN_EMAIL="alerts@${SITE_NAME}"
fi
if [ -z "$LOCAL_USER" ]; then
export LOCAL_USER="$NAME"
fi
if [ -z "$PROJECT_ROOT" ]; then
export PROJECT_ROOT="/home/$LOCAL_USER/$NAME"
fi
}
export DEBIAN_FRONTEND=noninteractive
update_system() {
aptitude update
aptitude -y safe-upgrade
}
install_baseline() {
apt-get install -y build-essential git emacs23-nox curl
}
install_python() {
apt-get install -y python python-dev python-pip python-setuptools python-mysqldb
}
install_nginx() {
apt-get install -y nginx
apt-get install -y python-flup
}
install_apache() {
apt-get install -y apache2 libapache2-mod-wsgi
pip install virtualenv
}
configure_apache() {
cat <<EOF | sudo tee /etc/apache2/sites-available/$NAME
<VirtualHost *:80>
ServerName $SITE_NAME
ServerAdmin $ADMIN_EMAIL
LogLevel warn
ErrorLog \${APACHE_LOG_DIR}/${SITE_NAME}_error.log
CustomLog \${APACHE_LOG_DIR}/${SITE_NAME}_access.log combined
WSGIDaemonProcess $NAME user=www-data group=www-data maximum-requests=10000 python-path=/home/$LOCAL_USER/env/lib/python2.6/site-packages
WSGIProcessGroup $NAME
WSGIScriptAlias / $PROJECT_ROOT/deploy/pinax.wsgi
<Directory $PROJECT_ROOT/deploy>
Order deny,allow
Allow from all
</Directory>
Alias $STATIC_URL $PROJECT_ROOT/media/
<Directory $PROJECT_ROOT/media>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
EOF
}
activate_apache() {
sudo a2dissite default
sudo a2ensite $NAME
sudo /etc/init.d/apache2 reload
}
install_webserver() {
if [[ $WEBSERVER == "nginx" ]]; then
install_nginx
die "cannot yet configure nginx"
else
if [[ $WEBSERVER == "apache" ]]; then
install_apache
configure_apache
else
die "unknown webserver $WEBSERVER"
fi
fi
}
activate_webserver() {
if [[ $WEBSERVER == "nginx" ]]; then
die "cannot yet activate nginx"
else
if [[ $WEBSERVER == "apache" ]]; then
activate_apache
else
die "unknown webserver $WEBSERVER"
fi
fi
}
bootstrap_project() {
adduser --system --disabled-password --disabled-login $LOCAL_USER
sudo -u $LOCAL_USER virtualenv /home/$LOCAL_USER/env
sudo -u $LOCAL_USER mkdir $PROJECT_ROOT
}
install_project() {
sudo -u $LOCAL_USER wget $DISTRIBUTION -O /home/$LOCAL_USER/app.tgz
sudo -u $LOCAL_USER tar zxvf /home/$LOCAL_USER/app.tgz -C $PROJECT_ROOT
sudo -u $LOCAL_USER pip -E /home/$LOCAL_USER/env install -r $PROJECT_ROOT/requirements/project.txt
}
configure_local_settings() {
cat <<EOF | sudo -u $LOCAL_USER tee $PROJECT_ROOT/local_settings.py
BASE_URL="http://$SITE_NAME"
DEBUG = False
TEMPLATE_DEBUG = False
SERVE_MEDIA = False
DATABASES = {
"default": {
"ENGINE": "mysql",
"NAME": "$DATABASE_NAME",
"USER": "$DATABASE_USER",
"PASSWORD": "$DATABASE_PASSWORD",
"HOST": "$DATABASE_HOST",
}
}
STATIC_URL = "$STATIC_URL"
STATIC_ROOT = "$PROJECT_ROOT/media"
TEMPLATE_DIRS = ["$PROJECT_ROOT/templates"]
EOF
}
django_syncdb() {
sudo -u $LOCAL_USER /home/$LOCAL_USER/env/bin/python $PROJECT_ROOT/manage.py syncdb --noinput
}
print_launch_conf() {
cat<<EOF
** To automate launching an instance like this one, put the following in your userdata script **
wget $CANONICAL_URL
sudo bash $SCRIPT_NAME -n $NAME -d $DISTRIBUTION -H $DATABASE_HOST -D $DATABASE_NAME -U $DATABASE_USER -P $DATABASE_PASSWORD -w $WEBSERVER -u $SITE_NAME -s $STATIC_URL -u $LOCAL_USER -p $PROJECT_ROOT"
EOF
}
print_django_info() {
PUBLIC_DNS=`curl http://169.254.169.254/latest/meta-data/public-hostname 2>/dev/null`
cat <<EOF
** To view the app you just installed, point your browser to **
http://${PUBLIC_DNS}/
EOF
}
get_options $*
handle_defaults
update_system
install_baseline
install_python
install_webserver
bootstrap_project
install_project
configure_local_settings
activate_webserver
print_django_info