-
Notifications
You must be signed in to change notification settings - Fork 6
/
FolderUploader.ts
254 lines (222 loc) · 6.09 KB
/
FolderUploader.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
import { hpcConfigMap } from "../configs/config";
import BaseConnector from "./connectors/BaseConnector";
import DB from "./DB";
import * as fs from "fs";
import * as path from "path";
import GlobusUtil from "./lib/GlobusUtil";
import FolderUtil from "./lib/FolderUtil";
import {
GitFolder,
GlobusFolder,
hpcConfig,
LocalFolder,
NeedUploadFolder,
} from "./types";
import Helper from "./Helper";
import { Folder } from "./models/Folder";
import { Git } from "./models/Git";
import GitUtil from "./lib/GitUtil";
import SlurmConnector from "./connectors/SlurmConnector";
import SingularityConnector from "./connectors/SingularityConnector";
type Connector =
| BaseConnector
| SlurmConnector
| SingularityConnector
export class BaseFolderUploader {
public id: string;
public hpcPath: string;
public globusPath: string;
public hpcName: string;
public userId: string;
public hpcConfig: hpcConfig;
public isComplete: boolean;
public isFailed: boolean;
protected db: DB;
constructor(hpcName: string, userId: string) {
this.hpcName = hpcName;
this.userId = userId;
this.hpcConfig = hpcConfigMap[hpcName];
if (!this.hpcConfig)
throw new Error(`cannot find hpcConfig with name ${hpcName}`);
this.id = Helper.generateId();
this.isComplete = false;
this.isFailed = false;
this.db = new DB();
this.hpcPath = path.join(this.hpcConfig.root_path, this.id);
this.globusPath = path.join(this.hpcConfig.globus.root_path, this.id);
}
async upload() {
throw new Error("FolderUploader upload not implemented");
}
protected async register() {
const connection = await this.db.connect();
const folder = new Folder();
folder.id = this.id;
folder.hpcPath = this.hpcPath;
folder.globusPath = this.globusPath;
folder.hpc = this.hpcName;
folder.userId = this.userId;
await connection.getRepository(Folder).save(folder);
}
}
export class EmptyFolderUploader extends BaseFolderUploader {
protected connector: Connector;
constructor(
hpcName: string,
userId: string,
jobId: string,
connector: Connector
) {
super(hpcName, userId);
this.connector = connector;
}
async upload() {
await this.connector.mkdir(this.hpcPath, {}, true);
await this.register();
this.isComplete = true;
}
}
export class GlobusFolderUploader extends BaseFolderUploader {
private from: GlobusFolder = {};
private to: GlobusFolder = {};
private taskId: string;
private jobId: string;
constructor(
from: GlobusFolder,
hpcName: string,
userId: string,
jobId: string
) {
super(hpcName, userId);
if (!this.hpcConfig)
throw new Error(`cannot find hpcConfig with name ${hpcName}`);
if (!this.hpcConfig.globus)
throw new Error(`cannot find hpcConfig.globus with name ${hpcName}`);
this.from = from;
this.to = {
endpoint: this.hpcConfig.globus.endpoint,
path: this.globusPath,
};
this.jobId = jobId;
}
async upload() {
this.taskId = await GlobusUtil.initTransfer(
this.from,
this.to,
this.hpcConfig,
"job-id-" + this.jobId + "-upload-folder-" + this.id
);
const status = await GlobusUtil.monitorTransfer(
this.taskId,
this.hpcConfig
);
if (status.includes("FAILED")) {
this.isComplete = true;
this.isFailed = true;
}
if (status.includes("SUCCEEDED")) {
this.isComplete = true;
}
if (this.isComplete) {
if (!this.isFailed) {
await this.register();
}
}
}
}
export class LocalFolderUploader extends BaseFolderUploader {
protected connector: Connector;
protected localPath: string;
constructor(
from: LocalFolder,
hpcName: string,
userId: string,
jobId: string,
connector: Connector = null
) {
super(hpcName, userId);
this.localPath = from.localPath;
this.connector = connector ?? new BaseConnector(hpcName);
}
async upload() {
if (!fs.existsSync(this.localPath)) {
throw new Error(`could not find folder under path ${this.localPath}`);
}
const from = await FolderUtil.getZip(this.localPath);
const to = this.hpcPath;
await this.connector.upload(from, to, false);
await FolderUtil.removeZip(from);
await this.register();
this.isComplete = true;
}
}
export class GitFolderUploader extends LocalFolderUploader {
private gitId: string;
private git: Git;
constructor(
from: GitFolder,
hpcName: string,
userId: string,
jobId: string,
connector: Connector = null
) {
const localPath = GitUtil.getLocalPath(from.gitId);
super({ localPath }, hpcName, userId, jobId, connector);
this.gitId = from.gitId;
}
async upload() {
const connection = await this.db.connect();
const gitRepo = connection.getRepository(Git);
this.git = await gitRepo.findOne(this.gitId);
if (!this.git) {
throw new Error(`cannot find git repo with id ${this.gitId}`);
}
await GitUtil.refreshGit(this.git);
await super.upload();
}
}
export class FolderUploaderHelper {
static async upload(
from: NeedUploadFolder,
hpcName: string,
userId: string,
jobId: string = "",
connector: Connector = null
) {
if (!from.type) throw new Error("invalid local file format");
var uploader: BaseFolderUploader;
switch (from.type) {
case "git":
uploader = new GitFolderUploader(
from,
hpcName,
userId,
jobId,
connector
);
await uploader.upload();
break;
case "local":
uploader = new LocalFolderUploader(
from,
hpcName,
userId,
jobId,
connector
);
await uploader.upload();
break;
case "globus":
uploader = new GlobusFolderUploader(from, hpcName, userId, jobId);
await uploader.upload();
break;
case "empty":
uploader = new EmptyFolderUploader(hpcName, userId, jobId, connector);
await uploader.upload();
break;
default:
throw new Error("undefined file type " + from.type);
}
return uploader;
}
}