-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pre-commit script and job for syncing scripts to ansible playbooks
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
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |