[OPEN-ILS-DEV] PATCH: Throw away bootstrap.conf and use opensrf_core.xml

Dan Scott denials at gmail.com
Wed Jun 27 12:02:44 EDT 2007


Building on Nathan Eady's suggestion / intention from December
(http://list.georgialibraries.org/pipermail/open-ils-dev/2006-December/000177.html),
here is a patch that enables OpenSRF to avoid duplicating settings in
opensrf_core.xml and bootstrap.conf by having both Perl and C apps
read from opensrf_core.xml

Hope I'm not stepping on your toes, Nathan! This itch just had to be
scratched, though...

Attaching a diff (capturing all of the wide-ranging effects of the
change) as well as the replacement Config.pm file to make it easier to
review my (hackish) changes.

-- 
Dan Scott
Laurentian University
-------------- next part --------------
Index: src/perlmods/OpenSRF/Utils/Config.pm
===================================================================
--- src/perlmods/OpenSRF/Utils/Config.pm	(revision 976)
+++ src/perlmods/OpenSRF/Utils/Config.pm	(working copy)
@@ -26,58 +26,22 @@
 
 	$self = bless {}, $class;
 
-	my $lines = shift;
+	$self->_sub_builder('__id');
+	# Hard-code this to match old bootstrap.conf section name
+	$self->__id('bootstrap');
 
-	for my $line (@$lines) {
+	my $bootstrap = shift;
 
-		#($line) = split(/\s+\/\//, $line);
-		#($line) = split(/\s+#/, $line);
-
-		if ($line =~ /^\s*\[([^\[\]]+)\]/) {
-			$self->_sub_builder('__id');
-			$self->__id( $1 );
-			next;
+	foreach my $key (sort keys %$bootstrap) {
+		$self->_sub_builder($key);
+		if (ref($key) eq 'ARRAY') {
+			$self->$key([$bootstrap->{$key}]);
 		}
-
-		my ($protokey,$value,$keytype,$key);
-		if ($line =~ /^([^=\s]+)\s*=\s*(.*)\s*$/s) {
-			($protokey,$value) = ($1,$2);
-			($keytype,$key) = split(/:/,$protokey);
+		else {
+			$self->$key($bootstrap->{$key});
 		}
-
-		$key = $protokey unless ($key);
-
-		if ($keytype ne $key) {
-			$keytype = lc $keytype;
-			if ($keytype eq 'list') {
-				$value = [split /\s*,\s*/, $value];
-			} elsif ($keytype eq 'bool') {
-				$value = do{ $value =~ /^t|y|1/i ? 1 : 0; };
-			} elsif ($keytype eq 'interval') {
-				$value = interval_to_seconds($value);
-			} elsif ($keytype eq 'subsection') {
-				if (exists $SECTIONCACHE{$value}) {
-					$value = $SECTIONCACHE{$value};
-				} else {
-					$SUBSECTION_FIXUP{$value}{$self->SECTION} = $key ;
-					next;
-				}
-			}
-		}
-
-		$self->_sub_builder($key);
-		$self->$key($value);
 	}
 
-	no warnings;
-	if (my $parent_def = $SUBSECTION_FIXUP{$self->SECTION}) {
-		my ($parent_section, $parent_key) = each %$parent_def;
-		$SECTIONCACHE{$parent_section}->{$parent_key} = $self;
-		delete $SUBSECTION_FIXUP{$self->SECTION};
-	}
-
-	$SECTIONCACHE{$self->SECTION} = $self;
-
 	return $self;
 }
 
@@ -87,6 +51,7 @@
 push @ISA, qw/OpenSRF::Utils/;
 
 use FileHandle;
+use XML::LibXML;
 use OpenSRF::Utils (':common');  
 use OpenSRF::Utils::Logger;
 use Net::Domain qw/hostfqdn/;
@@ -358,60 +323,52 @@
 
 sub load_config {
 	my $self = shift;
-	my $config = new FileHandle $self->FILE, 'r';
+	my $parser = XML::LibXML->new();
+
+	# Hash of config values
+	my %bootstrap;
+	
+	# Return an XML::LibXML::Document object
+	my $config = $parser->parse_file($self->FILE);
+
 	unless ($config) {
 		OpenSRF::Utils::Logger->error("Could not open ".$self->FILE.": $!\n");
 		die "Could not open ".$self->FILE.": $!\n";
 	}
-	my @stripped_config = $self->__strip_comments($config) if (defined $config);
 
-	my $chunk = [];
-	for my $line (@stripped_config) {
-		no warnings;
-		next unless ($line);
+	# Return an XML::LibXML::NodeList object matching all child elements
+	# of <config><opensrf>...
+	my $osrf_cfg = $config->findnodes('/config/opensrf/child::*');
 
-		if ($line =~ /^\s*\[/ and @$chunk) {
-			my $section = $self->section_pkg->new($chunk);
+	# Iterate through the nodes to pull out key=>value pairs of config settings
+	foreach my $node ($osrf_cfg->get_nodelist()) {
+		my $child_state = 0;
+		foreach my $child_node ($node->childNodes) {
+			# from libxml/tree.h: nodeType 1 = ELEMENT_NODE
+			next if $child_node->nodeType() != 1;
 
-			my $sub_name = $section->SECTION;
-			$self->_sub_builder($sub_name);
-			$self->$sub_name($section);
-
-			#$self->{$section->SECTION} = $section;
-
-			$chunk = [];
-			push @$chunk,$line;
-			next;
-		} 
-		if ($line =~ /^#\s*include\s+"(\S+)"\s*$/o) {
-                        my $included_file = $1;
-			my $section = OpenSRF::Utils::Config->load(config_file => $included_file, nocache => 1);
-
-			my $sub_name = $section->FILE;
-			$self->_sub_builder($sub_name);
-			$self->$sub_name($section);
-
-			for my $subsect (keys %$section) {
-				next if ($subsect eq '__id');
-
-				$self->_sub_builder($subsect);
-				$self->$subsect($$section{$subsect});
-
-				#$self->$subsect($section->$subsect);
-				$self->$subsect->{__sub} = 1;
-			}
-			next;
+			# If the child node is an element, this element may
+			# have multiple values; therefore, push it into an array
+			push @{$bootstrap{$node->nodeName()}}, OpenSRF::Utils::Config::extract_text($child_node->textContent);
+			$child_state = 1;
 		}
-
-		push @$chunk,$line;
+		if (!$child_state) {
+			$bootstrap{$node->nodeName()} = OpenSRF::Utils::Config::extract_text($node->textContent);
+		}
 	}
-	my $section = $self->section_pkg->new($chunk) if (@$chunk);
+
+	my $section = $self->section_pkg->new(\%bootstrap);
 	my $sub_name = $section->SECTION;
 	$self->_sub_builder($sub_name);
 	$self->$sub_name($section);
 
 }
 
+sub extract_text {
+	my $self = shift;
+	$self =~ s/^\s*([.*?])\s*$//m;
+	return $self;
+}
 
 #------------------------------------------------------------------------------------------------------------------------------------
 
@@ -421,7 +378,7 @@
 
 =head1 BUGS
 
-No know bugs, but report any to mrylander at gmail.com.
+No known bugs, but report any to mrylander at gmail.com.
 
 =head1 COPYRIGHT AND LICENSING
 
Index: src/perlmods/OpenSRF/Utils/Logger.pm
===================================================================
--- src/perlmods/OpenSRF/Utils/Logger.pm	(revision 932)
+++ src/perlmods/OpenSRF/Utils/Logger.pm	(working copy)
@@ -39,7 +39,6 @@
 my $act_syslog_enabled = 0;	# is syslog enabled?
 my $logfile_enabled = 1;		# are we logging to a file?
 my $act_logfile_enabled = 1;	# are we logging to a file?
-my $logdir;							# log file directory
 
 our $logger = "OpenSRF::Utils::Logger";
 
@@ -66,16 +65,14 @@
 		warn "*** Logger found no config.  Using STDERR ***\n";
 	}
 
-	$loglevel =  $config->bootstrap->debug; 
-	if($loglevel =~ /error/i){ $loglevel = ERROR(); }
-	elsif($loglevel =~ /warn/i){ $loglevel = WARN(); }
-	elsif($loglevel =~ /info/i){ $loglevel = INFO(); }
-	elsif($loglevel =~ /debug/i){ $loglevel = DEBUG(); }
-	elsif($loglevel =~ /internal/i){ $loglevel = INTERNAL(); }
+	$loglevel =  $config->bootstrap->loglevel; 
+	if($loglevel = 1){ $loglevel = ERROR(); }
+	elsif($loglevel = 2){ $loglevel = WARN(); }
+	elsif($loglevel = 3){ $loglevel = INFO(); }
+	elsif($loglevel = 4){ $loglevel = DEBUG(); }
+	elsif($loglevel = 5){ $loglevel = INTERNAL(); }
 	else{$loglevel= INFO(); }
 
-	my $logdir = $config->bootstrap->log_dir;
-
 	$logfile = $config->bootstrap->logfile;
 	if($logfile =~ /^syslog/) {
 		$syslog_enabled = 1;
@@ -86,7 +83,7 @@
 		$facility = _fac_to_const($facility);
 		openlog($service, 0, $facility);
 
-	} else { $logfile = "$logdir/$logfile"; }
+	} else { $logfile = "$logfile"; }
 
 	$actfile = $config->bootstrap->actlog;
 	if($actfile =~ /^syslog/) {
@@ -97,7 +94,7 @@
 		$actfile = undef;
 		$actfac = _fac_to_const($actfac);
 
-	} else { $actfile = "$logdir/$actfile"; }
+	} else { $actfile = "$actfile"; }
 
 	$isclient = (OpenSRF::Utils::Config->current->bootstrap->client =~ /^true$/iog) ?  1 : 0;
 }
Index: bin/osrf_ctl.sh
===================================================================
--- bin/osrf_ctl.sh	(revision 976)
+++ bin/osrf_ctl.sh	(working copy)
@@ -1,8 +1,7 @@
 #!/bin/bash
 
 OPT_ACTION=""
-OPT_PERL_CONFIG=""
-OPT_C_CONFIG=""
+OPT_CONFIG=""
 OPT_PID_DIR=""
 
 # ---------------------------------------------------------------------------
@@ -11,12 +10,9 @@
 [ $(whoami) != 'opensrf' ] && echo 'Must run as user "opensrf"' && exit;
 
 
-# NOTE: Eventually, there will be one OpenSRF config file format
-# When this happens, we will only need a single OPT_CONFIG variable
-
 function usage {
 	echo "";
-	echo "usage: $0 -d <pid_dir> -p <perl_config> -c <c_config> -a <action>";
+	echo "usage: $0 -d <pid_dir> -c <c_config> -a <action>";
 	echo "";
 	echo "Actions include:"
 	echo -e "\tstart_router"
@@ -36,7 +32,7 @@
 	echo -e "\trestart_all"
 	echo "";
     echo "Example:";
-    echo "  $0 -p bootstrap.conf -c opensrf_core.xml -a restart_all";
+    echo "  $0 -c opensrf_core.xml -a restart_all";
     echo "";
 	exit;
 }
@@ -45,11 +41,10 @@
 # ---------------------------------------------------------------------------
 # Load the command line options and set the global vars
 # ---------------------------------------------------------------------------
-while getopts  "p:c:a:d:h" flag; do
+while getopts  "c:a:d:h" flag; do
 	case $flag in	
 		"a")		OPT_ACTION="$OPTARG";;
-		"c")		OPT_C_CONFIG="$OPTARG";;
-		"p")		OPT_PERL_CONFIG="$OPTARG";;
+		"c")		OPT_CONFIG="$OPTARG";;
 		"d")		OPT_PID_DIR="$OPTARG";;
 		"h"|*)	usage;;
 	esac;
