|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | +IFS=$'\n\t' |
| 4 | + |
| 5 | +for i in "$@" |
| 6 | +do |
| 7 | +case $i in |
| 8 | + --subscription_id=*) |
| 9 | + subscription_id="${i#*=}" |
| 10 | + shift |
| 11 | + ;; |
| 12 | + --resource_group_name=*) |
| 13 | + resource_group_name="${i#*=}" |
| 14 | + shift |
| 15 | + ;; |
| 16 | + --nginx_deployment_name=*) |
| 17 | + nginx_deployment_name="${i#*=}" |
| 18 | + shift |
| 19 | + ;; |
| 20 | + --nginx_resource_location=*) |
| 21 | + nginx_resource_location="${i#*=}" |
| 22 | + shift |
| 23 | + ;; |
| 24 | + --certificates=*) |
| 25 | + certificates="${i#*=}" |
| 26 | + shift |
| 27 | + ;; |
| 28 | + *) |
| 29 | + echo "Not matched option '${i#*=}' passed in." |
| 30 | + exit 1 |
| 31 | + ;; |
| 32 | +esac |
| 33 | +done |
| 34 | + |
| 35 | +if [[ ! -v subscription_id ]]; |
| 36 | +then |
| 37 | + echo "Please set 'subscription-id' ..." |
| 38 | + exit 1 |
| 39 | +fi |
| 40 | +if [[ ! -v resource_group_name ]]; |
| 41 | +then |
| 42 | + echo "Please set 'resource-group-name' ..." |
| 43 | + exit 1 |
| 44 | +fi |
| 45 | +if [[ ! -v nginx_deployment_name ]]; |
| 46 | +then |
| 47 | + echo "Please set 'nginx-deployment-name' ..." |
| 48 | + exit 1 |
| 49 | +fi |
| 50 | +if [[ ! -v nginx_resource_location ]]; |
| 51 | +then |
| 52 | + echo "Please set 'nginx-resource-location' ..." |
| 53 | + exit 1 |
| 54 | +fi |
| 55 | +if [[ ! -v certificates ]]; |
| 56 | +then |
| 57 | + echo "Please set 'nginx-certificate-details' ..." |
| 58 | + exit 1 |
| 59 | +fi |
| 60 | + |
| 61 | +arm_template_file="nginx-for-azure-certificate-template.json" |
| 62 | + |
| 63 | +#get the ARM template file |
| 64 | +wget -O "$arm_template_file" https://nginxgithubactions.blob.core.windows.net/armtemplates/nginx-for-azure-certificate-template.json |
| 65 | +echo "Downloaded the ARM template for synchronizing NGINX certificate." |
| 66 | + |
| 67 | +cat "$arm_template_file" |
| 68 | +echo "" |
| 69 | + |
| 70 | +az account set -s "$subscription_id" --verbose |
| 71 | + |
| 72 | +count=$(echo $certificates | jq '. | length') |
| 73 | +for (( i=0; i<count; i++ )); |
| 74 | +do |
| 75 | + nginx_cert_name=$(echo $certificates | jq -r '.['"$i"'].certificateName') |
| 76 | + nginx_cert_file=$(echo $certificates | jq -r '.['"$i"'].certificateVirtualPath') |
| 77 | + nginx_key_file=$(echo $certificates | jq -r '.['"$i"'].keyVirtualPath') |
| 78 | + keyvault_secret=$(echo $certificates | jq -r '.['"$i"'].keyvaultSecret') |
| 79 | + |
| 80 | + do_nginx_arm_deployment=1 |
| 81 | + err_msg=" " |
| 82 | + if [ -z "$nginx_cert_name" ] || [ "$nginx_cert_name" = "null" ] |
| 83 | + then |
| 84 | + err_msg+="nginx_cert_name is empty;" |
| 85 | + do_nginx_arm_deployment=0 |
| 86 | + fi |
| 87 | + if [ -z "$nginx_cert_file" ] || [ "$nginx_cert_file" = "null" ] |
| 88 | + then |
| 89 | + err_msg+="nginx_cert_file is empty;" |
| 90 | + do_nginx_arm_deployment=0 |
| 91 | + fi |
| 92 | + if [ -z "$nginx_key_file" ] || [ "$nginx_key_file" = "null" ] |
| 93 | + then |
| 94 | + err_msg+="nginx_key_file is empty;" |
| 95 | + do_nginx_arm_deployment=0 |
| 96 | + fi |
| 97 | + if [ -z "$keyvault_secret" ] || [ "$keyvault_secret" = "null" ] |
| 98 | + then |
| 99 | + err_msg+="keyvault_secret is empty;" |
| 100 | + do_nginx_arm_deployment=0 |
| 101 | + fi |
| 102 | + |
| 103 | + uuid="$(cat /proc/sys/kernel/random/uuid)" |
| 104 | + template_file="template-$uuid.json" |
| 105 | + template_deployment_name="${nginx_deployment_name:0:20}-$uuid" |
| 106 | + |
| 107 | + cp "$arm_template_file" "$template_file" |
| 108 | + |
| 109 | + echo "Synchronizing NGINX certificate" |
| 110 | + echo "Subscription ID: $subscription_id" |
| 111 | + echo "Resource group name: $resource_group_name" |
| 112 | + echo "NGINX for Azure deployment name: $nginx_deployment_name" |
| 113 | + echo "NGINX for Azure Location: $nginx_resource_location" |
| 114 | + echo "ARM template deployment name: $template_deployment_name" |
| 115 | + echo "" |
| 116 | + echo "NGINX for Azure cert name: $nginx_cert_name" |
| 117 | + echo "NGINX for Azure cert file location: $nginx_cert_file" |
| 118 | + echo "NGINX for Azure key file location: $nginx_key_file" |
| 119 | + echo "" |
| 120 | + |
| 121 | + if [ $do_nginx_arm_deployment -eq 1 ] |
| 122 | + then |
| 123 | + set +e |
| 124 | + az deployment group create --name "$template_deployment_name" --resource-group "$resource_group_name" --template-file "$template_file" --parameters name="$nginx_cert_name" location="$nginx_resource_location" nginxDeploymentName="$nginx_deployment_name" certificateVirtualPath="$nginx_cert_file" keyVirtualPath="$nginx_key_file" keyVaultSecretID="$keyvault_secret" --verbose |
| 125 | + set -e |
| 126 | + else |
| 127 | + echo "Skipping JSON object $i cert deployment with error:$err_msg" |
| 128 | + echo "" |
| 129 | + fi |
| 130 | +done |
0 commit comments