-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinit.sh
69 lines (54 loc) · 2.21 KB
/
init.sh
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
#!/bin/bash
set -e
###############################################################
# Script Parameters #
###############################################################
while getopts c:e: option
do
case "${option}"
in
c) CLUSTER_NAME=${OPTARG};;
e) ENVIRONMENT=${OPTARG};;
esac
done
if [ -z "$CLUSTER_NAME" ]; then
echo "-c is a required argument - Resource Group Name for storage account"
exit 1
fi
if [ -z "$ENVIRONMENT" ]; then
echo "-e is a required argument - Environment (dev, prod)"
exit 1
fi
###############################################################
# Script Begins #
###############################################################
RESOURCE_GROUP_NAME=${CLUSTER_NAME}${ENVIRONMENT}
STORAGE_ACCOUNT_NAME=${CLUSTER_NAME}${ENVIRONMENT}
set +e # errors don't matter for a bit
# Create resource group
if [ $(az group exists -n $RESOURCE_GROUP_NAME -o tsv) = false ]
then
az group create --name $RESOURCE_GROUP_NAME --location eastus2
else
echo "Resource Group $RESOURCE_GROUP_NAME already exists"
fi
# Create storage account
az storage account show -n $STORAGE_ACCOUNT_NAME -g $RESOURCE_GROUP_NAME > /dev/null
if [ $? -eq 0 ]
then
echo "Storage account $STORAGE_ACCOUNT_NAME in resource group $RESOURCE_GROUP_NAME already exists"
else
az storage account create --resource-group $RESOURCE_GROUP_NAME --name $STORAGE_ACCOUNT_NAME --sku Standard_LRS --encryption-services blob
fi
set -e # errors matter again
# Get storage account key
ACCOUNT_KEY=$(az storage account keys list --resource-group $RESOURCE_GROUP_NAME --account-name $STORAGE_ACCOUNT_NAME --query [0].value -o tsv)
# Create blob container
az storage container create --name $ENVIRONMENT --account-name $STORAGE_ACCOUNT_NAME --account-key $ACCOUNT_KEY
ARM_SUBSCRIPTION_ID=$(az account show --query id --out tsv)
ARM_TENANT_ID=$(az account show --query tenantId --out tsv)
terraform init -upgrade -input=false \
-backend-config="access_key=$ACCOUNT_KEY" \
-backend-config="resource_group_name=$RESOURCE_GROUP_NAME" \
-backend-config="storage_account_name=$STORAGE_ACCOUNT_NAME" \
-backend-config="container_name=$ENVIRONMENT"