From 2eedae060cbcb33ea171bc1fe0ed062be5924e5a Mon Sep 17 00:00:00 2001 From: dbs Date: Sat, 11 Oct 2008 02:47:18 +0000 Subject: [PATCH] Augment kbeswick's version of the script; a bit more tolerant of relative paths and such git-svn-id: svn://svn.open-ils.org/ILS/trunk@10822 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- Open-ILS/src/support-scripts/eg_db_config.pl | 177 +++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 Open-ILS/src/support-scripts/eg_db_config.pl diff --git a/Open-ILS/src/support-scripts/eg_db_config.pl b/Open-ILS/src/support-scripts/eg_db_config.pl new file mode 100644 index 0000000000..92222ff114 --- /dev/null +++ b/Open-ILS/src/support-scripts/eg_db_config.pl @@ -0,0 +1,177 @@ +#!/usr/bin/perl +# eg_db_config.pl -- configure Evergreen database settings and create schema +# vim:noet:ts=4:sw=4: +# +# Copyright (C) 2008 Equinox Software, Inc. +# Copyright (C) 2008 Laurentian University +# Author: Kevin Beswick +# Author: Dan Scott +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +use strict; use warnings; +use XML::LibXML; +use File::Copy; +use Getopt::Long; +use File::Spec; +use File::Basename; + +my ($dbhost, $dbport, $dbname, $dbuser, $dbpw, $help); +my $config_file = ''; +my $build_db_sh = ''; +my @services; + +# Get the directory for this script +my $script_dir = dirname($0); + +sub update_config { + # Puts command line specified settings into xml file + my ($services, $settings) = @_; + + my $parser = XML::LibXML->new(); + my $opensrf_config = $parser->parse_file($config_file); + + if (@$services) { + foreach my $service (@$services) { + foreach my $key (keys %$settings) { + next unless $settings->{$key}; + my(@node) = $opensrf_config->findnodes("//$service//database/$key/text()"); + foreach(@node) { + $_->setData($settings->{$key}); + } + } + + } + } + else { + foreach my $key (keys %$settings) { + my(@node) = $opensrf_config->findnodes("//database/$key/text()"); + foreach(@node) { + $_->setData($settings->{$key}); + } + } + } + + if (copy($config_file, "$config_file.bak")) { + print "Backed up original configuration file to '$config_file.bak'\n"; + } else { + print STDERR "Unable to write to '$config_file.bak'; bailed out.\n"; + } + + $opensrf_config->toFile($config_file) or + die "ERROR: Failed to update the configuration file '$config_file'\n"; +} + +sub create_schema() { +# Extracts the info from opensrf.xml and builds the db by calling build-db.sh + my $host = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/host/text()"; + my $port = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/port/text()"; + my $dbname = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/db/text()"; + my $user = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/user/text()"; + my $pw = "/opensrf/default/apps/open-ils.storage/app_settings/databases/database/pw/text()"; + + my $parser = XML::LibXML->new(); + my $opensrf_config = $parser->parse_file($config_file); + + chdir(dirname($build_db_sh)); + system(File::Spec->catfile('.', basename($build_db_sh)) . " " . + $opensrf_config->findnodes($host) ." ". + $opensrf_config->findnodes($port) ." ". + $opensrf_config->findnodes($dbname) ." ". + $opensrf_config->findnodes($user) ." ". + $opensrf_config->findnodes($pw)); + chdir($script_dir); +} + +my $cschema = ''; +my $uconfig = ''; +my %settings = (); + +GetOptions("create-schema" => \$cschema, + "update-config" => \$uconfig, + "config-file=s" => \$config_file, + "build-db-file=s" => \$build_db_sh, + "service=s" => \@services, + "user=s" => \$settings{'user'}, + "password=s" => \$settings{'pw'}, + "database=s" => \$settings{'db'}, + "hostname=s" => \$settings{'host'}, + "port=i" => \$settings{'port'}, + "help" => \$help +); + +if (grep(/^all$/, @services)) { + @services = qw/reporter open-ils.cstore open-ils.storage open-ils.reporter-store/; +} + +my $eg_config = File::Spec->catfile($script_dir, '../extras/eg_config'); +if ($config_file eq '') { + my @temp = `$eg_config --sysconfdir`; + chomp $temp[0]; + $config_file = File::Spec->catfile($temp[0], "opensrf.xml"); +} +if ($build_db_sh eq '') { + $build_db_sh = File::Spec->catfile($script_dir, '../sql/Pg/build-db.sh'); +} +unless (-e $build_db_sh) { die "Error: $build_db_sh does not exist. \n"; } +unless (-e $config_file) { die "Error: $config_file does not exist. \n"; } +if ($uconfig) { update_config(\@services, \%settings); } +if ($cschema) { create_schema(); } + +if ((!$cschema && !$uconfig) || $help) { + print <