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