From d024335aab52e959e41e918aac21dcd81f409769 Mon Sep 17 00:00:00 2001 From: Justin Kolberg Date: Thu, 31 Oct 2024 01:43:55 -0700 Subject: [PATCH] wip Signed-off-by: Justin Kolberg --- utils/detect_version.cmake | 32 +++++++++++++ version_artifact.cmake | 98 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 utils/detect_version.cmake create mode 100644 version_artifact.cmake diff --git a/utils/detect_version.cmake b/utils/detect_version.cmake new file mode 100644 index 00000000..68f1c4aa --- /dev/null +++ b/utils/detect_version.cmake @@ -0,0 +1,32 @@ +function(detect_version _artifact_bin _working_dir) + # Construct version command + set(_cmd_str "./${_artifact_bin} --version") + separate_arguments(_cmd UNIX_COMMAND ${_cmd_str}) + + message(STATUS "Setting working directory: ${_working_dir}") + message(STATUS "Running version command: ${_cmd_str}") + + # Get output of otelcol-sumo --version + execute_process(COMMAND ${_cmd} + WORKING_DIRECTORY ${_working_dir} + COMMAND_ERROR_IS_FATAL ANY + OUTPUT_VARIABLE _version_output + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + + message(STATUS "Version output: ${_version_output}") + + string(REGEX MATCH ".* v([0-9]+\.[0-9]+\.[0-9]+)\-sumo\-([0-9]+)\-.*" _ ${_version_output}) + + if(NOT CMAKE_MATCH_COUNT EQUAL 2) + message(FATAL_ERROR "Could not parse version information from version output") + endif() + + set(_otc_version "${CMAKE_MATCH_1}") + set(_sumo_version "${CMAKE_MATCH_2}") + set(_otc_version "${_otc_version}" PARENT_SCOPE) + set(_sumo_version "${_sumo_version}" PARENT_SCOPE) + + message(STATUS "Detected OTC version: ${_otc_version}") + message(STATUS "Detected Sumo version: ${_sumo_version}") +endfunction() diff --git a/version_artifact.cmake b/version_artifact.cmake new file mode 100644 index 00000000..2a325574 --- /dev/null +++ b/version_artifact.cmake @@ -0,0 +1,98 @@ +## +# This file is a CMake script and should be run in CMake's script mode. +# E.g. cmake -P fetch_artifacts.cmake +# +# Its purpose is to fetch otelcol-sumo matching the host system's operating +# system & CPU architecture from a given GitHub Actions workflow. This +# otelcol-sumo binary is used to determine the version for packages and is not +# included in the generated packages. +## + +# Require CMake >= 3.24.1 +cmake_minimum_required(VERSION 3.24.1 FATAL_ERROR) + +# Required environment variables +if(NOT DEFINED ENV{GH_TOKEN}) + message(FATAL_ERROR "GH_TOKEN environment variable must be set") +endif() + +if(NOT DEFINED ENV{GH_WORKFLOW_ID}) + message(FATAL_ERROR "GH_WORKFLOW_ID environment variable must be set") +endif() + +# Required and optional programs. Attempts to find required and optional +# programs used to build the packages. +find_program(GH_PROGRAM gh REQUIRED) + +# Include the CMakeDetermineSystem module manually as it is not included when +# CMake runs in script mode. This is required for the host's CPU architecture +# to be detected. +set(CMAKE_PLATFORM_INFO_DIR "${CMAKE_SOURCE_DIR}/build/cmake") +include(CMakeDetermineSystem) + +# Set supported platforms for version detection +set(_supported_platforms "") +list(APPEND _supported_platforms "darwin_amd64") +list(APPEND _supported_platforms "darwin_arm64") +list(APPEND _supported_platforms "linux_amd64") +list(APPEND _supported_platforms "linux_arm64") + +# Detect host system +string(TOLOWER "${CMAKE_HOST_SYSTEM_NAME}" _os) +string(TOLOWER "${CMAKE_HOST_SYSTEM_PROCESSOR}" _arch) + +# Convert _arch to GOARCH equivalent name +if("${_arch}" STREQUAL "aarch64") + set(_arch "arm64") +endif() + +set(_platform "${_os}_${_arch}") +message(STATUS "Detected platform: ${_platform}") + +if(NOT "${_platform}" IN_LIST _supported_platforms) + message(FATAL_ERROR "This script does not support this machine's platform: ${_platform}") +endif() + +# Set directory variables +set(UTILS_DIR "${CMAKE_SOURCE_DIR}/utils") +set(_build_dir "${CMAKE_SOURCE_DIR}/build") +set(_version_detection_dir "${_build_dir}/version_detection") + +# Include CMake files +include("${CMAKE_SOURCE_DIR}/utils.cmake") + +# GitHub variables +set(_gh_org "SumoLogic") +set(_gh_repo "sumologic-otel-collector") +set(_gh_slug "${_gh_org}/${_gh_repo}") +set(_gh_workflow "$ENV{GH_WORKFLOW_ID}") + +# Remote OTC artifact +set(_artifact_bin "otelcol-sumo-${_os}_${_arch}") +set(_artifact_path "${_version_detection_dir}/${_artifact_bin}") +set(_new_artifact_bin "otelcol-sumo") +set(_new_artifact_path "${_version_detection_dir}/${_new_artifact_bin}") + +if(NOT EXISTS "${_new_artifact_path}") + # Construct download command + set(_cmd_str "${GH_PROGRAM} run download -R ${_gh_slug} ${_gh_workflow}") + set(_cmd_str "${_cmd_str} -n ${_artifact_bin}") + separate_arguments(_cmd UNIX_COMMAND ${_cmd_str}) + + # Create the version_detection directory + make_directory("${_version_detection_dir}") + + message(STATUS "Setting working directory: ${_version_detection_dir}") + message(STATUS "Running download command: ${_cmd_str}") + + # Execute download command + execute_process(COMMAND ${_cmd} + WORKING_DIRECTORY ${_version_detection_dir} + COMMAND_ERROR_IS_FATAL ANY) + + # Rename downloaded artifact to otelcol-sumo + file(RENAME ${_artifact_path} ${_new_artifact_path}) +endif() + +# Detect version +detect_version("${_new_artifact_bin}" "${_version_detection_dir}")