|
1 | 1 | #!/bin/bash
|
2 | 2 |
|
3 |
| -# Load values from infra_config.env |
4 |
| -CONFIG_FILE="$(dirname "$0")/infra_config.env" |
5 |
| -if [ -f "$CONFIG_FILE" ]; then |
6 |
| - echo "Loading configuration from $CONFIG_FILE..." |
7 |
| - source "$CONFIG_FILE" |
8 |
| -else |
9 |
| - echo "Configuration file $CONFIG_FILE not found!" |
10 |
| - exit 1 |
11 |
| -fi |
12 |
| - |
13 |
| -echo -e "\nSetting subscription context to $SUBSCRIPTION_ID..." |
14 |
| -az account set --subscription "$SUBSCRIPTION_ID" |
15 |
| -if [ $? -ne 0 ]; then |
16 |
| - echo "Failed to set subscription context." |
17 |
| - exit 1 |
18 |
| -fi |
19 |
| -echo "Subscription context set successfully." |
20 |
| - |
21 |
| -# Create the resource group |
22 |
| -echo -e "\nCreating resource group: $RESOURCE_GROUP_NAME in location $LOCATION..." |
23 |
| -az group create --name "$RESOURCE_GROUP_NAME" --location "$LOCATION" |
24 |
| -if [ $? -ne 0 ]; then |
25 |
| - echo "Failed to create resource group: $RESOURCE_GROUP_NAME." |
26 |
| - exit 1 |
27 |
| -fi |
28 |
| -echo "Resource group $RESOURCE_GROUP_NAME created successfully." |
29 |
| - |
30 |
| -# Create the Storage Account |
31 |
| -echo -e "\nCreating Storage Account: $STORAGE_ACCOUNT_NAME..." |
32 |
| -az storage account create --name "$STORAGE_ACCOUNT_NAME" --resource-group "$RESOURCE_GROUP_NAME" --location "$LOCATION" --sku Standard_LRS |
33 |
| -if [ $? -ne 0 ]; then |
34 |
| - echo "Failed to create Storage Account: $STORAGE_ACCOUNT_NAME." |
35 |
| - exit 1 |
36 |
| -fi |
37 |
| -echo "Storage Account $STORAGE_ACCOUNT_NAME created successfully." |
38 |
| - |
39 |
| -# Retrieve the Storage Account connection string |
40 |
| -echo -e "\nRetrieving connection string for Storage Account: $STORAGE_ACCOUNT_NAME..." |
41 |
| -STORAGE_CONNECTION_STRING=$(az storage account show-connection-string --name "$STORAGE_ACCOUNT_NAME" --resource-group "$RESOURCE_GROUP_NAME" --query "connectionString" -o tsv) |
42 |
| -if [ $? -ne 0 ]; then |
43 |
| - echo "Failed to retrieve connection string for Storage Account: $STORAGE_ACCOUNT_NAME." |
44 |
| - exit 1 |
45 |
| -fi |
46 |
| - |
47 |
| -# Create the Blob Container |
48 |
| -echo -e "\nCreating Blob Container: $BLOB_CONTAINER_NAME..." |
49 |
| -az storage container create --name "$BLOB_CONTAINER_NAME" --connection-string "$STORAGE_CONNECTION_STRING" |
50 |
| -if [ $? -ne 0 ]; then |
51 |
| - echo "Failed to create Blob Container: $BLOB_CONTAINER_NAME." |
52 |
| - exit 1 |
53 |
| -fi |
54 |
| -echo "Blob Container $BLOB_CONTAINER_NAME created successfully." |
55 |
| - |
56 |
| - |
57 |
| -# Create an App Service Plan |
58 |
| -echo -e "\nCreating App Service Plan: $SERVICE_PLAN_NAME..." |
59 |
| -az appservice plan create \ |
60 |
| - --name "$SERVICE_PLAN_NAME" \ |
61 |
| - --resource-group "$RESOURCE_GROUP_NAME" \ |
62 |
| - --location "$LOCATION" \ |
63 |
| - --sku S1 \ |
64 |
| - --is-linux |
65 |
| -if [ $? -ne 0 ]; then |
66 |
| - echo "Failed to create App Service Plan: $SERVICE_PLAN_NAME." |
67 |
| - exit 1 |
68 |
| -fi |
69 |
| -echo "App Service Plan $SERVICE_PLAN_NAME created successfully." |
| 3 | +# Define colors for console output |
| 4 | +GREEN="\e[32m" |
| 5 | +RED="\e[31m" |
| 6 | +BLUE="\e[34m" |
| 7 | +CYAN="\e[36m" |
| 8 | +YELLOW="\e[33m" |
| 9 | +RESET="\e[0m" |
| 10 | +# echo -e "${GREEN}This text is green.${RESET}" |
70 | 11 |
|
| 12 | +# Variables |
| 13 | +RESOURCE_GROUP="hvalfangstresourcegroup" |
| 14 | +LOCATION="westeurope" |
| 15 | +BICEP_FILE="infra/main.bicep" |
71 | 16 |
|
72 |
| -# Enable dynamic installation of extensions without prompting the user. |
73 |
| -echo -e "\nConfiguring Azure CLI to allow dynamic installation of extensions..." |
74 |
| -az config set extension.use_dynamic_install=yes_without_prompt |
75 |
| -if [ $? -ne 0 ]; then |
76 |
| - echo "Failed to configure Azure CLI for dynamic extension installation." |
77 |
| - exit 1 |
78 |
| -fi |
79 |
| -echo "Azure CLI configured successfully for dynamic extension installation." |
80 |
| - |
81 |
| -# Ensure the Application Insights extension is installed. |
82 |
| -echo -e "\nInstalling Application Insights extension if not already installed..." |
83 |
| -az extension add --name application-insights 2>/dev/null |
84 |
| -if [ $? -ne 0 ]; then |
85 |
| - echo "Failed to add the Application Insights extension. It might already be installed." |
86 |
| -else |
87 |
| - echo "Application Insights extension installed successfully." |
88 |
| -fi |
| 17 | +# Function to handle errors |
| 18 | +handle_error() { |
| 19 | + echo -e "${RED}Error occurred in script at line: ${BASH_LINENO[0]}. Exiting...${RESET}" |
| 20 | + exit 1 |
| 21 | +} |
89 | 22 |
|
90 |
| -# Verify that the Application Insights extension is installed. |
91 |
| -echo -e "\nVerifying Application Insights extension installation..." |
92 |
| -az extension show --name application-insights >/dev/null 2>&1 |
93 |
| -if [ $? -ne 0 ]; then |
94 |
| - echo "Application Insights extension is not installed or failed to load." |
95 |
| - exit 1 |
96 |
| -fi |
97 |
| -echo "Application Insights extension is verified as installed." |
| 23 | +# Set trap to catch errors and execute handle_error |
| 24 | +trap 'handle_error' ERR |
98 | 25 |
|
99 |
| -# Create an Application Insights resource |
100 |
| -echo -e "\nCreating Application Insights: $APP_INSIGHTS_NAME..." |
101 |
| -az monitor app-insights component create \ |
102 |
| - --app "$APP_INSIGHTS_NAME" \ |
103 |
| - --location "$LOCATION" \ |
104 |
| - --resource-group "$RESOURCE_GROUP_NAME" \ |
105 |
| - --application-type "web" |
106 |
| -if [ $? -ne 0 ]; then |
107 |
| - echo "Failed to create Application Insights: $APP_INSIGHTS_NAME." |
108 |
| - exit 1 |
109 |
| -fi |
110 |
| -echo "Application Insights $APP_INSIGHTS_NAME created successfully." |
111 |
| - |
112 |
| -# Retrieve Storage Account Key |
113 |
| -echo -e "\nRetrieving storage account key for $STORAGE_ACCOUNT_NAME..." |
114 |
| -STORAGE_ACCOUNT_KEY=$(az storage account keys list \ |
115 |
| - --account-name "$STORAGE_ACCOUNT_NAME" \ |
116 |
| - --resource-group "$RESOURCE_GROUP_NAME" \ |
117 |
| - --query "[0].value" -o tsv) |
118 |
| -if [ $? -ne 0 ]; then |
119 |
| - echo "Failed to retrieve storage account key for $STORAGE_ACCOUNT_NAME." |
120 |
| - exit 1 |
121 |
| -fi |
122 |
| -echo "Storage account key retrieved successfully." |
123 |
| - |
124 |
| -# Retrieve Application Insights Keys |
125 |
| -echo -e "\nRetrieving Application Insights keys for $APP_INSIGHTS_NAME..." |
126 |
| -APP_INSIGHTS_KEY=$(az monitor app-insights component show \ |
127 |
| - --app "$APP_INSIGHTS_NAME" \ |
128 |
| - --resource-group "$RESOURCE_GROUP_NAME" \ |
129 |
| - --query "instrumentationKey" -o tsv) |
130 |
| -if [ $? -ne 0 ]; then |
131 |
| - echo "Failed to retrieve Application Insights key." |
132 |
| - exit 1 |
133 |
| -fi |
134 |
| - |
135 |
| -# Create a Function App |
136 |
| -echo -e "\nCreating Function App: $FUNCTION_APP_NAME..." |
137 |
| -az functionapp create \ |
138 |
| - --name "$FUNCTION_APP_NAME" \ |
139 |
| - --resource-group "$RESOURCE_GROUP_NAME" \ |
140 |
| - --storage-account "$STORAGE_ACCOUNT_NAME" \ |
141 |
| - --plan "$SERVICE_PLAN_NAME" \ |
142 |
| - --runtime python \ |
143 |
| - --runtime-version 3.10 \ |
144 |
| - --os-type Linux |
145 |
| -if [ $? -ne 0 ]; then |
146 |
| - echo "Failed to create Function App: $FUNCTION_APP_NAME." |
147 |
| - exit 1 |
148 |
| -fi |
149 |
| -echo "Function App $FUNCTION_APP_NAME created successfully." |
| 26 | +# Create Resource Group |
| 27 | +echo -e "${YELLOW}Creating resource group $RESOURCE_GROUP in $LOCATION ${RESET}" |
| 28 | +az group create --name $RESOURCE_GROUP --location $LOCATION |
150 | 29 |
|
151 |
| -# Set Application Settings for Function App |
152 |
| -echo -e "\nConfiguring application settings for $FUNCTION_APP_NAME..." |
153 |
| -az functionapp config appsettings set \ |
154 |
| - --name "$FUNCTION_APP_NAME" \ |
155 |
| - --resource-group "$RESOURCE_GROUP_NAME" \ |
156 |
| - --settings \ |
157 |
| - "APPINSIGHTS_INSTRUMENTATIONKEY=$APP_INSIGHTS_KEY" \ |
158 |
| - "AzureWebJobsFeatureFlags=EnableWorkerIndexing" |
| 30 | +# Deploy Bicep template |
| 31 | +echo -e "${YELLOW}Deploying Bicep template...${RESET}" |
| 32 | +az deployment group create --resource-group $RESOURCE_GROUP --template-file $BICEP_FILE -c |
159 | 33 | if [ $? -ne 0 ]; then
|
160 |
| - echo "Failed to configure application settings for $FUNCTION_APP_NAME." |
161 |
| - exit 1 |
| 34 | + echo -e "${RED}Failed to deploy Bicep template.${RESET}" |
162 | 35 | fi
|
163 |
| -echo "Application settings configured successfully." |
164 | 36 |
|
165 |
| -echo -e "\n\n - - - - | ALL RESOURCES WERE SUCCESSFULLY PROVISIONED | - - - - \n\n" |
| 37 | +echo -e "${GREEN}Deployment completed.${RESET}" |
0 commit comments