refactor allocations into a single class
authorDustin J. Mitchell <dustin@mozilla.com>
Thu, 13 Jan 2011 14:38:28 -0600
changeset 1243 c2c98b2f0f0b6ca9980e438b1928746af00a85d7
parent 1242 d350507ff2407b80b3381c2d08e8c1abe8c1b144
child 1244 6c385324fb9ee85668b1f87c67556a8cedea2773
push id894
push userdmitchell@mozilla.com
push dateFri, 18 Mar 2011 22:21:21 +0000
refactor allocations into a single class
.hgignore
lib/python/slavealloc/daemon/service.py
lib/python/slavealloc/logic/allocate.py
lib/python/slavealloc/logic/buildbottac.py
lib/python/slavealloc/scripts/gettac.py
--- a/.hgignore
+++ b/.hgignore
@@ -1,2 +1,3 @@
 slavealloc.db
 .*.swp
+twistd.pid
--- a/lib/python/slavealloc/daemon/service.py
+++ b/lib/python/slavealloc/daemon/service.py
@@ -25,24 +25,24 @@ class AllocatorService(service.Service):
     def getBuildbotTac(self, slave_name):
         # slave allocation is *synchronous* and happens in the main thread.
         # For now, this is a good thing - it allows us to ensure that multiple
         # slaves are not simultaneously reassigned.  This should be revisited
         # later.
         d = defer.succeed(None)
         def gettac(_):
             try:
-                allocation = allocate.get_allocation(slave_name)
+                allocation = allocate.Allocation(slave_name)
             except exceptions.NoAllocationError:
                 log.msg("rejecting slave '%s'" % slave_name)
                 raise
 
             tac = buildbottac.make_buildbot_tac(allocation)
 
             allocation.commit()
             log.msg("allocated '%s' to '%s' (%s:%s)" % (slave_name,
-                    allocation.master_row.nickname,
-                    allocation.master_row.fqdn,
-                    allocation.master_row.pb_port))
+                    allocation.master_nickname,
+                    allocation.master_fqdn,
+                    allocation.master_pb_port))
 
             return tac
         d.addCallback(gettac)
         return d
--- a/lib/python/slavealloc/logic/allocate.py
+++ b/lib/python/slavealloc/logic/allocate.py
@@ -1,53 +1,59 @@
 from slavealloc import exceptions
 from slavealloc.data import queries, model
 
 class Allocation(object):
-    """A container class to hold all of the information necessary to make an allocation"""
-    # (all fields filled in by get_allocation)
-    slavename = None # the slave name
-    slaveid = None # its slaveid
-    master_row = None # a row from the masters table
-    slave_row = None # the slave's row
-    slave_password = None # the slave's password
+    """
+    
+    A container class to hold all of the information necessary to make an allocation
+    
+    @ivar slavename: the slave name
+    @ivar slaveid: its slaveid
+    @ivar master_nickname: master's nickname
+    @iver master_fqdn: master's hostname
+    @ivar master_pb_port: master's slave pb_port
+    @ivar slave_basedir: the slave's basedir
+    @ivar slave_password: the slave's password
+    @ivar masterid: the assigned masterid
+    """
+
+    def __init__(self, slavename):
+        self.slavename = slavename
+
+        # slave info
+        q = model.slaves.select(whereclause=(model.slaves.c.name == slavename))
+        slave_row = q.execute().fetchone()
+        if not slave_row:
+            raise exceptions.NoAllocationError
+        self.slaveid = slave_row.slaveid
+        self.slave_basedir = slave_row.basedir
+
+        # slave password
+        q = queries.slave_password
+        self.slave_password = q.execute(slaveid=self.slaveid).scalar()
+
+        # if this slave has a locked_masterid, just get that row; otherwise, run
+        # the self algorithm
+        if slave_row.locked_masterid:
+            q = model.masters.select(whereclause=(
+                model.masters.c.masterid == slave_row.locked_masterid))
+            master_row = q.execute().fetchone()
+        else:
+            # TODO: use slaveid, lose a join
+            q = queries.best_master
+            master_row = q.execute(slavename=slavename).fetchone()
+
+        if not master_row:
+            raise exceptions.NoAllocationError
+        self.master_nickname = master_row.nickname
+        self.master_fqdn = master_row.fqdn
+        self.master_pb_port = master_row.pb_port
+        self.masterid = master_row.masterid
 
     def commit(self):
         """
         Commit this allocation to the database
         """
         q = model.slaves.update(whereclause=(model.slaves.c.slaveid == self.slaveid),
-                            values=dict(current_masterid=self.master_row.masterid))
+                            values=dict(current_masterid=self.masterid))
         q.execute()
 
