]> git.uio.no Git - usit-rt.git/blame - sbin/rt-setup-database
Added path for rt-perl-modules for some scripts
[usit-rt.git] / sbin / rt-setup-database
CommitLineData
84fb5b46
MKG
1#!/usr/bin/perl
2# BEGIN BPS TAGGED BLOCK {{{
3#
4# COPYRIGHT:
5#
6# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
7# <sales@bestpractical.com>
8#
9# (Except where explicitly superseded by other copyright notices)
10#
11#
12# LICENSE:
13#
14# This work is made available to you under the terms of Version 2 of
15# the GNU General Public License. A copy of that license should have
16# been provided with this software, but in any event can be snarfed
17# from www.gnu.org.
18#
19# This work is distributed in the hope that it will be useful, but
20# WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22# General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program; if not, write to the Free Software
26# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27# 02110-1301 or visit their web page on the internet at
28# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
29#
30#
31# CONTRIBUTION SUBMISSION POLICY:
32#
33# (The following paragraph is not intended to limit the rights granted
34# to you to modify and distribute this software under the terms of
35# the GNU General Public License and is only of importance to you if
36# you choose to contribute your changes and enhancements to the
37# community by submitting them to Best Practical Solutions, LLC.)
38#
39# By intentionally submitting any modifications, corrections or
40# derivatives to this work, or any other work intended for use with
41# Request Tracker, to Best Practical Solutions, LLC, you confirm that
42# you are the copyright holder for those contributions and you grant
43# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
44# royalty-free, perpetual, license to use, copy, create derivative
45# works based on those contributions, and sublicense and distribute
46# those contributions and any derivatives thereof.
47#
48# END BPS TAGGED BLOCK }}}
49use strict;
50use warnings;
51
52use vars qw($Nobody $SystemUser $item);
53
54# fix lib paths, some may be relative
55BEGIN {
56 require File::Spec;
57 my @libs = ("lib", "local/lib");
58 my $bin_path;
59
60 for my $lib (@libs) {
61 unless ( File::Spec->file_name_is_absolute($lib) ) {
62 unless ($bin_path) {
63 if ( File::Spec->file_name_is_absolute(__FILE__) ) {
64 $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
65 }
66 else {
67 require FindBin;
68 no warnings "once";
69 $bin_path = $FindBin::Bin;
70 }
71 }
72 $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
73 }
74 unshift @INC, $lib;
75 }
76
77}
78
79use Term::ReadKey;
80use Getopt::Long;
81
82$| = 1; # unbuffer all output.
83
84my %args = (
85 dba => 'postgres',
86);
87GetOptions(
88 \%args,
89 'action=s',
90 'force', 'debug',
91 'dba=s', 'dba-password=s', 'prompt-for-dba-password',
92 'datafile=s', 'datadir=s', 'skip-create', 'root-password-file=s',
93 'help|h',
94);
95
96no warnings 'once';
97if ( $args{help} || ! $args{'action'} ) {
98 require Pod::Usage;
99 Pod::Usage::pod2usage({ verbose => 2 });
100 exit;
101}
102
103require RT;
104RT->LoadConfig();
105RT->InitClasses();
106
107# Force warnings to be output to STDERR if we're not already logging
108# them at a higher level
109RT->Config->Set( LogToScreen => 'warning')
110 unless ( RT->Config->Get( 'LogToScreen' )
111 && RT->Config->Get( 'LogToScreen' ) =~ /^(debug|info|notice)$/ );
112
113# get customized root password
114my $root_password;
115if ( $args{'root-password-file'} ) {
116 open( my $fh, '<', $args{'root-password-file'} )
117 or die "Couldn't open 'args{'root-password-file'}' for reading: $!";
118 $root_password = <$fh>;
119 chomp $root_password;
120 my $min_length = RT->Config->Get('MinimumPasswordLength');
121 if ($min_length) {
122 die
123"password needs to be at least $min_length long, please check file '$args{'root-password-file'}'"
124 if length $root_password < $min_length;
125 }
126 close $fh;
127}
128
129
130# check and setup @actions
131my @actions = grep $_, split /,/, $args{'action'};
132if ( @actions > 1 && $args{'datafile'} ) {
133 print STDERR "You can not use --datafile option with multiple actions.\n";
134 exit(-1);
135}
136foreach ( @actions ) {
137 unless ( /^(?:init|create|drop|schema|acl|coredata|insert|upgrade)$/ ) {
138 print STDERR "$0 called with an invalid --action parameter.\n";
139 exit(-1);
140 }
141 if ( /^(?:init|drop|upgrade)$/ && @actions > 1 ) {
142 print STDERR "You can not mix init, drop or upgrade action with any action.\n";
143 exit(-1);
144 }
145}
146
147# convert init to multiple actions
148my $init = 0;
149if ( $actions[0] eq 'init' ) {
150 if ($args{'skip-create'}) {
151 @actions = qw(schema coredata insert);
152 } else {
153 @actions = qw(create schema acl coredata insert);
154 }
155 $init = 1;
156}
157
158# set options from environment
159foreach my $key(qw(Type Host Name User Password)) {
160 next unless exists $ENV{ 'RT_DB_'. uc $key };
161 print "Using Database$key from RT_DB_". uc($key) ." environment variable.\n";
162 RT->Config->Set( "Database$key", $ENV{ 'RT_DB_'. uc $key });
163}
164
165my $db_type = RT->Config->Get('DatabaseType') || '';
166my $db_host = RT->Config->Get('DatabaseHost') || '';
167my $db_name = RT->Config->Get('DatabaseName') || '';
168my $db_user = RT->Config->Get('DatabaseUser') || '';
169my $db_pass = RT->Config->Get('DatabasePassword') || '';
170
171# load it here to get error immidiatly if DB type is not supported
172require RT::Handle;
173
174if ( $db_type eq 'SQLite' && !File::Spec->file_name_is_absolute($db_name) ) {
175 $db_name = File::Spec->catfile($RT::VarPath, $db_name);
176 RT->Config->Set( DatabaseName => $db_name );
177}
178
179my $dba_user = $args{'dba'} || $ENV{'RT_DBA_USER'} || $db_user || '';
180my $dba_pass = $args{'dba-password'} || $ENV{'RT_DBA_PASSWORD'};
181
182if ($args{'skip-create'}) {
183 $dba_user = $db_user;
184 $dba_pass = $db_pass;
185} else {
186 if ( !$args{force} && ( !defined $dba_pass || $args{'prompt-for-dba-password'} ) ) {
187 $dba_pass = get_dba_password();
188 chomp $dba_pass if defined($dba_pass);
189 }
190}
191
192print "Working with:\n"
193 ."Type:\t$db_type\nHost:\t$db_host\nName:\t$db_name\n"
194 ."User:\t$db_user\nDBA:\t$dba_user" . ($args{'skip-create'} ? ' (No DBA)' : '') . "\n";
195
196foreach my $action ( @actions ) {
197 no strict 'refs';
198 my ($status, $msg) = *{ 'action_'. $action }{'CODE'}->( %args );
199 error($action, $msg) unless $status;
200 print $msg .".\n" if $msg;
201 print "Done.\n";
202}
203
204sub action_create {
205 my %args = @_;
206 my $dbh = get_system_dbh();
207 my ($status, $msg) = RT::Handle->CheckCompatibility( $dbh, 'pre' );
208 return ($status, $msg) unless $status;
209
210 print "Now creating a $db_type database $db_name for RT.\n";
211 return RT::Handle->CreateDatabase( $dbh );
212}
213
214sub action_drop {
215 my %args = @_;
216
217 print "Dropping $db_type database $db_name.\n";
218 unless ( $args{'force'} ) {
219 print <<END;
220
221About to drop $db_type database $db_name on $db_host.
222WARNING: This will erase all data in $db_name.
223
224END
225 exit(-2) unless _yesno();
226 }
227
228 my $dbh = get_system_dbh();
229 return RT::Handle->DropDatabase( $dbh );
230}
231
232sub action_schema {
233 my %args = @_;
234 my $dbh = get_admin_dbh();
235 my ($status, $msg) = RT::Handle->CheckCompatibility( $dbh, 'pre' );
236 return ($status, $msg) unless $status;
237
238 print "Now populating database schema.\n";
239 return RT::Handle->InsertSchema( $dbh, $args{'datafile'} || $args{'datadir'} );
240}
241
242sub action_acl {
243 my %args = @_;
244 my $dbh = get_admin_dbh();
245 my ($status, $msg) = RT::Handle->CheckCompatibility( $dbh, 'pre' );
246 return ($status, $msg) unless $status;
247
248 print "Now inserting database ACLs.\n";
249 return RT::Handle->InsertACL( $dbh, $args{'datafile'} || $args{'datadir'} );
250}
251
252sub action_coredata {
253 my %args = @_;
254 $RT::Handle = RT::Handle->new;
255 $RT::Handle->dbh( undef );
256 RT::ConnectToDatabase();
257 RT::InitLogging();
258 my ($status, $msg) = RT::Handle->CheckCompatibility( $RT::Handle->dbh, 'pre' );
259 return ($status, $msg) unless $status;
260
261 print "Now inserting RT core system objects.\n";
262 return $RT::Handle->InsertInitialData;
263}
264
265sub action_insert {
266 my %args = @_;
267 $RT::Handle = RT::Handle->new;
268 RT::Init();
269 my ($status, $msg) = RT::Handle->CheckCompatibility( $RT::Handle->dbh, 'pre' );
270 return ($status, $msg) unless $status;
271
272 print "Now inserting data.\n";
273 my $file = $args{'datafile'};
274 $file = $RT::EtcPath . "/initialdata" if $init && !$file;
275 $file ||= $args{'datadir'}."/content";
276
277 # Slurp in backcompat
278 my %removed;
279 my @back = @{$args{backcompat} || []};
280 if (@back) {
281 my @lines = do {local @ARGV = @back; <>};
282 for (@lines) {
283 s/\#.*//;
284 next unless /\S/;
285 my ($class, @fields) = split;
286 $class->_BuildTableAttributes;
287 $RT::Logger->debug("Temporarily removing @fields from $class");
288 $removed{$class}{$_} = delete $RT::Record::_TABLE_ATTR->{$class}{$_}
289 for @fields;
290 }
291 }
292
293 my @ret = $RT::Handle->InsertData( $file, $root_password );
294
295 # Put back the fields we chopped off
296 for my $class (keys %removed) {
297 $RT::Record::_TABLE_ATTR->{$class}{$_} = $removed{$class}{$_}
298 for keys %{$removed{$class}};
299 }
300 return @ret;
301}
302
303sub action_upgrade {
304 my %args = @_;
305 my $base_dir = $args{'datadir'} || "./etc/upgrade";
306 return (0, "Couldn't read dir '$base_dir' with upgrade data")
307 unless -d $base_dir || -r _;
308
309 my $upgrading_from = undef;
310 do {
311 if ( defined $upgrading_from ) {
312 print "Doesn't match #.#.#: ";
313 } else {
314 print "Enter RT version you're upgrading from: ";
315 }
316 $upgrading_from = scalar <STDIN>;
317 chomp $upgrading_from;
318 $upgrading_from =~ s/\s+//g;
319 } while $upgrading_from !~ /^\d+\.\d+\.\w+$/;
320
321 my $upgrading_to = $RT::VERSION;
322 return (0, "The current version $upgrading_to is lower than $upgrading_from")
323 if RT::Handle::cmp_version( $upgrading_from, $upgrading_to ) > 0;
324
325 return (1, "The version $upgrading_to you're upgrading to is up to date")
326 if RT::Handle::cmp_version( $upgrading_from, $upgrading_to ) == 0;
327
328 my @versions = get_versions_from_to($base_dir, $upgrading_from, undef);
329 return (1, "No DB changes since $upgrading_from")
330 unless @versions;
331
332 if (RT::Handle::cmp_version($versions[-1], $upgrading_to) > 0) {
333 print "\n***** There are upgrades for $versions[-1], which is later than $upgrading_to,\n";
334 print "***** which you are nominally upgrading to. Upgrading to $versions[-1] instead.\n";
335 $upgrading_to = $versions[-1];
336 }
337
338 print "\nGoing to apply following upgrades:\n";
339 print map "* $_\n", @versions;
340
341 {
342 my $custom_upgrading_to = undef;
343 do {
344 if ( defined $custom_upgrading_to ) {
345 print "Doesn't match #.#.#: ";
346 } else {
347 print "\nEnter RT version if you want to stop upgrade at some point,\n";
348 print " or leave it blank if you want apply above upgrades: ";
349 }
350 $custom_upgrading_to = scalar <STDIN>;
351 chomp $custom_upgrading_to;
352 $custom_upgrading_to =~ s/\s+//g;
353 last unless $custom_upgrading_to;
354 } while $custom_upgrading_to !~ /^\d+\.\d+\.\w+$/;
355
356 if ( $custom_upgrading_to ) {
357 return (
358 0, "The version you entered ($custom_upgrading_to) is lower than\n"
359 ."version you're upgrading from ($upgrading_from)"
360 ) if RT::Handle::cmp_version( $upgrading_from, $custom_upgrading_to ) > 0;
361
362 return (1, "The version you're upgrading to is up to date")
363 if RT::Handle::cmp_version( $upgrading_from, $custom_upgrading_to ) == 0;
364
365 if ( RT::Handle::cmp_version( $RT::VERSION, $custom_upgrading_to ) < 0 ) {
366 print "Version you entered is greater than installed ($RT::VERSION).\n";
367 _yesno() or exit(-2);
368 }
369 # ok, checked everything no let's refresh list
370 $upgrading_to = $custom_upgrading_to;
371 @versions = get_versions_from_to($base_dir, $upgrading_from, $upgrading_to);
372
373 return (1, "No DB changes between $upgrading_from and $upgrading_to")
374 unless @versions;
375
376 print "\nGoing to apply following upgrades:\n";
377 print map "* $_\n", @versions;
378 }
379 }
380
381 print "\nIT'S VERY IMPORTANT TO BACK UP BEFORE THIS STEP\n\n";
382 _yesno() or exit(-2) unless $args{'force'};
383
384 my ( $ret, $msg );
385 foreach my $n ( 0..$#versions ) {
386 my $v = $versions[$n];
387 my @back = grep {-e $_} map {"$base_dir/$versions[$_]/backcompat"} $n+1..$#versions;
388 print "Processing $v\n";
389 my %tmp = (%args, datadir => "$base_dir/$v", datafile => undef, backcompat => \@back);
390 if ( -e "$base_dir/$v/schema.$db_type" ) {
391 ( $ret, $msg ) = action_schema( %tmp );
392 return ( $ret, $msg ) unless $ret;
393 }
394 if ( -e "$base_dir/$v/acl.$db_type" ) {
395 ( $ret, $msg ) = action_acl( %tmp );
396 return ( $ret, $msg ) unless $ret;
397 }
398 if ( -e "$base_dir/$v/content" ) {
399 ( $ret, $msg ) = action_insert( %tmp );
400 return ( $ret, $msg ) unless $ret;
401 }
402 }
403 return 1;
404}
405
406sub get_versions_from_to {
407 my ($base_dir, $from, $to) = @_;
408
409 opendir( my $dh, $base_dir ) or die "couldn't open dir: $!";
410 my @versions = grep -d "$base_dir/$_" && /\d+\.\d+\.\d+/, readdir $dh;
411 closedir $dh;
412
413 return
414 grep defined $to ? RT::Handle::cmp_version($_, $to) <= 0 : 1,
415 grep RT::Handle::cmp_version($_, $from) > 0,
416 sort RT::Handle::cmp_version @versions;
417}
418
419sub error {
420 my ($action, $msg) = @_;
421 print STDERR "Couldn't finish '$action' step.\n\n";
422 print STDERR "ERROR: $msg\n\n";
423 exit(-1);
424}
425
426sub get_dba_password {
427 print "In order to create or update your RT database,"
428 . " this script needs to connect to your "
429 . " $db_type instance on $db_host as $dba_user\n";
430 print "Please specify that user's database password below. If the user has no database\n";
431 print "password, just press return.\n\n";
432 print "Password: ";
433 ReadMode('noecho');
434 my $password = ReadLine(0);
435 ReadMode('normal');
436 print "\n";
437 return ($password);
438}
439
440# get_system_dbh
441# Returns L<DBI> database handle connected to B<system> with DBA credentials.
442# See also L<RT::Handle/SystemDSN>.
443
444
445sub get_system_dbh {
446 return _get_dbh( RT::Handle->SystemDSN, $dba_user, $dba_pass );
447}
448
449sub get_admin_dbh {
450 return _get_dbh( RT::Handle->DSN, $dba_user, $dba_pass );
451}
452
453# get_rt_dbh [USER, PASSWORD]
454
455# Returns L<DBI> database handle connected to RT database,
456# you may specify credentials(USER and PASSWORD) to connect
457# with. By default connects with credentials from RT config.
458
459sub get_rt_dbh {
460 return _get_dbh( RT::Handle->DSN, $db_user, $db_pass );
461}
462
463sub _get_dbh {
464 my ($dsn, $user, $pass) = @_;
465 my $dbh = DBI->connect(
466 $dsn, $user, $pass,
467 { RaiseError => 0, PrintError => 0 },
468 );
469 unless ( $dbh ) {
470 my $msg = "Failed to connect to $dsn as user '$user': ". $DBI::errstr;
471 if ( $args{'debug'} ) {
472 require Carp; Carp::confess( $msg );
473 } else {
474 print STDERR $msg; exit -1;
475 }
476 }
477 return $dbh;
478}
479
480sub _yesno {
481 print "Proceed [y/N]:";
482 my $x = scalar(<STDIN>);
483 $x =~ /^y/i;
484}
485
4861;
487
488__END__
489
490=head1 NAME
491
492rt-setup-database - Set up RT's database
493
494=head1 SYNOPSIS
495
496 rt-setup-database --action ...
497
498=head1 OPTIONS
499
500=over
501
502=item action
503
504Several actions can be combined using comma separated list.
505
506=over
507
508=item init
509
510Initialize the database. This is combination of multiple actions listed below.
511Create DB, schema, setup acl, insert core data and initial data.
512
513=item upgrade
514
515Apply all needed schema/acl/content updates (will ask for version to upgrade
516from)
517
518=item create
519
520Create the database.
521
522=item drop
523
524Drop the database. This will B<ERASE ALL YOUR DATA>.
525
526=item schema
527
528Initialize only the database schema
529
530To use a local or supplementary datafile, specify it using the '--datadir'
531option below.
532
533=item acl
534
535Initialize only the database ACLs
536
537To use a local or supplementary datafile, specify it using the '--datadir'
538option below.
539
540=item coredata
541
542Insert data into RT's database. This data is required for normal functioning of
543any RT instance.
544
545=item insert
546
547Insert data into RT's database. By default, will use RT's installation data.
548To use a local or supplementary datafile, specify it using the '--datafile'
549option below.
550
551=back
552
553=item datafile
554
555file path of the data you want to action on
556
557e.g. C<--datafile /path/to/datafile>
558
559=item datadir
560
561Used to specify a path to find the local database schema and acls to be
562installed.
563
564e.g. C<--datadir /path/to/>
565
566=item dba
567
568dba's username
569
570=item dba-password
571
572dba's password
573
574=item prompt-for-dba-password
575
576Ask for the database administrator's password interactively
577
578=item skip-create
579
580for 'init': skip creating the database and the user account, so we don't need
581administrator privileges
582
583=item root-password-file
584
585for 'init' and 'insert': rather than using the default administrative password
586for RT's "root" user, use the password in this file.
587
588=back