File:  [LON-CAPA] / loncom / build / loncaparestoreconfigurations
Revision 1.12: download - view: text, annotated - select for diffs
Sun Mar 3 00:21:23 2002 UTC (22 years, 2 months ago) by harris41
Branches: MAIN
CVS tags: stable_2002_april, HEAD
should process samba.conf on redhat7.* systems now...
also fixes weird formatting problem by only substituting
when there is something to substitute

#!/usr/bin/perl

# loncaparestoreconfigurations

# Scott Harrison, 10/25/2000
# Scott Harrison, 12/14/2000

# This tool helps in updating a system.  It restores backed-up
# configuration files (.rpmsave or other backup notations).

# By default, the .rpmsave suffix is used.
# Alternatively, there can be two other invocations
# Invocation #1:
#   ARGV[0]=suffix
#   ARGV[1]=.bak
# Invocation #2:
#   ARGV[0]=lasttimestamp

# The criteria for the lasttimestamp is that the 
# file suffix is a '.' followed by a 14-digit
# time-stamp (YYYYMMDDhhmmss).
# The time-stamp with the greatest value is
# taken as the backup file.

my $suffix=".rpmsave";
my $suffixpragma="";
if ($ARGV[0] eq 'suffix') {
    $suffix=$ARGV[1] if $ARGV[1]=~/^[\.\w]+$/;
}
elsif ($ARGV[0] eq 'lasttimestamp') {
    $suffixpragma="lasttimestamp";
}


use strict;

my @special_conf_files=(
			"/etc/httpd/conf/access.conf",
			"/etc/smb.conf",
			"/etc/samba/smb.conf"
			);

my @generic_conf_files=(
			"/home/httpd/lonTabs/hosts.tab",
			"/home/httpd/lonTabs/spare.tab",
			"/etc/krb.conf",
			"/etc/ntp.conf",
			"/etc/httpd/conf/srm.conf",
			"/etc/httpd/conf/httpd.conf",
			);

my @perlsetvars=("lonHostID","lonRole","lonAdmEMail","lonDefDomain","lonLoadLim","lonExpire","lonReceipt","lonSqlAccess");
my %pvar;
foreach (@special_conf_files) {
    if (/^\/etc\/httpd\/conf\/access.conf$/) {
	if ($suffixpragma eq 'lasttimestamp') {
	    $suffix=getsuffix('/etc/httpd/conf/access.conf');
	}
	my $template=`/bin/cat /etc/httpd/conf/access.conf`;
	my $lpmlnew=`/bin/cat /etc/httpd/conf/access.conf$suffix`;
#	`/bin/mv /etc/httpd/conf/access.conf /etc/httpd/conf/access.conf.template`;
	foreach my $psv (@perlsetvars) {
	    if ($template=~/\nPerlSetVar\s+$psv\s+(\S+)/) {
		my $pval=$1;
		$lpmlnew=~s/(\nPerlSetVar\s+$psv\s+)\S+/$1$pval/;
		$pvar{$psv}=$pval;
	    }
	}
	open OUT,">/etc/httpd/conf/access.conf$suffix";
	print OUT $lpmlnew;
	close OUT;
    }
    if (/^\/etc\/smb.conf$/ and -e "/etc/smb.conf$suffix") {
	if ($suffixpragma eq 'lasttimestamp') {
	    $suffix=getsuffix('/etc/smb.conf');
	}
	my $template=`/bin/cat /etc/smb.conf$suffix`;
	foreach my $psv (@perlsetvars) {
	    $template=~s/\{\{\{\{\[(.*?)\]\}\}\}\}/$pvar{$1}/ge;
	}
	open OUT,">/etc/smb.conf$suffix";
	print OUT $template;
	close OUT;
    }
    if (/^\/etc\/samba\/smb.conf$/ and -e "/etc/samba/smb.conf$suffix") {
	if ($suffixpragma eq 'lasttimestamp') {
	    $suffix=getsuffix('/etc/samba/smb.conf');
	}
	my $template=`/bin/cat /etc/samba/smb.conf$suffix`;
	foreach my $psv (@perlsetvars) {
	    $template=~s/\{\{\{\{\[(.*?)\]\}\}\}\}/$pvar{$1}/ge;
	}
	open OUT,">/etc/samba/smb.conf$suffix";
	print OUT $template;
	close OUT;
    }
}

exit; # Just because this is only about restoring configuration to
      # new files

foreach (@generic_conf_files) {
    my $file=$_;
    if ($suffixpragma eq 'lasttimestamp') {
	$suffix=getsuffix($file);
    }
    if (-e "$file$suffix") {
	`/bin/mv $file $_.template`;
	`/bin/cp $file$suffix $file`;
    }
}

sub getsuffix {
    my ($file)=@_;
    print "$file\n";
    my $dir=$file; $dir=~s/([^\/]+)$//;
    my $filename=$1;
    opendir(DIR,$dir);
    my @a=grep {/$filename\.\d{14}/} readdir DIR;
    closedir DIR;
    map {s/$filename\.//;} @a;
    my @b=sort {$a<=>$b} @a;
    my $suffix='.'.$b[$#b];
    return $suffix;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>