-
Notifications
You must be signed in to change notification settings - Fork 6
/
comp-shell.bash
118 lines (98 loc) · 3.1 KB
/
comp-shell.bash
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
#!/bin/bash
if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ $# -gt 4 ]; then
echo "Open shell in container image"
echo "Usage: $0 [--local-image] [--always-pull] [--no-tools] [IMAGE]"
exit 0
fi
image_repo="docker-registry.rahti.csc.fi/chipster-images-release/"
image_pull_policy="IfNotPresent"
# --local-image
for arg in "$@"; do
if [[ "$arg" == "--local-image" ]]; then
image_pull_policy="Never"
fi
done
# --always-pull
for arg in "$@"; do
if [[ "$arg" == "--always-pull" ]]; then
image_pull_policy="Always"
fi
done
# --no-tools
for arg in "$@"; do
if [[ "$arg" == "--no-tools" ]]; then
no_tools=true
fi
done
# image
image="comp-20-04-r-deps"
# Use the last argument as the image if it doesn't start with '--'
if [ $# -gt 0 ]; then
# Get the last argument
last_argument="${!#}"
# Check if the last argument doesn't start with '--'
if [[ ! "$last_argument" == --* ]]; then
image="$last_argument"
fi
fi
name="comp-shell"
if ! kubectl get pod | grep $name | grep Running >/dev/null 2>&1; then
deployment=$(
cat <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: $name
spec:
replicas: 1
selector:
matchLabels:
app: $name
template:
metadata:
labels:
app: $name
spec:
containers:
- name: $name
volumeMounts: []
volumes: []
EOF
)
echo "image: ${image_repo}${image}"
echo "imagePullPolicy: ${image_pull_policy}"
patch=".spec.template.spec.containers[0].image = \"${image_repo}${image}\" |
.spec.template.spec.containers[0].imagePullPolicy = \"${image_pull_policy}\""
TOOLS_BIN_NAME=$(cat ~/values.yaml | yq e - -o json | jq .toolsBin.version -r)
TOOLS_BIN_HOST_MOUNT_PATH=$(cat ~/values.yaml | yq e - -o json | jq .toolsBin.hostPath -r)
TOOLS_BIN_PATH="/opt/chipster/tools"
if [[ $TOOLS_BIN_NAME != "null" && $no_tools != "true" ]]; then
patch="$patch |
.spec.template.spec.containers[0].volumeMounts += [{\"name\": \"tools-bin\", \"readOnly\": false, \"mountPath\": \"$TOOLS_BIN_PATH\"}]"
if [[ $TOOLS_BIN_HOST_MOUNT_PATH != "null" ]]; then
echo "mount tools-bin from hostPath $TOOLS_BIN_HOST_MOUNT_PATH/$TOOLS_BIN_NAME to $TOOLS_BIN_PATH"
patch="$patch |
.spec.template.spec.volumes += [{\"name\": \"tools-bin\", \"hostPath\": { \"path\": \"$TOOLS_BIN_HOST_MOUNT_PATH/$TOOLS_BIN_NAME\", \"type\": \"Directory\"}}]"
else
echo "mount tools-bin from PVC $TOOLS_BIN_NAME to $TOOLS_BIN_PATH"
patch="$patch |
.spec.template.spec.volumes += [{\"name\": \"tools-bin\", \"persistentVolumeClaim\": { \"claimName\": \"tools-bin-$TOOLS_BIN_NAME\"}}]"
fi
else
echo "do not mount tools-bin"
fi
json=$(echo "$deployment" | yq e - -o=json | jq "$patch")
echo "$json" | kubectl apply -f -
echo "waiting pod to start"
for i in $(seq 30); do
if kubectl get pod 2>/dev/null | grep $name | grep Running >/dev/null; then
echo ""
break
fi
printf "."
sleep 1
done
fi
echo "comp shell, press Ctrl+D to exit"
kubectl exec -it $(kubectl get pod | grep $name | grep Running | cut -d " " -f 1) -- bash
kubectl delete deployment $name