Newer
Older
# TODO:
# * Check that KUBECONFIG is set, maybe load automatically like before?
# * env var for native/docker mode
# * env var for local (or remote) docker image name
set -u
# If KUBECONFIG is not set, we try to find a kubeconfig file in a standard
# location.
if ! [[ -v KUBECONFIG ]]
then
if [ -f "$(pwd)/kubeconfig/kube_config_cluster.yml" ]
then
export KUBECONFIG="$(pwd)/kubeconfig/kube_config_cluster.yml"
elif [ -f "$(pwd)/backend/kubeconfig/kube_config_cluster.yml" ]
then
export KUBECONFIG="$(pwd)/backend/kubeconfig/kube_config_cluster.yml"
fi
if [[ -v KUBECONFIG ]]
then
echo "Local kubeconfig file found."
echo "Setting KUBECONFIG=$KUBECONFIG"
fi
fi
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
TELEPRESENCE_NOT_RUNNING=0
TELEPRESENCE_NATIVE=1
TELEPRESENCE_DOCKER=2
telepresenceRunning() {
status=$(telepresence status | head -n 1)
if [ "${status#*: }" == "Running" ]
then
echo "telepresence running"
daemonType="${status%%:*}"
echo "daemon type: $daemonType"
if [ "${daemonType##* }" == "container" ]
then
echo "This seems to be a docker-mode daemon."
return $TELEPRESENCE_DOCKER
else
echo "This seems to be a native-mode daemon."
return $TELEPRESENCE_NATIVE
fi
elif [ "${status#*: }" == "Not running" ]
then
echo "telepresence not running"
return $TELEPRESENCE_NOT_RUNNING
else
echo "Unknown telepresence state: $status"
exit 2
fi
}
telepresenceDns() {
telepresence status | grep -i 'Remote IP' | awk -F': ' '{print $2}'
}
prepareDocker() {
telepresenceRunning
case $? in
$TELEPRESENCE_NOT_RUNNING)
echo "Starting telepresence daemon in docker mode."
telepresence connect -n stackspin --docker || exit 3
;;
$TELEPRESENCE_NATIVE)
echo "Error: you want to start a docker-mode intercept, but there is a native telepresence daemon running. This is not currently supported."
exit 3
;;
$TELEPRESENCE_DOCKER)
echo "telepresence daemon already running; connecting to it."
telepresence connect -n stackspin --docker || exit 3
;;
esac
}
prepareNative() {
telepresenceRunning
case $? in
$TELEPRESENCE_NOT_RUNNING)
echo "Starting telepresence daemon in native mode."
telepresence connect -n stackspin || exit 3
;;
$TELEPRESENCE_NATIVE)
echo "telepresence daemon already running; connecting to it."
telepresence connect -n stackspin || exit 3
;;
$TELEPRESENCE_DOCKER)
echo "Error: you want to start a native-mode intercept, but there is a docker telepresence daemon running. This is not currently supported."
exit 3
;;
esac
}
runBackend() {
echo "Running dashboard backend locally and connecting to cluster."
case $mode in
"native")
pushd backend > /dev/null
source venv/bin/activate
echo "Stopping any previous intercept for dashboard-backend..."
telepresence leave dashboard-backend
echo "Starting new intercept for dashboard-backend..."
telepresence intercept dashboard-backend --service=dashboard-backend --port 5000:80 --mount=true -- env TELEPRESENCE_MODE=native flask run --reload
deactivate
popd > /dev/null
;;
"docker")
echo "Stopping any previous intercept for dashboard-backend..."
telepresence leave dashboard-backend
telepresence intercept dashboard-backend --service=dashboard-backend --port 5000:80 --mount=true --docker-run -- --dns $(telepresenceDns) -e TELEPRESENCE_MODE=docker dashboard-backend:test
esac
}
runFrontend() {
echo "Running dashboard frontend locally and connecting to cluster."
case $mode in
"native")
pushd frontend > /dev/null
echo "Stopping any previous intercept for dashboard-frontend..."
telepresence leave dashboard-frontend
telepresence intercept dashboard-frontend --service=dashboard --port 3000:80 --mount=true -- yarn start --watch --verbose
popd > /dev/null
;;
"docker")
echo "Stopping any previous intercept for dashboard-frontend..."
telepresence leave dashboard-frontend
telepresence intercept dashboard-frontend --service=dashboard --port 3000:80 --mount=true --docker-run -- dashboard-frontend:test
;;
esac
}
setupFrontend() {
pushd frontend > /dev/null
yarn install
popd > /dev/null
}
setupBackend() {
pushd backend > /dev/null
if ! [ -d venv ]
then
python3 -m venv venv
fi
source venv/bin/activate
pip install -r requirements.txt
deactivate
popd > /dev/null
}
setupCluster() {
telepresence helm install -f telepresence-values.yaml
}
cleanCluster() {
telepresence uninstall --all-agents
helm uninstall -n ambassador traffic-manager
}
if [ $# -eq 0 ]
then
echo "No command given. See README.md for help."
exit 1
fi
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
if [ "$1" == "reset" ]
then
telepresence quit -s
kubectl rollout restart -n ambassador deploy/traffic-manager
exit 0
fi
if [ $# -eq 1 ]
then
mode="native"
else
mode=$2
fi
if [ $# -eq 2 ] && [ "$1" == "cluster" ] && [ "$2" == "clean" ]
then
echo "Removing all development modifications from remote cluster..."
cleanCluster
exit 0
fi
if [ $# -eq 2 ] && [ "$2" == "pause" ]
then
case $1 in
"backend")
kubectl set image -n stackspin deploy/dashboard-backend backend=docker.io/rancher/mirrored-pause:3.6
exit 0
;;
*)
echo "Unknown pause command: $1 $2"
exit 4
esac
fi
if [ $# -eq 2 ] && [ "$2" == "setup" ]
then
case $1 in
"backend")
setupBackend
exit 0
;;
"frontend")
setupFrontend
exit 0
;;
"cluster")
setupCluster
exit 0
;;
*)
echo "Unknown setup command: $1 $2"
exit 4
esac
fi
case $mode in
"docker")
prepareDocker
;;
"native")
prepareNative
;;
*)
echo "Unknown mode (should be docker or native): $mode"
exit 4
esac
case $1 in
"backend")
runBackend
;;
"frontend")
runFrontend
;;
*)
echo "Unknown command: $1"
exit 4
esac