From 638e1e3062a42be9c9a0d4e83ad557c4c006e8a6 Mon Sep 17 00:00:00 2001
From: Michal Nasiadka <mnasiadka@gmail.com>
Date: Fri, 29 Nov 2024 16:18:17 +0100
Subject: [PATCH] Retry Ansible Galaxy calls

Change-Id: Ic0e33c4f2939c36ed80caa3ec1015cf53a4d93e2
---
 kolla_ansible/utils.py                        | 25 ++++++++++++++-----
 ...install-deps-retries-fbe2a3abb41abb6d.yaml |  5 ++++
 2 files changed, 24 insertions(+), 6 deletions(-)
 create mode 100644 releasenotes/notes/install-deps-retries-fbe2a3abb41abb6d.yaml

diff --git a/kolla_ansible/utils.py b/kolla_ansible/utils.py
index 316ba68dd6..d2183de212 100644
--- a/kolla_ansible/utils.py
+++ b/kolla_ansible/utils.py
@@ -21,6 +21,7 @@ import sys
 import yaml
 
 from importlib.metadata import Distribution
+from time import sleep
 
 LOG = logging.getLogger(__name__)
 
@@ -106,12 +107,24 @@ def galaxy_collection_install(requirements_file: str,
     args += ["--requirements-file", requirements_file]
     if force:
         args += ["--force"]
-    try:
-        run_command("ansible-galaxy", args)
-    except subprocess.CalledProcessError as e:
-        LOG.error("Failed to install Ansible collections from %s via Ansible "
-                  "Galaxy: returncode %d", requirements_file, e.returncode)
-        sys.exit(e.returncode)
+
+    for retry_count in range(1, 6):
+        try:
+            run_command("ansible-galaxy", args)
+        except subprocess.CalledProcessError as e:
+            if retry_count < 5:
+                LOG.warning(f"Failed to install Ansible collections from "
+                            f"{requirements_file} using Ansible Galaxy "
+                            f"(error: {e}) (retry: {retry_count}/5)")
+                sleep(2)
+                continue
+            else:
+                LOG.error(f"Failed to install Ansible collections from "
+                          f"{requirements_file} using Ansible Galaxy "
+                          f"(error: {e}) after 5 retries")
+                LOG.error("Exiting")
+                sys.exit(e.returncode)
+        break
 
 
 def read_file(path: os.path, mode: str = "r") -> str | bytes:
diff --git a/releasenotes/notes/install-deps-retries-fbe2a3abb41abb6d.yaml b/releasenotes/notes/install-deps-retries-fbe2a3abb41abb6d.yaml
new file mode 100644
index 0000000000..263807b4b9
--- /dev/null
+++ b/releasenotes/notes/install-deps-retries-fbe2a3abb41abb6d.yaml
@@ -0,0 +1,5 @@
+---
+features:
+  - |
+    ``kolla-ansible install-deps`` subcommand will now retry on Ansible Galaxy
+    collections installation failures.