From c6902334b6383bec53d7c098319590a19c3c7aee Mon Sep 17 00:00:00 2001 From: Peter Marquardt Date: Fri, 10 Jun 2022 09:39:38 +0200 Subject: [PATCH] mxmirror: perltidy - to get some kind of standard formatting --- mxmirror/mxmirror | 487 ++++++++++++++++++++++++---------------------- 1 file changed, 254 insertions(+), 233 deletions(-) diff --git a/mxmirror/mxmirror b/mxmirror/mxmirror index 9a1ceec..bd5febc 100755 --- a/mxmirror/mxmirror +++ b/mxmirror/mxmirror @@ -7,45 +7,43 @@ use Getopt::Long; use strict; use warnings; -my $JBODMAP='/etc/amd/amd.jbod'; -my $MIRRORMAP='/etc/mxmirrors'; +my $JBODMAP = '/etc/amd/amd.jbod'; +my $MIRRORMAP = '/etc/mxmirrors'; -my %CONFIG = ( - CMD => 'STRING', - ); +my %CONFIG = ( CMD => 'STRING', ); my %opt = ( - from => undef, - to => undef, - check => undef, - verbose => 0, - debug => 0, - ); + from => undef, + to => undef, + check => undef, + verbose => 0, + debug => 0, +); my $fullhostname = hostname; -my ($hostname) = $fullhostname =~ /^(.*?)\./; -my $hostip = inet_ntoa(scalar gethostbyname($fullhostname)); +my ($hostname) = $fullhostname =~ /^(.*?)\./; +my $hostip = inet_ntoa( scalar gethostbyname($fullhostname) ); -my $jbodmap = read_jbodmap($JBODMAP); -my $mirrors = read_mirrormap($MIRRORMAP); +my $jbodmap = read_jbodmap($JBODMAP); +my $mirrors = read_mirrormap($MIRRORMAP); my $result = GetOptions( - "from:s" => \$opt{from}, - "to:s" => \$opt{to}, - "destinationhosts" => \$opt{destinationhosts}, - "sourcehosts" => \$opt{sourcehosts}, - "all" => \$opt{all}, - "quiet" => \$opt{quiet}, - "format|fmt:s" => \$opt{format}, - "execute" => \$opt{execute}, - "command|cmd:s" => \$opt{command}, - "check:s" => \$opt{check}, - "verbose" => \$opt{verbose}, - "debug" => \$opt{debug}, - "help" => \$opt{help} - ); - -if($opt{help}) { + "from:s" => \$opt{from}, + "to:s" => \$opt{to}, + "destinationhosts" => \$opt{destinationhosts}, + "sourcehosts" => \$opt{sourcehosts}, + "all" => \$opt{all}, + "quiet" => \$opt{quiet}, + "format|fmt:s" => \$opt{format}, + "execute" => \$opt{execute}, + "command|cmd:s" => \$opt{command}, + "check:s" => \$opt{check}, + "verbose" => \$opt{verbose}, + "debug" => \$opt{debug}, + "help" => \$opt{help} +); + +if ( $opt{help} ) { print <<"EOF"; $0 [options] [action] @@ -75,66 +73,66 @@ EOF exit 0; } -if(defined $opt{destinationhosts}) { - exit print_and_exec_hostlist(mirrors_reverse($mirrors)); +if ( defined $opt{destinationhosts} ) { + exit print_and_exec_hostlist( mirrors_reverse($mirrors) ); } -if(defined $opt{sourcehosts}) { - exit print_and_exec_hostlist($mirrors); +if ( defined $opt{sourcehosts} ) { + exit print_and_exec_hostlist($mirrors); } -if(defined $opt{all}) { - exit print_and_exec_all($mirrors); +if ( defined $opt{all} ) { + exit print_and_exec_all($mirrors); } -if(defined $opt{check}) { - my $host = $opt{check} || $hostname; +if ( defined $opt{check} ) { + my $host = $opt{check} || $hostname; - my $dest = mirrors_reverse($mirrors); + my $dest = mirrors_reverse($mirrors); - foreach(@{$dest->{$host}}) { - printf "RULE: $_->{string}\n" if($opt{debug}); - print get_mirror_status($_); - printf " $_->{source}->{host}:$_->{source}->{hostpath} $_->{destination}->{host}:$_->{destination}->{hostpath} \t$_->{args}\n"; - } + foreach ( @{ $dest->{$host} } ) { + printf "RULE: $_->{string}\n" if ( $opt{debug} ); + print get_mirror_status($_); + printf " $_->{source}->{host}:$_->{source}->{hostpath} $_->{destination}->{host}:$_->{destination}->{hostpath} \t$_->{args}\n"; + } - exit 0; + exit 0; } my $err = 0; -foreach(@{$mirrors->{$hostname}}) { +foreach ( @{ $mirrors->{$hostname} } ) { my $default_fmt = $CONFIG{CMD}; - my $fmt = (defined $opt{format})?$opt{format}:$default_fmt; + my $fmt = ( defined $opt{format} ) ? $opt{format} : $default_fmt; my $cmd = $opt{command}; - if(not defined $cmd and $opt{execute}) { + if ( not defined $cmd and $opt{execute} ) { $cmd = $fmt; } - if(not $fmt or (defined $cmd and not $cmd)) { - print "\n"; - print " default format: '$default_fmt'\n"; - print " default command: \n\n"; - print " format/command variables:\n"; - print " DSTHOST destination host\n"; - print " DSTPATH destination path\n"; - print " SRCHOST source host\n"; - print " SRCPATH source path\n"; - print " ARGS additional args\n"; - print " DESTINATION DSTHOST==SRCHOST : DSTHOST:DSTPATH \n"; - print " DSTHOST!=SRCHOST : DSTPATH \n\n"; - exit 1; + if ( not $fmt or ( defined $cmd and not $cmd ) ) { + print "\n"; + print " default format: '$default_fmt'\n"; + print " default command: \n\n"; + print " format/command variables:\n"; + print " DSTHOST destination host\n"; + print " DSTPATH destination path\n"; + print " SRCHOST source host\n"; + print " SRCPATH source path\n"; + print " ARGS additional args\n"; + print " DESTINATION DSTHOST==SRCHOST : DSTHOST:DSTPATH \n"; + print " DSTHOST!=SRCHOST : DSTPATH \n\n"; + exit 1; } - print do_format($fmt, $_) . "\n"; + print do_format( $fmt, $_ ) . "\n"; - if($cmd) { - my $command = do_format($cmd, $_); - if(system($command) != 0) { - print STDERR "mxmirror FAILED CMD: $command\n"; - $err++; - } + if ($cmd) { + my $command = do_format( $cmd, $_ ); + if ( system($command) != 0 ) { + print STDERR "mxmirror FAILED CMD: $command\n"; + $err++; + } } } @@ -143,80 +141,79 @@ exit($err); ################################################################### sub print_and_exec_all { - my $list = shift; - - my $default_fmt = "SRCHOST:SRCPATH DSTHOST:DSTPATH ARGS"; - my $fmt = (defined $opt{format})?$opt{format}:$default_fmt; - my $cmd = $opt{command}; - - if(not defined $cmd and $opt{execute}) { - $cmd = $fmt; - } - - if(not $fmt or (defined $cmd and not $cmd)) { - print "\n"; - print " default format: '$default_fmt'\n"; - print " default command: \n\n"; - print " format/command variables:\n"; - print " DSTHOST destination host\n"; - print " DSTPATH destination path\n"; - print " SRCHOST source host\n"; - print " SRCPATH source path\n"; - print " ARGS additional args\n"; - print " DESTINATION DSTHOST==SRCHOST : DSTHOST:DSTPATH \n"; - print " DSTHOST!=SRCHOST : DSTPATH \n\n"; - return 1; - } - - my @hosts = sort keys %{$list}; - - foreach my $h (@hosts) { - my %vars = ( - ); - foreach(@{$list->{$h}}) { - - print do_format($fmt, $_, \%vars) . "\n" if($opt{format} or not $opt{command}); - - if($cmd) { - my $command = do_format($cmd, $_, \%vars); - print qx($command); - } - } - } - - return 0; + my $list = shift; + + my $default_fmt = "SRCHOST:SRCPATH DSTHOST:DSTPATH ARGS"; + my $fmt = ( defined $opt{format} ) ? $opt{format} : $default_fmt; + my $cmd = $opt{command}; + + if ( not defined $cmd and $opt{execute} ) { + $cmd = $fmt; + } + + if ( not $fmt or ( defined $cmd and not $cmd ) ) { + print "\n"; + print " default format: '$default_fmt'\n"; + print " default command: \n\n"; + print " format/command variables:\n"; + print " DSTHOST destination host\n"; + print " DSTPATH destination path\n"; + print " SRCHOST source host\n"; + print " SRCPATH source path\n"; + print " ARGS additional args\n"; + print " DESTINATION DSTHOST==SRCHOST : DSTHOST:DSTPATH \n"; + print " DSTHOST!=SRCHOST : DSTPATH \n\n"; + return 1; + } + + my @hosts = sort keys %{$list}; + + foreach my $h (@hosts) { + my %vars = (); + foreach ( @{ $list->{$h} } ) { + + print do_format( $fmt, $_, \%vars ) . "\n" if ( $opt{format} or not $opt{command} ); + + if ($cmd) { + my $command = do_format( $cmd, $_, \%vars ); + print qx($command); + } + } + } + + return 0; } sub print_and_exec_hostlist { - my $list = shift; - - my $default_fmt = "HOST"; - my $fmt = (defined $opt{format})?$opt{format}:$default_fmt; - my $cmd = $opt{command}; - - if(not defined $cmd and $opt{execute}) { - $cmd = $fmt; - } - - if(not $fmt or (defined $cmd and not $cmd)) { - print "\n"; - print " default format: '$default_fmt'\n"; - print " default command: \n\n"; - print " format/command variables:\n"; - print " HOST destination\n\n"; - return 1; - } - - foreach(sort keys %{$list}) { - print do_format($fmt, {}, {HOST => $_}) . "\n" if($opt{format} or not $opt{command}); - - if($cmd) { - my $command = do_format($cmd, {}, {HOST => $_}); - print qx($command); - } - } - - return 0; + my $list = shift; + + my $default_fmt = "HOST"; + my $fmt = ( defined $opt{format} ) ? $opt{format} : $default_fmt; + my $cmd = $opt{command}; + + if ( not defined $cmd and $opt{execute} ) { + $cmd = $fmt; + } + + if ( not $fmt or ( defined $cmd and not $cmd ) ) { + print "\n"; + print " default format: '$default_fmt'\n"; + print " default command: \n\n"; + print " format/command variables:\n"; + print " HOST destination\n\n"; + return 1; + } + + foreach ( sort keys %{$list} ) { + print do_format( $fmt, {}, { HOST => $_ } ) . "\n" if ( $opt{format} or not $opt{command} ); + + if ($cmd) { + my $command = do_format( $cmd, {}, { HOST => $_ } ); + print qx($command); + } + } + + return 0; } sub mirrors_reverse { @@ -224,10 +221,10 @@ sub mirrors_reverse { my $r; - foreach my $h (sort keys %{$mirrors}) { - foreach(@{$mirrors->{$h}}) { - push @{$r->{$_->{destination}->{host}}}, $_; - } + foreach my $h ( sort keys %{$mirrors} ) { + foreach ( @{ $mirrors->{$h} } ) { + push @{ $r->{ $_->{destination}->{host} } }, $_; + } } return $r; @@ -236,16 +233,16 @@ sub mirrors_reverse { sub get_mirror_status { my $mirror = shift; - return 'REMOTE' unless($hostname eq $mirror->{destination}->{host}); + return 'REMOTE' unless ( $hostname eq $mirror->{destination}->{host} ); - if(open(F, '<', "$mirror->{destination}->{hostpath}/.PMIRROR_STATUS")) { + if ( open( F, '<', "$mirror->{destination}->{hostpath}/.PMIRROR_STATUS" ) ) { my $line = ; close F; - my ($status, $timestamp) = $line =~ /^(\S+) (\d+) /; + my ( $status, $timestamp ) = $line =~ /^(\S+) (\d+) /; - if($status eq 'OK') { - return sprintf ("%2.2f",(time()-$timestamp)/24/60/60); + if ( $status eq 'OK' ) { + return sprintf( "%2.2f", ( time() - $timestamp ) / 24 / 60 / 60 ); } return $line; @@ -259,10 +256,11 @@ sub do_format { my $data = shift; my $vars = shift || {}; - if(defined $data) { - my $dst = ($data->{source}->{host} eq $data->{destination}->{host})? - $data->{destination}->{hostpath} : - "$data->{destination}->{host}:$data->{destination}->{hostpath}"; + if ( defined $data ) { + my $dst + = ( $data->{source}->{host} eq $data->{destination}->{host} ) + ? $data->{destination}->{hostpath} + : "$data->{destination}->{host}:$data->{destination}->{hostpath}"; $fmt =~ s/DESTINATION/$dst/g; $fmt =~ s/SRCHOST/$data->{source}->{host}/g; @@ -271,10 +269,11 @@ sub do_format { $fmt =~ s/DSTPATH/$data->{destination}->{hostpath}/g; $fmt =~ s/ARGS/$data->{args}/g; $fmt =~ s/STRING/$data->{string}/g; -# $fmt =~ s/TIME/$data->{timeslot}->{time_str}/g; + + # $fmt =~ s/TIME/$data->{timeslot}->{time_str}/g; } - foreach(keys %{$vars}) { + foreach ( keys %{$vars} ) { print "XXX" . $_; $fmt =~ s/$_/$vars->{$_}/g; } @@ -283,9 +282,9 @@ sub do_format { } sub to_jbod_path { - my $host = shift; - my $jbod = shift; - my $path = shift; + my $host = shift; + my $jbod = shift; + my $path = shift; #return "/amd/${host}/X/${jbod}${prepend}${path}"; @@ -327,38 +326,47 @@ sub to_amd_path { sub convert_to_sourcepath { my $string = shift; - my ($jbod, $path, $host, $hostpath, $partition); + my ( $jbod, $path, $host, $hostpath, $partition ); - if(($jbod, $path) = $string =~ /^([XCMD][\dA-Z]\d\d\d)(.*?)$/) { - unless(defined $jbodmap->{$jbod}) { + if ( ( $jbod, $path ) = $string =~ /^([XCMD][\dA-Z]\d\d\d)(.*?)$/ ) { + unless ( defined $jbodmap->{$jbod} ) { printf STDERR "**ERROR: unknown jbod: $string\n"; - return undef + return undef; } $host = $jbodmap->{$jbod}; - $hostpath = to_jbod_path($host, $jbod, $path); + $hostpath = to_jbod_path( $host, $jbod, $path ); - return({ host => $host, - jbod => $jbod, - path => $path, - hostpath => $hostpath }); + return ( + { host => $host, + jbod => $jbod, + path => $path, + hostpath => $hostpath + } + ); } - if(($host,$partition,$path) = $string =~ /^(.*?):(\d+)(.*?)$/) { - $hostpath = to_amd_path($host, $partition, $path); + if ( ( $host, $partition, $path ) = $string =~ /^(.*?):(\d+)(.*?)$/ ) { + $hostpath = to_amd_path( $host, $partition, $path ); - return({ host => $host, + return ( + { host => $host, partition => $partition, path => $path, - hostpath => $hostpath }); + hostpath => $hostpath + } + ); } - if(($host,$path) = $string =~ m(^(.*?):(/.+?)$) ) { - return({ host => $host, - path => $path, - hostpath => $path }); + if ( ( $host, $path ) = $string =~ m(^(.*?):(/.+?)$) ) { + return ( + { host => $host, + path => $path, + hostpath => $path + } + ); } return; @@ -391,65 +399,77 @@ sub convert_to_destinationpath { my $string = shift; my $source = shift; - my ($jbod, $path, $host, $hostpath, $partition); + my ( $jbod, $path, $host, $hostpath, $partition ); - if(($jbod, $path) = $string =~ /^([XCMD][\dA-Z]\d\d\d)(.*?)$/) { - unless(defined $jbodmap->{$jbod}) { + if ( ( $jbod, $path ) = $string =~ /^([XCMD][\dA-Z]\d\d\d)(.*?)$/ ) { + unless ( defined $jbodmap->{$jbod} ) { printf STDERR "**ERROR: unknown jbod: $string\n"; return undef; } $host = $jbodmap->{$jbod}; - unless($path) { - if(defined $source->{jbod}) { - $path = "/mirror/$source->{jbod}$source->{path}"; - } elsif (defined $source->{partition}) { - $path = "/mirror/$source->{host}/$source->{partition}$source->{path}"; - } else { - $path = "/mirror/$source->{host}$source->{path}"; - } + unless ($path) { + if ( defined $source->{jbod} ) { + $path = "/mirror/$source->{jbod}$source->{path}"; + } + elsif ( defined $source->{partition} ) { + $path = "/mirror/$source->{host}/$source->{partition}$source->{path}"; + } + else { + $path = "/mirror/$source->{host}$source->{path}"; + } } - $hostpath = to_jbod_path($host, $jbod, $path); + $hostpath = to_jbod_path( $host, $jbod, $path ); - return({ host => $host, - jbod => $jbod, - path => $path, - hostpath => $hostpath }); + return ( + { host => $host, + jbod => $jbod, + path => $path, + hostpath => $hostpath + } + ); } -# to := : -# must be absolute or empty -# or must be set -# -# isemtpy(): :/amd///mirror/(|/) -# isset(): :/amd// -# isempty(): : + # to := : + # must be absolute or empty + # or must be set + # + # isemtpy(): :/amd///mirror/(|/) + # isset(): :/amd// + # isempty(): : - if(($host,$partition,$path) = $string =~ /^(.*?):(\d+)(.*?)$/) { + if ( ( $host, $partition, $path ) = $string =~ /^(.*?):(\d+)(.*?)$/ ) { - unless($path) { - if(defined $source->{jbod}) { - $path = "/mirror/$source->{jbod}$source->{path}"; - } else { - $path = "/mirror/$source->{host}/$source->{partition}$source->{path}"; - } - } + unless ($path) { + if ( defined $source->{jbod} ) { + $path = "/mirror/$source->{jbod}$source->{path}"; + } + else { + $path = "/mirror/$source->{host}/$source->{partition}$source->{path}"; + } + } - $hostpath = to_amd_path($host, $partition, $path); + $hostpath = to_amd_path( $host, $partition, $path ); - return({ host => $host, + return ( + { host => $host, partition => $partition, path => $path, - hostpath => $hostpath }); + hostpath => $hostpath + } + ); } - if(($host,$path) = $string =~ m(^(.*?):(/.+?)$) ) { - return({ host => $host, - path => $path, - hostpath => $path }); + if ( ( $host, $path ) = $string =~ m(^(.*?):(/.+?)$) ) { + return ( + { host => $host, + path => $path, + hostpath => $path + } + ); } @@ -463,43 +483,44 @@ sub read_mirrormap { my $string; - my ($from, $to, $args, $host, $frompath, $topath); + my ( $from, $to, $args, $host, $frompath, $topath ); - my ($source, $destination); + my ( $source, $destination ); - open(F, '<', $file) or die "can't open $file: $?"; + open( F, '<', $file ) or die "can't open $file: $?"; - foreach() { + foreach () { s/^\s+//; s/\s+$//; s/\s+/ /; - next if($_ eq ""); + next if ( $_ eq "" ); - if(/^#\s*(\S+)\s*=\s*(.*)/) { - $CONFIG{$1} = $2; + if (/^#\s*(\S+)\s*=\s*(.*)/) { + $CONFIG{$1} = $2; } - next if(/^#/); + next if (/^#/); $string = $_; - if(($from, $to, $args) = /^(\S+) (\S+)\s*(.*)$/) { + if ( ( $from, $to, $args ) = /^(\S+) (\S+)\s*(.*)$/ ) { - $source = convert_to_sourcepath($from); + $source = convert_to_sourcepath($from); unless ($source) { - print STDERR "**ERROR: CAN'T PARSE FROM: $_\n"; - next; + print STDERR "**ERROR: CAN'T PARSE FROM: $_\n"; + next; } - $destination = convert_to_destinationpath($to, $source); - unless($destination) { - print STDERR "**ERROR: CAN'T PARSE TO: $_\n"; - next; + $destination = convert_to_destinationpath( $to, $source ); + unless ($destination) { + print STDERR "**ERROR: CAN'T PARSE TO: $_\n"; + next; } - push @{$map->{$source->{host}}}, {string => $string, source => $source, destination => $destination, args => $args}; + push @{ $map->{ $source->{host} } }, { string => $string, source => $source, destination => $destination, args => $args }; - } else { + } + else { print STDERR "**ERROR: CAN'T PARSE: $_\n"; } @@ -513,11 +534,11 @@ sub read_mirrormap { sub read_jbodmap { my $file = shift; - open(F, '<', $file) or die "can't open $file: $?"; + open( F, '<', $file ) or die "can't open $file: $?"; my $map = {}; - foreach() { + foreach () { next unless (m(/amd/(.*?)/[XCMD]/([XCMD][\dA-Z]\d\d\d))); $map->{$2} = $1; }