Skip to content

Commit

Permalink
Report doomsday backup results to Slack
Browse files Browse the repository at this point in the history
  • Loading branch information
adobes1 committed Jan 16, 2025
1 parent 09b6419 commit dfe8f0b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
34 changes: 31 additions & 3 deletions pyartcd/pyartcd/pipelines/quay_doomsday_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ def __init__(self, runtime: Runtime, version: str, arches: Optional[str]):
self.runtime = runtime
self.version = version
self.workdir = "./workspace"
self.slack_client = self.runtime.new_slack_client()
self.slack_client.bind_channel(version)

self.arches = arches.split(",") if arches else ALL_ARCHES_LIST

async def sync_arch(self, arch: str):
async def sync_arch(self, arch: str) -> bool:
if arch not in ALL_ARCHES_LIST:
raise Exception(f"Invalid arch: {arch}")

Expand All @@ -54,25 +56,51 @@ async def sync_arch(self, arch: str):
self.runtime.logger.info("[%s] Mirror command ran successfully", arch)
if self.runtime.dry_run:
self.runtime.logger.info("[DRY RUN] [%s] Would have run %s", arch, " ".join(aws_cmd))
self.runtime.logger.info("[DRY RUN] [%s] Would have messaged Slack", arch)
else:
await asyncio.sleep(5)
self.runtime.logger.info("[%s] Running aws command: %s", arch, aws_cmd)
await retry(cmd_assert_async, aws_cmd)
self.runtime.logger.info("[%s] AWS command ran successfully", arch)
await asyncio.sleep(5)

await self.slack_client.say_in_thread(f":white_check_mark: Successfully synced {self.version}-{arch}")

except ChildProcessError as e:
self.runtime.logger.error("[%s] Failed to sync: %s", arch, e)
if self.runtime.dry_run:
self.runtime.logger.info("[DRY RUN] [%s] Would have messaged Slack", arch)
else:
await self.slack_client.say_in_thread(f":warning: Failed to sync {self.version}-{arch}: {e}")
return False

if os.path.exists(f"{self.workdir}/{path}"):
self.runtime.logger.info("[%s] Cleaning dir: %s", arch, f"{self.workdir}/{path}")
shutil.rmtree(f"{self.workdir}/{path}")

async def run(self):
return True

async def run(self) -> None:
mkdirs(self.workdir)

if not self.runtime.dry_run:
slack_response = await self.slack_client.say_in_thread(f":construction: Syncing arches {', '.join(self.arches)} of {self.version} to AWS S3 Bucket :construction:")
slack_channel_id = slack_response["channel"]
main_message_ts = slack_response["message"]["ts"]
else:
self.runtime.logger.info("[DRY RUN] Would have messaged Slack")

tasks = [self.sync_arch(arch) for arch in self.arches]
await asyncio.gather(*tasks)
results = await asyncio.gather(*tasks)

# Report the results to Slack
if not self.runtime.dry_run:
if all(results):
await self.slack_client._client.reactions_add(channel=slack_channel_id, timestamp=main_message_ts, name="done_it_is")
else:
await self.slack_client.say_in_thread(":x: Failed to sync some arches", broadcast=True)
else:
self.runtime.logger.info("[DRY RUN] Would have messaged Slack")


@cli.command("quay-doomsday-backup", help="Run doomsday pipeline for the specified version and all arches unless --arches is specified")
Expand Down
8 changes: 4 additions & 4 deletions pyartcd/pyartcd/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ def bind_channel(self, channel_or_release: Optional[str]):
else:
raise ValueError(f"Invalid channel_or_release value: {channel_or_release}")

async def say_in_thread(self, message: str, reaction: Optional[str] = None):
async def say_in_thread(self, message: str, reaction: Optional[str] = None, broadcast: bool = False):
if not self._thread_ts:
response_data = await self.say(message, thread_ts=None, reaction=reaction)
self._thread_ts = response_data["ts"]
return response_data
else:
return await self.say(message, thread_ts=self._thread_ts, reaction=reaction)
return await self.say(message, thread_ts=self._thread_ts, reaction=reaction, broadcast = broadcast)

async def say(self, message: str, thread_ts: Optional[str] = None, reaction: Optional[str] = None):
async def say(self, message: str, thread_ts: Optional[str] = None, reaction: Optional[str] = None, broadcast: bool = False):
attachments = []
if self.build_url:
attachments.append({
Expand All @@ -62,7 +62,7 @@ async def say(self, message: str, thread_ts: Optional[str] = None, reaction: Opt
return {"message": {"ts": "fake"}, "ts": "fake"}
response = await self._client.chat_postMessage(channel=self.channel, text=message, thread_ts=thread_ts,
username=self.as_user, link_names=True, attachments=attachments,
icon_emoji=self.icon_emoji, reply_broadcast=False)
icon_emoji=self.icon_emoji, reply_broadcast=broadcast)
# https://api.slack.com/methods/reactions.add
if reaction:
await self._client.reactions_add(
Expand Down

0 comments on commit dfe8f0b

Please sign in to comment.