Update docker/headscale/prep.sh

This commit is contained in:
colin 2024-01-21 23:04:52 +00:00
parent 09b67842f7
commit dcab9adf67
1 changed files with 32 additions and 33 deletions

View File

@ -1,18 +1,27 @@
#!/bin/bash #!/bin/bash
strip_yaml_comments() { strip_yaml_comments() {
local file=$1 local file=$1
if [[ ! -f "$file" ]]; then if [[ ! -f "$file" ]]; then
echo "File not found: $file" echo "File not found: $file"
return 1 return 1
fi fi
grep -v '^#' "$file" | grep -v '^$'
# Filter out lines that are either full-line comments or completely empty
sed -i '/^\s*#/d;/^\s*$/d' "$file"
if [ $? -eq 0 ]; then
echo "Comments stripped successfully in $file"
else
echo "Failed to strip comments from the file."
return 1
fi
} }
download_and_process_config() { download_and_process_config() {
local url="https://raw.githubusercontent.com/juanfont/headscale/main/config-example.yaml" local url="https://raw.githubusercontent.com/juanfont/headscale/main/config-example.yaml"
local target_dir="/etc/headscale" local target_dir="/etc/headscale"
local target_file="${target_dir}/config-example.yaml" local target_file="${target_dir}/config-example.yaml"
local processed_file="${target_dir}/config-example-processed.yaml"
# Create target directory if it does not exist # Create target directory if it does not exist
mkdir -p "$target_dir" mkdir -p "$target_dir"
@ -23,20 +32,15 @@ download_and_process_config() {
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "Config downloaded successfully to $target_file" echo "Config downloaded successfully to $target_file"
# Process the downloaded file to strip comments # Process the downloaded file to strip comments
strip_yaml_comments "$target_file" > "$processed_file" strip_yaml_comments "$target_file"
if [ $? -eq 0 ]; then
echo "Processed config file saved to $processed_file"
else
echo "Failed to process the config file"
return 1
fi
else else
echo "Failed to download the config file" echo "Failed to download the config file"
return 1 return 1
fi fi
} }
insert_placeholders() {
insert_env_placeholders() {
local config_file="/etc/headscale/config-example.yaml" local config_file="/etc/headscale/config-example.yaml"
if [[ ! -f "$config_file" ]]; then if [[ ! -f "$config_file" ]]; then
@ -44,30 +48,25 @@ insert_placeholders() {
return 1 return 1
fi fi
declare -a replacements=( # Substitute only simple, single-line configuration options
"server_url: .*" "server_url: \${SERVER_URL}" sed -i 's|^server_url:.*|server_url: ${SERVER_URL}|' "$config_file"
"listen_addr: .*" "listen_addr: \${LISTEN_ADDR}" sed -i 's|^listen_addr:.*|listen_addr: ${LISTEN_ADDR}|' "$config_file"
"metrics_listen_addr: .*" "metrics_listen_addr: \${METRICS_LISTEN_ADDR}" sed -i 's|^metrics_listen_addr:.*|metrics_listen_addr: ${METRICS_LISTEN_ADDR}|' "$config_file"
"grpc_listen_addr: .*" "grpc_listen_addr: \${GRPC_LISTEN_ADDR}" sed -i 's|^grpc_listen_addr:.*|grpc_listen_addr: ${GRPC_LISTEN_ADDR}|' "$config_file"
"grpc_allow_insecure: .*" "grpc_allow_insecure: \${GRPC_ALLOW_INSECURE}" sed -i 's|^grpc_allow_insecure:.*|grpc_allow_insecure: ${GRPC_ALLOW_INSECURE}|' "$config_file"
"private_key_path: .*" "private_key_path: \${NOISE_PRIVATE_KEY_PATH}" sed -i 's|^db_type:.*|db_type: ${DB_TYPE}|' "$config_file"
"acme_url: .*" "acme_url: \${ACME_URL}" sed -i 's|^db_path:.*|db_path: ${DB_PATH}|' "$config_file"
"acme_email: .*" "acme_email: \${ACME_EMAIL}" sed -i 's|^acme_url:.*|acme_url: ${ACME_URL}|' "$config_file"
"tls_letsencrypt_hostname: .*" "tls_letsencrypt_hostname: \${TLS_LETSENCRYPT_HOSTNAME}" sed -i 's|^acme_email:.*|acme_email: ${ACME_EMAIL}|' "$config_file"
"tls_letsencrypt_cache_dir: .*" "tls_letsencrypt_cache_dir: \${TLS_LETSENCRYPT_CACHE_DIR}" sed -i 's|^tls_letsencrypt_hostname:.*|tls_letsencrypt_hostname: ${TLS_LETSENCRYPT_HOSTNAME}|' "$config_file"
"db_type: .*" "db_type: \${DB_TYPE}" sed -i 's|^tls_letsencrypt_cache_dir:.*|tls_letsencrypt_cache_dir: ${TLS_LETSENCRYPT_CACHE_DIR}|' "$config_file"
"db_path: .*" "db_path: \${DB_PATH}" sed -i 's|^unix_socket:.*|unix_socket: ${UNIX_SOCKET}|' "$config_file"
) sed -i 's|^unix_socket_permission:.*|unix_socket_permission: ${UNIX_SOCKET_PERMISSION}|' "$config_file"
for i in "${!replacements[@]}"; do echo "Environment variable placeholders inserted in $config_file"
local search="${replacements[i]% *}"
local replace="${replacements[i]#* }"
sed -i "s|$search|$replace|" "$config_file"
done
echo "Placeholders inserted in $config_file"
} }
insert_placeholders
download_and_process_config download_and_process_config
insert_env_placeholders