def __init__(self):
if context.GLOBAL.cell is not None:
zkclient = context.GLOBAL.zk.conn
cell_state = CellState()
_LOGGER.info('Initializing api.')
watch_running(zkclient, cell_state)
watch_placement(zkclient, cell_state)
watch_finished(zkclient, cell_state)
watch_finished_history(zkclient, cell_state)
def _list(match=None, finished=False, partition=None):
"""List instances state."""
if match is None:
match = '*'
if '#' not in match:
match += '#*'
filtered = [
{'name': name, 'state': item['state'], 'host': item['host']}
for name, item in six.viewitems(cell_state.placement.copy())
if fnmatch.fnmatch(name, match)
]
if finished:
for name in six.viewkeys(cell_state.finished.copy()):
if fnmatch.fnmatch(name, match):
state = cell_state.get_finished(name)
item = {'name': name}
item.update(state)
filtered.append(item)
if partition is not None:
hosts = [rec['_id'] for rec in
API._get_server_info()
if rec['partition'] == partition]
filtered = [item for item in filtered
if item['host'] in hosts]
return sorted(filtered, key=lambda item: item['name'])
@schema.schema({'$ref': 'instance.json#/resource_id'})
def get(rsrc_id):
"""Get instance state."""
if rsrc_id in cell_state.placement:
state = cell_state.placement[rsrc_id]
else:
state = cell_state.get_finished(rsrc_id)
if not state:
return None
res = {'name': rsrc_id}
res.update(state)
return res
self.list = _list
self.get = get
评论列表
文章目录