Commit 08eb1f86 authored by Yadd's avatar Yadd
Browse files

lmMigrateConfFiles2ini now can be used to upgrade

parent 0e65a2bf
...@@ -5,10 +5,12 @@ use Getopt::Long; ...@@ -5,10 +5,12 @@ use Getopt::Long;
use Lemonldap::NG::Common::Conf; use Lemonldap::NG::Common::Conf;
my %opts; my %opts;
my $result = GetOptions ( \%opts, 'help|h', 'current|c=s', 'new|n=s', 'latest|l' ); my $result =
GetOptions( \%opts, 'help|h', 'current|c=s', 'new|n=s', 'latest|l' );
if( $opts{help} or not ( $opts{current} and $opts{new} ) ) { if ( $opts{help} or not( $opts{current} and $opts{new} ) ) {
print STDERR "Usage: $0 --current=/current/lemonldap-ng.ini --new=/new/lemonldap-ng.ini\n"; print STDERR
"Usage: $0 --current=/current/lemonldap-ng.ini --new=/new/lemonldap-ng.ini\n";
print STDERR "# other parameters: print STDERR "# other parameters:
--latest -l --latest -l
convert only last configuration convert only last configuration
...@@ -16,51 +18,60 @@ if( $opts{help} or not ( $opts{current} and $opts{new} ) ) { ...@@ -16,51 +18,60 @@ if( $opts{help} or not ( $opts{current} and $opts{new} ) ) {
exit 1; exit 1;
} }
foreach( $opts{current}, $opts{new} ) { foreach ( $opts{current}, $opts{new} ) {
unless( -e $_ ) { unless ( -e $_ ) {
print STDERR "$_ does not exist\n"; print STDERR "$_ does not exist\n";
exit 2; exit 2;
} }
unless( -r $_ ) { unless ( -r $_ ) {
print STDERR "$_ is not readable\n"; print STDERR "$_ is not readable\n";
exit 3; exit 3;
} }
} }
my $old = Lemonldap::NG::Common::Conf->new({ my $old = Lemonldap::NG::Common::Conf->new(
{
confFile => $opts{current}, confFile => $opts{current},
noCache=>1, noCache => 1,
}); }
unless($old) { );
print STDERR "Failed to get current conf : $Lemonldap::NG::Common::Conf::msg\n"; unless ($old) {
print STDERR
"Failed to get current conf : $Lemonldap::NG::Common::Conf::msg\n";
exit 4; exit 4;
} }
my $new = Lemonldap::NG::Common::Conf->new({ my $new = Lemonldap::NG::Common::Conf->new(
{
confFile => $opts{new}, confFile => $opts{new},
force=>1, force => 1,
noCache=>1, noCache => 1,
cfgNumFixed=>1, cfgNumFixed => 1,
}); }
unless($new) { );
print STDERR "Failed to create new conf object : $Lemonldap::NG::Common::Conf::msg\n"; unless ($new) {
print STDERR
"Failed to create new conf object : $Lemonldap::NG::Common::Conf::msg\n";
exit 5; exit 5;
} }
my @available; my @available;
if($opts{lastest}){ if ( $opts{lastest} ) {
@available = $old->lastCfg(); @available = $old->lastCfg();
}else{ }
else {
@available = $old->available(); @available = $old->available();
} }
foreach(@available) { foreach (@available) {
my $conf = $old->getConf({cfgNum=>$_}); my $conf = $old->getConf( { cfgNum => $_ } );
eval { delete $conf->{reVHosts}; eval {
delete $conf->{reVHosts};
delete $conf->{cipher}; delete $conf->{cipher};
}; };
unless($conf){ unless ($conf) {
print STDERR "\nFailed to get conf $_ : $Lemonldap::NG::Common::Conf::msg\n"; print STDERR
"\nFailed to get conf $_ : $Lemonldap::NG::Common::Conf::msg\n";
exit 6; exit 6;
} }
print "Conf $conf->{cfgNum}:"; print "Conf $conf->{cfgNum}:";
my $r = $new->saveConf($conf); my $r = $new->saveConf($conf);
print ($r ? "stored" : "failed: $Lemonldap::NG::Common::Conf::msg"); print( $r ? "stored" : "failed: $Lemonldap::NG::Common::Conf::msg" );
print "\n"; print "\n";
} }
...@@ -4,40 +4,77 @@ use strict; ...@@ -4,40 +4,77 @@ use strict;
use Getopt::Long; use Getopt::Long;
use Config::IniFiles; use Config::IniFiles;
our %opts; my %opts;
my $result = GetOptions( \%opts, 'storage|s=s', 'apply|a=s', 'dir|d=s', 'ini|i=s',
'preserve|p' );
use Lemonldap::NG::Common::Conf::Constants; use Lemonldap::NG::Common::Conf::Constants;
$opts{dir} ||= '/etc/lemonldap-ng'; $opts{dir} ||= '/etc/lemonldap-ng';
my $old = {storage => $opts{storage} || "$opts{dir}/storage.conf", my $old = {
storage => $opts{storage} || "$opts{dir}/storage.conf",
apply => $opts{apply} || "$opts{dir}/apply.conf", apply => $opts{apply} || "$opts{dir}/apply.conf",
}; };
my $new = $opts{ini} || "$opts{dir}/lemonldap-ng.ini"; my $new = $opts{ini} || "$opts{dir}/lemonldap-ng.ini";
my $datas; my $datas;
-r $old->{storage} or quit(2,"$old->{storage} is not readeable"); -r $old->{storage} or quit( 2, "$old->{storage} is not readeable" );
open F, $old->{storage}; open F, $old->{storage};
while(<F>){ while (<F>) {
next if(/^\s*(?:#.*)?$/); next if (/^\s*(?:#.*)?$/);
my($k,$v)=(/^(\w+)\s*=\s*(.*)$/) or quit(3,"bad line in $old->{storage}:$_"); my ( $k, $v ) = (/^(\w+)\s*=\s*(.*)$/)
$datas->{configuration}->{$k}=$v; or quit( 3, "bad line in $old->{storage}:$_" );
$datas->{configuration}->{$k} = $v;
} }
close F; close F;
if(-e $old->{apply}){ if ( -e $old->{apply} ) {
open F,$old->{apply}; open F, $old->{apply};
while(<F>){ while (<F>) {
next if(/^\s*(?:#.*)?$/); next if (/^\s*(?:#.*)?$/);
my($k,$v)=(/^([\w\.\-]+)\s+(.*)$/) or quit(3,"bad line in $old->{apply}:$_"); my ( $k, $v ) = (/^([\w\.\-]+)\s+(.*)$/)
$datas->{apply}->{$k}=$v; or quit( 3, "bad line in $old->{apply}:$_" );
$datas->{apply}->{$k} = $v;
} }
close F; close F;
} }
if(-e $new){ my $conf;
-w $new or quit(4,"$new is not writeable"); if ( -e $new ) {
my $conf = Config::IniFiles->new(-file=>$new); -w $new or quit( 4, "$new is not writeable" );
$conf = Config::IniFiles->new( -file => $new )
or quit( 4,
"Unable to open $new:\n\t"
. join( "\n\t", @Config::IniFiles::errors ) );
}
else {
$conf = Config::IniFiles->new();
}
my @sections = $conf->Sections();
foreach (qw(configuration apply)) {
next unless ( ref $datas->{$_} );
$conf->AddSection($_) unless ( $conf->SectionExists($_) );
while ( my ( $k, $v ) = each %{ $datas->{$_} } ) {
if ( $conf->exists( $_, $k ) ) {
$conf->setval( $_, $k, $v );
}
else {
$conf->newval( $_, $k, $v );
}
}
}
if ( -e $new ) {
$conf->RewriteConfig();
}
else {
$conf->WriteConfig($new)
or quit( 5,
"Unable to create $new:\n\t"
. join( "\n\t", @Config::IniFiles::errors ) );
unless ( $opts{preserve} ) {
unlink $old->{storage}, $old->{apply};
}
} }
sub quit { sub quit {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment