Skip to content

Commit

Permalink
fixing wait-for-run-to-complete
Browse files Browse the repository at this point in the history
  • Loading branch information
eamonnfaherty committed Oct 15, 2024
1 parent ec15db6 commit 4ba3f9a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 125 deletions.
5 changes: 3 additions & 2 deletions servicecatalog_puppet/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,11 +1022,12 @@ def wait_for_code_build_in(iam_role_arns):


@cli.command()
@click.argument("execution")
@click.option("--on-complete-url", default=None)
def wait_for_parameterised_run_to_complete(on_complete_url):
def wait_for_run_to_complete(execution, on_complete_url):
setup_config(puppet_account_id=remote_config.get_puppet_account_id())
click.echo("Starting to wait for parameterised run to complete")
succeeded = misc_commands.wait_for_parameterised_run_to_complete(on_complete_url)
succeeded = misc_commands.wait_for_run_to_complete(execution, on_complete_url)
if succeeded:
click.echo(f"Finished: 'SUCCESS'")
sys.exit(0)
Expand Down
189 changes: 72 additions & 117 deletions servicecatalog_puppet/commands/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from servicecatalog_puppet import aws, config, constants


logger = logging.getLogger(constants.PUPPET_LOGGER_NAME)


Expand Down Expand Up @@ -57,7 +56,7 @@ def wait_for_code_build_in(iam_role_arns):
index += 1

with betterboto_client.CrossMultipleAccountsClientContextManager(
"codebuild", cross_accounts
"codebuild", cross_accounts
) as codebuild:
while True:
try:
Expand All @@ -77,7 +76,7 @@ def wait_for_cloudformation_in(iam_role_arns):
index += 1

with betterboto_client.CrossMultipleAccountsClientContextManager(
"cloudformation", cross_accounts
"cloudformation", cross_accounts
) as cloudformation:
while True:
try:
Expand All @@ -89,121 +88,77 @@ def wait_for_cloudformation_in(iam_role_arns):
logger.error(traceback.format_exc())


