Skip to content

Commit 94c35de

Browse files
committed
Parametrized paths and removed unnecessary functions.
1 parent 298809a commit 94c35de

File tree

1 file changed

+29
-41
lines changed

1 file changed

+29
-41
lines changed

127 - lib-django.sh

+29-41
Original file line numberDiff line numberDiff line change
@@ -6,61 +6,49 @@
66
#
77
# My ref: http://www.linode.com/?r=aadfce9845055011e00f0c6c9a5c01158c452deb
88

9-
10-
function django_get_user_or_default {
11-
# django_get_user_or_default([user])
12-
if [ -z "$1" ];
13-
then USER="www-data"
14-
else USER="$1"
15-
fi
16-
echo "$USER"
17-
}
18-
19-
function django_get_project_path {
20-
# django_get_project_path(project_name)
21-
PROJECT_NAME="$1"
22-
echo "/srv/$PROJECT_NAME"
23-
}
9+
PROJECT_CODE_DIR=app
10+
DJANGO_PROJECT=webapp
2411

2512
function django_change_project_owner {
2613
# django_change_project_owner(project_path, user)
2714
PROJECT_PATH="$1"
2815
USER="$2"
29-
GROUP="$USER"
30-
chown -R "$USER:$GROUP" "$PROJECT_PATH"
16+
chown -R "$USER:$USER" "$PROJECT_PATH"
3117
}
3218

3319
function django_create_project {
34-
# django_create_project(project_name)
20+
# django_create_project(project_path)
3521

36-
PROJECT_NAME="$1"
37-
if [ -z "$PROJECT_NAME" ]; then
38-
echo "django_create_project() requires the project name as the first argument"
22+
PROJECT_PATH="$1"
23+
if [ -z "$PROJECT_PATH" ]; then
24+
echo "django_create_project() requires the project root path as the first argument"
3925
return 1;
4026
fi
41-
PROJECT_PATH=`django_get_project_path "$PROJECT_NAME"`
4227

43-
mkdir -p "$PROJECT_PATH/app/conf/apache"
28+
mkdir -p "$PROJECT_PATH/$PROJECT_CODE_DIR/conf/apache"
4429
mkdir -p "$PROJECT_PATH/logs" "$PROJECT_PATH/run/eggs"
4530

4631
virtualenv "$PROJECT_PATH/venv"
47-
$PROJECT_PATH/venv/bin/pip install django
32+
$PROJECT_PATH/venv/bin/pip install Django
4833

49-
pushd "$PROJECT_PATH/app"
50-
"$PROJECT_PATH/venv/bin/python" "$PROJECT_PATH/venv/bin/django-admin.py" startproject webapp .
34+
pushd "$PROJECT_PATH/$PROJECT_CODE_DIR"
35+
"$PROJECT_PATH/venv/bin/python" "$PROJECT_PATH/venv/bin/django-admin.py" startproject "$DJANGO_PROJECT" .
5136
popd
52-
mkdir -p "$PROJECT_PATH/app/webapp/static"
37+
mkdir -p "$PROJECT_PATH/$PROJECT_CODE_DIR/$DJANGO_PROJECT/static"
38+
39+
echo "Django" >> "$PROJECT_PATH/$PROJECT_CODE_DIR/requirements.txt"
5340
}
5441

5542
function django_install_db_driver {
5643
# django_install_db_driver(project_path, driver_package)
57-
$1/venv/bin/pip install django "$2"
44+
$1/venv/bin/pip install "$2"
45+
echo "$2" >> "$PROJECT_PATH/$PROJECT_CODE_DIR/requirements.txt"
5846
}
5947

