|
2 | 2 | set -euo pipefail
|
3 | 3 | IFS=$'\n\t'
|
4 | 4 |
|
5 |
| -subscriptionId=$1 |
6 |
| -resourceGroupName=$2 |
7 |
| -nginxDeploymentName=$3 |
8 |
| -nginxConfigurationFile=$4 |
| 5 | +subscription_id=$1 |
| 6 | +resource_group_name=$2 |
| 7 | +nginx_deployment_name=$3 |
| 8 | +config_dir_path=$4 |
| 9 | +root_config_file=$5 |
| 10 | +transformed_config_dir_path=${6:-''} |
9 | 11 |
|
10 |
| -# Read and encode the NGINX configuration file content. |
11 |
| -if [ -f "$nginxConfigurationFile" ] |
| 12 | +# Validation and preprocessing |
| 13 | + |
| 14 | +if [[ "$config_dir_path" = /* ]] |
| 15 | +then |
| 16 | + echo "The NGINX configuration directory path in the repository '$config_dir_path' must be a relative path." |
| 17 | + exit 1 |
| 18 | +elif [[ ! "$config_dir_path" = */ ]] |
| 19 | +then |
| 20 | + echo "The NGINX configuration directory path '$config_dir_path' does not end with '/'. Appending a trailing '/'." |
| 21 | + config_dir_path="$config_dir_path/" |
| 22 | +fi |
| 23 | + |
| 24 | +if [[ -d "$config_dir_path" ]] |
| 25 | +then |
| 26 | + echo "The NGINX configuration directory '$config_dir_path' was found." |
| 27 | +else |
| 28 | + echo "The NGINX configuration directory '$config_dir_path' does not exist." |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +if [[ "$root_config_file" = /* ]] |
| 33 | +then |
| 34 | + echo "The NGINX configuration root file path '$root_config_file' must be a relative path to the NGINX configuration directory." |
| 35 | + exit 1 |
| 36 | +fi |
| 37 | + |
| 38 | +# Remove the leading './' from the root configuration file path if any. |
| 39 | +root_config_file=${root_config_file/#'./'/} |
| 40 | + |
| 41 | +root_config_file_repo_path="$config_dir_path$root_config_file" |
| 42 | +if [[ -f "$root_config_file_repo_path" ]] |
12 | 43 | then
|
13 |
| - echo "The NGINX configuration file was found." |
| 44 | + echo "The root NGINX configuration file '$root_config_file_repo_path' was found." |
14 | 45 | else
|
15 |
| - echo "The NGINX configuration file $nginxConfigurationFile does not exist." |
16 |
| - exit 2 |
| 46 | + echo "The root NGINX configuration file '$root_config_file_repo_path' does not exist." |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | + |
| 50 | +if [[ -n "$transformed_config_dir_path" ]] |
| 51 | +then |
| 52 | + if [[ ! "$transformed_config_dir_path" = /* ]] |
| 53 | + then |
| 54 | + echo "The specified transformed NGINX configuration directory path '$transformed_config_dir_path' must be an absolute path that starts with '/'." |
| 55 | + exit 1 |
| 56 | + elif [[ ! "$transformed_config_dir_path" = */ ]] |
| 57 | + then |
| 58 | + echo "The specified transformed NGINX configuration directory path '$transformed_config_dir_path' does not end with '/'. Appending a trailing '/'." |
| 59 | + transformed_config_dir_path="$transformed_config_dir_path/" |
| 60 | + fi |
17 | 61 | fi
|
18 | 62 |
|
19 |
| -encodedConfigContent=$(base64 "$nginxConfigurationFile") |
20 |
| -echo "Base64 encoded NGINX configuration content" |
21 |
| -echo "$encodedConfigContent" |
| 63 | +transformed_root_config_file_path="$transformed_config_dir_path$root_config_file" |
| 64 | +echo "The transformed root NGINX configuration file path is '$transformed_root_config_file_path'." |
| 65 | + |
| 66 | +# Create a NGINX configuration tarball. |
| 67 | + |
| 68 | +config_tarball="nginx-config.tar.gz" |
| 69 | + |
| 70 | +echo "Creating a tarball from the NGINX configuration directory." |
| 71 | +tar -cvzf "$config_tarball" -C "$config_dir_path" --xform s:'./':"$transformed_config_dir_path": . |
| 72 | +echo "Successfully created the tarball from the NGINX configuration directory." |
| 73 | + |
| 74 | +echo "Listing the NGINX configuration file paths in the tarball." |
| 75 | +tar -tf "$config_tarball" |
| 76 | + |
| 77 | +encoded_config_tarball=$(base64 "$config_tarball") |
| 78 | +echo "The base64 encoded NGINX configuration tarball" |
| 79 | +echo "$encoded_config_tarball" |
22 | 80 | echo ""
|
23 | 81 |
|
24 |
| -# Deploy the configuration to the NGINX instance on Azure using an ARM template. |
| 82 | +# Synchronize the NGINX configuration tarball to the NGINX for Azure deployment. |
| 83 | + |
25 | 84 | uuid="$(cat /proc/sys/kernel/random/uuid)"
|
26 |
| -templateFile="template-$uuid.json" |
27 |
| -templateDeploymentName="${nginxDeploymentName:0:20}-$uuid" |
| 85 | +template_file="template-$uuid.json" |
| 86 | +template_deployment_name="${nginx_deployment_name:0:20}-$uuid" |
28 | 87 |
|
29 |
| -wget -O "$templateFile" https://raw.githubusercontent.com/nginxinc/nginx-for-azure-deploy-action/main/src/nginx-for-azure-configuration-template.json |
30 |
| -echo "Downloaded the ARM template for deploying NGINX configuration" |
31 |
| -cat "$templateFile" |
| 88 | +wget -O "$template_file" https://raw.githubusercontent.com/nginxinc/nginx-for-azure-deploy-action/main/src/nginx-for-azure-configuration-template.json |
| 89 | +echo "Downloaded the ARM template for synchronizing NGINX configuration." |
| 90 | +cat "$template_file" |
32 | 91 | echo ""
|
33 | 92 |
|
34 |
| -echo "Deploying NGINX configuration" |
35 |
| -echo "Subscription: $subscriptionId" |
36 |
| -echo "Resource group: $resourceGroupName" |
37 |
| -echo "NGINX deployment name: $nginxDeploymentName" |
38 |
| -echo "Template deployment name: $templateDeploymentName" |
| 93 | +echo "Synchronizing NGINX configuration" |
| 94 | +echo "Subscription ID: $subscription_id" |
| 95 | +echo "Resource group name: $resource_group_name" |
| 96 | +echo "NGINX for Azure deployment name: $nginx_deployment_name" |
| 97 | +echo "ARM template deployment name: $template_deployment_name" |
39 | 98 | echo ""
|
40 | 99 |
|
41 |
| -az account set -s "$subscriptionId" --verbose |
42 |
| -az deployment group create --name "$templateDeploymentName" --resource-group "$resourceGroupName" --template-file "$templateFile" --parameters nginxDeploymentName="$nginxDeploymentName" rootConfigContent="$encodedConfigContent" --verbose |
| 100 | +az account set -s "$subscription_id" --verbose |
| 101 | +az deployment group create --name "$template_deployment_name" --resource-group "$resource_group_name" --template-file "$template_file" --parameters nginxDeploymentName="$nginx_deployment_name" rootFile="$transformed_root_config_file_path" tarball="$encoded_config_tarball" --verbose |
0 commit comments