2 # eg_db_config.pl -- configure Evergreen database settings and create schema
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>
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.
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.
20 use strict; use warnings;
27 my ($dbhost, $dbport, $dbname, $dbuser, $dbpw, $help);
30 my $offline_file = '';
35 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
37 # Get the directory for this script
38 my $script_dir = dirname($0);
41 # Puts command line specified settings into xml file
42 my ($services, $settings) = @_;
44 my $parser = XML::LibXML->new();
45 my $opensrf_config = $parser->parse_file($config_file);
48 foreach my $service (@$services) {
49 foreach my $key (keys %$settings) {
50 next unless $settings->{$key};
53 if ($service eq 'state_store') {
54 (@node) = $opensrf_config->findnodes("//state_store/$key/text()");
56 (@node) = $opensrf_config->findnodes("//$service//database/$key/text()");
60 $_->setData($settings->{$key});
67 my $timestamp = sprintf("%d.%d.%d.%d.%d.%d",
68 $year + 1900, $mon +1, $mday, $hour, $min, $sec);
69 if (copy($config_file, "$config_file.$timestamp")) {
70 print "Backed up original configuration file to '$config_file.$timestamp'\n";
72 print STDERR "Unable to write to '$config_file.$timestamp'; bailed out.\n";
75 $opensrf_config->toFile($config_file) or
76 die "ERROR: Failed to update the configuration file '$config_file'\n";
79 # write out the offline config
80 sub create_offline_config {
81 my ($setup, $settings) = @_;
83 open(FH, '>', $setup) or die "Could not write offline database setup to $setup\n";
85 print "Writing offline database configuration to $setup\n";
87 printf FH "\$main::config{base_dir} = '%s/var/data/offline/';\n", $prefix;
88 printf FH "\$main::config{bootstrap} = '%s/opensrf_core.xml';\n", $sysconfdir;
90 printf FH "\$main::config{dsn} = 'dbi:Pg:host=%s;dbname=%s;port=%d';\n",
91 $settings->{host}, $settings->{db}, $settings->{port};
93 printf FH "\$main::config{usr} = '%s';\n", $settings->{user};
94 printf FH "\$main::config{pw} = '%s';\n", $settings->{pw};
98 # Extracts database settings from opensrf.xml
100 my $settings = shift;
102 my $host = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/host/text()";
103 my $port = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/port/text()";
104 my $dbname = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/db/text()";
105 my $user = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/user/text()";
106 my $pw = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/pw/text()";
108 my $parser = XML::LibXML->new();
109 my $opensrf_config = $parser->parse_file($config_file);
111 # If the user passed in settings at the command line,
112 # we don't want to override them
113 $settings->{host} = $settings->{host} || $opensrf_config->findnodes($host);
114 $settings->{port} = $settings->{port} || $opensrf_config->findnodes($port);
115 $settings->{db} = $settings->{db} || $opensrf_config->findnodes($dbname);
116 $settings->{user} = $settings->{user} || $opensrf_config->findnodes($user);
117 $settings->{pw} = $settings->{pw} || $opensrf_config->findnodes($pw);
120 # Creates the database schema by calling build-db.sh
122 my $settings = shift;
124 chdir(dirname($build_db_sh));
125 my $cmd = File::Spec->catfile('.', basename($build_db_sh)) . " " .
126 $settings->{host} ." ". $settings->{port} ." ".
127 $settings->{db} ." ". $settings->{user} ." ".
138 GetOptions("create-schema" => \$cschema,
139 "create-offline" => \$offline,
140 "update-config" => \$uconfig,
141 "config-file=s" => \$config_file,
142 "build-db-file=s" => \$build_db_sh,
143 "service=s" => \@services,
144 "user=s" => \$settings{'user'},
145 "password=s" => \$settings{'pw'},
146 "database=s" => \$settings{'db'},
147 "hostname=s" => \$settings{'host'},
148 "port=i" => \$settings{'port'},
152 if (grep(/^all$/, @services)) {
153 @services = qw/reporter open-ils.cstore open-ils.pcrud open-ils.storage open-ils.reporter-store state_store/;
156 my $eg_config = File::Spec->catfile($script_dir, '../extras/eg_config');
159 my @temp = `$eg_config --sysconfdir`;
161 $sysconfdir = $temp[0];
162 $config_file = File::Spec->catfile($sysconfdir, "opensrf.xml");
166 my @temp = `$eg_config --prefix`;
172 $build_db_sh = File::Spec->catfile($script_dir, '../sql/Pg/build-db.sh');
175 if (!$offline_file) {
176 $offline_file = File::Spec->catfile($sysconfdir, 'offline-config.pl');
179 unless (-e $build_db_sh) { die "Error: $build_db_sh does not exist. \n"; }
180 unless (-e $config_file) { die "Error: $config_file does not exist. \n"; }
182 if ($uconfig) { update_config(\@services, \%settings); }
184 # Get our settings from the config file
185 get_settings(\%settings);
187 if ($cschema) { create_schema(\%settings); }
188 if ($offline) { create_offline_config($offline_file, \%settings); }
190 if ((!$cschema && !$uconfig && !$offline) || $help) {
194 eg_db_config.pl [OPTION] ... [COMMAND] ... [CONFIG OPTIONS]
197 Creates or recreates the Evergreen database schema based on the settings
198 in the opensrf.xml configuration file.
200 Manipulates the configuration file
204 specifies the opensrf.xml file. Defaults to /openils/conf/opensrf.xml
207 specifies the script that creates the database schema. Defaults to
208 Open-ILS/src/sql/pg/build-db.sh
211 specifies the offline database settings file required by the offline
212 data uploader. Defaults to /openils/conf/offline-config.pl
216 Configures Evergreen database settings in the file specified by
220 Creates the database setting file required by the offline data uploader
223 Creates the Evergreen database schema according to the settings in
224 the file specified by --config-file.
228 Specify "all" or one or more of the following services to update:
233 * open-ils.reporter-store
236 DATABASE CONFIGURATION OPTIONS
237 --user username for the database
239 --password password for the user
241 --database name of the database
243 --hostname name or address of the database host
245 --port port number for database access
248 This script is normally used during the initial installation and
249 configuration process.
251 For a single server install, or an install with one web/application
252 server and one database server, you will typically want to invoke this
253 script with a complete set of commands:
255 perl Open-ILS/src/support-scripts/eg_db_config.pl --update-config \
256 --service all --create-schema --create-offline \
257 --user evergreen --password evergreen --hostname localhost --port 5432 \
260 To update the configuration for a single service - for example, if you
261 replicated a database for reporting purposes - just issue the
262 --update-config command with the service identified and the changed
263 database parameters specified:
265 perl Open-ILS/src/support-scripts/eg_db_config.pl --update-config \
266 --service reporter --hostname foobar --password newpass