/ Published in: Bash
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/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 'Trac detected an internal error:' print e 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