-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
212 lines (184 loc) · 6.64 KB
/
index.js
File metadata and controls
212 lines (184 loc) · 6.64 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
#! /bin/node
/*!
* Copyright 2018 Apereo Foundation (AF) Licensed under the
* Educational Community License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://opensource.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
console.log('Copying tenant resources');
* permissions and limitations under the License.
*/
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const logger = require('./logger');
const { Store } = require('./store');
const { initConnection } = require('./db');
const rsync = require('./rsync');
const createSchemaQueries = require('./schema');
const DO_RSYNC = false;
const DO_CQL_COPY = false;
const DO_MAINTENANCE = true;
const { copyPrincipals, copyPrincipalsByEmail } = require('./principals/dao');
const { copyFolders, copyFoldersGroupIds } = require('./folders/dao');
const { copyTenant, copyTenantConfig } = require('./tenants/dao');
const {
copyDiscussions,
copyMessageBoxMessages,
copyMessageBoxMessagesDeleted,
copyMessageBoxRecentContributions,
copyMessages
} = require('./messages/dao');
const {
copyContent,
copyRevisionByContent,
copyRevisions,
copyEtherpadContent
} = require('./content/dao');
const { copyAuthzMembers, copyAuthzRoles } = require('./roles/dao');
const { copyUsersGroupVisits } = require('./groups/dao');
const { copyFollowingUsersFollowers, copyFollowingUsersFollowing } = require('./following/dao');
const {
copyAuthenticationLoginId,
copyAuthenticationUserLoginId,
copyOAuthClients,
copyOAuthClientsByUser
} = require('./authentication/dao');
const {
copyAuthzInvitations,
copyAuthzInvitationsEmailByToken,
copyAuthzInvitationsResourceIdByEmail,
copyAuthzInvitationsTokenByEmail
} = require('./invitations/dao');
const { copyLibraryIndex } = require('./library/dao');
const { doGroupMaintenance } = require('./maintenance/api');
const SOURCE_CONFIG_FILE = 'source.json';
const DESTINATION_CONFIG_FILE = 'destination.json';
// eslint-disable-next-line no-new
new Store();
Store.init();
const makeSureTablesExistOnTarget = async function(targetClient, createTablesStatements) {
const allPromises = [];
for (let i = 0; i < createTablesStatements.length; i++) {
const eachCreateTableStatement = createTablesStatements[i];
allPromises.push(
new Promise(resolve => {
resolve(targetClient.execute(eachCreateTableStatement.query));
})
);
}
logger.info(`${chalk.green(`✓`)} Creating tables...\n`);
await Promise.all(allPromises);
};
const runDatabaseCopy = async function(...args) {
// Some of these are run one after the other, some are done concurrently
// the ones done isolated are the ones that set variables which other queries depend on
// all the others are just ran at the same time, so to maximize throughput
async function copyTenantDataAndConfig(...args) {
await Promise.all([copyTenant(...args), copyTenantConfig(...args)]);
}
async function copyTenantPrincipals(...args) {
await copyPrincipals(...args);
await copyPrincipalsByEmail(...args);
}
async function copyTenantResources(...args) {
await Promise.all([copyAuthzMembers(...args), copyAuthzRoles(...args)]);
}
async function copyTenantFolders(...args) {
await copyFolders(...args);
await copyFoldersGroupIds(...args);
}
async function copyTenantContent(...args) {
await copyContent(...args);
await copyEtherpadContent(...args);
await copyRevisionByContent(...args);
await copyRevisions(...args);
}
async function copyTenantDiscussions(...args) {
await Promise.all([copyDiscussions(...args), copyMessages(...args)]);
await Promise.all([
copyMessageBoxMessages(...args),
copyMessageBoxMessagesDeleted(...args),
copyMessageBoxRecentContributions(...args)
]);
}
async function copyTenantGroupsAndFollowers(...args) {
await Promise.all([
copyUsersGroupVisits(...args),
copyFollowingUsersFollowers(...args),
copyFollowingUsersFollowing(...args)
]);
}
async function copyTenantAuthenticationSettings(...args) {
await Promise.all([
await copyOAuthClientsByUser(...args),
await copyAuthenticationUserLoginId(...args)
]);
await Promise.all([await copyOAuthClients(...args), await copyAuthenticationLoginId(...args)]);
}
async function copyTenantInvitations(...args) {
await copyAuthzInvitations(...args);
await Promise.all([
copyAuthzInvitationsResourceIdByEmail(...args),
copyAuthzInvitationsTokenByEmail(...args)
]);
await copyAuthzInvitationsEmailByToken(...args);
}
await copyTenantDataAndConfig(...args);
await copyTenantFolders(...args);
await copyTenantPrincipals(...args);
await copyTenantResources(...args);
await copyTenantContent(...args);
await copyTenantDiscussions(...args);
await copyTenantGroupsAndFollowers(...args);
await copyTenantAuthenticationSettings(...args);
await copyTenantInvitations(...args);
// TODO still experimental, merge with the previous if statement later
await copyLibraryIndex(...args);
};
const parseConfiguration = function(filename) {
const fileContents = fs.readFileSync(path.join(__dirname, filename));
const { database, files } = JSON.parse(fileContents);
return {
database,
files
};
};
const init = async function() {
try {
const source = parseConfiguration(SOURCE_CONFIG_FILE);
const sourceConnection = await initConnection(source.database);
source.client = sourceConnection;
const destination = parseConfiguration(DESTINATION_CONFIG_FILE);
const destinationConnection = await initConnection(destination.database);
destination.client = destinationConnection;
if (DO_CQL_COPY) {
await makeSureTablesExistOnTarget(destinationConnection, createSchemaQueries);
await runDatabaseCopy(source, destination);
}
if (DO_MAINTENANCE) {
await doGroupMaintenance(source, destination);
}
// Rsync the files
const contentTypes = ['c', 'f', 'u', 'g'];
if (DO_RSYNC) {
await rsync.transferFiles(source, destination, contentTypes);
await rsync.transferAssets(source, destination);
}
logger.info(`${chalk.green(`✓`)} Exiting.`);
} catch (error) {
logger.error(`${chalk.red(`✗`)} Something went wrong: `);
logger.error(error.stack);
process.exit(-1);
} finally {
logger.end();
process.exit(0);
}
};
init();