def wait_for_parameterised_run_to_complete(on_complete_url: str) -> bool:
with betterboto_client.ClientContextManager("s3") as s3:
paginator = s3.get_paginator("list_object_versions")
pages = paginator.paginate(
Bucket=f"sc-puppet-parameterised-runs-{config.get_puppet_account_id()}",
)
for page in pages:
for version in page.get("Versions", []):
if version.get("Key") == "parameters.zip" and version.get("IsLatest"):
parameters_file_version_id = version.get("VersionId")
while True:
time.sleep(5)
with betterboto_client.ClientContextManager(
"codepipeline"
) as codepipeline:
click.echo(
f"looking for execution for {parameters_file_version_id}"
)
paginator = codepipeline.get_paginator(
"list_pipeline_executions"
def wait_for_run_to_complete(pipeline_execution_id, on_complete_url: str) -> bool:
while True:
time.sleep(5)
with betterboto_client.ClientContextManager(
"codepipeline"
) as codepipeline:
while True:
time.sleep(10)
pipelineExecution = codepipeline.get_pipeline_execution(
pipelineName=constants.PIPELINE_NAME,
pipelineExecutionId=pipeline_execution_id,
).get(
"pipelineExecution"
)
status = pipelineExecution.get(
"status"
)
click.echo(
f"Current status (A): {status}"
)
if status in [
"Cancelled",
"Stopped",
"Succeeded",
"Superseded",
"Failed",
]:
succeeded = status in [
"Succeeded"
]
if on_complete_url:
logger.info(
f"About to post results"
)
if succeeded:
result = dict(
Status="SUCCESS",
Reason=f"All tasks run with success: {pipeline_execution_id}",
UniqueId=pipeline_execution_id.replace(
":", ""
).replace(
"-", ""
),
Data=f"{pipeline_execution_id}",
)
pages = paginator.paginate(
pipelineName=constants.PIPELINE_NAME,
PaginationConfig={"PageSize": 100,},
else:
result = dict(
Status="FAILURE",
Reason=f"All tasks did not run with success: {pipeline_execution_id}",
UniqueId=pipeline_execution_id.replace(
":", ""
).replace(
"-", ""
),
Data=f"{pipeline_execution_id}",
)
for page in pages:
for pipeline_execution_summary in page.get(
"pipelineExecutionSummaries", []
):
if (
pipeline_execution_summary.get("trigger").get(
"triggerDetail"
)
== "ParameterisedSource"
):
for s in pipeline_execution_summary.get(
"sourceRevisions", []
):
if (
s.get("actionName")
== "ParameterisedSource"
and s.get("revisionId")
== parameters_file_version_id
):
pipeline_execution_id = pipeline_execution_summary.get(
"pipelineExecutionId"
)
click.echo(
f"Found execution id {pipeline_execution_id}"
)
while True:
time.sleep(10)
pipelineExecution = codepipeline.get_pipeline_execution(
pipelineName=constants.PIPELINE_NAME,
pipelineExecutionId=pipeline_execution_id,
).get(
"pipelineExecution"
)
status = pipelineExecution.get(
"status"
)
click.echo(
f"Current status (A): {status}"
)
if status in [
"Cancelled",
"Stopped",
"Succeeded",
"Superseded",
"Failed",
]:
succeeded = status in [
"Succeeded"
]
if on_complete_url:
logger.info(
f"About to post results"
)
if succeeded:
result = dict(
Status="SUCCESS",
Reason=f"All tasks run with success: {pipeline_execution_id}",
UniqueId=pipeline_execution_id.replace(
":", ""
).replace(
"-", ""
),
Data=f"{pipeline_execution_id}",
)
else:
result = dict(
Status="FAILURE",
Reason=f"All tasks did not run with success: {pipeline_execution_id}",
UniqueId=pipeline_execution_id.replace(
":", ""
).replace(
"-", ""
),
Data=f"{pipeline_execution_id}",
)
req = urllib.request.Request(
url=on_complete_url,
data=json.dumps(
result
).encode(),
method="PUT",
)
with urllib.request.urlopen(
req
) as f:
pass
logger.info(f.status)
logger.info(f.reason)

return succeeded
req = urllib.request.Request(
url=on_complete_url,
data=json.dumps(
result
).encode(),
method="PUT",
)
with urllib.request.urlopen(
req
) as f:
pass
logger.info(f.status)
logger.info(f.reason)

return succeeded


def run(what, tail):
Expand All @@ -217,6 +172,6 @@ def run(what, tail):

def uninstall(puppet_account_id):
with betterboto_client.ClientContextManager(
"cloudformation", region_name=config.get_home_region(puppet_account_id)
"cloudformation", region_name=config.get_home_region(puppet_account_id)
) as cloudformation:
cloudformation.ensure_deleted(StackName=constants.BOOTSTRAP_STACK_NAME)
12 changes: 6 additions & 6 deletions servicecatalog_puppet/template_builder/hub/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,12 +1347,12 @@ def generate_single_account_run_projects(
install=install_spec,
build={
"commands": [
"aws codepipeline start-pipeline-execution --name servicecatalog-puppet-pipeline --variables name=SINGLE_ACCOUNT,value=${SINGLE_ACCOUNT_ID}"
"export execution=$(aws codepipeline start-pipeline-execution --name servicecatalog-puppet-pipeline --variables name=SINGLE_ACCOUNT,value=${SINGLE_ACCOUNT_ID} --query pipelineExecutionId --output text)"
]
},
post_build={
"commands": [
"servicecatalog-puppet wait-for-parameterised-run-to-complete",
"servicecatalog-puppet wait-for-run-to-complete $execution"
]
},
),
Expand Down Expand Up @@ -1397,7 +1397,7 @@ def generate_single_account_run_projects(
codebuild.Project("SingleAccountRunProject", **single_account_run_project_args)
)
single_account_run_project_build_spec["phases"]["post_build"]["commands"] = [
"servicecatalog-puppet wait-for-parameterised-run-to-complete --on-complete-url $CALLBACK_URL"
"servicecatalog-puppet wait-for-run-to-complete $execution --on-complete-url $CALLBACK_URL"
]
single_account_run_project_args[
"Name"
Expand Down Expand Up @@ -1432,12 +1432,12 @@ def generate_single_action_run_projects(
install=install_spec,
build={
"commands": [
"aws codepipeline start-pipeline-execution --name servicecatalog-puppet-pipeline --variables name=SINGLE_ACTION_SECTION,value=${SINGLE_ACTION_SECTION} name=SINGLE_ACTION_ITEM,value=${SINGLE_ACTION_ITEM} name=SINGLE_ACTION_INCLUDE_DEPENDENCIES,value=${SINGLE_ACTION_INCLUDE_DEPENDENCIES} name=SINGLE_ACTION_INCLUDE_REVERSE_DEPENDENCIES,value=${SINGLE_ACTION_INCLUDE_REVERSE_DEPENDENCIES}"
"export execution=$(aws codepipeline start-pipeline-execution --name servicecatalog-puppet-pipeline --variables name=SINGLE_ACTION_SECTION,value=${SINGLE_ACTION_SECTION} name=SINGLE_ACTION_ITEM,value=${SINGLE_ACTION_ITEM} name=SINGLE_ACTION_INCLUDE_DEPENDENCIES,value=${SINGLE_ACTION_INCLUDE_DEPENDENCIES} name=SINGLE_ACTION_INCLUDE_REVERSE_DEPENDENCIES,value=${SINGLE_ACTION_INCLUDE_REVERSE_DEPENDENCIES} --query pipelineExecutionId --output text)"
]
},
post_build={
"commands": [
"servicecatalog-puppet wait-for-parameterised-run-to-complete",
"servicecatalog-puppet wait-for-run-to-complete $execution",
]
},
),
Expand Down Expand Up @@ -1497,7 +1497,7 @@ def generate_single_action_run_projects(
codebuild.Project("SingleActionRunProject", **single_action_run_project_args)
)
single_action_run_project_build_spec["phases"]["post_build"]["commands"] = [
"servicecatalog-puppet wait-for-parameterised-run-to-complete --on-complete-url $CALLBACK_URL"
"servicecatalog-puppet wait-for-run-to-complete $execution --on-complete-url $CALLBACK_URL"
]
single_action_run_project_args[
"Name"
Expand Down

0 comments on commit 4ba3f9a

Please sign in to comment.