source: branches/locker-dev/locker/deploy/bin/django

Last change on this file was 2443, checked in by lfaraone, 9 years ago
Additional updates for Django 1.4/1.5: static files, admin interface, README Here we provide proper support for static files on Scripts, see <https://docs.djangoproject.com/en/1.4/howto/static-files/> This means we designate a path for static files (~/web_scripts/$name/static) and refer to the static files by their full username.scripts.mit.edu url over HTTPS. This should work even if the user deploys the app on a subdomain or a non-MIT hostname. At the same time, we fix the urls.py rule to enable the admin interface, which broke with the new URL pattern syntax a while ago. Finally, we run collectstatic to ensure that the relevant files are copied over. In addition, a README was added to the web_scripts directory to help avoid confusion over "where're my files?".
  • Property svn:executable set to *
File size: 4.7 KB
Line 
1#!/usr/bin/perl
2use strict;
3use FindBin qw($Bin);
4use lib $Bin;
5use onserver;
6
7setup();
8
9print "\nEnter the code name for your project (a valid Python package name).\n";
10print "Do not use 'django' or the name of any other Python library.\n";
11print "Project name: ";
12my $name = <STDIN>;
13chomp $name;
14
15open FASTCGI, ">index.fcgi";
16print FASTCGI <<EOF;
17#!/usr/bin/env python
18import sys, os, time, threading, django.utils.autoreload
19sys.path.insert(0, "/mit/$USER/Scripts/django/$name")
20os.chdir("/mit/$USER/Scripts/django/$name")
21os.environ['DJANGO_SETTINGS_MODULE'] = "$name.settings"
22
23def reloader_thread():
24  while True:
25    if django.utils.autoreload.code_changed():
26      os._exit(3)
27    time.sleep(1)
28t = threading.Thread(target=reloader_thread)
29t.daemon = True
30t.start()
31
32from django.core.servers.fastcgi import runfastcgi
33runfastcgi(method="threaded", daemonize="false")
34EOF
35close FASTCGI;
36chmod 0755, "index.fcgi";
37
38open README, ">README.txt";
39print README <<EOF;
40This directory contains index.fcgi, a script that serves up your Django site.
41
42To modify your Django project access the files in
43        /mit/$user/Scripts/django/$name
44
45Files placed in this directory will be served directly to users without
46being processed by Django.
47
48Static files live in the "static" subdirectory; you should not add things
49there directly but instead place them with the relevant application as you
50normally would, then run "python manage.py collectstatic" from the above
51directory; see <https://docs.djangoproject.com/en/1.5/howto/static-files/>.
52
53  -- Scripts Team 2013-06-28
54EOF
55close README;
56chmod 0555, "README.txt";
57
58open HTACCESS, ">.htaccess";
59print HTACCESS <<EOF;
60RewriteEngine On
61
62RewriteRule ^\$ index.fcgi/ [QSA,L]
63
64RewriteCond %{REQUEST_FILENAME} !-f
65RewriteCond %{REQUEST_FILENAME} !-d
66RewriteRule ^(.*)\$ index.fcgi/\$1 [QSA,L]
67EOF
68close HTACCESS;
69chmod 0777, ".htaccess";
70
71chdir "/mit/$USER/Scripts/django/";
72system(qw{django-admin startproject}, $name)==0 or die "\nFailed to create app.\n\n";
73chdir "$name/$name";
74
75open SETTINGS, "settings.py";
76open NEWSETTINGS, ">settings.py.new";
77while (<SETTINGS>) {
78  chomp;
79  if (/Your Name/) {
80    $_ = "    ('$USER', '$email'),";
81  } elsif (/^DEBUG = /) {
82      $_ =~ s/DEBUG/import os\n\nDEBUG/;
83  } elsif (/'ENGINE'/) {
84    $_ = "        'ENGINE': 'django.db.backends.mysql',";
85  } elsif  (/'NAME'/) {
86    $_ = "        'NAME': '$sqldb',";
87  } elsif (/'USER'/) {
88    $_ = "        'OPTIONS': {\n            'read_default_file' : os.path.expanduser('~/.my.cnf'),\n        },";
89  } elsif (/'PASSWORD'/) {
90      next;
91  } elsif (/'HOST'/) {
92      next;
93  } elsif (/Chicago/) {
94    $_ =~ s/Chicago/New_York/;
95  } elsif (/^STATIC_URL/) {
96    $_ = "STATIC_URL = '//$USER.$server/$name/static/'";
97  } elsif (/^STATIC_ROOT/) {
98    $_ = "STATIC_ROOT = '/mit/$USER/web_scripts/$addrend/static/'";
99  } elsif (/^INSTALLED_APPS/) {
100    print NEWSETTINGS "$_\n";
101    while (<SETTINGS>) {
102      if (/^\)/) {
103        print NEWSETTINGS "    'django.contrib.admin',\n";
104        print NEWSETTINGS "    'django.contrib.admindocs',\n";
105      }
106      print NEWSETTINGS $_;
107    }
108  }
109  print NEWSETTINGS "$_\n";
110}
111close NEWSETTINGS;
112close SETTNGS;
113rename "settings.py.new", "settings.py";
114
115open URLS, "urls.py";
116open NEWURLS, ">urls.py.new";
117while (<URLS>) {
118  chomp;
119  if (/^#.*from django\.contrib import admin/) {
120    $_ =~ s/^# *//;
121  } elsif (/^#.*admin.autodiscover/) {
122    $_ =~ s/^# *//;
123  } elsif (/^ *# url\(r\'\^admin\//) {
124    $_ =~ s/# *//;
125  }
126  print NEWURLS "$_\n";
127}
128close NEWURLS;
129close URLS;
130rename "urls.py.new", "urls.py";
131
132chdir "..";
133
134system(qw{python manage.py collectstatic --noinput}) == 0 or die "\nFailed to collect static files.\n\n";
135
136print "Initializing your project's SQL database schema...\n";
137system qw{./manage.py syncdb --noinput};
138print "...done\n";
139
140print "Creating your superuser account... ";
141system qw{./manage.py createsuperuser --username}, $admin_username, "--email", $email, "--noinput";
142print "done\n";
143print "Setting your superuser password... ";
144system qw{mysql -D}, "$USER+$addrlast", "-e", "UPDATE auth_user SET password=MD5(\'$admin_password\') WHERE username=\'$admin_username\'";
145print "done\n";
146
147print "\nDjango has been installed. The setup is roughly what's described\n";
148print "in the shared-hosting section of\n";
149print "  http://docs.djangoproject.com/en/dev/howto/deployment/fastcgi/\n";
150print "We've also enabled the admin app. You can start from the 'Creating\n";
151print "models' step of the Django tutorial:\n";
152print "  http://docs.djangoproject.com/en/dev/intro/tutorial01/#id3\n\n";
153print "Your project is located in:\n";
154print "  /mit/$USER/Scripts/django/$name/\n";
155print "To access manage.py, run 'ssh -k $USER\@scripts' and cd to the above directory.\n\n";
156press_enter;
157
158exit 0;
Note: See TracBrowser for help on using the repository browser.