Commit | Line | Data |
---|---|---|
3f817eb4 | 1 | #! /usr/bin/perl |
2832c235 TH |
2 | # |
3 | # install.pl | |
4 | # | |
5 | # This script will create database tables as necessary. | |
dfc2b81c | 6 | # |
2832c235 TH |
7 | # It is part of the NewsStats package. |
8 | # | |
07c0b258 | 9 | # Copyright (c) 2010-2013 Thomas Hochstein <thh@inter.net> |
2832c235 | 10 | # |
dfc2b81c | 11 | # It can be redistributed and/or modified under the same terms under |
2832c235 TH |
12 | # which Perl itself is published. |
13 | ||
14 | BEGIN { | |
24d2011f | 15 | our $VERSION = "0.02"; |
2832c235 | 16 | use File::Basename; |
2ad99c20 TH |
17 | # we're in .../install, so our module is in ../lib |
18 | push(@INC, dirname($0).'/../lib'); | |
2832c235 TH |
19 | } |
20 | use strict; | |
3f817eb4 | 21 | use warnings; |
2832c235 TH |
22 | |
23 | use NewsStats qw(:DEFAULT); | |
24 | ||
2832c235 | 25 | use DBI; |
1fa94799 TH |
26 | use Getopt::Long qw(GetOptions); |
27 | Getopt::Long::config ('bundling'); | |
2832c235 TH |
28 | |
29 | ################################# Main program ################################# | |
30 | ||
31 | ### read commandline options | |
23ab67a0 | 32 | my ($OptUpdate,$OptConfFile); |
1fa94799 | 33 | GetOptions ('u|update=s' => \$OptUpdate, |
23ab67a0 | 34 | 'conffile=s' => \$OptConfFile, |
1fa94799 TH |
35 | 'h|help' => \&ShowPOD, |
36 | 'V|version' => \&ShowVersion) or exit 1; | |
2832c235 | 37 | |
2832c235 TH |
38 | ### read configuration |
39 | print("Reading configuration.\n"); | |
23ab67a0 | 40 | my %Conf = %{ReadConfig($OptConfFile)}; |
2832c235 TH |
41 | |
42 | ##### -------------------------------------------------------------------------- | |
43 | ##### Database table definitions | |
44 | ##### -------------------------------------------------------------------------- | |
45 | ||
36cffe7a TH |
46 | my $DBCreate = <<SQLDB; |
47 | CREATE DATABASE IF NOT EXISTS `$Conf{'DBDatabase'}` DEFAULT CHARSET=utf8; | |
48 | SQLDB | |
49 | ||
2832c235 | 50 | my %DBCreate = ('DBTableRaw' => <<RAW, 'DBTableGrps' => <<GRPS); |
23ab67a0 | 51 | -- |
2832c235 | 52 | -- Table structure for table DBTableRaw |
23ab67a0 | 53 | -- |
2832c235 TH |
54 | |
55 | CREATE TABLE IF NOT EXISTS `$Conf{'DBTableRaw'}` ( | |
56 | `id` bigint(20) unsigned NOT NULL auto_increment, | |
57 | `day` date NOT NULL, | |
58 | `mid` varchar(250) character set ascii NOT NULL, | |
59 | `date` datetime NOT NULL, | |
60 | `timestamp` bigint(20) NOT NULL, | |
61 | `token` varchar(80) character set ascii NOT NULL, | |
62 | `size` bigint(20) NOT NULL, | |
63 | `peer` varchar(250) NOT NULL, | |
64 | `path` varchar(1000) NOT NULL, | |
65 | `newsgroups` varchar(1000) NOT NULL, | |
66 | `headers` longtext NOT NULL, | |
67 | `disregard` tinyint(1) default '0', | |
68 | PRIMARY KEY (`id`), | |
69 | KEY `day` (`day`), | |
70 | KEY `mid` (`mid`), | |
71 | KEY `peer` (`peer`) | |
72 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Raw data'; | |
73 | RAW | |
23ab67a0 | 74 | -- |
2832c235 | 75 | -- Table structure for table DBTableGrps |
23ab67a0 | 76 | -- |
2832c235 TH |
77 | |
78 | CREATE TABLE IF NOT EXISTS `$Conf{'DBTableGrps'}` ( | |
79 | `id` bigint(20) unsigned NOT NULL auto_increment, | |
80 | `month` varchar(7) character set ascii NOT NULL, | |
81 | `newsgroup` varchar(100) NOT NULL, | |
82 | `postings` int(11) NOT NULL, | |
83 | `revision` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, | |
84 | PRIMARY KEY (`id`), | |
85 | UNIQUE KEY `month_newsgroup` (`month`,`newsgroup`), | |
86 | KEY `newsgroup` (`newsgroup`), | |
87 | KEY `postings` (`postings`) | |
88 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Postings per newsgroup'; | |
89 | GRPS | |
90 | ||
f6d15ca7 TH |
91 | ##### -------------------------------------------------------------------------- |
92 | ##### Installation / upgrade instructions | |
93 | ##### -------------------------------------------------------------------------- | |
2832c235 | 94 | |
f6d15ca7 TH |
95 | my $Install = <<INSTALL; |
96 | ---------- | |
2832c235 TH |
97 | Things left to do: |
98 | ||
99 | 1) Setup an INN feed to feedlog.pl | |
100 | ||
101 | a) Edit your 'newsfeeds' file and insert something like | |
102 | ||
103 | ## gather statistics for NewsStats | |
9c141b0c TH |
104 | newsstats!\\ |
105 | :!*,de.*\\ | |
fd0717a1 | 106 | :Tc,WmtfbsPNH,Ac:$HomePath/bin/feedlog.pl |
2832c235 TH |
107 | |
108 | Please | |
109 | ||
110 | * check that you got the path to feedlog.pl right | |
111 | * check that feedlog.pl can be executed by the news user | |
112 | * adapt the pattern (here: 'de.*') to your needs | |
113 | ||
114 | b) Check your 'newsfeeds' syntax: | |
115 | ||
116 | # ctlinnd checkfile | |
117 | ||
118 | and reload 'newsfeeds': | |
119 | ||
120 | # ctlinnd reload newsfeeds 'Adding newsstats! feed' | |
121 | ||
122 | c) Watch your 'news.notice' and 'errlog' files: | |
123 | ||
124 | # tail -f /var/log/news/news.notice | |
125 | ... | |
126 | # tail -f /var/log/news/errlog | |
127 | ||
128 | 2) Watch your $Conf{'DBTableRaw'} table fill. | |
129 | ||
130 | 3) Read the documentation. ;) | |
131 | ||
132 | Enjoy! | |
133 | ||
134 | -thh <thh\@inter.net> | |
f6d15ca7 TH |
135 | INSTALL |
136 | ||
da6fc073 TH |
137 | my $Upgrade =''; |
138 | if ($OptUpdate) { | |
139 | $Upgrade = <<UPGRADE; | |
f6d15ca7 | 140 | ---------- |
1fa94799 | 141 | Your installation was upgraded from $OptUpdate to $PackageVersion. |
f6d15ca7 TH |
142 | |
143 | Don't forget to restart your INN feed so that it can pick up the new version: | |
144 | ||
145 | # ctlinnd begin 'newsstats!' | |
146 | ||
147 | (or whatever you called your feed). | |
148 | UPGRADE | |
da6fc073 | 149 | } |
f6d15ca7 | 150 | |
f6d15ca7 TH |
151 | ##### --------------------------- End of definitions --------------------------- |
152 | ||
36cffe7a TH |
153 | ### create DB, if necessary |
154 | if (!$OptUpdate) { | |
155 | print "----------\nStarting database creation.\n"; | |
156 | # create database | |
157 | # we can't use InitDB() as that will use a table name of | |
158 | # the table that doesn't exist yet ... | |
159 | my $DBHandle = DBI->connect(sprintf('DBI:%s:host=%s',$Conf{'DBDriver'}, | |
160 | $Conf{'DBHost'}), $Conf{'DBUser'}, | |
161 | $Conf{'DBPw'}, { PrintError => 0 }); | |
162 | my $DBQuery = $DBHandle->prepare($DBCreate); | |
163 | $DBQuery->execute() or &Bleat(2, sprintf("Can't create database %s: %s%\n", | |
164 | $Conf{'DBDatabase'}, $DBI::errstr)); | |
23ab67a0 | 165 | |
36cffe7a TH |
166 | printf("Database table %s created succesfully.\n",$Conf{'DBDatabase'}); |
167 | $DBHandle->disconnect; | |
168 | }; | |
169 | ||
f6d15ca7 TH |
170 | ### DB init, read list of tables |
171 | print "Reading database information.\n"; | |
172 | my $DBHandle = InitDB(\%Conf,1); | |
1fa94799 TH |
173 | my %TablesInDB = |
174 | %{$DBHandle->table_info('%', '%', '%', 'TABLE')->fetchall_hashref('TABLE_NAME')}; | |
f6d15ca7 | 175 | |
1fa94799 | 176 | if (!$OptUpdate) { |
f6d15ca7 | 177 | ##### installation mode |
f6d15ca7 TH |
178 | # check for tables and create them, if they don't exist yet |
179 | foreach my $Table (keys %DBCreate) { | |
180 | &CreateTable($Table); | |
181 | }; | |
182 | print "Database table generation done.\n"; | |
dfc2b81c | 183 | |
f6d15ca7 TH |
184 | # Display install instructions |
185 | print $Install; | |
186 | } else { | |
187 | ##### upgrade mode | |
188 | print "----------\nStarting upgrade process.\n"; | |
59d0c5ef | 189 | $PackageVersion = '0.03'; |
1fa94799 TH |
190 | if ($OptUpdate < $PackageVersion) { |
191 | if ($OptUpdate < 0.02) { | |
59d0c5ef TH |
192 | # 0.01 -> 0.02 |
193 | # &DoMySQL('...;'); | |
194 | # print "v0.02: Database upgrades ...\n"; | |
195 | # &PrintInstructions('0.02',<<" INSTRUCTIONS"); | |
196 | # INSTRUCTIONS | |
f6d15ca7 TH |
197 | }; |
198 | }; | |
59d0c5ef | 199 | # Display general upgrade instructions |
f6d15ca7 TH |
200 | print $Upgrade; |
201 | }; | |
202 | ||
203 | # close handle | |
204 | $DBHandle->disconnect; | |
205 | ||
206 | exit(0); | |
207 | ||
208 | ################################# Subroutines ################################## | |
209 | ||
59d0c5ef | 210 | sub CreateTable { |
f6d15ca7 TH |
211 | my $Table = shift; |
212 | if (defined($TablesInDB{$Conf{$Table}})) { | |
1fa94799 TH |
213 | printf("Database table %s.%s already exists, skipping ....\n", |
214 | $Conf{'DBDatabase'},$Conf{$Table}); | |
f6d15ca7 TH |
215 | return; |
216 | }; | |
217 | my $DBQuery = $DBHandle->prepare($DBCreate{$Table}); | |
1fa94799 TH |
218 | $DBQuery->execute() or |
219 | &Bleat(2, sprintf("Can't create table %s in database %s: %s%\n",$Table, | |
220 | $Conf{'DBDatabase'},$DBI::errstr)); | |
221 | printf("Database table %s.%s created succesfully.\n", | |
222 | $Conf{'DBDatabase'},$Conf{$Table}); | |
f6d15ca7 TH |
223 | return; |
224 | }; | |
225 | ||
59d0c5ef | 226 | sub DoMySQL { |
f6d15ca7 TH |
227 | my $SQL = shift; |
228 | my $DBQuery = $DBHandle->prepare($SQL); | |
1fa94799 | 229 | $DBQuery->execute() or &Bleat(1, sprintf("Database error: %s\n",$DBI::errstr)); |
f6d15ca7 TH |
230 | return; |
231 | }; | |
232 | ||
59d0c5ef TH |
233 | sub PrintInstructions { |
234 | my ($UpVersion,$Instructions) = @_; | |
235 | print "v$UpVersion: Upgrade Instructions >>>>>\n"; | |
236 | my $Padding = ' ' x (length($UpVersion) + 3); | |
237 | $Instructions =~ s/^ /$Padding/mg; | |
238 | print $Instructions; | |
239 | print "<" x (length($UpVersion) + 29) . "\n"; | |
240 | }; | |
241 | ||
2832c235 TH |
242 | |
243 | __END__ | |
244 | ||
245 | ################################ Documentation ################################# | |
246 | ||
247 | =head1 NAME | |
248 | ||
249 | install - installation script | |
250 | ||
251 | =head1 SYNOPSIS | |
252 | ||
e39d4207 | 253 | B<install> [B<-Vh> [--update I<version>] [B<--conffile> I<filename>] |
2832c235 TH |
254 | |
255 | =head1 REQUIREMENTS | |
256 | ||
1fa94799 | 257 | See L<doc/README>. |
2832c235 TH |
258 | |
259 | =head1 DESCRIPTION | |
260 | ||
261 | This script will create database tables as necessary and configured. | |
262 | ||
263 | =head2 Configuration | |
264 | ||
44c19709 TH |
265 | B<install> will read its configuration from F<newsstats.conf> which should |
266 | be present in etc/ via Config::Auto or from a configuration file submitted | |
267 | by the B<--conffile> option. | |
2832c235 | 268 | |
1fa94799 | 269 | See L<doc/INSTALL> for an overview of possible configuration options. |
2832c235 TH |
270 | |
271 | =head1 OPTIONS | |
272 | ||
273 | =over 3 | |
274 | ||
1fa94799 | 275 | =item B<-V>, B<--version> |
2832c235 | 276 | |
1fa94799 | 277 | Print out version and copyright information and exit. |
2832c235 | 278 | |
1fa94799 | 279 | =item B<-h>, B<--help> |
2832c235 TH |
280 | |
281 | Print this man page and exit. | |
282 | ||
1fa94799 TH |
283 | =item B<-u>, B<--update> I<version> |
284 | ||
285 | Don't do a fresh install, but update from I<version>. | |
286 | ||
23ab67a0 TH |
287 | =item B<--conffile> I<filename> |
288 | ||
289 | Load configuration from I<filename> instead of F<newsstats.conf>. | |
290 | ||
2832c235 TH |
291 | =back |
292 | ||
293 | =head1 FILES | |
294 | ||
295 | =over 4 | |
296 | ||
2ad99c20 | 297 | =item F<install/install.pl> |
2832c235 TH |
298 | |
299 | The script itself. | |
300 | ||
2ad99c20 | 301 | =item F<lib/NewsStats.pm> |
2832c235 TH |
302 | |
303 | Library functions for the NewsStats package. | |
304 | ||
2ad99c20 | 305 | =item F<etc/newsstats.conf> |
2832c235 | 306 | |
1fa94799 | 307 | Runtime configuration file. |
2832c235 TH |
308 | |
309 | =back | |
310 | ||
311 | =head1 BUGS | |
312 | ||
313 | Please report any bugs or feature requests to the author or use the | |
314 | bug tracker at L<http://bugs.th-h.de/>! | |
315 | ||
316 | =head1 SEE ALSO | |
317 | ||
318 | =over 2 | |
319 | ||
320 | =item - | |
321 | ||
1fa94799 | 322 | L<doc/README> |
2832c235 TH |
323 | |
324 | =item - | |
325 | ||
1fa94799 | 326 | L<doc/INSTALL> |
2832c235 TH |
327 | |
328 | =back | |
329 | ||
330 | This script is part of the B<NewsStats> package. | |
331 | ||
332 | =head1 AUTHOR | |
333 | ||
334 | Thomas Hochstein <thh@inter.net> | |
335 | ||
336 | =head1 COPYRIGHT AND LICENSE | |
337 | ||
28717921 | 338 | Copyright (c) 2010-2013 Thomas Hochstein <thh@inter.net> |
2832c235 TH |
339 | |
340 | This program is free software; you may redistribute it and/or modify it | |
341 | under the same terms as Perl itself. | |
342 | ||
343 | =cut |