Skip to content

Commit 3d1e9f4

Browse files
committed
start conat RPC api for compute servers
1 parent 05160f0 commit 3d1e9f4

File tree

8 files changed

+74
-42
lines changed

8 files changed

+74
-42
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { authFirstRequireAccount } from "./util";
2+
import type {
3+
//Action,
4+
Cloud,
5+
//ComputeServerTemplate,
6+
//ComputeServerUserInfo,
7+
Configuration,
8+
//Images,
9+
//GoogleCloudImages,
10+
} from "@cocalc/util/db-schema/compute-servers";
11+
12+
export const compute = {
13+
createServer: authFirstRequireAccount,
14+
};
15+
16+
export interface Compute {
17+
createServer: (opts: {
18+
account_id?: string;
19+
project_id: string;
20+
title?: string;
21+
color?: string;
22+
autorestart?: boolean;
23+
cloud?: Cloud;
24+
configuration?: Configuration;
25+
notes?: string;
26+
course_project_id?: string;
27+
course_server_id?: number;
28+
}) => Promise<number>;
29+
}

src/packages/conat/hub/api/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { handleErrorMessage } from "@cocalc/conat/util";
88
import { type Sync, sync } from "./sync";
99
import { type Org, org } from "./org";
1010
import { type Messages, messages } from "./messages";
11+
import { type Compute, compute } from "./compute";
1112

