Skip to content

[supervisor] active client notifications #16056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion components/gitpod-cli/cmd/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var docsCmd = &cobra.Command{
Use: "docs",
Short: "Open Gitpod Documentation in default browser",
RunE: func(cmd *cobra.Command, args []string) error {
return openPreview("GP_EXTERNAL_BROWSER", DocsUrl)
return openPreview(cmd.Context(), DocsUrl, true)
},
}

Expand Down
40 changes: 31 additions & 9 deletions components/gitpod-cli/cmd/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ package cmd
import (
"os"
"os/exec"
"time"
"path/filepath"

"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"

"context"
"github.com/gitpod-io/gitpod/supervisor/api"

"github.com/google/shlex"
"github.com/spf13/cobra"
Expand All @@ -24,25 +23,48 @@ var openCmd = &cobra.Command{
Short: "Opens a file in Gitpod",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// TODO(ak) use NotificationService.NotifyActive supervisor API instead

ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
defer cancel()
ctx := cmd.Context()

client, err := supervisor.New(ctx)
if err != nil {
return err
}
defer client.Close()

client.WaitForIDEReady(ctx)

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

pcmd := os.Getenv("GP_OPEN_EDITOR")
if pcmd == "" {
return xerrors.Errorf("GP_OPEN_EDITOR is not set")
var paths []string
for _, path := range args {
absPath, err := filepath.Abs(path)
if err == nil {
path = absPath
}
paths = append(paths, path)
}

resp, err := client.Notification.Notify(ctx, &api.NotifyRequest{
Open: &api.NotifyRequest_Open{
Paths: paths,
Await: wait,
},
Active: true,
})
if err != nil {
return err
}
if resp.Command == nil {
return nil
}
c := exec.CommandContext(cmd.Context(), resp.Command.Cmd, resp.Command.Args...)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
}
// TODO: backward compatibilty, remove when all IDEs are updated
pargs, err := shlex.Split(pcmd)
if err != nil {
return xerrors.Errorf("cannot parse GP_OPEN_EDITOR: %w", err)
Expand Down
62 changes: 38 additions & 24 deletions components/gitpod-cli/cmd/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"regexp"
"strconv"
"strings"
"time"

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

ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second)
defer cancel()
client, err := supervisor.New(ctx)
func openPreview(ctx context.Context, url string, external bool) error {
client, err := supervisor.New(ctx)
if err != nil {
return err
}
defer client.Close()
client.WaitForIDEReady(ctx)

url = replaceLocalhostInURL(url)
gpBrowserEnvVar := "GP_PREVIEW_BROWSER"
if external {
gpBrowserEnvVar = "GP_EXTERNAL_BROWSER"
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
url = "https://" + url
}
}
pcmd := os.Getenv(gpBrowserEnvVar)
if pcmd == "" {
resp, err := client.Notification.Notify(ctx, &api.NotifyRequest{
Preview: &api.NotifyRequest_Preview{
Url: url,
External: external,
},
Active: true,
})
if err != nil {
return err
}
defer client.Close()

client.WaitForIDEReady(ctx)

gpBrowserEnvVar := "GP_PREVIEW_BROWSER"

url := replaceLocalhostInURL(args[0])
if previewCmdOpts.External {
if !strings.HasPrefix(url, "http://") && !strings.HasPrefix(url, "https://") {
url = "https://" + url
}
gpBrowserEnvVar = "GP_EXTERNAL_BROWSER"
if resp.Command == nil {
return nil
}

return openPreview(gpBrowserEnvVar, url)
},
}

func openPreview(gpBrowserEnvVar string, url string) error {
pcmd := os.Getenv(gpBrowserEnvVar)
c := exec.CommandContext(ctx, resp.Command.Cmd, resp.Command.Args...)
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
}
// TODO: backward compatibilty, remove when all IDEs are updated
if pcmd == "" {
return xerrors.Errorf("%s is not set", gpBrowserEnvVar)
}
Expand Down
2 changes: 2 additions & 0 deletions components/gitpod-cli/cmd/rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ func runRebuild(ctx context.Context, supervisorClient *supervisor.SupervisorClie
{Source: "/workspace"},
{Source: "/.supervisor"},
{Source: "/ide"},
{Source: "/ide-desktop"},
{Source: "/ide-desktop-plugins"},
{Source: "/workspace/.gitpod-debug/.docker-root", Target: "/workspace/.docker-root", Permission: 0710},
{Source: "/workspace/.gitpod-debug/.gitpod", Target: "/workspace/.gitpod", Permission: 0751},
{Source: "/workspace/.gitpod-debug/.vscode-remote", Target: "/workspace/.vscode-remote", Permission: 0751},
Expand Down
54 changes: 38 additions & 16 deletions components/gitpod-cli/hot-swap.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,53 @@
#!/bin/bash
# Copyright (c) 2022 Gitpod GmbH. All rights reserved.
# Copyright (c) 2023 Gitpod GmbH. All rights reserved.
# Licensed under the GNU Affero General Public License (AGPL).
# See License.AGPL.txt in the project root for license information.

set -Eeuo pipefail

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

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

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

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

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

# prepare ssh
ownerToken=$(kubectl get pod "$podName" -o=json | jq ".metadata.annotations.\"gitpod\/ownerToken\"" -r)
devClusterHost=$(gp info --json |jq .cluster_host -r)
echo "Dev Cluster Host: $devClusterHost"

preview=true
if [[ $clusterHost = "$devClusterHost" ]]
then
preview=false
fi
echo "Preview Env: $preview"

# prepare ssh config
sshConfig=$(mktemp)
echo "Host $workspaceId" > "$sshConfig"
echo " Hostname \"$workspaceId.ssh.$clusterHost\"" >> "$sshConfig"
echo " User \"$workspaceId#$ownerToken\"" >> "$sshConfig"
echo "Host $workspaceID" > "$sshConfig"
echo " Hostname \"$workspaceID.ssh.$clusterHost\"" >> "$sshConfig"
if [ $preview = "true" ]
then
workspaceDesc=$(gpctl workspaces describe "$workspaceURL" -o=json)

podName=$(echo "$workspaceDesc" | jq .runtime.pod_name -r)
echo "Workspace Pod: $podName"

ownerToken=$(kubectl get pod "$podName" -o=json | jq ".metadata.annotations.\"gitpod\/ownerToken\"" -r)
echo " User \"$workspaceID#$ownerToken\"" >> "$sshConfig"
else
# assume SSH keys configured via .dotfiles
echo " User \"$workspaceID\"" >> "$sshConfig"
fi

# build
go build .
Expand All @@ -34,7 +56,7 @@ echo "$component built"
# upload
uploadDest="/.supervisor/$component"
echo "Upload Dest: $uploadDest"
ssh -F "$sshConfig" "$workspaceId" "sudo chown -R gitpod:gitpod /.supervisor && rm $uploadDest 2> /dev/null"
ssh -F "$sshConfig" "$workspaceID" "sudo chown -R gitpod:gitpod /.supervisor && rm $uploadDest 2> /dev/null"
echo "Permissions granted"
scp -F "$sshConfig" -r "./$component" "$workspaceId":"$uploadDest"
scp -F "$sshConfig" -r "./$component" "$workspaceID":"$uploadDest"
echo "Swap complete"
9 changes: 1 addition & 8 deletions components/ide/code/leeway.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,9 @@ FROM scratch
COPY --from=code_builder --chown=33333:33333 /vscode-web/ /ide/
COPY --from=code_builder --chown=33333:33333 /vscode-reh-linux-x64/ /ide/

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

# editor config
ENV GITPOD_ENV_SET_EDITOR=/ide/bin/remote-cli/gitpod-code
ENV GITPOD_ENV_SET_VISUAL="$GITPOD_ENV_SET_EDITOR"
ENV GITPOD_ENV_SET_GP_OPEN_EDITOR="$GITPOD_ENV_SET_EDITOR"
ENV GITPOD_ENV_SET_GIT_EDITOR="$GITPOD_ENV_SET_EDITOR --wait"
ENV GITPOD_ENV_SET_GP_PREVIEW_BROWSER="/ide/bin/remote-cli/gitpod-code --preview"
ENV GITPOD_ENV_SET_GP_EXTERNAL_BROWSER="/ide/bin/remote-cli/gitpod-code --openExternal"

ARG CODE_VERSION
ARG CODE_COMMIT
LABEL "io.gitpod.ide.version"=$CODE_VERSION
Expand Down
76 changes: 52 additions & 24 deletions components/ide/jetbrains/backend-plugin/hot-swap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,54 @@ set -Eeuo pipefail

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

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

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

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

workspaceId=$(echo "$workspaceDesc" | jq .metadata.meta_id -r)
echo "ID: $workspaceId"

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

qualifier=$(kubectl exec -it "$podName" -- printenv JETBRAINS_BACKEND_QUALIFIER |sed -e "s/\s//g")
devClusterHost=$(gp info --json |jq .cluster_host -r)
echo "Dev Cluster Host: $devClusterHost"

preview=true
if [[ $clusterHost = "$devClusterHost" ]]
then
preview=false
fi
echo "Preview Env: $preview"

product=${2-intellij}
qualifier=${3-latest}

# prepare ssh config
sshConfig=$(mktemp)
echo "Host $workspaceID" > "$sshConfig"
echo " Hostname \"$workspaceID.ssh.$clusterHost\"" >> "$sshConfig"
if [ $preview = "true" ]
then
workspaceDesc=$(gpctl workspaces describe "$workspaceURL" -o=json)

podName=$(echo "$workspaceDesc" | jq .runtime.pod_name -r)
echo "Workspace Pod: $podName"

qualifier=$(kubectl exec -it "$podName" -- printenv JETBRAINS_BACKEND_QUALIFIER |sed -e "s/\s//g")

ownerToken=$(kubectl get pod "$podName" -o=json | jq ".metadata.annotations.\"gitpod\/ownerToken\"" -r)
echo " User \"$workspaceID#$ownerToken\"" >> "$sshConfig"
else
# assume SSH keys configured via .dotfiles
echo " User \"$workspaceID\"" >> "$sshConfig"
fi

echo "Product: $product"
echo "Version Qualifier: $qualifier"

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

# prepare ssh
ownerToken=$(kubectl get pod "$podName" -o=json | jq ".metadata.annotations.\"gitpod\/ownerToken\"" -r)
sshConfig="$tarDir/ssh-config"
echo "Host $workspaceId" > "$sshConfig"
echo " Hostname \"$workspaceId.ssh.$clusterHost\"" >> "$sshConfig"
echo " User \"$workspaceId#$ownerToken\"" >> "$sshConfig"

# build
tarFile="$tarDir/build.tar.gz"
leeway build -DnoVerifyJBPlugin=true .:"plugin-$qualifier" --save "$tarFile"
Expand All @@ -47,16 +71,20 @@ tar -xf "$tarFile" -C "$tarDir"
# upload
uploadDest="/ide-desktop-plugins/$component"
echo "Upload Dest: $uploadDest"
scp -F "$sshConfig" -r "$tarDir/build/gitpod-remote" "$workspaceId":"$uploadDest"
scp -F "$sshConfig" -r "$tarDir/build/gitpod-remote" "$workspaceID":"$uploadDest"

# link
link="/ide-desktop/backend/plugins/gitpod-remote"
ssh -F "$sshConfig" "$workspaceId" ln -sfn "$uploadDest" "$link"
link="/ide-desktop/$product/backend/plugins/gitpod-remote"
if [ "$qualifier" = "latest" ]
then
link="/ide-desktop/$product-$qualifier/backend/plugins/gitpod-remote"
fi
ssh -F "$sshConfig" "$workspaceID" ln -sfn "$uploadDest" "$link"
echo "Link: $link -> $uploadDest"

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

# clean up
rm -rf "$tarDir"
11 changes: 0 additions & 11 deletions components/ide/jetbrains/backend-plugin/launch-dev-server.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,6 @@ export IJ_HOST_SYSTEM_BASE_DIR=/workspace/.cache/JetBrains
# Enable host status endpoint
export CWM_HOST_STATUS_OVER_HTTP_TOKEN=gitpod

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

export JETBRAINS_GITPOD_BACKEND_KIND=intellij

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