Return to Snippet

Revision: 5919
at April 1, 2009 14:25 by mandric


Updated Code
#!/bin/bash
# Create a django skeleton.
# Dir structure looks like:
#~example/
#  bin
#  sites
#    example.com
#        |-- lib
#        |   `-- example
#        |       |-- media
#        |       `-- templates
#        |-- logs
#        |-- public
#        |   |-- admin_media -> /path/to/django/contrib/admin/media
#        |   `-- media -> /home/example/sites/example.com/lib/example/media
#        |-- static
#        |-- trac
#        |-- uploads


if [ -z $1 ] ; then
echo 'Please specify a username.'
exit
fi

if [ -z $2 ] ; then
echo 'Please specify a domain name.'
exit
fi

USER=$1
PROJECT=$USER
SITE=$2
USER_PASS=`makepasswd --chars=15`
DB_PASS=`makepasswd --chars=25`
SECRET_KEY=`makepasswd --chars=128`
HOME_DIR=/home/$USER
LIB_DIR=$HOME_DIR/sites/$SITE/projects

bootstrap_user () {
echo "rand user pass: $USER_PASS"
echo "Creating $USER"
# add unix user
adduser $USER --force-badname

mkdir $HOME_DIR/sites/$SITE -p
mkdir $LIB_DIR
mkdir $HOME_DIR/sites/$SITE/logs
mkdir $HOME_DIR/sites/$SITE/public
mkdir $HOME_DIR/sites/$SITE/static
mkdir $HOME_DIR/sites/$SITE/uploads
mkdir $HOME_DIR/bin
}



create_database () {
    # Create database
    NAME=$PROJECT
    USER=$USER
    echo "$USER db pass: $DB_PASS"
    echo "enter mysql root pass"
    mysql -u root -p  << EOF
create database $NAME default character set = 'utf8' default collate = 'utf8_general_ci';
GRANT ALL ON ${NAME}.* TO '${NAME}'@'localhost' IDENTIFIED BY '${DB_PASS}';
flush privileges;
EOF
}

# Setup Trac
setup_trac() {
mkdir -p $HOME_DIR/sites/$SITE/trac/env 
mkdir $HOME_DIR/sites/$SITE/trac/repos
trac-admin $HOME_DIR/sites/$SITE/trac/env initenv
svnadmin create $HOME_DIR/sites/$SITE/trac/repos/

cat >>$HOME_DIR/sites/$SITE/public/trac.fcgi <<EOF
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2004 Edgewall Software
# Copyright (C) 2003-2004 Jonas Borgström <[email protected]>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Jonas Borgström <[email protected]>

# for andric.us
import os

os.environ['TRAC_ENV'] = '${HOME_DIR}/sites/${SITE}/trac/env'

try:
    from trac.web import fcgi_frontend
    fcgi_frontend.run()
except SystemExit:
    raise
except Exception, e:
    print 'Content-Type: text/plain

',
    print 'Oops...'
    print
    print 'Trac detected an internal error:'
    print
    print e
    print
    import traceback
    import StringIO
    tb = StringIO.StringIO()
    traceback.print_exc(file=tb)
    print tb.getvalue()
EOF
}


create_wsgi () {
# Copy .htaccess and dispatch.wsgi into public dir
# This also depends on the proper configuration in apache.
cat >>$HOME_DIR/sites/$SITE/public/dispatch.wsgi <<EOF
#!/usr/bin/python2.5
import sys, os

# make stdout go to stderr for wsgi apps
sys.stdout = sys.stderr

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/projects" % (USERNAME,SITE) )
os.environ['PYTHON_EGG_CACHE'] = '/home/%s/sites/%s/.python-eggs'
# Import our django handler
import django.core.handlers.wsgi

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT
application = django.core.handlers.wsgi.WSGIHandler()
EOF
}

create_fcgi () {
# Add .htaccess only under fcgi
cat >>$HOME_DIR/sites/$SITE/public/.htaccess <<EOF
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]

# rewrite trac/ requests so they get served by trac.fcgi
RewriteRule ^(trac\.fcgi/.*)$ - [L]
RewriteRule ^trac/(.*)$ trac.fcgi/\$1 [L]

# rewrite everything else to disptach.fcgi
RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/\$1 [QSA,L]
EOF

cat >>$HOME_DIR/sites/$SITE/public/dispatch.fcgi <<EOF
#!/usr/bin/python
import sys, os

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/apps/%s" % (USERNAME,SITE,PROJECT) )
sys.path.insert(0, "/home/%s/lib/python" % (USERNAME,) )

# Switch to the directory of your project.
#os.chdir("/home/%s/dev/" % (USERNAME,) ) 

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
EOF

echo "make dispatch.fcgi executable"
chmod +x $HOME_DIR/sites/$SITE/public/dispatch.fcgi
}

setup_django () {
# Build django project
cd $LIB_DIR && \
/usr/bin/django-admin.py startproject $PROJECT && \
cd $PROJECT && \
mkdir media && \
mkdir templates 

# Link in media dirs
cd $HOME_DIR/sites/$SITE/public
ln -s /usr/lib/python2.4/site-packages/django/contrib/admin/media admin_media
ln -s $LIB_DIR/$PROJECT/media .
create_wsgi
#create_fcgi

# Add local override trick to settings.py
cat >> $LIB_DIR/$PROJECT/settings.py <<EOF

# allow local settings to override
try:
    from local_settings import *
except ImportError, exp:
    pass

EOF

echo "Adding overrides to local_settings.py"
cat >> $LIB_DIR/$PROJECT/local_settings.py <<EOF
import os
DATABASE_ENGINE='mysql'
DATABASE_NAME='${PROJECT}'
DATABASE_USER='${USER}'
DATABASE_PASSWORD='${DB_PASS}'
TIME_ZONE = 'America/Chicago'
# Make this unique, and don't share it with anybody.
SECRET_KEY = '${SECRET_KEY}'

TEMPLATE_DIRS = (
    os.path.join (os.getcwd(), 'templates'),
    '$LIB_DIR/$PROJECT/templates',
)

MEDIA_ROOT = '$LIB_DIR/$PROJECT/media/'

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.cache.CacheMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'django.contrib.markup',
    'template_utils',
    'django.contrib.flatpages',
)
ADMIN_MEDIA_PREFIX = '/admin_media/'

EOF


echo "local_settings has a pw in it, so only allow read from owner."
echo "chmod 400 $LIB_DIR/$PROJECT/local_settings.py"
chmod 400 $LIB_DIR/$PROJECT/local_settings.py

echo "Configure django apps by hand ..."
echo "$LIB_DIR/$PROJECT/local_settings.py"

# Hardening app
cd $HOME_DIR && \
find . -name \*.py -type f -exec chmod -w {} \;
find . -type d -exec chmod -w {} \;

}

cleanup_perms () {
# Make sure everything in user dir is owned by user
echo "Finalizing some permissions ..."
echo "chown -R $USER $HOME_DIR"
chown -R $USER:$USER $HOME_DIR

echo "chmod +w $HOME_DIR"
chmod +w $HOME_DIR

# Chown logs dir as root
echo "chown root $HOME_DIR/sites/$SITE/logs"
chown root $HOME_DIR/sites/$SITE/logs

echo "chmod +w $HOME_DIR/sites/$SITE/logs"
chmod +w $HOME_DIR/sites/$SITE/logs
}

