Filippo Giunchedi has submitted this change and it was merged.

Change subject: zuul: client to easily query Gearman server
......................................................................


zuul: client to easily query Gearman server

Zuul spawn a Gearman server which can be queried over TCP with a
oneliner such as:

  echo status|nc -q 3 localhost 4730

Documented at:
https://www.mediawiki.org/wiki/Continuous_integration/Zuul#Debugging

I find it cumbersome to remember the possible commands 'status' and
'workers', make it a python script so ones can easily query the server.

It defaults to querying the server on 127.0.0.1, so only install the
utility on host having zuul::server (gallium.wikimedia.org).

Script tested there and working fine, simply:

 zuul-gearman.py status
 zuul-gearman.py workers

Change-Id: I9be8e020aed3ee1b110adc09fe7d5ba9d6162387
---
A modules/zuul/files/zuul-gearman.py
M modules/zuul/manifests/server.pp
2 files changed, 96 insertions(+), 0 deletions(-)

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



diff --git a/modules/zuul/files/zuul-gearman.py 
b/modules/zuul/files/zuul-gearman.py
new file mode 100755
index 0000000..6d7975e
--- /dev/null
+++ b/modules/zuul/files/zuul-gearman.py
@@ -0,0 +1,87 @@
+#!/usr/bin/env python
+"""Easily send admin requests to a Gearman server."""
+# Copyright 2014 Antoine "hashar" Musso
+# Copyright 2014 Wikimedia Foundation Inc.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import argparse
+import gear
+import sys
+
+map_cmd_class = {
+    'cancel job': 'CancelJobAdminRequest',
+    'show jobs': 'ShowJobsAdminRequest',
+    'show unique jobs': 'ShowUniqueJobsAdminRequest',
+    'status': 'StatusAdminRequest',
+    'version': 'VersionAdminRequest',
+    'workers': 'WorkersAdminRequest',
+}
+
+
+class GearAdminRequestAction(argparse.Action):
+
+    """
+    Validates a user command
+    """
+
+    def __call__(self, parser, namespace, values, option_string=None):
+        if not len(values) > 0:
+            parser.error('must be given a command')
+
+        cmd = ' '.join([v.lower() for v in values])
+        if cmd not in map_cmd_class:
+            parser.error("invalid command '%s'" % cmd)
+        setattr(namespace, self.dest, cmd)
+
+
+parser = argparse.ArgumentParser(
+    description=__doc__,
+    formatter_class=argparse.RawTextHelpFormatter)
+
+parser.add_argument('--server', default='127.0.0.1',
+                    help='Gearman server to connect to')
+parser.add_argument('--timeout', default='30', type=int,
+                    help='timeout in seconds to connect to server')
+
+parser.add_argument(
+    'command',
+    help="\n".join(sorted(map_cmd_class)),
+    nargs=argparse.REMAINDER,
+    action=GearAdminRequestAction)
+
+opts = parser.parse_args()
+
+req_class = map_cmd_class[opts.command]
+try:
+    req = getattr(gear, req_class)()
+except AttributeError:
+    sys.stderr.write("Command '%s' not implemented.\n"
+                     "gear.%s does not exist.\n"
+                     % (opts.command, req_class))
+    exit(1)
+
+client = gear.Client('zuul-gearman.py')
+client.addServer(opts.server)
+client.waitForServer()
+server = client.getConnection()
+
+exit_code = 1
+try:
+    server.sendAdminRequest(req, timeout=opts.timeout)
+    print req.response
+    exit_code = 0
+except gear.TimeoutError:
+    print "Server timeout exceeded (%s)" % opts.server
+finally:
+    exit(exit_code)
diff --git a/modules/zuul/manifests/server.pp b/modules/zuul/manifests/server.pp
index 0c97eef..397df2c 100644
--- a/modules/zuul/manifests/server.pp
+++ b/modules/zuul/manifests/server.pp
@@ -83,6 +83,15 @@
         ],
     }
 
+    file { '/usr/local/bin/zuul-gearman.py':
+        ensure  => present,
+        owner   => 'root',
+        group   => 'root',
+        mode    => '0555',
+        require => Package['python-gear'],
+        source  => 'puppet:///modules/zuul/zuul-gearman.py',
+    }
+
     # Additionally provide a publicly readeable configuration file
     exec { 'craft public zuul conf':
         cwd         => '/etc/zuul/',

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I9be8e020aed3ee1b110adc09fe7d5ba9d6162387
Gerrit-PatchSet: 3
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Hashar <has...@free.fr>
Gerrit-Reviewer: Alexandros Kosiaris <akosia...@wikimedia.org>
Gerrit-Reviewer: Filippo Giunchedi <fgiunch...@wikimedia.org>
Gerrit-Reviewer: Giuseppe Lavagetto <glavage...@wikimedia.org>
Gerrit-Reviewer: Hashar <has...@free.fr>
Gerrit-Reviewer: Krinkle <krinklem...@gmail.com>
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