]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/eg_db_config.pl
695602c1542bce488681b0cdd654203cf5c3fa49
[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-2009 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 $bootstrap_file = '';
31 my $offline_file = '';
32 my $prefix = '';
33 my $sysconfdir = '';
34 my @services;
35
36 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
37
38 # Get the directory for this script
39 my $script_dir = dirname($0);
40
41 sub update_config {
42         # Puts command line specified settings into xml file
43         my ($services, $settings) = @_;
44
45         my $parser = XML::LibXML->new();
46         my $opensrf_config = $parser->parse_file($config_file);
47
48         if (@$services) {
49                 foreach my $service (@$services) {
50                         foreach my $key (keys %$settings) {
51                                 next unless $settings->{$key};
52                                 my @node;
53
54                                 if ($service eq 'state_store') {
55                                         (@node) = $opensrf_config->findnodes("//state_store/$key/text()");
56                                 } else {
57                                         (@node) = $opensrf_config->findnodes("//$service//database/$key/text()");
58                                 }
59
60                                 foreach (@node) {
61                                         $_->setData($settings->{$key});
62                                 }
63                         }
64
65                 }
66         }
67
68         my $timestamp = sprintf("%d.%d.%d.%d.%d.%d",
69                 $year + 1900, $mon +1, $mday, $hour, $min, $sec);
70         if (copy($config_file, "$config_file.$timestamp")) {
71                 print "Backed up original configuration file to '$config_file.$timestamp'\n";
72         } else {
73                 print STDERR "Unable to write to '$config_file.$timestamp'; bailed out.\n";
74         }
75
76         $opensrf_config->toFile($config_file) or
77                 die "ERROR: Failed to update the configuration file '$config_file'\n";
78 }
79
80 # write out the DB bootstrapping config
81 sub create_db_bootstrap {
82         my ($setup, $settings) = @_;
83
84         open(FH, '>', $setup) or die "Could not write database setup to $setup\n";
85
86         print "Writing database bootstrapping configuration to $setup\n";
87
88         printf FH "\$main::config{dsn} = 'dbi:Pg:host=%s;dbname=%s;port=%d';\n",
89                 $settings->{host}, $settings->{db}, $settings->{port};
90
91         printf FH "\$main::config{usr} = '%s';\n", $settings->{user};
92         printf FH "\$main::config{pw} = '%s';\n", $settings->{pw};
93         
94         print FH "\$main::config{index} = 'config.cgi';\n";
95         close(FH);
96 }
97
98 # write out the offline config
99 sub create_offline_config {
100         my ($setup, $settings) = @_;
101
102         open(FH, '>', $setup) or die "Could not write offline database setup to $setup\n";
103
104         print "Writing offline database configuration to $setup\n";
105
106         printf FH "\$main::config{base_dir} = '%s/var/data/offline/';\n", $prefix;
107         printf FH "\$main::config{bootstrap} = '%s/opensrf_core.xml';\n", $sysconfdir;
108
109         printf FH "\$main::config{dsn} = 'dbi:Pg:host=%s;dbname=%s;port=%d';\n",
110                 $settings->{host}, $settings->{db}, $settings->{port};
111
112         printf FH "\$main::config{usr} = '%s';\n", $settings->{user};
113         printf FH "\$main::config{pw} = '%s';\n", $settings->{pw};
114
115         close(FH);
116 }
117 # Extracts database settings from opensrf.xml
118 sub get_settings {
119         my $settings = shift;
120
121         my $host = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/host/text()";
122         my $port = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/port/text()";
123         my $dbname = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/db/text()";
124         my $user = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/user/text()";
125         my $pw = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/pw/text()";
126
127         my $parser = XML::LibXML->new();
128         my $opensrf_config = $parser->parse_file($config_file);
129
130         # If the user passed in settings at the command line,
131         # we don't want to override them
132         $settings->{host} = $settings->{host} || $opensrf_config->findnodes($host);
133         $settings->{port} = $settings->{port} || $opensrf_config->findnodes($port);
134         $settings->{db} = $settings->{db} || $opensrf_config->findnodes($dbname);
135         $settings->{user} = $settings->{user} || $opensrf_config->findnodes($user);
136         $settings->{pw} = $settings->{pw} || $opensrf_config->findnodes($pw);
137 }
138
139 # Creates the database schema by calling build-db.sh
140 sub create_schema {
141         my $settings = shift;
142
143         chdir(dirname($build_db_sh));
144         my $cmd = File::Spec->catfile('.', basename($build_db_sh)) . " " .
145                 $settings->{host} ." ".  $settings->{port} ." ". 
146                 $settings->{db} ." ".  $settings->{user} ." ". 
147                 $settings->{pw};
148         system($cmd);
149         chdir($script_dir);
150 }
151
152 my $bootstrap;
153 my $offline;
154 my $cschema;
155 my $uconfig;
156 my %settings;
157
158 GetOptions("create-schema" => \$cschema, 
159                 "create-bootstrap" => \$bootstrap,
160                 "create-offline" => \$offline,
161                 "update-config" => \$uconfig,
162                 "bootstrap-file=s" => \$bootstrap_file,
163                 "config-file=s" => \$config_file,
164                 "build-db-file=s" => \$build_db_sh,
165                 "service=s" => \@services,
166                 "user=s" => \$settings{'user'},
167                 "password=s" => \$settings{'pw'},
168                 "database=s" => \$settings{'db'},
169                 "hostname=s" => \$settings{'host'},
170                 "port=i" => \$settings{'port'}, 
171                 "help" => \$help
172 );
173
174 if (grep(/^all$/, @services)) {
175         @services = qw/reporter open-ils.cstore open-ils.pcrud open-ils.storage open-ils.reporter-store state_store/;
176 }
177
178 my $eg_config = File::Spec->catfile($script_dir, '../extras/eg_config');
179
180 if (!$config_file) { 
181         my @temp = `$eg_config --sysconfdir`;
182         chomp $temp[0];
183         $sysconfdir = $temp[0];
184         $config_file = File::Spec->catfile($sysconfdir, "opensrf.xml");
185 }
186
187 if (!$prefix) {
188         my @temp = `$eg_config --prefix`;
189         chomp $temp[0];
190         $prefix = $temp[0];
191 }
192
193 if (!$build_db_sh) {
194         $build_db_sh = File::Spec->catfile($script_dir, '../sql/Pg/build-db.sh');
195 }
196
197 if (!$bootstrap_file) {
198         $bootstrap_file = File::Spec->catfile($sysconfdir, 'live-db-setup.pl');
199 }
200
201 if (!$offline_file) {
202         $offline_file = File::Spec->catfile($sysconfdir, 'offline-config.pl');
203 }
204
205 unless (-e $build_db_sh) { die "Error: $build_db_sh does not exist. \n"; }
206 unless (-e $config_file) { die "Error: $config_file does not exist. \n"; }
207
208 if ($uconfig) { update_config(\@services, \%settings); }
209
210 # Get our settings from the config file
211 get_settings(\%settings);
212
213 if ($cschema) { create_schema(\%settings); }
214 if ($bootstrap) { create_db_bootstrap($bootstrap_file, \%settings); }
215 if ($offline) { create_offline_config($offline_file, \%settings); }
216
217 if ((!$cschema && !$uconfig && !$bootstrap && !$offline) || $help) {
218         print <<HERE;
219
220 SYNOPSIS
221     eg_db_config.pl [OPTION] ... [COMMAND] ... [CONFIG OPTIONS]
222
223 DESCRIPTION
224     Creates or recreates the Evergreen database schema based on the settings
225     in the opensrf.xml configuration file.
226
227     Manipulates the configuration file 
228
229 OPTIONS
230     --config-file
231         specifies the opensrf.xml file. Defaults to /openils/conf/opensrf.xml
232
233     --bootstrap-file
234         specifies the database bootstrap file required by the CGI setup
235         interface. Defaults to /openils/conf/live-db-setup.pl
236
237     --build-db-file
238         specifies the script that creates the database schema. Defaults to
239         Open-ILS/src/sql/pg/build-db.sh
240
241     --offline-file
242         specifies the offline database settings file required by the offline
243         data uploader. Defaults to /openils/conf/offline-config.pl
244
245 COMMANDS
246     --update-config
247         Configures Evergreen database settings in the file specified by
248         --build-db-file.  
249
250     --create-bootstrap
251         Creates the database bootstrap file required by the CGI setup interface
252
253     --create-offline
254         Creates the database setting file required by the offline data uploader
255
256     --create-schema
257         Creates the Evergreen database schema according to the settings in
258         the file specified by --config-file.  
259
260 SERVICE OPTIONS
261     --service
262         Specify "all" or one or more of the following services to update:
263             * reporter
264             * open-ils.cstore
265             * open-ils.pcrud
266             * open-ils.storage
267             * open-ils.reporter-store
268             * state_store
269     
270 DATABASE CONFIGURATION OPTIONS
271     --user            username for the database 
272
273     --password        password for the user 
274
275     --database        name of the database 
276
277     --hostname        name or address of the database host 
278
279     --port            port number for database access
280
281 EXAMPLES
282    This script is normally used during the initial installation and
283    configuration process.
284
285    For a single server install, or an install with one web/application
286    server and one database server, you will typically want to invoke this
287    script with a complete set of commands:
288
289    perl Open-ILS/src/support-scripts/eg_db_config.pl --update-config \
290        --service all --create-schema --create-bootstrap --create-offline \
291        --user evergreen --password evergreen --hostname localhost --port 5432 \
292        --database evergreen 
293
294    To update the configuration for a single service - for example, if you
295    replicated a database for reporting purposes - just issue the
296    --update-config command with the service identified and the changed
297    database parameters specified:
298
299    perl Open-ILS/src/support-scripts/eg_db_config.pl --update-config \
300        --service reporter --hostname foobar --password newpass
301
302 HERE
303 }