forked from hpi-schul-cloud/schulcloud-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate-etherpads.js
More file actions
248 lines (218 loc) · 7.06 KB
/
migrate-etherpads.js
File metadata and controls
248 lines (218 loc) · 7.06 KB
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
#!/usr/bin/env node
const arg = require('arg');
const appPromise = require('./src/app');
const { Configuration } = require('@hpi-schul-cloud/commons');
const etherpadClient = require('./src/services/etherpad/utils/EtherpadClient.js');
const { randomBytes } = require('crypto');
const { ObjectId } = require('mongodb');
const fs = require('fs').promises;
const date = new Date().toISOString();
const LOGDIR = __dirname + `/etherpad_migration_${date}.log`;
const ONE_RUN_LIMIT = 100_000;
async function log(...theArgs) {
for (let step = 0; step < Object.keys(theArgs).length; step++) {
const date = new Date().toISOString();
await fs.appendFile(LOGDIR, `[${date}]` + ': ' + theArgs[step] + '\n');
}
}
/** *****************************************
* ARGUMENT PARSING
****************************************** */
const args = arg(
{
// Types
'--help': Boolean, // show the help
'-h': '--help',
},
{
permissive: true,
argv: process.argv.slice(2),
}
);
const HELP = `Usage: node migrate-etherpads.js <oldPadDomain>
This script searches for etherpad pads in all lessons with an old pad url.
It then takes those old pads and uses the etherpad api's "copyPad" function
to copy the contents of the old pad to a new grouppad for given course.
Finally it saves the changes to the given lesson.
Example:
node ./migrate-etherpads.js etherpad.hpi-schul-cloud.de
npm run migrate-etherpads -- etherpad.hpi-schul-cloud.de
OPTIONS:
--help (-h) Show this help.
`;
if (args['--help']) {
console.log(HELP);
process.exit(0);
}
/** *****************************************
* HELPER
****************************************** */
const getPadIdFromUrl = (path) => {
path += '';
const parsedUrl = new URL(path);
path = parsedUrl.pathname;
return path.substring(path.lastIndexOf('/') + 1);
};
/** *****************************************
* Progress Bar
****************************************** */
const { bgWhite } = require('chalk');
class ProgressBar {
constructor() {
this.total;
this.current = 0;
this.bar_length = 80 - 30; // process.stdout.columns
}
init(total) {
this.total = total;
this.current = 0;
this.update(this.current);
}
update(current = 1) {
this.current += current;
const current_progress = this.current / this.total;
this.draw(current_progress);
}
draw(current_progress) {
const filled_bar_length = (current_progress * this.bar_length).toFixed(0);
const empty_bar_length = this.bar_length - filled_bar_length;
const filled_bar = this.get_bar(filled_bar_length, ' ', bgWhite);
const empty_bar = this.get_bar(empty_bar_length, '-');
//const progressSum = ((current_progress * 100).toFixed(2)) + "%";
const progressSum = this.current + '/' + this.total;
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(`Migrating Etherpads: [${filled_bar}${empty_bar}] | ${progressSum}`);
}
get_bar(length, char, color = (a) => a) {
let str = '';
for (let i = 0; i < length; i++) {
str += char;
}
return color(str);
}
}
function chunkArray(myArray, chunk_size) {
var index = 0;
var arrayLength = myArray.length;
var tempArray = [];
for (index = 0; index < arrayLength; index += chunk_size) {
myChunk = myArray.slice(index, index + chunk_size);
// Do something if you want with the group
tempArray.push(myChunk);
}
return tempArray;
}
/** *****************************************
* MAIN
****************************************** */
const run = async (oldPadDomain) => {
const app = await appPromise;
let searchRegex = new RegExp(`https://${oldPadDomain.replace(/\./g, '\\.')}.*`);
const lessonsService = app.service('/lessons');
const lessonResult = await lessonsService.Model.find({
contents: { $elemMatch: { component: 'Etherpad', 'content.url': searchRegex } },
}).limit(ONE_RUN_LIMIT);
if (lessonResult.length <= 0) {
return Promise.reject('No Pads found to migrate.');
}
const CurrentProgress = new ProgressBar();
CurrentProgress.total = lessonResult.length;
CurrentProgress.update(0);
const lessons = chunkArray(lessonResult, 1);
let errors = false;
for (let index = 0; index < lessons.length; index++) {
const foundLessons = lessons[index];
await Promise.allSettled(
foundLessons.map(async (lesson) => {
let courseId = lesson.courseId;
let groupResult = await etherpadClient.createOrGetGroup({
groupMapper: '' + courseId,
});
let groupID = groupResult.data.groupID;
let overallResult = {
message: `Successful saving lesson ${lesson._id}`,
state: 'resolved',
};
await Promise.allSettled(
lesson.contents.map(async (content) => {
if (content.component === 'Etherpad') {
let oldPadId = getPadIdFromUrl(content.content.url);
if (typeof content.content.title === 'undefined' || content.content.title === '') {
content.content.title = randomBytes(12).toString('hex');
}
let padName = content.content.title;
let createResult = await etherpadClient.createOrGetGroupPad({
groupID,
oldPadId,
padName,
});
let newPadId = createResult.data.padID;
content.content.url = Configuration.get('ETHERPAD_NEW_PAD_URI') + `/${newPadId}`;
await log(
`Successfully migrated Etherpad lesson ${lesson._id} content ${content._id} from /p/${oldPadId} to ${newPadId}`
);
return Promise.resolve(1);
}
})
).then(async (results) => {
await Promise.allSettled(
results.map(async (result, index) => {
if (result.status === 'rejected') {
let contentId = lesson.contents[index]._id;
const error = `lesson ${lesson._id} content ${contentId}: ${result.reason}`;
overallResult = {
message: 'some pads could not be migrated',
state: 'rejected',
};
await log(error);
return Promise.reject(error);
}
return Promise.resolve(1);
})
);
});
let result = await lessonsService.Model.updateOne({ _id: ObjectId(lesson._id) }, lesson);
if (overallResult.state === 'rejected') {
return Promise.reject(overallResult.message);
}
return Promise.resolve(overallResult.message);
})
).then(async (results) => {
await Promise.allSettled(
results.map(async (result, index) => {
if (result.status === 'rejected') {
errors = true;
let lessonId = foundLessons[index]._id;
const error = `Error saving lesson ${lessonId} ${result.reason}`;
await log(error);
return Promise.reject(error);
}
CurrentProgress.update();
await log(result.value);
return Promise.resolve(1);
})
);
});
}
if (errors) {
console.log(`\n\nThere were errors migrating pads from some lessons.\nCheck ${LOGDIR}`);
} else {
console.log(`\n\nLessons are successfully migrated.\nFor additional info Check ${LOGDIR}`);
}
process.stdout.write('\n');
return Promise.resolve(1);
};
const main = () => {
if (args._.length === 0 || args._.length > 1) {
console.log(HELP);
process.exit(0);
}
return new Promise(async (resolve) => {
await run(args._[0]).catch(async (err) => {
console.log(err);
});
process.exit(0);
});
};
main();