Commit b68c0507 authored by 智勇's avatar 智勇

同步数据库

parent 45eff20d
......@@ -6,18 +6,18 @@ const {
} = require('../kubeService/service')
const getClient = require('../resource/getClient')
const awaitRequest = require('../utils/awaitRequest')
const APP_CONFIG = require('../config')
const syncDB = async (cluster) => {
logger.info('dailySyncDB start')
const client = (await getClient(cluster)).clientInfo
const ns = await getAllNamespace(client)
// for test
const testNamespace = ['fis', 'arch']
const nsTest = ns.namespaces && ns.namespaces.filter(item => testNamespace.includes(item.name))
for (const namespace of nsTest) {
// for (const namespace of ns.namespaces) {
// // for test
// const testNamespace = ['fis']
// const nsTest = ns.namespaces && ns.namespaces.filter(item => testNamespace.includes(item.name))
// for (const namespace of nsTest) {
for (const namespace of ns.namespaces) {
const svcs = await getServicesFormat(client, namespace.name)
for (const svc of svcs) {
if (svc.serviceName === 'mysql') {
......@@ -30,15 +30,15 @@ const syncDB = async (cluster) => {
}
const res = await awaitRequest({
url: 'http://qaapi.liangkebang.com/dbsync/tke',
url: `${APP_CONFIG.qaapiHost}/dbsync/tke`,
method: 'post',
body: JSON.stringify(data),
})
if (JSON.parse(res.body).code === '0000') {
if (res.code === '0000') {
logger.info('开始同步ns:', namespace.name)
} else {
logger.info('同步ns失败:', namespace.name, JSON.parse(res.body).data)
logger.info('同步ns失败:', namespace.name, res.data)
}
sleep.msleep(2 * 1000)
break
......@@ -49,8 +49,8 @@ const syncDB = async (cluster) => {
}
module.exports = () => {
schedule.scheduleJob('*/2 * * * *', async () => {
// schedule.scheduleJob('0 2 * * *', async () => {
// schedule.scheduleJob('*/2 * * * *', async () => {
schedule.scheduleJob('0 1 * * *', async () => {
try {
await syncDB('qa')
} catch (e) {
......
......@@ -15,7 +15,7 @@ const tag = require('./tag')
const cluster = require('./cluster')
const ingressRoute = require('./ingress')
const deploy = require('./restartLatest')
// const dailySyncDB = require('./dailySyncDB')
const dailySyncDB = require('./dailySyncDB')
const client = require('../middleware/client')
const logFormat = ':req[x-real-ip] :req[x-forwarded-for] - -'
......@@ -59,7 +59,7 @@ exports.start = function (port) {
// 加载所有路由
loadRoutes(router)
deploy()
// dailySyncDB()
dailySyncDB()
app.use(log4js.koaLogger(log4js.getLogger('http'), { level: 'auto', format: logFormat }))
app.use(router.routes())
app.listen(port)
......
......@@ -11,8 +11,8 @@ const makeResouce = require('./../resource/makeResouce')
const deploy = async (cluster) => {
logger.info('deployLatest start')
const client = (await getClient(cluster)).clientInfo
const ns = await getAllNamespace(client)
const client = await getClient(cluster)
const ns = await getAllNamespace(client.clientInfo)
// // for test
// const testNamespace = ['fis']
......@@ -21,22 +21,22 @@ const deploy = async (cluster) => {
for (const namespace of ns.namespaces) {
logger.info('开始检查ns:', namespace.name)
const svcs = await getServicesFormat(client, namespace.name)
const svcs = await getServicesFormat(client.clientInfo, namespace.name)
for (const svc of svcs) {
// 重启latest
if (svc.image.indexOf('latest') !== -1) {
logger.info('检查项目', svc.image)
if (~svc.image.indexOf('latest')) {
logger.info('检查项目', namespace.name, svc.serviceName)
const repoName = svc.image.split(':')[0].split('/').slice(1).join('/')
const latest = await container.getTagByName(repoName, 'latest')
const latestImageID = _.get(latest.tagInfo, '[0].tagId', '')
if (svc.imageID.split('@')[1] !== latestImageID) {
logger.info('latest不一致,部署项目', namespace.name, svc.podName)
await serviceRestart(client, namespace.name, svc.podName)
await serviceRestart(client.clientInfo, namespace.name, svc.podName)
}
sleep.msleep(1000)
}
// 把master更新部署成lastet
if (svc.image.indexOf(':master-') !== -1) {
if (~svc.image.search(':master-')) {
logger.info('将master分支', svc.image, '部署成latest')
const repoName = svc.image.split(':')[0].split('/').slice(1).join('/')
let mock
......@@ -46,14 +46,14 @@ const deploy = async (cluster) => {
const data = {
image: `${repoName}:latest`,
namespace: namespace.name,
serviceName: repoName.split('/')[1],
serviceName: svc.serviceName,
type: svc.labels.type,
label: svc.labels.type,
mock,
}
const resources = await makeResouce(data.serviceName, data.label)
data.resources = resources
await deployUpdate(client.clusterInfo, client, data)
await deployUpdate(client.clusterInfo, client.clientInfo, data)
sleep.msleep(1000)
}
}
......
......@@ -143,7 +143,6 @@ router.post('/modifyDeploy', async (ctx) => {
const resources = await makeResouce(data.serviceName, data.label)
data.resources = resources
await deployUpdate(ctx.cluterParams, ctx.client, data)
// await deployUpdate(ctx.cluster, ctx.client, data)
ctx.body = ctx.ok('更新成功')
})
......
......@@ -13,5 +13,5 @@ module.exports = {
noHealthCheckApp: [
'acs-batch', 'acs-quartz', 'acs-service', 'pcm', 'vcc-talos', 'app-server', 'smart-recruitment-spyder', 'cash-loan-flow-report',
],
qaapiHost: 'http://qaapi.liangkebang.com',
}
const Client = require('kubernetes-client').Client
const config = require('kubernetes-client').config
const awaitRequest = require('../utils/awaitRequest')
const APP_CONFIG = require('../config')
const getClient = async function (cluster) {
const res = await awaitRequest({
url: `http://qaapi.liangkebang.com/cluster/list?clusterName=${cluster}`,
url: `${APP_CONFIG.qaapiHost}/cluster/list?clusterName=${cluster}`,
method: 'get',
})
const ca = JSON.parse(res.body).data[0]
const ca = res.data[0]
const clientInfo = new Client({
config: config.fromKubeconfig(ca.clusterCA),
......
const { defaultConfig } = require('../resource/resourceLimit')
const awaitRequest = require('../utils/awaitRequest')
const APP_CONFIG = require('../config')
// 资源限制
module.exports = async (serviceName, type) => {
let resources = defaultConfig[type]
const res = await awaitRequest({
url: `http://qaapi.liangkebang.com/proconfig/getK8sConf?project_name=${serviceName}`,
url: `${APP_CONFIG.qaapiHost}/proconfig/getK8sConf?project_name=${serviceName}`,
method: 'get',
})
const mongoResouces = JSON.parse(res.body).data
const mongoResouces = res.data
if (mongoResouces && mongoResouces.memLimit) {
resources = mongoResouces
}
......
......@@ -7,11 +7,11 @@ module.exports = function (options) {
'Content-Type': 'application/json;charset=utf-8',
cluster: 'qa',
},
}), (error, res) => {
}), (error, res, body) => {
if (error) {
reject(error)
} else {
resolve(res)
resolve(JSON.parse(body))
}
})
})
......
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