|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Kube-Stack Version Update Script |
| 4 | +
|
| 5 | +This script automatically updates the kube-stack version in the docset.yml file |
| 6 | +by fetching the latest version from the elastic-agent repository. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python update_kube_stack_version.py [--dry-run] |
| 10 | +
|
| 11 | +Options: |
| 12 | + --dry-run Show what would be updated without making changes |
| 13 | +""" |
| 14 | + |
| 15 | +import urllib.request |
| 16 | +import re |
| 17 | +import sys |
| 18 | +import argparse |
| 19 | +from pathlib import Path |
| 20 | + |
| 21 | + |
| 22 | +def fetch_url_content(url): |
| 23 | + """Fetch content from a URL""" |
| 24 | + try: |
| 25 | + print(f"Attempting to fetch: {url}") |
| 26 | + with urllib.request.urlopen(url) as response: |
| 27 | + content = response.read().decode('utf-8') |
| 28 | + return content |
| 29 | + except urllib.error.URLError as e: |
| 30 | + print(f"Failed to retrieve content: {e.reason}") |
| 31 | + return None |
| 32 | + |
| 33 | + |
| 34 | +def get_collector_version(file_path): |
| 35 | + """Extract the collector version from docset.yml""" |
| 36 | + try: |
| 37 | + with open(file_path, 'r', encoding='utf-8') as file: |
| 38 | + content = file.read() |
| 39 | + |
| 40 | + lines = content.splitlines() |
| 41 | + for line in lines: |
| 42 | + if line.strip().startswith('edot-collector-version:'): |
| 43 | + return line.split(':', 1)[1].strip() |
| 44 | + |
| 45 | + # If no specific version is found, use a default version that we know works |
| 46 | + return '9.1.2' |
| 47 | + except FileNotFoundError: |
| 48 | + print(f"Error: Could not find {file_path}") |
| 49 | + return None |
| 50 | + except Exception as e: |
| 51 | + print(f"Error reading {file_path}: {e}") |
| 52 | + return None |
| 53 | + |
| 54 | + |
| 55 | +def get_kube_stack_version(version='main'): |
| 56 | + """Extract KubeStackChartVersion from elastic-agent repository""" |
| 57 | + # Try different URL formats for the k8s.go file |
| 58 | + # First try with the version as-is (in case it already has 'v' prefix) |
| 59 | + url = f'https://raw.githubusercontent.com/elastic/elastic-agent/{version}/testing/integration/k8s/k8s.go' |
| 60 | + print(f"Trying k8s.go URL: {url}") |
| 61 | + content = fetch_url_content(url) |
| 62 | + |
| 63 | + # If first attempt fails and version doesn't start with 'v', try with 'v' prefix |
| 64 | + if content is None and not version.startswith('v') and version != 'main': |
| 65 | + url = f'https://raw.githubusercontent.com/elastic/elastic-agent/v{version}/testing/integration/k8s/k8s.go' |
| 66 | + print(f"Retrying k8s.go with URL: {url}") |
| 67 | + content = fetch_url_content(url) |
| 68 | + |
| 69 | + # If that fails too, try with main branch |
| 70 | + if content is None: |
| 71 | + url = 'https://raw.githubusercontent.com/elastic/elastic-agent/main/testing/integration/k8s/k8s.go' |
| 72 | + print(f"Falling back to main branch for k8s.go: {url}") |
| 73 | + content = fetch_url_content(url) |
| 74 | + |
| 75 | + if content is None: |
| 76 | + print(f"Could not fetch k8s.go from any URL") |
| 77 | + return None |
| 78 | + |
| 79 | + # Look for the KubeStackChartVersion line |
| 80 | + lines = content.splitlines() |
| 81 | + for line in lines: |
| 82 | + if 'KubeStackChartVersion' in line and '=' in line: |
| 83 | + # Extract the version from the line like: KubeStackChartVersion = "0.6.3" |
| 84 | + match = re.search(r'KubeStackChartVersion\s*=\s*"([^"]+)"', line) |
| 85 | + if match: |
| 86 | + return match.group(1) |
| 87 | + |
| 88 | + print("Could not find KubeStackChartVersion in k8s.go") |
| 89 | + return None |
| 90 | + |
| 91 | + |
| 92 | +def update_docset_kube_stack_version(version, docset_path, dry_run=False): |
| 93 | + """Update the kube-stack-version substitution in docset.yml""" |
| 94 | + try: |
| 95 | + with open(docset_path, 'r', encoding='utf-8') as file: |
| 96 | + content = file.read() |
| 97 | + |
| 98 | + # Replace the kube-stack-version line |
| 99 | + pattern = r'(kube-stack-version:\s*)[0-9]+\.[0-9]+\.[0-9]+' |
| 100 | + replacement = f'\\g<1>{version}' |
| 101 | + new_content = re.sub(pattern, replacement, content) |
| 102 | + |
| 103 | + if new_content != content: |
| 104 | + if dry_run: |
| 105 | + print(f"[DRY RUN] Would update kube-stack-version to {version} in {docset_path}") |
| 106 | + return True |
| 107 | + else: |
| 108 | + with open(docset_path, 'w', encoding='utf-8') as file: |
| 109 | + file.write(new_content) |
| 110 | + print(f"Updated kube-stack-version to {version} in {docset_path}") |
| 111 | + return True |
| 112 | + else: |
| 113 | + print(f"kube-stack-version already up to date: {version}") |
| 114 | + return False |
| 115 | + |
| 116 | + except Exception as e: |
| 117 | + print(f"Error updating {docset_path}: {e}") |
| 118 | + return False |
| 119 | + |
| 120 | + |
| 121 | +def main(): |
| 122 | + parser = argparse.ArgumentParser(description='Update kube-stack version in docset.yml') |
| 123 | + parser.add_argument('--dry-run', action='store_true', |
| 124 | + help='Show what would be updated without making changes') |
| 125 | + args = parser.parse_args() |
| 126 | + |
| 127 | + # Get the script directory and construct paths relative to it |
| 128 | + script_dir = Path(__file__).parent |
| 129 | + docset_path = script_dir.parent / 'docset.yml' |
| 130 | + |
| 131 | + print(f"Using docset.yml path: {docset_path}") |
| 132 | + |
| 133 | + # Get the current collector version from docset.yml |
| 134 | + col_version = get_collector_version(docset_path) |
| 135 | + if col_version is None: |
| 136 | + print("Error: Could not determine collector version") |
| 137 | + sys.exit(1) |
| 138 | + |
| 139 | + print(f"Collector version: {col_version}") |
| 140 | + |
| 141 | + # Get the kube-stack version from elastic-agent repository |
| 142 | + kube_stack_version = get_kube_stack_version(col_version) |
| 143 | + if kube_stack_version is None: |
| 144 | + print("Error: Could not fetch kube-stack version") |
| 145 | + sys.exit(1) |
| 146 | + |
| 147 | + print(f"Found kube-stack version: {kube_stack_version}") |
| 148 | + |
| 149 | + # Update the docset.yml file |
| 150 | + success = update_docset_kube_stack_version(kube_stack_version, docset_path, args.dry_run) |
| 151 | + |
| 152 | + if success: |
| 153 | + print("Kube-stack version update completed successfully") |
| 154 | + sys.exit(0) |
| 155 | + else: |
| 156 | + print("No update was needed or update failed") |
| 157 | + sys.exit(1) |
| 158 | + |
| 159 | + |
| 160 | +if __name__ == '__main__': |
| 161 | + main() |
0 commit comments