Skip to content

Commit 4826f84

Browse files
committed
hub/delete-project: refactor, delete files, bugfixes
1 parent a0dea09 commit 4826f84

File tree

6 files changed

+54
-35
lines changed

6 files changed

+54
-35
lines changed

src/packages/backend/misc.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { createHash } from "crypto";
2+
3+
import { projects } from "@cocalc/backend/data";
24
import { is_valid_uuid_string } from "@cocalc/util/misc";
35

46
/*
@@ -69,3 +71,8 @@ export function envForSpawn() {
6971
}
7072
return env;
7173
}
74+
75+
// return the absolute home directory of given @project_id project on disk
76+
export function homePath(project_id: string): string {
77+
return projects.replace("[project_id]", project_id);
78+
}

src/packages/database/postgres/delete-projects.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { join } from "node:path";
1313
import { pathToFiles } from "@cocalc/backend/files/path-to-files";
1414
import getLogger from "@cocalc/backend/logger";
1515
import { newCounter } from "@cocalc/backend/metrics";
16+
import { homePath } from "@cocalc/backend/misc";
1617
import getPool from "@cocalc/database/pool";
1718
import { getServerSettings } from "@cocalc/database/settings";
1819
import { callback2 } from "@cocalc/util/async-utils";
@@ -82,7 +83,8 @@ export async function unlink_old_deleted_projects(
8283
db: PostgreSQL,
8384
age_d = 30,
8485
): Promise<void> {
85-
await callback2(db._query, {
86+
const L = log.extend("unlink_old_deleted_projects").debug;
87+
const { rowCount } = await callback2(db._query, {
8688
query: "UPDATE projects",
8789
set: { users: null },
8890
where: [
@@ -91,6 +93,7 @@ export async function unlink_old_deleted_projects(
9193
`last_edited <= NOW() - '${age_d} days'::INTERVAL`,
9294
],
9395
});
96+
L("unlinked projects:", rowCount);
9497
}
9598

9699
const Q_CLEANUP_SYNCSTRINGS = `
@@ -184,6 +187,13 @@ export async function cleanup_old_projects_data(
184187
);
185188

186189
if (on_prem) {
190+
// we don't delete the central_log, it has its own expiration
191+
// such an entry is good to have for reconstructing what really happened
192+
db.log({
193+
event: "delete_project",
194+
value: { deleting: "files", project_id },
195+
});
196+
187197
L2(`delete all project files`);
188198
await deleteProjectFiles(L2, project_id);
189199

@@ -199,6 +209,10 @@ export async function cleanup_old_projects_data(
199209

200210
// This gets rid of all sorts of data in tables specific to the given project.
201211
delRows += await delete_associated_project_data(L2, project_id);
212+
db.log({
213+
event: "delete_project",
214+
value: { deleting: "database", project_id },
215+
});
202216

203217
// now, that we're done with that project, mark it as state.state ->> 'deleted'
204218
// in addition to the flag "deleted = true". This also updates the state.time timestamp.
@@ -283,7 +297,8 @@ async function delete_associated_project_data(
283297

284298
async function deleteProjectFiles(L2, project_id: string) {
285299
// TODO: this only works on-prem, and requires the project files to be mounted
286-
const projects_root = process.env["MOUNTED_PROJECTS_ROOT"];
300+
const projects_root =
301+
process.env["MOUNTED_PROJECTS_ROOT"] ?? homePath(project_id);
287302
if (!projects_root) return;
288303
const project_dir = join(projects_root, project_id);
289304
try {
@@ -297,7 +312,7 @@ async function deleteProjectFiles(L2, project_id: string) {
297312
}
298313
} catch (err) {
299314
L2(
300-
`not deleting project files: either it does not exist or is not accessible`,
315+
`not deleting project files: either path does not exist or is not accessible`,
301316
);
302317
}
303318
}

src/packages/server/projects/control/multi-user.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ This code is very similar to single-user.ts, except with some
1616
small modifications due to having to create and delete Linux users.
1717
*/
1818