bootstrap_user
create_database
setup_django
#setup_trac
cleanup_perms

Revision: 5918
at April 1, 2009 14:23 by mandric


Updated Code
#!/bin/bash
# Create a django skeleton.
# Dir structure looks like:
#~example/
#  bin
#  sites
#    example.com
#        |-- lib
#        |   `-- example
#        |       |-- media
#        |       `-- templates
#        |-- logs
#        |-- public
#        |   |-- admin_media -> /path/to/django/contrib/admin/media
#        |   `-- media -> /home/example/sites/example.com/lib/example/media
#        |-- static
#        |-- trac
#        |-- uploads


if [ -z $1 ] ; then
echo 'Please specify a username.'
exit
fi

if [ -z $2 ] ; then
echo 'Please specify a domain name.'
exit
fi

USER=$1
PROJECT=$USER
SITE=$2
USER_PASS=`makepasswd --chars=15`
DB_PASS=`makepasswd --chars=25`
SECRET_KEY=`makepasswd --chars=128`
HOME_DIR=/home/$USER
LIB_DIR=$HOME_DIR/sites/$SITE/projects

bootstrap_user () {
echo "rand user pass: $USER_PASS"
echo "Creating $USER"
# add unix user
adduser $USER --force-badname

mkdir $HOME_DIR/sites/$SITE -p
mkdir $LIB_DIR
mkdir $HOME_DIR/sites/$SITE/logs
mkdir $HOME_DIR/sites/$SITE/public
mkdir $HOME_DIR/sites/$SITE/static
mkdir $HOME_DIR/sites/$SITE/uploads
mkdir $HOME_DIR/bin
}



create_database () {
    # Create database
    NAME=$PROJECT
    USER=$USER
    echo "$USER db pass: $DB_PASS"
    echo "enter mysql root pass"
    mysql -u root -p  << EOF
create database $NAME default character set = 'utf8' default collate = 'utf8_general_ci';
GRANT ALL ON ${NAME}.* TO '${NAME}'@'localhost' IDENTIFIED BY '${DB_PASS}';
flush privileges;
EOF
}

# Setup Trac
setup_trac() {
mkdir -p $HOME_DIR/sites/$SITE/trac/env 
mkdir $HOME_DIR/sites/$SITE/trac/repos
trac-admin $HOME_DIR/sites/$SITE/trac/env initenv
svnadmin create $HOME_DIR/sites/$SITE/trac/repos/

cat >>$HOME_DIR/sites/$SITE/public/trac.fcgi <<EOF
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2004 Edgewall Software
# Copyright (C) 2003-2004 Jonas Borgström <[email protected]>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Jonas Borgström <[email protected]>

# for andric.us
import os

os.environ['TRAC_ENV'] = '${HOME_DIR}/sites/${SITE}/trac/env'

try:
    from trac.web import fcgi_frontend
    fcgi_frontend.run()
except SystemExit:
    raise
except Exception, e:
    print 'Content-Type: text/plain

',
    print 'Oops...'
    print
    print 'Trac detected an internal error:'
    print
    print e
    print
    import traceback
    import StringIO
    tb = StringIO.StringIO()
    traceback.print_exc(file=tb)
    print tb.getvalue()
EOF
}


create_wsgi () {
# Copy .htaccess and dispatch.wsgi into public dir
# This also depends on the proper configuration in apache.
cat >>$HOME_DIR/sites/$SITE/public/dispatch.wsgi <<EOF
#!/usr/bin/python2.5
import sys, os

# make stdout go to stderr for wsgi apps
sys.stdout = sys.stderr

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/projects" % (USERNAME,SITE) )
os.environ['PYTHON_EGG_CACHE'] = '/home/%s/sites/%s/.python-eggs'
# Import our django handler
import django.core.handlers.wsgi

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT
application = django.core.handlers.wsgi.WSGIHandler()
EOF
}

create_fcgi () {
# Add .htaccess only under fcgi
cat >>$HOME_DIR/sites/$SITE/public/.htaccess <<EOF
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]

# rewrite trac/ requests so they get served by trac.fcgi
RewriteRule ^(trac\.fcgi/.*)$ - [L]
RewriteRule ^trac/(.*)$ trac.fcgi/\$1 [L]

# rewrite everything else to disptach.fcgi
RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/\$1 [QSA,L]
EOF

cat >>$HOME_DIR/sites/$SITE/public/dispatch.fcgi <<EOF
#!/usr/bin/python
import sys, os

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/apps/%s" % (USERNAME,SITE,PROJECT) )
sys.path.insert(0, "/home/%s/lib/python" % (USERNAME,) )

# Switch to the directory of your project.
#os.chdir("/home/%s/dev/" % (USERNAME,) ) 

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false")
EOF

echo "make dispatch.fcgi executable"
chmod +x $HOME_DIR/sites/$SITE/public/dispatch.fcgi
}

setup_django () {
# Build django project
cd $LIB_DIR && \
/usr/bin/django-admin.py startproject $PROJECT && \
cd $PROJECT && \
mkdir media && \
mkdir templates 

# Link in media dirs
cd $HOME_DIR/sites/$SITE/public
ln -s /usr/lib/python2.4/site-packages/django/contrib/admin/media admin_media
ln -s $LIB_DIR/$PROJECT/media .
create_wsgi
#create_fcgi

# Add local override trick to settings.py
cat >> $LIB_DIR/$PROJECT/settings.py <<EOF

# allow local settings to override
try:
    from local_settings import *
except ImportError, exp:
    pass

EOF

echo "Adding overrides to local_settings.py"
cat >> $LIB_DIR/$PROJECT/local_settings.py <<EOF
import os
DATABASE_ENGINE='mysql'
DATABASE_NAME='${PROJECT}'
DATABASE_USER='${USER}'
DATABASE_PASSWORD='${DB_PASS}'
TIME_ZONE = 'America/Chicago'
# Make this unique, and don't share it with anybody.
SECRET_KEY = '${SECRET_KEY}'

TEMPLATE_DIRS = (
    os.path.join (os.getcwd(), 'templates'),
    '$LIB_DIR/$PROJECT/templates',
)

MEDIA_ROOT = '$LIB_DIR/$PROJECT/media/'

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.cache.CacheMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'django.contrib.markup',
    'template_utils',
    'django.contrib.flatpages',
)
ADMIN_MEDIA_PREFIX = '/admin_media/'

EOF


echo "local_settings has a pw in it, so only allow read from owner."
echo "chmod 400 $LIB_DIR/$PROJECT/local_settings.py"
chmod 400 $LIB_DIR/$PROJECT/local_settings.py

echo "Configure django apps by hand ..."
echo "$LIB_DIR/$PROJECT/local_settings.py"

# Hardening app
cd $HOME_DIR && \
find . -name \*.py -type f -exec chmod -w {} \;
find . -type d -exec chmod -w {} \;

}

