refactor: rename builders to server

This commit is contained in:
Mauricio Siu
2024-10-05 22:15:47 -06:00
parent 43555cdabe
commit f3ce69b656
361 changed files with 551 additions and 562 deletions

View File

@@ -0,0 +1,73 @@
import _ from "lodash";
import type {
ComposeSpecification,
DefinitionsConfig,
DefinitionsService,
} from "../types";
export const addSuffixToConfigsRoot = (
configs: { [key: string]: DefinitionsConfig },
suffix: string,
): { [key: string]: DefinitionsConfig } => {
const newConfigs: { [key: string]: DefinitionsConfig } = {};
_.forEach(configs, (config, configName) => {
const newConfigName = `${configName}-${suffix}`;
newConfigs[newConfigName] = _.cloneDeep(config);
});
return newConfigs;
};
export const addSuffixToConfigsInServices = (
services: { [key: string]: DefinitionsService },
suffix: string,
): { [key: string]: DefinitionsService } => {
const newServices: { [key: string]: DefinitionsService } = {};
_.forEach(services, (serviceConfig, serviceName) => {
const newServiceConfig = _.cloneDeep(serviceConfig);
// Reemplazar nombres de configs en configs
if (_.has(newServiceConfig, "configs")) {
newServiceConfig.configs = _.map(newServiceConfig.configs, (config) => {
if (_.isString(config)) {
return `${config}-${suffix}`;
}
if (_.isObject(config) && config.source) {
return {
...config,
source: `${config.source}-${suffix}`,
};
}
return config;
});
}
newServices[serviceName] = newServiceConfig;
});
return newServices;
};
export const addSuffixToAllConfigs = (
composeData: ComposeSpecification,
suffix: string,
): ComposeSpecification => {
const updatedComposeData = { ...composeData };
if (composeData?.configs) {
updatedComposeData.configs = addSuffixToConfigsRoot(
composeData.configs,
suffix,
);
}
if (composeData?.services) {
updatedComposeData.services = addSuffixToConfigsInServices(
composeData.services,
suffix,
);
}
return updatedComposeData;
};

View File

@@ -0,0 +1,83 @@
import _ from "lodash";
import type {
ComposeSpecification,
DefinitionsNetwork,
DefinitionsService,
} from "../types";
export const addSuffixToNetworksRoot = (
networks: { [key: string]: DefinitionsNetwork },
suffix: string,
): { [key: string]: DefinitionsNetwork } => {
return _.mapKeys(networks, (_value, key) => {
if (key === "dokploy-network") {
return "dokploy-network";
}
return `${key}-${suffix}`;
});
};
export const addSuffixToServiceNetworks = (
services: { [key: string]: DefinitionsService },
suffix: string,
): { [key: string]: DefinitionsService } => {
return _.mapValues(services, (service) => {
if (service.networks) {
// 1 Case the most common
if (Array.isArray(service.networks)) {
service.networks = service.networks.map((network: string) => {
if (network === "dokploy-network") {
return "dokploy-network";
}
return `${network}-${suffix}`;
});
} else {
// 2 Case
service.networks = _.mapKeys(service.networks, (_value, key) => {
if (key === "dokploy-network") {
return "dokploy-network";
}
return `${key}-${suffix}`;
});
// 3 Case
service.networks = _.mapValues(service.networks, (value) => {
if (value && typeof value === "object") {
return _.mapKeys(value, (_val, innerKey) => {
if (innerKey === "aliases") {
return "aliases";
}
return `${innerKey}-${suffix}`;
});
}
return value;
});
}
}
return service;
});
};
export const addSuffixToAllNetworks = (
composeData: ComposeSpecification,
suffix: string,
): ComposeSpecification => {
const updatedComposeData = { ...composeData };
if (updatedComposeData.networks) {
updatedComposeData.networks = addSuffixToNetworksRoot(
updatedComposeData.networks,
suffix,
);
}
if (updatedComposeData.services) {
updatedComposeData.services = addSuffixToServiceNetworks(
updatedComposeData.services,
suffix,
);
}
return updatedComposeData;
};

View File

@@ -0,0 +1,68 @@
import _ from "lodash";
import type { ComposeSpecification, DefinitionsService } from "../types";
export const addSuffixToSecretsRoot = (
secrets: ComposeSpecification["secrets"],
suffix: string,
): ComposeSpecification["secrets"] => {
const newSecrets: ComposeSpecification["secrets"] = {};
_.forEach(secrets, (secretConfig, secretName) => {
const newSecretName = `${secretName}-${suffix}`;
newSecrets[newSecretName] = _.cloneDeep(secretConfig);
});
return newSecrets;
};
export const addSuffixToSecretsInServices = (
services: { [key: string]: DefinitionsService },
suffix: string,
): { [key: string]: DefinitionsService } => {
const newServices: { [key: string]: DefinitionsService } = {};
_.forEach(services, (serviceConfig, serviceName) => {
const newServiceConfig = _.cloneDeep(serviceConfig);
// Replace secret names in secrets
if (_.has(newServiceConfig, "secrets")) {
newServiceConfig.secrets = _.map(newServiceConfig.secrets, (secret) => {
if (_.isString(secret)) {
return `${secret}-${suffix}`;
}
if (_.isObject(secret) && secret.source) {
return {
...secret,
source: `${secret.source}-${suffix}`,
};
}
return secret;
});
}
newServices[serviceName] = newServiceConfig;
});
return newServices;
};
export const addSuffixToAllSecrets = (
composeData: ComposeSpecification,
suffix: string,
): ComposeSpecification => {
const updatedComposeData = { ...composeData };
if (composeData?.secrets) {
updatedComposeData.secrets = addSuffixToSecretsRoot(
composeData.secrets,
suffix,
);
}
if (composeData?.services) {
updatedComposeData.services = addSuffixToSecretsInServices(
composeData.services,
suffix,
);
}
return updatedComposeData;
};

