]> git.nbdom.net Git - nb.git/commitdiff
Option ip-routed
authorDevops <sys@15gifts.com>
Mon, 3 Jul 2017 15:39:01 +0000 (16:39 +0100)
committerDevops <sys@15gifts.com>
Mon, 3 Jul 2017 15:39:01 +0000 (16:39 +0100)
bin/ovh-api

index a3a404238daec7bc9b27c32bffa727596588a59c..7691e27854264227521375538d7b076082a66cea 100755 (executable)
@@ -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.