145 lines
4.9 KiB
TypeScript
145 lines
4.9 KiB
TypeScript
import * as plugins from './plugins.js';
|
|
|
|
export type TClientType = 'coreflow' | 'cli' | 'serverconfig';
|
|
|
|
import { Image } from './classes.image.js';
|
|
|
|
export class CloudlyClient {
|
|
private cloudlyUrl: string;
|
|
private registerAs: string;
|
|
|
|
public typedrouter = new plugins.typedrequest.TypedRouter();
|
|
public typedsocketClient: plugins.typedsocket.TypedSocket;
|
|
|
|
// Subjects
|
|
public configUpdateSubject = new plugins.smartrx.rxjs.Subject<
|
|
plugins.servezoneInterfaces.requests.config.IRequest_Cloudly_Coreflow_PushClusterConfig['request']
|
|
>();
|
|
|
|
public serverActionSubject = new plugins.smartrx.rxjs.Subject<
|
|
plugins.servezoneInterfaces.requests.server.IRequest_TriggerServerAction['request']
|
|
>();
|
|
|
|
constructor(registerAsArg: TClientType) {
|
|
this.cloudlyUrl = process.env.CLOUDLY_URL || 'https://cloudly.layer.io:443';
|
|
this.registerAs = registerAsArg;
|
|
|
|
console.log(
|
|
`creating LoleCloudlyClient: registering as ${this.registerAs} and target url ${this.cloudlyUrl}`
|
|
);
|
|
|
|
this.typedrouter.addTypedHandler<plugins.servezoneInterfaces.requests.config.IRequest_Cloudly_Coreflow_PushClusterConfig>(
|
|
new plugins.typedrequest.TypedHandler('pushClusterConfig', async (dataArg) => {
|
|
this.configUpdateSubject.next(dataArg);
|
|
return {};
|
|
})
|
|
);
|
|
|
|
this.typedrouter.addTypedHandler<plugins.servezoneInterfaces.requests.config.IRequest_Cloudly_Coreflow_PushClusterConfig>(
|
|
new plugins.typedrequest.TypedHandler('pushClusterConfig', async (dataArg) => {
|
|
this.configUpdateSubject.next(dataArg);
|
|
return {};
|
|
})
|
|
);
|
|
|
|
this.typedrouter.addTypedHandler<plugins.servezoneInterfaces.requests.server.IRequest_TriggerServerAction>(
|
|
new plugins.typedrequest.TypedHandler('triggerServerAction', async (dataArg) => {
|
|
this.serverActionSubject.next(dataArg);
|
|
return {
|
|
actionConfirmed: true,
|
|
};
|
|
})
|
|
);
|
|
}
|
|
|
|
public async start() {
|
|
this.typedsocketClient = await plugins.typedsocket.TypedSocket.createClient(
|
|
this.typedrouter,
|
|
this.cloudlyUrl
|
|
);
|
|
console.log(`CloudlyCluent connected to cloudly at ${this.cloudlyUrl}. Remember to get an identity.`)
|
|
}
|
|
|
|
public async stop() {
|
|
await this.typedsocketClient.stop();
|
|
}
|
|
|
|
public identity: plugins.servezoneInterfaces.data.IClusterIdentifier;
|
|
public async getIdentityByJumpCode(
|
|
jumpCodeArg: string,
|
|
tagConnection = false,
|
|
statefullIdentity = true
|
|
): Promise<plugins.servezoneInterfaces.data.IClusterIdentifier> {
|
|
const identityRequest =
|
|
this.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.identity.IRequest_Any_Cloudly_CoreflowManager_GetIdentityByJumpCode>(
|
|
'getIdentityByJumpCode'
|
|
);
|
|
console.log(`trying to get identity from cloudly with supplied jumpCodeArg: ${jumpCodeArg}`);
|
|
const response = await identityRequest.fire({
|
|
jumpCode: jumpCodeArg,
|
|
});
|
|
console.log('got identity response');
|
|
const identity = response.clusterIdentifier;
|
|
|
|
if (tagConnection) {
|
|
this.typedsocketClient.addTag('identity', identity);
|
|
}
|
|
|
|
if (statefullIdentity) {
|
|
this.identity = identity;
|
|
}
|
|
|
|
return identity;
|
|
}
|
|
|
|
public async getClusterConfigFromCloudlyByIdentity(
|
|
identityArg: plugins.servezoneInterfaces.data.IClusterIdentifier
|
|
): Promise<plugins.servezoneInterfaces.data.ICluster> {
|
|
const clusterConfigRequest =
|
|
this.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.config.IRequest_Any_Cloudly_GetClusterConfig>(
|
|
'getClusterConfig'
|
|
);
|
|
const response = await clusterConfigRequest.fire({
|
|
jwt: '',
|
|
clusterIdentifier: identityArg,
|
|
});
|
|
return response.configData;
|
|
}
|
|
|
|
public async getServerConfigFromCloudlyByIdentity(
|
|
identityArg: plugins.servezoneInterfaces.data.IClusterIdentifier
|
|
): Promise<plugins.servezoneInterfaces.data.IServer> {
|
|
const serverConfigRequest =
|
|
this.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.config.IRequest_Any_Cloudly_GetServerConfig>(
|
|
'getServerConfig'
|
|
);
|
|
const response = await serverConfigRequest.fire({
|
|
jwt: '', // TODO: do proper auth here
|
|
serverId: '' // TODO: get server id here
|
|
});
|
|
return response.configData;
|
|
}
|
|
|
|
/**
|
|
* gets a certificate for a domain used by a service
|
|
* @param serviceNameArg
|
|
* @param domainNameArg
|
|
*/
|
|
public async getCertificateForDomainOverHttps(domainNameArg: string): Promise<plugins.tsclass.network.ICert> {
|
|
const typedCertificateRequest =
|
|
this.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.certificate.IRequest_Any_Cloudly_GetSslCertificate>(
|
|
'getSslCertificate'
|
|
);
|
|
const typedResponse = await typedCertificateRequest.fire({
|
|
authToken: '', // do proper auth here
|
|
requiredCertName: domainNameArg,
|
|
});
|
|
return typedResponse.certificate;
|
|
}
|
|
|
|
// Images
|
|
public async getImages() {
|
|
return Image.getImages(this);
|
|
}
|
|
}
|