From e48dc1ab020d715d67a32c4c50885029ecf23922 Mon Sep 17 00:00:00 2001 From: Dan Mick Date: Mon, 1 Jun 2015 16:02:20 -0700 Subject: [PATCH] lock.py: allow selection of machines to list by os-type/os-version Also, clean up the status selection a tiny bit with winnow() Signed-off-by: Dan Mick --- teuthology/lock.py | 56 ++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/teuthology/lock.py b/teuthology/lock.py index 3a078fab50..3eaf0b3909 100644 --- a/teuthology/lock.py +++ b/teuthology/lock.py @@ -151,6 +151,32 @@ def json_matching_statuses(json_file_or_str, statuses): return return_statuses +def winnow(statuses, arg, status_key, func=None): + """ + Call with a list of statuses, and the ctx. + 'arg' that you may want to filter by. + If arg is not None, filter statuses by either: + + 1) func=None: filter by status[status_key] == arg + remove any status that fails + + 2) func=: remove any + status for which func returns False + + Return the possibly-smaller set of statuses. + """ + + if arg is not None: + if func: + statuses = [_status for _status in statuses + if func(_status)] + else: + statuses = [_status for _status in statuses + if _status[status_key] == arg] + + return statuses + + def main(ctx): if ctx.verbose: teuthology.log.setLevel(logging.DEBUG) @@ -220,29 +246,21 @@ def main(ctx): # get statuses again to refresh any updated keys statuses = get_statuses(machines) if statuses: - if ctx.machine_type: - statuses = [_status for _status in statuses - if _status['machine_type'] == ctx.machine_type] + statuses = winnow(statuses, ctx.machine_type, 'machine_type') if not machines and ctx.owner is None and not ctx.all: ctx.owner = misc.get_user() - if ctx.owner is not None: - statuses = [_status for _status in statuses - if _status['locked_by'] == ctx.owner] - if ctx.status is not None: - statuses = [_status for _status in statuses - if _status['up'] == (ctx.status == 'up')] - if ctx.locked is not None: - statuses = [_status for _status in statuses - if _status['locked'] == (ctx.locked == 'true')] - if ctx.desc is not None: - statuses = [_status for _status in statuses - if _status['description'] == ctx.desc] - if ctx.desc_pattern is not None: - statuses = [_status for _status in statuses - if _status['description'] is not None and - _status['description'].find(ctx.desc_pattern) >= 0] + statuses = winnow(statuses, ctx.owner, 'locked_by') + statuses = winnow(statuses, ctx.status, 'up', + lambda s: s['up'] == (ctx.status == 'up')) + statuses = winnow(statuses, ctx.locked, 'locked', + lambda s: s['locked'] == (ctx.locked == 'true')) + statuses = winnow(statuses, ctx.desc, 'description') + statuses = winnow(statuses, ctx.desc_pattern, 'description', + lambda s: ctx.desc_pattern in s['description']) if ctx.json_query: statuses = json_matching_statuses(ctx.json_query, statuses) + statuses = winnow(statuses, ctx.os_type, 'os_type') + statuses = winnow(statuses, ctx.os_version, 'os_version') # When listing, only show the vm_host's name, not every detail for s in statuses: -- 2.39.5