]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/eg_db_config.pl
Augment kbeswick's version of the script; a bit more tolerant of relative paths and...
[Evergreen.git] / Open-ILS / src / support-scripts / eg_db_config.pl
1 #!/usr/bin/perl
2 # eg_db_config.pl -- configure Evergreen database settings and create schema
3 # vim:noet:ts=4:sw=4:
4 #
5 # Copyright (C) 2008 Equinox Software, Inc.
6 # Copyright (C) 2008 Laurentian University
7 # Author: Kevin Beswick <kevinbeswick00@gmail.com>
8 # Author: Dan Scott <dscott@laurentian.ca>
9 #
10 # This program is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU General Public License
12 # as published by the Free Software Foundation; either version 2
13 # of the License, or (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19
20 use strict; use warnings;
21 use XML::LibXML;
22 use File::Copy;
23 use Getopt::Long;
24 use File::Spec;
25 use File::Basename;
26
27 my ($dbhost, $dbport, $dbname, $dbuser, $dbpw, $help);
28 my $config_file = '';
29 my $build_db_sh = '';
30 my @services;
31
32 # Get the directory for this script
33 my $script_dir = dirname($0);
34
35 sub update_config {
36         # Puts command line specified settings into xml file
37         my ($services, $settings) = @_;
38
39         my $parser = XML::LibXML->new();
40         my $opensrf_config = $parser->parse_file($config_file);
41
42         if (@$services) {
43                 foreach my $service (@$services) {
44                         foreach my $key (keys %$settings) {
45                                 next unless $settings->{$key};
46                                 my(@node) = $opensrf_config->findnodes("//$service//database/$key/text()");
47                                 foreach(@node) {
48                                         $_->setData($settings->{$key});
49                                 }
50                         }
51
52                 }
53         }
54         else {
55                 foreach my $key (keys %$settings) {
56                         my(@node) = $opensrf_config->findnodes("//database/$key/text()");
57                         foreach(@node) {
58                                 $_->setData($settings->{$key});
59                         }
60                 }
61         }
62
63         if (copy($config_file, "$config_file.bak")) {
64                 print "Backed up original configuration file to '$config_file.bak'\n";
65         } else {
66                 print STDERR "Unable to write to '$config_file.bak'; bailed out.\n";
67     }
68
69         $opensrf_config->toFile($config_file) or
70                 die "ERROR: Failed to update the configuration file '$config_file'\n";
71 }
72
73 sub create_schema() {
74 # Extracts the info from opensrf.xml and builds the db by calling build-db.sh
75         my $host = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/host/text()";
76         my $port = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/port/text()";
77         my $dbname = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/db/text()";
78         my $user = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/user/text()";
79         my $pw = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/pw/text()";
80
81         my $parser = XML::LibXML->new();
82         my $opensrf_config = $parser->parse_file($config_file);
83
84         chdir(dirname($build_db_sh));
85         system(File::Spec->catfile('.', basename($build_db_sh)) . " " .
86                 $opensrf_config->findnodes($host) ." ". 
87                 $opensrf_config->findnodes($port) ." ". 
88                 $opensrf_config->findnodes($dbname) ." ". 
89                 $opensrf_config->findnodes($user) ." ".
90                 $opensrf_config->findnodes($pw));
91         chdir($script_dir);
92 }
93
94 my $cschema = '';
95 my $uconfig = '';
96 my %settings = ();
97
98 GetOptions("create-schema" => \$cschema, 
99                 "update-config" => \$uconfig,
100                 "config-file=s" => \$config_file,
101                 "build-db-file=s" => \$build_db_sh,
102                 "service=s" => \@services,
103                 "user=s" => \$settings{'user'},
104                 "password=s" => \$settings{'pw'},
105                 "database=s" => \$settings{'db'},
106                 "hostname=s" => \$settings{'host'},
107                 "port=i" => \$settings{'port'}, 
108                 "help" => \$help
109 );
110
111 if (grep(/^all$/, @services)) {
112         @services = qw/reporter open-ils.cstore open-ils.storage open-ils.reporter-store/;
113 }
114
115 my $eg_config = File::Spec->catfile($script_dir, '../extras/eg_config');
116 if ($config_file eq '') { 
117         my @temp = `$eg_config --sysconfdir`;
118         chomp $temp[0];
119         $config_file = File::Spec->catfile($temp[0], "opensrf.xml");
120 }
121 if ($build_db_sh eq '') {
122         $build_db_sh = File::Spec->catfile($script_dir, '../sql/Pg/build-db.sh');
123 }
124 unless (-e $build_db_sh) { die "Error: $build_db_sh does not exist. \n"; }
125 unless (-e $config_file) { die "Error: $config_file does not exist. \n"; }
126 if ($uconfig) { update_config(\@services, \%settings); }
127 if ($cschema) { create_schema(); }
128
129 if ((!$cschema && !$uconfig) || $help) {
130         print <<HERE;
131
132 SYNOPSIS
133     eg_db_config.pl [OPTION] ... [COMMAND] ... [CONFIG OPTIONS]
134
135 DESCRIPTION
136     Creates or recreates the Evergreen database schema based on the settings
137     in the opensrf.xml configuration file.
138
139     Manipulates the configuration file 
140
141 OPTIONS
142     --config-file
143         specifies the opensrf.xml file. Defaults to /openils/conf/opensrf.xml
144
145     --build-db-file
146         specifies the script that creates the database schema. Defaults to
147         Open-ILS/src/sql/pg/build-db.sh
148
149 COMMANDS
150     --create-schema
151         Create the Evergreen database schema according to the settings in
152         the file specified by --config-file.  
153
154     --update-config
155         Configure Evergreen database settings in the file specified by
156         --build-db-file.  
157
158 SERVICE OPTIONS
159     --service
160         Specify "all" or one or more of the following services to update:
161             * reporter
162             * open-ils.cstore
163             * open-ils.storage
164             * open-ils.reporter-store
165     
166 DATABASE CONFIGURATION OPTIONS
167      --user            username for the database 
168
169      --password        password for the user 
170
171      --database        name of the database 
172
173      --hostname        name or address of the database host 
174
175      --port            port number for database access
176 HERE
177 }