1 |
#!/usr/bin/perl -w |
2 |
|
3 |
use strict; |
4 |
use blib; |
5 |
use Fuse::DBI; |
6 |
use lib '/data/WebGUI/lib/'; |
7 |
use Data::Config; |
8 |
|
9 |
=head1 NAME |
10 |
|
11 |
webgui.pl - mount WebGUI templates as filesystem |
12 |
|
13 |
=head1 SYNOPSIS |
14 |
|
15 |
webgui.pl /data/WebGUI/etc/webgui.conf /mnt |
16 |
|
17 |
=head1 DESCRIPTION |
18 |
|
19 |
With this script, you can utilize C<Fuse> and C<Fuse::DBI> modules to mount |
20 |
templates from WebGUI and edit them using command-line utilities (like C<vi> |
21 |
or C<ftp>). |
22 |
|
23 |
It will present templates in WebGUI as directory tree consisting of |
24 |
directories (which represent template's namespace) and files (which are |
25 |
templates from database). If template name has slash (C</>) in name, deeper |
26 |
directories will be created. |
27 |
|
28 |
Template files will have correct lengths and write permissions which are |
29 |
specified in WebGUI database. |
30 |
|
31 |
=head2 Fuse module |
32 |
|
33 |
C<Fuse::DBI> module (which is core of this utility) uses C<Fuse> perl |
34 |
bindings. Perl bindings are rather new addition to C<Fuse>, so |
35 |
you will need recent CVS version of C<Fuse>. Current stable version doesn't |
36 |
include perl binding, so you will probably have to compile C<Fuse> yourself |
37 |
(see FUSE documentation for details about compilation and installation). |
38 |
|
39 |
After compilation and installation of C<fuse> kernel module and C<Fuse> perl |
40 |
bindings for it, you will have to load C<fuse> module into kernel. For that, |
41 |
you will have to be root. If you are not administrator on particular |
42 |
machine, ask your admin to install and load C<fuse> module for you. |
43 |
|
44 |
If you used C<fusermount> command before running this script, module will be |
45 |
already loaded. |
46 |
|
47 |
=head2 unsupported operations |
48 |
|
49 |
There is no support for creation of new templates, renaming, or deleting. |
50 |
Although those operations map nicely to file system semantics there are still |
51 |
possible only using WebGUI web interface. |
52 |
|
53 |
=head2 unlink to invalidate cache |
54 |
|
55 |
Unlink command (C<rm>) is implemented on files with special function: it |
56 |
will remove in-memory cache of particular template and reload it from |
57 |
database. That enables usage of web interface to make small changes and then |
58 |
continuing editing using this script without restarting it. |
59 |
|
60 |
In-memory cache is populated with data about available templates when you |
61 |
start this script. Currently only way to refresh template list (after you |
62 |
create copy of template through web interface) is to remove directory using |
63 |
C<rmdir> or C<rm -rf>. |
64 |
|
65 |
B<Don't panic!> Destructive operations in filesystem (C<rm> and C<rmdir>) |
66 |
just invalidate in-memory cache and re-read data from database (this will |
67 |
also change ctime of file, so your editor will probably notice that file has |
68 |
changed). |
69 |
|
70 |
In-memory cache is used to speed up operations like grep on templates. If it |
71 |
wasn't there, grep wouldn't be useful at all. I think this is acceptable |
72 |
compromise. |
73 |
|
74 |
=head2 invalidating of on-disk templates |
75 |
|
76 |
Every write operation will erase all templates on disk (so that next reload |
77 |
on browser will show your changes). It would be better if just changed |
78 |
template is erased, but this works well enough. You might notice performance |
79 |
penalty of this simplification if you are running very loaded production |
80 |
site. |
81 |
|
82 |
You have to have write permission on C<uploads/temp/templates/> directory |
83 |
for your WebGUI instance for this to work. If you don't C<Fuse::DBI> will |
84 |
complain. |
85 |
|
86 |
=head2 supported databases |
87 |
|
88 |
This script have embedded SQL queries for MySQL and PostgreSQL. Other databases |
89 |
could be supported easily. Contributions are welcomed. |
90 |
|
91 |
=head2 database transactions |
92 |
|
93 |
C<Fuse::DBI> uses transactions (if your database supports them) to prevent |
94 |
accidental corruption of data by reading old version. Depending on type of |
95 |
database back-end, MySQL users might be out of luck. |
96 |
|
97 |
=head2 recovering from errors |
98 |
|
99 |
B<Transport endpoint is not connected> is very often error when Fuse perl |
100 |
bindings exit without clean umount (through C<Fuse::DBI> C<umount> method or |
101 |
with C<fusermount -u /mnt> command). |
102 |
|
103 |
This script will automatically run C<fusermount -u /mnt> if it receives |
104 |
above error on startup. If it fails, mount point is still in use (that |
105 |
happens if you changed directory to mount point in other shell). Solution is |
106 |
simple, just change directory in other back to C<$HOME> (with just C<cd>) |
107 |
and re-run this script. |
108 |
|
109 |
=head2 missing Data::Config |
110 |
|
111 |
If this script complains about missing C<Data::Config> module, you will have |
112 |
to point path at top which points to lib directory of WebGUI installation. |
113 |
By default it points to C</data/WebGUI>: |
114 |
|
115 |
use lib '/data/WebGUI/lib/'; |
116 |
|
117 |
=cut |
118 |
|
119 |
my ($config_file,$mount) = @ARGV; |
120 |
|
121 |
unless ($config_file && $mount) { |
122 |
print STDERR <<_USAGE_; |
123 |
usage: $0 /data/WebGUI/etc/webgui.conf /mnt |
124 |
|
125 |
For more information see perldoc webgui.pl |
126 |
_USAGE_ |
127 |
exit 1; |
128 |
} |
129 |
|
130 |
system "fusermount -u $mount" unless (-w $mount); |
131 |
|
132 |
unless (-w $mount) { |
133 |
print STDERR "Current user doesn't have permission on mount point $mount: $!"; |
134 |
exit 1; |
135 |
} |
136 |
|
137 |
my $config = new Data::Config $config_file || "can't open config $config_file: $!"; |
138 |
|
139 |
my $sql = { |
140 |
'pg' => { |
141 |
'filenames' => q{ |
142 |
select |
143 |
oid as id, |
144 |
namespace||'/'||name||' ['||oid||']' as filename, |
145 |
length(template) as size, |
146 |
iseditable as writable |
147 |
from template ; |
148 |
}, |
149 |
'read' => q{ |
150 |
select template |
151 |
from template |
152 |
where oid = ?; |
153 |
}, |
154 |
'update' => q{ |
155 |
update template |
156 |
set template = ? |
157 |
where oid = ?; |
158 |
}, |
159 |
}, |
160 |
'mysql' => { |
161 |
'filenames' => q{ |
162 |
select |
163 |
concat(templateid,name) as id, |
164 |
concat(namespace,'/',name,'.html') as filename, |
165 |
length(template) as size, |
166 |
iseditable as writable |
167 |
from template ; |
168 |
}, |
169 |
'read' => q{ |
170 |
select template |
171 |
from template |
172 |
where concat(templateid,name) = ?; |
173 |
}, |
174 |
'update' => q{ |
175 |
update template |
176 |
set template = ? |
177 |
where concat(templateid,name) = ?; |
178 |
}, |
179 |
}, |
180 |
}; |
181 |
|
182 |
my $dsn = $config->param('dsn'); |
183 |
my $db; |
184 |
|
185 |
if ($dsn =~ m/DBI:(mysql|pg):/i) { |
186 |
$db = lc($1); |
187 |
} else { |
188 |
print STDERR "can't find supported database (mysql/pg) in dsn: $dsn\n"; |
189 |
exit 1; |
190 |
} |
191 |
|
192 |
my $template_dir = $config->param('uploadsPath') . '/temp/templates/'; |
193 |
|
194 |
print "using database '$db', template dir '$template_dir' and mountpoint $mount\n"; |
195 |
|
196 |
my $mnt = Fuse::DBI->mount({ |
197 |
filenames => $sql->{$db}->{'filenames'}, |
198 |
read => $sql->{$db}->{'read'}, |
199 |
update => $sql->{$db}->{'update'}, |
200 |
dsn => $config->param('dsn'), |
201 |
user => $config->param('dbuser'), |
202 |
password => $config->param('dbpass'), |
203 |
mount => $mount, |
204 |
fork => 1, |
205 |
invalidate => sub { |
206 |
print STDERR "invalidating content in $template_dir\n"; |
207 |
opendir(DIR, $template_dir) || die "can't opendir $template_dir: $!"; |
208 |
map { unlink "$template_dir/$_" || warn "can't remove $template_dir/$_: $!" } grep { !/^\./ && -f "$template_dir/$_" } readdir(DIR); |
209 |
closedir DIR; |
210 |
}, |
211 |
}); |
212 |
|
213 |
if (! $mnt) { |
214 |
print STDERR "can't mount filesystem!"; |
215 |
exit 1; |
216 |
} |
217 |
|
218 |
print "Press enter to exit..."; |
219 |
my $foo = <STDIN>; |
220 |
|
221 |
$mnt->umount; |
222 |
|
223 |
=head1 SEE ALSO |
224 |
|
225 |
C<Fuse::DBI> website |
226 |
L<https://www.rot13.org/~dpavlin/fuse_dbi.html> |
227 |
|
228 |
C<FUSE (Filesystem in USErspace)> website |
229 |
L<http://fuse.sourceforge.net/> |
230 |
|
231 |
=head1 AUTHOR |
232 |
|
233 |
Dobrica Pavlinusic, E<lt>dpavlin@rot13.orgE<gt> |
234 |
|
235 |
=head1 COPYRIGHT AND LICENSE |
236 |
|
237 |
Copyright (C) 2004 by Dobrica Pavlinusic |
238 |
|
239 |
This library is free software; you can redistribute it and/or modify |
240 |
it under the same terms as Perl itself, either Perl version 5.8.4 or, |
241 |
at your option, any later version of Perl 5 you may have available. |
242 |
|
243 |
=cut |
244 |
|