cleanup_perms () {
# Make sure everything in user dir is owned by user
echo "Finalizing some permissions ..."
echo "chown -R $USER $HOME_DIR"
chown -R $USER:$USER $HOME_DIR

echo "chmod +w $HOME_DIR"
chmod +w $HOME_DIR

# Chown logs dir as root
echo "chown root $HOME_DIR/sites/$SITE/logs"
chown root $HOME_DIR/sites/$SITE/logs

echo "chmod +w $HOME_DIR/sites/$SITE/logs"
chmod +w $HOME_DIR/sites/$SITE/logs
}

bootstrap_user
create_database
#setup_django
#setup_trac
cleanup_perms

Revision: 5917
at April 1, 2009 14:06 by mandric


Updated Code
#!/bin/bash
# Create a django skeleton.
# Dir structure looks like:
#~example/
#  bin
#  sites
#    example.com
#        |-- lib
#        |   `-- example
#        |       |-- media
#        |       `-- templates
#        |-- logs
#        |-- public
#        |   |-- admin_media -> /path/to/django/contrib/admin/media
#        |   `-- media -> /home/example/sites/example.com/lib/example/media
#        |-- static
#        |-- trac
#        |-- uploads


if [ -z $1 ] ; then
echo 'Please specify a username.'
exit
fi

if [ -z $2 ] ; then
echo 'Please specify a domain name.'
exit
fi

USER=$1
PROJECT=$USER
SITE=$2
USER_PASS=`makepasswd --chars=15`
DB_PASS=`makepasswd --chars=25`
SECRET_KEY=`makepasswd --chars=128`
HOME_DIR=/home/$USER
LIB_DIR=$HOME_DIR/sites/$SITE/projects

bootstrap_user () {
echo "rand user pass: $USER_PASS"
echo "Creating $USER"
# add unix user
adduser $USER --force-badname

mkdir $HOME_DIR/sites/$SITE -p
mkdir $LIB_DIR
mkdir $HOME_DIR/sites/$SITE/logs
mkdir $HOME_DIR/sites/$SITE/public
mkdir $HOME_DIR/sites/$SITE/static
mkdir $HOME_DIR/sites/$SITE/uploads
mkdir $HOME_DIR/bin
}



create_database () {
    # Create database
    NAME=$PROJECT
    USER=$USER
    echo "$USER db pass: $DB_PASS"
    echo "enter mysql root pass"
    mysql -u root -p  << EOF
create database $NAME;
GRANT ALL ON ${NAME}.* TO '${NAME}'@'localhost' IDENTIFIED BY '${DB_PASS}';
flush privileges;
EOF
}

# Setup Trac
setup_trac() {
mkdir -p $HOME_DIR/sites/$SITE/trac/env 
mkdir $HOME_DIR/sites/$SITE/trac/repos
trac-admin $HOME_DIR/sites/$SITE/trac/env initenv
svnadmin create $HOME_DIR/sites/$SITE/trac/repos/

cat >>$HOME_DIR/sites/$SITE/public/trac.fcgi <<EOF
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2004 Edgewall Software
# Copyright (C) 2003-2004 Jonas Borgström <[email protected]>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Jonas Borgström <[email protected]>

# for andric.us
import os

os.environ['TRAC_ENV'] = '${HOME_DIR}/sites/${SITE}/trac/env'

try:
    from trac.web import fcgi_frontend
    fcgi_frontend.run()
except SystemExit:
    raise
except Exception, e:
    print 'Content-Type: text/plain

',
    print 'Oops...'
    print
    print 'Trac detected an internal error:'
    print
    print e
    print
    import traceback
    import StringIO
    tb = StringIO.StringIO()
    traceback.print_exc(file=tb)
    print tb.getvalue()
EOF
}

setup_django () {
# Build django project
cd $LIB_DIR && \
/usr/bin/django-admin.py startproject $PROJECT && \
cd $PROJECT && \
mkdir media && \
mkdir templates 

# Link in media dirs
cd $HOME_DIR/sites/$SITE/public
ln -s /usr/lib/python2.4/site-packages/django/contrib/admin/media admin_media
ln -s $LIB_DIR/$PROJECT/media .

# Copy .htaccess and dispatch.fcgi into public dir
cat >>$HOME_DIR/sites/$SITE/public/dispatch.wsgi <<EOF
#!/usr/bin/python2.5
import sys, os

# make stdout go to stderr for wsgi apps
sys.stdout = sys.stderr

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/projects" % (USERNAME,SITE) )
os.environ['PYTHON_EGG_CACHE'] = '/home/%s/sites/%s/.python-eggs'
# Import our django handler
import django.core.handlers.wsgi

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT
application = django.core.handlers.wsgi.WSGIHandler()

EOF

# Add .htaccess only under fcgi
#cat >>$HOME_DIR/sites/$SITE/public/.htaccess <<EOF
#RewriteEngine On
#RewriteBase /
##RewriteCond %{REQUEST_FILENAME} !-f
##RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(media/.*)$ - [L]
#RewriteRule ^(admin_media/.*)$ - [L]
#
# rewrite trac/ requests so they get served by trac.fcgi
#RewriteRule ^(trac\.fcgi/.*)$ - [L]
#RewriteRule ^trac/(.*)$ trac.fcgi/\$1 [L]

# rewrite everything else to disptach.fcgi
#RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
#RewriteRule ^(.*)$ dispatch.fcgi/\$1 [QSA,L]
#EOF


# Add local override trick to settings.py
cat >> $LIB_DIR/$PROJECT/settings.py <<EOF

# allow local settings to override
try:
    from local_settings import *
except ImportError, exp:
    pass

EOF

echo "Adding overrides to local_settings.py"
cat >> $LIB_DIR/$PROJECT/local_settings.py <<EOF
import os
DATABASE_ENGINE='mysql'
DATABASE_NAME='${PROJECT}'
DATABASE_USER='${USER}'
DATABASE_PASSWORD='${DB_PASS}'
TIME_ZONE = 'America/Chicago'
# Make this unique, and don't share it with anybody.
SECRET_KEY = '${SECRET_KEY}'

TEMPLATE_DIRS = (
    os.path.join (os.getcwd(), 'templates'),
    '$LIB_DIR/$PROJECT/templates',
)

MEDIA_ROOT = '$LIB_DIR/$PROJECT/media/'

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.cache.CacheMiddleware',
    'django.middleware.doc.XViewMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'django.contrib.markup',
    'template_utils',
    'django.contrib.flatpages',
)
ADMIN_MEDIA_PREFIX = '/admin_media/'

EOF

echo "make dispatch.fcgi executable"
chmod +x $HOME_DIR/sites/$SITE/public/dispatch.fcgi

echo "settings has a pw in it, so only allow read from owner."
echo "chmod 400 $LIB_DIR/$PROJECT/local_settings.py"
chmod 400 $LIB_DIR/$PROJECT/local_settings.py

echo "Configure django apps by hand ..."
echo "$LIB_DIR/$PROJECT/local_settings.py"

# Hardening app
cd $HOME_DIR && \
find . -name \*.py -type f -exec chmod -w {} \;
find . -type d -exec chmod -w {} \;

}

