]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/eg_db_config.pl
Remove the previously deprecated CGI interfaces for configuring the Evergreen system
[working/Evergreen.git] / Open-ILS / src / support-scripts / eg_db_config.pl
1 #!/usr/bin/perl
2 # eg_db_config.pl -- configure Evergreen database settings and create schema
3 # vim:noet:ts=4:sw=4:
4 #
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>
9 #
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.
14 #
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.
19
20 use strict; use warnings;
21 use XML::LibXML;
22 use File::Copy;
23 use Getopt::Long;
24 use File::Spec;
25 use File::Basename;
26
27 my ($dbhost, $dbport, $dbname, $dbuser, $dbpw, $help);
28 my $config_file = '';
29 my $build_db_sh = '';
30 my $offline_file = '';
31 my $prefix = '';
32 my $sysconfdir = '';
33 my @services;
34
35 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
36
37 # Get the directory for this script
38 my $script_dir = dirname($0);
39
40 sub update_config {
41         # Puts command line specified settings into xml file
42         my ($services, $settings) = @_;
43
44         my $parser = XML::LibXML->new();
45         my $opensrf_config = $parser->parse_file($config_file);
46
47         if (@$services) {
48                 foreach my $service (@$services) {
49                         foreach my $key (keys %$settings) {
50                                 next unless $settings->{$key};
51                                 my @node;
52
53                                 if ($service eq 'state_store') {
54                                         (@node) = $opensrf_config->findnodes("//state_store/$key/text()");
55                                 } else {
56                                         (@node) = $opensrf_config->findnodes("//$service//database/$key/text()");
57                                 }
58
59                                 foreach (@node) {
60                                         $_->setData($settings->{$key});
61                                 }
62                         }
63
64                 }
65         }
66
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";
71         } else {
72                 print STDERR "Unable to write to '$config_file.$timestamp'; bailed out.\n";
73         }
74
75         $opensrf_config->toFile($config_file) or
76                 die "ERROR: Failed to update the configuration file '$config_file'\n";
77 }
78
79 # write out the offline config
80 sub create_offline_config {
81         my ($setup, $settings) = @_;
82
83         open(FH, '>', $setup) or die "Could not write offline database setup to $setup\n";
84
85         print "Writing offline database configuration to $setup\n";
86
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;
89
90         printf FH "\$main::config{dsn} = 'dbi:Pg:host=%s;dbname=%s;port=%d';\n",
91                 $settings->{host}, $settings->{db}, $settings->{port};
92
93         printf FH "\$main::config{usr} = '%s';\n", $settings->{user};
94         printf FH "\$main::config{pw} = '%s';\n", $settings->{pw};
95
96         close(FH);
97 }
98 # Extracts database settings from opensrf.xml
99 sub get_settings {
100         my $settings = shift;
101
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()";
107
108         my $parser = XML::LibXML->new();
109         my $opensrf_config = $parser->parse_file($config_file);
110
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);
118 }
119
120 # Creates the database schema by calling build-db.sh
121 sub create_schema {
122         my $settings = shift;
123
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} ." ". 
128                 $settings->{pw};
129         system($cmd);
130         chdir($script_dir);
131 }
132
133 my $offline;
134 my $cschema;
135 my $uconfig;
136 my %settings;
137
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'}, 
149                 "help" => \$help
150 );
151
152 if (grep(/^all$/, @services)) {
153         @services = qw/reporter open-ils.cstore open-ils.pcrud open-ils.storage open-ils.reporter-store state_store/;
154 }
155
156 my $eg_config = File::Spec->catfile($script_dir, '../extras/eg_config');
157
158 if (!$config_file) { 
159         my @temp = `$eg_config --sysconfdir`;
160         chomp $temp[0];
161         $sysconfdir = $temp[0];
162         $config_file = File::Spec->catfile($sysconfdir, "opensrf.xml");
163 }
164
165 if (!$prefix) {
166         my @temp = `$eg_config --prefix`;
167         chomp $temp[0];
168         $prefix = $temp[0];
169 }
170
171 if (!$build_db_sh) {
172         $build_db_sh = File::Spec->catfile($script_dir, '../sql/Pg/build-db.sh');
173 }
174
175 if (!$offline_file) {
176         $offline_file = File::Spec->catfile($sysconfdir, 'offline-config.pl');
177 }
178
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"; }
181
182 if ($uconfig) { update_config(\@services, \%settings); }
183
184 # Get our settings from the config file
185 get_settings(\%settings);
186
187 if ($cschema) { create_schema(\%settings); }
188 if ($offline) { create_offline_config($offline_file, \%settings); }
189
190 if ((!$cschema && !$uconfig && !$offline) || $help) {
191         print <<HERE;
192
193 SYNOPSIS
194     eg_db_config.pl [OPTION] ... [COMMAND] ... [CONFIG OPTIONS]
195
196 DESCRIPTION
197     Creates or recreates the Evergreen database schema based on the settings
198     in the opensrf.xml configuration file.
199
200     Manipulates the configuration file 
201
202 OPTIONS
203     --config-file
204         specifies the opensrf.xml file. Defaults to /openils/conf/opensrf.xml
205
206     --build-db-file
207         specifies the script that creates the database schema. Defaults to
208         Open-ILS/src/sql/pg/build-db.sh
209
210     --offline-file
211         specifies the offline database settings file required by the offline
212         data uploader. Defaults to /openils/conf/offline-config.pl
213
214 COMMANDS
215     --update-config
216         Configures Evergreen database settings in the file specified by
217         --build-db-file.  
218
219     --create-offline
220         Creates the database setting file required by the offline data uploader
221
222     --create-schema
223         Creates the Evergreen database schema according to the settings in
224         the file specified by --config-file.  
225
226 SERVICE OPTIONS
227     --service
228         Specify "all" or one or more of the following services to update:
229             * reporter
230             * open-ils.cstore
231             * open-ils.pcrud
232             * open-ils.storage
233             * open-ils.reporter-store
234             * state_store
235     
236 DATABASE CONFIGURATION OPTIONS
237     --user            username for the database 
238
239     --password        password for the user 
240
241     --database        name of the database 
242
243     --hostname        name or address of the database host 
244
245     --port            port number for database access
246
247 EXAMPLES
248    This script is normally used during the initial installation and
249    configuration process.
250
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:
254
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 \
258        --database evergreen 
259
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:
264
265    perl Open-ILS/src/support-scripts/eg_db_config.pl --update-config \
266        --service reporter --hostname foobar --password newpass
267
268 HERE
269 }