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;
28 my ($dbhost, $dbport, $dbname, $dbuser, $dbpw, $help, $admin_user, $admin_pw);
31 my $offline_file = '';
36 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
38 # Get the directory for this script
39 my $script_dir = dirname($0);
43 =item update_config() - Puts command line specified settings into xml file
46 my ($services, $settings) = @_;
48 my $parser = XML::LibXML->new();
49 my $opensrf_config = $parser->parse_file($config_file);
52 foreach my $service (@$services) {
53 foreach my $key (keys %$settings) {
54 next unless $settings->{$key};
57 if ($service eq 'state_store') {
58 (@node) = $opensrf_config->findnodes("//state_store/$key/text()");
60 (@node) = $opensrf_config->findnodes("//$service//database/$key/text()");
64 $_->setData($settings->{$key});
71 my $timestamp = sprintf("%d.%d.%d.%d.%d.%d",
72 $year + 1900, $mon +1, $mday, $hour, $min, $sec);
73 if (copy($config_file, "$config_file.$timestamp")) {
74 print "Backed up original configuration file to '$config_file.$timestamp'\n";
76 print STDERR "Unable to write to '$config_file.$timestamp'; bailed out.\n";
79 $opensrf_config->toFile($config_file) or
80 die "ERROR: Failed to update the configuration file '$config_file'\n";
83 =item create_offline_config() - Write out the offline config
85 sub create_offline_config {
86 my ($setup, $settings) = @_;
88 open(FH, '>', $setup) or die "Could not write offline database setup to $setup\n";
90 print "Writing offline database configuration to $setup\n";
92 printf FH "\$main::config{base_dir} = '%s/var/data/offline/';\n", $prefix;
93 printf FH "\$main::config{bootstrap} = '%s/opensrf_core.xml';\n", $sysconfdir;
95 printf FH "\$main::config{dsn} = 'dbi:Pg:host=%s;dbname=%s;port=%d';\n",
96 $settings->{host}, $settings->{db}, $settings->{port};
98 printf FH "\$main::config{usr} = '%s';\n", $settings->{user};
99 printf FH "\$main::config{pw} = '%s';\n", $settings->{pw};
104 =item get_settings() - Extracts database settings from opensrf.xml
107 my $settings = shift;
109 my $host = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/host/text()";
110 my $port = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/port/text()";
111 my $dbname = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/db/text()";
112 my $user = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/user/text()";
113 my $pw = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/pw/text()";
115 my $parser = XML::LibXML->new();
116 my $opensrf_config = $parser->parse_file($config_file);
118 # If the user passed in settings at the command line,
119 # we don't want to override them
120 $settings->{host} = $settings->{host} || $opensrf_config->findnodes($host);
121 $settings->{port} = $settings->{port} || $opensrf_config->findnodes($port);
122 $settings->{db} = $settings->{db} || $opensrf_config->findnodes($dbname);
123 $settings->{user} = $settings->{user} || $opensrf_config->findnodes($user);
124 $settings->{pw} = $settings->{pw} || $opensrf_config->findnodes($pw);
127 =item create_schema() - Creates the database schema by calling build-db.sh
130 my $settings = shift;
132 chdir(dirname($build_db_sh));
133 my $cmd = File::Spec->catfile('.', basename($build_db_sh)) . " " .
134 $settings->{host} ." ". $settings->{port} ." ".
135 $settings->{db} ." ". $settings->{user} ." ".
141 =item set_admin_account() - Sets the administrative user's user name and password
143 sub set_admin_account {
144 my $admin_user = shift;
145 my $admin_pw = shift;
146 my $settings = shift;
148 my $dbh = DBI->connect('dbi:Pg:dbname=' . $settings->{db} .
149 ';host=' . $settings->{host} . ';port=' . $settings->{port} . ';',
150 $settings->{user} . "", $settings->{pw} . "", {AutoCommit => 1}
153 print STDERR "Could not connect to database to set admin account. ";
154 print STDERR "Error was " . $dbh->errstr . "\n";
157 my $stmt = $dbh->prepare("UPDATE actor.usr SET usrname = ?, passwd = ? WHERE id = 1");
158 $stmt->execute(($admin_user, $admin_pw));
160 print STDERR "Failed to set admin account. ";
161 print STDERR "Error was " . $dbh->errstr . "\n";
171 GetOptions("create-schema" => \$cschema,
172 "create-offline" => \$offline,
173 "update-config" => \$uconfig,
174 "config-file=s" => \$config_file,
175 "build-db-file=s" => \$build_db_sh,
176 "admin-user=s" => \$admin_user,
177 "admin-password=s" => \$admin_pw,
178 "service=s" => \@services,
179 "user=s" => \$settings{'user'},
180 "password=s" => \$settings{'pw'},
181 "database=s" => \$settings{'db'},
182 "hostname=s" => \$settings{'host'},
183 "port=i" => \$settings{'port'},
187 if (grep(/^all$/, @services)) {
188 @services = qw/reporter open-ils.cstore open-ils.pcrud open-ils.storage open-ils.reporter-store state_store/;
191 my $eg_config = File::Spec->catfile($script_dir, '../extras/eg_config');
194 my @temp = `$eg_config --sysconfdir`;
196 $sysconfdir = $temp[0];
197 $config_file = File::Spec->catfile($sysconfdir, "opensrf.xml");
201 my @temp = `$eg_config --prefix`;
207 $build_db_sh = File::Spec->catfile($script_dir, '../sql/Pg/build-db.sh');
210 if (!$offline_file) {
211 $offline_file = File::Spec->catfile($sysconfdir, 'offline-config.pl');
214 unless (-e $build_db_sh) { die "Error: $build_db_sh does not exist. \n"; }
215 unless (-e $config_file) { die "Error: $config_file does not exist. \n"; }
217 if ($uconfig) { update_config(\@services, \%settings); }
219 # Get our settings from the config file
220 get_settings(\%settings);
222 if ($cschema) { create_schema(\%settings); }
223 if ($admin_user && $admin_pw) {
224 set_admin_account($admin_user, $admin_pw, \%settings);
226 if ($offline) { create_offline_config($offline_file, \%settings); }
228 if ((!$cschema && !$uconfig && !$offline && !$admin_pw) || $help) {
232 eg_db_config.pl [OPTION] ... [COMMAND] ... [CONFIG OPTIONS]
235 Creates or recreates the Evergreen database schema based on the settings
236 in the opensrf.xml configuration file.
238 Manipulates the configuration file
242 specifies the opensrf.xml file. Defaults to /openils/conf/opensrf.xml
245 specifies the script that creates the database schema. Defaults to
246 Open-ILS/src/sql/pg/build-db.sh
249 specifies the offline database settings file required by the offline
250 data uploader. Defaults to /openils/conf/offline-config.pl
254 Configures Evergreen database settings in the file specified by
258 Creates the database setting file required by the offline data uploader
261 Creates the Evergreen database schema according to the settings in
262 the file specified by --config-file.
266 Specify "all" or one or more of the following services to update:
271 * open-ils.reporter-store
274 DATABASE CONFIGURATION OPTIONS
275 --user username for the database
277 --password password for the user
279 --database name of the database
281 --hostname name or address of the database host
283 --port port number for database access
285 --admin-user administration user's user name
287 --admin-pass administration user's password
290 This script is normally used during the initial installation and
291 configuration process. This creates the database schema, sets
292 the administration user's user name and password, and modifies your
293 configuration files to include the correct database connection
296 For a single server install, or an install with one web/application
297 server and one database server, you will typically want to invoke this
298 script with a complete set of commands:
300 perl Open-ILS/src/support-scripts/eg_db_config.pl --update-config \
301 --service all --create-schema --create-offline \
302 --user <db-user> --password <db-pass> --hostname localhost --port 5432 \
303 --database evergreen --admin-user <admin-user> --admin-pass <admin-pass>
305 To update the configuration for a single service - for example, if you
306 replicated a database for reporting purposes - just issue the
307 --update-config command with the service identified and the changed
308 database parameters specified:
310 perl Open-ILS/src/support-scripts/eg_db_config.pl --update-config \
311 --service reporter --hostname foobar --password newpass