Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-cli/pkg/supervisor/client.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package supervisor
6
7
import (
8
"context"
9
"sync"
10
"time"
11
12
"golang.org/x/xerrors"
13
"google.golang.org/grpc"
14
"google.golang.org/grpc/credentials/insecure"
15
16
"github.com/gitpod-io/gitpod/common-go/util"
17
"github.com/gitpod-io/gitpod/supervisor/api"
18
)
19
20
type SupervisorClient struct {
21
conn *grpc.ClientConn
22
closeOnce sync.Once
23
24
Status api.StatusServiceClient
25
Terminal api.TerminalServiceClient
26
Info api.InfoServiceClient
27
Notification api.NotificationServiceClient
28
Control api.ControlServiceClient
29
Token api.TokenServiceClient
30
}
31
32
type SupervisorClientOption struct {
33
Address string
34
}
35
36
func New(ctx context.Context, options ...*SupervisorClientOption) (*SupervisorClient, error) {
37
address := util.GetSupervisorAddress()
38
for _, option := range options {
39
if option.Address != "" {
40
address = option.Address
41
}
42
}
43
conn, err := grpc.DialContext(ctx, address, grpc.WithTransportCredentials(insecure.NewCredentials()))
44
if err != nil {
45
return nil, xerrors.Errorf("failed connecting to supervisor: %w", err)
46
}
47
48
return &SupervisorClient{
49
conn: conn,
50
Status: api.NewStatusServiceClient(conn),
51
Terminal: api.NewTerminalServiceClient(conn),
52
Info: api.NewInfoServiceClient(conn),
53
Notification: api.NewNotificationServiceClient(conn),
54
Control: api.NewControlServiceClient(conn),
55
Token: api.NewTokenServiceClient(conn),
56
}, nil
57
}
58
59
func (client *SupervisorClient) Close() {
60
client.closeOnce.Do(func() {
61
client.conn.Close()
62
})
63
}
64
65
func (client *SupervisorClient) WaitForIDEReady(ctx context.Context) {
66
var ideReady bool
67
for !ideReady {
68
resp, _ := client.Status.IDEStatus(ctx, &api.IDEStatusRequest{Wait: true})
69
if resp != nil {
70
ideReady = resp.Ok
71
}
72
if ctx.Err() != nil {
73
return
74
}
75
if !ideReady {
76
time.Sleep(1 * time.Second)
77
}
78
}
79
}
80
81