Skip to content

Commit 7d93a8d

Browse files
authored
Merge pull request #57 from quortex/feature/azure
add script azure
2 parents ea4d8ab + 92e6591 commit 7d93a8d

3 files changed

Lines changed: 115 additions & 1 deletion

File tree

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,31 @@ Available options :
149149

150150
---
151151

152+
## init_tf_backend_azure
153+
154+
Provision the resources needed to store terraform states on AZURE.
155+
156+
A container on Azure Storage Account will be created. State locking is support by default.
157+
### Usage
158+
159+
```
160+
Usage : ./init_tf_backend_azure.sh -n NAME [options]
161+
162+
Mandatory arguments :
163+
-n NAME Set the name of created resources.
164+
Available options :
165+
-r The name of the region (default $REGION).
166+
-s The name of the subscription.
167+
-rg The name of Ressource Group (default $RESOURCE_GROUP_NAME).
168+
-st The name of Storage Account.
169+
-ct The name of Container.
170+
-h Display this help.
171+
172+
173+
If you have probleme with write access of state, go to portal.azure.com and assign you the rights "Storage Blob Data Owner" to the subscription or on the storage account.
174+
175+
---
176+
152177
## get_cluster_config
153178
154179
Compute a cluster Config from a given ServiceAccount.

init_tf_backend_aws.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Usage : $0 -n NAME [options]
2323
Mandatory arguments :
2424
-n NAME Set the name of created resources.
2525
Available options :
26-
-r REGION Specify the region in which to create the resources.
26+
-r REGION Specify the region in which to create the resources (default $REGION).
2727
-p PREFIXED Whether to prefix the name with "<ACCOUNT ID>-tfstate-" (default $PREFIXED)
2828
-b BLOCK_PUBLIC_ACCESS Whether to block public access for s3 bucket (default $BLOCK_PUBLIC_ACCESS)
2929
-y Execute script in non interactive mode.

init_tf_backend_azure.sh

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/bin/bash
2+
#
3+
# The purpose of this script is to provision the resources needed to store
4+
# terraform states on AZURE. It will provision a container on Azure Storage account as well as a
5+
# container to support state locking and consistency checking.
6+
#
7+
# Official documentation about terraform state in Azure Storage Account =>
8+
# https://developer.hashicorp.com/terraform/language/settings/backends/azurerm
9+
# Bash strict mode
10+
set -euo pipefail
11+
12+
REGION=westeurope
13+
NAME=
14+
RESOURCE_GROUP_NAME=administration
15+
SUBSCRIPTION=
16+
17+
function help() {
18+
cat <<EOF
19+
Provision the resources needed to store terraform states on Azure.
20+
A storage account is created and container for store state.
21+
Usage : $0 -n NAME [options]
22+
23+
Mandatory arguments :
24+
-n NAME Set the name of created resources.
25+
Available options :
26+
-r The name of the region (default $REGION).
27+
-s The name of the subscription.
28+
-rg The name of Ressource Group (default $RESOURCE_GROUP_NAME).
29+
-st The name of Storage Account.
30+
-ct The name of Container.
31+
-h Display this help.
32+
EOF
33+
}
34+
35+
while getopts "n:s:r:h:rg:st:ct" opt; do
36+
case "$opt" in
37+
h)
38+
help
39+
exit 0
40+
;;
41+
r)
42+
REGION=$OPTARG
43+
;;
44+
n)
45+
NAME=$OPTARG
46+
;;
47+
rg)
48+
RESOURCE_GROUP_NAME=$OPTARG
49+
;;
50+
st)
51+
STORAGE_ACCOUNT_NAME=$OPTARG
52+
;;
53+
ct)
54+
CONTAINER_NAME=$OPTARG
55+
;;
56+
s)
57+
SUBSCRIPTION=$OPTARG
58+
;;
59+
60+
esac
61+
done
62+
63+
if [ "$NAME" == "" ]; then
64+
echo "Name was not specified, aborting !"
65+
exit 1
66+
fi
67+
68+
if [ "$SUBSCRIPTION" == "" ]; then
69+
echo "Subscription not specified, the subscription is the default sub of the account."
70+
SUBSCRIPTION=$(az account list --query "[?isDefault].id | [0]" -o tsv)
71+
fi
72+
73+
#Set var with name input
74+
STORAGE_ACCOUNT_NAME=${NAME}staccount
75+
CONTAINER_NAME=tfstate${STORAGE_ACCOUNT_NAME}
76+
77+
export AZURE_extension_use_dynamic_install=yes_without_prompt
78+
user=$(az ad signed-in-user show --query userPrincipalName --output tsv)
79+
80+
# Create Storage Account
81+
echo "Creating storage account : ${RESOURCE_GROUP_NAME}"
82+
res=$(az storage account create --resource-group $RESOURCE_GROUP_NAME --name $STORAGE_ACCOUNT_NAME --sku Standard_LRS --encryption-services blob)
83+
84+
echo "Creating Storage Account : ${RESOURCE_GROUP_NAME}"
85+
# Create Storage Account Container
86+
res=$(az storage container create --name $CONTAINER_NAME --account-name $STORAGE_ACCOUNT_NAME --auth-mode login)
87+
88+
echo "storage_account_name: $STORAGE_ACCOUNT_NAME"
89+
echo "container_name: $CONTAINER_NAME"

0 commit comments

Comments
 (0)