1213
export interface HubApi {
1314
system: System;
@@ -18,6 +19,7 @@ export interface HubApi {
1819
sync: Sync;
1920
org: Org;
2021
messages: Messages;
22+
compute: Compute;
2123
}
2224

2325
const HubApiStructure = {
@@ -29,6 +31,7 @@ const HubApiStructure = {
2931
sync,
3032
org,
3133
messages,
34+
compute,
3235
} as const;
3336

3437
export function transformArgs({ name, args, account_id, project_id }) {

src/packages/frontend/compute/action.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default function getActions({
5151
const a = ACTION_INFO[action];
5252
if (!a) continue;
5353
if (action == "suspend") {
54-
if (configuration.cloud != "google-cloud") {
54+
if (configuration?.cloud != "google-cloud") {
5555
continue;
5656
}
5757
if (getArchitecture(configuration) == "arm64") {
@@ -61,12 +61,12 @@ export default function getActions({
6161
continue;
6262
}
6363
// must have no gpu and <= 208GB of RAM -- https://cloud.google.com/compute/docs/instances/suspend-resume-instance
64-
if (configuration.acceleratorType) {
64+
if (configuration?.acceleratorType) {
6565
continue;
6666
}
6767
// [ ] TODO: we don't have an easy way to check the RAM requirement right now.
6868
}
69-
if (!editModal && configuration.ephemeral && action == "stop") {
69+
if (!editModal && configuration?.ephemeral && action == "stop") {
7070
continue;
7171
}
7272
const {
@@ -79,10 +79,10 @@ export default function getActions({
7979
confirmMessage,
8080
clouds,
8181
} = a;
82-
if (danger && !configuration.ephemeral && !editModal) {
82+
if (danger && !configuration?.ephemeral && !editModal) {
8383
continue;
8484
}
85-
if (clouds && !clouds.includes(configuration.cloud)) {
85+
if (clouds && !clouds.includes(configuration?.cloud)) {
8686
continue;
8787
}
8888
v.push(
@@ -179,7 +179,7 @@ function ActionButton({
179179
}
180180
}
181181

182-
if (configuration.cloud == "onprem") {
182+
if (configuration?.cloud == "onprem") {
183183
if (action == "start") {
184184
setShowOnPremStart(true);
185185
} else if (action == "stop") {
@@ -244,7 +244,7 @@ function ActionButton({
244244
onOpenChange={setPopConfirm}
245245
placement="right"
246246
okButtonProps={{
247-
disabled: !configuration.ephemeral && danger && !understand,
247+
disabled: !configuration?.ephemeral && danger && !understand,
248248
}}
249249
title={
250250
<div>
@@ -269,7 +269,7 @@ function ActionButton({
269269
}`}
270270
/>
271271
)}
272-
{!configuration.ephemeral && danger && (
272+
{!configuration?.ephemeral && danger && (
273273
<div>
274274
{/* ATTN: Not using a checkbox here to WORKAROUND A BUG IN CHROME that I see after a day or so! */}
275275
<Button onClick={() => setUnderstand(!understand)} type="text">
@@ -443,7 +443,7 @@ function OnPremGuide({ setShow, configuration, id, title, action }) {
443443
</div>
444444
}
445445
/>
446-
{configuration.gpu && (
446+
{configuration?.gpu && (
447447
<span>
448448
Since you clicked GPU, you must also have an NVIDIA GPU and the
449449
Cuda drivers installed and working.{" "}

src/packages/frontend/compute/configuration.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,9 @@ function Config({
131131
template={template}
132132
/>
133133
);
134+
} else if (configuration == null) {
135+
return <span>Not Configured</span>;
134136
} else {
135-
return (
136-
<span>
137-
Configuration not implemented: {JSON.stringify(configuration)}
138-
</span>
139-
);
137+
return <span>Unknown Cloud: '{JSON.stringify(configuration?.cloud)}'</span>;
140138
}
141139
}

src/packages/server/compute/create-server.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ It's of course easy to make a compute serve that can't be started due to invalid
1010
import getPool from "@cocalc/database/pool";
1111
import { isValidUUID } from "@cocalc/util/misc";
1212
import isCollaborator from "@cocalc/server/projects/is-collaborator";
13-
import {
14-
CLOUDS_BY_NAME,
15-
} from "@cocalc/util/db-schema/compute-servers";
13+
import { CLOUDS_BY_NAME } from "@cocalc/util/db-schema/compute-servers";
1614
import { isDnsAvailable } from "./dns";
1715
import { getAvailableVpnIp } from "./vpn";
1816
import { getProjectSpecificId } from "./project-specific-id";
@@ -28,7 +26,7 @@ import type {
2826
} from "@cocalc/util/db-schema/compute-servers";
2927

3028
interface Options {
31-
account_id: string;
29+
account_id?: string;
3230
project_id: string;
3331
cloud?: Cloud;
3432
configuration?: Configuration;
@@ -38,7 +36,7 @@ interface Options {
3836
position?: number;
3937
notes?: string;
4038
course_project_id?: string;
41-
course_server_id?: string;
39+
course_server_id?: number;
4240
}
4341

4442
const FIELDS =
@@ -49,7 +47,7 @@ const FIELDS =
4947
export default async function createServer(opts: Options): Promise<number> {
5048
logger.debug("createServer", opts);
5149
if (!isValidUUID(opts.account_id)) {
52-
throw Error("created_by must be a valid uuid");
50+
throw Error("account_id must be a valid uuid");
5351
}
5452
if (!isValidUUID(opts.project_id)) {
5553
throw Error("project_id must be a valid uuid");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import createServer from "@cocalc/server/compute/create-server";
2+
export { createServer };

src/packages/server/conat/api/index.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ pnpm conat-watch 'hub.*.*.api' --match-replies
4040
4141
*/
4242

43+
import * as purchases from "./purchases";
44+
import * as db from "./db";
45+
import * as system from "./system";
46+
import * as projects from "./projects";
47+
import * as jupyter from "./jupyter";
48+
import * as sync from "./sync";
49+
import * as org from "./org";
50+
import * as messages from "./messages";
51+
import * as compute from "./compute";
52+
4353
import getLogger from "@cocalc/backend/logger";
4454
import { type HubApi, getUserId, transformArgs } from "@cocalc/conat/hub/api";
4555
import { conat } from "@cocalc/backend/conat";
@@ -50,6 +60,18 @@ import { close as terminateProjectRunner } from "@cocalc/server/conat/project/ru
5060
import { close as terminateProjectRunnerLoadBalancer } from "@cocalc/server/conat/project/load-balancer";
5161
import { delay } from "awaiting";
5262

63+
export const hubApi: HubApi = {
64+
system,
65+
projects,
66+
db,
67+
purchases,
68+
jupyter,
69+
sync,
70+
org,
71+
messages,
72+
compute,
73+
};
74+
5375
const logger = getLogger("server:conat:api");
5476

5577
export function initAPI() {
@@ -174,26 +196,6 @@ async function handleApiRequest({ request, mesg }) {
174196
}
175197
}
176198

177-
import * as purchases from "./purchases";
178-
import * as db from "./db";
179-
import * as system from "./system";
180-
import * as projects from "./projects";
181-
import * as jupyter from "./jupyter";
182-
import * as sync from "./sync";
183-
import * as org from "./org";
184-
import * as messages from "./messages";
185-
186-
export const hubApi: HubApi = {
187-
system,
188-
projects,
189-
db,
190-
purchases,
191-
jupyter,
192-
sync,
193-
org,
194-
messages,
195-
};
196-
197199
async function getResponse({ name, args, account_id, project_id }) {
198200
const [group, functionName] = name.split(".");
199201
const f = hubApi[group]?.[functionName];

src/packages/server/projects/is-collaborator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import getPool from "@cocalc/database/pool";
22
import { is_valid_uuid_string as isValid } from "@cocalc/util/misc";
33

44
interface Options {
5-
account_id: string;
6-
project_id: string;
5+
account_id?: string;
6+
project_id?: string;
77
}
88

99
// Return true if account_id is a collaborator on project_ids.

0 commit comments

Comments
 (0)