-
Notifications
You must be signed in to change notification settings - Fork 25
/
install
executable file
·331 lines (287 loc) · 9.63 KB
/
install
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
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use autodie;
use DBI;
use File::Path qw/make_path/;
use File::Copy qw/copy/;
use File::Basename qw/basename/;
use Getopt::Long qw/GetOptions/;
GetOptions(
'dbhost=s' => \my $Dbhost,
'dbport=s' => \my $Dbport,
'dbname=s' => \my $Dbname,
'dbuser=s' => \my $Dbuser,
'dbpass=s' => \my $Dbpass,
'timezone=s' => \my $Timezone,
'prefix=s' => \my $Prefix,
'url=s' => \my $Url,
'use-apache!' => \my $Use_apache,
'language=s' => \my $Language,
) or die <<_;
Usage: $0 [options]
Valid options are:
--dbhost=host Host of the database server
--dbport=3306 Port of the database server
--dbname=ilbot Name of the database
--dbuser=ilbot Database user name
--dbpass=geheim Database password
--timezone=gmt Timezone ("gmt" or "local")
--prefix=/here/ Installation prefix path
--use-apache Use Apache 2 as web server
--no-use-apache Don't use Apache
--url=http://1.to/ Full URL to the new ilbot instance
--language=en Language code (for the search index)
If you don't supply some of these options, $0 will
ask for them interactively.
_
my %files = (
bin => ['ilbot2.pl', 'util/cron-graphs.pl', 'util/create-search-index'],
www => ['ilbot.fcgi', 'ilbot.psgi'],
sql => ['sql/update-cache.mysql'],
'static/s' => ['static/s/*'],
'config/template' => ['config/template/*.tmpl'],
);
say 'Checking dependencies';
my $missing = 0;
sub requires {
my ($module, $version) = @_;
my $str = $version ? "use $module $version;" : "use $module;";
eval $str;
if ($@) {
say "Missing dependency: Module $module",
($version ? " Version $version" : "");
$missing++;
}
}
die "Missing file 'cpanfile'" unless -e 'cpanfile';
do 'cpanfile';
if ($missing) {
die "$missing missing dependencies; please install them from the CPAN (or through your package manager, if applicable), and then try running $0 again\n";
}
say '... dependencies all OK';
sub prompt {
my ($text, $default) = @_;
if (defined $default && length $default) {
$text .= " [$default]";
}
$| = 1;
print "$text> ";
my $answer = <>;
chomp $answer;
return $answer || $default;
}
print <<EOT;
Database Access
===============
You need a mysql database where ilbot stores the logs.
For installation you need privileges for creating tables and indxes.
For running ilbot, you need INSERT, SELECT and UPDATE privileges.
EOT
my $host = $Dbhost // prompt('Database host', 'localhost');
my $port = $Dbport // prompt('Database port', 3306);
my $dbname = $Dbname // prompt('Database name', 'ilbot');
my $dbuser = $Dbuser // prompt('Database username', 'ilbot');
my $dbpass = $Dbpass // prompt('Database password');
say "Now testing your database connection...";
my $dbh = DBI->connect("DBI:mysql:database=$dbname;host=$host;port=$port", $dbuser, $dbpass, { RaiseError => 1, PrintError => 0 });
$dbh->do('SELECT 1');
my $has_table_irclog = eval { $dbh->do('SELECT 1 FROM ilbot_channel LIMIT 1'); 1 };
if ($has_table_irclog) {
print <<EOT
The database $dbname already has a table named 'ilbot_channel'. I'm going to
use this table (and the others going along with it), and assume that they
already have the right structure. If not, either abort this installer with
Ctrl-C and run it again with a different database, or drop the relevant
tables and create them again from the schema in file 'sql/schema.mysql'
EOT
}
else {
say "Database connection is fine, creating the schema for you...";
eval {
open my $SCHEMA, '<', 'sql/schema.mysql';
local $/ = '';
while (<$SCHEMA>) {
# the .sql file contains a custom DELIMITER command, which is needed
# for applying the schema through the mysql command line.
# But through the C API it is neither necessary nor allowed, so
# it must be stripped before passing it through DBI's "do" method.
# Compare http://stackoverflow.com/questions/2109366/changing-the-mysql-query-delimiter-through-the-c-api
if ( s/DELIMITER\s*(\S+)// ) {
my $delim = $1;
s{$delim\s*\z}{};
}
$dbh->do($_);
}
};
if ($@) {
die "Schema creation failed: $@\n";
}
else {
say "Created that schema for you.";
}
}
$dbh->disconnect;
print <<EOT;
Time Zone
=========
You can either log and display the logs all in UTC/GMT, or
in whatever timezone your computer considers "local time".
Please enter either 'gmt' or 'local' below:
EOT
my $tz = $Timezone // prompt('Timezone', 'gmt');
print <<EOT;
Installation Path
=================
The installation path must
+ be writable by you (and potentially cron jobs)
+ be readable by Apache (if you use Apache for the web frontend)
+ not yet exist
EOT
my $path = $Prefix // prompt('Installation path', $ENV{HOME} . '/ilbot');
die "No installation path given, aborting\n" unless $path;
die "$path already exists" if -e $path;
make_path $path;
make_path "$path/config";
my $backend_conf = "$path/config/backend.conf";
open my $CONFIG, '>', $backend_conf;
print { $CONFIG } <<EOC;
# Database connectivity
dsn = mysql
database = $dbname
host = $host
port = $port
user = $dbuser
password = $dbpass
# once stuff works as it should, set this to 1
use_cache = 0
# Timezone
# Options:
# * 'local' for server's timezone
# * 'gmt' for UTC
timezone = $tz
EOC
close $CONFIG;
say "Written config to $backend_conf";
print <<_;
The search indexer stems words, and for that it needs to know the
language that will be logged primarily. Use the usual two-letter
language codes. For a list of supported languages, please see
http://search.cpan.org/perldoc?Lucy%3A%3AAnalysis%3A%3APolyAnalyzer
_
my $language = $Language // prompt('language', 'en');
my $search_config = "$path/config/search.conf";
open $CONFIG, '>', $search_config;
print { $CONFIG } <<_;
language = $language
context = 3
_
close $CONFIG;
sub copy_and_replace {
my $replacement = "use lib '$path/lib'; use Ilbot::Config '$path/config';\n";
my ($from, $to) = @_;
unless (-f $from) {
die "File '$from' seems to be missing.";
}
if (-T $from) {
open my $IN, '<:encoding(UTF-8)', $from;
open my $OUT, '>:encoding(UTF-8)', $to;
while (<$IN>) {
s/^# TO BE REPLACED BY THE INSTALLER.*/$replacement/s;
print $OUT $_;
}
close $IN;
close $OUT;
system('chmod', "--reference=$from", $to);
}
else {
copy($from, $to);
}
}
make_path "$path/static/s";
for my $dest (keys %files) {
my $p = "$path/$dest";
make_path $p unless -d $p;
for my $file (grep -f, map glob, @{ $files{$dest} }) {
copy_and_replace($file, "$p/" . basename($file));
}
}
make_path "$path/$_" for glob 'lib/Ilbot/*/';
for my $f (map glob, 'lib/Ilbot/*.pm', 'lib/Ilbot/*/*.pm') {
copy($f, "$path/$f") or die "Cannot copy $f to $path/$f: $!";
}
say "Files copied";
print <<EOT;
Webserver configuration
=======================
You can either deploy the web frontend with Apache 2 and fasgcgi,
or run a separate, Plack-compatiable HTTP server.
See http://plackperl.org/#servers if you take the latter rout.
EOT
my $use_apache = $Use_apache // prompt('Use Apache 2', 'yes');
if ($use_apache =~ /^y/i) {
print <<EOT;
What's the URL of the new ilbot instance?
(For example http://irclog.perlgeek.de/)
EOT
my $url = $Url // prompt('URL');
if ($url =~ qr{https?://([^/]+)(.*)}) {
my ($domain, $base) = ($1, $2);
$base .= '/' unless $base ~~ m{/$};
open my $CONF, '>', "$path/config/www.conf";
print $CONF <<EOC;
base_url = $base
static_path = $path/static
# enable this if you want to rate limit the dynamic pages
# (in requests per hour per IP)
#throttle = 200
EOC
close $CONF;
open $CONF, '>', "$path/$domain.conf";
print $CONF <<EOC;
<VirtualHost *:80>
ServerName $domain
FastCgiServer $path/www/ilbot.fcgi -processes 3
Alias /s/ $path/static/s/
Alias / $path/www/ilbot.fcgi/
# other useful stuff; make sure that /var/log/apach2/$domain/ exists
# before enabling it:
# CustomLog /var/log/apache2/$domain/access.log combined
# ErrorLog /var/log/apache2/$domain/error.log
#
# ServerSignature On
</VirtualHost>
EOC
close $CONF;
print <<EOT;
There's an initial apache config file in $path/$domain.conf.
Please edit it to your liking, and then enable it.
On Debian, you can do it with these commands (as root):
\$ mv $path/$domain.conf /etc/apache2/sites-available/
\$ a2ensite $domain.conf
\$ service apache2 reload
EOT
}
else {
say "I don't understand this URL; please configure Apache yourself";
}
}
print <<EOT;
It's your turn now
==================
Write a config file for the logging bot, taking file
config/bot.conf.example as an example. Then run the logger as
\$ perl $path/bin/ilbot2.pl config/bot.conf
Since the logging stops as soon as the program terminates, it is
advisable to run it inside a screen or tmux session.
Periodically run `$path/bin/create-search index` to create or update
the search index, and
`mysql -h $Dbhost -u $Dbuser $Dbname < $path/sql/update-cache.mysql`
to update some in-database caches.
If you want to show activity graphs on the front page, please read
file README.activity-graph.md
Please add your contact details to $path/config/template/footer.tmpl.
If you find ilbot useful, consider donating via the paypal
button at http://moritz.faui2k3.org/en/ilbot#donate
EOT