Skip to content

Commit

Permalink
working on ansible plyabook
Browse files Browse the repository at this point in the history
  • Loading branch information
arnol377 committed Feb 12, 2025
1 parent 19b73a2 commit db11726
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 83 deletions.
48 changes: 31 additions & 17 deletions scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
}
}
Expand All @@ -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
45 changes: 30 additions & 15 deletions scripts/sync-repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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"]],
Expand All @@ -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
Expand All @@ -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"]],
Expand All @@ -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}")
Expand Down
51 changes: 0 additions & 51 deletions scripts/sync-repos.sh

This file was deleted.

0 comments on commit db11726

Please sign in to comment.