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