6048
function django_configure_db_settings {
6149
# django_configure_db_settings(project_path, engine, name, user, password, [host, [port]])
62-
63-
SETTINGS="$1/app/webapp/settings.py"
50+
PROJECT_PATH="$1"
51+
SETTINGS="$PROJECT_PATH/$PROJECT_CODE_DIR/$DJANGO_PROJECT/settings.py"
6452
sed -i -e "s/'ENGINE': 'django.db.backends.'/'ENGINE': 'django.db.backends.$2'/" "$SETTINGS"
6553
sed -i -e "s/'NAME': ''/'NAME': '$3'/" "$SETTINGS"
6654
sed -i -e "s/'USER': ''/'USER': '$4'/" "$SETTINGS"
@@ -79,7 +67,7 @@ function django_configure_apache_virtualhost {
7967
VHOST_HOSTNAME="$1"
8068
PROJECT_PATH="$2"
8169
USER="$3"
82-
GROUP="$USER"
70+
GROUP="$USER"
8371

8472
if [ -z "$VHOST_HOSTNAME" ]; then
8573
echo "django_configure_apache_virtualhost() requires the hostname as the first argument"
@@ -92,43 +80,43 @@ function django_configure_apache_virtualhost {
9280
fi
9381

9482
APACHE_CONF="200-$VHOST_HOSTNAME"
95-
APACHE_CONF_PATH="$PROJECT_PATH/app/conf/apache/$APACHE_CONF"
83+
APACHE_CONF_PATH="$PROJECT_PATH/$PROJECT_CODE_DIR/conf/apache/$APACHE_CONF"
9684

9785
cat > "$APACHE_CONF_PATH" << EOF
9886
<VirtualHost *:80>
9987
ServerAdmin root@$VHOST_HOSTNAME
10088
ServerName $VHOST_HOSTNAME
10189
ServerSignature Off
10290
103-
Alias /static/ $PROJECT_PATH/app/webapp/static/
104-
Alias /robots.txt $PROJECT_PATH/app/webapp/static/robots.txt
105-
Alias /favicon.ico $PROJECT_PATH/app/webapp/static/favicon.ico
91+
Alias /static/ $PROJECT_PATH/$PROJECT_CODE_DIR/$DJANGO_PROJECT/static/
92+
Alias /robots.txt $PROJECT_PATH/$PROJECT_CODE_DIR/$DJANGO_PROJECT/static/robots.txt
93+
Alias /favicon.ico $PROJECT_PATH/$PROJECT_CODE_DIR/$DJANGO_PROJECT/static/favicon.ico
10694
10795
SetEnvIf User_Agent "monit/*" dontlog
10896
CustomLog "|/usr/sbin/rotatelogs $PROJECT_PATH/logs/access.log.%Y%m%d-%H%M 5M" combined env=!dontlog
10997
ErrorLog "|/usr/sbin/rotatelogs $PROJECT_PATH/logs/error.log.%Y%m%d-%H%M 5M"
11098
LogLevel warn
11199
112-
WSGIScriptAlias / $PROJECT_PATH/app/webapp/wsgi.py
100+
WSGIScriptAlias / $PROJECT_PATH/$PROJECT_CODE_DIR/$DJANGO_PROJECT/wsgi.py
113101
114-
WSGIDaemonProcess $VHOST_HOSTNAME user=$USER group=$GROUP processes=2 threads=10 maximum-requests=10000 display-name=%{GROUP} python-path=$PROJECT_PATH/app:$PROJECT_PATH/venv/lib/python2.7/site-packages python-eggs=$PROJECT_PATH/run/eggs
102+
WSGIDaemonProcess $VHOST_HOSTNAME user=$USER group=$GROUP processes=2 threads=10 maximum-requests=10000 display-name=%{GROUP} python-path=$PROJECT_PATH/$PROJECT_CODE_DIR:$PROJECT_PATH/venv/lib/python2.7/site-packages python-eggs=$PROJECT_PATH/run/eggs
115103
WSGIProcessGroup $VHOST_HOSTNAME
116-
WSGIScriptAlias / $PROJECT_PATH/app/webapp/wsgi.py
104+
WSGIScriptAlias / $PROJECT_PATH/$PROJECT_CODE_DIR/$DJANGO_PROJECT/wsgi.py
117105
118-
<Directory $PROJECT_PATH/app/webapp/static>
106+
<Directory $PROJECT_PATH/$PROJECT_CODE_DIR/$DJANGO_PROJECT/static>
119107
Order deny,allow
120108
Allow from all
121109
Options -Indexes FollowSymLinks
122110
</Directory>
123111
124-
<Directory $PROJECT_PATH/app/conf/apache>
112+
<Directory $PROJECT_PATH/$PROJECT_CODE_DIR/conf/apache>
125113
Order deny,allow
126114
Allow from all
127115
</Directory>
128116
129117
</VirtualHost>
130118
EOF
131119

132-
ln -t /etc/apache2/sites-available/ "$APACHE_CONF_PATH"
120+
ln -s -t /etc/apache2/sites-available/ "$APACHE_CONF_PATH"
133121
a2ensite "$APACHE_CONF"
134122
}

0 commit comments

Comments
 (0)