33# Usage: ./infra.sh <command> <project_name> [environment_name] [location]
44# Manages the Azure infrastructure for this project.
55# #############################################################################
6- # v0.9.4 | dependencies: Azure CLI, jq, perl
6+ # v0.9.5 | dependencies: Azure CLI, jq, perl
77# #############################################################################
88
9- set -e
10- cd $( dirname ${BASH_SOURCE[0]} )
9+ set -euo pipefail
10+ cd " $( dirname " ${BASH_SOURCE[0]} " ) "
1111if [[ -f " .settings" ]]; then
1212 source .settings
1313fi
1414
15- time=` date +%s`
16- subcommand=" ${1} "
15+ time=$( date +%s)
16+ subcommand=" ${1:- } "
1717project_name=" ${2:- $project_name } "
1818environment=" ${environment:- prod} "
1919environment=" ${3:- $environment } "
@@ -35,7 +35,7 @@ showUsage() {
3535}
3636
3737toLowerSnakeCase () {
38- echo ${1} |
38+ echo " ${1} " |
3939 perl -pe ' s/([a-z\d])([A-Z]+)/$1_$2/g' |
4040 perl -pe ' s/[ _-]+/_/g' |
4141 perl -ne ' print lc'
@@ -44,20 +44,20 @@ toLowerSnakeCase() {
4444createSettings () {
4545 env_file=" .${environment} .env"
4646
47- echo " # Generated settings for environment '${environment} '" > ${env_file}
48- echo " # Do not edit this file manually!" >> ${env_file}
49- echo >> ${env_file}
50- echo $1 | jq -c ' . | to_entries[] | [.key, .value.value, .value.type]' |
47+ echo " # Generated settings for environment '${environment} '" > " ${env_file} "
48+ echo " # Do not edit this file manually!" >> " ${env_file} "
49+ echo >> " ${env_file} "
50+ echo " $1 " | jq -c ' . | to_entries[] | [.key, .value.value, .value.type]' |
5151
5252 # For each output, export the value to the env file and convert the key to
5353 # lower snake case.
54- while IFS=$" \n " read -r output; do
55- ouput_name=$( toLowerSnakeCase $( echo " $output " | jq -r ' .[0]' ) )
54+ while IFS=$' \n ' read -r output; do
55+ ouput_name=$( toLowerSnakeCase " $( echo " $output " | jq -r ' .[0]' ) " )
5656 output_value=$( echo " $output " | jq -r ' .[1] | @sh' )
57- if [ $( echo " $output " | jq -r ' .[2]' ) == " Array" ]; then
58- echo " ${ouput_name} =(${output_value} )" >> ${env_file}
57+ if [ " $( echo " $output " | jq -r ' .[2]' ) " == " Array" ]; then
58+ echo " ${ouput_name} =(${output_value} )" >> " ${env_file} "
5959 else
60- echo " ${ouput_name} =${output_value} " >> ${env_file}
60+ echo " ${ouput_name} =${output_value} " >> " ${env_file} "
6161 fi
6262 done
6363 echo " Settings for environment '${environment} ' saved to '${env_file} '."
@@ -66,19 +66,19 @@ createSettings() {
6666updateInfrastructure () {
6767 echo " Preparing environment '${environment} ' of project '${project_name} '..."
6868 az group create \
69- --name ${resource_group_name} \
70- --location ${location} \
71- --tags project=${project_name} environment=${environment} managedBy=blue \
69+ --name " ${resource_group_name} " \
70+ --location " ${location} " \
71+ --tags project=" ${project_name} " environment=" ${environment} " managedBy=blue \
7272 --output none
7373 echo " Resource group '${resource_group_name} ' ready."
7474 outputs=$( \
7575 az deployment group create \
76- --resource-group ${resource_group_name} \
76+ --resource-group " ${resource_group_name} " \
7777 --template-file infra/main.bicep \
7878 --name " deployment-${project_name} -${environment} -${location} " \
79- --parameters projectName=${project_name} \
80- environment=${environment} \
81- location=${location} \
79+ --parameters projectName=" ${project_name} " \
80+ environment=" ${environment} " \
81+ location=" ${location} " \
8282 --query properties.outputs \
8383 --mode Complete \
8484 --verbose
@@ -98,17 +98,17 @@ deleteInfrastructure() {
9898cancelInfrastructureDeployment () {
9999 echo " Cancelling preparation of environment '${environment} ' of project '${project_name} '..."
100100 az deployment group cancel \
101- --resource-group ${resource_group_name} \
102- --name " deployment-${project_name} -${environment} -${location} "
101+ --resource-group " ${resource_group_name} " \
102+ --name " deployment-${project_name} -${environment} -${location} " \
103103 --verbose
104104 echo " Preparation of '${environment} ' of project '${project_name} ' cancelled."
105105}
106106
107107retrieveEnvironmentSettings () {
108- echo " Retrieving settings for environment '${environment} ' of project '${project_name} '..."
108+ echo " Retrieving settings for environment '${environment} ' of project '${project_name} '..."
109109 outputs=$( \
110110 az deployment group show \
111- --resource-group ${resource_group_name} \
111+ --resource-group " ${resource_group_name} " \
112112 --name " deployment-${project_name} -${environment} -${location} " \
113113 --query properties.outputs \
114114 )
@@ -122,71 +122,71 @@ retrieveSecrets() {
122122 echo " Retrieving secrets for environment '${environment} ' of project '${project_name} '..."
123123
124124 env_file=" .${environment} .env"
125- echo -e " \n${secrets_sep} \n" >> ${env_file}
125+ echo -e " \n${secrets_sep} \n" >> " ${env_file} "
126126
127127 # Get registry credentials
128- if [[ -n " $registry_name " ]]; then
128+ if [[ -n " ${ registry_name:- } " ]]; then
129129 registry_username=$( \
130130 az acr credential show \
131- --name ${registry_name} \
131+ --name " ${registry_name} " \
132132 --query " username" \
133133 --output tsv \
134134 )
135- echo " registry_username='${registry_username} '" >> ${env_file}
135+ echo " registry_username='${registry_username} '" >> " ${env_file} "
136136
137137 registry_password=$( \
138138 az acr credential show \
139- --name ${registry_name} \
139+ --name " ${registry_name} " \
140140 --query " passwords[0].value" \
141141 --output tsv \
142142 )
143- echo " registry_password='${registry_password} '" >> ${env_file}
143+ echo " registry_password='${registry_password} '" >> " ${env_file} "
144144 fi
145145
146146 # Get storage account connection string
147- if [[ -n " $storage_account_name " ]]; then
147+ if [[ -n " ${ storage_account_name:- } " ]]; then
148148 storage_account_connection_string=$( \
149149 az storage account show-connection-string \
150- --name ${storage_account_name} \
150+ --name " ${storage_account_name} " \
151151 --query " connectionString" \
152152 --output tsv \
153153 )
154- echo " storage_account_connection_string='${storage_account_connection_string} '" >> ${env_file}
154+ echo " storage_account_connection_string='${storage_account_connection_string} '" >> " ${env_file} "
155155 fi
156156
157157 # Get app insights instrumentation key and connection string
158- if [[ -n " $app_insights_name " ]]; then
158+ if [[ -n " ${ app_insights_name:- } " ]]; then
159159 app_insights_instrumentation_key=$( \
160160 az resource show \
161- --resource-group ${resource_group_name} \
161+ --resource-group " ${resource_group_name} " \
162162 --resource-type " Microsoft.Insights/components" \
163- --name ${app_insights_name} \
163+ --name " ${app_insights_name} " \
164164 --query properties.InstrumentationKey \
165165 --output tsv \
166166 )
167- echo " app_insights_instrumentation_key='${app_insights_instrumentation_key} '" >> ${env_file}
167+ echo " app_insights_instrumentation_key='${app_insights_instrumentation_key} '" >> " ${env_file} "
168168
169169 app_insights_connection_string=$( \
170170 az resource show \
171- --resource-group ${resource_group_name} \
171+ --resource-group " ${resource_group_name} " \
172172 --resource-type " Microsoft.Insights/components" \
173- --name ${app_insights_name} \
173+ --name " ${app_insights_name} " \
174174 --query properties.ConnectionString \
175175 --output tsv \
176176 )
177- echo " app_insights_connection_string='${app_insights_connection_string} '" >> ${env_file}
177+ echo " app_insights_connection_string='${app_insights_connection_string} '" >> " ${env_file} "
178178 fi
179179
180180 # Get cosmos db connection strings
181- if [[ -n " $database_name " ]]; then
181+ if [[ -n " ${ database_name:- } " ]]; then
182182 database_connection_string=$( \
183183 az cosmosdb keys list --type connection-strings \
184- --name ${database_name} \
185- --resource-group ${resource_group_name} \
184+ --name " ${database_name} " \
185+ --resource-group " ${resource_group_name} " \
186186 --query " connectionStrings[0].connectionString" \
187187 --output tsv \
188188 )
189- echo " database_connection_string='${database_connection_string} '" >> ${env_file}
189+ echo " database_connection_string='${database_connection_string} '" >> " ${env_file} "
190190 fi
191191
192192 # TODO: retrieve other secrets (swa tokens, etc.)
@@ -220,4 +220,4 @@ case "$subcommand" in
220220 exit 1
221221 ;;
222222esac
223- echo " Done in $(( $(date +% s)- $ time)) s"
223+ echo " Done in $(( $(date +% s) - time)) s"
0 commit comments