2021-09-09 21:37:38 +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.
|
|
|
|
//
|
|
|
|
|
2022-01-24 18:30:13 +00:00
|
|
|
import {
|
2021-12-17 09:09:26 +00:00
|
|
|
ACCOUNT_DB,
|
|
|
|
assignWorkspace,
|
|
|
|
createAccount,
|
|
|
|
createWorkspace,
|
|
|
|
dropAccount,
|
|
|
|
dropWorkspace,
|
|
|
|
getAccount,
|
2022-01-24 18:30:13 +00:00
|
|
|
listAccounts,
|
2021-12-27 12:02:31 +00:00
|
|
|
listWorkspaces,
|
2022-01-24 18:30:13 +00:00
|
|
|
upgradeWorkspace
|
2021-12-02 14:21:41 +00:00
|
|
|
} from '@anticrm/account'
|
2022-01-13 12:16:06 +00:00
|
|
|
import { setMetadata } from '@anticrm/platform'
|
2022-01-24 18:30:13 +00:00
|
|
|
import toolPlugin, { prepareTools, version } from '@anticrm/server-tool'
|
2021-12-06 16:57:35 +00:00
|
|
|
import { program } from 'commander'
|
|
|
|
import { Db, MongoClient } from 'mongodb'
|
2021-12-17 09:09:26 +00:00
|
|
|
import { rebuildElastic } from './elastic'
|
2021-12-23 09:02:14 +00:00
|
|
|
import { importXml } from './importer'
|
2021-12-02 14:21:41 +00:00
|
|
|
import { clearTelegramHistory } from './telegram'
|
2022-01-24 18:30:13 +00:00
|
|
|
import { diffWorkspace, dumpWorkspace, restoreWorkspace } from './workspace'
|
2021-09-10 07:57:49 +00:00
|
|
|
|
2022-01-24 18:30:13 +00:00
|
|
|
const { mongodbUri, minio } = prepareTools()
|
2021-09-10 07:57:49 +00:00
|
|
|
|
|
|
|
const transactorUrl = process.env.TRANSACTOR_URL
|
|
|
|
if (transactorUrl === undefined) {
|
|
|
|
console.error('please provide transactor url.')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2021-12-17 09:09:26 +00:00
|
|
|
const elasticUrl = process.env.ELASTIC_URL
|
|
|
|
if (elasticUrl === undefined) {
|
|
|
|
console.error('please provide elastic url')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:30:13 +00:00
|
|
|
setMetadata(toolPlugin.metadata.Endpoint, transactorUrl)
|
|
|
|
setMetadata(toolPlugin.metadata.Transactor, transactorUrl)
|
2021-09-09 21:37:38 +00:00
|
|
|
|
2021-09-10 15:06:51 +00:00
|
|
|
async function withDatabase (uri: string, f: (db: Db, client: MongoClient) => Promise<any>): Promise<void> {
|
2021-09-09 21:37:38 +00:00
|
|
|
console.log(`connecting to database '${uri}'...`)
|
|
|
|
|
|
|
|
const client = await MongoClient.connect(uri)
|
2021-09-10 15:06:51 +00:00
|
|
|
await f(client.db(ACCOUNT_DB), client)
|
2021-09-09 21:37:38 +00:00
|
|
|
await client.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
program.version('0.0.1')
|
|
|
|
|
|
|
|
// create-user john.appleseed@gmail.com --password 123 --workspace workspace --fullname "John Appleseed"
|
|
|
|
program
|
2021-09-10 15:06:51 +00:00
|
|
|
.command('create-account <email>')
|
2021-09-09 21:37:38 +00:00
|
|
|
.description('create user and corresponding account in master database')
|
|
|
|
.requiredOption('-p, --password <password>', 'user password')
|
2021-09-10 15:06:51 +00:00
|
|
|
.requiredOption('-f, --first <first>', 'first name')
|
2022-01-13 12:16:06 +00:00
|
|
|
.requiredOption('-l, --last <last>', 'last name')
|
2021-09-10 07:57:49 +00:00
|
|
|
.action(async (email: string, cmd) => {
|
2021-09-09 21:37:38 +00:00
|
|
|
return await withDatabase(mongodbUri, async (db) => {
|
2021-09-10 15:06:51 +00:00
|
|
|
console.log(`creating account ${cmd.first as string} ${cmd.last as string} (${email})...`)
|
|
|
|
await createAccount(db, email, cmd.password, cmd.first, cmd.last)
|
2021-09-09 21:37:38 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
program
|
|
|
|
.command('assign-workspace <email> <workspace>')
|
|
|
|
.description('assign workspace')
|
2021-09-10 07:57:49 +00:00
|
|
|
.action(async (email: string, workspace: string, cmd) => {
|
2021-09-10 15:06:51 +00:00
|
|
|
return await withDatabase(mongodbUri, async (db, client) => {
|
2021-09-10 07:57:49 +00:00
|
|
|
console.log(`assigning user ${email} to ${workspace}...`)
|
2021-09-09 21:37:38 +00:00
|
|
|
await assignWorkspace(db, email, workspace)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
program
|
|
|
|
.command('show-user <email>')
|
|
|
|
.description('show user')
|
|
|
|
.action(async (email) => {
|
|
|
|
return await withDatabase(mongodbUri, async (db) => {
|
|
|
|
const info = await getAccount(db, email)
|
|
|
|
console.log(info)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
program
|
|
|
|
.command('create-workspace <name>')
|
|
|
|
.description('create workspace')
|
|
|
|
.requiredOption('-o, --organization <organization>', 'organization name')
|
|
|
|
.action(async (workspace, cmd) => {
|
|
|
|
return await withDatabase(mongodbUri, async (db) => {
|
|
|
|
await createWorkspace(db, workspace, cmd.organization)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-29 11:09:55 +00:00
|
|
|
program
|
|
|
|
.command('upgrade-workspace <name>')
|
|
|
|
.description('upgrade workspace')
|
|
|
|
.action(async (workspace, cmd) => {
|
2022-01-24 18:30:13 +00:00
|
|
|
return await withDatabase(mongodbUri, async (db) => {
|
|
|
|
await upgradeWorkspace(db, workspace)
|
|
|
|
})
|
2021-11-29 11:09:55 +00:00
|
|
|
})
|
|
|
|
|
2021-09-10 20:43:51 +00:00
|
|
|
program
|
|
|
|
.command('drop-workspace <name>')
|
|
|
|
.description('drop workspace')
|
|
|
|
.action(async (workspace, cmd) => {
|
|
|
|
return await withDatabase(mongodbUri, async (db) => {
|
|
|
|
await dropWorkspace(db, workspace)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-02 09:04:32 +00:00
|
|
|
program
|
|
|
|
.command('list-workspaces')
|
|
|
|
.description('List workspaces')
|
|
|
|
.action(async () => {
|
|
|
|
return await withDatabase(mongodbUri, async (db) => {
|
|
|
|
const workspacesJSON = JSON.stringify(await listWorkspaces(db), null, 2)
|
|
|
|
console.info(workspacesJSON)
|
2022-01-24 18:30:13 +00:00
|
|
|
|
|
|
|
console.log('latest model version:', JSON.stringify(version))
|
2021-12-02 09:04:32 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-27 12:02:31 +00:00
|
|
|
program
|
|
|
|
.command('show-accounts')
|
|
|
|
.description('Show accounts')
|
|
|
|
.action(async () => {
|
|
|
|
return await withDatabase(mongodbUri, async (db) => {
|
|
|
|
const accountsJSON = JSON.stringify(await listAccounts(db), null, 2)
|
|
|
|
console.info(accountsJSON)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-09-10 20:43:51 +00:00
|
|
|
program
|
|
|
|
.command('drop-account <name>')
|
|
|
|
.description('drop account')
|
|
|
|
.action(async (email, cmd) => {
|
|
|
|
return await withDatabase(mongodbUri, async (db) => {
|
|
|
|
await dropAccount(db, email)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-11-29 11:41:36 +00:00
|
|
|
program
|
2021-12-07 18:45:11 +00:00
|
|
|
.command('dump-workspace <workspace> <dirName>')
|
2021-11-29 11:41:36 +00:00
|
|
|
.description('dump workspace transactions and minio resources')
|
2021-12-07 18:45:11 +00:00
|
|
|
.action(async (workspace, dirName, cmd) => {
|
|
|
|
return await dumpWorkspace(mongodbUri, workspace, dirName, minio)
|
|
|
|
})
|
|
|
|
|
|
|
|
program
|
|
|
|
.command('restore-workspace <workspace> <dirName>')
|
|
|
|
.description('restore workspace transactions and minio resources from previous dump.')
|
|
|
|
.action(async (workspace, dirName, cmd) => {
|
2022-01-05 09:07:01 +00:00
|
|
|
return await restoreWorkspace(mongodbUri, workspace, dirName, minio, elasticUrl, transactorUrl)
|
2021-11-29 11:41:36 +00:00
|
|
|
})
|
|
|
|
|
2021-12-14 18:34:18 +00:00
|
|
|
program
|
|
|
|
.command('diff-workspace <workspace>')
|
|
|
|
.description('restore workspace transactions and minio resources from previous dump.')
|
|
|
|
.action(async (workspace, cmd) => {
|
|
|
|
return await diffWorkspace(mongodbUri, workspace)
|
|
|
|
})
|
|
|
|
|
2021-12-02 14:21:41 +00:00
|
|
|
program
|
|
|
|
.command('clear-telegram-history')
|
|
|
|
.description('clear telegram history')
|
|
|
|
.option('-w, --workspace <workspace>', 'target workspace')
|
|
|
|
.action(async (cmd) => {
|
|
|
|
return await withDatabase(mongodbUri, async (db) => {
|
|
|
|
const telegramDB = process.env.TELEGRAM_DATABASE
|
|
|
|
if (telegramDB === undefined) {
|
|
|
|
console.error('please provide TELEGRAM_DATABASE.')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
const workspaces = await listWorkspaces(db)
|
|
|
|
const targetWorkspaces =
|
|
|
|
cmd.workspace !== undefined ? workspaces.filter((x) => x.workspace === cmd.workspace) : workspaces
|
|
|
|
|
|
|
|
for (const w of targetWorkspaces) {
|
|
|
|
console.log(`clearing ${w.workspace} history:`)
|
|
|
|
await clearTelegramHistory(mongodbUri, w.workspace, telegramDB)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-12-17 09:09:26 +00:00
|
|
|
program
|
2021-12-22 09:25:16 +00:00
|
|
|
.command('rebuild-elastic <workspace>')
|
2021-12-17 09:09:26 +00:00
|
|
|
.description('rebuild elastic index')
|
2021-12-22 09:25:16 +00:00
|
|
|
.action(async (workspace, cmd) => {
|
|
|
|
await rebuildElastic(mongodbUri, workspace, minio, elasticUrl)
|
|
|
|
console.log('rebuild end')
|
2021-12-17 09:09:26 +00:00
|
|
|
})
|
|
|
|
|
2021-12-23 09:02:14 +00:00
|
|
|
program
|
|
|
|
.command('import-xml <workspace> <fileName>')
|
|
|
|
.description('dump workspace transactions and minio resources')
|
|
|
|
.action(async (workspace, fileName, cmd) => {
|
|
|
|
return await importXml(transactorUrl, workspace, minio, fileName, mongodbUri, elasticUrl)
|
|
|
|
})
|
|
|
|
|
2021-09-09 21:37:38 +00:00
|
|
|
program.parse(process.argv)
|