Pyoungmeister has submitted this change and it was merged.

Change subject: adding a labsdb management tool and asociated class
......................................................................


adding a labsdb management tool and asociated class

Change-Id: If626c9bbab6f16847c95ab1584ec0560d5949939
---
A files/mysql/skrillex.py
M manifests/role/db.pp
A templates/mysql/skrillex.yaml.erb
3 files changed, 213 insertions(+), 0 deletions(-)

Approvals:
  Pyoungmeister: Verified; Looks good to me, approved



diff --git a/files/mysql/skrillex.py b/files/mysql/skrillex.py
new file mode 100755
index 0000000..7b26a0c
--- /dev/null
+++ b/files/mysql/skrillex.py
@@ -0,0 +1,134 @@
+#!/usr/bin/python
+
+import _mysql
+import argparse
+import re
+import sys
+import yaml
+
+
+def parseCommandLine():
+    """
+    Parses the command line arguments, and sets configuration options
+    in dictionary configuration.
+    """
+
+    parser = argparse.ArgumentParser(
+        description='skrillex [options] db::group  \"some sql;\"',
+        epilog='group must be one of the following:
+        all::all : all sanitarium and all labsdb instances
+        sanitarium::all : all sanitarium instances
+        labsdb::all : all labsdb instances
+        sanitarium::SHARD : specific sanitarium instance
+        labsdb::SHARD : specific labsdb instance')
+    parser.add_argument('-c', '--config',
+                        help='Specify yaml config file. Default 
/etc/skrill.yaml')
+    parser.add_argument('-q', '--query',
+                        help='Specify a MySQL query to run', required=True)
+    parser.add_argument('-g', '--group',
+                        help='Specify a group of mysql instances on which to 
run a query',
+                        required=True)
+    parser.add_argument('-a', '--forall', action='store_true',
+                        help='Run on all databases in the MySQL instance')
+    args = vars(parser.parse_args())
+
+    return args
+
+
+def loadConfig(configFile):
+    stream = open(configFile, 'r')
+    config = yaml.load(stream)
+    stream.close()
+
+    return config
+
+
+def setInstances(commandLineArg, topology):
+    executionDict = {}
+    fail = False
+
+    if commandLineArg == "all::all":
+        for logicalGroup, shard in topology.items():
+            key = logicalGroup + shard
+            executionDict[key] = topology[logicalGroup][shard]
+    elif re.match("(sanitarium|labsdb)::", commandLineArg):
+        logicalGroup = commandLineArg.split(":")[0]
+        shard = commandLineArg.split(":")[2]
+        if re.match("s[1-7]", shard):
+            executionDict = {shard: topology[logicalGroup][shard]}
+        elif shard == "all":
+            executionDict = topology[logicalGroup]
+        else:
+            fail = True
+    else:
+        fail = True
+
+    if fail is True:
+        print "Could not parse db/group. Please see help"
+        sys.exit(2)
+
+    return executionDict
+
+
+def executeQuery(instanceHost, instancePort,
+                 instancePassword, instanceQuery, forall):
+    con = None
+
+    try:
+        con = _mysql.connect(host=instanceHost, port=instancePort, user="root",
+                             passwd=instancePassword)
+        if forall is True:
+            con.query("show databases like '%wik%';")
+            d = con.store_result()
+            dbtuple = sum(d.fetch_row(maxrows=0), ())
+            for database in dbtuple:
+                con.select_db(database)
+                con.query(instanceQuery)
+                r = con.store_result()
+                rows = r.fetch_row(maxrows=0)
+                print "Output from {0}:{1} on {2} : {3}".format(
+                    instanceHost, str(instancePort), str(database), str(rows))
+
+        else:
+            con.query(instanceQuery)
+            r = con.store_result()
+            rows = r.fetch_row(maxrows=0)
+
+            print "Output from {0} on {1} : {2}".format(
+                instanceHost, str(database), str(rows))
+
+    except _mysql.Error, e:
+        print "Error %d: %s" % (e.args[0], e.args[1])
+        sys.exit(1)
+
+    finally:
+        if con:
+            con.close()
+
+
+def main():
+    ## Set any defaults that need setting
+    yamlConfig = '/etc/skrillex.yaml'
+
+    ## parse comnmand line and set defaults
+    args = parseCommandLine()
+    if args["config"] is None:
+        args["config"] = yamlConfig
+
+    ## load yaml file
+    topology = loadConfig(args["config"])
+
+    ## create dict of all instances that will be queried
+    executionInstances = setInstances(args["group"], topology)
+
+    ## iterate over dict and actuall run queries
+    for instance in executionInstances:
+        instanceHost = executionInstances[instance]["host"]
+        instancePort = executionInstances[instance]["port"]
+        instancePassword = executionInstances[instance]["mysqlpasswd"]
+
+        executeQuery(instanceHost, instancePort,
+                     instancePassword, args["query"], args["forall"])
+
+if __name__ == '__main__':
+    main()
diff --git a/manifests/role/db.pp b/manifests/role/db.pp
index 6b9e42d..6d9c088 100644
--- a/manifests/role/db.pp
+++ b/manifests/role/db.pp
@@ -92,3 +92,22 @@
                nrpe_command => "/usr/lib/nagios/plugins/check_procs -c 
${instances_count}:${instances_count} -C mysqld"
        }
 }
