1313import re
1414import sys
1515import requests
16+ import time
1617from pathlib import Path
1718from typing import Optional
1819
1920
2021def get_latest_elastic_agent_version () -> str :
2122 """
2223 Retrieve the latest semantic version from the elastic-agent repository by fetching all tags
23- and finding the highest version.
24+ and finding the highest version with retry logic .
2425
2526 Returns:
2627 str: The latest version tag (e.g., 'v8.12.0')
2728
2829 Raises:
29- Exception: If unable to fetch version information
30+ Exception: If unable to fetch version information after retries
3031 """
31- try :
32- # Get all tags from GitHub API
33- url = "https://api.github.com/repos/elastic/elastic-agent/tags"
34- response = requests .get (url , timeout = 30 )
35- response .raise_for_status ()
36-
37- tags_data = response .json ()
38- if not tags_data :
39- raise Exception ("No tags found in repository" )
40-
41- # Extract version tags matching pattern vX.Y.Z
42- version_pattern = re .compile (r'^v(\d+)\.(\d+)\.(\d+)$' )
43- versions = []
44-
45- for tag in tags_data :
46- tag_name = tag .get ('name' , '' )
47- if version_pattern .match (tag_name ):
48- # Extract version components for sorting
49- match = version_pattern .match (tag_name )
50- major , minor , patch = map (int , match .groups ())
51- versions .append ((major , minor , patch , tag_name ))
52-
53- if not versions :
54- raise Exception ("No valid version tags found" )
55-
56- # Sort by version components and get the latest
57- versions .sort (key = lambda x : (x [0 ], x [1 ], x [2 ]))
58- latest_version = versions [- 1 ][3 ]
59-
60- print (f"Latest elastic-agent version: { latest_version } " )
61- return latest_version
62-
63- except requests .RequestException as e :
64- raise Exception (f"Failed to fetch tags: { e } " )
65- except Exception as e :
66- raise Exception (f"Error retrieving version: { e } " )
32+ url = "https://api.github.com/repos/elastic/elastic-agent/tags"
33+ max_retries = 3
34+ retry_delay = 2 # seconds
35+
36+ for attempt in range (max_retries ):
37+ try :
38+ print (f"Fetching elastic-agent tags (attempt { attempt + 1 } /{ max_retries } )" )
39+ response = requests .get (url , timeout = 30 )
40+ response .raise_for_status ()
41+
42+ tags_data = response .json ()
43+ if not tags_data :
44+ raise Exception ("No tags found in repository" )
45+
46+ # Extract version tags matching pattern vX.Y.Z
47+ version_pattern = re .compile (r'^v(\d+)\.(\d+)\.(\d+)$' )
48+ versions = []
49+
50+ for tag in tags_data :
51+ tag_name = tag .get ('name' , '' )
52+ if version_pattern .match (tag_name ):
53+ # Extract version components for sorting
54+ match = version_pattern .match (tag_name )
55+ major , minor , patch = map (int , match .groups ())
56+ versions .append ((major , minor , patch , tag_name ))
57+
58+ if not versions :
59+ raise Exception ("No valid version tags found" )
60+
61+ # Sort by version components and get the latest
62+ versions .sort (key = lambda x : (x [0 ], x [1 ], x [2 ]))
63+ latest_version = versions [- 1 ][3 ]
64+
65+ print (f"Latest elastic-agent version: { latest_version } " )
66+ return latest_version
67+
68+ except requests .RequestException as e :
69+ if attempt < max_retries - 1 :
70+ print (f"Attempt { attempt + 1 } failed: { e } " )
71+ print (f"Retrying in { retry_delay } seconds..." )
72+ time .sleep (retry_delay )
73+ retry_delay *= 2 # Exponential backoff
74+ else :
75+ raise Exception (f"Failed to fetch tags after { max_retries } attempts: { e } " )
76+ except Exception as e :
77+ if attempt < max_retries - 1 :
78+ print (f"Attempt { attempt + 1 } failed: { e } " )
79+ print (f"Retrying in { retry_delay } seconds..." )
80+ time .sleep (retry_delay )
81+ retry_delay *= 2 # Exponential backoff
82+ else :
83+ raise Exception (f"Error retrieving version after { max_retries } attempts: { e } " )
6784
6885
6986def fetch_k8s_go_content (version : str ) -> str :
7087 """
71- Fetch the content of the k8s.go file from the elastic-agent repository.
88+ Fetch the content of the k8s.go file from the elastic-agent repository with retry logic .
7289
7390 Args:
7491 version (str): The version tag to fetch
@@ -77,18 +94,29 @@ def fetch_k8s_go_content(version: str) -> str:
7794 str: The content of the k8s.go file
7895
7996 Raises:
80- Exception: If unable to fetch the file content
97+ Exception: If unable to fetch the file content after retries
8198 """
82- try :
83- url = f"https://raw.githubusercontent.com/elastic/elastic-agent/{ version } /testing/integration/k8s/k8s.go"
84- response = requests .get (url , timeout = 30 )
85- response .raise_for_status ()
86-
87- print (f"Successfully fetched k8s.go from version { version } " )
88- return response .text
89-
90- except requests .RequestException as e :
91- raise Exception (f"Failed to fetch k8s.go file: { e } " )
99+ url = f"https://raw.githubusercontent.com/elastic/elastic-agent/{ version } /testing/integration/k8s/k8s.go"
100+ max_retries = 3
101+ retry_delay = 2 # seconds
102+
103+ for attempt in range (max_retries ):
104+ try :
105+ print (f"Fetching k8s.go from version { version } (attempt { attempt + 1 } /{ max_retries } )" )
106+ response = requests .get (url , timeout = 30 )
107+ response .raise_for_status ()
108+
109+ print (f"Successfully fetched k8s.go from version { version } " )
110+ return response .text
111+
112+ except requests .RequestException as e :
113+ if attempt < max_retries - 1 :
114+ print (f"Attempt { attempt + 1 } failed: { e } " )
115+ print (f"Retrying in { retry_delay } seconds..." )
116+ time .sleep (retry_delay )
117+ retry_delay *= 2 # Exponential backoff
118+ else :
119+ raise Exception (f"Failed to fetch k8s.go file after { max_retries } attempts: { e } " )
92120
93121
94122def extract_kube_stack_version (content : str ) -> str :
0 commit comments