-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcdb-www-create.perl
executable file
·379 lines (279 loc) · 10.2 KB
/
dcdb-www-create.perl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/perl -w
use lib qw(. lib blib/lib lib/lib lib/blib/lib);
use DiaColloDB::WWW;
use DiaColloDB::WWW::CGI;
use Getopt::Long qw(:config no_ignore_case);
use Cwd qw(abs_path);
use File::Basename qw(basename);
use File::ShareDir qw(:ALL);
use File::Copy qw(copy);
use File::Copy::Recursive qw(dircopy);
use File::chmod::Recursive;
use File::Path qw(make_path remove_tree);
use Pod::Usage;
use strict;
##----------------------------------------------------------------------
## Globals
##----------------------------------------------------------------------
##-- program vars
our $prog = basename($0);
our ($help,$version);
our %log = (level=>'TRACE', rootLevel=>'FATAL');
our $force = 0;
our $wwwdir = undef;
my %rcfiles = (
'dstar.rc' => dist_file("DiaColloDB-WWW",'rc/dstar.rc'),
'local.rc' => dist_file("DiaColloDB-WWW",'rc/local.rc'),
'dstar/corpus.ttk' => dist_file("DiaColloDB-WWW",'rc/corpus.ttk'),
'dstar/custom.ttk' => dist_file("DiaColloDB-WWW",'rc/custom.ttk'),
);
my $want_siterc = 1;
my %site = (
'alias' => undef,
);
##----------------------------------------------------------------------
## Command-line processing
##----------------------------------------------------------------------
GetOptions(##-- general
'help|h' => \$help,
'version|V' => \$version,
#'verbose|v=i' => \$verbose,
##-- generation
'dstar-rc|dstar|drc|rc|d=s' => \$rcfiles{'dstar.rc'},
'local-rc|local|lrc|l=s' => \$rcfiles{'local.rc'},
'corpus-ttk|corpus|c=s' => \$rcfiles{'dstar/corpus.ttk'},
'custom-ttk|custom|C=s' => \$rcfiles{'dstar/custom.ttk'},
'site-rc|siterc|site!' => \$want_siterc,
'site-alias|alias|a=s' => \$site{alias},
##-- logging
'log-level|level|ll=s' => sub { $log{level} = uc($_[1]); },
'log-option|logopt|lo=s' => \%log,
##-- I/O
'force|f!' => \$force,
'output|out|o=s' => \$wwwdir,
);
pod2usage({-exitval=>0,-verbose=>0}) if ($help);
if ($version) {
print STDERR "$prog version $DiaColloDB::WWW::VERSION by Bryan Jurish\n";
exit 0 if ($version);
}
pod2usage({-exitval=>0,-verbose=>0,-msg=>"no DBURL specified!"}) if (@ARGV < 1);
##----------------------------------------------------------------------
## MAIN
##----------------------------------------------------------------------
##-- setup logger
DiaColloDB::Logger->ensureLog(%log);
my $logger = 'DiaColloDB::WWW';
##-- command-line arguments
my $dburl = shift;
$dburl =~ s{/$}{};
$wwwdir //= "$dburl.www";
$wwwdir =~ s{/$}{};
$wwwdir =~ s/[^\w\.\-\+]/_/g if (!-d $dburl);
##-- get source directory via File::ShareDir
my $srcdir = File::ShareDir::dist_dir('DiaColloDB-WWW');
$srcdir =~ s{/$}{};
my $docdir = "$srcdir/htdocs";
-d $docdir
or $logger->logdie("no source directory '$docdir' found");
##-- ensure output directory exists
$logger->logdie("output directory '$wwwdir' exists, use --force option to overwrite") if (-e $wwwdir && !$force);
-d $wwwdir
or make_path($wwwdir)
or $logger->logdie("failed to create output directory '$wwwdir': $!");
##-- copy wrappers
$logger->info("copying wrappers from $docdir to $wwwdir");
{
no warnings 'once';
$File::Copy::Recursive::RmTrgFil = 2;
}
dircopy($docdir,$wwwdir)
or $logger->logdie("failed to copy $docdir to $wwwdir: $!");
##-- copy configuration files
$logger->info("copying configuration file(s)");
foreach (sort keys %rcfiles) {
!$rcfiles{$_}
or copy($rcfiles{$_},"$wwwdir/$_")
or $logger->logdie("failed to copy $rcfiles{$_} to $wwwdir/$_: $!");
}
##-- set permissions
$logger->info("setting permissions on $wwwdir");
chmod_recursive('u+w',$wwwdir)
or $logger->logdie("failed to update permissions on $wwwdir: $!");
if (-e $dburl) {
##-- dburl: file or dbdir: link to 'data'
my $dbdir_abs = abs_path($dburl);
$logger->info("linking $wwwdir/data to $dbdir_abs");
!-e "$wwwdir/data"
or unlink("$wwwdir/data")
or $logger->logdie("failed to unlink stale $wwwdir/data: $!");
symlink($dbdir_abs,"$wwwdir/data")
or $logger->logdie("failed to create symlink $wwwdir/data -> $dbdir_abs: $!");
} else {
##-- other url: create rcfile
$logger->info("creating config file $wwwdir/data for URL '$dburl'");
!-e "$wwwdir/data"
or unlink("$wwwdir/data")
or $logger->logdie("failed to unlink stale $wwwdir/data: $!");
DiaColloDB::Utils::saveJsonFile({url=>$dburl},"$wwwdir/data")
or $logger->logdie("failed to create config file $wwwdir/data: $!");
}
##-- create site.rc
if ($want_siterc) {
$logger->info("creating $wwwdir/site.rc");
$site{alias} //= "/".basename($wwwdir);
$site{wwwdir} = abs_path($wwwdir)."/";
my $dbcgi = DiaColloDB::WWW::CGI->new;
my $data = $dbcgi->ttk_process(dist_file("DiaColloDB-WWW", "rc/siterc.ttk"), {site=>\%site,prog=>$prog,version=>$DiaColloDB::WWW::VERSION});
CORE::open(my $fh, ">:raw", "$wwwdir/site.rc")
or $logger->logdie("$0: open failed for $wwwdir/site.rc: $!");
$fh->print($data);
CORE::close($fh);
print STDERR "$prog: $_\n"
foreach ("==================================================",
"created apache configuration file $wwwdir/site.rc",
"",
"remember to add $wwwdir/site.rc to your apache",
"site configuration and re-load the server config!",
"=================================================="
);
} else {
$logger->info("NOT creating apache site configuration $wwwdir/site.rc (disabled by user request)");
}
__END__
###############################################################
## pods
###############################################################
=pod
=head1 NAME
dcdb-www-create.perl - instantiate apache www wrappers for a DiaColloDB index
=head1 SYNOPSIS
dcdb-www-create.perl [OPTIONS] DBURL
General Options:
-help # this help message
-version # display program version and exit
Customization Options:
-dstar-rc RCFILE # instantiates WWWDIR/dstar.rc (default:none)
-local-rc RCFILE # instantiates WWWDIR/local.rc (default:none)
-corpus-ttk TTKFILE # instantiates WWWDIR/dstar/corpus.ttk (default:none)
-custom-ttk TTKFILE # instantiates WWWDIR/dstar/custom.ttk (default:none)
-[no]site-rc # do/don't create apache configuration in WWWDIR/site.rc (default:do)
-site-alias ALIAS # server path alias for WWWDIR/site.rc (default=/WWWDIR)
Output Options:
-[no]force # do/don't force-overwrite existing WWWDIR (default=don't)
-output WWWDIR # create wrapper directory WWWDIR (default=DBURL.www)
Caveats:
+ you will need to update and reload your apache server configuration after
adding or changing any site-wide aliases!
=cut
###############################################################
## DESCRIPTION
###############################################################
=pod
=head1 DESCRIPTION
dcdb-www-create.perl
instantiates a CGI wrapper directory for the
L<DiaColloDB|DiaColloDB> index specified by the
L<DBURL|/DBURL> argument in the output directory
F<WWWDIR> specified by the L<-output|/-output WWWDIR>
option. The directory created can be customized
by editing the configuration files and then served
by the http daemon of your choice (e.g. apache),
or used as a template for the standalone server
L<dcdb-www-server.perl(1)|dcdb-www-server.perl>.
In the latter case, note that you don't I<need> to
create a wrapper directory to use the standalone server
unless you want to override the default templates
included in the L<DiaColloDB::WWW|DiaColloDB::WWW> distribution.
=cut
###############################################################
## OPTIONS AND ARGUMENTS
###############################################################
=pod
=head1 OPTIONS AND ARGUMENTS
=cut
###############################################################
# Arguments
###############################################################
=pod
=head2 Arguments
=over 4
=item DBURL
L<DiaColloDB|DiaColloDB> database URL to be wrapped,
which must be supported by L<DiaColloDB::Client|DiaColloDB::Client>,
i.e. must use one of the supported schemes C<file://>, C<rcfile://>, C<http://>, and C<list://>.
If no scheme is specified, C<file://> is assumed.
Typically, I<DBURL> is simply the path to a localL<DiaColloDB|DiaColloDB> index directory
as created by
L<dcdb-create.perl(1)|dcdb-create.perl>.
=back
=cut
###############################################################
# General Options
###############################################################
=pod
=head2 General Options
=over 4
=item -help
Display a brief help message and exit.
=item -version
Display version information and exit.
=back
=cut
###############################################################
# Customization Options
###############################################################
=pod
=head2 Customization Options
=over 4
=item -dstar-rc RCFILE
Install a user-specified C<RCFILE> as F<WWWDIR/dstar.rc>
(perl format, base configuration).
=item -local-rc RCFILE
Install a user-specified C<RCFILE> as F<WWWDIR/local.rc>
(perl format, overrides).
=item -corpus-ttk TTKFILE
Install a user-specified C<TTKFILE> as F<WWWDIR/dstar/corpus.ttk>
(L<Template Toolkit|Template> format, base configuration).
=item -custom-ttk TTKFILE
Install a user-specified C<TTKFILE> as F<WWWDIR/dstar/custom.ttk>
(L<Template Toolkit|Template> format, overrides).
=item -site-rc , -nosite-rc
Do/don't create an apache site configuration stub in F<WWWDIR/site.rc>.
Default=do.
=item -site-alias ALIAS
Server path alias for F<WWWDIR/site.rc>,
Default=F</WWWDIR> (basename only).
=back
=cut
###############################################################
# I/O Options
###############################################################
=pod
=head2 I/O Options
=over 4
=item -[no]force
Do/don't force-overwrite an existing output directory (default=don't).
=item -output WWWDIR
Specify wrapper output directory F<WWWDIR>.
Default=F<DBURL.www>.
=back
=cut
###############################################################
# Bugs and Limitations
###############################################################
=pod
=head1 BUGS AND LIMITATIONS
Probably many.
=cut
###############################################################
# Footer
###############################################################
=pod
=head1 ACKNOWLEDGEMENTS
Perl by Larry Wall.
=head1 AUTHOR
Bryan Jurish E<lt>[email protected]<gt>
=head1 SEE ALSO
perl(1).
=cut