-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions
More file actions
156 lines (128 loc) · 4.14 KB
/
functions
File metadata and controls
156 lines (128 loc) · 4.14 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
##########################################################################################
#Accounts & Environment Variables
##########################################################################################
#check if .var file is present.
varFileCheck(){
local fileCheck=0
while [[ $fileCheck = 0 ]]
do
if [[ -f .var ]]
then
source .var
fileCheck=1
else
printf "\r \e[33mWARNING: the .var file is missing. Please create it.\e[0m \n"
fileCheck=0
sleep 5
fi
done
}
#check if .az_cred file is present
userCredentialFileCheck(){
local fileCheck=0
while [[ $fileCheck = 0 ]]
do
if [[ -f .az_cred ]]
then
source .az_cred
fileCheck=1
else
printf "\r \e[33mWARNING: the .az_cred file is missing. Please create it.\e[0m \n"
fileCheck=0
sleep 5
fi
done
}
loginUser(){
#login to Azure using AZ CLI
varFileCheck
userCredentialFileCheck
az login \
--username $az_username \
--password $az_password \
--output none
if [[ $? -ne 0 ]]
then
printf "\r \e[31mERROR: Azure login with User failed\e[0m \n"
exit 1
else
#make sure you use the right subscription
az account set \
--subscription $subscription
printf "\r timestamp: $(timeStamp) - \e[34mLogged in to Azure as user\e[0m \n"
fi
}
##########################################################################################
#Create Resource Groups & Accounts
##########################################################################################
provisioning(){
while getopts 'u:' OPTION;
do
case "$OPTION" in
u)
userCount=$OPTARG
;;
?)
printf "\r usage: $(basename \$0) [-u] desired amount of users] \n" >&2
exit 1
;;
esac
done
local subscriptionID=$(az account show --query id --output tsv)
printf "userCount=\"$userCount\" \n" > userCount
for i in $(seq 1 $userCount)
do
az group create \
--name rg_$prefix$i \
--location $location \
--output none
local userID=$(
az ad user create \
--display-name "$prefix$i" \
--password "VerySafe$i" \
--user-principal-name "$prefix$i@$mydomain" \
--force-change-password-next-sign-in false \
--query id \
--output tsv
)
printf "\r username = $prefix$i@$mydomain \n" >> azure_accounts
printf "\r password = VerySafe$i \n" >> azure_accounts
printf "\r resource group = rg_$prefix$i \n" >> azure_accounts
#write userID into file
printf "$userID \n" >> accounts
#set strict persmissions for every user
az role assignment create \
--role "Contributor" \
--assignee $userID \
--scope /subscriptions/$subscriptionID/resourceGroups/rg_$prefix$i \
--output none
printf "\r timestamp: $(timeStamp) - \e[34mCreated resource group and account for $prefix$i@$mydomain\e[0m \n"
done
}
##########################################################################################
#Timestamping
##########################################################################################
timeStamp(){
date +%H:%M:%S
}
##########################################################################################
#Cleanup
##########################################################################################
deleteUsersAndGroups(){
source userCount
source .var
readarray -t userIdArray < accounts
for i in $(seq 1 $userCount)
do
az group delete \
--name "rg_$prefix$i" \
--no-wait \
--yes \
--output none
az ad user delete \
--id ${userIdArray[$(expr $i - 1)]} \
--output none
done
rm userCount accounts
printf "\r timestamp: $(timeStamp) - \e[34mDeleted resource groups and user accounts\e[0m \n"
}