-
Notifications
You must be signed in to change notification settings - Fork 846
add ability to terminate execution of a step-fn state machine #1695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
14943d9
add ability to terminate execution of a step-fn state machine
madhur-ob ebc530e
don't use f-strings
madhur-ob 923eec4
remove checks for project, branch
madhur-ob 750c0c3
add comment
madhur-ob d7400b1
suggested changes
madhur-ob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,10 @@ class IncorrectProductionToken(MetaflowException): | |
headline = "Incorrect production token" | ||
|
||
|
||
class RunIdMismatch(MetaflowException): | ||
headline = "Run ID mismatch" | ||
|
||
|
||
class IncorrectMetadataServiceVersion(MetaflowException): | ||
headline = "Incorrect version for metaflow service" | ||
|
||
|
@@ -614,6 +618,83 @@ def _token_instructions(flow_name, prev_user): | |
) | ||
|
||
|
||
@step_functions.command(help="Terminate flow execution on Step Functions.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor nit - but for consistency |
||
@click.option( | ||
"--authorize", | ||
default=None, | ||
type=str, | ||
help="Authorize the termination with a production token", | ||
) | ||
@click.argument("run-id", required=True, type=str) | ||
@click.pass_obj | ||
def terminate(obj, run_id, authorize=None): | ||
def _token_instructions(flow_name, prev_user): | ||
obj.echo( | ||
"There is an existing version of *%s* on AWS Step Functions which was " | ||
"deployed by the user *%s*." % (flow_name, prev_user) | ||
) | ||
obj.echo( | ||
"To terminate this flow, you need to use the same production token that they used." | ||
) | ||
obj.echo( | ||
"Please reach out to them to get the token. Once you have it, call " | ||
"this command:" | ||
) | ||
obj.echo(" step-functions terminate --authorize MY_TOKEN RUN_ID", fg="green") | ||
obj.echo( | ||
'See "Organizing Results" at docs.metaflow.org for more information ' | ||
"about production tokens." | ||
) | ||
|
||
validate_run_id( | ||
obj.state_machine_name, obj.token_prefix, authorize, run_id, _token_instructions | ||
) | ||
|
||
# Trim prefix from run_id | ||
name = run_id[4:] | ||
obj.echo( | ||
"Terminating run *{run_id}* for {flow_name} ...".format( | ||
run_id=run_id, flow_name=obj.flow.name | ||
), | ||
bold=True, | ||
) | ||
|
||
terminated = StepFunctions.terminate(obj.state_machine_name, name) | ||
if terminated: | ||
obj.echo("\nRun terminated at %s." % terminated.get("stopDate")) | ||
|
||
|
||
def validate_run_id( | ||
state_machine_name, token_prefix, authorize, run_id, instructions_fn=None | ||
): | ||
if not run_id.startswith("sfn-"): | ||
raise RunIdMismatch( | ||
"Run IDs for flows executed through AWS Step Functions begin with 'sfn-'" | ||
) | ||
|
||
name = run_id[4:] | ||
execution = StepFunctions.get_execution(state_machine_name, name) | ||
if execution is None: | ||
raise MetaflowException( | ||
madhur-ob marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"Could not find the execution *%s* (in RUNNING state) for the state machine *%s* on AWS Step Functions" | ||
% (name, state_machine_name) | ||
) | ||
|
||
_, owner, token, _ = execution | ||
|
||
if authorize is None: | ||
authorize = load_token(token_prefix) | ||
elif authorize.startswith("production:"): | ||
authorize = authorize[11:] | ||
|
||
if owner != get_username() and authorize != token: | ||
if instructions_fn: | ||
instructions_fn(flow_name=name, prev_user=owner) | ||
raise IncorrectProductionToken("Try again with the correct production token.") | ||
|
||
return True | ||
|
||
|
||
def validate_token(name, token_prefix, authorize, instruction_fn=None): | ||
""" | ||
Validate that the production token matches that of the deployed flow. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.