import * as plugins from '../plugins.js'; import * as paths from '../paths.js'; import { Cloudly } from '../classes.cloudly.js'; import { logger } from '../logger.js'; import { Cluster } from './classes.cluster.js'; 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( new plugins.typedrequest.TypedHandler('createCluster', async (dataArg) => { // TODO: guards const cluster = await this.createCluster({ id: plugins.smartunique.uniSimple('cluster'), data: { userId: null, name: dataArg.clusterName, initialJumpToken: plugins.smartunique.uniSimple('initialJumpToken'), initialJumpTokenUsedAt: 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( 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()) ), }; }) ); // delete cluster this.typedrouter.addTypedHandler( new plugins.typedrequest.TypedHandler('deleteCluster', async (reqDataArg, toolsArg) => { await toolsArg.passGuards([this.cloudlyRef.authManager.adminIdentityGuard], reqDataArg); await this.deleteCluster(reqDataArg.clusterId); return { success: true, }; }) ); } 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 getClusterBy_JumpCode(initialJumpTokenArg: string) { await this.ready.promise; return await Cluster.getInstance({ data: { initialJumpToken: initialJumpTokenArg, }, }); } public async getClusterBy_Identity( clusterIdentity: plugins.servezoneInterfaces.data.IIdentity ) { await this.ready.promise; return await Cluster.getInstance({ data: { userId: clusterIdentity.userId, }, }); } /** * 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 createCluster(configObjectArg: plugins.servezoneInterfaces.data.ICluster) { // TODO: guards // lets create the cluster user const clusterUser = new this.cloudlyRef.authManager.CUser(); clusterUser.id = await this.cloudlyRef.authManager.CUser.getNewId(); clusterUser.data = { role: 'cluster', type: 'machine', } await clusterUser.save(); Object.assign(configObjectArg, { userId: clusterUser.id, }); const clusterInstance = await Cluster.fromConfigObject(configObjectArg); await clusterInstance.save(); return clusterInstance; } public async deleteCluster(clusterId: string) { await this.ready.promise; const clusterInstance = await Cluster.getInstance({ id: clusterId }); await clusterInstance.delete(); } }