1
- import api from "@cocalc/frontend/client/api" ;
2
1
import type {
3
- Action ,
4
- Cloud ,
5
2
ComputeServerTemplate ,
6
3
ComputeServerUserInfo ,
7
4
Configuration ,
@@ -16,94 +13,76 @@ import type {
16
13
} from "@cocalc/util/compute/templates" ;
17
14
import { reuseInFlight } from "@cocalc/util/reuse-in-flight" ;
18
15
import TTL from "@isaacs/ttlcache" ;
16
+ import { webapp_client } from "@cocalc/frontend/webapp-client" ;
17
+ import { type Compute } from "@cocalc/conat/hub/api/compute" ;
19
18
20
- export async function createServer ( opts : {
21
- project_id : string ;
22
- title ?: string ;
23
- color ?: string ;
24
- autorestart ?: boolean ;
25
- cloud ?: Cloud ;
26
- configuration ?: Configuration ;
27
- notes ?: string ;
28
- course_project_id ?: string ;
29
- course_server_id ?: number ;
30
- } ) : Promise < number > {
31
- return await api ( "compute/create-server" , opts ) ;
19
+ function compute ( ) : Compute {
20
+ return webapp_client . conat_client . hub . compute ;
32
21
}
33
22
34
- export async function computeServerAction ( opts : {
35
- id : number ;
36
- action : Action ;
37
- } ) {
38
- await api ( "compute/compute-server-action" , opts ) ;
23
+ export async function createServer ( opts ) : Promise < number > {
24
+ return await compute ( ) . createServer ( opts ) ;
39
25
}
40
26
41
- // Get servers across potentially different projects by their global unique id.
42
- // Use the fields parameter to restrict to a much smaller subset of information
43
- // about each server (e.g., just the state field). Caller must be a collaborator
44
- // on each project containing the servers.
45
- // If you give an id of a server that doesn't exist, it'll just be excluded in the result.
46
- // Similarly, if you give a field that doesn't exist, it is excluded.
47
- // The order of the returned servers and count probably will NOT match that in
48
- // ids, so you should include 'id' in fields.
49
- export async function getServersById ( opts : {
50
- ids : number [ ] ;
51
- fields ?: string [ ] ;
52
- } ) : Promise < Partial < ComputeServerUserInfo > [ ] > {
53
- return await api ( "compute/get-servers-by-id" , opts ) ;
27
+ export async function computeServerAction ( opts ) : Promise < void > {
28
+ await compute ( ) . computeServerAction ( opts ) ;
54
29
}
55
30
56
- export async function getServers ( opts : {
57
- id ?: number ;
58
- project_id : string ;
59
- } ) : Promise < ComputeServerUserInfo [ ] > {
60
- return await api ( "compute/get-servers" , opts ) ;
31
+ export async function getServersById (
32
+ opts ,
33
+ ) : Promise < Partial < ComputeServerUserInfo > [ ] > {
34
+ return await compute ( ) . getServersById ( opts ) ;
61
35
}
62
36
63
- export async function getServerState ( id : number ) {
64
- return await api ( " compute/get-server-state" , { id } ) ;
37
+ export async function getServers ( opts ) : Promise < ComputeServerUserInfo [ ] > {
38
+ return await compute ( ) . getServers ( opts ) ;
65
39
}
66
40
67
- export async function getSerialPortOutput ( id : number ) {
68
- return await api ( "compute/get-serial-port-output" , { id } ) ;
41
+ export async function getServerState (
42
+ id : number ,
43
+ ) : Promise < ComputeServerUserInfo [ "state" ] > {
44
+ return await compute ( ) . getServerState ( { id } ) ;
45
+ }
46
+
47
+ export async function getSerialPortOutput ( id : number ) : Promise < string > {
48
+ return await compute ( ) . getSerialPortOutput ( { id } ) ;
69
49
}
70
50
71
51
export async function deleteServer ( id : number ) {
72
- return await api ( " compute/delete-server" , { id } ) ;
52
+ return await compute ( ) . deleteServer ( { id } ) ;
73
53
}
74
54
75
55
export async function isDnsAvailable ( dns : string ) {
76
- const { isAvailable } = await api ( "compute/is-dns-available" , { dns } ) ;
77
- return isAvailable ;
56
+ return await compute ( ) . isDnsAvailable ( { dns } ) ;
78
57
}
79
58
80
59
export async function undeleteServer ( id : number ) {
81
- await api ( " compute/undelete-server" , { id } ) ;
60
+ return await compute ( ) . undeleteServer ( { id } ) ;
82
61
}
83
62
84
63
// only owner can change properties of a compute server.
85
64
86
65
export async function setServerColor ( opts : { id : number ; color : string } ) {
87
- return await api ( " compute/set-server-color" , opts ) ;
66
+ await compute ( ) . setServerColor ( opts ) ;
88
67
}
89
68
90
69
export async function setServerTitle ( opts : { id : number ; title : string } ) {
91
- return await api ( " compute/set-server-title" , opts ) ;
70
+ await compute ( ) . setServerTitle ( opts ) ;
92
71
}
93
72
94
73
export async function setServerConfiguration ( opts : {
95
74
id : number ;
96
75
configuration : Partial < Configuration > ;
97
76
} ) {
98
- return await api ( " compute/set-server-configuration" , opts ) ;
77
+ await compute ( ) . setServerConfiguration ( opts ) ;
99
78
}
100
79
101
80
// only for admins!
102
81
export async function setTemplate ( opts : {
103
82
id : number ;
104
83
template : ComputeServerTemplate ;
105
84
} ) {
106
- return await api ( " compute/set-template" , opts ) ;
85
+ await compute ( ) . setTemplate ( opts ) ;
107
86
}
108
87
109
88
// 5-minute client side ttl cache of all and specific template, since
@@ -115,7 +94,7 @@ export async function getTemplate(id: number): Promise<ConfigurationTemplate> {
115
94
if ( templatesCache . has ( id ) ) {
116
95
return templatesCache . get ( id ) ! ;
117
96
}
118
- const x = await api ( " compute/get-template" , { id } ) ;
97
+ const x = await compute ( ) . getTemplate ( { id } ) ;
119
98
templatesCache . set ( id , x ) ;
120
99
return x ;
121
100
}
@@ -124,20 +103,20 @@ export async function getTemplates(): Promise<ConfigurationTemplates> {
124
103
if ( templatesCache . has ( "templates" ) ) {
125
104
return templatesCache . get ( "templates" ) ! ;
126
105
}
127
- const x = await api ( " compute/get-templates" ) ;
106
+ const x = await compute ( ) . getTemplates ( ) ;
128
107
templatesCache . set ( "templates" , x ) ;
129
108
return x ;
130
109
}
131
110
132
111
export async function setServerCloud ( opts : { id : number ; cloud : string } ) {
133
- return await api ( " compute/set-server-cloud" , opts ) ;
112
+ await compute ( ) . setServerCloud ( opts ) ;
134
113
}
135
114
136
115
export async function setServerOwner ( opts : {
137
116
id : number ;
138
117
new_account_id : string ;
139
118
} ) {
140
- return await api ( " compute/set-server-owner" , opts ) ;
119
+ await compute ( ) . setServerOwner ( opts ) ;
141
120
}
142
121
143
122
// Cache for 12 hours
@@ -149,7 +128,7 @@ export const getGoogleCloudPriceData = reuseInFlight(
149
128
googleCloudPriceData == null ||
150
129
Date . now ( ) >= googleCloudPriceDataExpire
151
130
) {
152
- googleCloudPriceData = await api ( " compute/google-cloud/get-pricing-data" ) ;
131
+ googleCloudPriceData = await compute ( ) . getGoogleCloudPriceData ( ) ;
153
132
googleCloudPriceDataExpire = Date . now ( ) + 1000 * 60 * 60 * 12 ; // 12 hour cache
154
133
}
155
134
if ( googleCloudPriceData == null ) {
@@ -186,7 +165,7 @@ export const getHyperstackPriceData = reuseInFlight(
186
165
hyperstackPriceData == null ||
187
166
Date . now ( ) >= hyperstackPriceDataExpire
188
167
) {
189
- hyperstackPriceData = await api ( " compute/get-hyperstack-pricing-data" ) ;
168
+ hyperstackPriceData = await compute ( ) . getHyperstackPriceData ( ) ;
190
169
hyperstackPriceDataExpire = Date . now ( ) + 1000 * 60 * 5 ; // 5 minute cache
191
170
}
192
171
if ( hyperstackPriceData == null ) {
@@ -203,22 +182,22 @@ export async function getNetworkUsage(opts: {
203
182
start : Date ;
204
183
end : Date ;
205
184
} ) : Promise < { amount : number ; cost : number } > {
206
- return await api ( " compute/get-network-usage" , opts ) ;
185
+ return await compute ( ) . getNetworkUsage ( opts ) ;
207
186
}
208
187
209
188
// Get the current api key for a specific (on prem) server.
210
189
// We only need this for on prem, so we are restricting to that right now.
211
190
// If no key is allocated, one will be created.
212
191
export async function getApiKey ( opts : { id } ) : Promise < string > {
213
- return await api ( " compute/get-api-key" , opts ) ;
192
+ return await compute ( ) . getApiKey ( opts ) ;
214
193
}
215
194
export async function deleteApiKey ( opts : { id } ) : Promise < void > {
216
- await api ( " compute/delete-api-key" , opts ) ;
195
+ await compute ( ) . deleteApiKey ( opts ) ;
217
196
}
218
197
219
198
// Get the project log entries directly for just one compute server
220
199
export async function getLog ( opts : { id ; type : "activity" | "files" } ) {
221
- return await api ( " compute/get-log" , opts ) ;
200
+ return await compute ( ) . getLog ( opts ) ;
222
201
}
223
202
224
203
export const getTitle = reuseInFlight (
@@ -229,7 +208,7 @@ export const getTitle = reuseInFlight(
229
208
color : string ;
230
209
project_specific_id : number ;
231
210
} > => {
232
- return await api ( " compute/get-server-title" , opts ) ;
211
+ return await compute ( ) . getTitle ( opts ) ;
233
212
} ,
234
213
) ;
235
214
@@ -243,7 +222,7 @@ export async function setDetailedState(opts: {
243
222
timeout ?: number ;
244
223
progress ?: number ;
245
224
} ) {
246
- return await api ( " compute/set-detailed-state" , opts ) ;
225
+ await compute ( ) . setDetailedState ( opts ) ;
247
226
}
248
227
249
228
// We cache images for 5 minutes.
@@ -271,7 +250,6 @@ function cacheGet(cloud) {
271
250
function cacheSet ( cloud , images ) {
272
251
imagesCache [ cloud ] = { images, timestamp : Date . now ( ) } ;
273
252
}
274
-
275
253
async function getImagesFor ( {
276
254
cloud,
277
255
endpoint,
@@ -286,11 +264,14 @@ async function getImagesFor({
286
264
}
287
265
288
266
try {
289
- const images = await api (
290
- endpoint ,
291
- // admin reload forces fetch data from github and/or google cloud - normal users just have their cache ignored above
292
- reload ? { noCache : true } : undefined ,
293
- ) ;
267
+ let images ;
268
+ if ( endpoint == "compute/get-images" ) {
269
+ images = await compute ( ) . getImages ( { noCache : ! ! reload } ) ;
270
+ } else if ( endpoint == "compute/get-images-google" ) {
271
+ images = await compute ( ) . getGoogleCloudImages ( { noCache : ! ! reload } ) ;
272
+ } else {
273
+ throw Error ( `unknown endpoint ${ endpoint } ` ) ;
274
+ }
294
275
cacheSet ( cloud , images ) ;
295
276
return images ;
296
277
} catch ( err ) {
@@ -328,5 +309,5 @@ export async function setImageTested(opts: {
328
309
id : number ; // server id
329
310
tested : boolean ;
330
311
} ) {
331
- return await api ( " compute/set-image-tested" , opts ) ;
312
+ await compute ( ) . setImageTested ( opts ) ;
332
313
}
0 commit comments