+
+class role::labsdb::manager {
+       package { ["python-mysqldb", "python-yaml"]:
+               ensure => present;
+       }
+
+       file {
+               "/usr/local/sbin/skrillex.py":
+                       owner => root,
+                       group => root,
+                       mode => 0500,
+                       source => "puppet:///files/mysql/skrillex.py";
+               "/etc/skrillex.yaml":
+                       owner => root,
+                       group => root,
+                       mode => 0400,
+                       content => template('mysql/skrillex.yaml');
+       }
+}
diff --git a/templates/mysql/skrillex.yaml.erb 
b/templates/mysql/skrillex.yaml.erb
new file mode 100644
index 0000000..ce9984f
--- /dev/null
+++ b/templates/mysql/skrillex.yaml.erb
@@ -0,0 +1,60 @@
+---
+sanitarium:
+    s1:
+        host: db1053.eqiad.wmnet
+        port: 3306
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_root_pass') %>
+    s2:
+        host: db1054.eqiad.wmnet
+        port: 3306
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_root_pass') %>
+    s3:
+        host: db1057.eqiad.wmnet
+        port: 3306
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_root_pass') %>
+    s4:
+        host: db1054.eqiad.wmnet
+        port: 3307
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_root_pass') %>
+    s5:
+        host: db1054.eqiad.wmnet
+        port: 3308
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_root_pass') %>
+    s6:
+        host: db1057.eqiad.wmnet
+        port: 3307
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_root_pass') %>
+    s7:
+        host: db1057.eqiad.wmnet
+        port: 3308
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_root_pass') %>
+labsdb:
+    s1:
+        host: labsdb1001.eqiad.wmnet
+        port: 3306
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_labsdb_root_pass') %>
+    s2:
+        host: labsdb1002.eqiad.wmnet
+        port: 3306
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_labsdb_root_pass') %>
+    s3:
+        host: labsdb1003.eqiad.wmnet
+        port: 3306
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_labsdb_root_pass') %>
+    s4:
+        host: labsdb1002.eqiad.wmnet
+        port: 3307
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_labsdb_root_pass') %>
+    s5:
+        host: labsdb1002.eqiad.wmnet
+        port: 3308
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_labsdb_root_pass') %>
+    s6:
+        host: labsdb1003.eqiad.wmnet
+        port: 3307
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_labsdb_root_pass') %>
+    s7:
+        host: labsdb1003.eqiad.wmnet
+        port: 3308
+        mysqlpasswd: <%= 
scope.lookupvar('passwords::misc::scripts::mysql_labsdb_root_pass') %>
+...

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If626c9bbab6f16847c95ab1584ec0560d5949939
Gerrit-PatchSet: 6
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Pyoungmeister <p...@wikimedia.org>
Gerrit-Reviewer: Pyoungmeister <p...@wikimedia.org>
Gerrit-Reviewer: Ryan Lane <rl...@wikimedia.org>

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

Reply via email to