|
1 | 1 | #!/bin/sh
|
| 2 | +# Script to initialize and configure the Lightrun agent |
| 3 | +# This script: |
| 4 | +# 1. Validates required environment variables |
| 5 | +# 2. Sets up a working directory |
| 6 | +# 3. Merges configuration files |
| 7 | +# 4. Updates configuration with environment variables |
| 8 | +# 5. Copies the final configuration to destination |
| 9 | + |
2 | 10 | set -e
|
3 | 11 |
|
4 |
| -if [[ ${LIGHTRUN_KEY} == "" ]]; then |
5 |
| - echo "Missing LIGHTRUN_KEY env variable" |
6 |
| - exit 1 |
7 |
| -fi |
8 |
| -if [[ ${PINNED_CERT} == "" ]]; then |
9 |
| - echo "Missing PINNED_CERT env variable" |
10 |
| - exit 1 |
11 |
| -fi |
12 |
| -if [[ ${LIGHTRUN_SERVER} == "" ]]; then |
13 |
| - echo "Missing LIGHTRUN_SERVER env variable" |
14 |
| - exit 1 |
15 |
| -fi |
16 |
| - |
17 |
| - |
18 |
| -echo "Merging configs" |
19 |
| -awk -F'=' '{ if($1 in b) a[b[$1]]=$0;else{a[++i]=$0; b[$1]=i} }END{for(j=1;j<=i;j++) print a[j]}' /agent/agent.config /tmp/cm/agent.config > /tmp/tempconf |
20 |
| -cp /tmp/tempconf /agent/agent.config |
21 |
| -cp /tmp/cm/agent.metadata.json /agent/agent.metadata.json |
22 |
| -rm /tmp/tempconf |
23 |
| -echo "Set server and secrets" |
24 |
| -sed -i.bak "s|com.lightrun.server=.*|com.lightrun.server=https://$LIGHTRUN_SERVER|" /agent/agent.config && rm /agent/agent.config.bak |
25 |
| -sed -i.bak "s|com.lightrun.secret=.*|com.lightrun.secret=$LIGHTRUN_KEY|" /agent/agent.config && rm /agent/agent.config.bak |
26 |
| -sed -i.bak "s|pinned_certs=.*|pinned_certs=$PINNED_CERT|" /agent/agent.config && rm /agent/agent.config.bak |
27 |
| -cp -R /agent /tmp/agent |
28 |
| -echo "Finished" |
| 12 | +# Constants |
| 13 | +TMP_DIR="/tmp" |
| 14 | +WORK_DIR="${TMP_DIR}/agent-workdir" |
| 15 | +FINAL_DEST="${TMP_DIR}/agent" |
| 16 | +CONFIG_MAP_DIR="${TMP_DIR}/cm" |
| 17 | + |
| 18 | +# Function to validate required environment variables |
| 19 | +validate_env_vars() { |
| 20 | + local missing_vars="" |
| 21 | + |
| 22 | + if [ -z "${LIGHTRUN_KEY}" ]; then |
| 23 | + missing_vars="${missing_vars} LIGHTRUN_KEY" |
| 24 | + fi |
| 25 | + if [ -z "${PINNED_CERT}" ]; then |
| 26 | + missing_vars="${missing_vars} PINNED_CERT" |
| 27 | + fi |
| 28 | + if [ -z "${LIGHTRUN_SERVER}" ]; then |
| 29 | + missing_vars="${missing_vars} LIGHTRUN_SERVER" |
| 30 | + fi |
| 31 | + |
| 32 | + if [ -n "${missing_vars}" ]; then |
| 33 | + echo "Error: Missing required environment variables:${missing_vars}" |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | +} |
| 37 | + |
| 38 | +# Function to setup working directory |
| 39 | +setup_working_dir() { |
| 40 | + echo "Setting up working directory at ${WORK_DIR}" |
| 41 | + mkdir -p "${WORK_DIR}" |
| 42 | + cp -R /agent/* "${WORK_DIR}/" |
| 43 | +} |
| 44 | + |
| 45 | +# Function to merge configuration files |
| 46 | +merge_configs() { |
| 47 | + echo "Merging configuration files" |
| 48 | + local temp_conf="${WORK_DIR}/tempconf" |
| 49 | + |
| 50 | + # Merge base config and mounted configmap config |
| 51 | + awk -F'=' '{ |
| 52 | + if($1 in b) a[b[$1]]=$0; |
| 53 | + else{a[++i]=$0; b[$1]=i} |
| 54 | + } END{for(j=1;j<=i;j++) print a[j]}' \ |
| 55 | + "${WORK_DIR}/agent.config" \ |
| 56 | + "${CONFIG_MAP_DIR}/agent.config" > "${temp_conf}" |
| 57 | + |
| 58 | + # Replace the config in the workdir with the merged one |
| 59 | + cp "${temp_conf}" "${WORK_DIR}/agent.config" |
| 60 | + |
| 61 | + # Copy metadata from configmap to workdir |
| 62 | + cp "${CONFIG_MAP_DIR}/agent.metadata.json" "${WORK_DIR}/agent.metadata.json" |
| 63 | + |
| 64 | + rm "${temp_conf}" |
| 65 | +} |
| 66 | + |
| 67 | +# Function to update configuration with environment variables |
| 68 | +update_config() { |
| 69 | + echo "Updating configuration with environment variables" |
| 70 | + local config_file="${WORK_DIR}/agent.config" |
| 71 | + local missing_configuration_params="" |
| 72 | + |
| 73 | + if sed -n "s|com.lightrun.server=.*|com.lightrun.server=https://${LIGHTRUN_SERVER}|p" "${config_file}" | grep -q .; then |
| 74 | + # Perform actual in-place change |
| 75 | + sed -i "s|com.lightrun.server=.*|com.lightrun.server=https://${LIGHTRUN_SERVER}|" "${config_file}" |
| 76 | + else |
| 77 | + missing_configuration_params="${missing_configuration_params} com.lightrun.server" |
| 78 | + fi |
| 79 | + if sed -n "s|com.lightrun.secret=.*|com.lightrun.secret=${LIGHTRUN_KEY}|p" "${config_file}" | grep -q .; then |
| 80 | + # Perform actual in-place change |
| 81 | + sed -i "s|com.lightrun.secret=.*|com.lightrun.secret=${LIGHTRUN_KEY}|" "${config_file}" |
| 82 | + else |
| 83 | + missing_configuration_params="${missing_configuration_params} com.lightrun.secret" |
| 84 | + fi |
| 85 | + if sed -n "s|pinned_certs=.*|pinned_certs=${PINNED_CERT}|p" "${config_file}" | grep -q .; then |
| 86 | + # Perform actual in-place change |
| 87 | + sed -i "s|pinned_certs=.*|pinned_certs=${PINNED_CERT}|" "${config_file}" |
| 88 | + else |
| 89 | + missing_configuration_params="${missing_configuration_params} pinned_certs" |
| 90 | + fi |
| 91 | + if [ -n "${missing_configuration_params}" ]; then |
| 92 | + echo "Error: Missing configuration parameters:${missing_configuration_params}" |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | +} |
| 96 | + |
| 97 | +# Function to copy final configuration |
| 98 | +copy_final_config() { |
| 99 | + echo "Copying configured agent to final destination ${FINAL_DEST}" |
| 100 | + cp -R "${WORK_DIR}" "${FINAL_DEST}" |
| 101 | +} |
| 102 | + |
| 103 | +# Function to cleanup |
| 104 | +cleanup() { |
| 105 | + echo "Cleaning up working directory" |
| 106 | + rm -rf "${WORK_DIR}" |
| 107 | +} |
| 108 | + |
| 109 | +# Main execution |
| 110 | +main() { |
| 111 | + validate_env_vars |
| 112 | + setup_working_dir |
| 113 | + merge_configs |
| 114 | + update_config |
| 115 | + copy_final_config |
| 116 | + cleanup |
| 117 | + echo "Configuration completed successfully" |
| 118 | +} |
| 119 | + |
| 120 | +# Execute main function |
| 121 | +main |
0 commit comments