-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-acr.sh
145 lines (118 loc) · 4.8 KB
/
create-acr.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
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
#!/bin/bash
# Color theming
if [ -f ~/clouddrive/aspnet-learn/deploy/k8s/theme.sh ]
then
. <(cat ~/clouddrive/aspnet-learn/deploy/k8s/theme.sh)
fi
if [ -f ~/clouddrive/aspnet-learn-temp/create-aks-exports.txt ]
then
eval $(cat ~/clouddrive/aspnet-learn-temp/create-aks-exports.txt)
fi
if [ -f ~/clouddrive/aspnet-learn-temp/create-idtag-exports.txt ]
then
eval $(cat ~/clouddrive/aspnet-learn-temp/create-idtag-exports.txt)
fi
eshopRg=${ESHOP_RG}
eshopLocation=${ESHOP_LOCATION}
eshopIdTag=${ESHOP_IDTAG}
while [ "$1" != "" ]; do
case $1 in
-g | --resource-group) shift
eshopRg=$1
;;
-l | --location) shift
eshopLocation=$1
;;
* ) echo "Invalid param: $1"
exit 1
esac
shift
done
if [ -z "$eshopRg" ]
then
echo "${newline}${errorStyle}ERROR: Resource group is mandatory. Use -g to set it${defaultTextStyle}${newline}"
exit 1
fi
rg=`az group show -g $eshopRg -o json`
if [ -z "$rg" ]
then
if [ -z "$eshopLocation" ]
then
echo "${newline}${errorStyle}ERROR: If resource group has to be created, location is mandatory. Use -l to set it.${defaultTextStyle}${newline}"
exit 1
fi
echo "Creating resource group \"$eshopRg\" in location \"$eshopLocation\"..."
az group create -n $eshopRg -l $eshopLocation
if [ ! $? -eq 0 ]
then
echo "${newline}${errorStyle}ERROR: Can't create resource group${defaultTextStyle}${newline}"
exit 1
fi
echo "Created resource group \"$eshopRg\" in location \"$eshopLocation\"."
else
if [ -z "$eshopLocation" ]
then
eshopLocation=`az group show -g $eshopRg --query "location" -otsv`
fi
fi
# ACR Creation
eshopAcrName=${ESHOP_ACRNAME}
if [ -z "$eshopAcrName" ]
then
if [ -z "$eshopIdTag" ]
then
dateString=$(date "+%Y%m%d%H%M%S")
random=`head /dev/urandom | tr -dc 0-9 | head -c 3 ; echo ''`
eshopIdTag="$dateString$random"
fi
echo
echo "Creating Azure Container Registry \"eshoplearn$eshopIdTag\" in resource group \"$eshopRg\"..."
acrCommand="az acr create --name eshoplearn$eshopIdTag -g $eshopRg -l $eshopLocation -o json --sku basic --admin-enabled --query \"name\" -otsv"
echo "${newline} > ${azCliCommandStyle}$acrCommand${defaultTextStyle}${newline}"
eshopAcrName=`$acrCommand`
if [ ! $? -eq 0 ]
then
echo "${newline}${errorStyle}ERROR creating ACR!${defaultTextStyle}${newline}"
exit 1
fi
fi
eshopRegistry=`az acr show -n $eshopAcrName --query "loginServer" -otsv`
if [ -z "$eshopRegistry" ]
then
echo "${newline}${errorStyle}ERROR! ACR server $eshopAcrName doesn't exist!${defaultTextStyle}${newline}"
exit 1
fi
eshopAcrCredentials=`az acr credential show -n $eshopAcrName --query "[username,passwords[0].value]" -otsv`
eshopAcrUser=`echo "$eshopAcrCredentials" | head -1`
eshopAcrPassword=`echo "$eshopAcrCredentials" | tail -1`
# Grant permisions to AKS if created
aksIdentityObjectId=$(az aks show -g $eshopRg -n $ESHOP_AKSNAME --query identityProfile.kubeletidentity.objectId -otsv)
if [ ! -z "$aksIdentityObjectId" ]
then
acrResourceId=$(az acr show -n $eshopAcrName -g $eshopRg --query id -o tsv)
az role assignment create \
--role AcrPull \
--assignee-object-id $aksIdentityObjectId \
--scope $acrResourceId \
--output none
fi
echo export ESHOP_RG=$eshopRg >> create-acr-exports.txt
echo export ESHOP_LOCATION=$eshopLocation >> create-acr-exports.txt
echo export ESHOP_AKSNAME=$ESHOP_AKSNAME >> create-acr-exports.txt
echo export ESHOP_LBIP=$ESHOP_LBIP >> create-acr-exports.txt
echo export ESHOP_ACRNAME=$eshopAcrName >> create-acr-exports.txt
echo export ESHOP_REGISTRY=$eshopRegistry >> create-acr-exports.txt
echo export ESHOP_ACRUSER=$eshopAcrUser >> create-acr-exports.txt
echo export ESHOP_ACRPASSWORD=$eshopAcrPassword >> create-acr-exports.txt
echo export ESHOP_IDTAG=$eshopIdTag >> create-acr-exports.txt
echo export ESHOP_IDTAG=$eshopIdTag >> create-idtag-exports.txt
echo
echo "${defaultTextStyle}Created Azure Container Registry \"$eshopAcrName\" in resource group \"$eshopRg\" in location \"$eshopLocation\".${newline}"
mv -f create-acr-exports.txt ~/clouddrive/aspnet-learn-temp/
mv -f create-idtag-exports.txt ~/clouddrive/aspnet-learn-temp/
pushd ~/clouddrive/aspnet-learn-temp > /dev/null
echo "REGISTRY_LOGIN_SERVER: ${headingStyle}$eshopRegistry${defaultTextStyle}" >> config.txt
echo "REGISTRY_PASSWORD: ${headingStyle}$eshopAcrPassword${defaultTextStyle}" >> config.txt
echo "REGISTRY_USERNAME: ${headingStyle}$eshopAcrUser${defaultTextStyle}" >> config.txt
echo "${newline}" >> config.txt
popd > /dev/null