Skip to content

Commit

Permalink
[ACM-11501] Updated shell script for automation (#700)
Browse files Browse the repository at this point in the history
* updated shell script for automation

Signed-off-by: Disaiah Bennett <[email protected]>

* Added Github Action job for regenerating oeprator SHA commits

Signed-off-by: Disaiah Bennett <[email protected]>

---------

Signed-off-by: Disaiah Bennett <[email protected]>
  • Loading branch information
dislbenn authored May 8, 2024
1 parent 12a21ec commit 99e4c7e
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 17 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/regenerate-sha-commit.yml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
labels: |
do-not-merge/hold
ok-to-test
reviewers: cameronmwall,dislbenn,ngraham20
14 changes: 9 additions & 5 deletions Makefile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 38 additions & 3 deletions hack/bundle-automation/gen-hive-bundle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

Expand All @@ -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
Expand Down Expand Up @@ -100,4 +136,3 @@ fi
echo "Hive bundle copied to $output_dir."

rm -rf "$tmp_dir"

87 changes: 78 additions & 9 deletions hack/bundle-automation/generate-shell.py
Original file line number Diff line number Diff line change
@@ -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"))
args = parser.parse_args()
main(args)

0 comments on commit 99e4c7e

Please sign in to comment.