Authored by xuqi

ci with zookeeper

'use strict';
import Model from './model';
import _ from 'lodash';
const defaultDegrades = [
{
path: '/pc/common/myYohoHover',
name: '【公共头部】MY有货鼠标移入显示账户信息'
},
{
path: '/pc/common/cartCountShow',
name: '【公共头部】购物车图标显示购物车商品数目'
},
{
path: '/pc/common/cartHover',
name: '【公共头部】购物车图标鼠标移入请求、显示购物车商品列表'
},
{
path: '/pc/brandInfoTipSHow',
name: '【品牌一览】品牌名移入显示品牌简介Tip'
},
{
path: '/pc/recentViewShow',
name: '【商品列表/商品详情】显示最近浏览'
},
{
path: '/pc/guang/hotTagShow',
name: '【逛】显示热门标签'
},
{
path: '/pc/guang/adShow',
name: '【逛】显示广告banner'
},
{
path: '/pc/guang/itemCommentShow',
name: '【逛】详情页显示评论'
},
{
path: '/wap/plustar/collect',
name: '【Plustar】品牌收藏'
}
];
class Degrade extends Model {
constructor() {
super('degrade');
}
async init() {
for (let i of defaultDegrades) {
let count = await this.count({
path: i.path
});
if (count === 0) {
await this.insert(i);
}
}
}
}
export default Degrade;
\ No newline at end of file
... ...
... ... @@ -8,6 +8,7 @@ import DeployModel from './deploy';
import UserModel from './user';
import HotfixModel from './hotfix';
import OperationLoggerModel from './operation_logger';
import DegradeModel from './degrade';
shelljs.mkdir('-p', config.dbDir);
... ... @@ -18,9 +19,12 @@ const DeployInfo = new DeployModel();
const User = new UserModel();
const Hotfix = new HotfixModel();
const OperationLogger = new OperationLoggerModel();
const Degrade = new DegradeModel();
User.init();
Degrade.init();
export {
Server,
Building,
... ... @@ -28,5 +32,6 @@ export {
DeployInfo,
User,
Hotfix,
OperationLogger
OperationLogger,
Degrade
};
\ No newline at end of file
... ...
'use strict';
import Router from 'koa-router';
import moment from 'moment';
import _ from 'lodash';
import {Degrade} from '../../models';
import getter from '../../zookeeper/getter';
import setter from '../../zookeeper/setter';
const router = new Router();
const ctl = {
async index (ctx) {
let degrades = await Degrade.findAll();
for (let i of degrades) {
i.checked = await getter(i.path);
}
let pc = _.filter(degrades, o => _.startsWith(o.path, '/pc'));
let wap = _.filter(degrades, o => _.startsWith(o.path, '/wap'));
await ctx.render('action/degrade', {
pc: pc,
wap: wap
});
},
async setter(ctx) {
let {checked, id} = ctx.query;
let theDegrade = await Degrade.findById(id);
let path = theDegrade.path;
await setter(path, checked.toString());
ctx.body = {
code: 200,
message: 'update success'
};
}
};
router.get('/', ctl.index);
router.get('/setter', ctl.setter);
export default router;
\ No newline at end of file
... ...
... ... @@ -9,6 +9,7 @@ import monitor from './actions/monitor';
import users from './actions/users';
import hotfix from './actions/hotfix';
import operationLog from './actions/operation_log';
import degrade from './actions/degrade';
const noAuth = new Router();
const base = new Router();
... ... @@ -35,6 +36,8 @@ export default function (app) {
base.use('/hotfix', hotfix.routes(), hotfix.allowedMethods());
base.use('/operation', operationLog.routes(), operationLog.allowedMethods());
base.use('/degrade', degrade.routes(), degrade.allowedMethods());
base.use('', index.routes(), index.allowedMethods());
app.use(base.routes(), base.allowedMethods());
... ...
<style>
.degrade-tab li {
cursor: pointer;
}
.pc-degrade,
.wap-degrade {
list-style: none;
padding: 20px;
}
</style>
<ul id="degrade-tab" class="nav nav-tabs degrade-tab" role="tablist">
<li role="presentation" class="active">
<a>PC</a>
</li>
<li role="presentation">
<a>WAP</a>
</li>
</ul>
<ul class="pc-degrade degrade-content">
{{#each pc}}
<li data-id="{{_id}}">
<div class="checkbox">
<label>
<input type="checkbox"{{#if checked}} checked{{/if}}>
{{name}}
</label>
</div>
</li>
{{/each}}
</ul>
<ul class="wap-degrade hide">
{{#each wap}}
<li data-id="{{_id}}">
<div class="checkbox">
<label>
<input type="checkbox"{{#if checked}} checked{{/if}}>
{{name}}
</label>
</div>
</li>
{{/each}}
</ul>
<script>
$(function() {
$('#degrade-tab').on('click', 'li', function() {
var $this = $(this);
if ($this.hasClass('active')) {
return;
}
$('li', $('#degrade-tab')).toggleClass('active');
var index = $this.index();
if (index === 0) {
//PC active
$('.pc-degrade').removeClass('hide');
$('.wap-degrade').addClass('hide');
} else {
// wap active
$('.wap-degrade').removeClass('hide');
$('.pc-degrade').addClass('hide');
}
});
// change
$('.degrade-content input[type="checkbox"]').change(function() {
var $checkbox = $(this),
$li = $checkbox.closest('li');
var checked = $checkbox.prop('checked');
var id = $li.data('id');
$.ajax({
url: '/degrade/setter',
data: {
checked: checked,
id: id
}
});
});
})
</script>
\ No newline at end of file
... ...
... ... @@ -34,6 +34,7 @@
</ul>
</li>
{{/if}}
<li><a href="/degrade"><i class="fa fa-hand-o-down"></i> <span>降级配置</span></a></li>
</ul>
</div>
\ No newline at end of file
... ...
'use strict';
export default {
server: 'localhost:2181'
}
\ No newline at end of file
... ...
'use strict';
import zookeeper from 'node-zookeeper-client';
import config from './config';
module.exports = (path) => {
const client = zookeeper.createClient(config.server);
client.once('connected', () => {
client.mkdirp(path, new Buffer('true'), (err, path) => {
if (err) {
console.log('Node %s create err', path, err.stack);
} else {
console.log('Node %s is created', path);
}
client.close();
});
});
client.connect();
};
\ No newline at end of file
... ...
'usu strict';
import _ from 'lodash';
import zookeeper from 'node-zookeeper-client';
import config from './config';
import creator from './creator';
const getter = (client, path, resolve, reject) => {
client.exists(path, (err, stat) => {
if (err) {
console.log('path %s exits error', path, err.stack);
resolve(true);
return;
}
if (stat) {
client.getData(
path,
(err, data, stat) => {
if (err) {
console.log('Got path %s data error', path, err.stack);
}
resolve(data ? data.toString('utf8') === 'true' : true);
client.close();
}
)
} else {
// 不存在的路径
console.log('no path %s, we will create it with value "true" automatic', path);
client.close();
// create path
creator(path);
resolve(true);
}
});
};
module.exports = (path) => new Promise((resolve, reject) => {
const client = zookeeper.createClient(config.server);
client.once('connected', () => {
getter(client, path, resolve, reject);
});
client.connect();
});
... ...
'usu strict';
import _ from 'lodash';
import zookeeper from 'node-zookeeper-client';
import config from './config';
module.exports = (path, val) => new Promise((resolve, reject) => {
const client = zookeeper.createClient(config.server);
client.once('connected', function () {
client.setData(path, new Buffer(val.toString()), function(err, data, stat) {
console.log('path %s data change to', path, val);
resolve();
client.close();
});
});
client.connect();
});
... ...
... ... @@ -54,6 +54,7 @@
"moment": "^2.13.0",
"nedb": "^1.8.0",
"nedb-promise": "^2.0.0",
"node-zookeeper-client": "^0.2.2",
"qn": "^1.3.0",
"qs": "^6.2.0",
"shelljs": "^0.7.0",
... ...