19+
import getLogger from "@cocalc/backend/logger";
20+
import { getUid, homePath } from "@cocalc/backend/misc";
21+
import {
22+
BaseProject,
23+
CopyOptions,
24+
getProject,
25+
ProjectState,
26+
ProjectStatus,
27+
} from "./base";
1928
import {
2029
chown,
2130
copyPath,
@@ -25,22 +34,12 @@ import {
2534
getEnvironment,
2635
getState,
2736
getStatus,
28-
homePath,
2937
isProjectRunning,
3038
launchProjectDaemon,
3139
mkdir,
3240
setupDataPath,
3341
stopProjectProcesses,
3442
} from "./util";
35-
import {
36-
BaseProject,
37-
CopyOptions,
38-
getProject,
39-
ProjectStatus,
40-
ProjectState,
41-
} from "./base";
42-
import getLogger from "@cocalc/backend/logger";
43-
import { getUid } from "@cocalc/backend/misc";
4443

4544
const winston = getLogger("project-control:multi-user");
4645

@@ -71,7 +70,7 @@ class Project extends BaseProject {
7170
const status = await getStatus(this.HOME);
7271
// TODO: don't include secret token in log message.
7372
winston.debug(
74-
`got status of ${this.project_id} = ${JSON.stringify(status)}`
73+
`got status of ${this.project_id} = ${JSON.stringify(status)}`,
7574
);
7675
this.saveStatusToDatabase(status);
7776
return status;
@@ -155,7 +154,7 @@ class Project extends BaseProject {
155154
await copyPath(
156155
opts,
157156
this.project_id,
158-
opts.target_project_id ? getUid(opts.target_project_id) : undefined
157+
opts.target_project_id ? getUid(opts.target_project_id) : undefined,
159158
);
160159
return "";
161160
}

src/packages/server/projects/control/single-user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This is useful for:
1919
import { kill } from "process";
2020

2121
import getLogger from "@cocalc/backend/logger";
22+
import { homePath } from "@cocalc/backend/misc";
2223
import {
2324
BaseProject,
2425
CopyOptions,
@@ -33,7 +34,6 @@ import {
3334
getProjectPID,
3435
getState,
3536
getStatus,
36-
homePath,
3737
isProjectRunning,
3838
launchProjectDaemon,
3939
mkdir,

src/packages/server/projects/control/util.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
import { promisify } from "util";
2-
import { dirname, join, resolve } from "path";
3-
import { exec as exec0, spawn } from "child_process";
41
import spawnAsync from "await-spawn";
5-
import * as fs from "fs";
6-
import { writeFile } from "fs/promises";
7-
import { projects, root, blobstore } from "@cocalc/backend/data";
8-
import { is_valid_uuid_string } from "@cocalc/util/misc";
9-
import { callback2 } from "@cocalc/util/async-utils";
10-
import getLogger from "@cocalc/backend/logger";
11-
import { CopyOptions, ProjectState, ProjectStatus } from "./base";
12-
import { getUid } from "@cocalc/backend/misc";
2+
import { exec as exec0, spawn } from "node:child_process";
3+
import * as fs from "node:fs";
4+
import { writeFile } from "node:fs/promises";
5+
import { dirname, join, resolve } from "node:path";
6+
import { promisify } from "node:util";
7+
138
import base_path from "@cocalc/backend/base-path";
9+
import { blobstore, root } from "@cocalc/backend/data";
10+
import getLogger from "@cocalc/backend/logger";
11+
import { getUid, homePath } from "@cocalc/backend/misc";
1412
import { db } from "@cocalc/database";
15-
import { getProject } from ".";
13+
import { callback2 } from "@cocalc/util/async-utils";
14+
import { is_valid_uuid_string } from "@cocalc/util/misc";
1615
import { pidFilename, pidUpdateIntervalMs } from "@cocalc/util/project-info";
16+
import { getProject } from ".";
17+
import { CopyOptions, ProjectState, ProjectStatus } from "./base";
1718

1819
const logger = getLogger("project-control:util");
1920

@@ -31,10 +32,6 @@ export function dataPath(HOME: string): string {
3132
return join(HOME, ".smc");
3233
}
3334

34-
export function homePath(project_id: string): string {
35-
return projects.replace("[project_id]", project_id);
36-
}
37-
3835
export function getUsername(project_id: string): string {
3936
return project_id.split("-").join("");
4037
}

src/packages/util/db-schema/site-defaults.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,11 @@ export const site_settings_conf: SiteSettings = {
653653
to_val: split_iframe_comm_hosts,
654654
to_display: num_dns_hosts,
655655
},
656-
delete_project_data : {
657-
name :"Delete Project Data",
658-
desc: "When a project has been marked as deleted, also actually delete associated data from the database and (for OnPrem) also its files.",
656+
delete_project_data: {
657+
name: "Delete Project Data",
658+
desc: "When a project has been marked as deleted, also actually delete associated data from the database and (OnPrem only) also its files.",
659659
default: "no",
660+
valid: only_booleans,
660661
to_val: to_bool,
661662
},
662663
email_enabled: {

0 commit comments

Comments
 (0)