2024-05-08 18:49:10 +00:00
|
|
|
import * as plugins from './spark.plugins.js';
|
|
|
|
import * as paths from './spark.paths.js';
|
|
|
|
import { Spark } from './spark.classes.spark.js';
|
|
|
|
import { logger } from './spark.logging.js';
|
|
|
|
|
2024-12-20 00:29:21 +00:00
|
|
|
/**
|
|
|
|
* this class takes care of updating the services that are managed by spark
|
|
|
|
*/
|
|
|
|
export class SparkServicesManager {
|
2024-05-08 18:49:10 +00:00
|
|
|
public sparkRef: Spark;
|
|
|
|
public dockerHost: plugins.docker.DockerHost;
|
|
|
|
public smartupdate: plugins.smartupdate.SmartUpdate;
|
2024-12-20 00:29:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the services that are managed by spark
|
|
|
|
*/
|
|
|
|
services: Array<{
|
|
|
|
name: string;
|
|
|
|
image: string;
|
|
|
|
url: string;
|
|
|
|
port: string;
|
|
|
|
environment: string;
|
|
|
|
secretJson: any;
|
|
|
|
}> = [];
|
|
|
|
|
2024-05-08 18:49:10 +00:00
|
|
|
constructor(sparkrefArg: Spark) {
|
|
|
|
this.sparkRef = sparkrefArg;
|
2024-06-13 13:12:07 +00:00
|
|
|
this.dockerHost = new plugins.docker.DockerHost({});
|
2024-05-08 18:49:10 +00:00
|
|
|
this.smartupdate = new plugins.smartupdate.SmartUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* start the instance
|
|
|
|
*/
|
|
|
|
public async start() {
|
|
|
|
await this.dockerHost.activateSwarm();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async updateServices() {
|
2024-12-20 00:29:21 +00:00
|
|
|
for (const service of this.services) {
|
|
|
|
const existingService = await plugins.docker.DockerService.getServiceByName(
|
|
|
|
this.dockerHost,
|
|
|
|
service.name
|
|
|
|
);
|
|
|
|
const existingServiceSecret = await plugins.docker.DockerSecret.getSecretByName(
|
|
|
|
this.dockerHost,
|
|
|
|
`${service.name}Secret`
|
|
|
|
);
|
|
|
|
if (existingService) {
|
|
|
|
const needsUpdate: boolean = await existingService.needsUpdate();
|
|
|
|
if (!needsUpdate) {
|
|
|
|
logger.log('info', `service >>${service.name}<< not needing update.`);
|
|
|
|
// we simply return here to end the functions
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// continuing here means we need to update the service
|
|
|
|
logger.log('ok', `${service.name} needs to be updated!`);
|
|
|
|
await existingService.remove();
|
|
|
|
await existingServiceSecret.remove();
|
|
|
|
}
|
|
|
|
if (!existingService && existingServiceSecret) {
|
|
|
|
await existingServiceSecret.remove();
|
|
|
|
}
|
2024-06-13 13:12:07 +00:00
|
|
|
|
2024-12-20 00:29:21 +00:00
|
|
|
const newServiceImage = await plugins.docker.DockerImage.createFromRegistry(
|
|
|
|
this.dockerHost,
|
|
|
|
{
|
|
|
|
creationObject: {
|
|
|
|
imageUrl: service.image,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
const newServiceSecret = await plugins.docker.DockerSecret.createSecret(this.dockerHost, {
|
|
|
|
name: `${service.name}Secret`,
|
|
|
|
contentArg: plugins.smartjson.stringify(service.secretJson),
|
|
|
|
version: await newServiceImage.getVersion(),
|
|
|
|
labels: {},
|
2024-06-13 13:12:07 +00:00
|
|
|
});
|
|
|
|
|
2024-12-20 00:29:21 +00:00
|
|
|
const newService = await plugins.docker.DockerService.createService(this.dockerHost, {
|
|
|
|
image: newServiceImage,
|
|
|
|
labels: {},
|
|
|
|
name: service.name,
|
|
|
|
networkAlias: service.name,
|
|
|
|
networks: [],
|
|
|
|
secrets: [newServiceSecret],
|
|
|
|
ports: [`${service.port}:${service.secretJson.SERVEZONE_PORT}`],
|
2024-06-13 13:12:07 +00:00
|
|
|
});
|
2024-12-20 00:29:21 +00:00
|
|
|
logger.log('ok', `updated service >>${newService.Spec.Name}<<!`);
|
2024-05-08 18:49:10 +00:00
|
|
|
}
|
2024-12-20 00:29:21 +00:00
|
|
|
|
|
|
|
logger.log('success', `updated ${this.services.length} services!`);
|
2024-05-08 18:49:10 +00:00
|
|
|
}
|
|
|
|
}
|