-
Notifications
You must be signed in to change notification settings - Fork 0
DEVOPS-2704-lightrun-k-8-s-operator-init-container-modify-update-config-script-to-work-with-read-only-root-file-system #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
imeliran
merged 5 commits into
main
from
DEVOPS-2704-lightrun-k-8-s-operator-init-container-modify-update-config-script-to-work-with-read-only-root-file-system
Apr 20, 2025
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b8adf26
Refactor update_config.sh to improve structure and readability
356f2b2
Refactor update_config.sh to use a temporary directory structure
681cc42
Refactor update_config.sh to streamline configuration updates
ef9c70e
fix update config function
bacf880
fix update_config function
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,118 @@ | ||
| #!/bin/sh | ||
| # Script to initialize and configure the Lightrun agent | ||
| # This script: | ||
| # 1. Validates required environment variables | ||
| # 2. Sets up a working directory | ||
| # 3. Merges configuration files | ||
| # 4. Updates configuration with environment variables | ||
| # 5. Copies the final configuration to destination | ||
|
|
||
| set -e | ||
|
|
||
| if [[ ${LIGHTRUN_KEY} == "" ]]; then | ||
| echo "Missing LIGHTRUN_KEY env variable" | ||
| exit 1 | ||
| fi | ||
| if [[ ${PINNED_CERT} == "" ]]; then | ||
| echo "Missing PINNED_CERT env variable" | ||
| exit 1 | ||
| fi | ||
| if [[ ${LIGHTRUN_SERVER} == "" ]]; then | ||
| echo "Missing LIGHTRUN_SERVER env variable" | ||
| exit 1 | ||
| fi | ||
|
|
||
|
|
||
| echo "Merging configs" | ||
| 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 | ||
| cp /tmp/tempconf /agent/agent.config | ||
| cp /tmp/cm/agent.metadata.json /agent/agent.metadata.json | ||
| rm /tmp/tempconf | ||
| echo "Set server and secrets" | ||
| sed -i.bak "s|com.lightrun.server=.*|com.lightrun.server=https://$LIGHTRUN_SERVER|" /agent/agent.config && rm /agent/agent.config.bak | ||
| sed -i.bak "s|com.lightrun.secret=.*|com.lightrun.secret=$LIGHTRUN_KEY|" /agent/agent.config && rm /agent/agent.config.bak | ||
| sed -i.bak "s|pinned_certs=.*|pinned_certs=$PINNED_CERT|" /agent/agent.config && rm /agent/agent.config.bak | ||
| cp -R /agent /tmp/agent | ||
| echo "Finished" | ||
| # Constants | ||
| TMP_DIR="/tmp" | ||
| WORK_DIR="${TMP_DIR}/agent-workdir" | ||
| FINAL_DEST="${TMP_DIR}/agent" | ||
| CONFIG_MAP_DIR="${TMP_DIR}/cm" | ||
|
|
||
| # Function to validate required environment variables | ||
| validate_env_vars() { | ||
| local missing_vars="" | ||
|
|
||
| if [ -z "${LIGHTRUN_KEY}" ]; then | ||
| missing_vars="${missing_vars} LIGHTRUN_KEY" | ||
| fi | ||
| if [ -z "${PINNED_CERT}" ]; then | ||
| missing_vars="${missing_vars} PINNED_CERT" | ||
| fi | ||
| if [ -z "${LIGHTRUN_SERVER}" ]; then | ||
| missing_vars="${missing_vars} LIGHTRUN_SERVER" | ||
| fi | ||
|
|
||
| if [ -n "${missing_vars}" ]; then | ||
| echo "Error: Missing required environment variables:${missing_vars}" | ||
| exit 1 | ||
| fi | ||
| } | ||
|
|
||
| # Function to setup working directory | ||
| setup_working_dir() { | ||
| echo "Setting up working directory at ${WORK_DIR}" | ||
| mkdir -p "${WORK_DIR}" | ||
| cp -R /agent/* "${WORK_DIR}/" | ||
| } | ||
|
|
||
| # Function to merge configuration files | ||
| merge_configs() { | ||
| echo "Merging configuration files" | ||
| local temp_conf="${WORK_DIR}/tempconf" | ||
|
|
||
| # Merge base config and mounted configmap config | ||
| 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]}' \ | ||
| "${WORK_DIR}/agent.config" \ | ||
| "${CONFIG_MAP_DIR}/agent.config" > "${temp_conf}" | ||
|
|
||
| # Replace the config in the workdir with the merged one | ||
| cp "${temp_conf}" "${WORK_DIR}/agent.config" | ||
|
|
||
| # Copy metadata from configmap to workdir | ||
| cp "${CONFIG_MAP_DIR}/agent.metadata.json" "${WORK_DIR}/agent.metadata.json" | ||
|
|
||
| rm "${temp_conf}" | ||
| } | ||
|
|
||
| # Function to update configuration with environment variables | ||
| update_config() { | ||
| echo "Updating configuration with environment variables" | ||
| local config_file="${WORK_DIR}/agent.config" | ||
|
|
||
| # Update server URL | ||
| if ! sed -n "s|com.lightrun.server=.*|com.lightrun.server=https://${LIGHTRUN_SERVER}|p" "${config_file}" > "${config_file}.tmp"; then | ||
| echo "Error: Failed to update server URL in configuration" | ||
| exit 1 | ||
| fi | ||
| mv "${config_file}.tmp" "${config_file}" | ||
|
|
||
| # Update secret key | ||
| if ! sed -n "s|com.lightrun.secret=.*|com.lightrun.secret=${LIGHTRUN_KEY}|p" "${config_file}" > "${config_file}.tmp"; then | ||
| echo "Error: Failed to update secret key in configuration" | ||
| exit 1 | ||
| fi | ||
| mv "${config_file}.tmp" "${config_file}" | ||
|
|
||
| # Update pinned certificates | ||
| if ! sed -n "s|pinned_certs=.*|pinned_certs=${PINNED_CERT}|p" "${config_file}" > "${config_file}.tmp"; then | ||
| echo "Error: Failed to update pinned certificates in configuration" | ||
| exit 1 | ||
| fi | ||
| mv "${config_file}.tmp" "${config_file}" | ||
| } | ||
|
|
||
| # Function to copy final configuration | ||
| copy_final_config() { | ||
| echo "Copying configured agent to final destination ${FINAL_DEST}" | ||
| cp -R "${WORK_DIR}" "${FINAL_DEST}" | ||
| } | ||
|
|
||
| # Function to cleanup | ||
| cleanup() { | ||
| echo "Cleaning up working directory" | ||
| rm -rf "${WORK_DIR}" | ||
| } | ||
|
|
||
| # Main execution | ||
| main() { | ||
| validate_env_vars | ||
| setup_working_dir | ||
| merge_configs | ||
| update_config | ||
| copy_final_config | ||
| cleanup | ||
| echo "Configuration completed successfully" | ||
| } | ||
|
|
||
| # Execute main function | ||
| main | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sed -n prints just the line it replaced. you should use: