运维服务中台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

91 lines
3.1 KiB

try {
const { Pool, Client } = require('pg')
const path = require('path')
const fs = require("fs");
// 开发环境
// ! 这里是安心云
const anxinyunPool = new Pool({
user: 'postgres',
host: '10.8.40.210',
database: 'AnxinCloud',
password: 'postgres',
port: 5432,
})
// 这里是运维平台
const pomsPool = new Pool({
user: 'POMSAdmin',
host: '10.8.40.223',
database: 'POMS',
password: 'POMSAdmin_123',
port: 5432,
})
// 商用环境
// const pool = new Pool({
// user: '',
// host: '',
// database: '',
// password: '',
// port: 5432,
// })
// ! 这里是假定的创建人id
const create_user_id = 1
const fun = async () => {
// note: we don't try/catch this because if connecting throws an exception
// we don't need to dispose of the client (it will be undefined)
const anxinClient = await anxinyunPool.connect()
const pomsClient = await pomsPool.connect()
anxinClient.on('error', err => {
console.error(err);
})
pomsClient.on('error', () => {
console.error(err);
})
try {
await anxinClient.query('BEGIN')
await pomsClient.query('BEGIN')
console.log(`开始`);
const projects = (await anxinClient.query(`SELECT * FROM "t_project" WHERE project_state=4`)).rows
// console.log(projects);
// 遍历项目
for (let project of projects) {
console.log(`遍历 ${project.name} [${project.id}]`);
// 查项目下结构物
const strucCount = (await anxinClient.query(`SELECT count(*) FROM "t_project_structure" INNER JOIN "t_structure" ON "t_project_structure"."structure" = "t_structure"."id" WHERE "project" = ${project.id} AND "t_structure"."external_platform" IS NOT null`)).rows
console.log('查得本地化结构物数量 ' + strucCount[0].count);
if (strucCount[0].count > 0) {
// 判断有没有创建这个项目
const projectPoms = (await pomsClient.query(`SELECT * FROM project_correlation WHERE anxin_project_id @> ARRAY[${project.id}];`)).rows
if (projectPoms.length == 0) {
// 在运维平台新建项目
console.log(`创建运维项目`);
const projectData = await pomsClient.query(`INSERT INTO project_correlation (anxin_project_id, create_time, create_user, name, del, update_time) VALUES ('{${project.id}}', NOW(),${create_user_id || 1}, '${project.name}', false, NOW()) RETURNING id;`)
}
}
}
// await client.query('ROLLBACK')
await anxinClient.query('COMMIT')
await pomsClient.query('COMMIT')
console.log('执行完毕~')
} catch (e) {
await anxinClient.query('ROLLBACK')
await pomsClient.query('ROLLBACK')
console.log('执行错误~')
throw e
} finally {
anxinClient.release();
pomsClient.release();
}
}
fun()
} catch (error) {
console.error(error)
}