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.
|
|
|
|
//
|
|
|
|
|
|
|
|
import { MongoClient, Document } from 'mongodb'
|
|
|
|
import core, { DOMAIN_TX, Tx } from '@anticrm/core'
|
|
|
|
import { createContributingClient } from '@anticrm/contrib'
|
|
|
|
import { encode } from 'jwt-simple'
|
2021-11-29 11:41:36 +00:00
|
|
|
import { BucketItem, Client } from 'minio'
|
2021-11-29 11:09:55 +00:00
|
|
|
import contact from '@anticrm/contact'
|
2021-09-10 07:57:49 +00:00
|
|
|
|
2021-11-30 11:05:11 +00:00
|
|
|
import builder from '@anticrm/model-all'
|
2021-11-29 11:41:36 +00:00
|
|
|
import { existsSync } from 'fs'
|
|
|
|
import { mkdir, writeFile } from 'fs/promises'
|
|
|
|
import { join } from 'path'
|
2021-09-10 07:57:49 +00:00
|
|
|
|
2021-12-01 09:28:26 +00:00
|
|
|
const txes = JSON.parse(JSON.stringify(builder.getTxes())) as Tx[]
|
2021-09-10 07:57:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export async function initWorkspace (mongoUrl: string, dbName: string, clientUrl: string, minio: Client): Promise<void> {
|
|
|
|
const client = new MongoClient(mongoUrl)
|
|
|
|
try {
|
|
|
|
await client.connect()
|
|
|
|
const db = client.db(dbName)
|
|
|
|
|
|
|
|
console.log('dropping database...')
|
|
|
|
await db.dropDatabase()
|
|
|
|
|
|
|
|
console.log('creating model...')
|
|
|
|
const model = txes.filter(tx => tx.objectSpace === core.space.Model)
|
|
|
|
const result = await db.collection(DOMAIN_TX).insertMany(model as Document[])
|
|
|
|
console.log(`${result.insertedCount} model transactions inserted.`)
|
|
|
|
|
|
|
|
console.log('creating data...')
|
|
|
|
const data = txes.filter(tx => tx.objectSpace !== core.space.Model)
|
|
|
|
const token = encode({ email: 'anticrm@hc.engineering', workspace: dbName }, 'secret')
|
|
|
|
const url = new URL(`/${token}`, clientUrl)
|
|
|
|
const contrib = await createContributingClient(url.href)
|
|
|
|
for (const tx of data) {
|
|
|
|
await contrib.tx(tx)
|
|
|
|
}
|
|
|
|
contrib.close()
|
|
|
|
|
|
|
|
console.log('create minio bucket')
|
|
|
|
if (!await minio.bucketExists(dbName)) { await minio.makeBucket(dbName, 'k8s') }
|
|
|
|
} finally {
|
|
|
|
await client.close()
|
|
|
|
}
|
|
|
|
}
|
2021-11-29 11:09:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export async function upgradeWorkspace (mongoUrl: string, dbName: string, clientUrl: string, minio: Client): Promise<void> {
|
|
|
|
const client = new MongoClient(mongoUrl)
|
|
|
|
try {
|
|
|
|
await client.connect()
|
|
|
|
const db = client.db(dbName)
|
|
|
|
|
|
|
|
console.log('removing model...')
|
|
|
|
// we're preserving accounts (created by core.account.System).
|
|
|
|
const result = await db.collection(DOMAIN_TX).deleteMany({
|
|
|
|
objectSpace: core.space.Model,
|
|
|
|
modifiedBy: core.account.System,
|
2021-11-29 15:19:03 +00:00
|
|
|
objectClass: { $ne: contact.class.EmployeeAccount }
|
2021-11-29 11:09:55 +00:00
|
|
|
})
|
|
|
|
console.log(`${result.deletedCount} transactions deleted.`)
|
|
|
|
|
|
|
|
console.log('creating model...')
|
|
|
|
const model = txes.filter(tx => tx.objectSpace === core.space.Model)
|
|
|
|
const insert = await db.collection(DOMAIN_TX).insertMany(model as Document[])
|
|
|
|
console.log(`${insert.insertedCount} model transactions inserted.`)
|
|
|
|
} finally {
|
|
|
|
await client.close()
|
|
|
|
}
|
|
|
|
}
|
2021-11-29 11:41:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
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...')
|
|
|
|
|
|
|
|
const dbTxes = await db.collection<Tx>(DOMAIN_TX).find().toArray()
|
|
|
|
await writeFile(fileName + '.tx.json', JSON.stringify(dbTxes, undefined, 2))
|
|
|
|
|
|
|
|
console.log('Dump minio objects')
|
|
|
|
if (await minio.bucketExists(dbName)) {
|
|
|
|
const minioData: BucketItem[] = []
|
|
|
|
const list = await minio.listObjects(dbName, undefined, true)
|
|
|
|
await new Promise((resolve) => {
|
|
|
|
list.on('data', data => {
|
|
|
|
minioData.push(data)
|
|
|
|
})
|
|
|
|
list.on('end', () => {
|
|
|
|
resolve(null)
|
|
|
|
})
|
|
|
|
})
|
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)
|
|
|
|
}
|
|
|
|
await writeFile(fileName + '.minio.json', JSON.stringify(minioData, undefined, 2))
|
|
|
|
for (const d of minioData) {
|
|
|
|
const data = await minio.getObject(dbName, d.name)
|
|
|
|
const allData = []
|
|
|
|
let chunk
|
|
|
|
while ((chunk = data.read()) !== null) {
|
|
|
|
allData.push(chunk)
|
|
|
|
}
|
|
|
|
await writeFile(join(minioDbLocation, d.name), allData.join(''))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
await client.close()
|
|
|
|
}
|
|
|
|
}
|