-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack
More file actions
executable file
·155 lines (132 loc) · 4.27 KB
/
stack
File metadata and controls
executable file
·155 lines (132 loc) · 4.27 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
#!/bin/bash
#set -ex
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
DEPLOYMENT_DIR=${SCRIPT_DIR}/deployments
IMAGE="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 " dev\t\tDevelop the service. Port defaults to 8080.\n"
printf " \t\tSyntax: dev [action]\n"
printf " \t\tExample: dev up\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 " install\tInstall dependencies.\n"
printf " \t\tSyntax: install\n"
printf " \t\tExample: install\n"
printf "\n"
printf " logs\t\tSee logs.\n"
printf " \t\tSyntax: logs\n"
printf " \t\tExample: logs\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 " up | down\tDeploy or destroy the stack. Tag defaults to 'latest'.\n"
printf " \t\tSyntax: up [<tag>]\n"
printf " \t\tExample: up v0.1.0\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."
}
install_if_doesnt_exist(){
local tool=$1
local cmd=$2
type $tool > /dev/null 2>&1
if [[ "$?" == "1" ]]; then
printf "Installing ${tool} ...\n"
echo "$cmd" | /bin/bash
else
printf "${tool} is already installed.\n"
fi
}
install_tools() {
KUBECTL_VERSION=${KUBECTL_VERSION:-v1.28.4}
MINIKUBE_VERSION=${MINIKUBE_VERSION:-v1.31.1}
echo $KUBECTL_VERSION
install_if_doesnt_exist "kubectl" "curl -LO https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/$(uname -s | tr 'A-Z' 'a-z')/$(uname -m)/kubectl && chmod +x ./kubectl && sudo mv ./kubectl /usr/local/bin/kubectl"
install_if_doesnt_exist "minikube" "curl -Lo minikube https://storage.googleapis.com/minikube/releases/v1.31.1/minikube-$(uname -s | tr 'A-Z' 'a-z')-$(uname -m) && chmod +x minikube && sudo cp minikube /usr/local/bin/ && rm minikube"
install_if_doesnt_exist "kubetpl" "curl -sSL https://github.com/shyiko/kubetpl/releases/download/0.9.0/kubetpl-0.9.0-$(bash -c '[[ $OSTYPE == darwin* ]] && echo darwin || echo linux')-amd64 -o kubetpl && chmod a+x kubetpl && sudo mv kubetpl /usr/local/bin/"
}
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}
}
develop() {
local action="$1"
validate_empty_input "$action" "Error: action (e.g. up, down) can't be empty."
if [[ "$action" == "up" ]]; then
tilt up
elif [[ "$action" == "down" ]]; then
tilt down
fi
}
deploy_or_destroy_stack() {
local action="$1"
local target_env="$2"
local tag=${3:-latest}
validate_empty_input "$action" "Error: action (e.g. up, down) can't be empty."
validate_empty_input "$target_env" "Error: target environmnet (e.g. local, dev, etc.) can't be empty."
if [[ "$action" == "up" ]]; then
kubetpl render ${DEPLOYMENT_DIR}/app.yaml -i ${DEPLOYMENT_DIR}/config-${target_env}.env -s IMAGE_TAG=$tag | kubectl apply -f -
kubectl get pods
elif [[ "$action" == "down" ]]; then
kubetpl render ${DEPLOYMENT_DIR}/app.yaml -i ${DEPLOYMENT_DIR}/config-${target_env}.env -s IMAGE_TAG=$tag | kubectl delete -f -
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}"
;;
dev)
develop "${@:2}"
;;
expose)
expose_service "${@:2}"
;;
install)
install_tools "${@:2}"
;;
logs)
print_logs
;;
push)
push_images "${@:2}"
;;
up | down)
deploy_or_destroy_stack "${@:1}"
;;
* | -h | help)
usage
;;
esac