2024-05-28 16:45:34 +00:00
|
|
|
import * as plugins from '../plugins.js';
|
|
|
|
import * as paths from '../paths.js';
|
|
|
|
import { Cloudly } from '../classes.cloudly.js';
|
2024-05-30 20:49:39 +00:00
|
|
|
import { logger } from '../logger.js';
|
2024-04-20 10:21:41 +00:00
|
|
|
|
2024-06-13 07:36:02 +00:00
|
|
|
import { Cluster } from './classes.cluster.js';
|
2024-04-20 10:21:41 +00:00
|
|
|
|
|
|
|
export class ClusterManager {
|
|
|
|
public ready = plugins.smartpromise.defer();
|
|
|
|
|
|
|
|
public cloudlyRef: Cloudly;
|
|
|
|
public get db() {
|
|
|
|
return this.cloudlyRef.mongodbConnector.smartdataDb;
|
|
|
|
}
|
|
|
|
public typedrouter = new plugins.typedrequest.TypedRouter();
|
|
|
|
|
|
|
|
public CCluster = plugins.smartdata.setDefaultManagerForDoc(this, Cluster);
|
|
|
|
|
|
|
|
constructor(cloudlyRef: Cloudly) {
|
|
|
|
this.cloudlyRef = cloudlyRef;
|
|
|
|
this.cloudlyRef.typedrouter.addTypedRouter(this.typedrouter);
|
|
|
|
|
|
|
|
this.typedrouter.addTypedHandler<plugins.servezoneInterfaces.requests.cluster.IRequest_CreateCluster>(
|
|
|
|
new plugins.typedrequest.TypedHandler('createCluster', async (dataArg) => {
|
|
|
|
const cluster = await this.storeCluster({
|
|
|
|
id: plugins.smartunique.uniSimple('cluster'),
|
|
|
|
data: {
|
|
|
|
name: dataArg.clusterName,
|
|
|
|
jumpCode: plugins.smartunique.uniSimple('cluster'),
|
|
|
|
jumpCodeUsedAt: null,
|
|
|
|
acmeInfo: null,
|
|
|
|
cloudlyUrl: `https://${this.cloudlyRef.config.data.publicUrl}:${this.cloudlyRef.config.data.publicPort}/`,
|
|
|
|
servers: [],
|
|
|
|
sshKeys: [],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
console.log(await cluster.createSavableObject());
|
|
|
|
this.cloudlyRef.serverManager.ensureServerInfrastructure();
|
|
|
|
return {
|
|
|
|
clusterConfig: await cluster.createSavableObject(),
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
this.typedrouter.addTypedHandler<plugins.servezoneInterfaces.requests.cluster.IRequest_GetAllClusters>(
|
|
|
|
new plugins.typedrequest.TypedHandler('getAllClusters', async (dataArg) => {
|
|
|
|
// TODO: do authentication here
|
|
|
|
const clusters = await this.getAllClusters();
|
|
|
|
return {
|
|
|
|
clusters: await Promise.all(
|
|
|
|
clusters.map((clusterArg) => clusterArg.createSavableObject())
|
|
|
|
),
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
2024-06-13 07:36:02 +00:00
|
|
|
|
|
|
|
// delete cluster
|
|
|
|
this.typedrouter.addTypedHandler<plugins.servezoneInterfaces.requests.cluster.IRequest_DeleteCluster>(
|
|
|
|
new plugins.typedrequest.TypedHandler('deleteCluster', async (reqDataArg, toolsArg) => {
|
|
|
|
await toolsArg.passGuards([this.cloudlyRef.authManager.adminJwtGuard], reqDataArg);
|
|
|
|
await this.deleteCluster(reqDataArg.clusterId);
|
|
|
|
return {
|
|
|
|
success: true,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
2024-04-20 10:21:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async init() {
|
|
|
|
// lets read the config folder
|
|
|
|
logger.log('info', 'config manager is now initializing');
|
|
|
|
this.ready.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets a cluster config by Name
|
|
|
|
*/
|
|
|
|
public async getClusterConfigBy_ServerIP() {
|
|
|
|
await this.ready.promise;
|
|
|
|
// TODO: implement getclusterConfigByServerIp
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getClusterConfigBy_JumpCode(jumpCodeArg: string) {
|
|
|
|
await this.ready.promise;
|
|
|
|
|
|
|
|
return await Cluster.getInstance({
|
|
|
|
data: {
|
|
|
|
jumpCode: jumpCodeArg,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async getClusterConfigBy_ClusterIdentifier(
|
|
|
|
clusterIdentifier: plugins.servezoneInterfaces.data.IClusterIdentifier
|
|
|
|
) {
|
|
|
|
await this.ready.promise;
|
|
|
|
|
|
|
|
return await Cluster.getInstance({
|
2024-06-13 07:36:02 +00:00
|
|
|
id: clusterIdentifier.clusterId,
|
2024-04-20 10:21:41 +00:00
|
|
|
data: {
|
|
|
|
name: clusterIdentifier.clusterName,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get config by id
|
|
|
|
*/
|
|
|
|
public async getConfigBy_ConfigID(configId: string) {
|
|
|
|
await this.ready.promise;
|
|
|
|
|
|
|
|
return await Cluster.getInstance({
|
|
|
|
id: configId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets all cluster configs
|
|
|
|
*/
|
|
|
|
public async getAllClusters() {
|
|
|
|
await this.ready.promise;
|
|
|
|
|
|
|
|
return await Cluster.getInstances({});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* allows storage of a config
|
|
|
|
* @param configName
|
|
|
|
* @param configObjectArg
|
|
|
|
*/
|
|
|
|
public async storeCluster(configObjectArg: plugins.servezoneInterfaces.data.ICluster) {
|
|
|
|
let clusterInstance = await Cluster.getInstance({ id: configObjectArg.id });
|
|
|
|
if (!clusterInstance) {
|
|
|
|
clusterInstance = await Cluster.fromConfigObject(configObjectArg);
|
|
|
|
} else {
|
|
|
|
Object.assign(clusterInstance, configObjectArg);
|
|
|
|
}
|
|
|
|
await clusterInstance.save();
|
|
|
|
return clusterInstance;
|
|
|
|
}
|
2024-06-13 07:36:02 +00:00
|
|
|
|
|
|
|
public async deleteCluster(clusterId: string) {
|
|
|
|
await this.ready.promise;
|
|
|
|
const clusterInstance = await Cluster.getInstance({ id: clusterId });
|
|
|
|
await clusterInstance.delete();
|
|
|
|
}
|
2024-04-20 10:21:41 +00:00
|
|
|
}
|