From: Devops Date: Mon, 3 Jul 2017 15:39:01 +0000 (+0100) Subject: Option ip-routed X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=af09c6391f3353201607486a4106ee4b1d68f519;p=nb.git Option ip-routed --- diff --git a/bin/ovh-api b/bin/ovh-api index a3a40423..7691e278 100755 --- a/bin/ovh-api +++ b/bin/ovh-api @@ -56,6 +56,13 @@ auth() if @KEY < 3; 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); @@ -76,9 +83,43 @@ sub ips() { } `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(); @@ -91,7 +132,7 @@ sub ip_move { $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($_); @@ -509,8 +550,9 @@ $NAME - Script to query ovh's api 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.