cleanup_perms () {
# Make sure everything in user dir is owned by user
echo "Finalizing some permissions ..."
echo "chown -R $USER $HOME_DIR"
chown -R $USER:$USER $HOME_DIR

echo "chmod +w $HOME_DIR"
chmod +w $HOME_DIR

# Chown logs dir as root
echo "chown root $HOME_DIR/sites/$SITE/logs"
chown root $HOME_DIR/sites/$SITE/logs

echo "chmod +w $HOME_DIR/sites/$SITE/logs"
chmod +w $HOME_DIR/sites/$SITE/logs
}

bootstrap_user
create_database
#setup_django
#setup_trac
cleanup_perms

Revision: 5916
at May 21, 2008 11:32 by mandric


Updated Code
#!/bin/bash
# Create a django skeleton.
# Dir structure looks like:
#~example/
#  bin
#  sites
#    example.com
#        |-- lib
#        |   `-- example
#        |       |-- media
#        |       `-- templates
#        |-- logs
#        |-- public
#        |   |-- admin_media -> /path/to/django/contrib/admin/media
#        |   `-- media -> /home/example/sites/example.com/lib/example/media
#        |-- static
#        |-- trac
#        |-- uploads


if [ -z $1 ] ; then
echo 'Please specify a username.'
exit
fi

if [ -z $2 ] ; then
echo 'Please specify a domain name.'
exit
fi

USER=$1
PROJECT=$USER
SITE=$2
USER_PASS=`makepasswd --chars=25`
DB_PASS=`makepasswd --chars=25`
SECRET_KEY=`makepasswd --chars=128`
HOME_DIR=/home/$USER
LIB_DIR=$HOME_DIR/sites/$SITE/lib

echo "rand user pass: $USER_PASS"
echo "rand db pass: $DB_PASS"
echo "Creating $USER"
# add unix user
adduser $USER --force-badname

mkdir $HOME_DIR/sites/$SITE -p
mkdir $LIB_DIR
mkdir $HOME_DIR/sites/$SITE/logs
mkdir $HOME_DIR/sites/$SITE/public
mkdir $HOME_DIR/sites/$SITE/static
mkdir $HOME_DIR/sites/$SITE/uploads
mkdir $HOME_DIR/bin

# Build django project
cd $LIB_DIR && \
/usr/bin/django-admin.py startproject $PROJECT && \
cd $PROJECT && \
mkdir media && \
mkdir templates 

# Create database
echo "enter mysql root pass"
mysql -u root -p  << EOF
create database $PROJECT;
GRANT ALL ON ${PROJECT}.* TO '${USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';
flush privileges;
EOF

# Link in media dirs
cd $HOME_DIR/sites/$SITE/public
ln -s /usr/lib/python2.4/site-packages/django/contrib/admin/media admin_media
ln -s $LIB_DIR/$PROJECT/media .

# Setup Trac
mkdir -p $HOME_DIR/sites/$SITE/trac/env 
mkdir $HOME_DIR/sites/$SITE/trac/repos
trac-admin $HOME_DIR/sites/$SITE/trac/env initenv
svnadmin create $HOME_DIR/sites/$SITE/trac/repos/

cat >>$HOME_DIR/sites/$SITE/public/trac.fcgi <<EOF
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2004 Edgewall Software
# Copyright (C) 2003-2004 Jonas Borgström <[email protected]>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Jonas Borgström <[email protected]>

# for andric.us
import os

os.environ['TRAC_ENV'] = '${HOME_DIR}/sites/${SITE}/trac/env'

try:
    from trac.web import fcgi_frontend
    fcgi_frontend.run()
except SystemExit:
    raise
except Exception, e:
    print 'Content-Type: text/plain

',
    print 'Oops...'
    print
    print 'Trac detected an internal error:'
    print
    print e
    print
    import traceback
    import StringIO
    tb = StringIO.StringIO()
    traceback.print_exc(file=tb)
    print tb.getvalue()
EOF

# Copy .htaccess and dispatch.fcgi into public dir
cat >>$HOME_DIR/sites/$SITE/public/dispatch.fcgi <<EOF
#!/usr/bin/python
import sys, os

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/lib" % (USERNAME,SITE) )

# Switch to the directory of your project.
#os.chdir("/home/%s/dev/" % (USERNAME,) ) 

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false") 
EOF

# Add .htaccess
cat >>$HOME_DIR/sites/$SITE/public/.htaccess <<EOF
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]

# rewrite trac/ requests so they get served by trac.fcgi
RewriteRule ^(trac\.fcgi/.*)$ - [L]
RewriteRule ^trac/(.*)$ trac.fcgi/\$1 [L]

# rewrite everything else to disptach.fcgi
RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/\$1 [QSA,L]
EOF


# Add local override trick to settings.py
cat >> $LIB_DIR/$PROJECT/settings.py <<EOF

# allow local settings to override
try:
    from local_settings import *
except ImportError, exp:
    pass

EOF

echo "Adding overrides to local_settings.py"
cat >> $LIB_DIR/$PROJECT/local_settings.py <<EOF
import os
DATABASE_ENGINE='mysql'
DATABASE_NAME='${PROJECT}'
DATABASE_USER='${USER}'
DATABASE_PASSWORD='${DB_PASS}'
TIME_ZONE = 'America/Chicago'
# Make this unique, and don't share it with anybody.
SECRET_KEY = '${SECRET_KEY}'

TEMPLATE_DIRS = (
    os.path.join (os.getcwd(), 'templates'),
    '$LIB_DIR/$PROJECT/templates',
)

MEDIA_ROOT = '$LIB_DIR/$PROJECT/media/'
EOF

# Make sure everything in user dir is owned by user
echo "Finalizing some permissions ..."
echo "chown -R $USER $HOME_DIR"
chown -R $USER:$USER $HOME_DIR

echo "chmod +w $HOME_DIR"
chmod +w $HOME_DIR

echo "make dispatch.fcgi executable"
chmod +x $HOME_DIR/sites/$SITE/public/dispatch.fcgi

# Chown logs dir as root
echo "chown root $HOME_DIR/sites/$SITE/logs"
chown root $HOME_DIR/sites/$SITE/logs

echo "settings has a pw in it, so only allow read from owner."
echo "chmod 400 $LIB_DIR/$PROJECT/local_settings.py"
chmod 400 $LIB_DIR/$PROJECT/local_settings.py

echo "Configure django apps by hand ..."
echo "$LIB_DIR/$PROJECT/local_settings.py"

# Hardening, make 
cd $HOME_DIR && \
find . -name \*.py -type f -exec chmod -w {} \;
find . -type d -exec chmod -w {} \;

echo "chmod +w $HOME_DIR/sites/$SITE/logs"
chmod +w $HOME_DIR/sites/$SITE/logs

Revision: 5915
at May 20, 2008 23:58 by mandric


Updated Code
#!/bin/bash
# Create a django skeleton.
# Dir structure looks like:
#~example
#  bin
#  sites
#    example.com
#        |-- lib
#        |   `-- example
#        |       |-- media
#        |       `-- templates
#        |-- logs
#        |-- public
#        |   |-- admin_media -> /path/to/django/contrib/admin/media
#        |   `-- media -> /home/example/sites/example.com/lib/<proj>/media
#        |-- static
#        |-- trac
#        |-- uploads


if [ -z $1 ] ; then
echo 'Please specify a username.'
exit
fi

if [ -z $2 ] ; then
echo 'Please specify a domain name.'
exit
fi

