fix(core): Refactored configuration management classes and improved service update handling
This commit is contained in:
@@ -3,10 +3,26 @@ import * as paths from './spark.paths.js';
|
||||
import { Spark } from './spark.classes.spark.js';
|
||||
import { logger } from './spark.logging.js';
|
||||
|
||||
export class SparkUpdateManager {
|
||||
/**
|
||||
* this class takes care of updating the services that are managed by spark
|
||||
*/
|
||||
export class SparkServicesManager {
|
||||
public sparkRef: Spark;
|
||||
public dockerHost: plugins.docker.DockerHost;
|
||||
public smartupdate: plugins.smartupdate.SmartUpdate;
|
||||
|
||||
/**
|
||||
* the services that are managed by spark
|
||||
*/
|
||||
services: Array<{
|
||||
name: string;
|
||||
image: string;
|
||||
url: string;
|
||||
port: string;
|
||||
environment: string;
|
||||
secretJson: any;
|
||||
}> = [];
|
||||
|
||||
constructor(sparkrefArg: Spark) {
|
||||
this.sparkRef = sparkrefArg;
|
||||
this.dockerHost = new plugins.docker.DockerHost({});
|
||||
@@ -21,109 +37,58 @@ export class SparkUpdateManager {
|
||||
}
|
||||
|
||||
public async updateServices() {
|
||||
if (
|
||||
plugins.smartfile.fs.isDirectory(plugins.path.join(paths.homeDir, 'serve.zone/spark')) &&
|
||||
(await plugins.smartfile.fs.fileExists(
|
||||
plugins.path.join(paths.homeDir, 'serve.zone/spark/spark.json')
|
||||
))
|
||||
) {
|
||||
const services: Array<{
|
||||
name: string;
|
||||
image: string;
|
||||
url: string;
|
||||
port: string;
|
||||
environment: string;
|
||||
secretJson: any;
|
||||
}> = [];
|
||||
// lets add coreflow
|
||||
services.push({
|
||||
name: `coreflow`,
|
||||
image: `code.foss.global/serve.zone/coreflow`,
|
||||
url: `coreflow`,
|
||||
environment: `production`,
|
||||
port: `3000`,
|
||||
secretJson: {
|
||||
SERVEZONE_PORT: `3000`,
|
||||
SERVEZONE_ENVIRONMENT: `production`,
|
||||
},
|
||||
});
|
||||
|
||||
services.push({
|
||||
name: `coretraffic`,
|
||||
image: `code.foss.global/serve.zone/coretraffic`,
|
||||
url: `coreflow`,
|
||||
environment: `production`,
|
||||
port: `3000`,
|
||||
secretJson: {
|
||||
SERVEZONE_PORT: `3000`,
|
||||
SERVEZONE_ENVIRONMENT: `production`,
|
||||
},
|
||||
});
|
||||
|
||||
services.push({
|
||||
name: `corelog`,
|
||||
image: `code.foss.global/serve.zone/corelog`,
|
||||
url: `coreflow`,
|
||||
environment: `production`,
|
||||
port: `3000`,
|
||||
secretJson: {
|
||||
SERVEZONE_PORT: `3000`,
|
||||
SERVEZONE_ENVIRONMENT: `production`,
|
||||
},
|
||||
});
|
||||
|
||||
// lets add coretraffic
|
||||
|
||||
for (const service of 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', `not needing update.`);
|
||||
// we simply return here to end the functions
|
||||
return;
|
||||
}
|
||||
logger.log('ok', `${service.name} needs to be updated!`);
|
||||
await existingService.remove();
|
||||
await existingServiceSecret.remove();
|
||||
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;
|
||||
}
|
||||
if (!existingService && existingServiceSecret) {
|
||||
await existingServiceSecret.remove();
|
||||
}
|
||||
|
||||
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: {},
|
||||
});
|
||||
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}`],
|
||||
});
|
||||
logger.log('ok', `updated service >>${newService.Spec.Name}<<!`);
|
||||
// continuing here means we need to update the service
|
||||
logger.log('ok', `${service.name} needs to be updated!`);
|
||||
await existingService.remove();
|
||||
await existingServiceSecret.remove();
|
||||
}
|
||||
logger.log('success', `updated ${services.length} services!`);
|
||||
if (!existingService && existingServiceSecret) {
|
||||
await existingServiceSecret.remove();
|
||||
}
|
||||
|
||||
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: {},
|
||||
});
|
||||
|
||||
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}`],
|
||||
});
|
||||
logger.log('ok', `updated service >>${newService.Spec.Name}<<!`);
|
||||
}
|
||||
|
||||
logger.log('success', `updated ${this.services.length} services!`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user