Filippo Giunchedi has submitted this change and it was merged.

Change subject: Make `es-tool ban-node` handle both IP addressses and hostnames
......................................................................


Make `es-tool ban-node` handle both IP addressses and hostnames

If it's IPv4, it goes into _ip.
Otherwise it's treated as a hostname and put in _host

Change-Id: Icabca481430e8830c2dae998c7876f9e9a3ec311
---
M modules/elasticsearch/files/es-tool
M modules/elasticsearch/manifests/init.pp
M modules/elasticsearch/manifests/packages.pp
3 files changed, 40 insertions(+), 26 deletions(-)

Approvals:
  Filippo Giunchedi: Verified; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/modules/elasticsearch/files/es-tool 
b/modules/elasticsearch/files/es-tool
index 10ff906..8a999a0 100755
--- a/modules/elasticsearch/files/es-tool
+++ b/modules/elasticsearch/files/es-tool
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 import argparse
+import ipaddr
 import os
 import subprocess
 import sys
@@ -49,17 +50,17 @@
     return set_setting("cluster.routing.allocation.enable", status)
 
 
-def set_banned_ips(iplist):
-    return set_setting("cluster.routing.allocation.exclude._ip",
-                       ",".join(iplist))
+def set_banned_nodes(nodelist, node_type):
+    return set_setting("cluster.routing.allocation.exclude." + node_type,
+                       ",".join(nodelist))
 
 
-def get_banned_ips():
+def get_banned_nodes(node_type):
     es = Elasticsearch(args.server)
     res = es.cluster.get_settings()
     try:
         bannedstr = res["transient"]["cluster"]["routing"]["allocation"][
-            "exclude"]["_ip"]
+            "exclude"][node_type]
         if bannedstr:
             return bannedstr.split(",")
     except KeyError:
@@ -69,21 +70,27 @@
 
 # Add new command functions here
 def es_ban_node():
-    if args.IP == "":
-        print "No IP address provided"
+    if args.node == "":
+        print "No node provided"
         return os.EX_UNAVAILABLE
 
-    banned = get_banned_ips()
-    if args.IP in banned:
-        print args.IP + " already banned from allocation, nothing to do"
+    try:
+        ipaddr.IPv4Address(args.node)
+        node_type = "_ip"
+    except ipaddr.AddressValueError:
+        node_type = "_host"
+
+    banned = get_banned_nodes(node_type)
+    if args.node in banned:
+        print args.node + " already banned from allocation, nothing to do"
         return os.EX_OK
 
-    banned.append(args.IP)
-    if set_banned_ips(banned):
-        print "Banned " + args.IP
+    banned.append(args.node)
+    if set_banned_nodes(banned, node_type):
+        print "Banned " + args.node
         return os.EX_OK
     else:
-        print "Failed to ban " + args.IP
+        print "Failed to ban " + args.node
         return os.EX_UNAVAILABLE
 
 
@@ -180,21 +187,27 @@
 
 
 def es_unban_node():
-    if args.IP == "":
-        print "No IP address provided"
+    if args.node == "":
+        print "No node provided"
         return os.EX_UNAVAILABLE
 
-    banned = get_banned_ips()
-    if args.IP not in banned:
-        print args.IP + " not banned from allocation currently, nothing to do"
+    try:
+        ipaddr.IPv4Address(args.node)
+        node_type = "_ip"
+    except ipaddr.AddressValueError:
+        node_type = "_host"
+
+    banned = get_banned_nodes(node_type)
+    if args.node not in banned:
+        print args.node + " not banned from allocation, nothing to do"
         return os.EX_OK
 
-    banned.remove(args.IP)
-    if set_banned_ips(banned):
-        print "Unbanned " + args.IP
+    banned.remove(args.node)
+    if set_banned_nodes(banned, node_type):
+        print "Unbanned " + args.node
         return os.EX_OK
     else:
-        print "Failed to unban " + args.IP
+        print "Failed to unban " + args.node
         return os.EX_UNAVAILABLE
 
 # And register them here
@@ -214,8 +227,8 @@
 parser.add_argument("command", metavar='CMD', type=str,
                     choices=commands.keys(),
                     help="Subcommand, one of: " + ",".join(commands))
-parser.add_argument("IP", metavar='IP', type=str, nargs="?", default="",
-                    help="IP address, used by (un)ban-node")
+parser.add_argument("node", metavar='NODE', type=str, nargs="?", default="",
+                    help="IP address or hostname, used by (un)ban-node")
 parser.add_argument("--server", metavar='S', type=str, default="localhost",
                     help="Server to work on, default localhost")
 args = parser.parse_args()
diff --git a/modules/elasticsearch/manifests/init.pp 
b/modules/elasticsearch/manifests/init.pp
index 6f85855..838db52 100644
--- a/modules/elasticsearch/manifests/init.pp
+++ b/modules/elasticsearch/manifests/init.pp
@@ -163,7 +163,7 @@
             group   => 'root',
             mode    => '0755',
             source  => 'puppet:///modules/elasticsearch/es-tool',
-            require => Package['python-elasticsearch']
+            require => [Package['python-elasticsearch'], 
Package['python-ipaddr']]
         }
     }
 }
diff --git a/modules/elasticsearch/manifests/packages.pp 
b/modules/elasticsearch/manifests/packages.pp
index f3757b1..45d61f9 100644
--- a/modules/elasticsearch/manifests/packages.pp
+++ b/modules/elasticsearch/manifests/packages.pp
@@ -23,5 +23,6 @@
     # library for elasticsearch. only in trusty+
     if os_version('ubuntu >= trusty') {
         package { 'python-elasticsearch': ensure => present }
+        package { 'python-ipaddr': ensure => present }
     }
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/180210
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Icabca481430e8830c2dae998c7876f9e9a3ec311
Gerrit-PatchSet: 5
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Chad <ch...@wikimedia.org>
Gerrit-Reviewer: Andrew Bogott <abog...@wikimedia.org>
Gerrit-Reviewer: Chad <ch...@wikimedia.org>
Gerrit-Reviewer: Filippo Giunchedi <fgiunch...@wikimedia.org>
Gerrit-Reviewer: Manybubbles <never...@wikimedia.org>
Gerrit-Reviewer: Matanya <mata...@foss.co.il>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to