diff --git a/.github/workflows/regenerate-sha-commit.yml b/.github/workflows/regenerate-sha-commit.yml new file mode 100644 index 000000000..cd5978490 --- /dev/null +++ b/.github/workflows/regenerate-sha-commit.yml @@ -0,0 +1,72 @@ +# This is a basic workflow to help you get started with Actions + +name: Regenerate Operator SHAs Commits + +# Controls when the action will run. +on: + # Runs every three hours + schedule: + - cron: "0 */6 * * *" + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + # The type of runner that the job will run on + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.9] + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + with: + token: ${{ secrets.HUB_OPERATOR_TOKEN }} + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + + # Runs a set of commands using the runners shell + - name: Regenerate Operator SHAs Commits + id: generate_sha_commit + run: | + make regenerate-operator-sha-commits + exit_code=$? + if [ $exit_code -ne 0 ]; then + echo "Regenerate Operator SHAs Commit step failed with exit code $exit_code" + exit $exit_code + fi + + - name: Send Slack Message on Failure + if: ${{ failure() }} + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + run: | + SLACK_MESSAGE=":exclamation: *GitHub Actions Job Failed* :exclamation:\n\n" + SLACK_MESSAGE+="Job Name: $GITHUB_WORKFLOW/$GITHUB_JOB\n" + SLACK_MESSAGE+="Job URL: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID\n" + SLACK_MESSAGE+="Error Details: Your job failed. Please check the job logs for more information." + + curl -X POST -H "Content-type: application/json" --data "{ + \"text\": \"$SLACK_MESSAGE\" + }" $SLACK_WEBHOOK_URL + + - name: Create Pull Request + uses: peter-evans/create-pull-request@v3 + with: + signoff: true + branch: "regenerate-operator-sha-commits" + delete-branch: true + title: "Operator SHAs Commit Update" + committer: GitHub + author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com> + labels: | + do-not-merge/hold + ok-to-test + reviewers: cameronmwall,dislbenn,ngraham20 diff --git a/Makefile.dev b/Makefile.dev index ec16c3a39..f20f85b32 100644 --- a/Makefile.dev +++ b/Makefile.dev @@ -20,15 +20,19 @@ upstream-install: ## Installs the upstream Backplane Operator by deploying a Cat bash ./hack/scripts/upstream-install.sh lint-operator-bundles: ## Lints the operator bundles - pip install -r hack/bundle-automation/requirements.txt - python3 ./hack/bundle-automation/generate-shell.py --lint + pip3 install -r hack/bundle-automation/requirements.txt + python3 ./hack/bundle-automation/generate-shell.py --lint-bundles regenerate-operator-bundles: ## Regenerates the operator bundles - pip install -r hack/bundle-automation/requirements.txt - python3 ./hack/bundle-automation/generate-shell.py + pip3 install -r hack/bundle-automation/requirements.txt + python3 ./hack/bundle-automation/generate-shell.py --update-bundles + +regenerate-operator-sha-commits: ## Regenerates the operator bundles + pip3 install -r hack/bundle-automation/requirements.txt + python3 ./hack/bundle-automation/generate-shell.py --update-commits --repo backplane-pipeline --branch 2.6-integration regenerate-charts: ## Regenerates the charts - pip install -r hack/bundle-automation/chart-requirements.txt + pip3 install -r hack/bundle-automation/chart-requirements.txt python3 ./hack/bundle-automation/generate-charts.py --destination pkg/templates/ catalog-deploy: ## Deploys backplane operator via subscription diff --git a/hack/bundle-automation/gen-hive-bundle.sh b/hack/bundle-automation/gen-hive-bundle.sh index eb81557a9..511cdd6ea 100755 --- a/hack/bundle-automation/gen-hive-bundle.sh +++ b/hack/bundle-automation/gen-hive-bundle.sh @@ -11,8 +11,41 @@ # Note: The output directory is removed at the start of each run to ensure # a clean/consistent result. +cleanup() { + echo "Performing cleanup tasks... $tmp_dir" + if [[ -d "$tmp_dir" ]]; then + rm -rf "$tmp_dir" + fi + + prefix="gen-hive-bundle.sh." + find $(pwd) -type f -name "$prefix*" | while IFS= read -r file; do + # Check if the file exists and delete it + if [ -f "$file" ]; then + rm "$file" + echo "Deleted file: $file" + fi + done + + prefix="hive-operator-bundle-" + find $(pwd)/bundle -type d -name "$prefix*" | while IFS= read -r file; do + # Check if the file exists and delete it + if [ -d "$file" ]; then + rm -rf "$file" + echo "Deleted directory: $file" + fi + done +} + +# Define a temporary directory me=$(basename "$0") +tmp_dir=$(mktemp -td "$me.XXXXXXXX") +echo -e "Temporary directory created: $tmp_dir" + +# Trap for cleanup on exit or Ctrl+C (SIGINT) +trap 'cleanup' EXIT +trap 'cleanup' SIGINT + branch="$1" commit_ish="$2" output_dir="$3" @@ -25,8 +58,6 @@ fi hive_repo="https://github.com/openshift/hive.git" gen_tool="hack/bundle-gen.py" # Path within Hive's repo. -tmp_dir=$(mktemp -td "$me.XXXXXXXX") - start_cwd="$PWD" rm -rf "$output_dir" @@ -36,12 +67,17 @@ cd "$tmp_dir" echo "Cloning/checking out Hive repo at branch/commit ($commit_ish)." hive_repo_spot="$PWD/hive" +if [[ -d "$hive_repo_spot" ]]; then + rm -rf "$hive_repo_spot" +fi + git clone --no-progress "$hive_repo" "hive" rc=$? if [[ $rc -ne 0 ]]; then >&2 echo "Error: Could not clone openshift/hive (rc: $rc)." exit 3 fi + cd hive git fetch origin $branch git checkout $branch @@ -100,4 +136,3 @@ fi echo "Hive bundle copied to $output_dir." rm -rf "$tmp_dir" - diff --git a/hack/bundle-automation/generate-shell.py b/hack/bundle-automation/generate-shell.py index 453f9c342..68f2c99c0 100644 --- a/hack/bundle-automation/generate-shell.py +++ b/hack/bundle-automation/generate-shell.py @@ -1,13 +1,82 @@ -from git import Repo, exc +#!/usr/bin/env python3 +# Copyright (c) 2024 Red Hat, Inc. +# Copyright Contributors to the Open Cluster Management project +# Assumes: Python 3.6+ + +import argparse +import coloredlogs import os +import logging import shutil +import time + +from git import Repo, exc + +# Configure logging with coloredlogs +coloredlogs.install(level='DEBUG') # Set the logging level as needed + +def clone_repository(repo_url, repo_path, branch='main'): + if os.path.exists(repo_path): + shutil.rmtree(repo_path) + logging.info(f"Cloning repository from {repo_url} to {repo_path}...") + + repository = Repo.clone_from(repo_url, repo_path) + repository.git.checkout(branch) + logging.info("Repository cloned successfully.") + +def prepare_operation(script_dir, operation_script, operation_args): + shutil.copy(os.path.join(os.path.dirname(os.path.realpath(__file__)), f"{script_dir}/{operation_script}"), os.path.join(os.path.dirname(os.path.realpath(__file__)), operation_script)) + os.system("python3 " + os.path.dirname(os.path.realpath(__file__)) + f"/{operation_script} {operation_args}") + os.remove(os.path.join(os.path.dirname(os.path.realpath(__file__)), f"{operation_script}")) + +def main(args): + logging.basicConfig(level=logging.INFO) + start_time = time.time() # Record start time + + repo_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "tmp/dev-tools") # Destination path for cloned repository. + clone_repository("https://github.com/dislbenn/installer-dev-tools.git", repo_path) + script_dir = "tmp/dev-tools/bundle-generation" + + if args.lint_bundles: + logging.info("Preparing to perform linting for bundles...") + operation_script = "generate-bundles.py" + operation_args = "--lint --destination pkg/templates/" + + prepare_operation(script_dir, operation_script, operation_args) + logging.info("Bundles linted successfully.") + + elif args.update_bundles: + logging.info("Preparing to update bundles...") + operation_script = "generate-bundles.py" + operation_args = "--destination pkg/templates/" + + prepare_operation(script_dir, operation_script, operation_args) + logging.info("Bundles updated successfully.") + + elif args.update_commits: + logging.info("Preparing to update commit SHAs...") + operation_script = "generate-sha-commits.py" + operation_args = f"--repo {args.repo} --branch {args.branch}" + + prepare_operation(script_dir, operation_script, operation_args) + logging.info("Commit SHAs updated successfully.") + + else: + logging.warning("No operation specified.") + + end_time = time.time() # Record end time + logging.info(f"Script execution took {end_time - start_time:.2f} seconds.") # Log duration + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + + parser.add_argument("--lint-bundles", action="store_true", help="Perform linting for operator bundles") + parser.add_argument("--update-bundles", action="store_true", help="Regenerate operator bundles") + parser.add_argument("--update-commits", action="store_true", help="Regenerate operator bundles with commit SHA") + parser.add_argument("--repo", help="Repository name") + parser.add_argument("--branch", default='main', help="Branch name") + parser.set_defaults(bundle=False, commit=False, lint=False) -repo_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "tmp/dev-tools") # Path to clone repo to -if os.path.exists(repo_path): # If path exists, remove and re-clone - shutil.rmtree(repo_path) -repository = Repo.clone_from("https://github.com/stolostron/installer-dev-tools.git", repo_path) # Clone repo to above path -repository.git.checkout('main') # If a branch is specified, checkout that branch -shutil.copy(os.path.join(os.path.dirname(os.path.realpath(__file__)), "tmp/dev-tools/bundle-generation/generate-bundles.py"), os.path.join(os.path.dirname(os.path.realpath(__file__)), "generate-bundles.py")) -os.system("python3 " + os.path.dirname(os.path.realpath(__file__)) + "/generate-bundles.py --destination pkg/templates/") -os.remove(os.path.join(os.path.dirname(os.path.realpath(__file__)), "generate-bundles.py")) \ No newline at end of file + args = parser.parse_args() + main(args)