Merge pull request #70 from Mirantis/jnowak/cli_uids_history
Uids history for solar cli commands
This commit is contained in:
commit
9aa6bab574
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 graph
|
||||||
from solar.orchestration import tasks
|
from solar.orchestration import tasks
|
||||||
|
from solar.cli.uids_history import SOLARUID
|
||||||
|
|
||||||
|
|
||||||
@click.group(name='orch')
|
@click.group(name='orch')
|
||||||
@ -29,14 +30,14 @@ def create(plan):
|
|||||||
|
|
||||||
|
|
||||||
@orchestration.command()
|
@orchestration.command()
|
||||||
@click.argument('uid')
|
@click.argument('uid', type=SOLARUID)
|
||||||
@click.argument('plan', type=click.File('rb'))
|
@click.argument('plan', type=click.File('rb'))
|
||||||
def update(uid, plan):
|
def update(uid, plan):
|
||||||
graph.update_plan(uid, plan.read())
|
graph.update_plan(uid, plan.read())
|
||||||
|
|
||||||
|
|
||||||
@orchestration.command()
|
@orchestration.command()
|
||||||
@click.argument('uid')
|
@click.argument('uid', type=SOLARUID)
|
||||||
def report(uid):
|
def report(uid):
|
||||||
colors = {
|
colors = {
|
||||||
'PENDING': 'cyan',
|
'PENDING': 'cyan',
|
||||||
@ -53,7 +54,7 @@ def report(uid):
|
|||||||
click.echo(click.style(msg, fg=colors[item[1]]))
|
click.echo(click.style(msg, fg=colors[item[1]]))
|
||||||
|
|
||||||
@orchestration.command(name='run-once')
|
@orchestration.command(name='run-once')
|
||||||
@click.argument('uid')
|
@click.argument('uid', type=SOLARUID)
|
||||||
@click.option('--start', default=None)
|
@click.option('--start', default=None)
|
||||||
@click.option('--end', default=None)
|
@click.option('--end', default=None)
|
||||||
def run_once(uid, start, end):
|
def run_once(uid, start, end):
|
||||||
@ -63,20 +64,20 @@ def run_once(uid, start, end):
|
|||||||
queue='scheduler')
|
queue='scheduler')
|
||||||
|
|
||||||
@orchestration.command()
|
@orchestration.command()
|
||||||
@click.argument('uid')
|
@click.argument('uid', type=SOLARUID)
|
||||||
def restart(uid):
|
def restart(uid):
|
||||||
graph.reset(uid)
|
graph.reset(uid)
|
||||||
tasks.schedule_start.apply_async(args=[uid], queue='scheduler')
|
tasks.schedule_start.apply_async(args=[uid], queue='scheduler')
|
||||||
|
|
||||||
|
|
||||||
@orchestration.command()
|
@orchestration.command()
|
||||||
@click.argument('uid')
|
@click.argument('uid', type=SOLARUID)
|
||||||
def reset(uid):
|
def reset(uid):
|
||||||
graph.reset(uid)
|
graph.reset(uid)
|
||||||
|
|
||||||
|
|
||||||
@orchestration.command()
|
@orchestration.command()
|
||||||
@click.argument('uid')
|
@click.argument('uid', type=SOLARUID)
|
||||||
def stop(uid):
|
def stop(uid):
|
||||||
# TODO(dshulyak) how to do "hard" stop?
|
# TODO(dshulyak) how to do "hard" stop?
|
||||||
# using revoke(terminate=True) will lead to inability to restart execution
|
# using revoke(terminate=True) will lead to inability to restart execution
|
||||||
@ -86,21 +87,21 @@ def stop(uid):
|
|||||||
|
|
||||||
|
|
||||||
@orchestration.command()
|
@orchestration.command()
|
||||||
@click.argument('uid')
|
@click.argument('uid', type=SOLARUID)
|
||||||
def resume(uid):
|
def resume(uid):
|
||||||
graph.reset(uid, ['SKIPPED'])
|
graph.reset(uid, ['SKIPPED'])
|
||||||
tasks.schedule_start.apply_async(args=[uid], queue='scheduler')
|
tasks.schedule_start.apply_async(args=[uid], queue='scheduler')
|
||||||
|
|
||||||
|
|
||||||
@orchestration.command()
|
@orchestration.command()
|
||||||
@click.argument('uid')
|
@click.argument('uid', type=SOLARUID)
|
||||||
def retry(uid):
|
def retry(uid):
|
||||||
graph.reset(uid, ['ERROR'])
|
graph.reset(uid, ['ERROR'])
|
||||||
tasks.schedule_start.apply_async(args=[uid], queue='scheduler')
|
tasks.schedule_start.apply_async(args=[uid], queue='scheduler')
|
||||||
|
|
||||||
|
|
||||||
@orchestration.command()
|
@orchestration.command()
|
||||||
@click.argument('uid')
|
@click.argument('uid', type=SOLARUID)
|
||||||
def dg(uid):
|
def dg(uid):
|
||||||
plan = graph.get_graph(uid)
|
plan = graph.get_graph(uid)
|
||||||
|
|
||||||
@ -122,6 +123,6 @@ def dg(uid):
|
|||||||
|
|
||||||
|
|
||||||
@orchestration.command()
|
@orchestration.command()
|
||||||
@click.argument('uid')
|
@click.argument('uid', type=SOLARUID)
|
||||||
def show(uid):
|
def show(uid):
|
||||||
click.echo(graph.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 change
|
||||||
from solar.system_log import operations
|
from solar.system_log import operations
|
||||||
from solar.system_log import data
|
from solar.system_log import data
|
||||||
|
from solar.cli.uids_history import get_uid, remember_uid, SOLARUID
|
||||||
|
|
||||||
|
|
||||||
@click.group()
|
@click.group()
|
||||||
@ -35,11 +36,13 @@ def stage():
|
|||||||
|
|
||||||
@changes.command()
|
@changes.command()
|
||||||
def process():
|
def process():
|
||||||
click.echo(change.send_to_orchestration())
|
uid = change.send_to_orchestration()
|
||||||
|
remember_uid(uid)
|
||||||
|
click.echo(uid)
|
||||||
|
|
||||||
|
|
||||||
@changes.command()
|
@changes.command()
|
||||||
@click.argument('uid')
|
@click.argument('uid', type=SOLARUID)
|
||||||
def commit(uid):
|
def commit(uid):
|
||||||
operations.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