Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 827504f

Browse files
authored
Merge pull request #1092 from cmilanf/dev
Several improvement and fixes in deploy-all.sh
2 parents fad4085 + 186b2eb commit 827504f

File tree

1 file changed

+63
-39
lines changed

1 file changed

+63
-39
lines changed

k8s/helm/deploy-all.sh

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,32 @@ Parameters:
1414
The resource group for the AKS cluster. Required when the registry (using the -r parameter) is set to "aks".
1515
-b | --build-solution
1616
Force a solution build before deployment (default: false).
17-
-d | --dns <dns or ip address>
18-
Specifies the external DNS/ IP address of the Kubernetes cluster.
17+
-d | --dns <dns or ip address> | --dns aks
18+
Specifies the external DNS/ IP address of the Kubernetes cluster.
19+
If 'aks' is set as value, the DNS value is retrieved from the AKS. --aks-name and --aks-rg are needed.
1920
When --use-local-k8s is specified the external DNS is automatically set to localhost.
2021
-h | --help
2122
Displays this help text and exits the script.
23+
--image-build
24+
Build images (default is to not build all images).
25+
--image-push
26+
Upload images to the container registry (default is not pushing to the custom registry)
2227
-n | --app-name <the name of the app>
2328
Specifies the name of the application (default: eshop).
29+
--namespace <namespace name>
30+
Specifies the namespace name to deploy the app. If it doesn't exists it will be created (default: eshop).
2431
-p | --docker-password <docker password>
2532
The Docker password used to logon to the custom registry, supplied using the -r parameter.
2633
-r | --registry <container registry>
2734
Specifies the container registry to use (required), e.g. myregistry.azurecr.io.
2835
--skip-clean
2936
Do not clean the Kubernetes cluster (default is to clean the cluster).
30-
--skip-image-build
31-
Do not build images (default is to build all images).
32-
--skip-image-push
33-
Do not upload images to the container registry (just run the Kubernetes deployment portion).
34-
Default is to push the images to the container registry.
3537
--skip-infrastructure
3638
Do not deploy infrastructure resources (like sql-data, no-sql or redis).
3739
This is useful for production environments where infrastructure is hosted outside the Kubernetes cluster.
3840
-t | --tag <docker image tag>
39-
The tag used for the newly created docker images. Default: newly created, date-based timestamp, with 1-minute resolution.
40-
-u | --docker-user <docker username>
41+
The tag used for the newly created docker images. Default: latest.
42+
-u | --docker-username <docker username>
4143
The Docker username used to logon to the custom registry, supplied using the -r parameter.
4244
--use-local-k8s
4345
Deploy to a locally installed Kubernetes (default: false).
@@ -47,29 +49,29 @@ If using AKS and ACR see link for more info:
4749
https://docs.microsoft.com/en-us/azure/container-registry/container-registry-auth-aks
4850
4951
WARNING! THE SCRIPT WILL COMPLETELY DESTROY ALL DEPLOYMENTS AND SERVICES VISIBLE
50-
FROM THE CURRENT CONFIGURATION CONTEXT.
51-
It is recommended that you create a separate namespace and confguration context
52-
for the $app_name application, to isolate it from other applications on the cluster.
52+
FROM THE CURRENT CONFIGURATION CONTEXT AND NAMESPACE.
53+
It is recommended that you check your selected namespace, 'eshop' by default, is already in use.
54+
Every deployment and service done in the namespace will be deleted.
5355
For more information see https://kubernetes.io/docs/tasks/administer-cluster/namespaces/
54-
You can use namespace.yaml file (in the same directory) to create the namespace.
5556
5657
END
5758
}
5859

5960
app_name='eshop'
6061
aks_name=''
6162
aks_rg=''
62-
build_images='yes'
63+
build_images=''
6364
clean='yes'
6465
build_solution=''
6566
container_registry=''
6667
docker_password=''
6768
docker_username=''
6869
dns=''
69-
image_tag=$(date '+%Y%m%d%H%M')
70-
push_images='yes'
70+
image_tag='latest'
71+
push_images=''
7172
skip_infrastructure=''
7273
use_local_k8s=''
74+
namespace='eshop'
7375

