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);
32 # Get the directory for this script
33 my $script_dir = dirname($0);
36 # Puts command line specified settings into xml file
37 my ($services, $settings) = @_;
39 my $parser = XML::LibXML->new();
40 my $opensrf_config = $parser->parse_file($config_file);
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()");
48 $_->setData($settings->{$key});
55 foreach my $key (keys %$settings) {
56 my(@node) = $opensrf_config->findnodes("//database/$key/text()");
58 $_->setData($settings->{$key});
63 if (copy($config_file, "$config_file.bak")) {
64 print "Backed up original configuration file to '$config_file.bak'\n";
66 print STDERR "Unable to write to '$config_file.bak'; bailed out.\n";
69 $opensrf_config->toFile($config_file) or
70 die "ERROR: Failed to update the configuration file '$config_file'\n";
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()";
81 my $parser = XML::LibXML->new();
82 my $opensrf_config = $parser->parse_file($config_file);
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));
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'},
111 if (grep(/^all$/, @services)) {
112 @services = qw/reporter open-ils.cstore open-ils.storage open-ils.reporter-store/;
115 my $eg_config = File::Spec->catfile($script_dir, '../extras/eg_config');
116 if ($config_file eq '') {
117 my @temp = `$eg_config --sysconfdir`;
119 $config_file = File::Spec->catfile($temp[0], "opensrf.xml");
121 if ($build_db_sh eq '') {
122 $build_db_sh = File::Spec->catfile($script_dir, '../sql/Pg/build-db.sh');
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(); }
129 if ((!$cschema && !$uconfig) || $help) {
133 eg_db_config.pl [OPTION] ... [COMMAND] ... [CONFIG OPTIONS]
136 Creates or recreates the Evergreen database schema based on the settings
137 in the opensrf.xml configuration file.
139 Manipulates the configuration file
143 specifies the opensrf.xml file. Defaults to /openils/conf/opensrf.xml
146 specifies the script that creates the database schema. Defaults to
147 Open-ILS/src/sql/pg/build-db.sh
151 Create the Evergreen database schema according to the settings in
152 the file specified by --config-file.
155 Configure Evergreen database settings in the file specified by
160 Specify "all" or one or more of the following services to update:
164 * open-ils.reporter-store
166 DATABASE CONFIGURATION OPTIONS
167 --user username for the database
169 --password password for the user
171 --database name of the database
173 --hostname name or address of the database host
175 --port port number for database access