View File

@@ -0,0 +1,90 @@
// En la sección depends_on de otros servicios: Para definir dependencias entre servicios.
// En la sección networks de otros servicios: Aunque esto no es común, es posible referenciar servicios en redes personalizadas.
// En la sección volumes_from de otros servicios: Para reutilizar volúmenes definidos por otro servicio.
// En la sección links de otros servicios: Para crear enlaces entre servicios.
// En la sección extends de otros servicios: Para extender la configuración de otro servicio.
import _ from "lodash";
import type { ComposeSpecification, DefinitionsService } from "../types";
type DependsOnObject = NonNullable<
Exclude<DefinitionsService["depends_on"], string[]> extends infer T
? { [K in keyof T]: T[K] }
: never
>;
export const addSuffixToServiceNames = (
services: { [key: string]: DefinitionsService },
suffix: string,
): { [key: string]: DefinitionsService } => {
const newServices: { [key: string]: DefinitionsService } = {};
for (const [serviceName, serviceConfig] of Object.entries(services)) {
const newServiceName = `${serviceName}-${suffix}`;
const newServiceConfig = _.cloneDeep(serviceConfig);
// Reemplazar nombres de servicios en depends_on
if (newServiceConfig.depends_on) {
if (Array.isArray(newServiceConfig.depends_on)) {
newServiceConfig.depends_on = newServiceConfig.depends_on.map(
(dep) => `${dep}-${suffix}`,
);
} else {
const newDependsOn: DependsOnObject = {};
for (const [depName, depConfig] of Object.entries(
newServiceConfig.depends_on,
)) {
newDependsOn[`${depName}-${suffix}`] = depConfig;
}
newServiceConfig.depends_on = newDependsOn;
}
}
// Reemplazar nombre en container_name
if (newServiceConfig.container_name) {
newServiceConfig.container_name = `${newServiceConfig.container_name}-${suffix}`;
}
// Reemplazar nombres de servicios en links
if (newServiceConfig.links) {
newServiceConfig.links = newServiceConfig.links.map(
(link) => `${link}-${suffix}`,
);
}
// Reemplazar nombres de servicios en extends
if (newServiceConfig.extends) {
if (typeof newServiceConfig.extends === "string") {
newServiceConfig.extends = `${newServiceConfig.extends}-${suffix}`;
} else {
newServiceConfig.extends.service = `${newServiceConfig.extends.service}-${suffix}`;
}
}
// Reemplazar nombres de servicios en volumes_from
if (newServiceConfig.volumes_from) {
newServiceConfig.volumes_from = newServiceConfig.volumes_from.map(
(vol) => `${vol}-${suffix}`,
);
}
newServices[newServiceName] = newServiceConfig;
}
return newServices;
};
export const addSuffixToAllServiceNames = (
composeData: ComposeSpecification,
suffix: string,
): ComposeSpecification => {
const updatedComposeData = { ...composeData };
if (updatedComposeData.services) {
updatedComposeData.services = addSuffixToServiceNames(
updatedComposeData.services,
suffix,
);
}
return updatedComposeData;
};

View File

@@ -0,0 +1,78 @@
import _ from "lodash";
import type {
ComposeSpecification,
DefinitionsService,
DefinitionsVolume,
} from "../types";
// Función para agregar prefijo a volúmenes
export const addSuffixToVolumesRoot = (
volumes: { [key: string]: DefinitionsVolume },
suffix: string,
): { [key: string]: DefinitionsVolume } => {
return _.mapKeys(volumes, (_value, key) => `${key}-${suffix}`);
};
export const addSuffixToVolumesInServices = (
services: { [key: string]: DefinitionsService },
suffix: string,
): { [key: string]: DefinitionsService } => {
const newServices: { [key: string]: DefinitionsService } = {};
_.forEach(services, (serviceConfig, serviceName) => {
const newServiceConfig = _.cloneDeep(serviceConfig);
// Reemplazar nombres de volúmenes en volumes
if (_.has(newServiceConfig, "volumes")) {
newServiceConfig.volumes = _.map(newServiceConfig.volumes, (volume) => {
if (_.isString(volume)) {
const [volumeName, path] = volume.split(":");
// skip bind mounts and variables (e.g. $PWD)
if (
volumeName?.startsWith(".") ||
volumeName?.startsWith("/") ||
volumeName?.startsWith("$")
) {
return volume;
}
return `${volumeName}-${suffix}:${path}`;
}
if (_.isObject(volume) && volume.type === "volume" && volume.source) {
return {
...volume,
source: `${volume.source}-${suffix}`,
};
}
return volume;
});
}
newServices[serviceName] = newServiceConfig;
});
return newServices;
};
export const addSuffixToAllVolumes = (
composeData: ComposeSpecification,
suffix: string,
): ComposeSpecification => {
const updatedComposeData = { ...composeData };
if (updatedComposeData.volumes) {
updatedComposeData.volumes = addSuffixToVolumesRoot(
updatedComposeData.volumes,
suffix,
);
}
if (updatedComposeData.services) {
updatedComposeData.services = addSuffixToVolumesInServices(
updatedComposeData.services,
suffix,
);
}
return updatedComposeData;
};