7476
while [[ $# -gt 0 ]]; do
7577
case "$1" in
@@ -86,15 +88,15 @@ while [[ $# -gt 0 ]]; do
8688
-n | --app-name )
8789
app_name="$2"; shift 2;;
8890
-p | --docker-password )
89-
docker_password="$2"; shift;;
91+
docker_password="$2"; shift 2;;
9092
-r | --registry )
9193
container_registry="$2"; shift 2;;
9294
--skip-clean )
9395
clean=''; shift ;;
94-
--skip-image-build )
95-
build_images=''; shift ;;
96-
--skip-image-push )
97-
push_images=''; shift ;;
96+
--image-build )
97+
build_images='yes'; shift ;;
98+
--image-push )
99+
push_images='yes'; shift ;;
98100
--skip-infrastructure )
99101
skip_infrastructure='yes'; shift ;;
100102
-t | --tag )
@@ -103,6 +105,8 @@ while [[ $# -gt 0 ]]; do
103105
docker_username="$2"; shift 2;;
104106
--use-local-k8s )
105107
use_local_k8s='yes'; shift ;;
108+
--namespace )
109+
namespace="$2"; shift 2;;
106110
*)
107111
echo "Unknown option $1"
108112
usage; exit 2 ;;
@@ -124,10 +128,26 @@ if [[ $build_images ]]; then
124128
docker rmi $(docker images -qf "dangling=true")
125129
fi
126130

131+
use_custom_registry=''
132+
133+
if [[ -n $container_registry ]]; then
134+
echo "################ Log into custom registry $container_registry ##################"
135+
use_custom_registry='yes'
136+
if [[ -z $docker_username ]] || [[ -z $docker_password ]]; then
137+
echo "Error: Must use -u (--docker-username) AND -p (--docker-password) if specifying custom registry"
138+
exit 1
139+
fi
140+
docker login -u $docker_username -p $docker_password $container_registry
141+
fi
142+
127143
if [[ $push_images ]]; then
128144
echo "#################### Pushing images to the container registry ####################"
129145
services=(basket.api catalog.api identity.api ordering.api marketing.api payment.api locations.api webmvc webspa webstatus)
130146

147+
if [[ -z "$(docker image ls -q --filter=reference=eshop/$service:$image_tag)" ]]; then
148+
image_tag=linux-$image_tag
149+
fi
150+
131151
for service in "${services[@]}"
132152
do
133153
echo "Pushing image for service $service..."
@@ -152,14 +172,23 @@ if [[ $dns == "aks" ]]; then
152172
exit 1
153173
fi
154174

