|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# ***************************************************************************** |
| 4 | +# Copyright (c) 2025 IBM Corporation and other Contributors. |
| 5 | +# |
| 6 | +# All rights reserved. This program and the accompanying materials |
| 7 | +# are made available under the terms of the Eclipse Public License v1.0 |
| 8 | +# which accompanies this distribution, and is available at |
| 9 | +# http://www.eclipse.org/legal/epl-v10.html |
| 10 | +# |
| 11 | +# ***************************************************************************** |
| 12 | + |
| 13 | +import argparse |
| 14 | +import os |
| 15 | +import sys |
| 16 | +from mas.devops.slack import SlackUtil |
| 17 | + |
| 18 | + |
| 19 | +def notifyProvisionFyre(channel: str, rc: int) -> bool: |
| 20 | + name = os.getenv("CLUSTER_NAME", None) |
| 21 | + if name is None: |
| 22 | + print("CLUSTER_NAME env var must be set") |
| 23 | + sys.exit(1) |
| 24 | + |
| 25 | + # Support optional metadata from standard IBM CD Toolchains environment variables |
| 26 | + toolchainLink = "" |
| 27 | + toolchainUrl = os.getenv("TOOLCHAIN_PIPELINERUN_URL", None) |
| 28 | + toolchainTriggerName = os.getenv("TOOLCHAIN_TRIGGER_NAME", None) |
| 29 | + if toolchainUrl is not None and toolchainTriggerName is not None: |
| 30 | + toolchainLink = f" | <{toolchainUrl}|Pipeline Run>" |
| 31 | + |
| 32 | + if rc == 0: |
| 33 | + url = os.getenv("OCP_CLUSTER_URL", None) |
| 34 | + username = os.getenv("OCP_USERNAME", None) |
| 35 | + password = os.getenv("OCP_PASSWORD", None) |
| 36 | + |
| 37 | + if url is None or username is None or password is None: |
| 38 | + print("OCP_CLUSTER_URL, OCP_USERNAME, and OCP_PASSWORD env vars must all be set") |
| 39 | + sys.exit(1) |
| 40 | + |
| 41 | + message = [ |
| 42 | + SlackUtil.buildHeader(f":glyph-ok: Your IBM DevIT Fyre OCP cluster ({name}) is ready"), |
| 43 | + SlackUtil.buildSection(f"{url}"), |
| 44 | + SlackUtil.buildSection(f"- Username: `{username}`\n - Password: `{password}`"), |
| 45 | + SlackUtil.buildSection(f"<https://beta.fyre.ibm.com/development/vms|Fyre Dashboard>{toolchainLink}") |
| 46 | + ] |
| 47 | + else: |
| 48 | + message = [ |
| 49 | + SlackUtil.buildHeader(f":glyph-fail: Your IBM DevIT Fyre OCP cluster ({name}) failed to deploy"), |
| 50 | + SlackUtil.buildSection(f"<https://beta.fyre.ibm.com/development/vms|Fyre Dashboard>{toolchainLink}") |
| 51 | + ] |
| 52 | + |
| 53 | + response = SlackUtil.postMessageBlocks(channel, message) |
| 54 | + return response.data.get("ok", False) |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + # If SLACK_TOKEN or SLACK_CHANNEL env vars are not set then silently exit taking no action |
| 59 | + SLACK_TOKEN = os.getenv("SLACK_TOKEN", None) |
| 60 | + SLACK_CHANNEL = os.getenv("SLACK_CHANNEL", None) |
| 61 | + if SLACK_TOKEN is None or SLACK_CHANNEL is None: |
| 62 | + sys.exit(0) |
| 63 | + |
| 64 | + # Initialize the properties we need |
| 65 | + parser = argparse.ArgumentParser() |
| 66 | + |
| 67 | + # Primary Options |
| 68 | + parser.add_argument("--action", required=True) |
| 69 | + parser.add_argument("--rc", required=True, type=int) |
| 70 | + args, unknown = parser.parse_known_args() |
| 71 | + |
| 72 | + if args.action == "ocp-provision-fyre": |
| 73 | + notifyProvisionFyre(SLACK_CHANNEL, args.rc) |
0 commit comments