Skip to content
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

[gp-cli] allow to create snapshot #7097

Merged
merged 1 commit into from
Dec 7, 2021
Merged
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
78 changes: 78 additions & 0 deletions components/gitpod-cli/cmd/snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2020 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.

package cmd

import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"time"

gitpod "github.com/gitpod-io/gitpod/gitpod-cli/pkg/gitpod"
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
"github.com/sourcegraph/jsonrpc2"
"github.com/spf13/cobra"
)

const (
ErrorCodeSnapshotNotFound = 404
ErrorCodeSnapshotError = 630
)

// snapshotCmd represents the snapshotCmd command
var snapshotCmd = &cobra.Command{
Use: "snapshot",
Short: "Take a snapshot of the current workspace",
Args: cobra.ArbitraryArgs,
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := context.WithCancel(context.Background())
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigChan
cancel()
}()
wsInfo, err := gitpod.GetWSInfo(ctx)
if err != nil {
fail(err.Error())
}
client, err := gitpod.ConnectToServer(ctx, wsInfo, []string{
"function:takeSnapshot",
"function:waitForSnapshot",
"resource:workspace::" + wsInfo.WorkspaceId + "::get/update",
})
if err != nil {
fail(err.Error())
}
snapshotId, err := client.TakeSnapshot(ctx, &protocol.TakeSnapshotOptions{
WorkspaceID: wsInfo.WorkspaceId,
DontWait: true,
})
if err != nil {
fail(err.Error())
}
for ctx.Err() == nil {
err := client.WaitForSnapshot(ctx, snapshotId)
akosyakov marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
var responseErr *jsonrpc2.Error
if errors.As(err, &responseErr) && (responseErr.Code == ErrorCodeSnapshotNotFound || responseErr.Code == ErrorCodeSnapshotError) {
panic(err)
}
time.Sleep(time.Second * 3)
} else {
break
}
}
url := fmt.Sprintf("%s/#snapshot/%s", wsInfo.GitpodHost, snapshotId)
akosyakov marked this conversation as resolved.
Show resolved Hide resolved
fmt.Println(url)
},
}

func init() {
rootCmd.AddCommand(snapshotCmd)
}
12 changes: 4 additions & 8 deletions components/gitpod-protocol/go/gitpod-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type APIInterface interface {
SendFeedback(ctx context.Context, feedback string) (res string, err error)
RegisterGithubApp(ctx context.Context, installationID string) (err error)
TakeSnapshot(ctx context.Context, options *TakeSnapshotOptions) (res string, err error)
WaitForSnapshot(ctx context.Context, options *WaitForSnapshotOptions) (err error)
WaitForSnapshot(ctx context.Context, snapshotId string) (err error)
GetSnapshots(ctx context.Context, workspaceID string) (res []*string, err error)
StoreLayout(ctx context.Context, workspaceID string, layoutData string) (err error)
GetLayout(ctx context.Context, workspaceID string) (res string, err error)
Expand Down Expand Up @@ -1279,14 +1279,14 @@ func (gp *APIoverJSONRPC) TakeSnapshot(ctx context.Context, options *TakeSnapsho
}

// WaitForSnapshot calls waitForSnapshot on the server
func (gp *APIoverJSONRPC) WaitForSnapshot(ctx context.Context, options *WaitForSnapshotOptions) (err error) {
func (gp *APIoverJSONRPC) WaitForSnapshot(ctx context.Context, snapshotId string) (err error) {
if gp == nil {
err = errNotConnected
return
}
var _params []interface{}

_params = append(_params, options)
_params = append(_params, snapshotId)

var result string
err = gp.C.Call(ctx, "waitForSnapshot", _params, &result)
Expand Down Expand Up @@ -1939,11 +1939,7 @@ type GenerateNewGitpodTokenOptions struct {
type TakeSnapshotOptions struct {
LayoutData string `json:"layoutData,omitempty"`
WorkspaceID string `json:"workspaceId,omitempty"`
}

// WaitForSnapshotOptions is the WaitForSnapshotOptions message type
type WaitForSnapshotOptions struct {
SnapshotID string `json:"snapshotID,omitempty"`
DontWait bool `json:"dontWait",omitempty`
}

// PreparePluginUploadParams is the PreparePluginUploadParams message type
Expand Down
4 changes: 2 additions & 2 deletions components/gitpod-protocol/go/mock.go

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