]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/eg_db_config.pl
Add OILS_SIP_MSG_BILL_ERR for when an error occurs getting bills.
[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 use DBI;
27
28 my ($dbhost, $dbport, $dbname, $dbuser, $dbpw, $help, $admin_user, $admin_pw);
29 my $config_file = '';
30 my $build_db_sh = '';
31 my $offline_file = '';
32 my $prefix = '';
33 my $sysconfdir = '';
34 my @services;
35
36 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
37
38 # Get the directory for this script
39 my $script_dir = dirname($0);
40
41 =over
42
43 =item update_config() - Puts command line specified settings into xml file
44 =cut
45 sub update_config {
46         my ($services, $settings) = @_;
47
48         my $parser = XML::LibXML->new();
49         my $opensrf_config = $parser->parse_file($config_file);
50
51         if (@$services) {
52                 foreach my $service (@$services) {
53                         foreach my $key (keys %$settings) {
54                                 next unless $settings->{$key};
55                                 my @node;
56
57                                 if ($service eq 'state_store') {
58                                         (@node) = $opensrf_config->findnodes("//state_store/$key/text()");
59                                 } else {
60                                         (@node) = $opensrf_config->findnodes("//$service//database/$key/text()");
61                                 }
62
63                                 foreach (@node) {
64                                         $_->setData($settings->{$key});
65                                 }
66                         }
67
68                 }
69         }
70
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";
75         } else {
76                 print STDERR "Unable to write to '$config_file.$timestamp'; bailed out.\n";
77         }
78
79         $opensrf_config->toFile($config_file) or
80                 die "ERROR: Failed to update the configuration file '$config_file'\n";
81 }
82
83 =item create_offline_config() - Write out the offline config
84 =cut
85 sub create_offline_config {
86         my ($setup, $settings) = @_;
87
88         open(FH, '>', $setup) or die "Could not write offline database setup to $setup\n";
89
90         print "Writing offline database configuration to $setup\n";
91
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;
94
95         printf FH "\$main::config{dsn} = 'dbi:Pg:host=%s;dbname=%s;port=%d';\n",
96                 $settings->{host}, $settings->{db}, $settings->{port};
97
98         printf FH "\$main::config{usr} = '%s';\n", $settings->{user};
99         printf FH "\$main::config{pw} = '%s';\n", $settings->{pw};
100
101         close(FH);
102 }
103
104 =item get_settings() - Extracts database settings from opensrf.xml
105 =cut
106 sub get_settings {
107         my $settings = shift;
108
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()";
114
115         my $parser = XML::LibXML->new();
116         my $opensrf_config = $parser->parse_file($config_file);
117
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);
125 }
126
127 =item create_schema() - Creates the database schema by calling build-db.sh
128 =cut
129 sub create_schema {
130         my $settings = shift;
131
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} ." ". 
136                 $settings->{pw};
137         system($cmd);
138         chdir($script_dir);
139 }
140
141 =item set_admin_account() - Sets the administrative user's user name and password
142 =cut
143 sub set_admin_account {
144         my $admin_user = shift;
145         my $admin_pw = shift;
146         my $settings = shift;
147
148         my $dbh = DBI->connect('dbi:Pg:dbname=' . $settings->{db} . 
149                 ';host=' . $settings->{host} . ';port=' . $settings->{port} . ';',
150                  $settings->{user} . "", $settings->{pw} . "", {AutoCommit => 1}
151         );
152         if ($dbh->err) {
153                 print STDERR "Could not connect to database to set admin account. ";
154                 print STDERR "Error was " . $dbh->errstr . "\n";
155                 return;
156         }
157         my $stmt = $dbh->prepare("UPDATE actor.usr SET usrname = ?, passwd = ? WHERE id = 1");
158         $stmt->execute(($admin_user, $admin_pw));
159         if ($dbh->err) {
160                 print STDERR "Failed to set admin account. ";
161                 print STDERR "Error was " . $dbh->errstr . "\n";
162                 return;
163         }
164 }
165
166 my $offline;
167 my $cschema;
168 my $uconfig;
169 my %settings;
170
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'}, 
184                 "help" => \$help
185 );
186
187 if (grep(/^all$/, @services)) {
188         @services = qw/reporter open-ils.cstore open-ils.pcrud open-ils.storage open-ils.reporter-store state_store/;
189 }
190
191 my $eg_config = File::Spec->catfile($script_dir, '../extras/eg_config');
192
193 if (!$config_file) { 
194         my @temp = `$eg_config --sysconfdir`;
195         chomp $temp[0];
196         $sysconfdir = $temp[0];
197         $config_file = File::Spec->catfile($sysconfdir, "opensrf.xml");
198 }
199
200 if (!$prefix) {
201         my @temp = `$eg_config --prefix`;
202         chomp $temp[0];
203         $prefix = $temp[0];
204 }
205
206 if (!$build_db_sh) {
207         $build_db_sh = File::Spec->catfile($script_dir, '../sql/Pg/build-db.sh');
208 }
209
210 if (!$offline_file) {
211         $offline_file = File::Spec->catfile($sysconfdir, 'offline-config.pl');
212 }
213
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"; }
216
217 if ($uconfig) { update_config(\@services, \%settings); }
218
219 # Get our settings from the config file
220 get_settings(\%settings);
221
222 if ($cschema) { create_schema(\%settings); }
223 if ($admin_user && $admin_pw) {
224         set_admin_account($admin_user, $admin_pw, \%settings);
225 }
226 if ($offline) { create_offline_config($offline_file, \%settings); }
227
228 if ((!$cschema && !$uconfig && !$offline && !$admin_pw) || $help) {
229         print <<HERE;
230
231 SYNOPSIS
232     eg_db_config.pl [OPTION] ... [COMMAND] ... [CONFIG OPTIONS]
233
234 DESCRIPTION
235     Creates or recreates the Evergreen database schema based on the settings
236     in the opensrf.xml configuration file.
237
238     Manipulates the configuration file 
239
240 OPTIONS
241     --config-file
242         specifies the opensrf.xml file. Defaults to /openils/conf/opensrf.xml
243
244     --build-db-file
245         specifies the script that creates the database schema. Defaults to
246         Open-ILS/src/sql/pg/build-db.sh
247
248     --offline-file
249         specifies the offline database settings file required by the offline
250         data uploader. Defaults to /openils/conf/offline-config.pl
251
252 COMMANDS
253     --update-config
254         Configures Evergreen database settings in the file specified by
255         --build-db-file.  
256
257     --create-offline
258         Creates the database setting file required by the offline data uploader
259
260     --create-schema
261         Creates the Evergreen database schema according to the settings in
262         the file specified by --config-file.  
263
264 SERVICE OPTIONS
265     --service
266         Specify "all" or one or more of the following services to update:
267             * reporter
268             * open-ils.cstore
269             * open-ils.pcrud
270             * open-ils.storage
271             * open-ils.reporter-store
272             * state_store
273     
274 DATABASE CONFIGURATION OPTIONS
275     --user            username for the database 
276
277     --password        password for the user 
278
279     --database        name of the database 
280
281     --hostname        name or address of the database host 
282
283     --port            port number for database access
284
285     --admin-user      administration user's user name
286
287     --admin-pass      administration user's password
288
289 EXAMPLES
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
294    information.
295
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:
299
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> 
304
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:
309
310    perl Open-ILS/src/support-scripts/eg_db_config.pl --update-config \
311        --service reporter --hostname foobar --password newpass
312
313 HERE
314 }