-
Notifications
You must be signed in to change notification settings - Fork 0
/
applicaton.server.ts
294 lines (273 loc) · 8.39 KB
/
applicaton.server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import JSZip from "jszip";
import { stringify, parse } from "@ltd/j-toml";
import {
JOB_OUTPUT_DIR,
WORKFLOW_CONFIG_FILENAME,
} from "../bartender-client/constants";
import { createClient, multipart } from "./config.server";
import { getJobById } from "./job.server";
import {
dedupWorkflow,
parseWorkflowFromTable,
} from "@i-vresse/wb-core/dist/toml.js";
import { BartenderError, ForbiddenError, InvalidUploadError } from "./errors";
import type { ExpertiseLevel } from "~/drizzle/schema.server";
import type { IFiles, IWorkflow } from "@i-vresse/wb-core/dist/types";
import {
Errors,
ValidationError,
validateWorkflow,
} from "@i-vresse/wb-core/dist/validate.js";
import { getCatalog } from "~/catalogs/index.server";
import {
instance,
mimeType,
object,
optional,
picklist,
parse as valibotParse,
pipe,
} from "valibot";
import { NodeOnDiskFile } from "@remix-run/node";
export async function submitJob(
rawFormData: FormData,
accessToken: string,
expertiseLevels: ExpertiseLevel[],
) {
const Schema = object({
upload: pipe(
instance(NodeOnDiskFile),
mimeType(
["application/zip", "application/x-zip-compressed"],
"Please upload a zip file",
),
),
kind: optional(picklist(["run", "workflow"]), "workflow"),
});
const obj = Object.fromEntries(rawFormData.entries());
const formData = valibotParse(Schema, obj);
if (formData.kind === "run") {
return submitRunImportJob(formData.upload, accessToken, expertiseLevels);
}
return submitHaddock3Job(formData.upload, accessToken, expertiseLevels);
}
async function submitHaddock3Job(
upload: File,
accessToken: string,
expertiseLevels: ExpertiseLevel[],
) {
const rewritten_blob = await rewriteConfigInArchive(upload, expertiseLevels);
const rewritten_upload = new File([rewritten_blob], upload.name, {
type: upload.type,
lastModified: upload.lastModified,
});
const client = createClient(accessToken);
const { response } = await client.PUT("/api/application/haddock3", {
redirect: "manual",
body: { upload: rewritten_upload },
bodySerializer: multipart,
});
return getJobByRedirect(response, accessToken);
}
async function submitRunImportJob(
upload: File,
accessToken: string,
expertiseLevels: ExpertiseLevel[],
) {
if (!expertiseLevels.includes("guru")) {
throw new ForbiddenError(
"You don't have permission to submit run import jobs",
);
}
const client = createClient(accessToken);
const { response } = await client.PUT("/api/application/runimport", {
redirect: "manual",
body: { upload },
bodySerializer: multipart,
});
return getJobByRedirect(response, accessToken);
}
async function getJobByRedirect(response: Response, accessToken: string) {
if (!response.ok && response.status !== 303) {
throw new BartenderError(
`Unable to submit job: ${response.status} ${
response.statusText
} ${await response.text()}`,
{ cause: response },
);
}
const jobUrl = response.headers.get("Location");
// url is like /api/job/1234
const jobId = parseInt(jobUrl!.split("/").pop()!);
return getJobById(jobId, accessToken);
}
/**
* Rewrite the workflow config with
*
* * Force run_dir to JOB_OUTPUT_DIR
* -> To make run_dir predictable so results can be shown
* * Force mode to 'local' and remove remove all other fields in execution group
* -> To make the web site in charge of how job is executed
* * Force postprocess to true
* -> To have some html files in run_dir that can be shown
*
* @param config_body Body of workflow config file to rewrite
* @returns The rewritten config file
*/
function rewriteConfig(table: ReturnType<typeof parse>) {
table.run_dir = JOB_OUTPUT_DIR;
table.mode = "local";
table.postprocess = true;
table.clean = true;
table.offline = false;
table.debug = false;
const haddock3_ncores = getNCores();
if (haddock3_ncores > 0) {
table.ncores = haddock3_ncores;
} else {
delete table.ncores;
}
delete table.max_cpus;
delete table.batch_type;
delete table.queue;
delete table.queue_limit;
delete table.concat;
delete table.self_contained;
delete table.cns_exec;
// force plot_matrix to true for clustfcc, clustrmsd modules
Object.entries(table).forEach(([key, value]) => {
if (typeof value === "object" && value !== null) {
if (key.startsWith("clustfcc") || key.startsWith("clustrmsd")) {
(value as Record<string, boolean>).plot_matrix = true;
}
if (key.startsWith("alascan")) {
(value as Record<string, boolean>).plot = true;
(value as Record<string, boolean>).output = true;
}
}
});
return table;
}
/**
*
* @returns Number of cores to use for haddock3. 0 means use default.
*/
function getNCores() {
let haddock3_ncores = 0;
if (process.env.NODE_ENV === "test") {
return 1;
}
if (
process.env.HADDOCK3_NCORES !== undefined &&
process.env.HADDOCK3_NCORES !== ""
) {
haddock3_ncores = parseInt(process.env.HADDOCK3_NCORES);
if (isNaN(haddock3_ncores)) {
throw new Error(
`HADDOCK3_NCORES env var is not a number: ${process.env.HADDOCK3_NCORES}`,
);
}
}
return haddock3_ncores;
}
function parseToml(toml: string) {
try {
return parse(dedupWorkflow(toml), { bigint: false });
} catch (error) {
if (error instanceof Error) {
throw new InvalidUploadError(`Invalid ${WORKFLOW_CONFIG_FILENAME} file`, {
cause: error,
});
}
throw error;
}
}
export async function rewriteConfigInArchive(
upload: Blob,
expertiseLevels: ExpertiseLevel[],
) {
const zip = new JSZip();
// Tried to give upload blob directly to loadAsync, but failed with
// Error: Can't read the data of 'the loaded zip file'. Is it in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?
// however converting to array buffer works, but will load entire file into memory
try {
await zip.loadAsync(await upload.arrayBuffer());
} catch (e) {
if (e instanceof Error) {
throw new InvalidUploadError(`Unable to read archive`, {
cause: e,
});
}
throw e;
}
const config_file = zip.file(WORKFLOW_CONFIG_FILENAME);
if (config_file === null) {
throw new InvalidUploadError(
`Unable to find ${WORKFLOW_CONFIG_FILENAME} in archive`,
);
}
const config_body = await config_file.async("string");
// Keep backup of original config, before rewriting it
zip.file(`${WORKFLOW_CONFIG_FILENAME}.orig`, config_body);
const table = parseToml(config_body);
const new_table = rewriteConfig(table);
const new_config = parseWorkflowFromTable(new_table, getCatalog("guru"));
await validateWorkflowAgainstExpertiseLevels(
new_config,
expertiseLevels,
zip,
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const new_config_body = stringify(new_table as any, {
newline: "\n",
indent: 2,
integer: Number.MAX_SAFE_INTEGER,
});
zip.file(WORKFLOW_CONFIG_FILENAME, new_config_body);
return await zip.generateAsync({ type: "blob" });
}
async function validateWorkflowAgainstExpertiseLevels(
workflow: IWorkflow,
expertiseLevels: ExpertiseLevel[],
zip: JSZip,
) {
const files = await filesFromZip(zip);
let errors: Errors = [];
if (expertiseLevels.length === 0) {
throw new ValidationError("No expertise levels provided", []);
}
for (const expertiseLevel of expertiseLevels) {
const catalog = getCatalog(expertiseLevel);
errors = await validateWorkflow(workflow, catalog, files);
if (errors.length === 0) {
// If workflow is valid for any expertise level, then it's valid
return;
}
}
throw new ValidationError("Invalid workflow", errors);
}
async function filesFromZip(zip: JSZip): Promise<IFiles> {
const blobs: Promise<[string, string]>[] = [];
zip.forEach((relativePath, file) => {
if (
relativePath === WORKFLOW_CONFIG_FILENAME ||
relativePath === `${WORKFLOW_CONFIG_FILENAME}.orig`
) {
// Skip workflow config file
return;
}
blobs.push(
file.async("blob").then((blob) => blob2dataurl(blob, relativePath)),
);
});
const dataurls = await Promise.all(blobs);
return Object.fromEntries(dataurls);
}
async function blob2dataurl(
value: Blob,
path: string,
): Promise<[string, string]> {
const base64 = Buffer.from(await value.arrayBuffer()).toString("base64");
const dataurl = `data:${value.type};name=${path};base64,${base64}`;
return [path, dataurl];
}