USER=$1
PROJECT=$USER
SITE=$2
USER_PASS=`makepasswd --chars=25`
DB_PASS=`makepasswd --chars=25`
SECRET_KEY=`makepasswd --chars=128`
HOME_DIR=/home/$USER
LIB_DIR=$HOME_DIR/sites/$SITE/lib

echo "rand user pass: $USER_PASS"
echo "rand db pass: $DB_PASS"
echo "Creating $USER"
# add unix user
adduser $USER --force-badname

mkdir $HOME_DIR/sites/$SITE -p
mkdir $LIB_DIR
mkdir $HOME_DIR/sites/$SITE/logs
mkdir $HOME_DIR/sites/$SITE/public
mkdir $HOME_DIR/sites/$SITE/static
mkdir $HOME_DIR/sites/$SITE/uploads
mkdir $HOME_DIR/bin

# Build django project
cd $LIB_DIR && \
/usr/bin/django-admin.py startproject $PROJECT && \
cd $PROJECT && \
mkdir media && \
mkdir templates 

# Create database
echo "enter mysql root pass"
mysql -u root -p  << EOF
create database $PROJECT;
GRANT ALL ON ${PROJECT}.* TO '${USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';
flush privileges;
EOF

# Link in media dirs
cd $HOME_DIR/sites/$SITE/public
ln -s /usr/lib/python2.4/site-packages/django/contrib/admin/media admin_media
ln -s $LIB_DIR/$PROJECT/media .

# Setup Trac
mkdir -p $HOME_DIR/sites/$SITE/trac/env 
mkdir $HOME_DIR/sites/$SITE/trac/repos
trac-admin $HOME_DIR/sites/$SITE/trac/env initenv
svnadmin create $HOME_DIR/sites/$SITE/trac/repos/

# Hardening, make 
cd $HOME_DIR
find . -name \*.py -type f -exec chmod -w {} \;
find . -type d -exec chmod -w {} \;

cat >>$HOME_DIR/sites/$SITE/public/trac.fcgi <<EOF
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2004 Edgewall Software
# Copyright (C) 2003-2004 Jonas Borgstr�¶m <[email protected]>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Jonas Borgstr�¶m <[email protected]>

# for andric.us
import os

os.environ['TRAC_ENV'] = '${HOME_DIR}/sites/${SITE}/trac/env'

try:
    from trac.web import fcgi_frontend
    fcgi_frontend.run()
except SystemExit:
    raise
except Exception, e:
    print 'Content-Type: text/plain

',
    print 'Oops...'
    print
    print 'Trac detected an internal error:'
    print
    print e
    print
    import traceback
    import StringIO
    tb = StringIO.StringIO()
    traceback.print_exc(file=tb)
    print tb.getvalue()
EOF

# Copy .htaccess and dispatch.fcgi into public dir
cat >>$HOME_DIR/sites/$SITE/public/dispatch.fcgi <<EOF
#!/usr/bin/python
import sys, os

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/lib" % (USERNAME,SITE) )

# Switch to the directory of your project.
#os.chdir("/home/%s/dev/" % (USERNAME,) ) 

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false") 
EOF

# Add .htaccess
cat >>$HOME_DIR/sites/$SITE/public/.htaccess <<EOF
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]

# rewrite trac/ requests so they get served by trac.fcgi
RewriteRule ^(trac\.fcgi/.*)$ - [L]
RewriteRule ^trac/(.*)$ trac.fcgi/$1 [L]

# rewrite everything else to disptach.fcgi
RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]
EOF


# Add local override trick to settings.py
cat >> $LIB_DIR/$PROJECT/settings.py <<EOF

# allow local settings to override
try:
    from local_settings import *
except ImportError, exp:
    pass

EOF

echo "Adding overrides to local_settings.py"
cat >> $LIB_DIR/$PROJECT/local_settings.py <<EOF
import os
DATABASE_ENGINE='mysql'
DATABASE_NAME='${PROJECT}'
DATABASE_USER='${USER}'
DATABASE_PASSWORD='${DB_PASS}'
TIME_ZONE = 'America/Chicago'
# Make this unique, and don't share it with anybody.
SECRET_KEY = '${SECRET_KEY}'

TEMPLATE_DIRS = (
    os.path.join (os.getcwd(), 'templates'),
    '$LIB_DIR/$PROJECT/templates',
)

MEDIA_ROOT = '$LIB_DIR/$PROJECT/media/'
EOF

# Make sure everything in user dir is owned by user
echo "Finalizing some permissions ..."
echo "chown -R $USER $HOME_DIR"
chown -R $USER:$USER $HOME_DIR

echo "chmod +w $HOME_DIR"
chmod +w $HOME_DIR

echo "make dispatch.fcgi executable"
chmod +x $HOME_DIR/sites/$SITE/public/dispatch.fcgi

# Chown logs dir as root
echo "chown root $HOME_DIR/sites/$SITE/logs"
chown root $HOME_DIR/sites/$SITE/logs

echo "settings has a pw in it, so only allow read from owner."
echo "chmod 400 $LIB_DIR/$PROJECT/local_settings.py"
chmod 400 $LIB_DIR/$PROJECT/local_settings.py

echo "Configure django apps by hand ..."
echo "$LIB_DIR/$PROJECT/local_settings.py"

Revision: 5914
at May 20, 2008 23:56 by mandric


Updated Code
#!/bin/bash
# Create a django skeleton.
# Dir structure looks like:
#~example
#  bin
#  sites
#    example.com
#        |-- lib
#        |   `-- example
#        |       |-- media
#        |       `-- templates
#        |-- logs
#        |-- public
#        |   |-- admin_media -> /usr/lib/python2.4/site-packages/django/contrib/admin/media
#        |   `-- media -> /home/example/sites/example.com/lib/<proj>/media
#        |-- static
#        |-- trac
#        |-- uploads


if [ -z $1 ] ; then
echo 'Please specify a username.'
exit
fi

if [ -z $2 ] ; then
echo 'Please specify a domain name.'
exit
fi

USER=$1
PROJECT=$USER
SITE=$2
USER_PASS=`makepasswd --chars=25`
DB_PASS=`makepasswd --chars=25`
SECRET_KEY=`makepasswd --chars=128`
HOME_DIR=/home/$USER
LIB_DIR=$HOME_DIR/sites/$SITE/lib

echo "rand user pass: $USER_PASS"
echo "rand db pass: $DB_PASS"
echo "Creating $USER"
# add unix user
adduser $USER --force-badname

mkdir $HOME_DIR/sites/$SITE -p
mkdir $LIB_DIR
mkdir $HOME_DIR/sites/$SITE/logs
mkdir $HOME_DIR/sites/$SITE/public
mkdir $HOME_DIR/sites/$SITE/static
mkdir $HOME_DIR/sites/$SITE/uploads
mkdir $HOME_DIR/bin

# Build django project
cd $LIB_DIR && \
/usr/bin/django-admin.py startproject $PROJECT && \
cd $PROJECT && \
mkdir media && \
mkdir templates 

# Create database
echo "enter mysql root pass"
mysql -u root -p  << EOF
create database $PROJECT;
GRANT ALL ON ${PROJECT}.* TO '${USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';
flush privileges;
EOF

