#!/bin/bash # Variables declare -A variables variables=( ["groupId"]="${GROUP_ID}" ["tenant"]="${SCS_TENANT}" ["env"]="${SCS_ENV}" ["scspk"]="${SCS_PK}" ["scssp"]="${SCS_SP}" ) splunk_edge_path="/splunk-edge" auth_command="/auth" config_path="${splunk_edge_path}/etc" token_path="${splunk_edge_path}/var" config_file="config.yaml" token_file="token" bootstrap_version="v0.0.198-689fac1f-20221119t005809" regular=1 debug=2 # Function to check environmental variables check_variables() { for var in "${!variables[@]}"; do if [ -z "${variables[$var]}" ]; then echo "\$$var is empty, exiting" exit 1 else echo "\$$var is NOT empty" fi done echo "All variables are not empty; proceeding with Edge Processor initialization" return 0 } # Debug function debug() { echo "Debug logging enabled" echo "---------------------" env $auth_command -privatekey "${variables['scspk']}" -serviceprincipal "${variables['scssp']}" -tenant "${variables['tenant']}" -debug echo "Exit Code $?" return 0 } # Function to check configuration file check_configuration_file() { config_file_full_path="$config_path/$config_file" # Create configuration directory if it does not exist if [ ! -d "$config_path" ]; then mkdir -p "$config_path" fi # Create configuration file if it does not exist if [ ! -f "$config_file_full_path" ]; then echo "Creating new configuration file with variables" touch "$config_file_full_path" echo "env: ${variables[env]}" >> "$config_file_full_path" echo "groupId: ${variables[groupId]}" >> "$config_file_full_path" echo "tenant: ${variables[tenant]}" >> "$config_file_full_path" else echo "Configuration file already exists" fi return 0 } # Function to generate token get_token() { token_full_path="$token_path/$token_file" if [[ $log_level -eq $debug ]]; then echo "Auth command debug:" $auth_command -privatekey "${variables['scspk']}" -serviceprincipal "${variables['scssp']}" -tenant "${variables['tenant']}" -debug fi echo "Authorizing and creating token file" $auth_command -privatekey "${variables['scspk']}" -serviceprincipal "${variables['scssp']}" -tenant "${variables['tenant']}" > "$token_full_path" return 0 } # Function to check and generate token file check_token_file() { if [ ! -d "$token_path" ]; then echo "Creating path to token file" mkdir -p $token_path else echo "Token file path already exists" fi token_full_path="$token_path/$token_file" if [ ! -f "$token_full_path" ] || [ ! -s "$token_full_path" ]; then get_token fi return 0 } pull_bootstrapper() { curl "https://beam.scs.splunk.com/splunk-edge/${bootstrap_version}/linux/splunk-edge.tar.gz" -O tar -xvzf splunk-edge.tar.gz } offboard() { echo "offboard function executing due to SIGTERM or SIGINT" SPLUNK_EDGE_PID=$(pidof splunk-edge) kill $SPLUNK_EDGE_PID $splunk_edge_path/bin/splunk-edge offboard } trap offboard SIGTERM SIGINT # Set log level if [ -n "$log_level" ] && [ "$log_level" -ne "$debug" ]; then log_level=$regular fi # Run functions if [[ $log_level -eq $debug ]]; then debug fi pull_bootstrapper check_variables check_configuration_file check_token_file # Run the application mkdir -p ${splunk_edge_path}/var/log #$splunk_edge_path/bin/splunk-edge run nohup $splunk_edge_path/bin/splunk-edge run >> $splunk_edge_path/var/log/install-splunk-edge.out 2>&1