You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aggregation-platform/plugins/kubernetes/ts/utilHelpers.ts

241 lines
6.8 KiB

/// <reference path="../../includes.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 = ["admin","cfgtoollogs","checkpoints","diag","flash-recovery-area","oradata"];
public createRC(Obj){
var labels = {
"style": "oracle",
"status": "0",
"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()
}],
"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;
}
}