# Link in media dirs
cd $HOME_DIR/sites/$SITE/public
ln -s /usr/lib/python2.4/site-packages/django/contrib/admin/media admin_media
ln -s $LIB_DIR/$PROJECT/media .

# Setup Trac
mkdir -p $HOME_DIR/sites/$SITE/trac/env 
mkdir $HOME_DIR/sites/$SITE/trac/repos
trac-admin $HOME_DIR/sites/$SITE/trac/env initenv
svnadmin create $HOME_DIR/sites/$SITE/trac/repos/

# Hardening, make 
cd $HOME_DIR
find . -name \*.py -type f -exec chmod -w {} \;
find . -type d -exec chmod -w {} \;

cat >>$HOME_DIR/sites/$SITE/public/trac.fcgi <<EOF
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2004 Edgewall Software
# Copyright (C) 2003-2004 Jonas Borgström <[email protected]>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Jonas Borgström <[email protected]>

# for andric.us
import os

os.environ['TRAC_ENV'] = '${HOME_DIR}/sites/${SITE}/trac/env'

try:
    from trac.web import fcgi_frontend
    fcgi_frontend.run()
except SystemExit:
    raise
except Exception, e:
    print 'Content-Type: text/plain

',
    print 'Oops...'
    print
    print 'Trac detected an internal error:'
    print
    print e
    print
    import traceback
    import StringIO
    tb = StringIO.StringIO()
    traceback.print_exc(file=tb)
    print tb.getvalue()
EOF

# Copy .htaccess and dispatch.fcgi into public dir
cat >>$HOME_DIR/sites/$SITE/public/dispatch.fcgi <<EOF
#!/usr/bin/python
import sys, os

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/lib" % (USERNAME,SITE) )

# Switch to the directory of your project.
#os.chdir("/home/%s/dev/" % (USERNAME,) ) 

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false") 
EOF

# Add .htaccess
cat >>$HOME_DIR/sites/$SITE/public/.htaccess <<EOF
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]

# rewrite trac/ requests so they get served by trac.fcgi
RewriteRule ^(trac\.fcgi/.*)$ - [L]
RewriteRule ^trac/(.*)$ trac.fcgi/$1 [L]

# rewrite everything else to disptach.fcgi
RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]
EOF


# Add local override trick to settings.py
cat >> $LIB_DIR/$PROJECT/settings.py <<EOF

# allow local settings to override
try:
    from local_settings import *
except ImportError, exp:
    pass

EOF

echo "Adding overrides to local_settings.py"
cat >> $LIB_DIR/$PROJECT/local_settings.py <<EOF
import os
DATABASE_ENGINE='mysql'
DATABASE_NAME='${PROJECT}'
DATABASE_USER='${USER}'
DATABASE_PASSWORD='${DB_PASS}'
TIME_ZONE = 'America/Chicago'
# Make this unique, and don't share it with anybody.
SECRET_KEY = '${SECRET_KEY}'

TEMPLATE_DIRS = (
    os.path.join (os.getcwd(), 'templates'),
    '$LIB_DIR/$PROJECT/templates',
)

MEDIA_ROOT = '$LIB_DIR/$PROJECT/media/'
EOF

# Make sure everything in user dir is owned by user
echo "Finalizing some permissions ..."
echo "chown -R $USER $HOME_DIR"
chown -R $USER:$USER $HOME_DIR

echo "chmod +w $HOME_DIR"
chmod +w $HOME_DIR

echo "make dispatch.fcgi executable"
chmod +x $HOME_DIR/sites/$SITE/public/dispatch.fcgi

# Chown logs dir as root
echo "chown root $HOME_DIR/sites/$SITE/logs"
chown root $HOME_DIR/sites/$SITE/logs

echo "settings has a pw in it, so only allow read from owner."
echo "chmod 400 $LIB_DIR/$PROJECT/local_settings.py"
chmod 400 $LIB_DIR/$PROJECT/local_settings.py

echo "Configure django apps by hand ..."
echo "$LIB_DIR/$PROJECT/local_settings.py"

Revision: 5913
at May 20, 2008 23:52 by mandric


Updated Code
#!/bin/bash
# Create a django skeleton.
# Dir structure looks like:
#  bin
#  sites
#    example.com
#        |-- lib
#        |   `-- example
#        |       |-- media
#        |       `-- templates
#        |-- logs
#        |-- public
#        |   |-- admin_media -> /usr/lib/python2.4/site-packages/django/contrib/admin/media
#        |   `-- media -> /home/example/sites/example.com/lib/mm401k/media
#        |-- static
#        |-- trac
#        |-- uploads


if [ -z $1 ] ; then
echo 'Please specify a username.'
exit
fi

if [ -z $2 ] ; then
echo 'Please specify a domain name.'
exit
fi

USER=$1
PROJECT=$USER
SITE=$2
USER_PASS=`makepasswd --chars=25`
DB_PASS=`makepasswd --chars=25`
SECRET_KEY=`makepasswd --chars=128`
HOME_DIR=/home/$USER
LIB_DIR=$HOME_DIR/sites/$SITE/lib

echo "rand user pass: $USER_PASS"
echo "rand db pass: $DB_PASS"
echo "Creating $USER"
# add unix user
adduser $USER --force-badname

mkdir $HOME_DIR/sites/$SITE -p
mkdir $LIB_DIR
mkdir $HOME_DIR/sites/$SITE/logs
mkdir $HOME_DIR/sites/$SITE/public
mkdir $HOME_DIR/sites/$SITE/static
mkdir $HOME_DIR/sites/$SITE/uploads
mkdir $HOME_DIR/bin

# Build django project
cd $LIB_DIR && \
/usr/bin/django-admin.py startproject $PROJECT && \
cd $PROJECT && \
mkdir media && \
mkdir templates 

# Create database
echo "enter mysql root pass"
mysql -u root -p  << EOF
create database $PROJECT;
GRANT ALL ON ${PROJECT}.* TO '${USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';
flush privileges;
EOF

# Link in media dirs
cd $HOME_DIR/sites/$SITE/public
ln -s /usr/lib/python2.4/site-packages/django/contrib/admin/media admin_media
ln -s $LIB_DIR/$PROJECT/media .

# Setup Trac
mkdir -p $HOME_DIR/sites/$SITE/trac/env 
mkdir $HOME_DIR/sites/$SITE/trac/repos
trac-admin $HOME_DIR/sites/$SITE/trac/env initenv
svnadmin create $HOME_DIR/sites/$SITE/trac/repos/

# Hardening, make 
cd $HOME_DIR
find . -name \*.py -type f -exec chmod -w {} \;
find . -type d -exec chmod -w {} \;

cat >>$HOME_DIR/sites/$SITE/public/trac.fcgi <<EOF
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2003-2004 Edgewall Software
# Copyright (C) 2003-2004 Jonas Borgström <[email protected]>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
#
# Author: Jonas Borgström <[email protected]>

# for andric.us
import os

os.environ['TRAC_ENV'] = '${HOME_DIR}/sites/${SITE}/trac/env'

try:
    from trac.web import fcgi_frontend
    fcgi_frontend.run()
except SystemExit:
    raise
except Exception, e:
    print 'Content-Type: text/plain

