Implemented uids history for solar cli commands
This commit is contained in:
parent
c8d75e128f
commit
42867869d1
21
solar/solar/cli/orch.py
Normal file → Executable file
21
solar/solar/cli/orch.py
Normal file → Executable file
@ -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))
|
||||
|
@ -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)
|
||||
|
||||
|
58
solar/solar/cli/uids_history.py
Normal file
58
solar/solar/cli/uids_history.py
Normal file
@ -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()
|
Loading…
x
Reference in New Issue
Block a user