From 70c3557bf29324e763e5dfa36aa19b72fbfa8d83 Mon Sep 17 00:00:00 2001 From: Jedrzej Nowak Date: Fri, 21 Aug 2015 16:59:39 +0200 Subject: [PATCH] Implemented uids history for solar cli commands --- solar/solar/cli/orch.py | 21 ++++++------ solar/solar/cli/system_log.py | 7 ++-- solar/solar/cli/uids_history.py | 58 +++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 12 deletions(-) mode change 100644 => 100755 solar/solar/cli/orch.py create mode 100644 solar/solar/cli/uids_history.py diff --git a/solar/solar/cli/orch.py b/solar/solar/cli/orch.py old mode 100644 new mode 100755 index aa72f908..8e0f56d3 --- a/solar/solar/cli/orch.py +++ b/solar/solar/cli/orch.py @@ -7,6 +7,7 @@ import networkx as nx from solar.orchestration import graph from solar.orchestration import tasks +from solar.cli.uids_history import SOLARUID @click.group(name='orch') @@ -29,14 +30,14 @@ def create(plan): @orchestration.command() -@click.argument('uid') +@click.argument('uid', type=SOLARUID) @click.argument('plan', type=click.File('rb')) def update(uid, plan): graph.update_plan(uid, plan.read()) @orchestration.command() -@click.argument('uid') +@click.argument('uid', type=SOLARUID) def report(uid): colors = { 'PENDING': 'cyan', @@ -53,7 +54,7 @@ def report(uid): click.echo(click.style(msg, fg=colors[item[1]])) @orchestration.command(name='run-once') -@click.argument('uid') +@click.argument('uid', type=SOLARUID) @click.option('--start', default=None) @click.option('--end', default=None) def run_once(uid, start, end): @@ -63,20 +64,20 @@ def run_once(uid, start, end): queue='scheduler') @orchestration.command() -@click.argument('uid') +@click.argument('uid', type=SOLARUID) def restart(uid): graph.reset(uid) tasks.schedule_start.apply_async(args=[uid], queue='scheduler') @orchestration.command() -@click.argument('uid') +@click.argument('uid', type=SOLARUID) def reset(uid): graph.reset(uid) @orchestration.command() -@click.argument('uid') +@click.argument('uid', type=SOLARUID) def stop(uid): # TODO(dshulyak) how to do "hard" stop? # using revoke(terminate=True) will lead to inability to restart execution @@ -86,21 +87,21 @@ def stop(uid): @orchestration.command() -@click.argument('uid') +@click.argument('uid', type=SOLARUID) def resume(uid): graph.reset(uid, ['SKIPPED']) tasks.schedule_start.apply_async(args=[uid], queue='scheduler') @orchestration.command() -@click.argument('uid') +@click.argument('uid', type=SOLARUID) def retry(uid): graph.reset(uid, ['ERROR']) tasks.schedule_start.apply_async(args=[uid], queue='scheduler') @orchestration.command() -@click.argument('uid') +@click.argument('uid', type=SOLARUID) def dg(uid): plan = graph.get_graph(uid) @@ -122,6 +123,6 @@ def dg(uid): @orchestration.command() -@click.argument('uid') +@click.argument('uid', type=SOLARUID) def show(uid): click.echo(graph.show(uid)) diff --git a/solar/solar/cli/system_log.py b/solar/solar/cli/system_log.py index 896655f5..6dbe4987 100644 --- a/solar/solar/cli/system_log.py +++ b/solar/solar/cli/system_log.py @@ -8,6 +8,7 @@ from solar.core import resource from solar.system_log import change from solar.system_log import operations from solar.system_log import data +from solar.cli.uids_history import get_uid, remember_uid, SOLARUID @click.group() @@ -35,11 +36,13 @@ def stage(): @changes.command() def process(): - click.echo(change.send_to_orchestration()) + uid = change.send_to_orchestration() + remember_uid(uid) + click.echo(uid) @changes.command() -@click.argument('uid') +@click.argument('uid', type=SOLARUID) def commit(uid): operations.commit(uid) diff --git a/solar/solar/cli/uids_history.py b/solar/solar/cli/uids_history.py new file mode 100644 index 00000000..7a00e702 --- /dev/null +++ b/solar/solar/cli/uids_history.py @@ -0,0 +1,58 @@ +import click +import os +import re + +UIDS_HISTORY = os.path.join(os.getcwd(), '.solar_cli_uids') + + +def remember_uid(uid): + """ + Remembers last 3 uids. + Can be used then as `last`, `last1`, `last2` anywhere + """ + try: + with open(UIDS_HISTORY, 'rb') as f: + hist = [x.strip() for x in f.readlines()] + except IOError: + hist = [] + hist.insert(0, uid) + if len(hist) > 3: + hist = hist[:3] + with open(UIDS_HISTORY, 'wb') as f: + f.write('\n'.join(hist)) + + +def get_uid(given_uid): + """ + Converts given uid to real uid. + """ + matched = re.search('last(\d*)', given_uid) + if matched: + try: + position = int(matched.group(1)) + except ValueError: + position = 0 + with open(UIDS_HISTORY, 'rb') as f: + uids = [x.strip() for x in f.readlines()] + try: + return uids[position] + except IndexError: + # fallback to original + return given_uid + return given_uid + + +class SolarUIDParameterType(click.types.StringParamType): + """ + Type for solar changes uid. + Works like a string but can convert `last(\d+)` to valid uid. + """ + name = 'uid' + + def convert(self, value, param, ctx): + value = click.types.StringParamType.convert(self, value, param, ctx) + value = get_uid(value) + return value + + +SOLARUID = SolarUIDParameterType()