',
    print 'Oops...'
    print
    print 'Trac detected an internal error:'
    print
    print e
    print
    import traceback
    import StringIO
    tb = StringIO.StringIO()
    traceback.print_exc(file=tb)
    print tb.getvalue()
EOF

# Copy .htaccess and dispatch.fcgi into public dir
cat >>$HOME_DIR/sites/$SITE/public/dispatch.fcgi <<EOF
#!/usr/bin/python
import sys, os

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/lib" % (USERNAME,SITE) )

# Switch to the directory of your project.
#os.chdir("/home/%s/dev/" % (USERNAME,) ) 

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false") 
EOF

# Add .htaccess
cat >>$HOME_DIR/sites/$SITE/public/.htaccess <<EOF
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]

# rewrite trac/ requests so they get served by trac.fcgi
RewriteRule ^(trac\.fcgi/.*)$ - [L]
RewriteRule ^trac/(.*)$ trac.fcgi/$1 [L]

# rewrite everything else to disptach.fcgi
RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]
EOF


# Add local override trick to settings.py
cat >> $LIB_DIR/$PROJECT/settings.py <<EOF

# allow local settings to override
try:
    from local_settings import *
except ImportError, exp:
    pass

EOF

echo "Adding overrides to local_settings.py"
cat >> $LIB_DIR/$PROJECT/local_settings.py <<EOF
import os
DATABASE_ENGINE='mysql'
DATABASE_NAME='${PROJECT}'
DATABASE_USER='${USER}'
DATABASE_PASSWORD='${DB_PASS}'
TIME_ZONE = 'America/Chicago'
# Make this unique, and don't share it with anybody.
SECRET_KEY = '${SECRET_KEY}'

TEMPLATE_DIRS = (
    os.path.join (os.getcwd(), 'templates'),
    '$LIB_DIR/$PROJECT/templates',
)

MEDIA_ROOT = '$LIB_DIR/$PROJECT/media/'
EOF

# Make sure everything in user dir is owned by user
echo "Finalizing some permissions ..."
echo "chown -R $USER $HOME_DIR"
chown -R $USER:$USER $HOME_DIR

echo "chmod +w $HOME_DIR"
chmod +w $HOME_DIR

echo "make dispatch.fcgi executable"
chmod +x $HOME_DIR/sites/$SITE/public/dispatch.fcgi

# Chown logs dir as root
echo "chown root $HOME_DIR/sites/$SITE/logs"
chown root $HOME_DIR/sites/$SITE/logs

echo "settings has a pw in it, so only allow read from owner."
echo "chmod 400 $LIB_DIR/$PROJECT/local_settings.py"
chmod 400 $LIB_DIR/$PROJECT/local_settings.py

echo "Configure django apps by hand ..."
echo "$LIB_DIR/$PROJECT/local_settings.py"

Revision: 5912
at April 11, 2008 12:22 by mandric


Updated Code
#!/bin/bash
# Create a django skeleton on my debian box.

if [ -z $1 ] ; then
echo 'Please specify a username.'
exit
fi

if [ -z $2 ] ; then
echo 'Please specify a domain name.'
exit

USER=$1
PROJECT=$USER
SITE=$2
USER_PASS=`makepasswd --chars=25`
DB_PASS=`makepasswd --chars=25`
HOME_DIR=/home/$USER
LIB_DIR=$HOME_DIR/sites/$SITE/lib
DJANGO_DIR=/usr/lib/python2.4/site-packages/django

echo "Creating $USER"
# add unix user, allowing numbers in the username.
adduser $USER --force-badname

mkdir $HOME_DIR/sites/$SITE -p
mkdir $LIB_DIR
mkdir $HOME_DIR/sites/$SITE/logs
mkdir $HOME_DIR/sites/$SITE/public
mkdir $HOME_DIR/sites/$SITE/static
mkdir $HOME_DIR/sites/$SITE/uploads
mkdir $HOME_DIR/bin

# Build django project
cd $LIB_DIR && \
$DJANGO_DIR/bin/django-admin.py startproject $PROJECT && \
cd $PROJECT && \
mkdir media && \
mkdir templates 

# Create database
mysql -u root -p  << EOF
create database $PROJECT;
GRANT ALL ON ${PROJECT}.* TO '${USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';
flush privileges;
exit;
EOF

# Link in media dirs
cd $HOME_DIR/sites/$SITE/public
ln -s $DJANGO_DIR/contrib/admin/media admin_media
ln -s $HOME_DIR/sites/$SITE/$PROJECT/media .

# Setup Trac
mkdir -p $HOME_DIR/sites/$SITE/trac/env 
mkdir $HOME_DIR/sites/$SITE/trac/repos
trac-admin $HOME_DIR/sites/$SITE/trac/env initenv
svnadmin create $HOME_DIR/sites/$SITE/trac/repos/

# Hardening a bit
cd $HOME_DIR
find . -name \*.py -type f -exec chmod -w {} \;
find . -type d -exec chmod -w {} \;

# Copy .htaccess and dispatch.fcgi into public dir
cat >>$HOME_DIR/sites/$SITE/public/dispatch.fcgi <<EOF
#!/usr/bin/python
import sys, os

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/lib" % (USERNAME,SITE) )

# Switch to the directory of your project.
#os.chdir("/home/%s/dev/" % (USERNAME,) ) 

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false") 
EOF

# Add .htaccess
cat >>$HOME_DIR/sites/$SITE/public/.htaccess <<EOF
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]

# rewrite trac/ requests so they get served by trac.fcgi
RewriteRule ^(trac\.fcgi/.*)$ - [L]
RewriteRule ^trac/(.*)$ trac.fcgi/$1 [L]

# rewrite everything else to disptach.fcgi
RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]
EOF

# Make sure everything in user dir is owned by user
echo "Finalizing some permissions ..."

echo "chown -R $USER $HOME_DIR"
chown -R $USER $HOME_DIR

echo "chmod +w $HOME_DIR"
chmod +w $HOME_DIR

# Chown logs dir as root
echo "chown root $HOME_DIR/sites/$SITE/logs"
chown root $HOME_DIR/sites/$SITE/logs

# Configure the rest by hand ...
echo "DATABASE_USER='$USER'" >> $LIB_DIR/$PROJECT/settings.py
echo "DATABASE_PASSWORD='$DB_PASS'" >> $LIB_DIR/$PROJECT/settings.py
echo "Configure $LIB_DIR/$PROJECT/settings.py"
echo "Configure Apache"

Revision: 5911
at April 11, 2008 12:21 by mandric


Updated Code
#!/bin/bash
# Create a django skeleton on my debian box.

if [ -z $1 ] ; then
echo 'Please specify a username.'
exit
fi

if [ -z $2 ] ; then
echo 'Please specify a domain name.'
exit

USER=$1
PROJECT=$USER
SITE=$2
USER_PASS=`makepasswd --chars=25`
DB_PASS=`makepasswd --chars=25`
HOME_DIR=/home/$USER
LIB_DIR=$HOME_DIR/sites/$SITE/lib
DJANGO_DIR=/usr/lib/python2.4/site-packages/django

echo "Creating $USER"
# add unix user, allowing numbers in the username.
adduser $USER --force-badname

