|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | +import subprocess |
| 5 | +import os |
| 6 | + |
| 7 | +# Check if this is an automated or manual execution |
| 8 | +automated = os.getenv("FLUENT_BIT_CVE_AUTOMATION") |
| 9 | +automated = isinstance(automated, string) and len(automated) > 0 and int(automated) > 0 |
| 10 | + |
| 11 | +git_cli_path = '/usr/bin/git' |
| 12 | + |
| 13 | +def run_command(command_arr, timeout=None, tries=1, cwd=None, fail_on_error=False, env=None): |
| 14 | + i = 0 |
| 15 | + output = None |
| 16 | + for i in range(tries): |
| 17 | + try: |
| 18 | + output = subprocess.run(command_arr, |
| 19 | + stdout=subprocess.PIPE, |
| 20 | + timeout=timeout, |
| 21 | + cwd=cwd, |
| 22 | + env=env).stdout.decode('utf-8') |
| 23 | + except subprocess.TimeoutExpired as ex: |
| 24 | + print(f"Command {command_arr} timed out after {ex.timeout} seconds.") |
| 25 | + print(f"Partial output (stdout): {ex.stdout}") |
| 26 | + print(f"Partial output (stderr): {ex.stderr}") |
| 27 | + except Exception as ex: |
| 28 | + print(f"Command {command_arr} had unexpected exception {ex}") |
| 29 | + |
| 30 | + # Return output if the command succeeded |
| 31 | + if output is not None: |
| 32 | + return output |
| 33 | + |
| 34 | + # Failure handling |
| 35 | + print(f"Failed to run command {command_arr}") |
| 36 | + if fail_on_error: |
| 37 | + print("Quitting...") |
| 38 | + quit() |
| 39 | + |
| 40 | + |
| 41 | +is_new_al_version = run_command(["./scripts/check_for_new_al_version.sh"], timeout=10, tries=3, fail_on_error=True) |
| 42 | +new_al2_version = None |
| 43 | +if is_new_al_version == 'false': |
| 44 | + print("Amazon Linux version is up to date.") |
| 45 | + quit() |
| 46 | +else: |
| 47 | + print("New Amazon Linux version is available.") |
| 48 | + new_al2_version = run_command(["./scripts/most_recent_al2.sh"], timeout=10, tries=3, fail_on_error=True) |
| 49 | + print("New AL2 version: {}".format(new_al2_version)) |
| 50 | + |
| 51 | +# Get upstream_to_fluent_bit |
| 52 | +max_clone_attempts = 3 |
| 53 | +upstream_repo = "/tmp/fluent-bit/" |
| 54 | +upstream_uri = "https://github.com/amazon-contributing/upstream-to-fluent-bit.git" |
| 55 | +for i in range(max_clone_attempts): |
| 56 | + |
| 57 | + run_command(["rm", "-rf", upstream_repo]) |
| 58 | + run_command(["mkdir", upstream_repo]) |
| 59 | + clone_output = run_command(["git", "clone", upstream_uri, upstream_repo], |
| 60 | + fail_on_error=False, timeout=30) |
| 61 | + if clone_output is not None: |
| 62 | + run_command(["git", "checkout", "1.9.10"], cwd=upstream_repo, |
| 63 | + fail_on_error=True) |
| 64 | + break |
| 65 | +else: |
| 66 | + print("Couldn't clone FLB upstream. Will ignore upstream commits.") |
| 67 | + upstream_repo = None |
| 68 | + |
| 69 | +# Go to a new branch for this automation. |
| 70 | +numeric_date_string = datetime.now().strftime("%Y%m%d") |
| 71 | +branch_name = "patch-automation-{}".format(numeric_date_string) |
| 72 | +run_command([git_cli_path, "fetch".format(numeric_date_string)], |
| 73 | + fail_on_error=True, tries=2) |
| 74 | +run_command([git_cli_path, "switch", "-C", branch_name], |
| 75 | + fail_on_error=True) |
| 76 | +current_branch = run_command(["git", "rev-parse", "--abbrev-ref", "HEAD"], |
| 77 | + fail_on_error=True) |
| 78 | +current_branch = current_branch.strip() |
| 79 | +if current_branch != branch_name: |
| 80 | + print("failed to switch branch to {}".format(branch_name)) |
| 81 | + print("current branch: {}".format(current_branch)) |
| 82 | + quit() |
| 83 | +# Reset the branch to master in case it isn't already |
| 84 | +run_command([git_cli_path, "reset", "--hard", "mainline"]) |
| 85 | + |
| 86 | +# Derive the new version number |
| 87 | +old_version = run_command(["cat", "AWS_FOR_FLUENT_BIT_VERSION"], fail_on_error=True) |
| 88 | +semantic_version_components = old_version.split('.') |
| 89 | +version_num_count = len(semantic_version_components) |
| 90 | +if version_num_count == 3: |
| 91 | + semantic_version_components = semantic_version_components + [numeric_date_string] |
| 92 | +elif version_num_count == 4: |
| 93 | + semantic_version_components[3] = numeric_date_string |
| 94 | +else: |
| 95 | + print("Invalid semantic versioning for current version. Aborting...") |
| 96 | + quit() |
| 97 | + |
| 98 | +new_version = ".".join(semantic_version_components) |
| 99 | + |
| 100 | +# Update version file |
| 101 | +with open("AWS_FOR_FLUENT_BIT_VERSION", "w") as version_file: |
| 102 | + version_file.write(new_version) |
| 103 | + |
| 104 | +# Update linux.version file |
| 105 | +with open("linux.version", 'r') as version_file: |
| 106 | + linux_version = version_file.readlines() |
| 107 | + |
| 108 | +old_version = "{}".format(old_version) |
| 109 | +print("old aws-for-fluent-bit version number: {}".format(old_version)) |
| 110 | +print("new aws-for-fluent-bit version number: {}".format(new_version)) |
| 111 | +for linum in range(len(linux_version)): |
| 112 | + # print("linux.version line: {}".format(linux_version[linum])) |
| 113 | + if old_version in linux_version[linum]: |
| 114 | + linux_version[linum] = linux_version[linum].replace(old_version, new_version) |
| 115 | + break |
| 116 | +else: |
| 117 | + print("could not auto-locate version number in linux.version!!!") |
| 118 | + print("Guessing at line to modify based on file format...") |
| 119 | + linux_version[2] = ' "version": "{}",\n'.format(new_version) |
| 120 | + |
| 121 | +with open("linux.version", 'w') as version_file: |
| 122 | + version_file.writelines(linux_version) |
| 123 | + |
| 124 | +# Update changelog |
| 125 | +run_command(["./scripts/generate_changelog.sh"], |
| 126 | + # Provide fluent bit directory to the changelog script, |
| 127 | + # if we were able to clone it. |
| 128 | + env={"FLUENT_BIT_DIRECTORY": upstream_repo} if upstream_repo is not None else None, |
| 129 | + timeout=20, tries=3, |
| 130 | + fail_on_error=True) |
| 131 | + |
| 132 | +# Commit changes |
| 133 | +run_command(["git", "commit", "-a", "--message", '"Release {}"'.format(new_version)], |
| 134 | + fail_on_error=True) |
| 135 | + |
| 136 | +# Push changes to remote. |
| 137 | +# Might fail if there are unexpected commits, but we ignore that as expected |
| 138 | +core_push_command = ["git", "push"] |
| 139 | +if automated: |
| 140 | + core_push_command = core_push_command + ["--force"] |
| 141 | +push_output = run_command(core_push_command, |
| 142 | + fail_on_error=False) |
| 143 | +if push_output is None or push_output == "" or ("fatal: The current branch" in push_output and "has no upstream branch" in push_output): |
| 144 | + # Sometimes you need to set the pustream if it doesn't exist |
| 145 | + print("push may have failed due to missing set-upstream. Retrying") |
| 146 | + push_output = run_command(core_push_command + ["--set-upstream", "origin", branch_name], |
| 147 | + fail_on_error=False) |
| 148 | + |
| 149 | +# Go back to mainline |
| 150 | +run_command(["git", "checkout", "mainline"], timeout=30) |
0 commit comments