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 | |
---|
11 | la-sql-switch-cname - Exchange (c)name between two host |
---|
12 | |
---|
13 | =head1 SYNOPSIS |
---|
14 | |
---|
15 | la-exchange-ip cname [cname] [-a name] desthost |
---|
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 | } |
---|
54 | |
---|
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 | |
---|
74 | { |
---|
75 | my @name = grep { $_ && $_ ne $cname } $onhost->get_attributes('cname'); |
---|
76 | $onhost->set_c_fields(cname => @name ? [ @name ] : undef) or die "Cannot set $cname to $nhost\n"; |
---|
77 | } |
---|
78 | |
---|
79 | { |
---|
80 | my @name = $cname, $oto->get_attributes('cname'); |
---|
81 | $oto->set_c_fields(cname => [ grep { $_ } @name ]) or die "Cannot add $cname to $to\n"; |
---|
82 | } |
---|
83 | } |
---|
84 | foreach my $aname (@arecords) { |
---|
85 | my @listhost = $labase->search_objects('nethost', "otherName=$aname"); |
---|
86 | my ($nhost, @others) = $from |
---|
87 | ? grep { $_ eq $from } @listhost |
---|
88 | : @listhost; |
---|
89 | if (@others) { |
---|
90 | die "Multiple hosts found for $aname: " . join(', ', $nhost, @others) . "\n"; |
---|
91 | } elsif (!$nhost) { |
---|
92 | die "No otherName $aname found\n"; |
---|
93 | } elsif ($nhost eq $to) { |
---|
94 | die "Source and destination are the same host for a $aname\n"; |
---|
95 | } |
---|
96 | my $onhost = $labase->get_object('nethost', $nhost); |
---|
97 | warn "CNAME $aname: $nhost => $to"; |
---|
98 | |
---|
99 | { |
---|
100 | my @name = grep { $_ && $_ ne $aname } $onhost->get_attributes('otherName'); |
---|
101 | $onhost->set_c_fields(otherName => @name ? [ @name ] : undef) or die "Cannot set $aname to $nhost\n"; |
---|
102 | } |
---|
103 | |
---|
104 | { |
---|
105 | my @name = $aname, $oto->get_attributes('otherName'); |
---|
106 | $oto->set_c_fields(otherName => [ grep { $_ } @name ]) or die "Cannot add $aname to $to\n"; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | $labase->commit; |
---|