@@ -110,7 +105,7 @@
 
 function start_router {
 	do_action "start" $PID_ROUTER "OpenSRF Router";
-	opensrf_router $OPT_C_CONFIG router
+	opensrf_router $OPT_CONFIG router
 	pid=$(ps ax | grep "OpenSRF Router" | grep -v grep | awk '{print $1}')
 	echo $pid > $PID_ROUTER;
 	return 0;
@@ -123,7 +118,7 @@
 
 function start_perl {
 	do_action "start" $PID_OSRF_PERL "OpenSRF Perl";
-	perl -MOpenSRF::System="$OPT_PERL_CONFIG" -e 'OpenSRF::System->bootstrap()' & 
+	perl -MOpenSRF::System="$OPT_CONFIG" -e 'OpenSRF::System->bootstrap()' & 
 	pid=$!;
 	echo $pid > $PID_OSRF_PERL;
 	sleep 5;
@@ -143,7 +138,7 @@
 	fi;
 
 	do_action "start" $PID_OSRF_C "OpenSRF C (host=$host)";
-	opensrf-c $host $OPT_C_CONFIG opensrf;
+	opensrf-c $host $OPT_CONFIG opensrf;
 	pid=$(ps ax | grep "OpenSRF System-C" | grep -v grep | awk '{print $1}')
 	echo $pid > "$PID_OSRF_C";
 	return 0;
Index: examples/opensrf_core.xml.example
===================================================================
--- examples/opensrf_core.xml.example	(revision 978)
+++ examples/opensrf_core.xml.example	(working copy)
@@ -2,7 +2,7 @@
 
 <config>
 
-	<opensrf> <!-- bootstrap config for the C apps -->
+  <opensrf> <!-- bootstrap config for OpenSRF apps -->
 
 		<!--  the routers's name on the network -->
 		<!-- do not change this -->
@@ -17,7 +17,7 @@
       </routers>
 
       <domains>
-			<!-- Our jabber domain, currenlty only one domain is supported -->
+      <!-- Our jabber domain, currently only one domain is supported -->
          <domain>localhost</domain>
       </domains>
 
@@ -41,8 +41,10 @@
 
 		<!-- 0 None, 1 Error, 2 Warning, 3 Info, 4 debug, 5 Internal (Nasty) -->
       <loglevel>3</loglevel>
-   </opensrf>
 
+    <!-- config file for the services -->
+    <settings_config>/openils/conf/opensrf.xml</settings_config>
+  </opensrf>
 
 	<!-- Update this if you use ChopChop -->
 	<chopchop> <!-- Our jabber server -->
@@ -58,7 +60,6 @@
 		<logfile>/openils/var/log/osrfsys.log</logfile>
 	</chopchop>
 
-
 	<!-- The section between <gateway>...</gateway> is a standard OpenSRF C stack config file -->
 	<gateway>
 
@@ -140,7 +141,3 @@
 	<!-- ======================================================================================== -->
 
 </config>
-
-
-
-
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Config.pm
Type: text/x-perl
Size: 9139 bytes
Desc: not available
Url : http://list.georgialibraries.org/pipermail/open-ils-dev/attachments/20070627/25a32c53/Config-0001.bin


More information about the Open-ils-dev mailing list