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