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 @@
.DS_Store
qa_shell_script/config_server/node_modules
.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 cproc = require('child_process');
var bodyParser = require('body-parser');
var db_config = require('./config/db');
var db_config = require('./config/db');
const mongodb = require('mongodb');
// return
var execSync = cproc.execSync;
var options = {
inflate: true,
limit: '100kb',
type: 'application/octet-stream'
inflate: true,
limit: '100kb',
type: 'application/octet-stream'
};
// const mongoose = require('mongoose');
// mongoose.connect('mongodb://localhost/my_database');
......@@ -18,134 +19,135 @@ app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
var attrs = {
configPath:"config_file",
buildCmd:"command",
gitPath:"git_path",
gitGroup:"gitPathHead",
host:"www",
projectType:"type",
projectAuth:"auth",
projectDesc:"desc",
logName:"log_name",
jarFilePath:"target_path",
auth:"auth",
port:"port",
start_command:"",
stop_command:"",
command2:"command2",
node_version:"node_version"
configPath: "config_file",
buildCmd: "command",
gitPath: "git_path",
gitGroup: "gitPathHead",
host: "www",
projectType: "type",
projectAuth: "auth",
projectDesc: "desc",
logName: "log_name",
jarFilePath: "target_path",
auth: "auth",
port: "port",
start_command: "",
stop_command: "",
command2: "command2",
node_version: "node_version"
}
var attrComs = Object.keys(attrs)
var attrComsArr = [];
for(let i of attrComs){
attrComsArr.push(attrs[i]);
for (let i of attrComs) {
attrComsArr.push(attrs[i]);
}
// ------------UTILS------------
function parseStr2Json(str){
var a = {};
var strs = str.split(" ");
for(let i of strs){
if(i){
let os = i.split("|");
a[os[0]]={
name: os[0],
ip: os[1],
http: os[2],
https: os[3]
}
}
}
return a
function parseStr2Json(str) {
var a = {};
var strs = str.split(" ");
for (let i of strs) {
if (i) {
let os = i.split("|");
a[os[0]] = {
name: os[0],
ip: os[1],
http: os[2],
https: os[3]
}
}
}
return a
}
function parseJson2Str(json){
var a = [];
let keys = Object.keys(json);
for(let k of keys){
a.push([
json[k].name,
json[k].ip,
json[k].http,
json[k].https
].join("|"))
}
return a.join(" ")
function parseJson2Str(json) {
var a = [];
let keys = Object.keys(json);
for (let k of keys) {
a.push([
json[k].name,
json[k].ip,
json[k].http,
json[k].https
].join("|"))
}
return a.join(" ")
}
// ------------UTILS----------
// DB 维护数据
let db_conf = db_config.db_config();
let db_conf_keys = Object.keys(db_conf);
//
admin.get('/get_db_config/:name/:attr', function (req, res) {
let str = ""
if(req.params.name == "all"){
if(req.params.attr == "name"){
res.send(db_conf_keys.join(" "));
}
}else{
if(req.params.attr != "all"){
res.send(db_conf[req.params.name][req.params.attr])
}else{
let item = db_conf[req.params.name]
res.send(`${item.host};${item.port};${item.user};${item.pwd};${item.sys_table_list};`)
}
}
admin.get('/get_db_config/:name/:attr', function(req, res) {
let str = ""
if (req.params.name == "all") {
if (req.params.attr == "name") {
res.send(db_conf_keys.join(" "));
}
} else {
if (req.params.attr != "all") {
res.send(db_conf[req.params.name][req.params.attr])
} else {
let item = db_conf[req.params.name]
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) {
if(req.params.opt!="opt"){
res.send(db_config.get_mysql_dump_table(req.params.name));
}else{
res.send(db_config.get_mysql_dump_opt(req.params.name));
}
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));
} else {
res.send(db_config.get_mysql_dump_opt(req.params.name));
}
});
admin.get('/get_db_config/refersh',function(){
res.setHeader("Content-Type","application/json");
db_conf = db_config.db_config();
db_conf_keys = Object.keys(db_conf);
res.send({code:"OK"})
admin.get('/get_db_config/refersh', function() {
res.setHeader("Content-Type", "application/json");
db_conf = db_config.db_config();
db_conf_keys = Object.keys(db_conf);
res.send({ code: "OK" })
})
// 获取节点命令
admin.get('/get_node_command/:name', 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});
admin.get('/get_node_command/:name', 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 });
});
// 获取项目配置
admin.get('/get_project_attr/:name', 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){
p_name = names[0]
}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);
admin.get('/get_project_attr/:name', 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) {
p_name = names[0]
} 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);
});
// 获取所有域名
admin.get('/get_hosts', 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);
admin.get('/get_hosts', 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);
});
// 根据类型获取系统名称
......@@ -169,127 +171,120 @@ admin.get('/get_systems_by_type', function (req, res) {
});
// 给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', 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);
});
// 给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', 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
});
});
// 增加一个新的nginx映射
admin.post('/add_new_proxy', function (req, res) {
let request = req.body;
let new_namespace = request["namespace"],new_ip = request["ip"],new_ports=request["ports"];
var str = String(execSync(`cat ../ngrok/docker_env_name.sh`)).replace(/\n/,"");
let data = parseStr2Json(str);
data[new_namespace] = {
name: new_namespace,
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`)
cproc.exec("../ngrok/make_frp_ini_V2.sh");
res.send({msg:"执行完成"});
admin.post('/add_new_proxy', function(req, res) {
let request = req.body;
let new_namespace = request["namespace"],
new_ip = request["ip"],
new_ports = request["ports"];
var str = String(execSync(`cat ../ngrok/docker_env_name.sh`)).replace(/\n/, "");
let data = parseStr2Json(str);
data[new_namespace] = {
name: new_namespace,
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`)
cproc.exec("../ngrok/make_frp_ini_V2.sh");
res.send({ msg: "执行完成" });
});
// 删除一个namespace映射
admin.post('/remove_proxy', function (req, res) {
let request = req.body;
console.log(request);
let new_namespace = request["namespace"];
var str = String(execSync(`cat ../ngrok/docker_env_name.sh`)).replace(/\n/,"");
let data = parseStr2Json(str);
delete data[new_namespace]
let s = parseJson2Str(data)
s = s.replace("\n","")
execSync(`echo "${s}" > ../ngrok/docker_env_name.sh`)
cproc.exec("../ngrok/make_frp_ini_V2.sh");
var last_str = String(execSync(`cat ../ngrok/docker_env_name.sh`))
res.setHeader("Content-Type","application/json");
res.send({
admin.post('/remove_proxy', function(req, res) {
let request = req.body;
console.log(request);
let new_namespace = request["namespace"];
var str = String(execSync(`cat ../ngrok/docker_env_name.sh`)).replace(/\n/, "");
let data = parseStr2Json(str);
delete data[new_namespace]
let s = parseJson2Str(data)
s = s.replace("\n", "")
execSync(`echo "${s}" > ../ngrok/docker_env_name.sh`)
cproc.exec("../ngrok/make_frp_ini_V2.sh");
var last_str = String(execSync(`cat ../ngrok/docker_env_name.sh`))
res.setHeader("Content-Type", "application/json");
res.send({
"details": {
name_spaces:last_str
},
name_spaces: last_str
},
"success": true
});
});
});
// 获取现有映射关系
admin.get('/get_proxy', function (req, res) {
res.setHeader("Content-Type","application/json");
var str = execSync(`cat ../ngrok/docker_env_name.sh`)
res.send({namespace:String(str)});
admin.get('/get_proxy', function(req, res) {
res.setHeader("Content-Type", "application/json");
var str = execSync(`cat ../ngrok/docker_env_name.sh`)
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)
console.log("http://192.168.4.3:10088/config_server/get_node_command")
app.listen(10088)
console.log("http://192.168.4.3:10088/config_server/get_node_command")
app.listen(10088)
\ No newline at end of file
......@@ -12,8 +12,8 @@
"body-parser": "^1.18.2",
"child_process": "^1.0.2",
"express": "^4.16.2",
"mongoose": "^5.0.4",
"pm2": "^2.7.2",
"request": "^2.83.0"
"request": "^2.83.0",
"mongodb": "^2.2.33",
"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