5
5
*/
6
6
7
7
import { injectable , inject } from "inversify" ;
8
- import { GitpodServerImpl , traceAPIParams , traceWI , censor } from "../../../src/workspace/gitpod-server-impl" ;
8
+ import { GitpodServerImpl , traceAPIParams , traceWI } from "../../../src/workspace/gitpod-server-impl" ;
9
9
import { TraceContext , TraceContextWithSpan } from "@gitpod/gitpod-protocol/lib/util/tracing" ;
10
10
import {
11
11
GitpodClient ,
12
- AdminGetListRequest ,
13
12
User ,
14
13
Team ,
15
- AdminGetListResult ,
16
14
Permission ,
17
- AdminBlockUserRequest ,
18
- GetWorkspaceTimeoutResult ,
19
- WorkspaceTimeoutDuration ,
20
- SetWorkspaceTimeoutResult ,
21
15
WorkspaceContext ,
22
16
WorkspaceCreationResult ,
23
17
PrebuiltWorkspaceContext ,
24
18
CommitContext ,
25
19
PrebuiltWorkspace ,
26
- WorkspaceInstance ,
27
20
ProviderRepository ,
28
21
PrebuildWithStatus ,
29
22
CreateProjectParams ,
30
23
Project ,
31
24
StartPrebuildResult ,
32
25
ClientHeaderFields ,
33
26
FindPrebuildsParams ,
34
- WORKSPACE_TIMEOUT_DEFAULT_SHORT ,
35
27
PrebuildEvent ,
36
28
OpenPrebuildContext ,
37
29
} from "@gitpod/gitpod-protocol" ;
38
30
import { ResponseError } from "vscode-jsonrpc" ;
39
- import {
40
- AdmissionLevel ,
41
- ControlAdmissionRequest ,
42
- DescribeWorkspaceRequest ,
43
- SetTimeoutRequest ,
44
- } from "@gitpod/ws-manager/lib" ;
31
+ import { AdmissionLevel , ControlAdmissionRequest } from "@gitpod/ws-manager/lib" ;
45
32
import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error" ;
46
33
import { log , LogContext } from "@gitpod/gitpod-protocol/lib/util/logging" ;
47
34
import { LicenseValidationResult } from "@gitpod/gitpod-protocol/lib/license-protocol" ;
@@ -65,7 +52,7 @@ import { ClientMetadata, traceClientMetadata } from "../../../src/websocket/webs
65
52
import { BitbucketAppSupport } from "../bitbucket/bitbucket-app-support" ;
66
53
import { URL } from "url" ;
67
54
import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution" ;
68
- import { EntitlementService , MayStartWorkspaceResult } from "../../../src/billing/entitlement-service" ;
55
+ import { EntitlementService } from "../../../src/billing/entitlement-service" ;
69
56
import { BillingMode } from "@gitpod/gitpod-protocol/lib/billing-mode" ;
70
57
import { BillingModes } from "../billing/billing-mode" ;
71
58
import { UsageServiceDefinition } from "@gitpod/usage-api/lib/usage/v1/usage.pb" ;
@@ -158,149 +145,10 @@ export class GitpodServerEEImpl extends GitpodServerImpl {
158
145
return allProjects ;
159
146
}
160
147
161
- protected async mayStartWorkspace (
162
- ctx : TraceContext ,
163
- user : User ,
164
- organizationId : string | undefined ,
165
- runningInstances : Promise < WorkspaceInstance [ ] > ,
166
- ) : Promise < void > {
167
- await super . mayStartWorkspace ( ctx , user , organizationId , runningInstances ) ;
168
-
169
- let result : MayStartWorkspaceResult = { } ;
170
- try {
171
- result = await this . entitlementService . mayStartWorkspace (
172
- user ,
173
- organizationId ,
174
- new Date ( ) ,
175
- runningInstances ,
176
- ) ;
177
- TraceContext . addNestedTags ( ctx , { mayStartWorkspace : { result } } ) ;
178
- } catch ( err ) {
179
- log . error ( { userId : user . id } , "EntitlementSerivce.mayStartWorkspace error" , err ) ;
180
- TraceContext . setError ( ctx , err ) ;
181
- return ; // we don't want to block workspace starts because of internal errors
182
- }
183
- if ( ! ! result . needsVerification ) {
184
- throw new ResponseError ( ErrorCodes . NEEDS_VERIFICATION , `Please verify your account.` ) ;
185
- }
186
- if ( ! ! result . usageLimitReachedOnCostCenter ) {
187
- throw new ResponseError ( ErrorCodes . PAYMENT_SPENDING_LIMIT_REACHED , "Increase usage limit and try again." , {
188
- attributionId : result . usageLimitReachedOnCostCenter ,
189
- } ) ;
190
- }
191
- if ( ! ! result . hitParallelWorkspaceLimit ) {
192
- throw new ResponseError (
193
- ErrorCodes . TOO_MANY_RUNNING_WORKSPACES ,
194
- `You cannot run more than ${ result . hitParallelWorkspaceLimit . max } workspaces at the same time. Please stop a workspace before starting another one.` ,
195
- ) ;
196
- }
197
- }
198
-
199
148
async validateLicense ( ctx : TraceContext ) : Promise < LicenseValidationResult > {
200
149
return { valid : true } ;
201
150
}
202
151
203
- goDurationToHumanReadable ( goDuration : string ) : string {
204
- const [ , value , unit ] = goDuration . match ( / ^ ( \d + ) ( [ m h ] ) $ / ) ! ;
205
- let duration = parseInt ( value ) ;
206
-
207
- switch ( unit ) {
208
- case "m" :
209
- duration *= 60 ;
210
- break ;
211
- case "h" :
212
- duration *= 60 * 60 ;
213
- break ;
214
- }
215
-
216
- const hours = Math . floor ( duration / 3600 ) ;
217
- duration %= 3600 ;
218
- const minutes = Math . floor ( duration / 60 ) ;
219
- duration %= 60 ;
220
-
221
- let result = "" ;
222
- if ( hours ) {
223
- result += `${ hours } hour${ hours === 1 ? "" : "s" } ` ;
224
- if ( minutes ) {
225
- result += " and " ;
226
- }
227
- }
228
- if ( minutes ) {
229
- result += `${ minutes } minute${ minutes === 1 ? "" : "s" } ` ;
230
- }
231
-
232
- return result ;
233
- }
234
-
235
- public async setWorkspaceTimeout (
236
- ctx : TraceContext ,
237
- workspaceId : string ,
238
- duration : WorkspaceTimeoutDuration ,
239
- ) : Promise < SetWorkspaceTimeoutResult > {
240
- traceAPIParams ( ctx , { workspaceId, duration } ) ;
241
- traceWI ( ctx , { workspaceId } ) ;
242
-
243
- const user = this . checkUser ( "setWorkspaceTimeout" ) ;
244
-
245
- if ( ! ( await this . entitlementService . maySetTimeout ( user , new Date ( ) ) ) ) {
246
- throw new ResponseError ( ErrorCodes . PLAN_PROFESSIONAL_REQUIRED , "Plan upgrade is required" ) ;
247
- }
248
-
249
- let validatedDuration ;
250
- try {
251
- validatedDuration = WorkspaceTimeoutDuration . validate ( duration ) ;
252
- } catch ( err ) {
253
- throw new ResponseError ( ErrorCodes . INVALID_VALUE , "Invalid duration : " + err . message ) ;
254
- }
255
-
256
- const workspace = await this . internalGetWorkspace ( workspaceId , this . workspaceDb . trace ( ctx ) ) ;
257
- const runningInstances = await this . workspaceDb . trace ( ctx ) . findRegularRunningInstances ( user . id ) ;
258
- const runningInstance = runningInstances . find ( ( i ) => i . workspaceId === workspaceId ) ;
259
- if ( ! runningInstance ) {
260
- throw new ResponseError ( ErrorCodes . NOT_FOUND , "Can only set keep-alive for running workspaces" ) ;
261
- }
262
- await this . guardAccess ( { kind : "workspaceInstance" , subject : runningInstance , workspace : workspace } , "update" ) ;
263
-
264
- const client = await this . workspaceManagerClientProvider . get ( runningInstance . region ) ;
265
-
266
- const req = new SetTimeoutRequest ( ) ;
267
- req . setId ( runningInstance . id ) ;
268
- req . setDuration ( validatedDuration ) ;
269
- await client . setTimeout ( ctx , req ) ;
270
-
271
- return {
272
- resetTimeoutOnWorkspaces : [ workspace . id ] ,
273
- humanReadableDuration : this . goDurationToHumanReadable ( validatedDuration ) ,
274
- } ;
275
- }
276
-
277
- public async getWorkspaceTimeout ( ctx : TraceContext , workspaceId : string ) : Promise < GetWorkspaceTimeoutResult > {
278
- traceAPIParams ( ctx , { workspaceId } ) ;
279
- traceWI ( ctx , { workspaceId } ) ;
280
-
281
- const user = this . checkUser ( "getWorkspaceTimeout" ) ;
282
-
283
- const canChange = await this . entitlementService . maySetTimeout ( user , new Date ( ) ) ;
284
-
285
- const workspace = await this . internalGetWorkspace ( workspaceId , this . workspaceDb . trace ( ctx ) ) ;
286
- const runningInstance = await this . workspaceDb . trace ( ctx ) . findRunningInstance ( workspaceId ) ;
287
- if ( ! runningInstance ) {
288
- log . warn ( { userId : user . id , workspaceId } , "Can only get keep-alive for running workspaces" ) ;
289
- const duration = WORKSPACE_TIMEOUT_DEFAULT_SHORT ;
290
- return { duration, canChange, humanReadableDuration : this . goDurationToHumanReadable ( duration ) } ;
291
- }
292
- await this . guardAccess ( { kind : "workspaceInstance" , subject : runningInstance , workspace : workspace } , "get" ) ;
293
-
294
- const req = new DescribeWorkspaceRequest ( ) ;
295
- req . setId ( runningInstance . id ) ;
296
-
297
- const client = await this . workspaceManagerClientProvider . get ( runningInstance . region ) ;
298
- const desc = await client . describeWorkspace ( ctx , req ) ;
299
- const duration = desc . getStatus ( ) ! . getSpec ( ) ! . getTimeout ( ) ;
300
-
301
- return { duration, canChange, humanReadableDuration : this . goDurationToHumanReadable ( duration ) } ;
302
- }
303
-
304
152
public async isPrebuildDone ( ctx : TraceContext , pwsId : string ) : Promise < boolean > {
305
153
traceAPIParams ( ctx , { pwsId } ) ;
306
154
0 commit comments