Skip to content

Commit

Permalink
Add pre-commit script and job for syncing scripts to ansible playbooks
Browse files Browse the repository at this point in the history
pre-commit serves to stage the file without the developer having to do
it manually.

github job serves to notify the developer if they forgot to run the
pre-commit or if they forgot to stage the playbooks changes.
  • Loading branch information
Wecros committed Jan 16, 2024
1 parent 7bd3516 commit ed7ff39
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
33 changes: 33 additions & 0 deletions .github/workflows/check-scripts-updates.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Check Script and Playbook Updates

on:
push:
paths:
- 'scripts/**'

jobs:
check-updates:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Check for Matching Updates in Scripts and Playbooks
run: |
#!/bin/bash
set -e
# Get list of changed files in last commit
changed_files=$(git diff --name-only HEAD HEAD~1)
# Check if any files in scripts/ were updated (excluding wrap_scripts_in_yaml.py)
scripts_updated=$(echo "$changed_files" | grep -v 'wrap_scripts_in_yaml.py' | grep 'scripts/')
# Check if any files in playbooks/ were updated
playbooks_updated=$(echo "$changed_files" | grep 'playbooks/')
if [[ -n "$scripts_updated" && -z "$playbooks_updated" ]]; then
echo "Files in scripts/ were updated without corresponding updates in playbooks/"
exit 1
fi
echo "Check passed"
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ repos:
"-sn", # Don't display the score
"--rcfile=.pylintrc", # Link to your config file
]
- id: wrap-scripts-in-yaml
name: wrap-scripts-in-yaml
entry: python scripts/wrap_scripts_in_yaml.py
language: python
files: scripts/.*\.py$


- repo: https://github.com/gitleaks/gitleaks
rev: v8.18.0
Expand Down
2 changes: 1 addition & 1 deletion scripts/leapp_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
TXT_REPORT_PATH = "/var/log/leapp/leapp-report.txt"
REBOOT_GUIDANCE_MESSAGE = "A reboot is required to continue. Please reboot your system."

# Based on https://github.com/oamg/leapp/blob/master/report-schema-v110.json#L211
# Based on https:/ /github.com/oamg/leapp/blob/master/report-schema-v110.json#L211
STATUS_CODE = {
"high": 3,
"medium": 2,
Expand Down
54 changes: 54 additions & 0 deletions scripts/wrap_scripts_in_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
This file serves to be run during a pre-commit hook to wrap all scripts/ files
in yaml and convert them to ansible playbooks placed under playbooks/ folder.
"""

import re
import sys
from pathlib import Path


def wrap_script_in_yaml(python_file):
yaml_file_path = f"playbooks/{Path(python_file).stem}_script.yaml"
yaml_content = generate_yaml_content(python_file)

if not Path(yaml_file_path).exists() or open(yaml_file_path).read() != yaml_content:
with open(yaml_file_path, "w") as yaml_file:
yaml_file.write(yaml_content)
return True
return False


def generate_yaml_content(python_file):
with open(python_file, "r") as py_file:
content = "- name: Leapp pre-upgrade for rhc-worker-script\n"
content += " vars:\n"
content += " insights_signature: !!binary |\n"
content += " needs signature\n"
content += ' insights_signature_exclude: "/vars/insights_signature"\n'
content += " interpreter: /usr/bin/python\n"
content += " content: |\n"
for line in py_file:
content += f" {line}"
content += " content_vars:\n"
return content


def main():
changes_detected = False
for filename in sys.argv[1:]:
if re.match(r"scripts/.*.py$", filename):
if filename == f"scripts/{Path(__file__).name}":
continue
if wrap_script_in_yaml(filename):
changes_detected = True

if changes_detected:
print(
"Changes detected in ansible playbooks (coming from scripts). Please stage them and commit again."
)
sys.exit(1)


if __name__ == "__main__":
main()

0 comments on commit ed7ff39

Please sign in to comment.