forked from yamaszone/spring-greetings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack
executable file
·105 lines (87 loc) · 2.39 KB
/
stack
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
#!/bin/bash
#set -ex
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
DEPLOYMENT_DIR=${SCRIPT_DIR}/deployments
IMAGE="airbusutm/hello-spring"
usage() {
printf "Usage:\n"
printf " help\t\tShow this help\n"
printf "\n"
printf " build\t\tBuild the stack. Tag defaults to 'latest'.\n"
printf " \t\tSyntax: build [<tag>]\n"
printf " \t\tExample: build e0daeddc1f1f39f1f305e4cc5c4db6a247d05123\n"
printf "\n"
printf " expose\tExpose the service. Port defaults to 8080.\n"
printf " \t\tSyntax: expose [<port>]\n"
printf " \t\tExample: expose\n"
printf "\n"
printf " push\t\tPush containers to registry. Tag defaults to 'latest'\n"
printf " \t\tSyntax: push [<tag>]\n"
printf " \t\tExample: push e0daeddc1f1f39f1f305e4cc5c4db6a247d05123\n"
printf "\n"
printf " logs\t\tSee logs.\n"
printf " \t\tSyntax: logs\n"
printf " \t\tExample: logs\n"
printf "\n"
printf " up | down\tDeploy or destroy the stack. Tag defaults to 'latest'.\n"
printf " \t\tSyntax: up [<tag>]\n"
printf " \t\tExample: up e0daeddc1f1f39f1f305e4cc5c4db6a247d05123\n"
printf "\n"
}
validate_empty_input() {
local user_input="$1"
local err_msg="$2"
[[ -z "$user_input" ]] && printf "$err_msg\n" && exit 1
}
validate_target_env() {
local target_env="$1"
validate_empty_input "$target_env" "Error: target environment (allowed values: local, remote) can't be empty."
}
build_stack() {
local tag=${1:-latest}
# Cleanup old jars
rm -rf target/*-SNAPSHOT.jar
docker run --rm -v $PWD:/src -w /src maven:3.8-openjdk-18-slim /bin/bash -c "mvn clean install"
docker build --platform=linux/amd64 -t ${IMAGE}:${tag} -f Dockerfile .
}
push_images() {
local tag=${1:-latest}
docker push ${IMAGE}:${tag}
}
deploy_or_destroy_stack() {
local action="$1"
validate_empty_input "$action" "Error: action (e.g. up, down) can't be empty."
if [[ "$action" == "up" ]]; then
kubectl apply -f ${DEPLOYMENT_DIR}/
kubectl get pods
elif [[ "$action" == "down" ]]; then
kubectl delete -f ${DEPLOYMENT_DIR}/
fi
}
expose_service(){
local port="${1-:${CONTAINER_PORT}}"
kubectl port-forward deployments/greetings ${port}:8080
}
print_logs() {
kubectl logs -f deployment/greetings
}
case "$1" in
build)
build_stack "${@:2}"
;;
expose)
expose_service "${@:2}"
;;
push)
push_images "${@:2}"
;;
logs)
print_logs
;;
up | down)
deploy_or_destroy_stack "${@:1}"
;;
* | -h | help)
usage
;;
esac