Commit 9683ee1c authored by tywldx's avatar tywldx

Merge branch 'master' of http://git.quantgroup.cn/QA/qa-deploy-utils

# Conflicts:
#	qa_shell_script/config_server/package-lock.json
#	qa_shell_script/config_server/package.json
parents de709d60 4d49407e
...@@ -9,3 +9,5 @@ ...@@ -9,3 +9,5 @@
.DS_Store .DS_Store
qa_shell_script/config_server/node_modules qa_shell_script/config_server/node_modules
.log .log
./qa_shell_script/config_server/package.json
./qa_shell_script/config_server/package-lock.json
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 = String(execSync(`curl -s http://127.0.0.1:10088/config_server/get_project_config_from_db`))
projects = JSON.parse(projects)
// console.log(typeof(projects))
// console.log(projects)
for (var index in projects) {
// if (projects[index]['project_name'] == 'xyqb') {
// console.log(index)
// console.log(projects[index])
// }
switch (projects[index]['type']) {
case "java":
java_projects.push(projects[index]);
break;
case "node":
node_projects.push(projects[index]);
break;
case "ui":
ui_projects.push(projects[index]);
break;
case "python":
python_projects.push(projects[index]);
break;
}
}
// console.log(111)
// // --java
// new pClass().init(1,true,"java", "7042", "xyqb-user2", "head_group", "xyqb-user2.log", "application.properties", "--worker 1 --currentWorker 1 -t -ts", "-Denv=dev -Didc=default -Dtech.localhost= -Dtest=true", "用户注册登录-V2-server", "", "mvn clean package -Ptest -Dmaven.test.skip=true", "");
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;
}
}
});
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("111")
// } else {
// console.log("222")
// var attrs = pAttr.split("/");
// if (attrs.length == 0) {
// console.log("")
// return
// }
// var list = [];
// for (var j = 0; j < attrs.length; j++) {
// if(projects[pName][attrs[j]]){
// console.log(projects)
// list.push(projects[pName][attrs[j]].split(" ").join("^"));
// }else{
// list.push(" ");
// }
// }
// console.log(list.join("|"))
// }
// },
getProjectAttr: function(pName, pAttr) {
for (var index in projects) {
if (projects[index]['project_name'] == pName) {
var attrs = pAttr.split("/");
if (attrs.length == 0) {
console.log("")
return
}
var list = [];
for (var j = 0; j < attrs.length; j++) {
if (projects[index][attrs[j]]) {
list.push(projects[index][attrs[j]].split(" ").join("^"));
} else {
list.push(" ");
}
}
console.log(list.join("|"))
} else {}
}
},
// 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("|"));
// }
// }
// }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("|"));
// // }
// }
// }
// return 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]["project_name"]];
for (var j = 0; j < attrs.length; j++) {
li.push(projects[i][attrs[j]]);
}
// li.push(projects[i].url);
list.push(li.join("|"));
}
}
} else {
for (var i = 0; i < projects.length; i++) {
// if (projects[i].is_active){
var li = [projects[i]["project_name"]];
for (var j = 0; j < attrs.length; j++) {
li.push(projects[i][attrs[j]]);
}
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;
}
}
}
//tower add
module.exports = {
java: java_projects,
node: node_projects,
python: python_projects,
ui: ui_projects
}
var F = new configFunction();
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"]);
}
\ No newline at end of file
var express = require('express'); var express = require('express');
var cproc = require('child_process'); var cproc = require('child_process');
var bodyParser = require('body-parser'); var bodyParser = require('body-parser');
var db_config = require('./config/db'); var db_config = require('./config/db');
const mongodb = require('mongodb');
// return // return
var execSync = cproc.execSync; var execSync = cproc.execSync;
var options = { var options = {
inflate: true, inflate: true,
limit: '100kb', limit: '100kb',
type: 'application/octet-stream' type: 'application/octet-stream'
}; };
// const mongoose = require('mongoose'); // const mongoose = require('mongoose');
// mongoose.connect('mongodb://localhost/my_database'); // mongoose.connect('mongodb://localhost/my_database');
...@@ -18,134 +19,135 @@ app.use(bodyParser.urlencoded({ extended: false })) ...@@ -18,134 +19,135 @@ app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json()) app.use(bodyParser.json())
var attrs = { var attrs = {
configPath:"config_file", configPath: "config_file",
buildCmd:"command", buildCmd: "command",
gitPath:"git_path", gitPath: "git_path",
gitGroup:"gitPathHead", gitGroup: "gitPathHead",
host:"www", host: "www",
projectType:"type", projectType: "type",
projectAuth:"auth", projectAuth: "auth",
projectDesc:"desc", projectDesc: "desc",
logName:"log_name", logName: "log_name",
jarFilePath:"target_path", jarFilePath: "target_path",
auth:"auth", auth: "auth",
port:"port", port: "port",
start_command:"", start_command: "",
stop_command:"", stop_command: "",
command2:"command2", command2: "command2",
node_version:"node_version" node_version: "node_version"
} }
var attrComs = Object.keys(attrs) var attrComs = Object.keys(attrs)
var attrComsArr = []; var attrComsArr = [];
for(let i of attrComs){ for (let i of attrComs) {
attrComsArr.push(attrs[i]); attrComsArr.push(attrs[i]);
} }
// ------------UTILS------------ // ------------UTILS------------
function parseStr2Json(str){ function parseStr2Json(str) {
var a = {}; var a = {};
var strs = str.split(" "); var strs = str.split(" ");
for(let i of strs){ for (let i of strs) {
if(i){ if (i) {
let os = i.split("|"); let os = i.split("|");
a[os[0]]={ a[os[0]] = {
name: os[0], name: os[0],
ip: os[1], ip: os[1],
http: os[2], http: os[2],
https: os[3] https: os[3]
} }
} }
} }
return a return a
} }
function parseJson2Str(json){ function parseJson2Str(json) {
var a = []; var a = [];
let keys = Object.keys(json); let keys = Object.keys(json);
for(let k of keys){ for (let k of keys) {
a.push([ a.push([
json[k].name, json[k].name,
json[k].ip, json[k].ip,
json[k].http, json[k].http,
json[k].https json[k].https
].join("|")) ].join("|"))
} }
return a.join(" ") return a.join(" ")
} }
// ------------UTILS---------- // ------------UTILS----------
// DB 维护数据 // DB 维护数据
let db_conf = db_config.db_config(); let db_conf = db_config.db_config();
let db_conf_keys = Object.keys(db_conf); let db_conf_keys = Object.keys(db_conf);
// //
admin.get('/get_db_config/:name/:attr', function (req, res) { admin.get('/get_db_config/:name/:attr', function(req, res) {
let str = "" let str = ""
if(req.params.name == "all"){ if (req.params.name == "all") {
if(req.params.attr == "name"){ if (req.params.attr == "name") {
res.send(db_conf_keys.join(" ")); res.send(db_conf_keys.join(" "));
} }
}else{ } else {
if(req.params.attr != "all"){ if (req.params.attr != "all") {
res.send(db_conf[req.params.name][req.params.attr]) res.send(db_conf[req.params.name][req.params.attr])
}else{ } else {
let item = db_conf[req.params.name] let item = db_conf[req.params.name]
res.send(`${item.host};${item.port};${item.user};${item.pwd};${item.sys_table_list};`) res.send(`${item.host};${item.port};${item.user};${item.pwd};${item.sys_table_list};`)
} }
} }
}); });
admin.get('/get_db_dump_command/:name/:opt', function (req, res) { admin.get('/get_db_dump_command/:name/:opt', function(req, res) {
if(req.params.opt!="opt"){ if (req.params.opt != "opt") {
res.send(db_config.get_mysql_dump_table(req.params.name)); res.send(db_config.get_mysql_dump_table(req.params.name));
}else{ } else {
res.send(db_config.get_mysql_dump_opt(req.params.name)); res.send(db_config.get_mysql_dump_opt(req.params.name));
} }
}); });
admin.get('/get_db_config/refersh',function(){ admin.get('/get_db_config/refersh', function() {
res.setHeader("Content-Type","application/json"); res.setHeader("Content-Type", "application/json");
db_conf = db_config.db_config(); db_conf = db_config.db_config();
db_conf_keys = Object.keys(db_conf); db_conf_keys = Object.keys(db_conf);
res.send({code:"OK"}) res.send({ code: "OK" })
}) })
// 获取节点命令 // 获取节点命令
admin.get('/get_node_command/:name', function (req, res) { admin.get('/get_node_command/:name', function(req, res) {
res.setHeader("Content-Type","application/json"); res.setHeader("Content-Type", "application/json");
var str = execSync(`node ./get_project_config.js -name ${req.params.name} -attr config_file/command/gitPathHead`) 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 items = String(str).replace(/\n/g, "").split("|");
var config_file = items[0].replace(/\^/g," "); var config_file = items[0].replace(/\^/g, " ");
var command = items[1].replace(/\^/g," "); var command = items[1].replace(/\^/g, " ");
var gitGroup = items[2].replace(/\^/g," "); var gitGroup = items[2].replace(/\^/g, " ");
res.send({configPath:config_file,buildCmd:command,gitGroup:gitGroup}); res.send({ configPath: config_file, buildCmd: command, gitGroup: gitGroup });
}); });
// 获取项目配置 // 获取项目配置
admin.get('/get_project_attr/:name', function (req, res) { admin.get('/get_project_attr/:name', function(req, res) {
res.setHeader("Content-Type","application/json"); res.setHeader("Content-Type", "application/json");
var comstr = attrComsArr.join("/"); var comstr = attrComsArr.join("/");
var names = req.params.name.split("--"); var names = req.params.name.split("--");
var p_name = ""; var p_name = "";
if(names.length > 1){ if (names.length > 1) {
p_name = names[0] p_name = names[0]
}else{ } else {
p_name = req.params.name p_name = req.params.name
} }
var str = execSync(`node ./get_project_config.js -name ${p_name} -attr ${comstr}`) var str = execSync(`node ./get_project_config.js -name ${p_name} -attr ${comstr}`)
var items = String(str).replace(/\n/g,"").split("|"); var items = String(str).replace(/\n/g, "").split("|");
let rs = {},index = 0; let rs = {},
for(let i of items){ index = 0;
rs[attrComs[index]] = i.replace(/\^/g," "); for (let i of items) {
index++ rs[attrComs[index]] = i.replace(/\^/g, " ");
} index++
res.send(rs); }
res.send(rs);
}); });
// 获取所有域名 // 获取所有域名
admin.get('/get_hosts', function (req, res) { admin.get('/get_hosts', function(req, res) {
res.setHeader("Content-Type","application/json"); res.setHeader("Content-Type", "application/json");
var str = execSync(`node ./get_project_config.js -type hosts`) var str = execSync(`node ./get_project_config.js -type hosts`)
var items = String(str); var items = String(str);
res.send(items); res.send(items);
}); });
// 根据类型获取系统名称 // 根据类型获取系统名称
...@@ -169,127 +171,120 @@ admin.get('/get_systems_by_type', function (req, res) { ...@@ -169,127 +171,120 @@ admin.get('/get_systems_by_type', function (req, res) {
}); });
// 给jenkins使用根据类型获取系统名称 // 给jenkins使用根据类型获取系统名称
admin.get('/get_systems_by_type_for_jenkins/:_type', function (req, res) { admin.get('/get_systems_by_type_for_jenkins/:_type', function(req, res) {
let str = "" let str = ""
if(req.params._type === "frontend"){ if (req.params._type === "frontend") {
var ui_str = execSync(`node ./get_project_config.js -type ui -attr name`) var ui_str = execSync(`node ./get_project_config.js -type ui -attr name`)
str = String(ui_str) str = String(ui_str)
.replace(/xyqb-ui/g,"").replace(/xjd-ui/,"xyqb-ui--xjd-ui") .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(/new-paycenter-ui/g, "").replace(/paycenter-ui/, "paycenter-ui--new-paycenter-ui");
}else{ } else {
var java_str = execSync(`node ./get_project_config.js -type java -attr name`) 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 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 python_str = execSync(`node ./get_project_config.js -type python -attr name`)
str = `${String(java_str)} ${String(node_str)} ${String(python_str)}` str = `${String(java_str)} ${String(node_str)} ${String(python_str)}`
} }
str = str.replace(/\n/g,"").replace(/\s+/g,"\n"); str = str.replace(/\n/g, "").replace(/\s+/g, "\n");
res.send(str); res.send(str);
}); });
// 给jenkins使用根据类型获取系统名称 // 给jenkins使用根据类型获取系统名称
admin.get('/get_systems_by_type_for_temp', function (req, res) { admin.get('/get_systems_by_type_for_temp', function(req, res) {
var ui_str = execSync(`node ./get_project_config.js -type ui -attr name`) var ui_str = execSync(`node ./get_project_config.js -type ui -attr name`)
ui_str = String(ui_str).split(/\s+/) ui_str = String(ui_str).split(/\s+/)
var java_str = execSync(`node ./get_project_config.js -type java -attr name`) var java_str = execSync(`node ./get_project_config.js -type java -attr name`)
java_str = String(java_str).split(/\s+/) java_str = String(java_str).split(/\s+/)
var node_str = execSync(`node ./get_project_config.js -type node -attr name`) var node_str = execSync(`node ./get_project_config.js -type node -attr name`)
node_str = String(node_str).split(/\s+/) node_str = String(node_str).split(/\s+/)
var python_str = execSync(`node ./get_project_config.js -type python -attr name`) var python_str = execSync(`node ./get_project_config.js -type python -attr name`)
python_str = String(python_str).split(/\s+/) python_str = String(python_str).split(/\s+/)
res.send({ res.send({
ui:ui_str, ui: ui_str,
java:java_str, java: java_str,
node:node_str, node: node_str,
python:python_str python: python_str
}); });
}); });
// 增加一个新的nginx映射 // 增加一个新的nginx映射
admin.post('/add_new_proxy', function (req, res) { admin.post('/add_new_proxy', function(req, res) {
let request = req.body; let request = req.body;
let new_namespace = request["namespace"],new_ip = request["ip"],new_ports=request["ports"]; let new_namespace = request["namespace"],
var str = String(execSync(`cat ../ngrok/docker_env_name.sh`)).replace(/\n/,""); new_ip = request["ip"],
let data = parseStr2Json(str); new_ports = request["ports"];
data[new_namespace] = { var str = String(execSync(`cat ../ngrok/docker_env_name.sh`)).replace(/\n/, "");
name: new_namespace, let data = parseStr2Json(str);
ip: new_ip, data[new_namespace] = {
http: new_ports.http_port, name: new_namespace,
https: new_ports.https_port ip: new_ip,
} http: new_ports.http_port,
https: new_ports.https_port
let s = parseJson2Str(data) }
s = s.replace("\n","")
execSync(`echo "${s}" > ../ngrok/docker_env_name.sh`) let s = parseJson2Str(data)
cproc.exec("../ngrok/make_frp_ini_V2.sh"); s = s.replace("\n", "")
res.send({msg:"执行完成"}); execSync(`echo "${s}" > ../ngrok/docker_env_name.sh`)
cproc.exec("../ngrok/make_frp_ini_V2.sh");
res.send({ msg: "执行完成" });
}); });
// 删除一个namespace映射 // 删除一个namespace映射
admin.post('/remove_proxy', function (req, res) { admin.post('/remove_proxy', function(req, res) {
let request = req.body; let request = req.body;
console.log(request); console.log(request);
let new_namespace = request["namespace"]; let new_namespace = request["namespace"];
var str = String(execSync(`cat ../ngrok/docker_env_name.sh`)).replace(/\n/,""); var str = String(execSync(`cat ../ngrok/docker_env_name.sh`)).replace(/\n/, "");
let data = parseStr2Json(str); let data = parseStr2Json(str);
delete data[new_namespace] delete data[new_namespace]
let s = parseJson2Str(data) let s = parseJson2Str(data)
s = s.replace("\n","") s = s.replace("\n", "")
execSync(`echo "${s}" > ../ngrok/docker_env_name.sh`) execSync(`echo "${s}" > ../ngrok/docker_env_name.sh`)
cproc.exec("../ngrok/make_frp_ini_V2.sh"); cproc.exec("../ngrok/make_frp_ini_V2.sh");
var last_str = String(execSync(`cat ../ngrok/docker_env_name.sh`)) var last_str = String(execSync(`cat ../ngrok/docker_env_name.sh`))
res.setHeader("Content-Type","application/json"); res.setHeader("Content-Type", "application/json");
res.send({ res.send({
"details": { "details": {
name_spaces:last_str name_spaces: last_str
}, },
"success": true "success": true
}); });
}); });
// 获取现有映射关系 // 获取现有映射关系
admin.get('/get_proxy', function (req, res) { admin.get('/get_proxy', function(req, res) {
res.setHeader("Content-Type","application/json"); res.setHeader("Content-Type", "application/json");
var str = execSync(`cat ../ngrok/docker_env_name.sh`) var str = execSync(`cat ../ngrok/docker_env_name.sh`)
res.send({namespace:String(str)}); res.send({ namespace: String(str) });
}); });
// 从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://192.168.28.179:27017/qaHome', async function(err, db) {
let proconfigs = db.collection('proconfigs');
let projects = await proconfigs.find().toArray();
let re = [];
// for (var i = 0; i < projects.length; i++) {
// let project_name=projects[i]['project_name'];
// let temp = { project_name: projects[i] };
// // temp[projects[i]['project_name']] = projects[i];
// console.log(temp)
// re.push(temp);
// }
db.close();
res.send(projects)
});
}
test();
});
app.use('/config_server', admin) app.use('/config_server', admin)
console.log("http://192.168.4.3:10088/config_server/get_node_command") console.log("http://192.168.4.3:10088/config_server/get_node_command")
app.listen(10088) app.listen(10088)
\ No newline at end of file
...@@ -12,8 +12,8 @@ ...@@ -12,8 +12,8 @@
"body-parser": "^1.18.2", "body-parser": "^1.18.2",
"child_process": "^1.0.2", "child_process": "^1.0.2",
"express": "^4.16.2", "express": "^4.16.2",
"mongoose": "^5.0.4", "request": "^2.83.0",
"pm2": "^2.7.2", "mongodb": "^2.2.33",
"request": "^2.83.0" "pm2": "^2.7.2"
} }
} }
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