Browse Source

自动绑定安心云本地化项目

dev
CODE 1 year ago
parent
commit
1cd6ce9abd
  1. 25
      jenkinsfile_script_3.6.2_data_bind_anxinproject_local
  2. 17
      script/3.6.2/data/1_bing_anxinproject_local/.vscode/launch.json
  3. 9
      script/3.6.2/data/1_bing_anxinproject_local/Dockerfile
  4. 91
      script/3.6.2/data/1_bing_anxinproject_local/index.js
  5. 15
      script/3.6.2/data/1_bing_anxinproject_local/package.json

25
jenkinsfile_script_3.6.2_data_bind_anxinproject_local

@ -0,0 +1,25 @@
podTemplate {
node('pod-templ-jenkins-slave-common') {
env.IMAGE_NAME = "${IOT_IMAGES_REGISTRY}/${IOT}/${JOB_NAME}"
env.IMAGE_NAME_SHORT = "${IOT}/${JOB_NAME}"
env.CODE_ADDR = "${GIT_ADDRESS}/free-sun/Inspection.git"
stage('Run shell') {
git branch: 'master', credentialsId: 'gitea-builder', url: "${CODE_ADDR}"
container('image-builder') {
sh'''
pwd
ls -al
/kaniko/executor --context=${BUILD_WORKSPACE} --dockerfile=./script/2.0.0/data/1_reset_project_bind_point/Dockerfile --destination=${IMAGE_NAME}:${IMAGE_VERSION} --cache=false --cleanup
'''
}
buildName "${IMAGE_NAME_SHORT}:${IMAGE_VERSION}"
buildDescription "${IMAGE_NAME}:${IMAGE_VERSION}"
}
}
}

17
script/3.6.2/data/1_bing_anxinproject_local/.vscode/launch.json

@ -0,0 +1,17 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "启动程序",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\index.js"
}
]
}

9
script/3.6.2/data/1_bing_anxinproject_local/Dockerfile

@ -0,0 +1,9 @@
FROM repository.anxinyun.cn/base-images/nodejs12:20.10.12.2
COPY . /var/app
WORKDIR /var/app
RUN npm cache clean -f && npm install --production --force --registry https://nexus.ngaiot.com/repository/fs-npm/
ENTRYPOINT [ "node", "index.js" ]

91
script/3.6.2/data/1_bing_anxinproject_local/index.js

@ -0,0 +1,91 @@
try {
const { Pool, Client } = require('pg')
const path = require('path')
const fs = require("fs");
// 开发环境
// ! 这里是安心云
const anxinyunPool = new Pool({
user: 'postgres',
host: '10.8.30.166',
database: 'Anxinyun0726',
password: '123',
port: 5432,
})
// 这里是运维平台
const pomsPool = new Pool({
user: 'postgres',
host: '10.8.30.32',
database: 'poms_231026_backup',
password: '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"`)).rows
// console.log(projects);
// 遍历项目
for (let project of projects) {
console.log(`遍历 ${project.id} ${project.name}`);
// 查项目下结构物
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);
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)
}

15
script/3.6.2/data/1_bing_anxinproject_local/package.json

@ -0,0 +1,15 @@
{
"name": "appkey-generator",
"version": "1.0.0",
"description": "tool",
"main": "index.js",
"scripts": {
"test": "mocha",
"start": "set NODE_ENV=development&&node index"
},
"author": "liu",
"license": "ISC",
"dependencies": {
"pg": "^7.18.2"
}
}
Loading…
Cancel
Save