2 # eg_db_config.pl -- configure Evergreen database settings and create schema
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>
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 $bootstrap_file = '';
33 # Get the directory for this script
34 my $script_dir = dirname($0);
37 # Puts command line specified settings into xml file
38 my ($services, $settings) = @_;
40 my $parser = XML::LibXML->new();
41 my $opensrf_config = $parser->parse_file($config_file);
44 foreach my $service (@$services) {
45 foreach my $key (keys %$settings) {
46 next unless $settings->{$key};
47 my(@node) = $opensrf_config->findnodes("//$service//database/$key/text()");
49 $_->setData($settings->{$key});
56 foreach my $key (keys %$settings) {
57 my(@node) = $opensrf_config->findnodes("//database/$key/text()");
59 $_->setData($settings->{$key});
64 if (copy($config_file, "$config_file.bak")) {
65 print "Backed up original configuration file to '$config_file.bak'\n";
67 print STDERR "Unable to write to '$config_file.bak'; bailed out.\n";
70 $opensrf_config->toFile($config_file) or
71 die "ERROR: Failed to update the configuration file '$config_file'\n";
74 # write out the DB bootstrapping config
75 sub create_db_bootstrap {
76 my ($setup, $settings) = @_;
78 open(FH, '>', $setup) or die "Could not write database setup to $setup\n";
80 print "Writing database bootstrapping configuration to $setup...\n";
82 printf FH "\$main::config{dsn} = 'dbi:Pg:host=%s;dbname=%s;port=%d';\n",
83 $settings->{host}, $settings->{db}, $settings->{port};
85 printf FH "\$main::config{usr} = '%s';\n", $settings->{user};
86 printf FH "\$main::config{pw} = '%s';\n", $settings->{pw};
88 print FH "\$main::config{index} = 'config.cgi';\n";
92 # Extracts database settings from opensrf.xml
96 my $host = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/host/text()";
97 my $port = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/port/text()";
98 my $dbname = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/db/text()";
99 my $user = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/user/text()";
100 my $pw = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/pw/text()";
102 my $parser = XML::LibXML->new();
103 my $opensrf_config = $parser->parse_file($config_file);
105 $settings->{host} = $opensrf_config->findnodes($host);
106 $settings->{port} = $opensrf_config->findnodes($port);
107 $settings->{db} = $opensrf_config->findnodes($dbname);
108 $settings->{user} = $opensrf_config->findnodes($user);
109 $settings->{pw} = $opensrf_config->findnodes($pw);
112 # Creates the database schema by calling build-db.sh
114 my $settings = shift;
116 chdir(dirname($build_db_sh));
117 system(File::Spec->catfile('.', basename($build_db_sh)) . " " .
118 $settings->{host} ." ". $settings->{port} ." ".
119 $settings->{db} ." ". $settings->{user} ." ".
129 GetOptions("create-schema" => \$cschema,
130 "create-bootstrap" => \$bootstrap,
131 "update-config" => \$uconfig,
132 "bootstrap-file=s" => \$bootstrap_file,
133 "config-file=s" => \$config_file,
134 "build-db-file=s" => \$build_db_sh,
135 "service=s" => \@services,
136 "user=s" => \$settings{'user'},
137 "password=s" => \$settings{'pw'},
138 "database=s" => \$settings{'db'},
139 "hostname=s" => \$settings{'host'},
140 "port=i" => \$settings{'port'},
144 if (grep(/^all$/, @services)) {
145 @services = qw/reporter open-ils.cstore open-ils.storage open-ils.reporter-store/;
148 my $eg_config = File::Spec->catfile($script_dir, '../extras/eg_config');
151 my @temp = `$eg_config --sysconfdir`;
153 $config_file = File::Spec->catfile($temp[0], "opensrf.xml");
157 $build_db_sh = File::Spec->catfile($script_dir, '../sql/Pg/build-db.sh');
160 if (!$bootstrap_file) {
161 $bootstrap_file = ('/openils/var/cgi-bin/live-db-setup.pl');
164 unless (-e $build_db_sh) { die "Error: $build_db_sh does not exist. \n"; }
165 unless (-e $config_file) { die "Error: $config_file does not exist. \n"; }
167 if ($uconfig) { update_config(\@services, \%settings); }
169 # Get our settings from the config file
170 get_settings(\%settings);
172 if ($cschema) { create_schema(\%settings); }
173 if ($bootstrap) { create_db_bootstrap($bootstrap_file, \%settings); }
175 if ((!$cschema && !$uconfig && !$bootstrap) || $help) {
179 eg_db_config.pl [OPTION] ... [COMMAND] ... [CONFIG OPTIONS]
182 Creates or recreates the Evergreen database schema based on the settings
183 in the opensrf.xml configuration file.
185 Manipulates the configuration file
189 specifies the opensrf.xml file. Defaults to /openils/conf/opensrf.xml
192 specifies the database bootstrap file required by the CGI setup interface
195 specifies the script that creates the database schema. Defaults to
196 Open-ILS/src/sql/pg/build-db.sh
200 Configures Evergreen database settings in the file specified by
204 Creates the database bootstrap file required by the CGI setup interface
207 Creates the Evergreen database schema according to the settings in
208 the file specified by --config-file.
212 Specify "all" or one or more of the following services to update:
216 * open-ils.reporter-store
218 DATABASE CONFIGURATION OPTIONS
219 --user username for the database
221 --password password for the user
223 --database name of the database
225 --hostname name or address of the database host
227 --port port number for database access
230 This script is normally used during the initial installation and
231 configuration process.
233 For a single server install, or an install with one web/application
234 server and one database server, you will typically want to invoke this
235 script with a complete set of commands:
237 perl Open-ILS/src/support-scripts/eg_db_config.pl --update-config \
238 --service all --create-schema --create-bootstrap \
239 --user evergreen --password evergreen --hostname localhost --port 5432 \
242 To update the configuration for a single service - for example, if you
243 replicated a database for reporting purposes - just issue the
244 --update-config command with the service identified and the changed
245 database parameters specified:
247 perl Open-ILS/src/support-scripts/eg_db_config.pl --update-config \
248 --service reporter --hostname foobar --password newpass