From db1172621e5e4b9a08b003c7e7322993e99195ba Mon Sep 17 00:00:00 2001 From: arnol377 Date: Wed, 12 Feb 2025 14:29:04 -0500 Subject: [PATCH] working on ansible plyabook --- scripts/README.md | 48 +++++++++++++++++++++++++--------------- scripts/sync-repos.py | 45 +++++++++++++++++++++++++------------- scripts/sync-repos.sh | 51 ------------------------------------------- 3 files changed, 61 insertions(+), 83 deletions(-) delete mode 100755 scripts/sync-repos.sh diff --git a/scripts/README.md b/scripts/README.md index 160e53d..16c9c18 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -4,21 +4,38 @@ This Python script automates the process of syncing (pulling and pushing) change ## Usage +Basic usage: ```bash ./scripts/sync-repos.py ``` -The script will: -1. Pull latest changes from both 'hpw' and 'origin' remotes for each repository -2. Look for local changes in each repository -3. If changes exist, commit them with predefined messages -4. Always attempt to push to both 'hpw' and 'origin' remotes (even if there are no local changes) +With specific remote: +```bash +./scripts/sync-repos.py --remote origin +./scripts/sync-repos.py -r hpw +``` + +With commit message: +```bash +./scripts/sync-repos.py --message "your commit message" +./scripts/sync-repos.py -m "fix: update volume configuration" +``` + +Combined usage: +```bash +./scripts/sync-repos.py -r origin -m "feat: add new feature" +``` + +## Command Line Arguments + +- `--remote`, `-r`: Specify which remote to sync with + - `all`: Sync with both origin and hpw (default) + - `origin`: Only sync with origin remote + - `hpw`: Only sync with hpw remote -This ensures that: -- Local changes are committed and pushed -- Remote changes are pulled down -- Local branch stays in sync with both remotes -- Changes from one remote are propagated to the other +- `--message`, `-m`: Specify a commit message to use for all repositories + - If not provided, changes won't be committed + - If provided, any local changes will be committed with this message ## Prerequisites @@ -28,22 +45,19 @@ This ensures that: - Script must be run from the aws-image-pipeline directory - Appropriate git credentials/SSH keys must be configured -## Modifying Repository Configuration +## Repository Configuration -To update the repositories and their commit messages, edit the `REPOS` dictionary in the script: +The script maintains a default configuration for repositories: ```python -REPOS = { +DEFAULT_REPOS = { "aws-image-pipeline": { - "message": "your commit message here", "branch": "main" }, "image-pipeline-ansible-playbooks": { - "message": "your commit message here", "branch": "main" }, "linux-image-pipeline": { - "message": "your commit message here", "branch": "main" } } @@ -54,6 +68,6 @@ REPOS = { The script will: - Continue processing even if one repository fails - Report failures for both pull and push operations -- Skip repositories with no changes +- Skip repositories with no changes (unless commit message is provided) - Exit with status code 1 if any repository fails to process - Provide detailed error messages from git operations \ No newline at end of file diff --git a/scripts/sync-repos.py b/scripts/sync-repos.py index 28247bb..89f2dfd 100755 --- a/scripts/sync-repos.py +++ b/scripts/sync-repos.py @@ -2,27 +2,40 @@ import os import sys import subprocess -from typing import Dict, Optional +import argparse +from typing import Dict, Optional, List # Base directory containing all repositories BASE_DIR = "/home/a/arnol377/git" -# Repository configurations with their commit messages -REPOS: Dict[str, Dict[str, str]] = { +# Default repository configurations +DEFAULT_REPOS: Dict[str, Dict[str, str]] = { "aws-image-pipeline": { - "message": "fix: remove LVM configuration and simplify disk mounting", "branch": "main" }, "image-pipeline-ansible-playbooks": { - "message": "fix: remove LVM and use direct disk formatting with XFS", "branch": "main" }, "linux-image-pipeline": { - "message": "fix: simplify disk configuration by removing LVM dependencies", "branch": "main" } } +def parse_args(): + """Parse command line arguments.""" + parser = argparse.ArgumentParser(description='Sync git repositories with multiple remotes') + parser.add_argument('--remote', '-r', + choices=['all', 'origin', 'hpw'], + default='all', + help='Remote to sync with (default: all)') + parser.add_argument('--message', '-m', + help='Commit message to use for all repositories') + return parser.parse_args() + +def get_remotes(args) -> List[str]: + """Get list of remotes based on command line argument.""" + return ['origin', 'hpw'] if args.remote == 'all' else [args.remote] + def run_command(cmd: list[str], cwd: Optional[str] = None) -> tuple[int, str, str]: """Run a shell command and return the exit code, stdout, and stderr.""" try: @@ -38,7 +51,7 @@ def run_command(cmd: list[str], cwd: Optional[str] = None) -> tuple[int, str, st except Exception as e: return 1, "", str(e) -def sync_repo(repo_name: str, config: dict) -> bool: +def sync_repo(repo_name: str, config: dict, remotes: List[str], commit_message: Optional[str] = None) -> bool: """Sync (pull/push) a single repository.""" repo_path = os.path.join(BASE_DIR, repo_name) if not os.path.isdir(repo_path): @@ -47,8 +60,8 @@ def sync_repo(repo_name: str, config: dict) -> bool: print(f"\nProcessing {repo_name}...") - # Pull from both remotes - for remote in ["origin", "hpw"]: + # Pull from specified remotes + for remote in remotes: print(f"Pulling from {remote}...") code, out, err = run_command( ["git", "pull", remote, config["branch"]], @@ -64,7 +77,7 @@ def sync_repo(repo_name: str, config: dict) -> bool: print(f"Error checking git status: {err}") return False - if out.strip(): + if out.strip() and commit_message: print(f"Changes detected in {repo_name}, committing...") # Add all changes @@ -75,15 +88,15 @@ def sync_repo(repo_name: str, config: dict) -> bool: # Commit changes code, out, err = run_command( - ["git", "commit", "-m", config["message"]], + ["git", "commit", "-m", commit_message], repo_path ) if code != 0: print(f"Error committing changes: {err}") return False - # Always try to push to both remotes, even if no local changes - for remote in ["hpw", "origin"]: + # Always try to push to specified remotes + for remote in remotes: print(f"Pushing to {remote}...") code, out, err = run_command( ["git", "push", remote, config["branch"]], @@ -99,12 +112,14 @@ def sync_repo(repo_name: str, config: dict) -> bool: def main(): """Main function to sync all repositories.""" + args = parse_args() + remotes = get_remotes(args) success = True # Process each repository - for repo_name, config in REPOS.items(): + for repo_name, config in DEFAULT_REPOS.items(): try: - if not sync_repo(repo_name, config): + if not sync_repo(repo_name, config, remotes, args.message): success = False except Exception as e: print(f"Error processing {repo_name}: {e}") diff --git a/scripts/sync-repos.sh b/scripts/sync-repos.sh deleted file mode 100755 index 62cd043..0000000 --- a/scripts/sync-repos.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/bash -set -e - -# Directory containing all repos -BASE_DIR=/home/a/arnol377/git - -# Define repositories and their commit messages -declare -A REPOS -REPOS["aws-image-pipeline"]="fix: update morpheus module config to use proper device mappings for NVMe EBS volumes" -REPOS["image-pipeline-ansible-playbooks"]="fix: update volume mounting to handle NVMe device mapping and add enhanced error checking" -REPOS["linux-image-pipeline"]="fix: update packer configuration to properly handle root volume and NVMe device mappings" - -# Function to commit and push changes -commit_and_push() { - local repo=$1 - local message=$2 - local dir="${BASE_DIR}/${repo}" - - echo "Processing ${repo}..." - - if [ ! -d "$dir" ]; then - echo "Error: Directory $dir does not exist" - return 1 - fi - - cd "$dir" - - # Check if there are changes to commit - if git status --porcelain | grep -q '^'; then - echo "Changes detected in ${repo}, committing..." - git add . - git commit -m "$message" - - # Push to both remotes - echo "Pushing to hpw..." - git push hpw main || echo "Warning: Failed to push to hpw remote" - - echo "Pushing to origin..." - git push origin main || echo "Warning: Failed to push to origin remote" - else - echo "No changes to commit in ${repo}" - fi -} - -# Main execution -for repo in "${!REPOS[@]}"; do - commit_message=${REPOS[$repo]} - commit_and_push "$repo" "$commit_message" || echo "Failed to process ${repo}" -done - -echo "All repositories processed successfully!" \ No newline at end of file