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

181 lines
5.0 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="s1:latest";
public names = ["admin","cfgtoollogs","checkpoints","diag","flash-recovery-area","oradata"];
public createRC(Obj){
var labels = {
"style": "oracle",
"status": "0"
};
for(var item in Obj.labels)
labels[item] = Obj.labels[item];
return {
"apiVersion" : Kubernetes.defaultApiVersion,
"kind" : "ReplicationController",
"metadata" : {
"name" : Obj.name,
"labels" : labels
},
"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 findLabelValue(value:string, key: string):string {
var trueValue = '';
switch(value){
case '01':
trueValue = '财政';
break;
case '02':
trueValue = '社保';
break;
case 'A':
trueValue = '批次A';
break;
case 'B':
trueValue = '批次B';
break;
default:
if(key === "version"){
var reg= new RegExp('^[0-9]$').exec(value);
if(reg)
trueValue = '版本' + reg[0];
else trueValue = value;
}
else
trueValue = value;
}
return trueValue
}
export function labelToChinese(labels){
var answer = {};
angular.forEach(labels,(value, key) => {
answer[key] = findLabelValue(value, key);
});
return answer
}
}