| 1 | #!/usr/bin/perl | 
|---|
| 2 | use strict; | 
|---|
| 3 | use FindBin qw($Bin); | 
|---|
| 4 | use lib $Bin; | 
|---|
| 5 | use onserver; | 
|---|
| 6 | use Cwd; | 
|---|
| 7 | use File::Path; | 
|---|
| 8 | use URI::Escape; | 
|---|
| 9 | use DBI; | 
|---|
| 10 | use Config::IniFiles; | 
|---|
| 11 | use FileHandle; | 
|---|
| 12 |  | 
|---|
| 13 | setup(); | 
|---|
| 14 |  | 
|---|
| 15 | print "\nEnter the name of your project (the title of this TurboGears instance).\n"; | 
|---|
| 16 | my $name; | 
|---|
| 17 | while (1) { | 
|---|
| 18 |     print "Project name: "; | 
|---|
| 19 |     $name=<STDIN>; | 
|---|
| 20 |     chomp($name); | 
|---|
| 21 |     if ($name =~ /^[a-zA-Z][a-zA-Z0-9_ -]+$/) { | 
|---|
| 22 |         last; | 
|---|
| 23 |     } | 
|---|
| 24 |     print "Invalid project name; it should start with a letter and not contain\npunctuation other than dashes or underscores.\n"; | 
|---|
| 25 | }        | 
|---|
| 26 |  | 
|---|
| 27 | # quickstart turns spaces or underscores into dashes... | 
|---|
| 28 | $name =~ s/[ _-]+/-/g; | 
|---|
| 29 |  | 
|---|
| 30 | my $defpack=lc($name); | 
|---|
| 31 | $defpack =~ s/[ -]/_/g; | 
|---|
| 32 | $defpack =~ s/[^a-z0-9_]//g; | 
|---|
| 33 | if (! ($defpack =~ /^[a-zA-Z]/)) { | 
|---|
| 34 |     $defpack = "p$defpack"; | 
|---|
| 35 | } | 
|---|
| 36 | print "\nEnter the name for your project's python package.\n"; | 
|---|
| 37 | my $pack; | 
|---|
| 38 | while (1) { | 
|---|
| 39 |     print "Package name [${defpack}]: "; | 
|---|
| 40 |     $pack=<STDIN>; | 
|---|
| 41 |     chomp($pack); | 
|---|
| 42 |     if (!($pack)) { | 
|---|
| 43 |         $pack=$defpack; | 
|---|
| 44 |         last; | 
|---|
| 45 |     } elsif ($pack =~ /^[a-zA-Z][a-zA-Z0-9_]+$/) { | 
|---|
| 46 |         last; | 
|---|
| 47 |     } | 
|---|
| 48 |     print "Invalid package name; it should start with a letter and contain only letters,\nnumbers, and underscores.\n"; | 
|---|
| 49 | } | 
|---|
| 50 |  | 
|---|
| 51 | print "\nWhat ORM (Object-Relational Mapper) do you want to use with this TurboGears\ninstance?  Select from the following list:\n"; | 
|---|
| 52 | print "1. SQLAlchemy Elixir\n"; | 
|---|
| 53 | print "2. SQLAlchemy\n"; | 
|---|
| 54 | print "3. SQLObject\n"; | 
|---|
| 55 | my $orm; | 
|---|
| 56 | while (1) { | 
|---|
| 57 |     print "ORM [1]: "; | 
|---|
| 58 |     my $ormnum=<STDIN>; | 
|---|
| 59 |     chomp($ormnum); | 
|---|
| 60 |     if ((!$ormnum) || $ormnum == 1) { | 
|---|
| 61 |         $orm = "elixir"; | 
|---|
| 62 |         last; | 
|---|
| 63 |     } elsif ($ormnum == 2) { | 
|---|
| 64 |         $orm = "sqlalchemy"; | 
|---|
| 65 |         last; | 
|---|
| 66 |     } elsif ($ormnum == 3) { | 
|---|
| 67 |         $orm = "sqlobject"; | 
|---|
| 68 |         last; | 
|---|
| 69 |     } | 
|---|
| 70 |     print "Please choose 1, 2, or 3.\n"; | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 | print "\nWhat template do you want to use with this TurboGears instance?  Select from\nthe following list:\n"; | 
|---|
| 74 | print "1. turbogears: normal template, recommended for most projects\n"; | 
|---|
| 75 | print "2. tgbig:  a more complex directory structure for big projects\n"; | 
|---|
| 76 | my $templ; | 
|---|
| 77 | while (1) { | 
|---|
| 78 |     print "Template [1]: "; | 
|---|
| 79 |     my $templnum=<STDIN>; | 
|---|
| 80 |     chomp($templnum); | 
|---|
| 81 |     if ((!$templnum) || $templnum == 1) { | 
|---|
| 82 |         $templ = "turbogears"; | 
|---|
| 83 |         last; | 
|---|
| 84 |     } elsif ($templnum == 2) { | 
|---|
| 85 |         $templ = "tgbig"; | 
|---|
| 86 |         last; | 
|---|
| 87 |     } | 
|---|
| 88 |     print "Please choose 1, 2, or 3.\n"; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | print "\nDo you want to use Identity (usernames/passwords) in this project?\n(These would be separate from Athena usernames/passwords.)\n"; | 
|---|
| 92 | print "1. no identity: no logins, everyone sees the same pages\n"; | 
|---|
| 93 | print "2. standard identity: users log in with site-specific usernames and passwords\n"; | 
|---|
| 94 | #print "3. certificates: users are identified by their MIT certificates\n"; | 
|---|
| 95 | my $ident; | 
|---|
| 96 | my $certpatch=0; | 
|---|
| 97 | while (1) { | 
|---|
| 98 |     print "Identity [1]: "; | 
|---|
| 99 |     my $identnum=<STDIN>; | 
|---|
| 100 |     chomp($identnum); | 
|---|
| 101 |     if ((!$identnum) || $identnum == 1) { | 
|---|
| 102 |         $ident = "no"; | 
|---|
| 103 |         last; | 
|---|
| 104 |     } elsif ($identnum == 2) { | 
|---|
| 105 |         $ident = "yes"; | 
|---|
| 106 |         last; | 
|---|
| 107 |     } elsif ($identnum == 3) { | 
|---|
| 108 |         $ident = "yes"; | 
|---|
| 109 |         $certpatch = 1; | 
|---|
| 110 |         last; | 
|---|
| 111 |     } | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | open (FLUPCONF, ">flupconfig.py"); | 
|---|
| 115 | print FLUPCONF <<EOF; | 
|---|
| 116 | code_dir = "/mit/$USER/Scripts/turbogears/$name/" | 
|---|
| 117 | project_name = "$name" | 
|---|
| 118 | package_name = "$pack" | 
|---|
| 119 | EOF | 
|---|
| 120 | close (FLUPCONF); | 
|---|
| 121 |  | 
|---|
| 122 | system("ln","-s","/mit/$USER/Scripts/turbogears/$name/$pack","./$pack"); | 
|---|
| 123 | system("ln","-s","/mit/$USER/Scripts/turbogears/$name/$pack/static", | 
|---|
| 124 |     "./static"); | 
|---|
| 125 |  | 
|---|
| 126 | chdir("/mit/$USER/Scripts/turbogears/"); | 
|---|
| 127 | print "\nRunning tg-admin quickstart...\n"; | 
|---|
| 128 | open(QS, "|/usr/bin/tg-admin quickstart $name --package=$pack --$orm --templates=$templ") | 
|---|
| 129 |     or die("tg-admin quickstart failed open!"); | 
|---|
| 130 | QS->autoflush(1); | 
|---|
| 131 | print QS "$ident\n" or die("tg-admin quickstart failed specify ident!"); | 
|---|
| 132 | close(QS) or die("tg-admin quickstart failed close!"); | 
|---|
| 133 |  | 
|---|
| 134 | # Put in the sqldb | 
|---|
| 135 | if ($orm eq "sqlobject") { | 
|---|
| 136 |     my $uriuser = uri_escape($sqluser); | 
|---|
| 137 |     my $uripass = uri_escape($sqlpass); | 
|---|
| 138 |     foreach my $fil (("$name/dev.cfg", "$name/sample-prod.cfg")) | 
|---|
| 139 |     { | 
|---|
| 140 |         open my $in, '<', $fil or die "Can't read old file: $!"; | 
|---|
| 141 |         open my $out, '>', "$fil.new" or die "Can't write new file: $!"; | 
|---|
| 142 |  | 
|---|
| 143 |         while (<$in>) { | 
|---|
| 144 |             s/^sqlobject\.dburi(.*)$/#sqlobject.dburi\2\nsqlobject.dburi="mysql:\/\/$uriuser:$uripass\@$sqlhost\/$sqldb"/; | 
|---|
| 145 |             print $out $_; | 
|---|
| 146 |         } | 
|---|
| 147 |         close $out; | 
|---|
| 148 |         rename "$fil.new", $fil | 
|---|
| 149 |             or die "Cannot rename: $!"; | 
|---|
| 150 |     } | 
|---|
| 151 | } else { | 
|---|
| 152 |     system(qw(sed -ri),"s&^sql(alchemy|object)\.dburi(.*)\$&#sql\\1.dburi\\2\\nsql\\1.dburi=\"mysql://$sqlhost/$sqldb?read_default_file=~/.my.cnf\"&","$name/dev.cfg", "$name/sample-prod.cfg") == 0 or die "sed db failed!"; | 
|---|
| 153 | } | 
|---|
| 154 | system(qw(sed -ri),'s/^#? *autoreload\.on.*$/autoreload.on = False # breaks the scripts flup setup/',"$name/dev.cfg") == 0 or die "sed autoreload failed!"; | 
|---|
| 155 | my $addrendescsl = $addrend; | 
|---|
| 156 | $addrendescsl =~ s|/|\\/|g; | 
|---|
| 157 | # Obviated by a TurboGears upgrade | 
|---|
| 158 | #system(qw(sed -ri),'s/^(\[global\] *)$/\1\nserver.webpath = "\/'."$addrendescsl".'"/',"$name/dev.cfg") == 0 or die "sed webpath failed!"; | 
|---|
| 159 | if ($orm eq "elixir" or $orm eq "sqlalchemy") { | 
|---|
| 160 |     system(qw(sed -ri),'s/^(\[global\] *)$/\1\nsqlalchemy.pool_recycle = 30 # Need a short timeout for sql.mit.edu/',"$name/$pack/config/app.cfg") == 0 or die "sed pool_recycle failed!"; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | # Make logdir | 
|---|
| 164 | system('mkdir','-p',"$name/log"); | 
|---|
| 165 |  | 
|---|
| 166 | # Cert patch | 
|---|
| 167 | if ($certpatch) { | 
|---|
| 168 |     # comment out the password = line in model | 
|---|
| 169 |     system(qw(sed -ri), | 
|---|
| 170 |            's/^(.*password.*)$/#\1 -- we use certs, not passwords/', | 
|---|
| 171 |            "$name/$pack/model.py") == 0 or die "sed model for certs failed!"; | 
|---|
| 172 |      | 
|---|
| 173 |     # Stick cert.py in | 
|---|
| 174 |     system('cp',"/mit/scripts/deploy$scriptsdev/turbogears-certs/certs.py", | 
|---|
| 175 |            "$name/$pack/") == 0 or die "cp certs.py failed!"; | 
|---|
| 176 |      | 
|---|
| 177 |     # Add the certness to controllers.py | 
|---|
| 178 |     system(qw(sed -ri), | 
|---|
| 179 |            's/^(from cherrypy.*)$/\1\nfrom '."$pack".'.certs import with_mit_certs/', | 
|---|
| 180 |            "$name/$pack/controllers.py") == 0 or die "sed controllers import for certs failed!"; | 
|---|
| 181 |     system(qw(sed -ri), | 
|---|
| 182 |            's/^(\s+)(def login.*)$/\1@with_mit_certs\n\1\2', | 
|---|
| 183 |            "$name/$pack/model.py") == 0 or die "sed model for certs failed!"; | 
|---|
| 184 |     #-! replace login body | 
|---|
| 185 |     #-! replace logout body | 
|---|
| 186 |     #-! replace login.kid | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | exit 0; | 
|---|