155-
echo "Getting DNS of AKS of AKS $aks_name (in resource group $aks_rg)"
156-
dns="$(az aks show -n $aks_name -g $aks_rg --query addonProfiles.httpApplicationRouting.config.HTTPApplicationRoutingZoneName)"
157-
if [[ -z dns ]]; then
175+
echo "Getting AKS cluster $aks_name AKS (in resource group $aks_rg)"
176+
# JMESPath queries are case sensitive and httpapplicationrouting can be lowercase sometimes
177+
jmespath_dnsqueries=(\
178+
addonProfiles.httpApplicationRouting.config.HTTPApplicationRoutingZoneName \
179+
addonProfiles.httpapplicationrouting.config.HTTPApplicationRoutingZoneName \
180+
)
181+
for q in "${jmespath_dnsqueries[@]}"
182+
do
183+
dns="$(az aks show -n $aks_name -g $aks_rg --query $q -o tsv)"
184+
if [[ -n $dns ]]; then break; fi
185+
done
186+
if [[ -z $dns ]]; then
158187
echo "Error: when getting DNS of AKS $aks_name (in resource group $aks_rg). Please ensure AKS has httpRouting enabled AND Azure CLI is logged in and is of version 2.0.37 or higher."
159188
exit 1
160189
fi
161-
$dns=${dns//[\"]/""}
162190
echo "DNS base found is $dns. Will use $aks_name.$dns for the app!"
191+
dns="$aks_name.$dns"
163192
fi
164193

165194
# Initialization & check commands
@@ -169,17 +198,12 @@ fi
169198

170199
if [[ $clean ]]; then
171200
echo "Cleaning previous helm releases..."
172-
helm delete --purge $(helm ls -q)
173-
echo "Previous releases deleted"
174-
fi
175-
176-
use_custom_registry=''
177-
178-
if [[ -n $container_registry ]]; then
179-
use_custom_registry='yes'
180-
if [[ -z $docker_user ]] || [[ -z $docker_password ]]; then
181-
echo "Error: Must use -u (--docker-username) AND -p (--docker-password) if specifying custom registry"
182-
exit 1
201+
if [[ -z $(helm ls -q --namespace $namespace) ]]; then
202+
echo "No previous releases found"
203+
else
204+
helm delete --purge $(helm ls -q --namespace $namespace)
205+
echo "Previous releases deleted"
206+
waitsecs=10; while [ $waitsecs -gt 0 ]; do echo -ne "$waitsecs\033[0K\r"; sleep 1; : $((waitsecs--)); done
183207
fi
184208
fi
185209

@@ -191,17 +215,17 @@ if [[ !$skip_infrastructure ]]; then
191215
for infra in "${infras[@]}"
192216
do
193217
echo "Installing infrastructure: $infra"
194-
helm install --values app.yaml --values inf.yaml --values $ingress_values_file --set app.name=$app_name --set inf.k8s.dns=$dns --name="$app_name-$infra" $infra
218+
helm install --namespace $namespace --set "ingress.hosts={$dns}" --values app.yaml --values inf.yaml --values $ingress_values_file --set app.name=$app_name --set inf.k8s.dns=$dns --name="$app_name-$infra" $infra
195219
done
196220
fi
197221

198222
for chart in "${charts[@]}"
199223
do
200224
echo "Installing: $chart"
201225
if [[ $use_custom_registry ]]; then
202-
helm install --set inf.registry.server=$container_registry --set inf.registry.login=$docker_username --set inf.registry.pwd=$docker_password --set inf.registry.secretName=eshop-docker-scret --values app.yaml --values inf.yaml --values $ingress_values_file --set app.name=$app_name --set inf.k8s.dns=$dns --set image.tag=$image_tag --set image.pullPolicy=Always --name="$app_name-$chart" $chart
226+
helm install --namespace $namespace --set "ingress.hosts={$dns}" --set inf.registry.server=$container_registry --set inf.registry.login=$docker_username --set inf.registry.pwd=$docker_password --set inf.registry.secretName=eshop-docker-scret --values app.yaml --values inf.yaml --values $ingress_values_file --set app.name=$app_name --set inf.k8s.dns=$dns --set image.tag=$image_tag --set image.pullPolicy=Always --name="$app_name-$chart" $chart
203227
elif [[ $chart != "eshop-common" ]]; then # eshop-common is ignored when no secret must be deployed
204-
helm install --values app.yaml --values inf.yaml --values $ingress_values_file --set app.name=$app_name --set inf.k8s.dns=$dns --set image.tag=$image_tag --set image.pullPolicy=Always --name="$app_name-$chart" $chart
228+
helm install --namespace $namespace --set "ingress.hosts={$dns}" --values app.yaml --values inf.yaml --values $ingress_values_file --set app.name=$app_name --set inf.k8s.dns=$dns --set image.tag=$image_tag --set image.pullPolicy=Always --name="$app_name-$chart" $chart
205229
fi
206230
done
207231

0 commit comments

Comments
 (0)