Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ide/jetbrains/backend-plugin/remote-debug.sh
2500 views
1
#!/bin/bash
2
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3
# Licensed under the GNU Affero General Public License (AGPL).
4
# See License.AGPL.txt in the project root for license information.
5
6
# This script configure remote debugging in a workspace running in a preview environment.
7
# It updates VM options with remote debug agent, restart the JB backend to apply them,
8
# and start port forwarding of the remote debug port. You can configure `Remote JVM Debug`
9
# run configuration using the forwarded port.
10
#
11
# ./remote-debug.sh <workspaceUrl> (<localPort>)?
12
13
workspaceUrl=${1-}
14
[ -z "$workspaceUrl" ] && echo "Please provide a workspace URL as first argument." && exit 1
15
workspaceUrl=$(echo "$workspaceUrl" |sed -e "s/\/$//")
16
echo "URL: $workspaceUrl"
17
18
workspaceDesc=$(gpctl workspaces describe "$workspaceUrl" -o=json)
19
20
podName=$(echo "$workspaceDesc" | jq .runtime.pod_name -r)
21
echo "Pod: $podName"
22
23
workspaceId=$(echo "$workspaceDesc" | jq .metadata.meta_id -r)
24
echo "ID: $workspaceId"
25
26
clusterHost=$(kubectl exec -it "$podName" -- printenv GITPOD_WORKSPACE_CLUSTER_HOST |sed -e "s/\s//g")
27
echo "Cluster Host: $clusterHost"
28
29
# prepare ssh
30
ownerToken=$(kubectl get pod "$podName" -o=json | jq ".metadata.annotations.\"gitpod\/ownerToken\"" -r)
31
sshConfig="/tmp/$workspaceId-ssh-config"
32
echo "Host $workspaceId" > "$sshConfig"
33
echo " Hostname \"$workspaceId.ssh.$clusterHost\"" >> "$sshConfig"
34
echo " User \"$workspaceId#$ownerToken\"" >> "$sshConfig"
35
36
while true
37
do
38
# configure remote debugging
39
remotePort=$(ssh -F "$sshConfig" "$workspaceId" curl http://localhost:24000/debug)
40
if [ -n "$remotePort" ]; then
41
localPort=${2-$remotePort}
42
# forward
43
echo "Forwarding Debug Port: $localPort -> $remotePort"
44
ssh -F "$sshConfig" -L "$remotePort:localhost:$localPort" "$workspaceId" -N
45
fi
46
47
sleep 1
48
done
49
50