source: trunk/LATMOS-Accounts/bin/la-sql-switch-cname @ 1806

Last change on this file since 1806 was 1318, checked in by nanardon, 9 years ago

Use new delAttributeValue and addAttributeValue to make code shorter (and working)

  • Property svn:executable set to *
File size: 2.5 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use 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
17Example : la-rename-host foo bar
18
19=cut
20
21GetOptions(
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
35Use this configuration directory instead of the default one.
36
37=item -b|--base basename
38
39Query this specific base instead of the default one.
40
41=back
42
43=cut
44
45my $LA = LATMOS::Accounts->new($config, noacl => 1);
46my $labase = $LA->base($base);
47$labase && $labase->load or die "Cannot load base";
48
49$labase->unexported(1);
50
51if (!@ARGV) {
52    pod2usage(1);
53}
54
55my ($to, $from) = ($ARGV[-1], $ARGV[-2]);
56
57my $oto = $labase->get_object('nethost', $to) or die "Cannot find $to\n";
58
59foreach 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    $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";
76}
77
78foreach 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
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";
95}
96
97$labase->commit;
Note: See TracBrowser for help on using the repository browser.