diff --git a/datasources/linux/kubernetes/eks/kubernetes/installer/functions.bash b/datasources/linux/kubernetes/eks/kubernetes/installer/functions.bash index f388391e7..a599fce74 100755 --- a/datasources/linux/kubernetes/eks/kubernetes/installer/functions.bash +++ b/datasources/linux/kubernetes/eks/kubernetes/installer/functions.bash @@ -127,37 +127,11 @@ function get_is_fargate_was_selected { # Eksctl binary file in Logz.io temp directory function download_eksctl { local func_name="${FUNCNAME[0]}" + local binary_name="eksctl" + local download_url=$(get_arch_specific_url "$EKSCTL_URL_DOWNLOAD" "$EKSCTL_ARM_URL_DOWNLOAD") + local binary_path="$LOGZIO_TEMP_DIR/$binary_name" - local message='Downloading eksctl ...' - send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" - write_log "$LOG_LEVEL_DEBUG" "$message" - - curl -fsSL "$EKSCTL_URL_DOWNLOAD" >"$LOGZIO_TEMP_DIR/eksctl.tar.gz" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="installer.bash ($EXIT_CODE): $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" - write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE - fi - - tar -zxf "$LOGZIO_TEMP_DIR/eksctl.tar.gz" --directory "$LOGZIO_TEMP_DIR" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="installer.bash ($EXIT_CODE): error extracting files from eksctl.tar.gz: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" - write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE - fi - - chmod +x "$EKSCTL_BIN" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="installer.bash ($EXIT_CODE): error giving execute premissions to '$EKSCTL_BIN': $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" - write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE - fi + download_binary "$download_url" "$binary_name" "$binary_path" } # Creates Fargate profile with monitoring namespace on Kubernetes cluster diff --git a/datasources/mac/kubernetes/eks/kubernetes/installer/functions.bash b/datasources/mac/kubernetes/eks/kubernetes/installer/functions.bash index 541e6e33e..ec33db8fa 100755 --- a/datasources/mac/kubernetes/eks/kubernetes/installer/functions.bash +++ b/datasources/mac/kubernetes/eks/kubernetes/installer/functions.bash @@ -127,39 +127,14 @@ function get_is_fargate_was_selected { # Eksctl binary file in Logz.io temp directory function download_eksctl { local func_name="${FUNCNAME[0]}" + local binary_name="eksctl" + local download_url=$(get_arch_specific_url "$EKSCTL_URL_DOWNLOAD" "$EKSCTL_ARM_URL_DOWNLOAD") + local binary_path="$LOGZIO_TEMP_DIR/$binary_name" - local message='Downloading eksctl ...' - send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" - write_log "$LOG_LEVEL_DEBUG" "$message" - - curl -fsSL "$EKSCTL_URL_DOWNLOAD" >"$LOGZIO_TEMP_DIR/eksctl.tar.gz" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="installer.bash ($EXIT_CODE): $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" - write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE - fi - - tar -zxf "$LOGZIO_TEMP_DIR/eksctl.tar.gz" --directory "$LOGZIO_TEMP_DIR" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="installer.bash ($EXIT_CODE): error extracting files from eksctl.tar.gz: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" - write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE - fi - - chmod +x "$EKSCTL_BIN" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="installer.bash ($EXIT_CODE): error giving execute premissions to '$EKSCTL_BIN': $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" - write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE - fi + download_binary "$download_url" "$binary_name" "$binary_path" } + # Creates Fargate profile with monitoring namespace on Kubernetes cluster # Input: # --- diff --git a/resources-linux/otel/subtype_installer_utils.bash b/resources-linux/otel/subtype_installer_utils.bash index 495f5bdb5..0c18d328a 100755 --- a/resources-linux/otel/subtype_installer_utils.bash +++ b/resources-linux/otel/subtype_installer_utils.bash @@ -79,39 +79,11 @@ function remove_logzio_otel_collector_service { # OTEL collector exe in Logz.io temp directory function download_otel_collector_binary { local func_name="${FUNCNAME[0]}" + local binary_name="$OTEL_COLLECTOR_BIN_NAME" + local download_url=$(get_arch_specific_url "$OTEL_COLLECTOR_URL_DOWNLOAD" "$OTEL_COLLECTOR_ARM_URL_DOWNLOAD") + local binary_path="$LOGZIO_TEMP_DIR/$binary_name" - local message='Downloading OTEL collector binary ...' - send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" - write_log "$LOG_LEVEL_DEBUG" "$message" - - if [[ ! -z "$PROXY" ]]; then - curl --proxy "$PROXY" -fsSL "$OTEL_COLLECTOR_URL_DOWNLOAD" >"$LOGZIO_TEMP_DIR/otelcol-contrib.tar.gz" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="installer.bash ($EXIT_CODE): error downloading otelcol-logzio.tar.gz: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" - write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE - fi - else - curl -fsSL "$OTEL_COLLECTOR_URL_DOWNLOAD" >"$LOGZIO_TEMP_DIR/otelcol-contrib.tar.gz" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="installer.bash ($EXIT_CODE): error downloading otelcol-logzio.tar.gz: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" - write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE - fi - fi - - tar -zxf "$LOGZIO_TEMP_DIR/otelcol-contrib.tar.gz" --directory "$LOGZIO_TEMP_DIR" 'otelcol-contrib' 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="installer.bash ($EXIT_CODE): error extracting files from otelcol-contrib.tar.gz: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" - write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE - fi + download_binary "$download_url" "$binary_name" "$binary_path" } # Creates Logz.io opt subdirectory diff --git a/resources-mac/otel/subtype_installer_utils.bash b/resources-mac/otel/subtype_installer_utils.bash index 6767d7629..3be18c14d 100755 --- a/resources-mac/otel/subtype_installer_utils.bash +++ b/resources-mac/otel/subtype_installer_utils.bash @@ -71,28 +71,11 @@ function remove_logzio_otel_collector_service { # OTEL collector exe in Logz.io temp directory function download_otel_collector_binary { local func_name="${FUNCNAME[0]}" + local binary_name="$OTEL_COLLECTOR_BIN_NAME" + local download_url=$(get_arch_specific_url "$OTEL_COLLECTOR_URL_DOWNLOAD" "$OTEL_COLLECTOR_ARM_URL_DOWNLOAD") + local binary_path="$LOGZIO_TEMP_DIR/$binary_name" - local message='Downloading OTEL collector binary ...' - send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" - write_log "$LOG_LEVEL_DEBUG" "$message" - - curl -fsSL "$OTEL_COLLECTOR_URL_DOWNLOAD" >"$LOGZIO_TEMP_DIR/otelcol-logzio.tar.gz" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="installer.bash ($EXIT_CODE): error downloading otelcol-logzio.tar.gz: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" - write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE - fi - - tar -zxf "$LOGZIO_TEMP_DIR/otelcol-logzio.tar.gz" --directory "$LOGZIO_TEMP_DIR" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="installer.bash ($EXIT_CODE): error extracting files from otelcol-logzio.tar.gz: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" - write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE - fi + download_binary "$download_url" "$binary_name" "$binary_path" } # Creates Logz.io opt subdirectory diff --git a/resources/otel/receivers/hostmetrics.yaml b/resources/otel/receivers/hostmetrics.yaml index cc399d34c..4867958eb 100644 --- a/resources/otel/receivers/hostmetrics.yaml +++ b/resources/otel/receivers/hostmetrics.yaml @@ -32,6 +32,9 @@ receiver: network: paging: process: + exclude: + names: ['launcher'] + match_type: strict mute_process_name_error: true mute_process_exe_error: true mute_process_io_error: true diff --git a/scripts/linux/agent_linux/functions.bash b/scripts/linux/agent_linux/functions.bash new file mode 100644 index 000000000..4b4073112 --- /dev/null +++ b/scripts/linux/agent_linux/functions.bash @@ -0,0 +1,758 @@ +#!/bin/bash + +################################################################################################################################# +###################################################### LINUX Agent Functions #################################################### +################################################################################################################################# + +# Gets Linux info +# Input: +# --- +# Output: +# LINUX_NAME - Linux name +# LINUX_VERSION - Linux version +# CPU_ARCH - Linux cpu architecture +function get_linux_info { + local func_name="${FUNCNAME[0]}" + + local message='Getting Linux info ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + local linux_info=$(cat /etc/os-release 2>"$TASK_ERROR_FILE") + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): error getting Linux info: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + local linux_name=$(echo -e "$linux_info" | grep -oP '(?<=^NAME=").*?(?=")') + write_task_post_run "LINUX_NAME=\"$linux_name\"" + + message="Linux name is '$linux_name'" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + local linux_version=$(echo -e "$linux_info" | grep -oP '(?<=^VERSION=").*?(?=")') + write_task_post_run "LINUX_VERSION=\"$linux_version\"" + + message="Linux version is '$linux_version'" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + local cpu_arch=$(uname -p 2>"$TASK_ERROR_FILE") + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): error getting cpu arch: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + message="CPU architecture is '$cpu_arch'" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + write_task_post_run "CPU_ARCH=\"$cpu_arch\"" +} + +# Checks if bash version is 4.0 or above +# Input: +# --- +# Output: +# --- +function is_bash_version_4_or_above { + local func_name="${FUNCNAME[0]}" + + local message='Checking if bash version is 4.0 or above ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + local bash_major_version=$(echo -e "$BASH_VERSION" | cut -d. -f1) + if [[ $bash_major_version -lt 4 ]]; then + message="agent.bash ($EXIT_CODE): bash version must be 4.0 or above" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi +} + + +# Prints usage +# Input: +# --- +# Output: +# Help usage +function show_help { + write_task_post_run "echo -e \"Usage: .\agent.bash --url= --id= [--debug=] [--release=] [--tmp_dest=] [--proxy=}\"" + write_task_post_run "echo -e ' --url= Logz.io app URL (https://app.logz.io)'" + write_task_post_run "echo -e ' --id= Logz.io agent ID'" + write_task_post_run "echo -e ' --debug= Debug run using a local agent json file'" + write_task_post_run "echo -e ' --release= The release of Logz.io repo. Default is latest release'" + write_task_post_run "echo -e ' --temp_dest= The temp files destination path. Default is /tmp/logzio'" + write_task_post_run "echo -e ' --proxy= The proxy url'" + write_task_post_run "echo -e ' --help Show usage'" +} + +# Gets arguments +# Input: +# --- +# Output: +# APP_URL - Logz.io app url +# AGENT_ID - Logz.io agent id +# AGENT_JSON_FILE - Agent json file path (for debug) +# REPO_RELEASE - Repo release (for debug) +function get_arguments { + local func_name="${FUNCNAME[0]}" + + local message='Getting arguments ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + for arg in "${AGENT_ARGS[@]}"; do + case "$arg" in + --help) + show_help + write_task_post_run "IS_SHOW_HELP=true" + + return + ;; + --url=*) + app_url=$(echo -e "$arg" | cut -d '=' -f2) + if [[ -z "$app_url" ]]; then + message="agent.bash ($EXIT_CODE): no Logz.io app URL specified!" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + message="Agent argument 'url' is '$app_url'" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + write_task_post_run "APP_URL='$app_url'" + ;; + --id=*) + agent_id=$(echo "$arg" | cut -d '=' -f2) + if [[ -z "$agent_id" ]]; then + message="agent.bash ($EXIT_CODE): no agent ID specified!" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + message="Agent argument 'id' is '$agent_id'" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + write_task_post_run "AGENT_ID='$agent_id'" + ;; + --debug=*) + agent_json_file=$(echo "$arg" | cut -d '=' -f2) + if [[ -z "$agent_json_file" ]]; then + message="agent.bash ($EXIT_CODE): no json file specified!" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + message="Agent argument 'debug' is '$agent_json_file'" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + write_task_post_run "AGENT_JSON_FILE='$agent_json_file'" + ;; + --release=*) + repo_release=$(echo "$arg" | cut -d '=' -f2) + + message="Agent argument 'release' is '$repo_release'" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + write_task_post_run "REPO_RELEASE='$repo_release'" + ;; + --temp_dest=*) + temp_dest=$(echo "$arg" | cut -d '=' -f2) + + message="Agent argument 'temp_dest' is '$temp_dest'" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + ;; + --proxy=*) + proxy=$(echo "$arg" | cut -d '=' -f2) + + message="Agent argument 'proxy' is '$proxy'" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + write_task_post_run "PROXY='$proxy'" + ;; + *) + message="agent.bash ($EXIT_CODE): unrecognized flag" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + message="agent.bash ($EXIT_CODE): try running the agent with '--help' flag for more information" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + ;; + esac + done +} + +# Checks validation of the arguments +# Input: +# --- +# Output: +# if debug flag was used AGENT_ID='Debug' +function check_arguments_validation { + local func_name="${FUNCNAME[0]}" + + local message='Checking validation ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + if [[ ! -z "$AGENT_JSON_FILE" ]]; then + if [[ -f "$AGENT_JSON_FILE" ]]; then + write_task_post_run "AGENT_ID='Debug'" + return + fi + + message="agent.bash ($EXIT_CODE): the json file '$AGENT_JSON_FILE' does not exist" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + local is_error=false + + if [[ -z "$APP_URL" ]]; then + is_error=true + message="agent.bash ($EXIT_CODE): Logz.io app url must be specified" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + fi + if [[ -z "$AGENT_ID" ]]; then + is_error=true + message="agent.bash ($EXIT_CODE): agent id must be specified" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + fi + + if ! $is_error; then + return + fi + + message="agent.bash ($EXIT_CODE): try running the agent with '--help' flag for more information" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE +} + +# Download binary +function download_binary { + local download_url="$1" + local binary_name="$2" + local binary_path="$3" + local func_name="${FUNCNAME[0]}" + + # Check if the binary is already installed + if copy_installed_binary "$binary_name" "$download_url" "$binary_path"; then + write_log "$LOG_LEVEL_DEBUG" "$binary_name is already installed. Skipping download." + return 0 + fi + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$download_message" "$LOG_STEP_PRE_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_log "$LOG_LEVEL_DEBUG" "$download_message" + # Extract the binary from the tar.gz archive + if [[ "$download_url" == *".tar.gz" ]]; then + local tar_path="$LOGZIO_TEMP_DIR/$binary_name.tar.gz" + + curl -fsSL "$download_url" >"$tar_path" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="installer.bash ($EXIT_CODE): error downloading $binary_name.tar.gz: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + tar -zxf "$tar_path" --directory "$LOGZIO_TEMP_DIR" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="installer.bash ($EXIT_CODE): error extracting files from $tar_path: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + # Assuming the extracted binary starts with "$binary_name(_|-)" + local extracted_binary=$(find "$LOGZIO_TEMP_DIR" -type f -name "${binary_name}[-|_]*" | head -n 1) + message="Attempting to move extracted binary file: $extracted_binary using this pattern: '${binary_name}[-|_]*'" + send_log_to_logzio "$LOG_LEVEL_INFO" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" + # Check if a matching file is found + if [ -n "$extracted_binary" ]; then + # Move the file to the destination + mv "$extracted_binary" "$binary_path" + + # Check if the move was successful + if [ $? -eq 0 ]; then + message="$extracted_binary binary file moved successfully." + send_log_to_logzio "$LOG_LEVEL_INFO" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" + else + message="Error: Failed to move the $extracted_binary binary file to: $binary_path." + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" + + fi + else + message="No matching binary file that contains this name: $binary_name found." + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" + + fi + rm -f $tar_path + else + # Directly download the binary file + curl -fsSL --create-dirs "$download_url" > "$binary_path" 2>"$TASK_ERROR_FILE" + + if [[ $? -ne 0 ]]; then + local error_message="installer.bash ($EXIT_CODE): error downloading $binary_name: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$error_message" "$LOG_STEP_PRE_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_task_post_run "write_error \"$error_message\"" + return $EXIT_CODE + + fi + fi + + + # Provide execution permissions to binary file + chmod +x "$binary_path" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="installer.bash ($EXIT_CODE): error giving execute premissions to '$binary_path': $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi +} + +# Downloads jq +# Input: +# --- +# Output: +# Jq binary file in Logz.io temp directory +function download_jq { + local func_name="${FUNCNAME[0]}" + local binary_name="jq" + local binary_path="$LOGZIO_TEMP_DIR/$binary_name" + local download_url=$(get_arch_specific_url "$JQ_URL_DOWNLOAD" "$JQ_ARM_URL_DOWNLOAD") + + download_binary "$download_url" "$binary_name" "$binary_path" +} + +# Downloads yq +# Input: +# --- +# Output: +# Yq binary file in Logz.io temp directory +function download_yq { + local func_name="${FUNCNAME[0]}" + local binary_name="yq" + local binary_path="$LOGZIO_TEMP_DIR/$binary_name" + local download_url=$(get_arch_specific_url "$YQ_URL_DOWNLOAD" "$YQ_ARM_URL_DOWNLOAD") + + download_binary "$download_url" "$binary_name" "$binary_path" +} + +# Gets the agent json from the agent or local file +# Input: +# --- +# Output: +# Agent json file in Logz.io temp directory +function get_agent_json { + local func_name="${FUNCNAME[0]}" + + local message='Getting agent json ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + if [[ ! -z "$AGENT_JSON_FILE" ]]; then + # Using local app json file + message='Using local agent json file ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + cp "$AGENT_JSON_FILE" "$AGENT_JSON" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): error copying '$AGENT_JSON_FILE' to '$AGENT_JSON': $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + return + fi + + # Getting agent json from agent + message='Getting agent json from agent ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + if [[ ! -z "$PROXY" ]]; then + curl --proxy "$PROXY" -fsSL "$APP_URL/telemetry-agent/public/agents/configuration/$AGENT_ID" >"$AGENT_JSON" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): error getting Logz.io agent json from agent. make sure your url is valid: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + else + curl -fsSL "$APP_URL/telemetry-agent/public/agents/configuration/$AGENT_ID" >"$AGENT_JSON" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): error getting Logz.io agent json from agent. make sure your url is valid: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + fi + + get_json_file_field_value "$AGENT_JSON" '.statusCode' + local func_status=$? + if [[ $func_status -eq 1 ]]; then + message="agent.bash ($EXIT_CODE): $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + if [[ $func_status -eq 0 ]]; then + local status_code="$JSON_VALUE" + + message="agent.bash ($EXIT_CODE): error getting Logz.io agent json from agent (statusCode '$status_code'). make sure your id is valid." + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi +} + +# Gets agent json info +# input: +# --- +# Output: +# PLATFORM - Platfrom name +# SUB_TYPE - Subtype name +# DATA_SOURCES - List of datasource names +function get_agent_json_info { + local func_name="${FUNCNAME[0]}" + + local message='Getting agent json info ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + if [[ ! -f "$AGENT_JSON" ]]; then + message="agent.bash ($EXIT_CODE): agent json file does not exist" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + fi + + get_json_file_field_value "$AGENT_JSON" '.configuration.name' + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + local platform="$JSON_VALUE" + + message="Platform is '$platform'" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + local command="PLATFORM='$platform'" + write_task_post_run "$command" + + get_json_file_field_value "$AGENT_JSON" '.configuration.subtypes[0].name' + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + local sub_type="$JSON_VALUE" + + message="Subtype is '$sub_type'" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + command="SUB_TYPE='$sub_type'" + write_task_post_run "$command" + + get_json_file_field_value_list "$AGENT_JSON" '.configuration.subtypes[0].datasources[]' + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + local data_sources=("${JSON_VALUE[@]}") + + local index=0 + local data_source_names=() + for data_source in "${data_sources[@]}"; do + get_json_str_field_value "$data_source" '.name' + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + local data_source_name="$JSON_VALUE" + + message="DataSource #$((index+1)) is '$data_source_name'" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + write_log "$LOG_LEVEL_DEBUG" "$message" + + data_source_names+=("$data_source_name") + ((index++)) + done + + LIST=("${data_source_names[@]}") + local data_sources_str=$(convert_list_to_str) + command="DATA_SOURCES=$data_sources_str" + write_task_post_run "$command" +} + +# Gets Logz.io listener url +# Input: +# --- +# Ouput: +# LISTENER_URL - Logz.io listener url +function get_logzio_listener_url { + local func_name="${FUNCNAME[0]}" + + local message='Getting Logz.io listener url ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_log "$LOG_LEVEL_DEBUG" "$message" + + get_json_file_field_value "$AGENT_JSON" '.listenerUrl' + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): $result" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + + local listener_url="$JSON_VALUE" + + message="Logz.io listener url is '$listener_url'" + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_log "$LOG_LEVEL_DEBUG" "$message" + + write_task_post_run "LISTENER_URL='$listener_url'" +} + +# Downloads subtype files +# Input: +# --- +# Output: +# Subtype files in Logz.io temp directory +function download_sub_type_files { + local func_name="${FUNCNAME[0]}" + + local message='Donwloading subtype files ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_log "$LOG_LEVEL_DEBUG" "$message" + + if [[ -z "$REPO_RELEASE" ]]; then + if [[ ! -z "$PROXY" ]]; then + curl --proxy "$PROXY" -fsSL "https://github.com/logzio/logzio-agent-manifest/releases/latest/download/linux_${PLATFORM,,}_${SUB_TYPE,,}.tar.gz" >"$LOGZIO_TEMP_DIR/linux_${PLATFORM,,}_${SUB_TYPE,,}.tar.gz" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): error downloading subtype tar.gz file from Logz.io repo: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + else + curl -fsSL "https://github.com/logzio/logzio-agent-manifest/releases/latest/download/linux_${PLATFORM,,}_${SUB_TYPE,,}.tar.gz" >"$LOGZIO_TEMP_DIR/linux_${PLATFORM,,}_${SUB_TYPE,,}.tar.gz" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): error downloading subtype tar.gz file from Logz.io repo: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + fi + else + if [[ ! -z "$PROXY" ]]; then + curl --proxy "$PROXY" -fsSL "https://github.com/logzio/logzio-agent-manifest/releases/download/$REPO_RELEASE/linux_${PLATFORM,,}_${SUB_TYPE,,}.tar.gz" >"$LOGZIO_TEMP_DIR/linux_${PLATFORM,,}_${SUB_TYPE,,}.tar.gz" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): error downloading subtype tar.gz file from Logz.io repo: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + else + curl -fsSL "https://github.com/logzio/logzio-agent-manifest/releases/download/$REPO_RELEASE/linux_${PLATFORM,,}_${SUB_TYPE,,}.tar.gz" >"$LOGZIO_TEMP_DIR/linux_${PLATFORM,,}_${SUB_TYPE,,}.tar.gz" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): error downloading subtype tar.gz file from Logz.io repo: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi + fi + fi + + tar -zxf "$LOGZIO_TEMP_DIR/linux_${PLATFORM,,}_${SUB_TYPE,,}.tar.gz" --directory "$LOGZIO_TEMP_DIR" --overwrite 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($EXIT_CODE): error extracting files from '$LOGZIO_TEMP_DIR/linux_${PLATFORM,,}_${SUB_TYPE,,}.tar.gz': $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_task_post_run "write_error \"$message\"" + + return $EXIT_CODE + fi +} + +# Runs subtype prerequisites +# Input: +# --- +# Output: +# --- +function run_sub_type_prerequisites { + local func_name="${FUNCNAME[0]}" + + local message='Laoding subtype prerequisites functions ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_log "$LOG_LEVEL_DEBUG" "$message" + + source "$LOGZIO_TEMP_DIR/${PLATFORM,,}/${SUB_TYPE,,}/$PREREQUISITES_FUNCTIONS_FILE" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($AGENT_EXIT_CODE): error loading subtype prerequisites functions: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_error "$message" + + IS_AGENT_FAILED=true + run_final + exit $AGENT_EXIT_CODE + fi + + message='Running subtype prerequisites ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_log "$LOG_LEVEL_DEBUG" "$message" + + source "$LOGZIO_TEMP_DIR/${PLATFORM,,}/${SUB_TYPE,,}/$PREREQUISITES_FILE" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($AGENT_EXIT_CODE): error running subtype prerequisites: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_error "$message" + + IS_AGENT_FAILED=true + run_final + exit $AGENT_EXIT_CODE + fi + + ((AGENT_EXIT_CODE++)) +} + +# Runs subtype installer +# Input: +# --- +# Output: +# --- +function run_sub_type_installer { + local func_name="${FUNCNAME[0]}" + + local message='Laoding subtype installer functions ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_log "$LOG_LEVEL_DEBUG" "$message" + + source "$LOGZIO_TEMP_DIR/${PLATFORM,,}/${SUB_TYPE,,}/$INSTALLER_FUNCTIONS_FILE" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($AGENT_EXIT_CODE): error loading subtype installer functions: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_error "$message" + + IS_AGENT_FAILED=true + run_final + exit $AGENT_EXIT_CODE + fi + + message='Running subtype installer ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_log "$LOG_LEVEL_DEBUG" "$message" + + source "$LOGZIO_TEMP_DIR/${PLATFORM,,}/${SUB_TYPE,,}/$INSTALLER_FILE" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($AGENT_EXIT_CODE): error running subtype installer: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_error "$message" + + IS_AGENT_FAILED=true + run_final + exit $AGENT_EXIT_CODE + fi + + ((AGENT_EXIT_CODE++)) +} + +# Runs subtype post-requisites +# Input: +# --- +# Output: +# --- +function run_sub_type_postrequisites { + local func_name="${FUNCNAME[0]}" + + local message='Laoding subtype post-requisites functions ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_log "$LOG_LEVEL_DEBUG" "$message" + + source "$LOGZIO_TEMP_DIR/${PLATFORM,,}/${SUB_TYPE,,}/$POSTREQUISITES_FUNCTIONS_FILE" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($AGENT_EXIT_CODE): error loading subtype post-requisites functions: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_error "$message" + + IS_AGENT_FAILED=true + run_final + exit $AGENT_EXIT_CODE + fi + + local message='Running subtype post-requisites ...' + send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_log "$LOG_LEVEL_DEBUG" "$message" + + source "$LOGZIO_TEMP_DIR/${PLATFORM,,}/${SUB_TYPE,,}/$POSTREQUISITES_FILE" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="agent.bash ($AGENT_EXIT_CODE): error running subtype post-requisites: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" + write_error "$message" + + IS_AGENT_FAILED=true + run_final + exit $AGENT_EXIT_CODE + fi + + ((AGENT_EXIT_CODE++)) +} diff --git a/scripts/linux/consts.bash b/scripts/linux/consts.bash index 7debf5166..8ce5cdc97 100755 --- a/scripts/linux/consts.bash +++ b/scripts/linux/consts.bash @@ -53,13 +53,21 @@ AGENT_JSON="$LOGZIO_TEMP_DIR/agent.json" ## Urls # Url for downloading jq binary -JQ_URL_DOWNLOAD='https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64' +JQ_URL_DOWNLOAD='https://github.com/stedolan/jq/releases/download/jq-1.7.1/jq-linux64' # Url for downloading yq binary -YQ_URL_DOWNLOAD='https://github.com/mikefarah/yq/releases/download/v4.33.2/yq_linux_amd64.tar.gz' +YQ_URL_DOWNLOAD='https://github.com/mikefarah/yq/releases/download/v4.40.5/yq_linux_amd64.tar.gz' # Url for downloading OTEL collector tar.gz OTEL_COLLECTOR_URL_DOWNLOAD='https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.82.0/otelcol-contrib_0.82.0_linux_amd64.tar.gz' # Url for downloading eksctl tar.gz EKSCTL_URL_DOWNLOAD='https://github.com/weaveworks/eksctl/releases/download/v0.133.0/eksctl_Linux_amd64.tar.gz' +# Url for downloading jq arm64 binary +JQ_ARM_URL_DOWNLOAD='https://github.com/stedolan/jq/releases/download/jq-1.7.1/jq-linux-arm64' +# Url for downloading yq arm64 binary +YQ_ARM_URL_DOWNLOAD='https://github.com/mikefarah/yq/releases/download/v4.40.5/yq_linux_arm64.tar.gz' +# Url for downloading OTEL collector arm64 tar.gz +OTEL_COLLECTOR_ARM_URL_DOWNLOAD='https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.82.0/otelcol-contrib_0.82.0_linux_arm64.tar.gz' +# Url for downloading eksctl arm64 tar.gz +EKSCTL_ARM_URL_DOWNLOAD='https://github.com/weaveworks/eksctl/releases/download/v0.133.0/eksctl_linux_arm64.tar.gz' # Url for AWS SQS SQS_URL='https://sqs.us-east-1.amazonaws.com/486140753397/LogzioAgentQueue' @@ -67,7 +75,7 @@ SQS_URL='https://sqs.us-east-1.amazonaws.com/486140753397/LogzioAgentQueue' # Jq binary file path JQ_BIN="$LOGZIO_TEMP_DIR/jq" # Yq binary file path -YQ_BIN="$LOGZIO_TEMP_DIR/yq_linux_amd64" +YQ_BIN="$LOGZIO_TEMP_DIR/yq" # Eksctl binary file path EKSCTL_BIN="$LOGZIO_TEMP_DIR/eksctl" diff --git a/scripts/linux/functions.bash b/scripts/linux/functions.bash index 392de1df5..68dd2ec59 100755 --- a/scripts/linux/functions.bash +++ b/scripts/linux/functions.bash @@ -257,97 +257,164 @@ function check_arguments_validation { return $EXIT_CODE } -# Downloads jq -# Input: -# --- -# Output: -# Jq binary file in Logz.io temp directory -function download_jq { +# Download binary +function download_binary { + local download_url="$1" + local binary_name="$2" + local binary_path="$3" local func_name="${FUNCNAME[0]}" - local message='Downloading jq ...' - send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" - write_log "$LOG_LEVEL_DEBUG" "$message" - - if [[ ! -z "$PROXY" ]]; then - curl --proxy "$PROXY" -fsSL "$JQ_URL_DOWNLOAD" >"$JQ_BIN" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="agent.bash ($EXIT_CODE): error downloading jq binary: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" - write_task_post_run "write_error \"$message\"" + # Check if the binary is already installed + if copy_installed_binary "$binary_name" "$download_url" "$binary_path"; then + write_log "$LOG_LEVEL_DEBUG" "$binary_name is already installed. Skipping download." + return 0 + fi - return $EXIT_CODE - fi + # Download the binary + local downloaded_file + if [[ "$download_url" == *".tar.gz" ]]; then + downloaded_file=$(download_tarball "$download_url" "$binary_name") else - curl -fsSL "$JQ_URL_DOWNLOAD" >"$JQ_BIN" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="agent.bash ($EXIT_CODE): error downloading jq binary: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" - write_task_post_run "write_error \"$message\"" + downloaded_file=$(download_direct "$download_url" "$binary_path") || return 1 + fi - return $EXIT_CODE - fi + # Provide execution permissions to binary file + chmod_binary "$binary_path" "$func_name" || return 1 + + return 0 +} + +# Helper function to download tarballs +function download_tarball { + local download_url="$1" + local binary_name="$2" + local tar_path="$LOGZIO_TEMP_DIR/$binary_name.tar.gz" + + # Create a timestamp-based subdirectory for extraction + local extract_dir="$LOGZIO_TEMP_DIR/${binary_name}_extract" + mkdir -p "$extract_dir" + + # Download the tarball + curl -fsSL "$download_url" >"$tar_path" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="installer.bash: error downloading $binary_name.tar.gz: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "${FUNCNAME[1]}" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" + write_task_post_run "write_error \"$message\"" + return 1 fi - chmod +x "$JQ_BIN" 2>"$TASK_ERROR_FILE" + # Extract the tarball + tar -zxf "$tar_path" --directory "$extract_dir" 2>"$TASK_ERROR_FILE" if [[ $? -ne 0 ]]; then - message="agent.bash ($EXIT_CODE): error giving execute premissions to '$JQ_BIN': $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + message="installer.bash: error extracting files from $tar_path: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "${FUNCNAME[1]}" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" write_task_post_run "write_error \"$message\"" + return 1 + fi - return $EXIT_CODE + # Find the extracted binary + local extracted_binary + extracted_binary=$(find "$extract_dir" -maxdepth 1 -type f -name "${binary_name}*" ! -name "*.*" | head -n 1) + + if [[ -z "$extracted_binary" ]]; then + message="installer.bash: no matching binary file named: ${binary_name} found in the extracted directory" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "${FUNCNAME[1]}" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" + write_task_post_run "write_error \"$message\"" + return 1 fi -} -# Downloads yq -# Input: -# --- -# Output: -# Yq binary file in Logz.io temp directory -function download_yq { - local func_name="${FUNCNAME[0]}" + # Move the binary to the destination + move_binary "$extracted_binary" "$LOGZIO_TEMP_DIR/$binary_name" - local message='Downloading yq ...' - send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" - write_log "$LOG_LEVEL_DEBUG" "$message" + # Clean up the temporary directory and tar file + rm -rf "$extract_dir" "$LOGZIO_TEMP_DIR/$binary_name.tar.gz" - if [[ ! -z "$PROXY" ]]; then - curl --proxy "$PROXY" -fsSL "$YQ_URL_DOWNLOAD" >"$LOGZIO_TEMP_DIR/yq.tar.gz" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="agent.bash ($EXIT_CODE): error downloading yq tar.gz: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" - write_task_post_run "write_error \"$message\"" + echo "$binary_name" +} - return $EXIT_CODE - fi - else - curl -fsSL "$YQ_URL_DOWNLOAD" >"$LOGZIO_TEMP_DIR/yq.tar.gz" 2>"$TASK_ERROR_FILE" - if [[ $? -ne 0 ]]; then - message="agent.bash ($EXIT_CODE): error downloading yq tar.gz: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" - write_task_post_run "write_error \"$message\"" - return $EXIT_CODE - fi - fi +# Helper function to download binary directly +function download_direct { + local download_url="$1" + local binary_path="$2" - tar -zxf "$LOGZIO_TEMP_DIR/yq.tar.gz" --directory "$LOGZIO_TEMP_DIR" --overwrite 2>"$TASK_ERROR_FILE" + curl -fsSL --create-dirs "$download_url" >"$binary_path" 2>"$TASK_ERROR_FILE" if [[ $? -ne 0 ]]; then - message="agent.bash ($EXIT_CODE): error extracting yq tar.gz file: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + message="installer.bash: error downloading binary: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" write_task_post_run "write_error \"$message\"" + return 1 + fi - return $EXIT_CODE + echo "$binary_path" +} + +# Helper function to move the binary to the destination +function move_binary { + local source_path="$1" + local binary_path="$2" + local func_name="$3" + + if [[ -z "$source_path" ]]; then + message="installer.bash: error moving binary to $binary_path: Source path is empty" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" + write_task_post_run "write_error \"$message\"" + return 1 fi - chmod +x "$YQ_BIN" 2>"$TASK_ERROR_FILE" + mv "$source_path" "$binary_path" if [[ $? -ne 0 ]]; then - message="agent.bash ($EXIT_CODE): error giving execute premissions to '$YQ_BIN': $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + message="installer.bash: error moving binary to $binary_path: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" write_task_post_run "write_error \"$message\"" + return 1 + fi - return $EXIT_CODE + return 0 +} + +# Helper function to provide execution permissions to binary file +function chmod_binary { + local binary_path="$1" + local func_name="$2" + + chmod +x "$binary_path" 2>"$TASK_ERROR_FILE" + if [[ $? -ne 0 ]]; then + message="installer.bash: error giving execute permissions to '$binary_path': $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" + write_task_post_run "write_error \"$message\"" + return 1 fi + + return 0 +} + +# Downloads jq +# Input: +# --- +# Output: +# Jq binary file in Logz.io temp directory +function download_jq { + local func_name="${FUNCNAME[0]}" + local binary_name="jq" + local binary_path="$LOGZIO_TEMP_DIR/$binary_name" + local download_url=$(get_arch_specific_url "$JQ_URL_DOWNLOAD" "$JQ_ARM_URL_DOWNLOAD") + + download_binary "$download_url" "$binary_name" "$binary_path" +} + +# Downloads yq +# Input: +# --- +# Output: +# Yq binary file in Logz.io temp directory +function download_yq { + local func_name="${FUNCNAME[0]}" + local binary_name="yq" + local binary_path="$LOGZIO_TEMP_DIR/$binary_name" + local download_url=$(get_arch_specific_url "$YQ_URL_DOWNLOAD" "$YQ_ARM_URL_DOWNLOAD") + + download_binary "$download_url" "$binary_name" "$binary_path" } # Gets the agent json from the agent or local file diff --git a/scripts/linux/utils_functions.bash b/scripts/linux/utils_functions.bash index 492900667..8ac292a44 100755 --- a/scripts/linux/utils_functions.bash +++ b/scripts/linux/utils_functions.bash @@ -61,8 +61,9 @@ function write_task_post_run { function get_task_error_message { local err=$(cat "$TASK_ERROR_FILE") err="${err//\"/\\\"}" - - echo -e "$err" + if [[ -n "$err" ]]; then + echo -e "$err" + fi } # Sends log to Logz.io @@ -584,3 +585,50 @@ function execute_task { ((EXIT_CODE++)) >"$TASK_POST_RUN_FILE" } + +# Check if binary is already installed with the correct version +function copy_installed_binary { + local binary_name="$1" + local download_url="$2" + local binary_path="$3" + + # Check if the binary is installed + if command -v "$binary_name" &>/dev/null; then + # If version validation is not required, binary is considered installed + if [ -z "$download_url" ]; then + # Copy the installed binary to the specified path, if provided + if [ -n "$binary_path" ]; then + cp "$(command -v "$binary_name")" "$binary_path" + fi + return 0 # Binary is installed + fi + + # Fetch the content of the version URL + downloaded_version=$(curl -s "$download_url") + + # Check if the downloaded version matches the installed version + installed_version=$("$binary_name" --version | head -n 1) + if echo "$installed_version" | grep -E -q "$downloaded_version"; then + # Copy the installed binary to the specified path, if provided + if [ -n "$binary_path" ]; then + cp "$(command -v "$binary_name")" "$binary_path" + fi + return 0 # Binary is installed with the correct version + else + return 1 # Binary is installed, but version does not match + fi + else + return 1 # Binary is not installed + fi +} +# Function to get the architecture-specific download URL +function get_arch_specific_url { + local default_url="$1" + local arm_url="$2" + + if [[ ( "$CPU_ARCH" == "arm64" || "$CPU_ARCH" == "aarch64" ) && ! -z "$arm_url" ]]; then + echo "$arm_url" + else + echo "$default_url" + fi +} \ No newline at end of file diff --git a/scripts/mac/consts.bash b/scripts/mac/consts.bash index b226b20fc..60291b900 100755 --- a/scripts/mac/consts.bash +++ b/scripts/mac/consts.bash @@ -55,13 +55,21 @@ AGENT_JSON="$LOGZIO_TEMP_DIR/agent.json" ## Urls # Url for downloading jq binary -JQ_URL_DOWNLOAD='https://github.com/stedolan/jq/releases/download/jq-1.6/jq-osx-amd64' +JQ_URL_DOWNLOAD='https://github.com/stedolan/jq/releases/download/jq-1.7.1/jq-osx-amd64' # Url for downloading yq binary -YQ_URL_DOWNLOAD='https://github.com/mikefarah/yq/releases/download/v4.33.2/yq_darwin_amd64.tar.gz' +YQ_URL_DOWNLOAD='https://github.com/mikefarah/yq/releases/download/v4.40.5/yq_darwin_amd64.tar.gz' # Url for downloading OTEL collector tar.gz OTEL_COLLECTOR_URL_DOWNLOAD='https://github.com/logzio/otel-collector-distro/releases/download/v0.82.0/otelcol-logzio-darwin_amd64.tar.gz' # Url for downloading eksctl tar.gz EKSCTL_URL_DOWNLOAD='https://github.com/weaveworks/eksctl/releases/download/v0.133.0/eksctl_Darwin_amd64.tar.gz' +# Url for downloading jq arm64 binary +JQ_ARM_URL_DOWNLOAD='https://github.com/stedolan/jq/releases/download/jq-1.7.1/jq-macos-arm64' +# Url for downloading yq arm64 binary +YQ_ARM_URL_DOWNLOAD='https://github.com/mikefarah/yq/releases/download/v4.40.5/yq_darwin_arm64.tar.gz' +# Url for downloading OTEL collector arm64 tar.gz +OTEL_COLLECTOR_ARM_URL_DOWNLOAD='https://github.com/logzio/otel-collector-distro/releases/download/v0.82.0/otelcol-logzio-darwin_arm64.tar.gz' +# Url for downloading eksctl arm64 tar.gz +EKSCTL_ARM_URL_DOWNLOAD='https://github.com/weaveworks/eksctl/releases/download/v0.133.0/eksctl_Darwin_arm64.tar.gz' # Url for AWS SQS SQS_URL='https://sqs.us-east-1.amazonaws.com/486140753397/LogzioAgentQueue' @@ -69,7 +77,7 @@ SQS_URL='https://sqs.us-east-1.amazonaws.com/486140753397/LogzioAgentQueue' # Jq binary file path JQ_BIN="$LOGZIO_TEMP_DIR/jq" # Yq binary file path -YQ_BIN="$LOGZIO_TEMP_DIR/yq_darwin_amd64" +YQ_BIN="$LOGZIO_TEMP_DIR/yq" # Eksctl binary file path EKSCTL_BIN="$LOGZIO_TEMP_DIR/eksctl" @@ -77,7 +85,7 @@ EKSCTL_BIN="$LOGZIO_TEMP_DIR/eksctl" # OTEL function file OTEL_FUNCTION_FILE="$LOGZIO_TEMP_DIR/otel_function.bash" # OTEL collector binary file name -OTEL_COLLECTOR_BIN_NAME='otelcol-logzio-darwin_amd64' +OTEL_COLLECTOR_BIN_NAME='otelcol-logzio' # OTEL collector binary file path OTEL_COLLECTOR_BIN="$LOGZIO_OTEL_COLLECTOR_DIR/$OTEL_COLLECTOR_BIN_NAME" # OTEL config file name diff --git a/scripts/mac/functions.bash b/scripts/mac/functions.bash index f4f1b5564..bdec6e006 100755 --- a/scripts/mac/functions.bash +++ b/scripts/mac/functions.bash @@ -31,7 +31,7 @@ function get_mac_info { write_task_post_run "MAC_NAME=\"$mac_name\"" message="Mac name is '$mac_name'" - send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + send_log_to_logzio "$LOG_LEVEL_DEBUG"˜ "$message" "$LOG_STEP_PRE_INIT" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" write_log "$LOG_LEVEL_DEBUG" "$message" local mac_version=$(echo -e "$mac_info" | grep 'ProductVersion:' | tr -d " \t\n\r" | cut -d':' -f2) @@ -242,75 +242,165 @@ function check_arguments_validation { return $EXIT_CODE } -# Downloads jq -# Input: -# --- -# Output: -# Jq binary file in Logz.io temp directory -function download_jq { +# Download binary +function download_binary { + local download_url="$1" + local binary_name="$2" + local binary_path="$3" local func_name="${FUNCNAME[0]}" - local message='Downloading jq ...' - send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" - write_log "$LOG_LEVEL_DEBUG" "$message" + # Check if the binary is already installed + if copy_installed_binary "$binary_name" "$download_url" "$binary_path"; then + write_log "$LOG_LEVEL_DEBUG" "$binary_name is already installed. Skipping download." + return 0 + fi - curl -fsSL "$JQ_URL_DOWNLOAD" >"$JQ_BIN" 2>"$TASK_ERROR_FILE" + # Download the binary + local downloaded_file + if [[ "$download_url" == *".tar.gz" ]]; then + downloaded_file=$(download_tarball "$download_url" "$binary_name") + else + downloaded_file=$(download_direct "$download_url" "$binary_path") || return 1 + fi + + # Provide execution permissions to binary file + chmod_binary "$binary_path" "$func_name" || return 1 + + return 0 +} + +# Helper function to download tarballs +function download_tarball { + local download_url="$1" + local binary_name="$2" + local tar_path="$LOGZIO_TEMP_DIR/$binary_name.tar.gz" + + # Create a timestamp-based subdirectory for extraction + local extract_dir="$LOGZIO_TEMP_DIR/${binary_name}_extract" + mkdir -p "$extract_dir" + + # Download the tarball + curl -fsSL "$download_url" >"$tar_path" 2>"$TASK_ERROR_FILE" if [[ $? -ne 0 ]]; then - message="agent.bash ($EXIT_CODE): error downloading jq binary: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + message="installer.bash: error downloading $binary_name.tar.gz: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "${FUNCNAME[1]}" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE + return 1 fi - chmod +x "$JQ_BIN" 2>"$TASK_ERROR_FILE" + # Extract the tarball + tar -zxf "$tar_path" --directory "$extract_dir" 2>"$TASK_ERROR_FILE" if [[ $? -ne 0 ]]; then - message="agent.bash ($EXIT_CODE): error giving execute premissions to '$JQ_BIN': $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + message="installer.bash: error extracting files from $tar_path: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "${FUNCNAME[1]}" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" write_task_post_run "write_error \"$message\"" + return 1 + fi - return $EXIT_CODE + # Find the extracted binary + local extracted_binary + extracted_binary=$(find "$extract_dir" -maxdepth 1 -type f -name "${binary_name}*" ! -name "*.*" | head -n 1) + + if [[ -z "$extracted_binary" ]]; then + message="installer.bash: no matching binary file named: ${binary_name} found in the extracted directory" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "${FUNCNAME[1]}" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" + write_task_post_run "write_error \"$message\"" + return 1 fi + + # Move the binary to the destination + move_binary "$extracted_binary" "$LOGZIO_TEMP_DIR/$binary_name" + + # Clean up the temporary directory and tar file + rm -rf "$extract_dir" "$LOGZIO_TEMP_DIR/$binary_name.tar.gz" + + echo "$binary_name" } -# Downloads yq -# Input: -# --- -# Output: -# Yq binary file in Logz.io temp directory -function download_yq { - local func_name="${FUNCNAME[0]}" - local message='Downloading yq ...' - send_log_to_logzio "$LOG_LEVEL_DEBUG" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" - write_log "$LOG_LEVEL_DEBUG" "$message" +# Helper function to download binary directly +function download_direct { + local download_url="$1" + local binary_path="$2" - curl -fsSL "$YQ_URL_DOWNLOAD" >"$LOGZIO_TEMP_DIR/yq.tar.gz" 2>"$TASK_ERROR_FILE" + curl -fsSL --create-dirs "$download_url" >"$binary_path" 2>"$TASK_ERROR_FILE" if [[ $? -ne 0 ]]; then - message="agent.bash ($EXIT_CODE): error downloading yq tar.gz: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + message="installer.bash: error downloading binary: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" write_task_post_run "write_error \"$message\"" + return 1 + fi - return $EXIT_CODE + echo "$binary_path" +} + +# Helper function to move the binary to the destination +function move_binary { + local source_path="$1" + local binary_path="$2" + local func_name="$3" + + if [[ -z "$source_path" ]]; then + message="installer.bash: error moving binary to $binary_path: Source path is empty" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" + write_task_post_run "write_error \"$message\"" + return 1 fi - tar -zxf "$LOGZIO_TEMP_DIR/yq.tar.gz" --directory "$LOGZIO_TEMP_DIR" 2>"$TASK_ERROR_FILE" + mv "$source_path" "$binary_path" if [[ $? -ne 0 ]]; then - message="agent.bash ($EXIT_CODE): error extracting yq tar.gz file: $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + message="installer.bash: error moving binary to $binary_path: $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE + return 1 fi - chmod +x "$YQ_BIN" 2>"$TASK_ERROR_FILE" + return 0 +} + +# Helper function to provide execution permissions to binary file +function chmod_binary { + local binary_path="$1" + local func_name="$2" + + chmod +x "$binary_path" 2>"$TASK_ERROR_FILE" if [[ $? -ne 0 ]]; then - message="agent.bash ($EXIT_CODE): error giving execute premissions to '$YQ_BIN': $(get_task_error_message)" - send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_DOWNLOADS" "$LOG_SCRIPT_AGENT" "$func_name" "$AGENT_ID" + message="installer.bash: error giving execute permissions to '$binary_path': $(get_task_error_message)" + send_log_to_logzio "$LOG_LEVEL_ERROR" "$message" "$LOG_STEP_INSTALLATION" "$LOG_SCRIPT_INSTALLER" "$func_name" "$AGENT_ID" "$PLATFORM" "$SUB_TYPE" "$CURRENT_DATA_SOURCE" write_task_post_run "write_error \"$message\"" - - return $EXIT_CODE + return 1 fi + + return 0 +} + + +# Downloads jq +# Input: +# --- +# Output: +# Jq binary file in Logz.io temp directory +function download_jq { + local func_name="${FUNCNAME[0]}" + local binary_name="jq" + local binary_path="$LOGZIO_TEMP_DIR/$binary_name" + local download_url=$(get_arch_specific_url "$JQ_URL_DOWNLOAD" "$JQ_ARM_URL_DOWNLOAD") + + download_binary "$download_url" "$binary_name" "$binary_path" +} + +# Downloads yq +# Input: +# --- +# Output: +# Yq binary file in Logz.io temp directory +function download_yq { + local func_name="${FUNCNAME[0]}" + local binary_name="yq" + local binary_path="$LOGZIO_TEMP_DIR/$binary_name" + local download_url=$(get_arch_specific_url "$YQ_URL_DOWNLOAD" "$YQ_ARM_URL_DOWNLOAD") + + download_binary "$download_url" "$binary_name" "$binary_path" } # Gets the agent json from the agent or local file diff --git a/scripts/mac/utils_functions.bash b/scripts/mac/utils_functions.bash index 307cddd3a..82d5db22d 100755 --- a/scripts/mac/utils_functions.bash +++ b/scripts/mac/utils_functions.bash @@ -580,3 +580,50 @@ function execute_task { ((EXIT_CODE++)) >"$TASK_POST_RUN_FILE" } + +# Check if binary is already installed with the correct version, if it does copy it +function copy_installed_binary { + local binary_name="$1" + local download_url="$2" + local binary_path="$3" + + # Check if the binary is installed + if command -v "$binary_name" &>/dev/null; then + # If version validation is not required, binary is considered installed + if [ -z "$download_url" ]; then + # Copy the installed binary to the specified path, if provided + if [ -n "$binary_path" ]; then + cp "$(command -v "$binary_name")" "$binary_path" + fi + return 0 # Binary is installed + fi + + # Fetch the content of the version URL + downloaded_version=$(curl -s "$download_url") + + # Check if the downloaded version matches the installed version + installed_version=$("$binary_name" --version | head -n 1) + if echo "$installed_version" | grep -E -q "$downloaded_version"; then + # Copy the installed binary to the specified path, if provided + if [ -n "$binary_path" ]; then + cp "$(command -v "$binary_name")" "$binary_path" + fi + return 0 # Binary is installed with the correct version + else + return 1 # Binary is installed, but version does not match + fi + else + return 1 # Binary is not installed + fi +} +# Function to get the architecture-specific download URL +function get_arch_specific_url { + local default_url="$1" + local arm_url="$2" + + if [[ ( "$CPU_ARCH" == "arm64" || "$CPU_ARCH" == "aarch64" ) && ! -z "$arm_url" ]]; then + echo "$arm_url" + else + echo "$default_url" + fi +} \ No newline at end of file