Add request-list nodepool command

Command to list all node requests in ZooKeeper.

Change-Id: I2437aa69b3aacf8a0c71466af55aab539271789c
This commit is contained in:
David Shrewsbury 2017-03-13 16:35:28 -04:00
parent 1fdb050207
commit 6595344a48
5 changed files with 57 additions and 1 deletions

View File

@ -125,6 +125,11 @@ class NodePoolCmd(NodepoolApp):
help='Validate configuration file')
cmd_config_validate.set_defaults(func=self.config_validate)
cmd_request_list = subparsers.add_parser(
'request-list',
help='list the current node requests')
cmd_request_list.set_defaults(func=self.request_list)
self.args = parser.parse_args()
def setup_logging(self):
@ -306,6 +311,9 @@ class NodePoolCmd(NodepoolApp):
log.info("Configuration validation complete")
#TODO(asselin,yolanda): add validation of secure.conf
def request_list(self):
print status.request_list(self.zk)
def _wait_for_threads(self, threads):
for t in threads:
if t:
@ -325,7 +333,8 @@ class NodePoolCmd(NodepoolApp):
if self.args.command in ('image-build', 'dib-image-list',
'image-list', 'dib-image-delete',
'image-delete', 'alien-image-list',
'alien-list', 'list', 'hold', 'delete'):
'alien-list', 'list', 'hold', 'delete',
'request-list'):
self.zk = zk.ZooKeeper()
self.zk.connect(config.zookeeper_servers.values())

View File

@ -111,3 +111,14 @@ def image_list(zk):
upload.state,
age(upload.state_time)])
return str(t)
def request_list(zk):
t = PrettyTable(["Request ID", "State", "Requestor", "Node Types", "Nodes",
"Declined By"])
t.align = 'l'
for req in zk.nodeRequestIterator():
t.add_row([req.id, req.state, req.requestor,
','.join(req.node_types),
','.join(req.nodes),
','.join(req.declined_by)])
return str(t)

View File

@ -266,3 +266,20 @@ class TestNodepoolCMD(tests.DBTestCase):
self.waitForImage('fake-provider', 'fake-image', [image])
self.assert_listed(configfile, ['dib-image-list'], 4, zk.READY, 2)
def test_request_list(self):
configfile = self.setup_config('node.yaml')
pool = self.useNodepool(configfile, watermark_sleep=1)
self._useBuilder(configfile)
pool.start()
self.waitForImage( 'fake-provider', 'fake-image')
nodes = self.waitForNodes('fake-label')
self.assertEqual(len(nodes), 1)
req = zk.NodeRequest()
req.state = zk.PENDING # so it will be ignored
req.node_types = ['fake-label']
req.requestor = 'test_request_list'
self.zk.storeNodeRequest(req)
self.assert_listed(configfile, ['request-list'], 0, req.id, 1)

View File

@ -613,6 +613,16 @@ class TestZooKeeper(tests.DBTestCase):
self.zk.unlockNodeRequest(req)
self.zk.deleteNodeRequest(req)
def test_nodeRequestIterator(self):
req = self._create_node_request()
self.zk.lockNodeRequest(req, blocking=False)
i = self.zk.nodeRequestIterator()
self.assertEqual(req, i.next())
with testtools.ExpectedException(StopIteration):
i.next()
self.zk.unlockNodeRequest(req)
self.zk.deleteNodeRequest(req)
def test_deleteNodeRequestLock(self):
req = self._create_node_request()
self.zk.lockNodeRequest(req, blocking=False)

View File

@ -1577,3 +1577,12 @@ class ZooKeeper(object):
lock = self.getNodeRequestLock(lock_id)
if lock:
yield lock
def nodeRequestIterator(self):
'''
Utility generator method for iterating through all nodes requests.
'''
for req_id in self.getNodeRequests():
req = self.getNodeRequest(req_id)
if req:
yield req