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 | system(qw(sed -ri),'s/^sql(alchemy|object)\.dburi(.*)$/#sql\1.dburi\2\nsqlalchemy.dburi="mysql:\/\/' . uri_escape($sqluser) . ":" . uri_escape($sqlpass) . "\@$sqlhost\\/$sqldb\"/","$name/dev.cfg", "$name/sample-prod.cfg") == 0 or die "sed db failed!"; |
---|
136 | system(qw(sed -ri),'s/^#? *autoreload\.on.*$/autoreload.on = False # breaks the scripts flup setup/',"$name/dev.cfg") == 0 or die "sed autoreload failed!"; |
---|
137 | my $addrendescsl = $addrend; |
---|
138 | $addrendescsl =~ s|/|\\/|g; |
---|
139 | # Obviated by a TurboGears upgrade |
---|
140 | #system(qw(sed -ri),'s/^(\[global\] *)$/\1\nserver.webpath = "\/'."$addrendescsl".'"/',"$name/dev.cfg") == 0 or die "sed webpath failed!"; |
---|
141 | if ($orm eq "elixir" or $orm eq "sqlalchemy") { |
---|
142 | 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!"; |
---|
143 | } |
---|
144 | |
---|
145 | # Make logdir |
---|
146 | system('mkdir','-p',"$name/log"); |
---|
147 | |
---|
148 | # Cert patch |
---|
149 | if ($certpatch) { |
---|
150 | # comment out the password = line in model |
---|
151 | system(qw(sed -ri), |
---|
152 | 's/^(.*password.*)$/#\1 -- we use certs, not passwords/', |
---|
153 | "$name/$pack/model.py") == 0 or die "sed model for certs failed!"; |
---|
154 | |
---|
155 | # Stick cert.py in |
---|
156 | system('cp',"/mit/scripts/deploy$scriptsdev/turbogears-certs/certs.py", |
---|
157 | "$name/$pack/") == 0 or die "cp certs.py failed!"; |
---|
158 | |
---|
159 | # Add the certness to controllers.py |
---|
160 | system(qw(sed -ri), |
---|
161 | 's/^(from cherrypy.*)$/\1\nfrom '."$pack".'.certs import with_mit_certs/', |
---|
162 | "$name/$pack/controllers.py") == 0 or die "sed controllers import for certs failed!"; |
---|
163 | system(qw(sed -ri), |
---|
164 | 's/^(\s+)(def login.*)$/\1@with_mit_certs\n\1\2', |
---|
165 | "$name/$pack/model.py") == 0 or die "sed model for certs failed!"; |
---|
166 | #-! replace login body |
---|
167 | #-! replace logout body |
---|
168 | #-! replace login.kid |
---|
169 | } |
---|
170 | |
---|
171 | exit 0; |
---|