2021-09-10 07:57:49 +00:00
|
|
|
//
|
|
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
|
|
// Copyright © 2021 Hardcore Engineering Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public 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 https://www.eclipse.org/legal/epl-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 permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
2021-11-29 11:09:55 +00:00
|
|
|
import contact from '@anticrm/contact'
|
2021-12-06 16:57:35 +00:00
|
|
|
import core, { DOMAIN_TX, Tx } from '@anticrm/core'
|
2022-01-24 18:30:13 +00:00
|
|
|
import builder, { version } from '@anticrm/model-all'
|
|
|
|
import { upgradeModel } from '@anticrm/server-tool'
|
2021-11-29 11:41:36 +00:00
|
|
|
import { existsSync } from 'fs'
|
2021-12-08 09:08:34 +00:00
|
|
|
import { mkdir, open, readFile, writeFile } from 'fs/promises'
|
2021-12-17 09:09:26 +00:00
|
|
|
import { Client } from 'minio'
|
2021-12-06 16:57:35 +00:00
|
|
|
import { Document, MongoClient } from 'mongodb'
|
2021-11-29 11:41:36 +00:00
|
|
|
import { join } from 'path'
|
2022-01-24 18:30:13 +00:00
|
|
|
import { rebuildElastic } from './elastic'
|
2021-12-14 18:34:18 +00:00
|
|
|
import { generateModelDiff, printDiff } from './mdiff'
|
2021-12-17 09:09:26 +00:00
|
|
|
import { listMinioObjects, MinioWorkspaceItem } from './minio'
|
2021-12-07 18:45:11 +00:00
|
|
|
interface CollectionInfo {
|
|
|
|
name: string
|
|
|
|
file: string
|
|
|
|
}
|
|
|
|
|
|
|
|
interface WorkspaceInfo {
|
|
|
|
version: string
|
|
|
|
collections: CollectionInfo[]
|
|
|
|
minioData: MinioWorkspaceItem[]
|
|
|
|
}
|
|
|
|
|
2021-11-29 11:41:36 +00:00
|
|
|
/**
|
2021-12-02 14:21:41 +00:00
|
|
|
* @public
|
|
|
|
*/
|
2021-11-29 11:41:36 +00:00
|
|
|
export async function dumpWorkspace (mongoUrl: string, dbName: string, fileName: string, minio: Client): Promise<void> {
|
|
|
|
const client = new MongoClient(mongoUrl)
|
|
|
|
try {
|
|
|
|
await client.connect()
|
|
|
|
const db = client.db(dbName)
|
|
|
|
|
|
|
|
console.log('dumping transactions...')
|
|
|
|
|
2021-12-07 18:45:11 +00:00
|
|
|
if (!existsSync(fileName)) {
|
|
|
|
await mkdir(fileName, { recursive: true })
|
|
|
|
}
|
|
|
|
|
|
|
|
const workspaceInfo: WorkspaceInfo = {
|
2022-01-24 18:30:13 +00:00
|
|
|
version: `${version.major}.${version.minor}.${version.patch}`,
|
2021-12-07 18:45:11 +00:00
|
|
|
collections: [],
|
|
|
|
minioData: []
|
|
|
|
}
|
|
|
|
const collections = await db.collections()
|
|
|
|
for (const c of collections) {
|
|
|
|
const docs = await c.find().toArray()
|
|
|
|
workspaceInfo.collections.push({ name: c.collectionName, file: c.collectionName + '.json' })
|
|
|
|
await writeFile(fileName + c.collectionName + '.json', JSON.stringify(docs, undefined, 2))
|
|
|
|
}
|
2021-11-29 11:41:36 +00:00
|
|
|
|
|
|
|
console.log('Dump minio objects')
|
|
|
|
if (await minio.bucketExists(dbName)) {
|
2021-12-07 18:45:11 +00:00
|
|
|
workspaceInfo.minioData.push(...(await listMinioObjects(minio, dbName)))
|
2021-11-29 15:19:03 +00:00
|
|
|
const minioDbLocation = fileName + '.minio'
|
2021-11-29 11:41:36 +00:00
|
|
|
if (!existsSync(minioDbLocation)) {
|
|
|
|
await mkdir(minioDbLocation)
|
|
|
|
}
|
2021-12-07 18:45:11 +00:00
|
|
|
for (const d of workspaceInfo.minioData) {
|
|
|
|
const stat = await minio.statObject(dbName, d.name)
|
|
|
|
d.metaData = stat.metaData
|
2021-12-08 09:08:34 +00:00
|
|
|
|
|
|
|
const fileHandle = await open(join(minioDbLocation, d.name), 'w')
|
|
|
|
|
|
|
|
const data = await minio.getObject(dbName, d.name)
|
|
|
|
const chunks: Buffer[] = []
|
|
|
|
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
data.on('readable', () => {
|
|
|
|
let chunk
|
|
|
|
while ((chunk = data.read()) !== null) {
|
2021-12-17 09:09:26 +00:00
|
|
|
const b = chunk as Buffer
|
2021-12-08 09:08:34 +00:00
|
|
|
chunks.push(b)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
data.on('end', () => {
|
|
|
|
resolve(null)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
for (const b of chunks) {
|
|
|
|
await fileHandle.write(b)
|
2021-11-29 11:41:36 +00:00
|
|
|
}
|
2021-12-08 09:08:34 +00:00
|
|
|
await fileHandle.close()
|
2021-11-29 11:41:36 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-07 18:45:11 +00:00
|
|
|
|
|
|
|
await writeFile(fileName + '.workspace.json', JSON.stringify(workspaceInfo, undefined, 2))
|
|
|
|
} finally {
|
|
|
|
await client.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:09:26 +00:00
|
|
|
export async function restoreWorkspace (
|
|
|
|
mongoUrl: string,
|
|
|
|
dbName: string,
|
|
|
|
fileName: string,
|
|
|
|
minio: Client,
|
2022-01-05 09:07:01 +00:00
|
|
|
elasticUrl: string,
|
|
|
|
transactorUrl: string
|
2021-12-17 09:09:26 +00:00
|
|
|
): Promise<void> {
|
2022-01-26 10:29:22 +00:00
|
|
|
console.log('Restoring workspace', mongoUrl, dbName, fileName)
|
2021-12-07 18:45:11 +00:00
|
|
|
const client = new MongoClient(mongoUrl)
|
|
|
|
try {
|
|
|
|
await client.connect()
|
|
|
|
const db = client.db(dbName)
|
|
|
|
|
|
|
|
const workspaceInfo = JSON.parse((await readFile(fileName + '.workspace.json')).toString()) as WorkspaceInfo
|
|
|
|
|
|
|
|
// Drop existing collections
|
|
|
|
|
|
|
|
const cols = await db.collections()
|
|
|
|
for (const c of cols) {
|
2022-01-05 09:07:01 +00:00
|
|
|
console.log('dropping existing table', c.collectionName)
|
2021-12-07 18:45:11 +00:00
|
|
|
await db.dropCollection(c.collectionName)
|
|
|
|
}
|
|
|
|
// Restore collections.
|
|
|
|
for (const c of workspaceInfo.collections) {
|
|
|
|
const collection = db.collection(c.name)
|
|
|
|
await collection.deleteMany({})
|
|
|
|
const data = JSON.parse((await readFile(fileName + c.name + '.json')).toString()) as Document[]
|
2021-12-22 09:02:51 +00:00
|
|
|
if (data.length > 0) {
|
2022-01-05 09:07:01 +00:00
|
|
|
console.log('restore existing collection', c.name, data.length)
|
2021-12-22 09:02:51 +00:00
|
|
|
await collection.insertMany(data)
|
|
|
|
}
|
2021-12-07 18:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (await minio.bucketExists(dbName)) {
|
2021-12-17 09:09:26 +00:00
|
|
|
const objectNames = (await listMinioObjects(minio, dbName)).map((i) => i.name)
|
2021-12-07 18:45:11 +00:00
|
|
|
await minio.removeObjects(dbName, objectNames)
|
|
|
|
await minio.removeBucket(dbName)
|
|
|
|
}
|
|
|
|
await minio.makeBucket(dbName, 'k8s')
|
|
|
|
|
|
|
|
const minioDbLocation = fileName + '.minio'
|
2022-01-05 09:07:01 +00:00
|
|
|
console.log('Restore minio objects', workspaceInfo.minioData.length)
|
2022-01-12 09:06:04 +00:00
|
|
|
let promises: Promise<void>[] = []
|
2021-12-07 18:45:11 +00:00
|
|
|
for (const d of workspaceInfo.minioData) {
|
2022-01-12 09:06:04 +00:00
|
|
|
const file = await open(join(minioDbLocation, d.name), 'r')
|
|
|
|
const stream = file.createReadStream()
|
|
|
|
promises.push(minio.putObject(dbName, d.name, stream, d.size, d.metaData).then(async () => {
|
|
|
|
await file.close()
|
|
|
|
}))
|
|
|
|
if (promises.length > 10) {
|
|
|
|
await Promise.all(promises)
|
|
|
|
promises = []
|
|
|
|
}
|
2021-12-07 18:45:11 +00:00
|
|
|
}
|
2021-12-17 09:09:26 +00:00
|
|
|
|
2022-01-26 10:29:22 +00:00
|
|
|
await upgradeModel(transactorUrl, dbName)
|
2022-01-05 09:07:01 +00:00
|
|
|
|
2021-12-17 09:09:26 +00:00
|
|
|
await rebuildElastic(mongoUrl, dbName, minio, elasticUrl)
|
2021-11-29 11:41:36 +00:00
|
|
|
} finally {
|
|
|
|
await client.close()
|
|
|
|
}
|
|
|
|
}
|
2021-12-14 18:34:18 +00:00
|
|
|
|
|
|
|
export async function diffWorkspace (mongoUrl: string, dbName: string): Promise<void> {
|
|
|
|
const client = new MongoClient(mongoUrl)
|
|
|
|
try {
|
|
|
|
await client.connect()
|
|
|
|
const db = client.db(dbName)
|
|
|
|
|
|
|
|
console.log('diffing transactions...')
|
|
|
|
|
2021-12-17 09:09:26 +00:00
|
|
|
const currentModel = await db
|
|
|
|
.collection(DOMAIN_TX)
|
|
|
|
.find<Tx>({
|
2021-12-14 18:34:18 +00:00
|
|
|
objectSpace: core.space.Model,
|
|
|
|
modifiedBy: core.account.System,
|
|
|
|
objectClass: { $ne: contact.class.EmployeeAccount }
|
2021-12-17 09:09:26 +00:00
|
|
|
})
|
|
|
|
.toArray()
|
2021-12-14 18:34:18 +00:00
|
|
|
|
2021-12-17 09:09:26 +00:00
|
|
|
const txes = builder.getTxes().filter((tx) => {
|
|
|
|
return (
|
|
|
|
tx.objectSpace === core.space.Model &&
|
2021-12-14 18:34:18 +00:00
|
|
|
tx.modifiedBy === core.account.System &&
|
|
|
|
(tx as any).objectClass !== contact.class.EmployeeAccount
|
2021-12-17 09:09:26 +00:00
|
|
|
)
|
2021-12-14 18:34:18 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const { diffTx, dropTx } = await generateModelDiff(currentModel, txes)
|
|
|
|
if (diffTx.length > 0) {
|
|
|
|
console.log('DIFF Transactions:')
|
|
|
|
|
|
|
|
printDiff(diffTx)
|
|
|
|
}
|
|
|
|
if (dropTx.length > 0) {
|
|
|
|
console.log('Broken Transactions:')
|
|
|
|
for (const tx of dropTx) {
|
|
|
|
console.log(JSON.stringify(tx, undefined, 2))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
await client.close()
|
|
|
|
}
|
|
|
|
}
|