Skip to content

Commit 21d4bbd

Browse files
committed
test
1 parent 9c2a0f7 commit 21d4bbd

12 files changed

Lines changed: 192 additions & 2 deletions

OpenFlow/src/Config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export class Config {
1212
public static auto_create_users: boolean = Config.parseBoolean(Config.getEnv("auto_create_users", "false"));
1313
public static auto_create_domains: string[] = Config.parseArray(Config.getEnv("auto_create_domains", ""));
1414
public static allow_user_registration: boolean = Config.parseBoolean(Config.getEnv("allow_user_registration", "false"));
15+
public static allow_personal_nodered: boolean = Config.parseBoolean(Config.getEnv("allow_personal_nodered", "false"));
1516

1617
public static api_bypass_perm_check: boolean = Config.parseBoolean(Config.getEnv("api_bypass_perm_check", "false"));
1718
public static websocket_package_size: number = parseInt(Config.getEnv("websocket_package_size", "1024"), 10);
@@ -24,6 +25,7 @@ export class Config {
2425
public static port: number = parseInt(Config.getEnv("port", "3000"));
2526
public static domain: string = Config.getEnv("domain", "localhost");
2627
public static namespace: string = Config.getEnv("namespace", "");
28+
public static nodered_domain_schema: string = Config.getEnv("nodered_domain_schema", "");
2729

2830
public static protocol: string = Config.getEnv("protocol", "http");
2931
public static saml_issuer: string = Config.getEnv("saml_issuer", "the-issuer");
@@ -36,6 +38,8 @@ export class Config {
3638
public static aes_secret: string = Config.getEnv("aes_secret", "");
3739
public static skip_history_collections: string = Config.getEnv("skip_history_collections", "");
3840

41+
public static kubeconfig: string = Config.getEnv("kubeconfig", "");
42+
3943
public static baseurl(): string {
4044
var result: string = "";
4145
if (Config.tls_crt != '' && Config.tls_key != '') {

OpenFlow/src/KubeUtil.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import * as k8s from "@kubernetes/client-node";
2+
import { CoreV1Api, AppsV1Api, ExtensionsV1beta1Api } from "@kubernetes/client-node";
3+
import { Config } from "./Config";
4+
5+
export class KubeUtil {
6+
private CoreV1Api: CoreV1Api = null; // kc.makeApiClient(k8s.CoreV1Api);
7+
private AppsV1Api: AppsV1Api = null; // kc.makeApiClient(k8s.AppsV1Api);
8+
private ExtensionsV1beta1Api: ExtensionsV1beta1Api = null; // kc.makeApiClient(k8s.ExtensionsV1beta1Api);
9+
10+
private static _instance: KubeUtil = null;
11+
public static instance(): KubeUtil {
12+
if (this._instance == null) {
13+
this._instance = new KubeUtil();
14+
}
15+
return this._instance;
16+
}
17+
constructor() {
18+
var config = Config.kubeconfig;
19+
const kc = new k8s.KubeConfig();
20+
kc.loadFromString(config);
21+
this.CoreV1Api = kc.makeApiClient(k8s.CoreV1Api);
22+
this.AppsV1Api = kc.makeApiClient(k8s.AppsV1Api);
23+
this.ExtensionsV1beta1Api = kc.makeApiClient(k8s.ExtensionsV1beta1Api);
24+
}
25+
26+
async GetStatefulSet(namespace, name) {
27+
var list = await this.AppsV1Api.listNamespacedStatefulSet(namespace);
28+
for (var i = 0; i < list.body.items.length; i++) {
29+
var item = list.body.items[i];
30+
if (item.metadata.name == name) return item;
31+
}
32+
return null;
33+
}
34+
async GetService(namespace, name) {
35+
var list = await this.CoreV1Api.listNamespacedService(namespace);
36+
for (var i = 0; i < list.body.items.length; i++) {
37+
var item = list.body.items[i];
38+
//console.log(item);
39+
//var json = JSON.stringify(item, null, 3);
40+
//console.log(json);
41+
if (item.metadata.name == name) return item;
42+
}
43+
return null;
44+
}
45+
async GetPod(namespace, name) {
46+
var list = await this.CoreV1Api.listNamespacedPod(namespace);
47+
for (var i = 0; i < list.body.items.length; i++) {
48+
var item = list.body.items[i];
49+
if (item.metadata.name == name) return item;
50+
}
51+
return null;
52+
}
53+
async GetDeployment(namespace, name) {
54+
var list = await this.ExtensionsV1beta1Api.listNamespacedDeployment(namespace);
55+
for (var i = 0; i < list.body.items.length; i++) {
56+
var item = list.body.items[i];
57+
if (item.metadata.name == name) return item;
58+
}
59+
return null;
60+
}
61+
async GetIngress(namespace, name) {
62+
var list = await this.ExtensionsV1beta1Api.listNamespacedIngress(namespace);
63+
for (var i = 0; i < list.body.items.length; i++) {
64+
var item = list.body.items[i];
65+
if (item.metadata.name == name) return item;
66+
}
67+
return null;
68+
}
69+
70+
}

OpenFlow/src/LoginProvider.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ export class LoginProvider {
140140
}
141141
_url += "/";
142142
var res2 = {
143-
wshost: _url
143+
wshost: _url,
144+
domain: Config.domain,
145+
allow_personal_nodered: Config.allow_personal_nodered,
146+
namespace: Config.namespace,
147+
nodered_domain_schema: Config.nodered_domain_schema
144148
}
145149
res.end(JSON.stringify(res2));
146150
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Base } from "../base";
2+
3+
export class DeleteNoderedInstanceMessage<T extends Base> implements IReplyMessage {
4+
public error: string;
5+
public jwt: any;
6+
7+
static assign<T extends Base>(o: any): DeleteNoderedInstanceMessage<T> {
8+
if (typeof o === "string" || o instanceof String) {
9+
return Object.assign(new DeleteNoderedInstanceMessage(), JSON.parse(o.toString()));
10+
}
11+
return Object.assign(new DeleteNoderedInstanceMessage(), o);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Base } from "../base";
2+
3+
export class EnsureNoderedInstanceMessage implements IReplyMessage {
4+
public error: string;
5+
public jwt: any;
6+
7+
static assign(o: any): EnsureNoderedInstanceMessage {
8+
if (typeof o === "string" || o instanceof String) {
9+
return Object.assign(new EnsureNoderedInstanceMessage(), JSON.parse(o.toString()));
10+
}
11+
return Object.assign(new EnsureNoderedInstanceMessage(), o);
12+
}
13+
}

OpenFlow/src/Messages/Message.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { RegisterQueueMessage } from "./RegisterQueueMessage";
2222
import { QueueMessage } from "./QueueMessage";
2323
import { RegisterUserMessage } from "./RegisterUserMessage";
2424
import { UpdateManyMessage } from "./UpdateManyMessage";
25+
import { EnsureNoderedInstanceMessage } from "./EnsureNoderedInstanceMessage";
2526

2627
export class Message {
2728
public id: string;
@@ -121,6 +122,18 @@ export class Message {
121122
case "closequeue":
122123
this.CloseQueue(cli);
123124
break;
125+
case "ensurenoderedinstance":
126+
this.EnsureNoderedInstance(cli);
127+
break;
128+
case "deletenoderedinstance":
129+
this.DeleteNoderedInstance(cli);
130+
break;
131+
case "startnoderedinstance":
132+
this.StartNoderedInstance(cli);
133+
break;
134+
case "stopnoderedinstance":
135+
this.StopNoderedInstance(cli);
136+
break;
124137
default:
125138
this.UnknownCommand(cli);
126139
break;
@@ -476,6 +489,41 @@ export class Message {
476489
}
477490
this.Send(cli);
478491
}
492+
493+
private async EnsureNoderedInstance(cli: WebSocketClient): Promise<void> {
494+
this.Reply();
495+
var msg: EnsureNoderedInstanceMessage;
496+
var user: User;
497+
try {
498+
msg = EnsureNoderedInstanceMessage.assign(this.data);
499+
} catch (error) {
500+
msg.error = error.toString();
501+
}
502+
try {
503+
this.data = JSON.stringify(msg);
504+
} catch (error) {
505+
this.data = "";
506+
msg.error = error.toString();
507+
}
508+
this.Send(cli);
509+
}
510+
private async DeleteNoderedInstance(cli: WebSocketClient): Promise<void> {
511+
this.Reply();
512+
this.Send(cli);
513+
}
514+
private async StartNoderedInstance(cli: WebSocketClient): Promise<void> {
515+
this.Reply();
516+
this.Send(cli);
517+
}
518+
private async StopNoderedInstance(cli: WebSocketClient): Promise<void> {
519+
this.Reply();
520+
this.Send(cli);
521+
}
522+
523+
524+
525+
526+
479527
}
480528

481529
export class JSONfn {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Base } from "../base";
2+
3+
export class StartNoderedInstanceMessage<T extends Base> implements IReplyMessage {
4+
public error: string;
5+
public jwt: any;
6+
7+
static assign<T extends Base>(o: any): StartNoderedInstanceMessage<T> {
8+
if (typeof o === "string" || o instanceof String) {
9+
return Object.assign(new StartNoderedInstanceMessage(), JSON.parse(o.toString()));
10+
}
11+
return Object.assign(new StartNoderedInstanceMessage(), o);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Base } from "../base";
2+
3+
export class StopNoderedInstanceMessage<T extends Base> implements IReplyMessage {
4+
public error: string;
5+
public jwt: any;
6+
7+
static assign<T extends Base>(o: any): StopNoderedInstanceMessage<T> {
8+
if (typeof o === "string" || o instanceof String) {
9+
return Object.assign(new StopNoderedInstanceMessage(), JSON.parse(o.toString()));
10+
}
11+
return Object.assign(new StopNoderedInstanceMessage(), o);
12+
}
13+
}

OpenFlow/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ process.on('unhandledRejection', up => {
125125
WebSocketServer.configure(logger, server);
126126
logger.info("listening on " + Config.baseurl());
127127
logger.info("namespace: " + Config.namespace);
128+
logger.info("kubeconfig: " + Config.kubeconfig);
128129
if (!await initDatabase()) {
129130
process.exit(404);
130131
}

OpenFlow/src/public/WebSocketClient.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ module openflow {
2323
}
2424
export class WebSocketClient {
2525
private _socketObject: ReconnectingWebSocket = null;
26+
27+
public domain: string = null;
28+
public allow_personal_nodered: boolean = false;
29+
public namespace: string = null;
30+
public nodered_domain_schema: string = null;
31+
2632
private _url: string = null;
2733
private static instance: WebSocketClient = null;
2834
private _receiveQueue: SocketMessage[] = [];
@@ -198,6 +204,10 @@ module openflow {
198204
}
199205
console.log(data.wshost);
200206
console.debug("WebSocketClient::onopen: connecting to " + data.wshost);
207+
this.domain = data.domain;
208+
this.allow_personal_nodered = data.allow_personal_nodered;
209+
this.namespace = data.namespace;
210+
this.nodered_domain_schema = data.nodered_domain_schema;
201211
this._socketObject = new ReconnectingWebSocket(data.wshost);
202212
this._socketObject.onopen = (this.onopen).bind(this);
203213
this._socketObject.onmessage = (this.onmessage).bind(this);

0 commit comments

Comments
 (0)