Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·100 lines (84 loc) · 3.62 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·100 lines (84 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
# Variables
PREFIX="${PREFIX:-local}" #system or user
SUFFIX="${SUFFIX:-test}"
LOCATION='westeurope'
CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)"
ZIPFILE="function_app.zip"
DEPLOY_RETRY_COUNT=3
DEPLOY_RETRY_SLEEP=30
# Change the current directory to the script's directory
cd "$CURRENT_DIR" || exit
# Dual-target provider configuration. providers.tf hardcodes nothing: the azurerm provider reads its endpoints and the
# target subscription from the ARM_* environment variables exported here.
# - On both targets: the subscription and tenant of the active az account.
# - Only when the active az cloud is LocalStack: the emulator's metadata endpoint, taken from the az cloud registration
# that `lstk az start-interception` created, so nothing is hardcoded here either.
ACTIVE_CLOUD=$(az cloud show --query name --output tsv --only-show-errors | tr -d '\r')
ARM_SUBSCRIPTION_ID=$(az account show --query id --output tsv --only-show-errors | tr -d '\r')
ARM_TENANT_ID=$(az account show --query tenantId --output tsv --only-show-errors | tr -d '\r')
export ARM_SUBSCRIPTION_ID ARM_TENANT_ID
if [[ $ACTIVE_CLOUD == "LocalStack" ]]; then
RESOURCE_MANAGER_ENDPOINT=$(az cloud show --query endpoints.resourceManager --output tsv --only-show-errors | tr -d '\r')
METADATA_HOSTNAME="${RESOURCE_MANAGER_ENDPOINT#https://}"
export ARM_METADATA_HOSTNAME="${METADATA_HOSTNAME%/}"
echo "Active cloud is [$ACTIVE_CLOUD]: Terraform targets the emulator at [$RESOURCE_MANAGER_ENDPOINT]"
else
# Cleared rather than merely not set: a value inherited from an earlier emulator deployment in the same shell would
# keep the provider doing metadata discovery against the emulator while the CLI is on Azure.
unset ARM_METADATA_HOSTNAME
echo "Active cloud is [$ACTIVE_CLOUD]: Terraform targets Azure subscription [$ARM_SUBSCRIPTION_ID]"
fi
echo "Initializing Terraform..."
terraform init -upgrade
# Run terraform plan and check for errors
echo "Planning Terraform deployment..."
terraform plan -out=tfplan \
-var "prefix=$PREFIX" \
-var "suffix=$SUFFIX" \
-var "location=$LOCATION"
# Apply the Terraform configuration
echo "Applying Terraform configuration..."
terraform apply -auto-approve tfplan
if [[ $? != 0 ]]; then
echo "Terraform apply failed. Exiting."
exit 1
fi
# Get the output values
RESOURCE_GROUP_NAME=$(terraform output -raw resource_group_name)
FUNCTION_APP_NAME=$(terraform output -raw function_app_name)
# Print the variables
echo "Resource Group: $RESOURCE_GROUP_NAME"
echo "Function App: $FUNCTION_APP_NAME"
# CD into the function app directory
cd ../src || exit
# Create the zip package of the function app: the remote build installs requirements.txt
rm -f $ZIPFILE
zip -r $ZIPFILE function_app.py host.json requirements.txt
# Deploy the function app using the zip file
echo "Deploying function app [$FUNCTION_APP_NAME]..."
# On Azure the first deployment to a new function app can time out (504) while its deployment site starts, so a failed
# attempt is retried
DEPLOYED=0
for ((ATTEMPT = 1; ATTEMPT <= DEPLOY_RETRY_COUNT; ATTEMPT++)); do
az functionapp deploy \
--resource-group "$RESOURCE_GROUP_NAME" \
--name "$FUNCTION_APP_NAME" \
--src-path "$ZIPFILE" \
--type zip \
--async true 1>/dev/null
if [[ $? == 0 ]]; then
DEPLOYED=1
break
fi
if [[ $ATTEMPT -lt $DEPLOY_RETRY_COUNT ]]; then
echo "Attempt $ATTEMPT of $DEPLOY_RETRY_COUNT to deploy function app [$FUNCTION_APP_NAME] failed. Waiting [$DEPLOY_RETRY_SLEEP] seconds before retry..."
sleep $DEPLOY_RETRY_SLEEP
fi
done
if [[ $DEPLOYED == 1 ]]; then
echo "Function app [$FUNCTION_APP_NAME] deployed successfully."
else
echo "Failed to deploy function app [$FUNCTION_APP_NAME]."
exit 1
fi