-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworkspace.go
More file actions
221 lines (189 loc) · 6.96 KB
/
workspace.go
File metadata and controls
221 lines (189 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package api
import (
"fmt"
"github.com/codesphere-cloud/cs-go/api/errors"
"github.com/codesphere-cloud/cs-go/api/openapi_client"
"time"
)
func (c *Client) ListWorkspaces(teamId int) ([]Workspace, error) {
workspaces, r, err := c.api.WorkspacesAPI.WorkspacesListWorkspaces(c.ctx, float32(teamId)).Execute()
return workspaces, errors.FormatAPIError(r, err)
}
func (c *Client) GetWorkspace(workspaceId int) (Workspace, error) {
workspace, r, err := c.api.WorkspacesAPI.WorkspacesGetWorkspace(c.ctx, float32(workspaceId)).Execute()
if workspace != nil {
return *workspace, errors.FormatAPIError(r, err)
}
return Workspace{}, errors.FormatAPIError(r, err)
}
func (c *Client) DeleteWorkspace(workspaceId int) error {
r, err := c.api.WorkspacesAPI.WorkspacesDeleteWorkspace(c.ctx, float32(workspaceId)).Execute()
return errors.FormatAPIError(r, err)
}
func (c *Client) WorkspaceStatus(workspaceId int) (*WorkspaceStatus, error) {
status, r, err := c.api.WorkspacesAPI.WorkspacesGetWorkspaceStatus(c.ctx, float32(workspaceId)).Execute()
return status, errors.FormatAPIError(r, err)
}
func (c *Client) CreateWorkspace(args CreateWorkspaceArgs) (*Workspace, error) {
workspace, r, err := c.api.WorkspacesAPI.WorkspacesCreateWorkspace(c.ctx).WorkspacesCreateWorkspaceRequest(args).Execute()
return workspace, errors.FormatAPIError(r, err)
}
func (c *Client) SetEnvVarOnWorkspace(workspaceId int, envVars map[string]string) error {
vars := []openapi_client.WorkspacesCreateWorkspaceRequestEnvInner{}
for k, v := range envVars {
vars = append(vars, openapi_client.WorkspacesCreateWorkspaceRequestEnvInner{
Name: k,
Value: v,
})
}
req := c.api.WorkspacesAPI.WorkspacesSetEnvVar(c.ctx, float32(workspaceId)).
WorkspacesCreateWorkspaceRequestEnvInner(vars)
r, err := c.api.WorkspacesAPI.WorkspacesSetEnvVarExecute(req)
return errors.FormatAPIError(r, err)
}
func (c *Client) ExecCommand(workspaceId int, command string, workdir string, env map[string]string) (string, string, error) {
workdirP := &workdir
if workdir == "" {
workdirP = nil
}
cmd := openapi_client.WorkspacesExecuteCommandRequest{
Command: command,
WorkingDir: workdirP,
Env: &env,
}
req := c.api.WorkspacesAPI.WorkspacesExecuteCommand(c.ctx, float32(workspaceId)).WorkspacesExecuteCommandRequest(cmd)
res, r, err := req.Execute()
if err != nil {
return "", "", errors.FormatAPIError(r, err)
}
if res == nil {
return "", "", errors.FormatAPIError(r, err)
}
return res.Output, res.Error, errors.FormatAPIError(r, err)
}
func (c *Client) DeployLandscape(wsId int, profile string) error {
if profile == "ci.yml" || profile == "" {
req := c.api.WorkspacesAPI.WorkspacesDeployLandscape(c.ctx, float32(wsId))
r, err := req.Execute()
return errors.FormatAPIError(r, err)
}
req := c.api.WorkspacesAPI.WorkspacesDeployLandscape1(c.ctx, float32(wsId), profile)
r, err := req.Execute()
return errors.FormatAPIError(r, err)
}
func (c *Client) StartPipelineStage(wsId int, profile string, stage string) error {
if profile == "ci.yml" || profile == "" {
req := c.api.WorkspacesAPI.WorkspacesStartPipelineStage(c.ctx, float32(wsId), stage)
r, err := req.Execute()
return errors.FormatAPIError(r, err)
}
req := c.api.WorkspacesAPI.WorkspacesStartPipelineStage1(c.ctx, float32(wsId), stage, profile)
r, err := req.Execute()
return errors.FormatAPIError(r, err)
}
func (c *Client) GetPipelineState(wsId int, stage string) ([]PipelineStatus, error) {
req := c.api.WorkspacesAPI.WorkspacesPipelineStatus(c.ctx, float32(wsId), stage)
res, r, err := req.Execute()
return res, errors.FormatAPIError(r, err)
}
// ScaleWorkspace sets the number of replicas for a workspace.
// For on-demand workspaces, setting replicas to 1 wakes up the workspace.
func (c *Client) ScaleWorkspace(wsId int, replicas int) error {
req := c.api.WorkspacesAPI.WorkspacesUpdateWorkspace(c.ctx, float32(wsId)).
WorkspacesUpdateWorkspaceRequest(openapi_client.WorkspacesUpdateWorkspaceRequest{
Replicas: &replicas,
})
r, err := req.Execute()
return errors.FormatAPIError(r, err)
}
// ScaleLandscapeServices scales landscape services by name.
// The services map contains service name -> replica count.
func (c *Client) ScaleLandscapeServices(wsId int, services map[string]int) error {
req := c.api.WorkspacesAPI.WorkspacesScaleLandscapeServices(c.ctx, float32(wsId)).
RequestBody(services)
resp, err := req.Execute()
return errors.FormatAPIError(resp, err)
}
// Waits for a given workspace to be running.
//
// Returns [TimedOut] error if the workspace does not become running in time.
func (client *Client) WaitForWorkspaceRunning(workspace *Workspace, timeout time.Duration) error {
delay := 5 * time.Second
maxWaitTime := client.time.Now().Add(timeout)
for {
status, err := client.WorkspaceStatus(workspace.Id)
if err != nil {
if client.time.Now().After(maxWaitTime) {
return err
}
client.time.Sleep(delay)
continue
}
if status.IsRunning {
return nil
}
if client.time.Now().After(maxWaitTime) {
break
}
client.time.Sleep(delay)
}
return errors.TimedOut(
fmt.Sprintf("waiting for workspace %s(%d) to be ready", workspace.Name, workspace.Id),
timeout)
}
type DeployWorkspaceArgs struct {
TeamId int
PlanId int
Name string
EnvVars map[string]string
VpnConfigName *string //must be nil to use default
IsPrivateRepo bool
GitUrl *string //must be nil to use default
Branch *string //must be nil to use default
BaseImage *string //must be nil to use default
Restricted *bool //must be nil to use default
Timeout time.Duration
}
// Deploys a workspace with the given configuration.
//
// Returns [TimedOut] error if the timeout is reached
func (client Client) DeployWorkspace(args DeployWorkspaceArgs) (*Workspace, error) {
workspace, err := client.CreateWorkspace(CreateWorkspaceArgs{
TeamId: args.TeamId,
Name: args.Name,
PlanId: args.PlanId,
IsPrivateRepo: args.IsPrivateRepo,
GitUrl: args.GitUrl,
InitialBranch: args.Branch,
BaseImage: args.BaseImage,
Restricted: args.Restricted,
SourceWorkspaceId: nil,
WelcomeMessage: nil,
Replicas: 1,
VpnConfig: args.VpnConfigName,
})
if err != nil {
return nil, err
}
if err := client.WaitForWorkspaceRunning(workspace, args.Timeout); err != nil {
return workspace, err
}
if len(args.EnvVars) != 0 {
if err := client.SetEnvVarOnWorkspace(workspace.Id, args.EnvVars); err != nil {
return workspace, err
}
}
return workspace, nil
}
func (c Client) GitPull(workspaceId int, remote string, branch string) error {
if remote == "" {
req := c.api.WorkspacesAPI.WorkspacesGitPull(c.ctx, float32(workspaceId))
r, err := req.Execute()
return errors.FormatAPIError(r, err)
}
req := c.api.WorkspacesAPI.WorkspacesGitPull2(c.ctx, float32(workspaceId), remote, branch)
r, err := req.Execute()
return errors.FormatAPIError(r, err)
}