Skip to content

Commit

Permalink
adds HOST_IP, HOST_ID and HOST_GEO to the docker containers as env. p…
Browse files Browse the repository at this point in the history
…arameters
  • Loading branch information
Cabecinha84 committed Nov 7, 2024
1 parent d7a6944 commit ba75780
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions ZelBack/src/services/dockerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const Docker = require('dockerode');
const path = require('path');
const serviceHelper = require('./serviceHelper');
const fluxCommunicationMessagesSender = require('./fluxCommunicationMessagesSender');
const geolocationService = require('./geolocationService');
const fluxNetworkHelper = require('./fluxNetworkHelper');
const generalService = require('./generalService');
const pgpService = require('./pgpService');
const deviceHelper = require('./deviceHelper');
const log = require('../lib/log');
Expand Down Expand Up @@ -639,6 +642,32 @@ async function appDockerCreate(appSpecifications, appName, isComponent, fullAppS
throw new Error('Environment parameters from Secrets are invalid - not an array');
}
}
const hostId = envParams.find((env) => env.startsWith('HOST_ID'));
if (!hostId) {
const collateral = await generalService.nodeCollateral().catch((error) => {
log.error(error);
});
if (collateral) {
envParams.push(`HOST_ID=${collateral}`);
}
}
const hostIp = envParams.find((env) => env.startsWith('HOST_IP'));
if (!hostIp) {
const myIP = await fluxNetworkHelper.getMyFluxIPandPort();
if (myIP) {
envParams.push(`HOST_IP=${myIP.split(':')[0]}`);
}
}
const hostGeo = envParams.find((env) => env.startsWith('HOST_GEO'));
if (!hostGeo) {
const myGeo = await geolocationService.getNodeGeolocation();
if (myGeo) {
delete myGeo.ip;
delete myGeo.org;
envParams.push(`HOST_GEO=${JSON.stringify(myGeo)}`);
}
}

const adjustedCommands = [];
appSpecifications.commands.forEach((command) => {
if (command !== '--privileged') {
Expand Down Expand Up @@ -777,6 +806,33 @@ async function appDockerUpdateCpu(idOrName, nanoCpus) {
}
}

/**
* Updates the Env Parameters of a Docker container.
*
* @param {string} idOrName - The ID or name of the Docker container.
* @param {array} envParams - new App Env. Parameters.
* @returns {Promise<string>} message
*/
async function appDockerUpdateEnv(idOrName, envParams) {
try {
// Get the Docker container by ID or name
const dockerContainer = await getDockerContainerByIdOrName(idOrName);

// Update the container's CPU resources
await dockerContainer.update({
Env: envParams,
});

// eslint-disable-next-line no-use-before-define
await appDockerRestart(idOrName);

return `Flux App ${idOrName} env parameters successfully updated with.`;
} catch (error) {
log.error(error);
throw new Error(`Failed to updateenv parameters for ${idOrName}: ${error.message}`);
}
}

/**
* Starts app's docker.
*
Expand Down Expand Up @@ -1124,6 +1180,7 @@ async function dockerLogsFix() {
module.exports = {
appDockerCreate,
appDockerUpdateCpu,
appDockerUpdateEnv,
appDockerImageRemove,
appDockerKill,
appDockerPause,
Expand Down

0 comments on commit ba75780

Please sign in to comment.