Skip to content

Commit 97296e1

Browse files
committed
use tarball with protected files
1 parent 5b90df0 commit 97296e1

File tree

1 file changed

+47
-167
lines changed

1 file changed

+47
-167
lines changed

src/deploy-config.sh

Lines changed: 47 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -124,175 +124,26 @@ fi
124124
transformed_root_config_file_path="$transformed_config_dir_path$root_config_file"
125125
echo "The transformed root NGINX configuration file path is '$transformed_root_config_file_path'."
126126

127-
# Common utility functions
127+
# Create a NGINX configuration tarball.
128128

129-
# Function to trim whitespace from a string
130-
trim_whitespace() {
131-
local var="$1"
132-
# Trim leading whitespace from the file path (var)
133-
# ${var%%[![:space:]]*} starts at the file path's end
134-
# and finds the longest match of non-whitespace
135-
# characters leaving only leading whitespaces
136-
# ${var#"..." } removes the leading whitespace found
137-
var="${var#"${var%%[![:space:]]*}"}"
138-
# Remove trailing whitespace
139-
# See explanation above. The process is reversed here.
140-
var="${var%"${var##*[![:space:]]}"}"
141-
# Check if the file exists in the repository
142-
echo "$var"
143-
}
144-
145-
# Function to encode file content to base64
146-
encode_file_base64() {
147-
local file_path="$1"
148-
# Use base64 to encode the file content
149-
# -w 0 option is used to avoid line wrapping in the output
150-
base64 -w 0 "$file_path"
151-
}
152-
153-
# Function to build virtual path from relative path
154-
build_virtual_path() {
155-
local relative_path="$1"
156-
echo "${transformed_config_dir_path}${relative_path}"
157-
}
129+
config_tarball="nginx-config.tar.gz"
158130

159-
# Function to add a file entry to a JSON array
160-
# The add_file_to_json_array function uses indirect variable references
161-
# and global assignment to update JSON arrays and flags that track
162-
# which files have been processed. The variable names for the JSON array
163-
# and the "first file" flag are passed as arguments, allowing the
164-
# function to generically update different arrays
165-
# (for regular and protected files) without hardcoding their names.
166-
# The syntax ${!var} retrieves the value of the variable whose
167-
# name is stored in 'var', and declare -g ensures the updated
168-
# values are set globally, so changes persist outside the function.
169-
add_file_to_json_array() {
170-
local file_path="$1"
171-
local virtual_path="$2"
172-
local file_type="$3" # "regular" or "protected"
173-
local json_var_name="$4" # Variable name to modify
174-
local first_file_var_name="$5" # Variable name for first_file flag
175-
176-
if [ -f "$file_path" ]; then
177-
echo "Processing $file_type file: $file_path -> $virtual_path"
178-
179-
# Base64 encode the file content
180-
local file_content_b64
181-
file_content_b64=$(encode_file_base64 "$file_path")
182-
183-
# Get current values using indirect variable references
184-
local current_json="${!json_var_name}"
185-
local is_first_file="${!first_file_var_name}"
186-
187-
# Add comma separator if not the first file
188-
if [ "$is_first_file" = false ]; then
189-
current_json+=","
190-
fi
191-
192-
# Add the file entry to JSON array
193-
current_json+="{\"content\":\"$file_content_b64\",\"virtual-path\":\"$virtual_path\"}"
194-
195-
# Update the variables using indirect assignment
196-
declare -g "$json_var_name=$current_json"
197-
declare -g "$first_file_var_name=false"
198-
199-
if [[ "$debug" == true ]]; then
200-
echo "$file_type file virtual path: $virtual_path"
201-
echo "$file_type file content (base64): ${file_content_b64:0:50}..."
202-
fi
203-
else
204-
echo "Warning: $file_type file '$file_path' not found"
205-
fi
206-
}
131+
echo "Creating a tarball from the NGINX configuration directory."
132+
tar -cvzf "$config_tarball" -C "$config_dir_path" --xform s:'./':"$transformed_config_dir_path": .
133+
echo "Successfully created the tarball from the NGINX configuration directory."
207134

208-
# Process protected files first to build exclusion list
209-
protected_files_list=()
210-
if [ -n "$protected_files" ]; then
211-
IFS=',' read -ra files <<< "$protected_files"
212-
213-
for file in "${files[@]}"; do
214-
file=$(trim_whitespace "$file")
215-
if [ -n "$file" ]; then
216-
protected_files_list+=("$file")
217-
fi
218-
done
219-
fi
220-
221-
# Function to check if a file is in the protected files list
222-
is_protected_file() {
223-
local relative_path="$1"
224-
for protected_file in "${protected_files_list[@]}"; do
225-
if [ "$relative_path" = "$protected_file" ]; then
226-
return 0
227-
fi
228-
done
229-
return 1
230-
}
231-
232-
# Process all configuration files individually (excluding protected files)
233-
234-
echo "Processing NGINX configuration files individually."
235-
236-
# Build the files JSON array
237-
files_json="["
238-
# shellcheck disable=SC2034 # Variable is used via indirect reference in add_file_to_json_array
239-
files_first_file=true
240-
241-
# Find all files in the config directory and process them (excluding protected files)
242-
while IFS= read -r -d '' file; do
243-
# Get relative path from config directory
244-
relative_path="${file#"$config_dir_path"}"
245-
246-
# Skip if this file is in the protected files list
247-
if is_protected_file "$relative_path"; then
248-
echo "Skipping protected file from regular files: $relative_path"
249-
continue
250-
fi
251-
252-
# Apply transformation to get virtual path
253-
virtual_path=$(build_virtual_path "$relative_path")
254-
255-
add_file_to_json_array "$file" "$virtual_path" "regular" "files_json" "files_first_file"
256-
done < <(find "$config_dir_path" -type f -print0)
135+
echo "Listing the NGINX configuration file paths in the tarball."
136+
tar -tf "$config_tarball"
257137

258-
files_json+="]"
138+
encoded_config_tarball=$(base64 "$config_tarball" -w 0)
259139

260140
if [[ "$debug" == true ]]; then
261-
echo "Regular files JSON: $files_json"
141+
echo "The base64 encoded NGINX configuration tarball"
142+
echo "$encoded_config_tarball"
262143
fi
144+
echo ""
263145

264-
# Process protected files if specified
265-
protected_files_arg=""
266-
if [ -n "$protected_files" ]; then
267-
echo "Processing protected files: $protected_files"
268-
269-
# Build the protected files JSON array
270-
protected_files_json="["
271-
protected_first_file=true
272-
IFS=',' read -ra files <<< "$protected_files"
273-
274-
for file in "${files[@]}"; do
275-
file=$(trim_whitespace "$file")
276-
if [ -n "$file" ]; then
277-
repo_file_path="${config_dir_path}${file}"
278-
virtual_path=$(build_virtual_path "$file")
279-
280-
add_file_to_json_array "$repo_file_path" "$virtual_path" "protected" "protected_files_json" "protected_first_file"
281-
fi
282-
done
283-
284-
protected_files_json+="]"
285-
286-
if [ "$protected_first_file" = false ]; then
287-
protected_files_arg="--protected-files"
288-
if [[ "$debug" == true ]]; then
289-
echo "Protected files JSON: $protected_files_json"
290-
fi
291-
fi
292-
fi
293-
294-
295-
# Synchronize the NGINX configuration files to the NGINXaaS for Azure deployment.
146+
# Synchronize the NGINX configuration tarball to the NGINXaaS for Azure deployment.
296147

297148
echo "Synchronizing NGINX configuration"
298149
echo "Subscription ID: $subscription_id"
@@ -311,18 +162,47 @@ az_cmd=(
311162
"deployment"
312163
"configuration"
313164
"update"
165+
"--verbose"
314166
"--name" "default"
315167
"--deployment-name" "$nginx_deployment_name"
316168
"--resource-group" "$resource_group_name"
317169
"--root-file" "$transformed_root_config_file_path"
318-
"--files" "$files_json"
319-
"--verbose"
170+
"--package" "data=$encoded_config_tarball"
320171
)
321172

322-
# Add protected files argument if present
323-
if [ -n "$protected_files_arg" ]; then
324-
az_cmd+=("$protected_files_arg")
325-
az_cmd+=("$protected_files_json")
173+
# Function to trim whitespace from a string
174+
trim_whitespace() {
175+
local var="$1"
176+
# Trim leading whitespace from the file path (var)
177+
# ${var%%[![:space:]]*} starts at the file path's end
178+
# and finds the longest match of non-whitespace
179+
# characters leaving only leading whitespaces
180+
# ${var#"..." } removes the leading whitespace found
181+
var="${var#"${var%%[![:space:]]*}"}"
182+
# Remove trailing whitespace
183+
# See explanation above. The process is reversed here.
184+
var="${var%"${var##*[![:space:]]}"}"
185+
# Check if the file exists in the repository
186+
echo "$var"
187+
}
188+
189+
# Add protected-files parameter if provided
190+
if [[ -n "$protected_files" ]]; then
191+
# Convert comma-separated list to JSON array format
192+
IFS=',' read -ra files <<< "$protected_files"
193+
json_array="["
194+
for i in "${files[@]}"; do
195+
# Trim whitespace and add quotes
196+
file_path="$(trim_whitespace "$i")"
197+
if [[ "$json_array" != "[" ]]; then
198+
json_array+=","
199+
fi
200+
json_array+="\"$file_path\""
201+
done
202+
json_array+="]"
203+
204+
az_cmd+=("protected-files=\'$json_array\'")
205+
echo "Protected files: $json_array"
326206
fi
327207

328208
if [[ "$debug" == true ]]; then

0 commit comments

Comments
 (0)