-def get_allocation(slavename):
-    """
-    Return the C{masters} row for the master to which C{slavename}
-    should be assigned.
-    """
-    allocation = Allocation()
-    allocation.slavename = slavename
-
-    q = model.slaves.select(whereclause=(model.slaves.c.name == slavename))
-    allocation.slave_row = q.execute().fetchone()
-    if not allocation.slave_row:
-        raise exceptions.NoAllocationError
-
-    allocation.slaveid = allocation.slave_row.slaveid
-
-    q = queries.slave_password
-    allocation.slave_password = q.execute(slaveid=allocation.slaveid).scalar()
-
-    # if this slave has a locked_masterid, just get that row; otherwise, run
-    # the allocation algorithm
-    if allocation.slave_row.locked_masterid:
-        q = model.masters.select(whereclause=(
-            model.masters.c.masterid == allocation.slave_row.locked_masterid))
-        allocation.master_row = q.execute().fetchone()
-    else:
-        # TODO: use slaveid, lose a join
-        q = queries.best_master
-        allocation.master_row = q.execute(slavename=slavename).fetchone()
-
-    if not allocation.master_row:
-        raise exceptions.NoAllocationError
-
-    return allocation
--- a/lib/python/slavealloc/logic/buildbottac.py
+++ b/lib/python/slavealloc/logic/buildbottac.py
@@ -32,15 +32,15 @@ s = BuildSlave(buildmaster_host, port, s
 s.setServiceParent(application)
 """
 
 def make_buildbot_tac(allocation):
     info = dict()
 
     info['gendate'] = time.ctime()
     info['genhost'] = socket.getfqdn()
-    info['buildmaster_host'] = allocation.master_row.fqdn
-    info['port'] = allocation.master_row.pb_port
+    info['buildmaster_host'] = allocation.master_fqdn
+    info['port'] = allocation.master_pb_port
     info['slavename'] = allocation.slavename
-    info['basedir'] = allocation.slave_row.basedir
+    info['basedir'] = allocation.slave_basedir
     info['passwd'] = allocation.slave_password
 
     return tac_template % info
--- a/lib/python/slavealloc/scripts/gettac.py
+++ b/lib/python/slavealloc/scripts/gettac.py
@@ -18,22 +18,22 @@ def process_args(subparser, args):
     if not args.slave:
         subparser.error("at least one slave name is required")
     if '.' in ''.join(args.slave):
         subparser.error("slave name must not contain '.'; give the unqualified hostname")
 
 def main(args):
     for slave in args.slave:
         try:
-            allocation = allocate.get_allocation(slave)
+            allocation = allocate.Allocation(slave)
         except exceptions.NoAllocationError:
             print >>sys.stderr, "No buildbot.tac available (404 from slave allocator)"
             sys.exit(1)
 
         if not args.quiet:
             print buildbottac.make_buildbot_tac(allocation)
 
         if not args.noop:
             allocation.commit()
         print >>sys.stderr, "Allocated '%s' to '%s' (%s:%s)" % (slave,
-                allocation.master_row.nickname,
-                allocation.master_row.fqdn,
-                allocation.master_row.pb_port)
+                allocation.master_nickname,
+                allocation.master_fqdn,
+                allocation.master_pb_port)