-
Notifications
You must be signed in to change notification settings - Fork 4
/
session-spawner.sh
50 lines (44 loc) · 1.56 KB
/
session-spawner.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
#!/bin/sh
export rm=${rm:-false}
export template=${template:-'restricted.json'}
export image=${image:-'ubuntu:latest'}
exec_session() {
uid=$(kubectl get cm "user-$USER" -o jsonpath='{.data.uid}')
gid=$(kubectl get cm "user-$USER" -o jsonpath='{.data.gid}')
shell=$(kubectl get cm "user-$USER" -o jsonpath='{.data.shell}')
overrides=$(
sed -e "s/__UID__/$uid/g" -e "s/__USER__/$USER/g" \
-e "s/__GID__/$gid/g" -e "s/__IMAGE__/$image/g" "/templates/$template"
)
echo "[$(date)][INFO] Starting session instance $pod_name with image $image rm: $rm"
exec kubectl run "session-$USER" --rm="$rm" -i -t \
--labels=app=session-host,user="$USER" --image="$image" \
--overrides="$overrides" -- "$shell"
}
main() {
pod_name=$(
kubectl get pods -l app=session-host,user="$USER" \
--output=jsonpath='{.items..metadata.name}'
)
if [ "$pod_name" != "" ]; then
if [ "$rm" = "true" ]; then
echo "[$(date)][INFO] Deleting previous instance.."
kubectl delete deploy "session-$USER"
while ! kubectl get pod -l "app=session-host,user=$USER" --no-headers=true 2>&1 \
| grep -q "No resources found"; do
echo "[$(date)][INFO] Waiting for Pod to terminate..."
sleep 5
done
exec_session
else
echo "[$(date)][INFO] Attaching to session instance $pod_name"
exec kubectl attach "$pod_name" -c session-host -i -t
fi
elif ! kubectl get cm "user-$USER" > /dev/null 2>&1; then
echo "[$(date)][ERROR] Could not locate user configMap."
exit 1
else
exec_session
fi
}
main "$@"