aggregation-platform/plugins/kubernetes/ts/utilHelpers.ts

397 lines
13 KiB
TypeScript

/// <reference path="../../includes.ts"/>
/// <reference path="./kubernetesHelpers.ts"/>
module Kubernetes {
export var hostPorts = [];
/**
* Sorts the the ip field
*
* @param ip the ip such as '10.1.2.13'
* @returns {any}
*/
export function sortByPodIp(ip): any {
// i guess there is maybe nicer ways of sort this without parsing and slicing
var regex = /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
var groups = regex.exec(ip);
if (angular.isDefined(groups)) {
var g1 = ("00" + groups[1]).slice(-3);
var g2 = ("00" + groups[2]).slice(-3);
var g3 = ("00" + groups[3]).slice(-3);
var g4 = ("00" + groups[4]).slice(-3);
var answer = g1 + g2 + g3 + g4;
return answer;
} else {
return 0;
}
}
export function ramdomPort(): number {
var hostPort = Math.floor(30000 + Math.random() * (65535 - 30000));
while (hostPorts.indexOf(hostPort) === 0) {
hostPort = Math.floor(30000 + Math.random() * (65535 - 30000));
}
hostPorts.push(hostPort);
return hostPort
}
export function getRandomString(len: number): string {
len = len || 32;
var $chars = 'abcdefhijkmnprstwxyz'; // 默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1
var maxPos = $chars.length;
var pwd = '';
for (var i = 0; i < len; i++) {
pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
}
export class resourceRCTemplate {
public image = "oracle:utf8";
public names = ["oradata"];
public createRC(Obj) {
var labels = {
"style": "oracle",
"status": "0",
"isExtract": Obj.isExtract + "" || "0",
"isTarget": Obj.isTarget
};
for (var item in Obj.labels)
labels[item] = Obj.labels[item];
return {
"apiVersion": Kubernetes.defaultApiVersion,
"kind": "ReplicationController",
"metadata": {
"name": Obj.name,
"labels": labels,
"annotations": Obj.annotations
},
"spec": {
replicas: Obj.replicas || 1,
"template": this.createTemplate(Obj)
}
}
}
public createVolumeMounts() {
var volumeMounts = [];
for (var item in this.names) {
if (this.names[item] === 'flash-recovery-area')
volumeMounts.push({
"name": this.names[item],
"mountPath": "/opt/oracle/app/flash_recovery_area"
});
else
volumeMounts.push({
"name": this.names[item],
"mountPath": "/opt/oracle/app/" + this.names[item]
});
}
return volumeMounts;
}
public createVolumes(rootPath: string) {
var volumes = [];
for (var item in this.names) {
if (this.names[item] === 'flash-recovery-area')
volumes.push({
"name": this.names[item],
"hostPath": {
"path": rootPath + "flash_recovery_area"
}
});
else
volumes.push({
"name": this.names[item],
"hostPath": {
"path": rootPath + this.names[item]
}
});
}
return volumes;
}
public createContainers(Obj) {
var containers = [];
containers.push({
"name": "oracle",
"image": this.image,
"imagePullPolicy": "IfNotPresent",
"command": ["/assets/entrypoint.sh"],
"ports": [{
"containerPort": 1521,
"hostPort": Obj.port || ramdomPort()
}],
"resources": {
"cpu": "100m",
"memory": "3Gi"
},
"volumeMounts": this.createVolumeMounts()
});
return containers
}
public createTemplate(Obj) {
return {
"metadata": {
//"name": Obj.name,
"labels": Obj.labels
},
"spec": {
"terminationGracePeriodSeconds": 0,
"containers": this.createContainers(Obj),
"volumes": this.createVolumes(Obj.path)
}
}
}
}
export function labelToChinese(labels) {
var answer = {};
angular.forEach(labels, (value, key) => {
answer[key] = labelChangeToChines(value, key);
});
return answer
}
export function findSameNameReplicationControllers(replicationControllers, name) {
var names = [];
replicationControllers.forEach((rc) => {
var rcName = getName(rc);
if (rcName.indexof(name) !== -1)
names.push(rcName);
});
if (names.length === 0) {
return name + "_1";
} else {
var max = 0;
names.forEach((value) => {
var answer = value.split("_");
var key = parseInt(answer[1]);
if (max < key)
max = key;
});
return name + (max + 1);
}
}
export function isFilterRC(rc) {
var answer = false;
angular.forEach(Core.pathGet(rc, ["metadata", "labels"]), (value, key) => {
if (key === 'isTarget' && value === 'true') {
answer = true;
}
});
return answer;
}
export function isInclude(rcs, rc) {
for (var i in rcs) {
if (getName(rcs[i]) === getName(rc))
return true;
}
return false;
}
export function labelChangeToChines(value: string, key: string) {
var trueValue = '';
switch (key) {
case 'type':
if (value === '01')
trueValue = '财政';
else if (value === '02')
trueValue = '社保';
else
trueValue = value;
break;
case 'batch':
if (value === 'A')
trueValue = '批次A';
else if (value === 'B')
trueValue = '批次B';
else
trueValue = value;
break;
case 'region':
trueValue = Kubernetes.getCountyByCode(value);
break;
case 'system':
trueValue = Kubernetes.getSystemNameById(value);
break;
case 'version':
var reg = new RegExp('^[0-9]$').exec(value);
if (reg)
trueValue = '版本' + reg[0];
else
trueValue = value;
break;
case 'isTarget':
if (value === 'true')
trueValue = '汇总数据库';
else
trueValue = value;
break;
default:
trueValue = value;
}
return trueValue;
}
export function checkForExit(replicationControllers: Array < KubePod > , selectItem) {
for (var index in replicationControllers) {
var replicas = replicationControllers[index];
if (getName(replicas) == selectItem._key && getAnnotationValueBykey(replicas, "year") == selectItem["year"])
return true;
}
return false;
}
function getAnnotationValueBykey(replicationController, key: string) {
var annotations = getAnnotations(replicationController);
if (annotations && annotations != undefined) {
return annotations[key];
}
}
/*
启动oracle时检查是否正在迁移
*/
export function checkForCreateOracle(transferTasks: Array < any > , selectItems: Array < any > ) {
var result = [];
angular.forEach(selectItems, (item) => {
if (checkForExitTask(transferTasks, item))
result.push(item);
});
return result;
}
function checkForExitTask(transferTasks: Array < any > , selectItem) {
for (var index in transferTasks) {
var task = transferTasks[index];
if (task["_key"] == selectItem["_key"] && selectItem["year"] == task.labels.dataYear) {
if (task.status == 2 || task.status == 3)
return false;
else
return true;
}
}
return false;
}
/**
**/
export function checkForExtract(replicationControllers: Array < any > ) {
var result = [];
angular.forEach(replicationControllers, (replicas) => {
var labels = getLabels(replicas);
if (labels["isExtract"] == "1")
result.push(replicas);
});
return result;
}
export function checkIsStartSuccess(replicationControllers: Array < any > ) {
var result = [];
angular.forEach(replicationControllers, (replicas) => {
var labels = getLabels(replicas);
if (labels["status"] != "2")
result.push(replicas);
});
return result;
};
/*
检查数据否已挂载使用或正在迁移。
*/
export function checkForMigration(replicationControllers: Array < KubePod > , transferTasks: Array < any > , selectItem: Array < any > , type: string) {
var message = "";
for (var index in selectItem) {
var item = selectItem[index];
var id = null;
if (type == 'manager')
id = item.id;
if (type == 'verify')
id = item.dataId
/*
检查迁移文件是否正在迁移
*/
for (i in transferTasks) {
/*if(transferTasks[i]["_key"] == item["_key"] && (transferTasks[i]["status"] == "1" || transferTasks[i]["status"] == "0") && item["year"] == transferTasks[i].labels.dataYear){
message = "数据正在迁移"
return message;
}*/
if (transferTasks[i].fkid == id && (transferTasks[i]["status"] == "1" || transferTasks[i]["status"] == "0")) {
message = "数据正在迁移"
return message;
}
}
/**
数据是否在热区,如果在则需要判断数据是否已启动
*/
if (item["mark"] == 1)
continue;
/*
检查迁移文件是否已挂载使用
*/
for (var i in replicationControllers) {
var labels = getLabels(replicationControllers[i]);
if (getName(replicationControllers[i]) == item._key && getAnnotationValueBykey(replicationControllers[i], "year") == item["year"]) {
message = "数据正在使用";
return message;
}
}
}
return message;
}
export function eliminateChechBoxClick() {
var element = $("#tableConfig > thead > tr input:checkbox");
if (element.is(':checked'))
element.click();
}
/*
是否已存在当前
*/
export function alreadyExitInFolder(selectedItems: Array < any > , selectNode) {
var childNodes = selectNode.childNodes;
for (var i in selectedItems) {
var itemName = selectedItems[i].regionalismCode + "_" + selectedItems[i].systemCode;
for (var j in childNodes) {
if (childNodes[j].name == itemName) {
var childrens = childNodes[j].childNodes;
for (var k in childrens)
if (childrens[k].name == selectedItems[i].dataVersion)
return true;
}
}
}
return false;
}
export function alreadyExitInCluster(selectedItems: Array < any > , volumePath) {
for (var i = 0; i < selectedItems.length; ++i) {
if (selectedItems[i].dataPath.indexOf(volumePath) == 0) {
return true;
}
}
return false;
}
}