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

Last change on this file since 1315 was 1280, checked in by nanardon, 9 years ago

add log inside database tracking big objects event and some attributes

  • Property svn:executable set to *
File size: 2.9 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    {
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}
84foreach 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;
Note: See TracBrowser for help on using the repository browser.