mkdir $HOME_DIR/sites/$SITE -p
mkdir $LIB_DIR
mkdir $HOME_DIR/sites/$SITE/logs
mkdir $HOME_DIR/sites/$SITE/public
mkdir $HOME_DIR/sites/$SITE/static
mkdir $HOME_DIR/sites/$SITE/uploads
mkdir $HOME_DIR/bin

# Build django project
cd $LIB_DIR && \
$DJANGO_DIR/bin/django-admin.py startproject $PROJECT && \
cd $PROJECT && \
mkdir media && \
mkdir templates 

# Create database
mysql -u root -p  << EOF
create database $PROJECT;
GRANT ALL ON ${PROJECT}.* TO '${USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';
flush privileges;
exit;
EOF

# Link in media dirs
cd $HOME_DIR/sites/$SITE/public
ln -s $DJANGO_DIR/contrib/admin/media admin_media
ln -s $HOME_DIR/sites/$SITE/$PROJECT/media .

# Setup Trac
mkdir -p $HOME_DIR/sites/$SITE/trac/env 
mkdir $HOME_DIR/sites/$SITE/trac/repos
trac-admin $HOME_DIR/sites/$SITE/trac/env initenv
svnadmin create $HOME_DIR/sites/$SITE/trac/repos/

# Hardening a bit
cd $HOME_DIR
find . -name \*.py -type f -exec chmod -w {} \;
find . -type d -exec chmod -w {} \;

# Copy .htaccess and dispatch.fcgi into public dir
cat >>$HOME_DIR/sites/$SITE/public/dispatch.fcgi <<EOF
#!/usr/bin/python
import sys, os

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/lib" % (USERNAME,SITE) )

# Switch to the directory of your project.
#os.chdir("/home/%s/dev/" % (USERNAME,) ) 

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false") 
EOF

# Add .htaccess
cat >>$HOME_DIR/sites/$SITE/public/.htaccess <<EOF
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]

# rewrite trac/ requests so they get served by trac.fcgi
RewriteRule ^(trac\.fcgi/.*)$ - [L]
RewriteRule ^trac/(.*)$ trac.fcgi/$1 [L]

# rewrite everything else to disptach.fcgi
RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]
EOF

# Make sure everything in user dir is owned by user
echo "Finalizing some permissions ..."

echo "chown -R $USER $HOME_DIR"
chown -R $USER $HOME_DIR

echo "chmod +w $HOME_DIR"
chmod +w $HOME_DIR

# Chown logs dir as root
echo "chown root $HOME_DIR/sites/$SITE/logs"
chown root $HOME_DIR/sites/$SITE/logs

# Configure the rest by hand ...
echo "DATABASE_USER='$DB_USER'" >> $LIB_DIR/$PROJECT/settings.py
echo "DATABASE_PASSWORD='$DB_PASS'" >> $LIB_DIR/$PROJECT/settings.py
echo "Configure $LIB_DIR/$PROJECT/settings.py"
echo "Configure Apache"

Revision: 5910
at April 11, 2008 12:07 by mandric


Initial Code
#!/bin/bash
# Create a django skeleton on my debian box.

if [ -z $1 ] ; then
echo 'Please specify a username.'
exit
fi

if [ -z $2 ] ; then
echo 'Please specify a domain name.'
exit

USER=$1
PROJECT=$USER
SITE=$2
USER_PASS=`makepasswd --chars=25`
DB_PASS=`makepasswd --chars=25`
HOME_DIR=/home/$USER
LIB_DIR=$HOME_DIR/sites/$SITE/lib
DJANGO_DIR=/usr/lib/python2.4/site-packages/django

echo "Creating $USER"
# add unix user, allowing numbers in the username.
adduser $USER --force-badname

mkdir $HOME_DIR/sites/$SITE -p
mkdir $LIB_DIR
mkdir $HOME_DIR/sites/$SITE/logs
mkdir $HOME_DIR/sites/$SITE/public
mkdir $HOME_DIR/sites/$SITE/static
mkdir $HOME_DIR/sites/$SITE/uploads
mkdir $HOME_DIR/bin

# Build django project
cd $LIB_DIR && \
$DJANGO_DIR/bin/django-admin.py startproject $PROJECT && \
cd $PROJECT && \
mkdir media && \
mkdir templates 

# Create database
mysql -u root -p  << EOF
create database $PROJECT;
GRANT ALL ON ${PROJECT}.* TO '${USER}'@'localhost' IDENTIFIED BY '${DB_PASS}';
flush privileges;
exit;
EOF

# Link in media dirs
cd $HOME_DIR/sites/$SITE/public
ln -s $DJANGO_DIR/contrib/admin/media admin_media
ln -s $HOME_DIR/sites/$SITE/$PROJECT/media .

# Setup Trac
mkdir -p $HOME_DIR/sites/$SITE/trac/env 
mkdir $HOME_DIR/sites/$SITE/trac/repos
trac-admin $HOME_DIR/sites/$SITE/trac/env initenv
svnadmin create $HOME_DIR/sites/$SITE/trac/repos/

# Hardening a bit
cd $HOME_DIR
find . -name \*.py -type f -exec chmod -w {} \;
find . -type d -exec chmod -w {} \;

# Copy .htaccess and dispatch.fcgi into public dir
cat >>$HOME_DIR/sites/$SITE/public/dispatch.fcgi <<EOF
#!/usr/bin/python
import sys, os

USERNAME = '$USER'
SITE = '$SITE'
PROJECT = '$USER'

# Add a custom Python path.
sys.path.insert(0, "/home/%s/sites/%s/lib" % (USERNAME,SITE) )

# Switch to the directory of your project.
#os.chdir("/home/%s/dev/" % (USERNAME,) ) 

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = "%s.settings"  % PROJECT

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="threaded", daemonize="false") 
EOF

# Add .htaccess
cat >>$HOME_DIR/sites/$SITE/public/.htaccess <<EOF
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(media/.*)$ - [L]
RewriteRule ^(admin_media/.*)$ - [L]

# rewrite trac/ requests so they get served by trac.fcgi
RewriteRule ^(trac\.fcgi/.*)$ - [L]
RewriteRule ^trac/(.*)$ trac.fcgi/$1 [L]

# rewrite everything else to disptach.fcgi
RewriteRule ^(dispatch\.fcgi/.*)$ - [L]
RewriteRule ^(.*)$ dispatch.fcgi/$1 [QSA,L]
EOF

# Make sure everything in user dir is owned by user
echo "Finalizing some permissions ..."

echo "chown -R $USER $HOME_DIR"
chown -R $USER $HOME_DIR

echo "chmod +w $HOME_DIR"
chmod +w $HOME_DIR

# Chown logs dir as root
echo "chown root $HOME_DIR/sites/$SITE/logs"
chown root $HOME_DIR/sites/$SITE/logs

# Configure django apps by hand ...
echo "Configure $LIB_DIR/$PROJECT/settings.py"

Initial URL

                                

Initial Description
This is meant to be run as root, so do it at your own risk.  It's very raw and needs error checking, but works.  I run this script to get new django sites off the ground in  a few minutes.

Initial Title
Bootstrap a Django setup

Initial Tags
Bash, script, unix, linux, django

Initial Language
Bash