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 { | |
15 | our $VERSION = "0.01"; | |
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 | ||
25 | use Cwd; | |
26 | ||
27 | use DBI; | |
1fa94799 TH |
28 | use Getopt::Long qw(GetOptions); |
29 | Getopt::Long::config ('bundling'); | |
2832c235 TH |
30 | |
31 | ################################# Main program ################################# | |
32 | ||
33 | ### read commandline options | |
1fa94799 TH |
34 | my ($OptUpdate); |
35 | GetOptions ('u|update=s' => \$OptUpdate, | |
36 | 'h|help' => \&ShowPOD, | |
37 | 'V|version' => \&ShowVersion) or exit 1; | |
2832c235 TH |
38 | |
39 | ### change working directory to .. (as we're in .../install) | |
512781bd | 40 | chdir dirname($FullPath).'/..'; |
f6d15ca7 | 41 | my $Path = cwd(); |
2832c235 TH |
42 | |
43 | ### read configuration | |
44 | print("Reading configuration.\n"); | |
2ad99c20 | 45 | my %Conf = %{ReadConfig('')}; |
2832c235 TH |
46 | |
47 | ##### -------------------------------------------------------------------------- | |
48 | ##### Database table definitions | |
49 | ##### -------------------------------------------------------------------------- | |
50 | ||
36cffe7a TH |
51 | my $DBCreate = <<SQLDB; |
52 | CREATE DATABASE IF NOT EXISTS `$Conf{'DBDatabase'}` DEFAULT CHARSET=utf8; | |
53 | SQLDB | |
54 | ||
2832c235 TH |
55 | my %DBCreate = ('DBTableRaw' => <<RAW, 'DBTableGrps' => <<GRPS); |
56 | -- | |
57 | -- Table structure for table DBTableRaw | |
58 | -- | |
59 | ||
60 | CREATE TABLE IF NOT EXISTS `$Conf{'DBTableRaw'}` ( | |
61 | `id` bigint(20) unsigned NOT NULL auto_increment, | |
62 | `day` date NOT NULL, | |
63 | `mid` varchar(250) character set ascii NOT NULL, | |
64 | `date` datetime NOT NULL, | |
65 | `timestamp` bigint(20) NOT NULL, | |
66 | `token` varchar(80) character set ascii NOT NULL, | |
67 | `size` bigint(20) NOT NULL, | |
68 | `peer` varchar(250) NOT NULL, | |
69 | `path` varchar(1000) NOT NULL, | |
70 | `newsgroups` varchar(1000) NOT NULL, | |
71 | `headers` longtext NOT NULL, | |
72 | `disregard` tinyint(1) default '0', | |
73 | PRIMARY KEY (`id`), | |
74 | KEY `day` (`day`), | |
75 | KEY `mid` (`mid`), | |
76 | KEY `peer` (`peer`) | |
77 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Raw data'; | |
78 | RAW | |
79 | -- | |
80 | -- Table structure for table DBTableGrps | |
81 | -- | |
82 | ||
83 | CREATE TABLE IF NOT EXISTS `$Conf{'DBTableGrps'}` ( | |
84 | `id` bigint(20) unsigned NOT NULL auto_increment, | |
85 | `month` varchar(7) character set ascii NOT NULL, | |
86 | `newsgroup` varchar(100) NOT NULL, | |
87 | `postings` int(11) NOT NULL, | |
88 | `revision` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, | |
89 | PRIMARY KEY (`id`), | |
90 | UNIQUE KEY `month_newsgroup` (`month`,`newsgroup`), | |
91 | KEY `newsgroup` (`newsgroup`), | |
92 | KEY `postings` (`postings`) | |
93 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Postings per newsgroup'; | |
94 | GRPS | |
95 | ||
f6d15ca7 TH |
96 | ##### -------------------------------------------------------------------------- |
97 | ##### Installation / upgrade instructions | |
98 | ##### -------------------------------------------------------------------------- | |
2832c235 | 99 | |
f6d15ca7 TH |
100 | my $Install = <<INSTALL; |
101 | ---------- | |
2832c235 TH |
102 | Things left to do: |
103 | ||
104 | 1) Setup an INN feed to feedlog.pl | |
105 | ||
106 | a) Edit your 'newsfeeds' file and insert something like | |
107 | ||
108 | ## gather statistics for NewsStats | |
9c141b0c TH |
109 | newsstats!\\ |
110 | :!*,de.*\\ | |
2832c235 TH |
111 | :Tc,WmtfbsPNH,Ac:$Path/feedlog.pl |
112 | ||
113 | Please | |
114 | ||
115 | * check that you got the path to feedlog.pl right | |
116 | * check that feedlog.pl can be executed by the news user | |
117 | * adapt the pattern (here: 'de.*') to your needs | |
118 | ||
119 | b) Check your 'newsfeeds' syntax: | |
120 | ||
121 | # ctlinnd checkfile | |
122 | ||
123 | and reload 'newsfeeds': | |
124 | ||
125 | # ctlinnd reload newsfeeds 'Adding newsstats! feed' | |
126 | ||
127 | c) Watch your 'news.notice' and 'errlog' files: | |
128 | ||
129 | # tail -f /var/log/news/news.notice | |
130 | ... | |
131 | # tail -f /var/log/news/errlog | |
132 | ||
133 | 2) Watch your $Conf{'DBTableRaw'} table fill. | |
134 | ||
135 | 3) Read the documentation. ;) | |
136 | ||
137 | Enjoy! | |
138 | ||
139 | -thh <thh\@inter.net> | |
f6d15ca7 TH |
140 | INSTALL |
141 | ||
da6fc073 TH |
142 | my $Upgrade =''; |
143 | if ($OptUpdate) { | |
144 | $Upgrade = <<UPGRADE; | |
f6d15ca7 | 145 | ---------- |
1fa94799 | 146 | Your installation was upgraded from $OptUpdate to $PackageVersion. |
f6d15ca7 TH |
147 | |
148 | Don't forget to restart your INN feed so that it can pick up the new version: | |
149 | ||
150 | # ctlinnd begin 'newsstats!' | |
151 | ||
152 | (or whatever you called your feed). | |
153 | UPGRADE | |
da6fc073 | 154 | } |
f6d15ca7 | 155 | |
f6d15ca7 TH |
156 | ##### --------------------------- End of definitions --------------------------- |
157 | ||
36cffe7a TH |
158 | ### create DB, if necessary |
159 | if (!$OptUpdate) { | |
160 | print "----------\nStarting database creation.\n"; | |
161 | # create database | |
162 | # we can't use InitDB() as that will use a table name of | |
163 | # the table that doesn't exist yet ... | |
164 | my $DBHandle = DBI->connect(sprintf('DBI:%s:host=%s',$Conf{'DBDriver'}, | |
165 | $Conf{'DBHost'}), $Conf{'DBUser'}, | |
166 | $Conf{'DBPw'}, { PrintError => 0 }); | |
167 | my $DBQuery = $DBHandle->prepare($DBCreate); | |
168 | $DBQuery->execute() or &Bleat(2, sprintf("Can't create database %s: %s%\n", | |
169 | $Conf{'DBDatabase'}, $DBI::errstr)); | |
170 | ||
171 | printf("Database table %s created succesfully.\n",$Conf{'DBDatabase'}); | |
172 | $DBHandle->disconnect; | |
173 | }; | |
174 | ||
f6d15ca7 TH |
175 | ### DB init, read list of tables |
176 | print "Reading database information.\n"; | |
177 | my $DBHandle = InitDB(\%Conf,1); | |
1fa94799 TH |
178 | my %TablesInDB = |
179 | %{$DBHandle->table_info('%', '%', '%', 'TABLE')->fetchall_hashref('TABLE_NAME')}; | |
f6d15ca7 | 180 | |
1fa94799 | 181 | if (!$OptUpdate) { |
f6d15ca7 | 182 | ##### installation mode |
f6d15ca7 TH |
183 | # check for tables and create them, if they don't exist yet |
184 | foreach my $Table (keys %DBCreate) { | |
185 | &CreateTable($Table); | |
186 | }; | |
187 | print "Database table generation done.\n"; | |
dfc2b81c | 188 | |
f6d15ca7 TH |
189 | # Display install instructions |
190 | print $Install; | |
191 | } else { | |
192 | ##### upgrade mode | |
193 | print "----------\nStarting upgrade process.\n"; | |
59d0c5ef | 194 | $PackageVersion = '0.03'; |
1fa94799 TH |
195 | if ($OptUpdate < $PackageVersion) { |
196 | if ($OptUpdate < 0.02) { | |
59d0c5ef TH |
197 | # 0.01 -> 0.02 |
198 | # &DoMySQL('...;'); | |
199 | # print "v0.02: Database upgrades ...\n"; | |
200 | # &PrintInstructions('0.02',<<" INSTRUCTIONS"); | |
201 | # INSTRUCTIONS | |
f6d15ca7 TH |
202 | }; |
203 | }; | |
59d0c5ef | 204 | # Display general upgrade instructions |
f6d15ca7 TH |
205 | print $Upgrade; |
206 | }; | |
207 | ||
208 | # close handle | |
209 | $DBHandle->disconnect; | |
210 | ||
211 | exit(0); | |
212 | ||
213 | ################################# Subroutines ################################## | |
214 | ||
59d0c5ef | 215 | sub CreateTable { |
f6d15ca7 TH |
216 | my $Table = shift; |
217 | if (defined($TablesInDB{$Conf{$Table}})) { | |
1fa94799 TH |
218 | printf("Database table %s.%s already exists, skipping ....\n", |
219 | $Conf{'DBDatabase'},$Conf{$Table}); | |
f6d15ca7 TH |
220 | return; |
221 | }; | |
222 | my $DBQuery = $DBHandle->prepare($DBCreate{$Table}); | |
1fa94799 TH |
223 | $DBQuery->execute() or |
224 | &Bleat(2, sprintf("Can't create table %s in database %s: %s%\n",$Table, | |
225 | $Conf{'DBDatabase'},$DBI::errstr)); | |
226 | printf("Database table %s.%s created succesfully.\n", | |
227 | $Conf{'DBDatabase'},$Conf{$Table}); | |
f6d15ca7 TH |
228 | return; |
229 | }; | |
230 | ||
59d0c5ef | 231 | sub DoMySQL { |
f6d15ca7 TH |
232 | my $SQL = shift; |
233 | my $DBQuery = $DBHandle->prepare($SQL); | |
1fa94799 | 234 | $DBQuery->execute() or &Bleat(1, sprintf("Database error: %s\n",$DBI::errstr)); |
f6d15ca7 TH |
235 | return; |
236 | }; | |
237 | ||
59d0c5ef TH |
238 | sub PrintInstructions { |
239 | my ($UpVersion,$Instructions) = @_; | |
240 | print "v$UpVersion: Upgrade Instructions >>>>>\n"; | |
241 | my $Padding = ' ' x (length($UpVersion) + 3); | |
242 | $Instructions =~ s/^ /$Padding/mg; | |
243 | print $Instructions; | |
244 | print "<" x (length($UpVersion) + 29) . "\n"; | |
245 | }; | |
246 | ||
2832c235 TH |
247 | |
248 | __END__ | |
249 | ||
250 | ################################ Documentation ################################# | |
251 | ||
252 | =head1 NAME | |
253 | ||
254 | install - installation script | |
255 | ||
256 | =head1 SYNOPSIS | |
257 | ||
1fa94799 | 258 | B<install> [B<-Vh> [--update I<version>] |
2832c235 TH |
259 | |
260 | =head1 REQUIREMENTS | |
261 | ||
1fa94799 | 262 | See L<doc/README>. |
2832c235 TH |
263 | |
264 | =head1 DESCRIPTION | |
265 | ||
266 | This script will create database tables as necessary and configured. | |
267 | ||
268 | =head2 Configuration | |
269 | ||
1fa94799 | 270 | B<install> will read its configuration from F<newsstats.conf> via |
2832c235 TH |
271 | Config::Auto. |
272 | ||
1fa94799 | 273 | See L<doc/INSTALL> for an overview of possible configuration options. |
2832c235 TH |
274 | |
275 | =head1 OPTIONS | |
276 | ||
277 | =over 3 | |
278 | ||
1fa94799 | 279 | =item B<-V>, B<--version> |
2832c235 | 280 | |
1fa94799 | 281 | Print out version and copyright information and exit. |
2832c235 | 282 | |
1fa94799 | 283 | =item B<-h>, B<--help> |
2832c235 TH |
284 | |
285 | Print this man page and exit. | |
286 | ||
1fa94799 TH |
287 | =item B<-u>, B<--update> I<version> |
288 | ||
289 | Don't do a fresh install, but update from I<version>. | |
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 | ||
1fa94799 | 338 | Copyright (c) 2010-2012 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 |