[1277] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use LATMOS::Accounts; |
---|
| 6 | use Getopt::Long; |
---|
| 7 | use Pod::Usage; |
---|
| 8 | |
---|
| 9 | =head1 NAME |
---|
| 10 | |
---|
[1280] | 11 | la-sql-switch-cname - Exchange (c)name between two host |
---|
[1277] | 12 | |
---|
| 13 | =head1 SYNOPSIS |
---|
| 14 | |
---|
[1280] | 15 | la-exchange-ip cname [cname] [-a name] desthost |
---|
[1277] | 16 | |
---|
| 17 | Example : la-rename-host foo bar |
---|
| 18 | |
---|
| 19 | =cut |
---|
| 20 | |
---|
| 21 | GetOptions( |
---|
| 22 | 'c|config=s' => \my $config, |
---|
| 23 | 'b|base=s' => \my $base, |
---|
| 24 | 'cname|cn=s' => \my @cnames, |
---|
| 25 | 'a|arecord=s' => \my @arecords, |
---|
| 26 | 'help' => sub { pod2usage(0) }, |
---|
| 27 | ) or pod2usage(); |
---|
| 28 | |
---|
| 29 | =head1 OPTIONS |
---|
| 30 | |
---|
| 31 | =over 4 |
---|
| 32 | |
---|
| 33 | =item -c|--config configdir |
---|
| 34 | |
---|
| 35 | Use this configuration directory instead of the default one. |
---|
| 36 | |
---|
| 37 | =item -b|--base basename |
---|
| 38 | |
---|
| 39 | Query this specific base instead of the default one. |
---|
| 40 | |
---|
| 41 | =back |
---|
| 42 | |
---|
| 43 | =cut |
---|
| 44 | |
---|
| 45 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
| 46 | my $labase = $LA->base($base); |
---|
| 47 | $labase && $labase->load or die "Cannot load base"; |
---|
| 48 | |
---|
| 49 | $labase->unexported(1); |
---|
| 50 | |
---|
| 51 | if (!@ARGV) { |
---|
| 52 | pod2usage(1); |
---|
| 53 | } |
---|
[1318] | 54 | |
---|
[1277] | 55 | my ($to, $from) = ($ARGV[-1], $ARGV[-2]); |
---|
| 56 | |
---|
| 57 | my $oto = $labase->get_object('nethost', $to) or die "Cannot find $to\n"; |
---|
| 58 | |
---|
| 59 | foreach my $cname (@cnames) { |
---|
| 60 | my @listhost = $labase->search_objects('nethost', "cname=$cname"); |
---|
| 61 | my ($nhost, @others) = $from |
---|
| 62 | ? grep { $_ eq $from } @listhost |
---|
| 63 | : @listhost; |
---|
| 64 | if (@others) { |
---|
| 65 | die "Multiple hosts found for $cname: " . join(', ', $nhost, @others) . "\n"; |
---|
| 66 | } elsif (!$nhost) { |
---|
| 67 | die "No cname $cname found\n"; |
---|
| 68 | } elsif ($nhost eq $to) { |
---|
| 69 | die "Source and destination are the same host for cname $cname\n"; |
---|
| 70 | } |
---|
| 71 | my $onhost = $labase->get_object('nethost', $nhost); |
---|
| 72 | warn "CNAME $cname: $nhost => $to\n"; |
---|
| 73 | |
---|
[1318] | 74 | $onhost->delAttributeValue('cname', $cname) or die "Cannot remove $cname from $nhost\n"; |
---|
| 75 | $oto->addAttributeValue ('cname', $cname) or die "Cannot add $cname to $to\n"; |
---|
[1277] | 76 | } |
---|
[1318] | 77 | |
---|
[1277] | 78 | foreach my $aname (@arecords) { |
---|
| 79 | my @listhost = $labase->search_objects('nethost', "otherName=$aname"); |
---|
| 80 | my ($nhost, @others) = $from |
---|
| 81 | ? grep { $_ eq $from } @listhost |
---|
| 82 | : @listhost; |
---|
| 83 | if (@others) { |
---|
| 84 | die "Multiple hosts found for $aname: " . join(', ', $nhost, @others) . "\n"; |
---|
| 85 | } elsif (!$nhost) { |
---|
| 86 | die "No otherName $aname found\n"; |
---|
| 87 | } elsif ($nhost eq $to) { |
---|
| 88 | die "Source and destination are the same host for a $aname\n"; |
---|
| 89 | } |
---|
| 90 | my $onhost = $labase->get_object('nethost', $nhost); |
---|
| 91 | warn "CNAME $aname: $nhost => $to"; |
---|
| 92 | |
---|
[1318] | 93 | $onhost->delAttributeValue('otherName', $aname) or die "Cannot remove $aname from $nhost\n"; |
---|
| 94 | $oto->addAttributeValue ('otherName', $aname) or die "Cannot add $aname to $to\n"; |
---|
[1277] | 95 | } |
---|
| 96 | |
---|
| 97 | $labase->commit; |
---|