-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtricks
executable file
·179 lines (155 loc) · 3.71 KB
/
tricks
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env bash
FILE=/tmp/.tricks.yaml
HTTP_PORT=8080
# https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/#accessing-the-api-from-a-pod
K8S_API_SERVER=$(bin/k8s_api_server.sh)
K8S_API_TOKEN=$(bin/k8s_api_token.sh)
## create deployment file
cat <<EOF > ${FILE}
apiVersion: v1
kind: Pod
metadata:
name: tricks
labels:
tricks: "true"
spec:
restartPolicy: Never
containers:
- name: tricks
image: vitorenesduarte/tricks
imagePullPolicy: Never
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: K8S_API_SERVER
value: "${K8S_API_SERVER}"
- name: K8S_API_TOKEN
value: "${K8S_API_TOKEN}"
EOF
function get_pod_status {
###
# Returns one of:
# - Running
# - Terminating
# - NotFound
###
kubectl get -f ${FILE} 2>&1 | grep -oE "(Running|Terminating|NotFound)"
}
function get_service_status {
###
# Returns one of:
# - NotFound
###
kubectl get service tricks 2>&1 | grep -oE "(NotFound)"
}
function get_service_url {
###
# Return tricks service http endpoint.
###
# TODO check if context is minikube
minikube service tricks --url 2> /dev/null
}
function build {
# TODO remove
bin/minikube-docker-build.sh
}
function start {
###
# Start tricks if not found in the cluster.
# If in the cluster, it is assumed it is
# configured correclty.
###
local _status=$(get_pod_status)
local _url=$(get_service_url)
if [ "${_status}" == "NotFound" ]; then
echo "Starting Tricks..."
kubectl create -f ${FILE}
kubectl expose pod tricks --type=NodePort --port=${HTTP_PORT}
echo "Configuring Tricks..."
while [ -z "${_url}" ]; do
_url=$(get_service_url)
sleep 1
done
echo "Tricks configured correctly!"
else
if [ -z "${_url}" ]; then
echo "Tricks is running, but not configured correctly!"
fi
fi
}
function stop {
###
# Loop while tricks pod exists.
# Loop while tricks service exists.
###
local _pod_status=$(get_pod_status)
local _service_status=$(get_service_status)
while [ "${_pod_status}" != "NotFound" ]; do
sleep 1
kubectl delete -f ${FILE} --now > /dev/null 2>&1
_pod_status=$(get_pod_status)
done
while [ "${_service_status}" != "NotFound" ]; do
sleep 1
kubectl delete service tricks > /dev/null 2>&1
_service_status=$(get_service_status)
done
}
function logs {
###
# Tail the logs forever.
###
kubectl logs -f tricks
}
function experiment {
local _yaml=$1
local _json=/tmp/exp.json
local _url=$(get_service_url)
local _endpoint=${_url}/exp
if [ ! -f ${_yaml} ]; then
echo "File ${_yaml} not found!"
exit 1
fi
# convert yaml user file to json
local _cmd="import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)"
python -c "${_cmd}" < ${_yaml} > ${_json}
# post json in service endpoint
local _reply
local _id
_cmd="import sys, json; print json.load(sys.stdin)['expId']"
_reply=$(curl -s -H "Content-Type: application/json" -X POST -d @"${_json}" ${_endpoint})
_id=$(echo ${_reply} | python -c "${_cmd}")
echo "Experiment identifier: ${_id}"
watch kubectl get pods -l expId=${_id}
}
case "$1" in
start)
build
start
;;
stop)
stop
;;
logs)
logs
;;
exp)
if [ $# -ne 2 ]; then
echo "Usage: tricks exp FILE"
exit 1
fi
start
experiment $2
;;
*)
echo "usage: tricks <command>"
echo ""
echo "list of commands:"
echo " start Starts Tricks in the running Kubernetes cluster."
echo " logs Tails the logs of Tricks."
echo " exp <file> Runs the experiment described in the file."
echo " stop Stops Tricks."
exit 1
esac