Commit 94bbb4b1 authored by tywldx's avatar tywldx

fix

parent 3c3428fc
var cproc = require('child_process');
var execSync = cproc.execSync;
var project_name, project_attr, projects = {},
java_projects = [],
node_projects = [],
python_projects = [],
ui_projects = [];
var param = {
type: "",
name: "",
attr: ""
}
projects = {}
_projects = String(execSync(`curl -s http://192.168.4.3:10088/config_server/get_project_config_from_db`))
_projects = JSON.parse(_projects)
// console.log(typeof(projects))
for (let pro of _projects) {
projects[pro.name] = pro;
switch (pro['type']) {
case "java":
java_projects.push(pro);
break;
case "node":
node_projects.push(pro);
break;
case "ui":
ui_projects.push(pro);
break;
case "python":
python_projects.push(pro);
break;
}
}
var temp = "";
process.argv.forEach(function(val, index, array) {
if (index >= 2) {
if (index % 2 == 0) {
temp = val.replace(/\-/, "");
param[temp] = "";
} else {
param[temp] = val;
}
}
});
const mongoose = require('mongoose');
function getListAttr(projects, attr) {
var list = [];
for (var i = 0; i < projects.length; i++) {
// if (projects[i].is_active) {
list.push(projects[i][attr])
// }
}
return list.join(" ");
}
var configFunction = function() {};
configFunction.prototype = {
getProjectAttr: function(pName, pAttr) {
if (typeof projects[pName] == "undefined") {
console.log("")
} else {
var attrs = pAttr.split("/");
if (attrs.length == 0) {
console.log("")
return
function getProjectAttr(projects,pName, pAttr) {
if (typeof projects[pName] == "undefined") {
return ""
} else {
var attrs = pAttr.split("/");
if (attrs.length == 0) {
return ""
}
var list = [];
for (var j = 0; j < attrs.length; j++) {
if(projects[pName][attrs[j]]){
list.push(projects[pName][attrs[j]].split(" ").join("^"));
}else{
list.push(" ");
}
var list = [];
}
return list.join("|")
}
}
function getProjectsAttrs(projects, attr, attr2) {
var attrs = attr.split("/");
if (attrs.length == 0) {
return
}
var list = [];
//attr2 专有参数,用来表示是否要取带有对外域名的服务
if(attr2 == "true"){
for (var i = 0; i < projects.length; i++) {
if(projects[i].url != ""){
var li = [projects[i]["name"]];
for (var j = 0; j < attrs.length; j++) {
if(projects[pName][attrs[j]]){
list.push(projects[pName][attrs[j]].split(" ").join("^"));
}else{
list.push(" ");
}
}
console.log(list.join("|"))
}
},
getProjectsAttrs: function(projects, attr, attr2) {
var attrs = attr.split("/");
if (attrs.length == 0) {
return
}
var list = [];
//attr2 专有参数,用来表示是否要取带有对外域名的服务
if(attr2 == "true"){
for (var i = 0; i < projects.length; i++) {
if(projects[i].url != ""){
var li = [projects[i]["name"]];
for (var j = 0; j < attrs.length; j++) {
li.push(projects[i][attrs[j]]);
}
// li.push(projects[i].url);
list.push(li.join("|"));
li.push(projects[i][attrs[j]]);
}
}
}else{
for (var i = 0; i < projects.length; i++) {
// if (projects[i].is_active){
var li = [projects[i]["name"]];
for (var j = 0; j < attrs.length; j++) {
li.push(projects[i][attrs[j]]);
}
list.push(li.join("|"));
// }
list.push(li.join("|"));
}
}
return list.join(" ");
},
getProjectsAttr: function(type, attr) {
switch (type) {
case "java":
console.log(getListAttr(java_projects, attr));
break;
case "ui":
console.log(getListAttr(ui_projects, attr));
break;
case "python":
console.log(getListAttr(python_projects, attr));
break;
case "node":
console.log(getListAttr(node_projects, attr));
break;
}else{
for (var i = 0; i < projects.length; i++) {
var li = [projects[i]["name"]];
for (var j = 0; j < attrs.length; j++) {
li.push(projects[i][attrs[j]]);
}
list.push(li.join("|"));
}
}
return list.join(" ");
}
//tower add
module.exports={
java : java_projects,
node : node_projects,
python : python_projects,
ui : ui_projects
function db2db(pro){
let temp = {
project_group: "",
name: pro["project_name"],
type: pro["type"],
url: pro["host_name"].replace(".xyqb.com", "").replace(".quantgroup.cn", ""),
www: pro["host_name"],
git_path: pro["git_path"],
log_name: pro["log_path"],
config_file: pro["config_path"],
command: "",
command_: "",
command2: "",
node_version: "",
port: `${pro["port"]}`,
desc: pro["des"],
is_active: pro["is_active"],
auth: pro["auth"],
target_path: ""
}
if(pro["type"] == "java"){
temp.node_version = pro["command1"];
temp.target_path = pro["jar_path"];
temp.command2 = pro["build_command"];
temp.command = pro["start_command"];
}else if(pro["type"] == "ui"){
temp.command = pro["build_command"]
}else if(pro["type"] == "node"){
temp.command = pro["start_command"]
temp.command2 = pro["stop_command"]
}else if(pro["type"] == "python"){
}
return temp
}
var F = new configFunction();
function controller(param){
//链接数据库
mongoose.connect('mongodb://172.30.220.22:27017/qaHome')
mongoose.model("ProConfig",new mongoose.Schema({})).find({}).exec().then(res=>{
// 定义数据
let _res = JSON.parse(JSON.stringify(res));
var project_name, project_attr, projects = {},
java_projects = [],
node_projects = [],
python_projects = [],
ui_projects = []
hosts = {};
projects = {}
for (let pro of _res) {
let temp = db2db(pro);
projects[temp.name] = temp;
hosts[temp.name] = temp.www;
switch (pro['type']) {
case "java":
java_projects.push(temp);
break;
case "node":
node_projects.push(temp);
break;
case "ui":
ui_projects.push(temp);
break;
case "python":
python_projects.push(temp);
break;
}
}
try {
if (param["type"] == "") {
F.getProjectAttr(param["name"], param["attr"]);
} else if (param["type"] == "multi") {
var str = "";
str += F.getProjectsAttrs(java_projects, param["attr"], param["attr2"]);
str += " " + F.getProjectsAttrs(ui_projects, param["attr"], param["attr2"]);
str += " " + F.getProjectsAttrs(python_projects, param["attr"], param["attr2"]);
str += " " + F.getProjectsAttrs(node_projects, param["attr"], param["attr2"]);
console.log(str)
} else if (param["type"] == "hosts") {
console.log(JSON.stringify(hosts));
} else {
F.getProjectsAttr(param["type"], param["attr"]);
//定义方法
if (param["type"] == "") {
let str = getProjectAttr(projects,param["name"], param["attr"]);
console.log(str);
} else if (param["type"] == "multi") {
var str = "";
str += getProjectsAttrs(java_projects, param["attr"], param["attr2"]);
str += " " + getProjectsAttrs(ui_projects, param["attr"], param["attr2"]);
str += " " + getProjectsAttrs(python_projects, param["attr"], param["attr2"]);
str += " " + getProjectsAttrs(node_projects, param["attr"], param["attr2"]);
console.log(str)
} else if (param["type"] == "hosts") {
console.log(JSON.stringify(hosts));
} else {
switch (param["type"]) {
case "java":
console.log(getListAttr(java_projects, param["attr"]));
break;
case "ui":
console.log(getListAttr(ui_projects, param["attr"]));
break;
case "python":
console.log(getListAttr(python_projects, param["attr"]));
break;
case "node":
console.log(getListAttr(node_projects, param["attr"]));
break;
}
}
//关闭连接
mongoose.connection.close()
}).catch((err) => {
//关闭链接
console.log(err)
mongoose.connection.close()
});
}
function main(){
var param = {
type: "",
name: "",
attr: ""
}
//获取请求参数
var temp = "";
process.argv.forEach(function(val, index, array) {
if (index >= 2) {
if (index % 2 == 0) {
temp = val.replace(/\-/, "");
param[temp] = "";
} else {
param[temp] = val;
}
}
});
controller(param)
//这里区分使用那个方法,需要注意的是,这里不在提供输出结果,结果在mongo链接中输出
}
try {
main()
} catch (e) {
console.log("")
}
\ No newline at end of file
......@@ -2,7 +2,9 @@ var express = require('express');
var cproc = require('child_process');
var bodyParser = require('body-parser');
var db_config = require('./config/db');
const mongodb = require('mongodb');
// const mongodb = require('mongodb');
const mongoose = require('mongoose');
var ProConfig = require('./model/proconfig.js');
// return
var execSync = cproc.execSync;
var options = {
......@@ -10,8 +12,10 @@ var options = {
limit: '100kb',
type: 'application/octet-stream'
};
// const mongoose = require('mongoose');
// mongoose.connect('mongodb://localhost/my_database');
mongoose.connect('mongodb://172.30.220.22:27017/qaHome')
var app = express();
var admin = express(); // the sub app
......@@ -76,6 +80,65 @@ function parseJson2Str(json) {
// DB 维护数据
let db_conf = db_config.db_config();
let db_conf_keys = Object.keys(db_conf);
// 从mongo中获取配置
function getProjectConfigPromise () {
return new Promise(function (resolve, reject) {
// mongoose.model("ProConfig",new mongoose.Schema({}))
ProConfig.find({}).exec().then(res=>{
let _pros = JSON.parse(JSON.stringify(res));
let re = {},hosts={};
for(let pro of _pros){
hosts[pro["project_name"]] = pro["host_name"];
if(pro["project_name"] == "xjd-ui"){
hosts["xyqb-ui--xjd-ui"] = pro["host_name"];
}else if(pro["project_name"] == "new-paycenter-ui"){
hosts["paycenter-ui--new-paycenter-ui"] = pro["host_name"];
}
let temp = {
project_group: "",
name: pro["project_name"],
type: pro["type"],
url: pro["host_name"].replace(".xyqb.com", "").replace(".quantgroup.cn", ""),
www: pro["host_name"],
git_path: pro["git_path"],
log_name: pro["log_path"],
config_file: pro["config_path"],
command: "",
command_: "",
command2: "",
node_version: "",
port: `${pro["port"]}`,
desc: pro["des"],
is_active: pro["is_active"],
auth: pro["auth"],
target_path: ""
}
if(pro["type"] == "java"){
temp.node_version = pro["command1"];
temp.target_path = pro["jar_path"];
temp.command2 = pro["build_command"];
temp.command = pro["start_command"];
}else if(pro["type"] == "ui"){
temp.command = pro["build_command"]
}else if(pro["type"] == "node"){
temp.command = pro["start_command"]
temp.command2 = pro["stop_command"]
}else if(pro["type"] == "python"){
}
re[temp["name"]] = temp;
}
resolve({projects:re,hosts:hosts})
}).catch((err) => {
reject(err)
});
})
}
//
admin.get('/get_db_config/:name/:attr', function(req, res) {
let str = ""
......@@ -92,6 +155,7 @@ admin.get('/get_db_config/:name/:attr', function(req, res) {
}
}
});
admin.get('/get_db_dump_command/:name/:opt', function(req, res) {
if (req.params.opt != "opt") {
res.send(db_config.get_mysql_dump_table(req.params.name));
......@@ -108,21 +172,18 @@ admin.get('/get_db_config/refersh', function() {
// 获取节点命令
admin.get('/get_node_command/:name', function(req, res) {
admin.get('/get_node_command/:name', async function(req, res) {
res.setHeader("Content-Type", "application/json");
var str = execSync(`node ./get_project_config.js -name ${req.params.name} -attr config_file/command/gitPathHead`)
var items = String(str).replace(/\n/g, "").split("|");
var config_file = items[0].replace(/\^/g, " ");
var command = items[1].replace(/\^/g, " ");
var gitGroup = items[2].replace(/\^/g, " ");
res.send({ configPath: config_file, buildCmd: command, gitGroup: gitGroup });
let configs = await getProjectConfigPromise();
let projects = configs.projects
let pro = projects[req.params.name];
res.send({ configPath: pro.config_file, buildCmd: pro.command, gitGroup: pro.gitPathHead });
});
// 获取项目配置
admin.get('/get_project_attr/:name', function(req, res) {
admin.get('/get_project_attr/:name', async function(req, res) {
res.setHeader("Content-Type", "application/json");
var comstr = attrComsArr.join("/");
var names = req.params.name.split("--");
var p_name = "";
if (names.length > 1) {
......@@ -130,80 +191,100 @@ admin.get('/get_project_attr/:name', function(req, res) {
} else {
p_name = req.params.name
}
var str = execSync(`node ./get_project_config.js -name ${p_name} -attr ${comstr}`)
var items = String(str).replace(/\n/g, "").split("|");
let rs = {},
index = 0;
for (let i of items) {
rs[attrComs[index]] = i.replace(/\^/g, " ");
index++
}
res.send(rs);
try{
let configs = await getProjectConfigPromise();
let projects = configs.projects
let pro = projects[p_name];
let rs = {};
for (let item of attrComs){
rs[item] = pro[attrs[item]]
}
res.send(rs);
}
catch (e){
console.log(e)
res.send(e)
}
});
// 获取所有域名
admin.get('/get_hosts', function(req, res) {
admin.get('/get_hosts', async function(req, res) {
res.setHeader("Content-Type", "application/json");
var str = execSync(`node ./get_project_config.js -type hosts`)
var items = String(str);
res.send(items);
let configs = await getProjectConfigPromise();
res.send(configs.hosts);
});
// 根据类型获取系统名称
admin.get('/get_systems_by_type', function(req, res) {
function getfrontendbackend(pros){
let temp = {
frontend : [],
backend : [],
ui:[],
node:[],
java:[],
python:[]
}
let keys = Object.keys(pros)
// console.log(keys)
for(let key of keys){
let pro = pros[key]
if(pro.type == "ui"){
if(pro.name != "xyqb-ui" && pro.name != "paycenter-ui" ){
if(pro.name == "xjd-ui"){
temp.frontend.push("xyqb-ui--xjd-ui");
temp[pro.type].push("xyqb-ui--xjd-ui")
}else if(pro.name == "new-paycenter-ui"){
temp.frontend.push("paycenter-ui--new-paycenter-ui");
temp[pro.type].push("paycenter-ui--new-paycenter-ui")
}else{
temp.frontend.push(pro.name);
temp[pro.type].push(pro.name)
}
}
}else{
temp.backend.push(pro.name);
temp[pro.type].push(pro.name)
}
}
return temp
}
admin.get('/get_systems_by_type',async function(req, res) {
res.setHeader("Content-Type", "application/json");
var ui_str = execSync(`node ./get_project_config.js -type ui -attr name`)
ui_str = String(ui_str)
.replace(/xyqb-ui/g, "").replace(/xjd-ui/, "xyqb-ui--xjd-ui")
.replace(/new-paycenter-ui/g, "").replace(/paycenter-ui/, "paycenter-ui--new-paycenter-ui")
.replace(/\s+/g, ",").replace(/\n/g, "").slice(0, -1);
var java_str = execSync(`node ./get_project_config.js -type java -attr name`)
var node_str = execSync(`node ./get_project_config.js -type node -attr name`)
var python_str = execSync(`node ./get_project_config.js -type python -attr name`)
var server_str = `${String(java_str)} ${String(node_str)} ${String(python_str)}`
server_str = server_str.replace(/\s+/g, ",").replace(/\n/g, "").slice(0, -1);;
let configs = await getProjectConfigPromise();
let pros = configs.projects;
let fb = getfrontendbackend(pros);
res.send({
common: "db,redis,rabbitmq,zookeeper",
frontend: ui_str,
backend: server_str
frontend: fb.frontend.join(" "),
backend: fb.backend.join(" ")
});
});
// 给jenkins使用根据类型获取系统名称
admin.get('/get_systems_by_type_for_jenkins/:_type', function(req, res) {
let str = ""
if (req.params._type === "frontend") {
var ui_str = execSync(`node ./get_project_config.js -type ui -attr name`)
str = String(ui_str)
.replace(/xyqb-ui/g, "").replace(/xjd-ui/, "xyqb-ui--xjd-ui")
.replace(/new-paycenter-ui/g, "").replace(/paycenter-ui/, "paycenter-ui--new-paycenter-ui");
} else {
var java_str = execSync(`node ./get_project_config.js -type java -attr name`)
var node_str = execSync(`node ./get_project_config.js -type node -attr name`)
var python_str = execSync(`node ./get_project_config.js -type python -attr name`)
str = `${String(java_str)} ${String(node_str)} ${String(python_str)}`
}
str = str.replace(/\n/g, "").replace(/\s+/g, "\n");
res.send(str);
admin.get('/get_systems_by_type_for_jenkins/:_type',async function(req, res) {
let str = "";
let configs = await getProjectConfigPromise();
let pros = configs.projects;
let fb = getfrontendbackend(pros);
if(req.params._type === "frontend"){
res.send(fb.frontend.join(" "));
}else{
res.send(fb.backend.join(" "));
}
});
// 给jenkins使用根据类型获取系统名称
admin.get('/get_systems_by_type_for_temp', function(req, res) {
var ui_str = execSync(`node ./get_project_config.js -type ui -attr name`)
ui_str = String(ui_str).split(/\s+/)
var java_str = execSync(`node ./get_project_config.js -type java -attr name`)
java_str = String(java_str).split(/\s+/)
var node_str = execSync(`node ./get_project_config.js -type node -attr name`)
node_str = String(node_str).split(/\s+/)
var python_str = execSync(`node ./get_project_config.js -type python -attr name`)
python_str = String(python_str).split(/\s+/)
res.send({
ui: ui_str,
java: java_str,
node: node_str,
python: python_str
});
admin.get('/get_systems_by_type_for_temp', async function(req, res) {
let configs = await getProjectConfigPromise();
let pros = configs.projects;
let fb = getfrontendbackend(pros);
res.send({
ui: fb.ui.join(" "),
java: fb.java.join(" "),
node: fb.node.join(" "),
python: fb.python.join(" ")
});
});
......@@ -260,74 +341,7 @@ admin.get('/get_proxy', function(req, res) {
});
// 从mongo中获取配置
admin.get('/get_project_config_from_db', function(req, res) {
res.setHeader("Content-Type", "application/json");
async function test() {
let db = await mongodb.MongoClient.connect('mongodb://172.30.220.22:27017/qaHome', async function(err, db) {
let proconfigs = db.collection('proconfigs');
let projects = await proconfigs.find().toArray();
let re = [];
for (var index = 0; index < projects.length; index++) {
let temp = {
project_group: "",
name: projects[index]["project_name"],
type: projects[index]["type"],
url: projects[index]["host_name"].replace(".xyqb.com", "").replace(".quantgroup.cn", ""),
www: projects[index]["host_name"],
git_path: projects[index]["git_path"],
log_name: projects[index]["log_path"],
config_file: projects[index]["config_path"],
command: "",
command_: "",
command2: "",
node_version: "",
port: `${projects[index]["port"]}`,
desc: projects[index]["des"],
is_active: projects[index]["is_active"],
auth: projects[index]["auth"],
target_path: ""
}
// let temp = {
// name: projects[index]["project_name"],
// type: projects[index]["type"],
// url: projects[index]["host_name"].replace(".xyqb.com", "").replace(".quantgroup.cn", ""),
// www: projects[index]["host_name"],
// git_path: projects[index]["git_path"],
// config_path: projects[index]["config_path"],
// log_name: projects[index]["log_path"],
// command: projects[index]["start_command"],
// command2: projects[index]["build_command"],
// stop_command: projects[index]["stop_command"],
// port: projects[index]["port"],
// desc: projects[index]["full_name"],
// is_active: projects[index]["is_active"],
// auth: projects[index]["auth"],
// target_path: projects[index]["jar_path"]
// }
if(projects[index]["type"] == "java"){
temp.node_version = projects[index]["command1"];
temp.target_path = projects[index]["jar_path"];
temp.command2 = projects[index]["build_command"];
temp.command = projects[index]["start_command"];
}else if(projects[index]["type"] == "ui"){
temp.command = projects[index]["build_command"]
}else if(projects[index]["type"] == "node"){
temp.command = projects[index]["start_command"]
temp.command2 = projects[index]["stop_command"]
}else if(projects[index]["type"] == "python"){
}
re.push(temp);
}
db.close();
res.send(re)
});
}
test();
});
app.use('/config_server', admin)
console.log("http://192.168.4.3:10088/config_server/get_node_command")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment