-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
358 lines (321 loc) · 9.11 KB
/
index.js
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
const EventEmitter = require('events')
const url = require('url')
const qrate = require('qrate')
const ccurllib = require('ccurllib')
const cliProgress = require('cli-progress')
const pkg = require('./package.json')
const h = {
'user-agent': `${pkg.name}@${pkg.version}`,
'content-type': 'application/json'
}
const isEmptyObject = function (obj) {
return typeof obj === 'object' && Object.keys(obj).length === 0
}
// extend a URL by adding a database name
// Handles present or absent trailing slash
const extendURL = function (url, dbname) {
if (url.match(/\/$/)) {
return url + encodeURIComponent(dbname)
} else {
return url + '/' + encodeURIComponent(dbname)
}
}
// get source document count before we start
const getStartInfo = async function (status) {
const req = {
url: status.sourceURL,
method: 'get',
headers: h
}
const response = await ccurllib.request(req)
return response.result
}
// create the _replicator database
const createReplicator = async function (u) {
const req = {
method: 'put',
url: extendURL(u, '_replicator'),
headers: h
}
try {
await ccurllib.request(req)
} catch (e) {
// do nothing - _replicator exists already
}
return true
}
// start replicating by creating a _replicator document
const startReplication = async function (status, docId, sourceURL, targetURL, live) {
// start the replication
const obj = {
_id: docId,
source: sourceURL,
target: targetURL,
create_target: true,
continuous: live
}
const req = {
method: 'post',
url: status.replicatorURL,
headers: h,
body: JSON.stringify(obj)
}
const response = await ccurllib.request(req)
return response.result
}
const getReplStatus = async (status) => {
const req = {
method: 'get',
headers: h,
url: extendURL(status.replicatorURL, status.docId)
}
const response = await ccurllib.request(req)
return response.result
}
const getTargetInfo = async (status) => {
const req = {
method: 'get',
headers: h,
url: status.targetURL
}
const response = await ccurllib.request(req)
return response.result
}
// fetch the replication document and the target database's info
const fetchReplicationStatusDocs = async function (status) {
// target database
const data = await Promise.allSettled([
getReplStatus(status),
getTargetInfo(status)
])
if (data[0].status === 'fulfilled') {
status.status = data[0].value._replication_state || 'new'
if (typeof data[0].value._replication_stats === 'object') {
status.docFail = data[0].value._replication_stats.doc_write_failures
}
}
if (data[1].status === 'fulfilled') {
status.targetDocCount = data[1].value.doc_count + data[1].value.doc_del_count
}
if (process.env.DEBUG === 'couchreplicate') {
console.error(JSON.stringify(data))
}
return status
}
const wait = async function (ms) {
return new Promise((resolve, reject) => {
setTimeout(resolve, ms)
})
}
// poll the replication status until it finishes correctly or in error
const monitorReplication = async function (status, ee) {
let finished = false
do {
await fetchReplicationStatusDocs(status)
if (status.status === 'error' || status.status === 'failed') {
status.error = true
finished = true
}
if (status.status === 'completed') {
finished = true
}
if (status.sourceDocCount > 0) {
status.percent = status.targetDocCount / status.sourceDocCount
}
ee.emit('status', status)
// console.log('status', status)
if (!finished) {
await wait(5000)
}
} while (!finished)
if (status.error) {
status.status = 'error'
ee.emit('status', status)
ee.emit('error', status)
} else {
ee.emit('completed', status)
}
}
// migrate the _security document from the source to the target
const migrateAuth = async function (opts) {
const securityDoc = '_security'
// establish the source account's username
const parsed = new url.URL(opts.sourceURL)
let username = null
if (parsed.auth) {
username = parsed.auth.split(':')[0]
}
// fetch the source database's _security document
let req = {
method: 'get',
url: extendURL(opts.sourceURL, securityDoc),
headers: h
}
let response = await ccurllib.request(req)
let data = response.result
// if it's empty, do nothing
if (isEmptyObject(data)) {
return
}
// remove any reference to the source database's username
if (username && typeof data.cloudant === 'object') {
delete data.cloudant[username]
}
data._id = securityDoc
req = {
method: 'put',
url: extendURL(opts.targetURL, securityDoc),
headers: h
}
response = await ccurllib.request(req)
}
// migrate a single database from source ---> target
const migrateSingleDB = async function (opts) {
// sanity check URLs
const sourceParsed = new url.URL(opts.source)
const targetParsed = new url.URL(opts.target)
// check source URL
if (!sourceParsed.protocol || !sourceParsed.hostname) {
throw new Error('invalid source URL')
}
// check target URL
if (!targetParsed.protocol || !targetParsed.hostname) {
throw new Error('invalid target URL')
}
// we return an event emitter so we can give real-time updates
const ee = opts.ee || new EventEmitter()
// extract dbname
const rparsed = new url.URL(opts.source)
// turn source URL into '_replicator' database
const dbname = decodeURIComponent(sourceParsed.pathname.replace(/^\//, ''))
rparsed.pathname = rparsed.path = '/_replicator'
// status object
const status = {
replicatorURL: rparsed.href,
sourceURL: opts.source,
targetURL: opts.target,
dbname,
docId: dbname.replace(/[^a-zA-Z0-9]/g, '') + '_' + (new Date()).getTime(),
status: 'new',
sourceDocCount: 0,
targetDocCount: 0,
docFail: 0,
percent: 0,
error: false,
live: opts.live
}
const info = await getStartInfo(status)
status.sourceDocCount = info.doc_count + info.doc_del_count
await startReplication(status, status.docId, status.sourceURL, status.targetURL, status.live)
ee.emit('status', status)
// optionally migrate the auth document
if (opts.auth) {
await migrateAuth(status)
}
// monitor the replication
if (opts.nomonitor && opts.live) {
return status
} else {
try {
await monitorReplication(status, ee)
} catch (e) {
let msg = 'error'
if (e.error && e.error.reason) {
msg += ' - ' + e.error.reason
}
status.status = msg
status.error = true
ee.emit('status', status)
ee.emit('error', msg)
}
}
}
// migrate a list of documents from source --> target
const migrateList = async function (opts) {
// enforce maximum number of continuous replications
if (opts.live && opts.databases.length > 50) {
throw new Error('Maximum number of continuous replications is fifty')
}
// ignore concurrency in live mode
if (opts.live) {
opts.concurrency = 50
}
// progress bar
let multibar
if (!opts.quiet) {
// create new container
multibar = new cliProgress.MultiBar({
clearOnComplete: false,
hideCursor: true,
format: '{dbname} {bar} | {status} | ETA: {eta_formatted} | {percentage}%'
}, cliProgress.Presets.shades_grey)
}
// get database names
return new Promise((resolve, reject) => {
// async queue of migrations
const q = qrate(async (dbname) => {
const newopts = JSON.parse(JSON.stringify(opts))
if (!newopts.quiet) {
newopts.bar = multibar.create(100, 0)
newopts.bar.update(0, { dbname, status: '_' })
}
if (!opts.skipExtend) {
newopts.source = extendURL(newopts.source, dbname)
newopts.target = extendURL(newopts.target, dbname)
}
newopts.ee = new EventEmitter()
newopts.ee.on('status', (s) => {
if (!newopts.quiet) {
newopts.bar.update(Math.floor(s.percent * 100), { dbname, status: s.status })
}
}).on('completed', (s) => {
if (!newopts.quiet) {
newopts.bar.update(100, { dbname, status: s.status })
}
})
await migrateSingleDB(newopts)
}, opts.concurrency)
// push to the queue
for (const i in opts.databases) {
const dbname = opts.databases[i]
if (!dbname.match(/^_/)) {
q.push(dbname)
}
}
// when the queue is drained, we're done
q.drain = () => {
resolve()
if (!opts.quiet) {
multibar.stop()
}
}
})
}
// migrate all documents
const migrateAll = async function (opts) {
// get db names and push to the queue
const req = {
url: extendURL(opts.source, '_all_dbs'),
method: 'get',
headers: h
}
const response = await ccurllib.request(req)
const data = response.result
opts.databases = data
await migrateList(opts)
}
// migrate a single database
const migrateDB = async function (opts) {
// convert to a list of database names to avoid code duplication
const sourceParsed = new url.URL(opts.source)
const dbname = decodeURIComponent(sourceParsed.pathname.replace(/^\//, ''))
opts.databases = [dbname]
opts.skipExtend = true
await migrateList(opts)
}
module.exports = {
migrateDB,
migrateList,
migrateAll,
createReplicator
}