sql() if $Opt{sql};
ip_move($Opt{'ip-move'}) if $Opt{'ip-move'};
+if ($Opt{'ip-routed'}) {
+ my @routed = ip_routed($Opt{'ip-routed'});
+ print JSON::encode_json(\@routed);
+ exit 0;
+}
+
+
# Query
help() unless @ARGV;
query(@ARGV);
} `ifconfig`;
}
+sub ip_routed {
+ my $ip = shift @_;
+ my $rec = _query("/ip/$ip") or exit 1;
+ my @routed;
+
+ if ($rec->{type} =~ /^(failover|dedicated)$/) {
+ push @routed, $rec->{routedTo}{serviceName};
+
+ } elsif ($rec->{type} eq 'loadBalancing') {
+
+ my $frontends = _query("/ipLoadbalancing/ip-$ip/frontend") or exit 1;
+
+ for my $frontend_id (@$frontends) {
+ my $frontend = _query("/ipLoadbalancing/ip-$ip/frontend/$frontend_id") or exit 1;
+ next unless $frontend->{defaultBackendId};
+
+ my $farms = _query("/ipLoadbalancing/ip-$ip/".$frontend->{type}."/farm") or exit 1;
+ for my $farm_id (@$farms) {
+
+ my $servers = _query("/ipLoadbalancing/ip-$ip/".$frontend->{type}."/farm/$farm_id/server") or exit 1;
+ for my $id_server (@$servers) {
+ my $server = _query("/ipLoadbalancing/ip-$ip/".$frontend->{type}."/farm/$farm_id/server/$id_server") or exit 1;
+ push @routed,ip_routed($server->{address});
+ }
+
+ }
+
+ }
+
+ }
+
+ return wantarray ? @routed : \@routed;
+}
+
sub ip_move {
- require Sys::Hostname;
my ($ip) = @_;
+ require Sys::Hostname;
my $localhost = Sys::Hostname::hostname();
$localhost ne $routed or die "$ip is already routed to $localhost!\n";
grep {$ip eq $_} ips() or die "$ip is is not installed on $localhost!\n";
- use Data::Dumper;
+ require Data::Dumper;
die Dumper([$localhost,$routed,$rec]);
local $_ = _query("/ip/$ip/move",'POST',{'to' => $localhost});
print JSON::encode_json($_);
option[conf=s] Use another config file instaed of ~/.ovh-api
option[jq] Pipe stdout to | jq .
- option[ip-move=s] Move IP to local
- option[sql] Produce sql to update a database
+ option[ip-move=s] Move IP to local
+ option[ip-routed=s] Print where is routed an ip
+ option[sql] Produce sql to update a database
option[verbose|v+] Verbose mode: increase the verbosity level.
option[debug+] Debug mode: increase the verbosity level.