diff --git a/.buildkite/hooks/pre-command b/.buildkite/hooks/pre-command index cb5bb51eb803..7ed8c64879a7 100644 --- a/.buildkite/hooks/pre-command +++ b/.buildkite/hooks/pre-command @@ -19,7 +19,9 @@ if [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-xpack-packetbeat" && "$BUILDKITE_STEP fi if [[ "$BUILDKITE_PIPELINE_SLUG" == "beats-xpack-agentbeat" && "$BUILDKITE_STEP_KEY" == *"agentbeat-it"* ]]; then - AGENTBEAT_PATH=$(.buildkite/scripts/agentbeat/setup_agentbeat.py | tail -n 1) + out=$(.buildkite/scripts/agentbeat/setup_agentbeat.py) + echo "--- setup_agentbeat.py out: $out" + AGENTBEAT_PATH=$( $out | tail -n 1) export AGENTBEAT_PATH fi diff --git a/.buildkite/scripts/agentbeat/setup_agentbeat.py b/.buildkite/scripts/agentbeat/setup_agentbeat.py index 35139ec22787..c1e27890fdcd 100755 --- a/.buildkite/scripts/agentbeat/setup_agentbeat.py +++ b/.buildkite/scripts/agentbeat/setup_agentbeat.py @@ -6,6 +6,26 @@ import tarfile PATH = 'x-pack/agentbeat/build/distributions' +PLATFORMS = { + 'windows': { + 'x86_64': 'x86_64', + }, + 'linux': { + 'x86_64': 'x86_64', + 'arm64': 'arm64', + }, + 'darwin': { + 'x86_64': 'x86_64', + 'arm64': 'aarch64', + } + } + + +class Archive: + def __init__(self, os, arch, ext): + self.os = os + self.arch = arch + self.ext = ext def log(msg): @@ -18,38 +38,29 @@ def log_err(msg): sys.stderr.flush() -def get_os() -> str: - return platform.system().lower() +def get_archive_params() -> Archive: + system = platform.system().lower() + machine = platform.machine().lower() + arch = PLATFORMS.get(system, {}).get(machine) + ext = get_artifact_extension(system) + return Archive(system, arch, ext) -def get_arch() -> str: - arch = platform.machine().lower() - if arch == 'amd64': - return 'x86_64' - else: - if get_os() == 'darwin': - return 'aarch64' - else: - return arch - - -def get_artifact_extension(agent_os) -> str: - if agent_os == 'windows': +def get_artifact_extension(system) -> str: + if system == 'windows': return 'zip' else: return 'tar.gz' -def get_artifact_pattern() -> str: - agent_os = get_os() - agent_arch = get_arch() - extension = get_artifact_extension(agent_os) - print('Artifact params: ' + agent_os + ' ' + agent_arch + ' ' + extension) - return f'{PATH}/agentbeat-*-{agent_os}-{agent_arch}.{extension}' +def get_artifact_pattern(archive_obj) -> str: + return f'{PATH}/agentbeat-*-{archive_obj.os}-{archive_obj.arch}.{archive_obj.ext}' -def download_agentbeat(pattern, path) -> str: +def download_agentbeat(archive_obj) -> str: + pattern = get_artifact_pattern(archive_obj) + print('Downloading matching agentbeat artifact: ' + pattern) try: subprocess.run( ['buildkite-agent', 'artifact', 'download', pattern, '.', @@ -59,21 +70,23 @@ def download_agentbeat(pattern, path) -> str: except subprocess.CalledProcessError: exit(1) - return get_filename(path) + return get_full_filename() -def get_filename(path) -> str: +def get_full_filename() -> str: try: out = subprocess.run( - ['ls', '-p', path], + ['ls', '-p', PATH], check=True, capture_output=True, text=True) - print("--- ls -p: " + out.stdout) - return out.stdout.strip() + filename = out.stdout.strip() + print("Archive name: " + filename) + return filename except subprocess.CalledProcessError: exit(1) def extract_agentbeat(filename): + print('Extracting agentbeat artifact: ' + filename) filepath = PATH + '/' + filename if filepath.endswith('.zip'): @@ -107,12 +120,14 @@ def get_path_to_executable(filepath) -> str: match = re.match(pattern, filepath) if match: path = f'../../{match.group(1)}/agentbeat' + print('Calculated path to executable: ' + path) return path else: log_err("No agentbeat executable found") exit(1) -artifact_pattern = get_artifact_pattern() -archive = download_agentbeat(artifact_pattern, PATH) +print('--- Setting up Agentbeat') +archive_params = get_archive_params() +archive = download_agentbeat(archive_params) extract_agentbeat(archive) log(get_path_to_executable(archive))