|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | + |
| 5 | +# --- CONFIGURATION --- |
| 6 | +GITHUB_NAMESPACE = "RedHatOfficial" |
| 7 | +RHEL_ROLE_PREFIX = "ansible-role-rhel" |
| 8 | +ROLE_PREFIX = "ansible-role-" |
| 9 | + |
| 10 | +# The token must be exported in your terminal session before running the script: |
| 11 | +# export GALAXY_TOKEN="your_token_here" |
| 12 | +GALAXY_TOKEN = os.getenv("GALAXY_TOKEN") |
| 13 | + |
| 14 | + |
| 15 | +if not GALAXY_TOKEN: |
| 16 | + print("ERROR: The GALAXY_TOKEN environment variable is not set.") |
| 17 | + print("Please set it (export GALAXY_TOKEN=\"...\") and try again.") |
| 18 | + sys.exit(1) |
| 19 | + |
| 20 | + |
| 21 | +def get_role_repositories(): |
| 22 | + """ |
| 23 | + Searches for the list of repositories in the namespace using the GitHub CLI (gh). |
| 24 | + """ |
| 25 | + print(f"Searching for repositories in '{GITHUB_NAMESPACE}'") |
| 26 | + |
| 27 | + # Command 'gh search repos' to search for repositories and extract only the name |
| 28 | + command = [ |
| 29 | + "gh", "search", "repos", |
| 30 | + f"org:{GITHUB_NAMESPACE}", |
| 31 | + "--json", "name", |
| 32 | + "--jq", ".[].name", |
| 33 | + "--limit", "1000" |
| 34 | + ] |
| 35 | + |
| 36 | + try: |
| 37 | + # Execute the command and capture the output |
| 38 | + result = subprocess.run(command, capture_output=True, text=True, check=True) |
| 39 | + repos = result.stdout.strip().split('\n') |
| 40 | + return [repo for repo in repos if repo] |
| 41 | + |
| 42 | + except subprocess.CalledProcessError as e: |
| 43 | + print("\nERROR running the 'gh' command. Check if GitHub CLI is installed and authenticated.") |
| 44 | + print(f"Error details: {e.stderr}") |
| 45 | + sys.exit(1) |
| 46 | + except FileNotFoundError: |
| 47 | + print("\nERROR: The 'gh' command (GitHub CLI) was not found.") |
| 48 | + print("Ensure that GitHub CLI is installed and accessible in your PATH.") |
| 49 | + sys.exit(1) |
| 50 | + |
| 51 | + |
| 52 | +def import_role(repo_name): |
| 53 | + """ |
| 54 | + Executes the ansible-galaxy role import command for a specific repository. |
| 55 | + The role name in Galaxy is derived by stripping the ROLE_PREFIX from the repo name. |
| 56 | + """ |
| 57 | + # ---------------------------------------------------------------------- |
| 58 | + # Calculate the destination role name for Ansible Galaxy |
| 59 | + # Example: 'ansible-role-rhel9-cis' -> 'rhel9-cis' |
| 60 | + # ---------------------------------------------------------------------- |
| 61 | + galaxy_role_name = repo_name.replace(ROLE_PREFIX, '', 1) |
| 62 | + |
| 63 | + print(f"-> Syncing: GitHub Repo='{repo_name}' -> Galaxy Role='{galaxy_role_name}'") |
| 64 | + |
| 65 | + # 'ansible-galaxy role import' command |
| 66 | + # Note: The command syntax is 'ansible-galaxy role import <github_user> <github_repo> --role-name <galaxy_name>' |
| 67 | + command = [ |
| 68 | + "ansible-galaxy", "role", "import", |
| 69 | + f"--token={GALAXY_TOKEN}", # Pass the token |
| 70 | + GITHUB_NAMESPACE, |
| 71 | + repo_name, |
| 72 | + "--role-name", galaxy_role_name, # Specify the final role name without the prefix |
| 73 | + "--no-wait" # do not wait for the import to finish |
| 74 | + ] |
| 75 | + |
| 76 | + try: |
| 77 | + # Execute the command and capture output |
| 78 | + process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) |
| 79 | + stdout, stderr = process.communicate() |
| 80 | + |
| 81 | + if process.returncode == 0: |
| 82 | + print(f" [SUCCESS] Import request submitted for https://galaxy.ansible.com/ui/standalone/roles/RedHatOfficial/{repo_name}.") |
| 83 | + print("-" * 10) |
| 84 | + else: |
| 85 | + print(f" [FAILURE] Error importing {repo_name}. Code: {process.returncode}") |
| 86 | + print(f" Ansible Galaxy output: {stderr.strip()}") |
| 87 | + # Print the command if it failed for debugging purposes |
| 88 | + print(f" Failed Command: {' '.join(command)}") |
| 89 | + print("-" * 10) |
| 90 | + |
| 91 | + except FileNotFoundError: |
| 92 | + print("\nERROR: The 'ansible-galaxy' command was not found.") |
| 93 | + print("Ensure that Ansible is installed and accessible in your PATH.") |
| 94 | + sys.exit(1) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + |
| 99 | + # Get the list of repositories |
| 100 | + repos_found = get_role_repositories() |
| 101 | + |
| 102 | + if not repos_found: |
| 103 | + print(f"No repositories found starting with in '{GITHUB_NAMESPACE}'.") |
| 104 | + sys.exit(0) |
| 105 | + |
| 106 | + # Iterate over the list and execute the import for each one |
| 107 | + repo_count = 0 |
| 108 | + for repo in repos_found: |
| 109 | + if repo.startswith(RHEL_ROLE_PREFIX): |
| 110 | + # print(repo) |
| 111 | + if repo != "ansible-role-rhel7-stig": |
| 112 | + import_role(repo) |
| 113 | + repo_count += 1 |
| 114 | + |
| 115 | + print(f"Bulk synchronization process complete. A total of {repo_count} repositories were updated.") |
0 commit comments