fix(core): update
This commit is contained in:
32
ts/manager.version/containerversion.ts
Normal file
32
ts/manager.version/containerversion.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import * as plugins from '../cloudly.plugins.js';
|
||||
|
||||
/*
|
||||
A container version is managed by the versionmanager
|
||||
*/
|
||||
@plugins.smartdata.Manager()
|
||||
export class ContainerVersion
|
||||
extends plugins.smartdata.SmartDataDbDoc<ContainerVersion, unknown>
|
||||
implements plugins.servezoneInterfaces.data.IContainerVersionData
|
||||
{
|
||||
public static async fromIVersionData(
|
||||
dataArg: plugins.servezoneInterfaces.data.IContainerVersionData
|
||||
) {
|
||||
const containerVersionInstance = new ContainerVersion();
|
||||
containerVersionInstance.id = plugins.smartunique.shortId();
|
||||
Object.assign(containerVersionInstance, dataArg);
|
||||
return containerVersionInstance;
|
||||
}
|
||||
|
||||
@plugins.smartdata.unI()
|
||||
public id: string;
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
public dockerImageUrl: string;
|
||||
|
||||
@plugins.smartdata.svDb()
|
||||
public dockerImageVersion: string;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
}
|
149
ts/manager.version/versionmanager.ts
Normal file
149
ts/manager.version/versionmanager.ts
Normal file
@ -0,0 +1,149 @@
|
||||
import * as plugins from '../cloudly.plugins.js';
|
||||
|
||||
import { ContainerVersion } from './containerversion.js';
|
||||
import { Cloudly } from '../cloudly.classes.cloudly.js';
|
||||
|
||||
export class CloudlyVersionManager {
|
||||
// INSTANCE
|
||||
public cloudlyRef: Cloudly;
|
||||
public get db() {
|
||||
return this.cloudlyRef.mongodbConnector.smartdataDb;
|
||||
}
|
||||
public typedRouter = new plugins.typedrequest.TypedRouter();
|
||||
|
||||
// connected classes
|
||||
public CContainerVersion = plugins.smartdata.setDefaultManagerForDoc(this, ContainerVersion);
|
||||
|
||||
constructor(cloudlyRefArg: Cloudly) {
|
||||
this.cloudlyRef = cloudlyRefArg;
|
||||
this.cloudlyRef.typedrouter.addTypedRouter(this.typedRouter);
|
||||
// get version
|
||||
this.typedRouter.addTypedHandler<plugins.servezoneInterfaces.requests.version.IRequest_Any_Cloudly_VersionManager_GetLatestContainerVersion>(
|
||||
new plugins.typedrequest.TypedHandler(
|
||||
'getLatestContainerVersion',
|
||||
async (typedRequestData) => {
|
||||
const containerVersionGet: ContainerVersion =
|
||||
await ContainerVersion.getInstance<ContainerVersion>({
|
||||
dockerImageUrl: typedRequestData.dockerImageUrl,
|
||||
});
|
||||
return {
|
||||
dockerImageUrl: containerVersionGet.dockerImageUrl,
|
||||
dockerImageVersion: containerVersionGet.dockerImageVersion,
|
||||
};
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// update version
|
||||
this.typedRouter.addTypedHandler<plugins.servezoneInterfaces.requests.version.IRequest_Any_Cloudly_VersionManager_InformCloudlyAboutNewContainerVersion>(
|
||||
new plugins.typedrequest.TypedHandler(
|
||||
'informCloudlyAboutNewContainerVersion',
|
||||
async (dataArg) => {
|
||||
console.log(`Got a container version announcement! "${dataArg.dockerImageUrl}"`);
|
||||
let containerVersion: ContainerVersion =
|
||||
await ContainerVersion.getInstance<ContainerVersion>({
|
||||
dockerImageUrl: dataArg.dockerImageUrl,
|
||||
});
|
||||
|
||||
if (containerVersion) {
|
||||
containerVersion.dockerImageVersion = dataArg.dockerImageVersion;
|
||||
await containerVersion.save();
|
||||
} else {
|
||||
containerVersion = await ContainerVersion.fromIVersionData(dataArg);
|
||||
await containerVersion.save();
|
||||
}
|
||||
|
||||
// lets push this info to the relevant clusters
|
||||
const clusters = await this.cloudlyRef.clusterManager.getAllClusters();
|
||||
let foundServices: plugins.servezoneInterfaces.data.IService;
|
||||
let relevantClusterIdentifier: plugins.servezoneInterfaces.data.IClusterIdentifier;
|
||||
for (const clusterArg of clusters) {
|
||||
console.log(clusterArg);
|
||||
for (const serviceArg of await clusterArg.getServices()) {
|
||||
if (serviceArg.image === containerVersion.dockerImageUrl) {
|
||||
foundServices = serviceArg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (foundServices) {
|
||||
relevantClusterIdentifier = {
|
||||
clusterName: clusterArg.data.name,
|
||||
secretKey: clusterArg.data.secretKey,
|
||||
};
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!relevantClusterIdentifier) {
|
||||
console.log('no cluster found that needs to update');
|
||||
return {};
|
||||
} else {
|
||||
console.log('found relevant cluster identifier:');
|
||||
console.log(relevantClusterIdentifier);
|
||||
}
|
||||
|
||||
const targetConnection =
|
||||
await this.cloudlyRef.server.typedsocketServer.findTargetConnection(
|
||||
async (connectionArg) => {
|
||||
const identityTag = await connectionArg.getTagById('identity');
|
||||
if (!identityTag) {
|
||||
return false;
|
||||
}
|
||||
const result =
|
||||
plugins.smartjson.stringify(identityTag.payload) ===
|
||||
plugins.smartjson.stringify(relevantClusterIdentifier);
|
||||
return result;
|
||||
}
|
||||
);
|
||||
|
||||
if (targetConnection) {
|
||||
console.log(`the relevant cluster is connected and is now being informed.`);
|
||||
const informCoreflowTR =
|
||||
this.cloudlyRef.server.typedsocketServer.createTypedRequest<plugins.servezoneInterfaces.requests.version.IRequest_Cloudly_Coreflow_VersionManager_InformCoreflowAboutNewContainerVersion>(
|
||||
'informCoreflowAboutNewContainerVersion',
|
||||
targetConnection
|
||||
);
|
||||
informCoreflowTR.fire({
|
||||
dockerImageUrl: containerVersion.dockerImageUrl,
|
||||
dockerImageVersion: containerVersion.dockerImageVersion,
|
||||
});
|
||||
} else {
|
||||
console.log('the relevant cluster is not connected at this time.');
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
// lets support the servezone standard
|
||||
this.typedRouter.addTypedHandler(
|
||||
new plugins.typedrequest.TypedHandler<plugins.servezoneInterfaces.requests.IRequest_InformAboutNewContainerImage>(
|
||||
'servezonestandard_InformAboutNewContainerVersion',
|
||||
async (dataArg) => {
|
||||
const result =
|
||||
await this.typedRouter.routeAndAddResponse<plugins.servezoneInterfaces.requests.version.IRequest_Any_Cloudly_VersionManager_InformCloudlyAboutNewContainerVersion>(
|
||||
{
|
||||
method: 'informCloudlyAboutNewContainerVersion',
|
||||
request: {
|
||||
dockerImageUrl: dataArg.containerImageInfo.registryUrl,
|
||||
dockerImageVersion: dataArg.containerImageInfo.version,
|
||||
},
|
||||
response: null
|
||||
},
|
||||
true
|
||||
);
|
||||
return result.response;
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* gets all versions
|
||||
*/
|
||||
public async getAllVersions() {
|
||||
const result = await ContainerVersion.getInstances<ContainerVersion>({});
|
||||
return result;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user