Skip to content

Commit 179ab83

Browse files
committed
[supervisor] active client notifications
1 parent 697c103 commit 179ab83

File tree

30 files changed

+715
-1473
lines changed

30 files changed

+715
-1473
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/gitpod-cli/cmd/docs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var docsCmd = &cobra.Command{
1515
Use: "docs",
1616
Short: "Open Gitpod Documentation in default browser",
1717
RunE: func(cmd *cobra.Command, args []string) error {
18-
return openPreview("GP_EXTERNAL_BROWSER", DocsUrl)
18+
return openPreview(cmd.Context(), DocsUrl, true)
1919
},
2020
}
2121

components/gitpod-cli/cmd/open.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ package cmd
77
import (
88
"os"
99
"os/exec"
10-
"time"
10+
"path/filepath"
1111

1212
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
13-
14-
"context"
13+
"github.com/gitpod-io/gitpod/supervisor/api"
1514

1615
"github.com/google/shlex"
1716
"github.com/spf13/cobra"
@@ -24,25 +23,48 @@ var openCmd = &cobra.Command{
2423
Short: "Opens a file in Gitpod",
2524
Args: cobra.MinimumNArgs(1),
2625
RunE: func(cmd *cobra.Command, args []string) error {
27-
// TODO(ak) use NotificationService.NotifyActive supervisor API instead
28-
29-
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
30-
defer cancel()
26+
ctx := cmd.Context()
3127

3228
client, err := supervisor.New(ctx)
3329
if err != nil {
3430
return err
3531
}
3632
defer client.Close()
37-
3833
client.WaitForIDEReady(ctx)
3934

4035
wait, _ := cmd.Flags().GetBool("wait")
4136

4237
pcmd := os.Getenv("GP_OPEN_EDITOR")
4338
if pcmd == "" {
44-
return xerrors.Errorf("GP_OPEN_EDITOR is not set")
39+
var paths []string
40+
for _, path := range args {
41+
absPath, err := filepath.Abs(path)
42+
if err == nil {
43+
path = absPath
44+
}
45+
paths = append(paths, path)
46+
}
47+
48+
resp, err := client.Notification.Notify(ctx, &api.NotifyRequest{
49+
Open: &api.NotifyRequest_Open{
50+
Paths: paths,
51+
Await: wait,
52+
},
53+
Active: true,
54+
})
55+
if err != nil {
56+
return err
57+
}
58+
if resp.Command == nil {
59+
return nil
60+
}
61+
c := exec.CommandContext(cmd.Context(), resp.Command.Cmd, resp.Command.Args...)
62+
c.Stdin = os.Stdin
63+
c.Stdout = os.Stdout
64+
c.Stderr = os.Stderr
65+
return c.Run()
4566
}
67+
// TODO: backward compatibilty, remove when all IDEs are updated
4668
pargs, err := shlex.Split(pcmd)
4769
if err != nil {
4870
return xerrors.Errorf("cannot parse GP_OPEN_EDITOR: %w", err)

components/gitpod-cli/cmd/preview.go

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"regexp"
1212
"strconv"
1313
"strings"
14-
"time"
1514

1615
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
16+
"github.com/gitpod-io/gitpod/supervisor/api"
1717
"github.com/google/shlex"
1818
"github.com/spf13/cobra"
1919
"golang.org/x/xerrors"
@@ -31,34 +31,48 @@ var previewCmd = &cobra.Command{
3131
Short: "Opens a URL in the IDE's preview",
3232
Args: cobra.ExactArgs(1),
3333
RunE: func(cmd *cobra.Command, args []string) error {
34-
// TODO(ak) use NotificationService.NotifyActive supervisor API instead
34+
return openPreview(cmd.Context(), args[0], previewCmdOpts.External)
35+
},
36+
}
3537

36-
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
37-
defer cancel()
38-
client, err := supervisor.New(ctx)
38+
func openPreview(ctx context.Context, url string, external bool) error {
39+
client, err := supervisor.New(ctx)
40+
if err != nil {
41+
return err
42+
}
43+
defer client.Close()
44+
client.WaitForIDEReady(ctx)
45+
46+
url = replaceLocalhostInURL(url)
47+
gpBrowserEnvVar := "GP_PREVIEW_BROWSER"
48+
if external {
49+
gpBrowserEnvVar = "GP_EXTERNAL_BROWSER"
50+
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
51+
url = "https://" + url
52+
}
53+
}
54+
pcmd := os.Getenv(gpBrowserEnvVar)
55+
if pcmd == "" {
56+
resp, err := client.Notification.Notify(ctx, &api.NotifyRequest{
57+
Preview: &api.NotifyRequest_Preview{
58+
Url: url,
59+
External: external,
60+
},
61+
Active: true,
62+
})
3963
if err != nil {
4064
return err
4165
}
42-
defer client.Close()
43-
44-
client.WaitForIDEReady(ctx)
45-
46-
gpBrowserEnvVar := "GP_PREVIEW_BROWSER"
47-
48-
url := replaceLocalhostInURL(args[0])
49-
if previewCmdOpts.External {
50-
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
51-
url = "https://" + url
52-
}
53-
gpBrowserEnvVar = "GP_EXTERNAL_BROWSER"
66+
if resp.Command == nil {
67+
return nil
5468
}
55-
56-
return openPreview(gpBrowserEnvVar, url)
57-
},
58-
}
59-
60-
func openPreview(gpBrowserEnvVar string, url string) error {
61-
pcmd := os.Getenv(gpBrowserEnvVar)
69+
c := exec.CommandContext(ctx, resp.Command.Cmd, resp.Command.Args...)
70+
c.Stdin = os.Stdin
71+
c.Stdout = os.Stdout
72+
c.Stderr = os.Stderr
73+
return c.Run()
74+
}
75+
// TODO: backward compatibilty, remove when all IDEs are updated
6276
if pcmd == "" {
6377
return xerrors.Errorf("%s is not set", gpBrowserEnvVar)
6478
}

components/gitpod-cli/cmd/rebuild.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ func runRebuild(ctx context.Context, supervisorClient *supervisor.SupervisorClie
262262
{Source: "/workspace"},
263263
{Source: "/.supervisor"},
264264
{Source: "/ide"},
265+
{Source: "/ide-desktop"},
266+
{Source: "/ide-desktop-plugins"},
265267
{Source: "/workspace/.gitpod-debug/.docker-root", Target: "/workspace/.docker-root", Permission: 0710},
266268
{Source: "/workspace/.gitpod-debug/.gitpod", Target: "/workspace/.gitpod", Permission: 0751},
267269
{Source: "/workspace/.gitpod-debug/.vscode-remote", Target: "/workspace/.vscode-remote", Permission: 0751},

components/gitpod-cli/hot-swap.sh

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,53 @@
11
#!/bin/bash
2-
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
# Copyright (c) 2023 Gitpod GmbH. All rights reserved.
33
# Licensed under the GNU Affero General Public License (AGPL).
44
# See License.AGPL.txt in the project root for license information.
55

66
set -Eeuo pipefail
77

88
component=${PWD##*/}
9-
workspaceUrl=$(echo "${1}" |sed -e "s/\/$//")
10-
echo "URL: $workspaceUrl"
119

12-
workspaceDesc=$(gpctl workspaces describe "$workspaceUrl" -o=json)
10+
workspaceURL=${1-}
11+
[ -z "$workspaceURL" ] && echo "Please provide a workspace URL as first argument." && exit 1
12+
workspaceURL=$(echo "${1}" |sed -e "s/\/$//")
13+
echo "Workspace URL: $workspaceURL"
1314

14-
podName=$(echo "$workspaceDesc" | jq .runtime.pod_name -r)
15-
echo "Pod: $podName"
15+
workspaceHost=${workspaceURL//https:\/\//}
16+
echo "Workspace Host: $workspaceHost"
1617

17-
workspaceId=$(echo "$workspaceDesc" | jq .metadata.meta_id -r)
18-
echo "ID: $workspaceId"
18+
workspaceID=$(echo "${workspaceHost}" | cut -d. -f1)
19+
echo "Workspace ID: $workspaceID"
1920

20-
clusterHost=$(kubectl exec -it "$podName" -- printenv GITPOD_WORKSPACE_CLUSTER_HOST |sed -e "s/\s//g")
21+
clusterHost=${workspaceHost//$workspaceID./}
2122
echo "Cluster Host: $clusterHost"
2223

23-
# prepare ssh
24-
ownerToken=$(kubectl get pod "$podName" -o=json | jq ".metadata.annotations.\"gitpod\/ownerToken\"" -r)
24+
devClusterHost=$(gp info --json |jq .cluster_host -r)
25+
echo "Dev Cluster Host: $devClusterHost"
26+
27+
preview=true
28+
if [[ $clusterHost = "$devClusterHost" ]]
29+
then
30+
preview=false
31+
fi
32+
echo "Preview Env: $preview"
33+
34+
# prepare ssh config
2535
sshConfig=$(mktemp)
26-
echo "Host $workspaceId" > "$sshConfig"
27-
echo " Hostname \"$workspaceId.ssh.$clusterHost\"" >> "$sshConfig"
28-
echo " User \"$workspaceId#$ownerToken\"" >> "$sshConfig"
36+
echo "Host $workspaceID" > "$sshConfig"
37+
echo " Hostname \"$workspaceID.ssh.$clusterHost\"" >> "$sshConfig"
38+
if [ $preview = "true" ]
39+
then
40+
workspaceDesc=$(gpctl workspaces describe "$workspaceURL" -o=json)
41+
42+
podName=$(echo "$workspaceDesc" | jq .runtime.pod_name -r)
43+
echo "Workspace Pod: $podName"
44+
45+
ownerToken=$(kubectl get pod "$podName" -o=json | jq ".metadata.annotations.\"gitpod\/ownerToken\"" -r)
46+
echo " User \"$workspaceID#$ownerToken\"" >> "$sshConfig"
47+
else
48+
# assume SSH keys configured via .dotfiles
49+
echo " User \"$workspaceID\"" >> "$sshConfig"
50+
fi
2951

3052
# build
3153
go build .
@@ -34,7 +56,7 @@ echo "$component built"
3456
# upload
3557
uploadDest="/.supervisor/$component"
3658
echo "Upload Dest: $uploadDest"
37-
ssh -F "$sshConfig" "$workspaceId" "sudo chown -R gitpod:gitpod /.supervisor && rm $uploadDest 2> /dev/null"
59+
ssh -F "$sshConfig" "$workspaceID" "sudo chown -R gitpod:gitpod /.supervisor && rm $uploadDest 2> /dev/null"
3860
echo "Permissions granted"
39-
scp -F "$sshConfig" -r "./$component" "$workspaceId":"$uploadDest"
61+
scp -F "$sshConfig" -r "./$component" "$workspaceID":"$uploadDest"
4062
echo "Swap complete"

components/ide/code/leeway.Dockerfile

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,9 @@ FROM scratch
9999
COPY --from=code_builder --chown=33333:33333 /vscode-web/ /ide/
100100
COPY --from=code_builder --chown=33333:33333 /vscode-reh-linux-x64/ /ide/
101101

102+
# TODO(ak) get rid of it as well
102103
ENV GITPOD_ENV_APPEND_PATH=/ide/bin/remote-cli:
103104

104-
# editor config
105-
ENV GITPOD_ENV_SET_EDITOR=/ide/bin/remote-cli/gitpod-code
106-
ENV GITPOD_ENV_SET_VISUAL="$GITPOD_ENV_SET_EDITOR"
107-
ENV GITPOD_ENV_SET_GP_OPEN_EDITOR="$GITPOD_ENV_SET_EDITOR"
108-
ENV GITPOD_ENV_SET_GIT_EDITOR="$GITPOD_ENV_SET_EDITOR --wait"
109-
ENV GITPOD_ENV_SET_GP_PREVIEW_BROWSER="/ide/bin/remote-cli/gitpod-code --preview"
110-
ENV GITPOD_ENV_SET_GP_EXTERNAL_BROWSER="/ide/bin/remote-cli/gitpod-code --openExternal"
111-
112105
ARG CODE_VERSION
113106
ARG CODE_COMMIT
114107
LABEL "io.gitpod.ide.version"=$CODE_VERSION

components/ide/jetbrains/backend-plugin/hot-swap.sh

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,54 @@ set -Eeuo pipefail
77

88
# This script builds the backend plugin, replaces the backend plugin on a running workspace and restarts the JB backend.
99

10-
workspaceUrl=${1-}
11-
[ -z "$workspaceUrl" ] && echo "Please provide a workspace URL as first argument." && exit 1
12-
workspaceUrl=$(echo "$workspaceUrl" |sed -e "s/\/$//")
13-
echo "URL: $workspaceUrl"
10+
workspaceURL=${1-}
11+
[ -z "$workspaceURL" ] && echo "Please provide a workspace URL as first argument." && exit 1
12+
workspaceURL=$(echo "$workspaceURL" |sed -e "s/\/$//")
13+
echo "Workspace URL: $workspaceURL"
1414

15-
workspaceDesc=$(gpctl workspaces describe "$workspaceUrl" -o=json)
15+
workspaceHost=${workspaceURL//https:\/\//}
16+
echo "Workspace Host: $workspaceHost"
1617

17-
podName=$(echo "$workspaceDesc" | jq .runtime.pod_name -r)
18-
echo "Pod: $podName"
18+
workspaceID=$(echo "${workspaceHost}" | cut -d. -f1)
19+
echo "Workspace ID: $workspaceID"
1920

20-
workspaceId=$(echo "$workspaceDesc" | jq .metadata.meta_id -r)
21-
echo "ID: $workspaceId"
22-
23-
clusterHost=$(kubectl exec -it "$podName" -- printenv GITPOD_WORKSPACE_CLUSTER_HOST |sed -e "s/\s//g")
21+
clusterHost=${workspaceHost//$workspaceID./}
2422
echo "Cluster Host: $clusterHost"
2523

26-
qualifier=$(kubectl exec -it "$podName" -- printenv JETBRAINS_BACKEND_QUALIFIER |sed -e "s/\s//g")
24+
devClusterHost=$(gp info --json |jq .cluster_host -r)
25+
echo "Dev Cluster Host: $devClusterHost"
26+
27+
preview=true
28+
if [[ $clusterHost = "$devClusterHost" ]]
29+
then
30+
preview=false
31+
fi
32+
echo "Preview Env: $preview"
33+
34+
product=${2-intellij}
35+
qualifier=${3-latest}
36+
37+
# prepare ssh config
38+
sshConfig=$(mktemp)
39+
echo "Host $workspaceID" > "$sshConfig"
40+
echo " Hostname \"$workspaceID.ssh.$clusterHost\"" >> "$sshConfig"
41+
if [ $preview = "true" ]
42+
then
43+
workspaceDesc=$(gpctl workspaces describe "$workspaceURL" -o=json)
44+
45+
podName=$(echo "$workspaceDesc" | jq .runtime.pod_name -r)
46+
echo "Workspace Pod: $podName"
47+
48+
qualifier=$(kubectl exec -it "$podName" -- printenv JETBRAINS_BACKEND_QUALIFIER |sed -e "s/\s//g")
49+
50+
ownerToken=$(kubectl get pod "$podName" -o=json | jq ".metadata.annotations.\"gitpod\/ownerToken\"" -r)
51+
echo " User \"$workspaceID#$ownerToken\"" >> "$sshConfig"
52+
else
53+
# assume SSH keys configured via .dotfiles
54+
echo " User \"$workspaceID\"" >> "$sshConfig"
55+
fi
56+
57+
echo "Product: $product"
2758
echo "Version Qualifier: $qualifier"
2859

2960
# prepare build
@@ -32,13 +63,6 @@ tarDir="/tmp/hot-swap/$component"
3263
mkdir -p "$tarDir"
3364
echo "Build Dir: $tarDir"
3465

35-
# prepare ssh
36-
ownerToken=$(kubectl get pod "$podName" -o=json | jq ".metadata.annotations.\"gitpod\/ownerToken\"" -r)
37-
sshConfig="$tarDir/ssh-config"
38-
echo "Host $workspaceId" > "$sshConfig"
39-
echo " Hostname \"$workspaceId.ssh.$clusterHost\"" >> "$sshConfig"
40-
echo " User \"$workspaceId#$ownerToken\"" >> "$sshConfig"
41-
4266
# build
4367
tarFile="$tarDir/build.tar.gz"
4468
leeway build -DnoVerifyJBPlugin=true .:"plugin-$qualifier" --save "$tarFile"
@@ -47,16 +71,20 @@ tar -xf "$tarFile" -C "$tarDir"
4771
# upload
4872
uploadDest="/ide-desktop-plugins/$component"
4973
echo "Upload Dest: $uploadDest"
50-
scp -F "$sshConfig" -r "$tarDir/build/gitpod-remote" "$workspaceId":"$uploadDest"
74+
scp -F "$sshConfig" -r "$tarDir/build/gitpod-remote" "$workspaceID":"$uploadDest"
5175

5276
# link
53-
link="/ide-desktop/backend/plugins/gitpod-remote"
54-
ssh -F "$sshConfig" "$workspaceId" ln -sfn "$uploadDest" "$link"
77+
link="/ide-desktop/$product/backend/plugins/gitpod-remote"
78+
if [ "$qualifier" = "latest" ]
79+
then
80+
link="/ide-desktop/$product-$qualifier/backend/plugins/gitpod-remote"
81+
fi
82+
ssh -F "$sshConfig" "$workspaceID" ln -sfn "$uploadDest" "$link"
5583
echo "Link: $link -> $uploadDest"
5684

5785
# restart
58-
ssh -F "$sshConfig" "$workspaceId" curl http://localhost:24000/restart
59-
echo "Restarted: please reconenct to JB backend to try new changes."
86+
ssh -F "$sshConfig" "$workspaceID" curl http://localhost:24000/restart
87+
echo "Restarted: please reconnect to JB backend to try new changes."
6088

6189
# clean up
6290
rm -rf "$tarDir"

components/ide/jetbrains/backend-plugin/launch-dev-server.sh

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,6 @@ export IJ_HOST_SYSTEM_BASE_DIR=/workspace/.cache/JetBrains
8080
# Enable host status endpoint
8181
export CWM_HOST_STATUS_OVER_HTTP_TOKEN=gitpod
8282

83-
# Build and move idea-cli, then overwrite environment variables initially defined by `components/ide/jetbrains/image/leeway.Dockerfile`
84-
# Note: IDEA_CLI_DEV_PATH path needs to be the same string used in components/ide/jetbrains/cli/cmd/root.go
85-
IDEA_CLI_DEV_PATH=/ide-desktop/bin/idea-cli-dev
86-
(cd ../cli && go build -o $IDEA_CLI_DEV_PATH)
87-
export EDITOR="$IDEA_CLI_DEV_PATH open"
88-
export VISUAL="$EDITOR"
89-
export GP_OPEN_EDITOR="$EDITOR"
90-
export GIT_EDITOR="$EDITOR --wait"
91-
export GP_PREVIEW_BROWSER="$IDEA_CLI_DEV_PATH preview"
92-
export GP_EXTERNAL_BROWSER="$IDEA_CLI_DEV_PATH preview"
93-
9483
export JETBRAINS_GITPOD_BACKEND_KIND=intellij
9584

9685
$TEST_BACKEND_DIR/bin/remote-dev-server.sh run "$TEST_DIR"

0 commit comments

Comments
 (0)