Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
RunOnFluxBot committed Oct 13, 2024
1 parent 031f818 commit 3b06346
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 162 deletions.
248 changes: 155 additions & 93 deletions services/appsService.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion services/benchmarkService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ let response = messageHelper.createErrorMessage();
let benchdClient = null;

async function buildBenchdClient() {
const fluxbenchdPath = '/dat/usr/lib/fluxbenchd';
const homeDirPath = path.join(__dirname, '../../../../');
const fluxbenchdPath = path.join(homeDirPath, '.fluxbenchmark');

const exists = await fs.stat(fluxbenchdPath).catch(() => false);

Expand Down
18 changes: 7 additions & 11 deletions services/dockerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,15 +409,11 @@ async function dockerContainerLogs(idOrName, lines) {

async function dockerContainerLogsPolling(idOrName, lineCount, sinceTimestamp, callback) {
try {
console.log('Starting dockerContainerLogsPolling');
const dockerContainer = await getDockerContainerByIdOrName(idOrName);
console.log(`Retrieved container: ${idOrName}`);

const logStream = new stream.PassThrough();
let logBuffer = '';

logStream.on('data', (chunk) => {
console.log('Received chunk of data');
logBuffer += chunk.toString('utf8');
const lines = logBuffer.split('\n');
logBuffer = lines.pop();
Expand All @@ -432,14 +428,13 @@ async function dockerContainerLogsPolling(idOrName, lineCount, sinceTimestamp, c
});

logStream.on('error', (error) => {
console.error('Log stream encountered an error:', error);
log.error('Log stream encountered an error:', error);
if (callback) {
callback(error);
}
});

logStream.on('end', () => {
console.log('logStream ended');
if (callback) {
callback(null, 'Stream ended'); // Notify end of logs
}
Expand All @@ -460,7 +455,7 @@ async function dockerContainerLogsPolling(idOrName, lineCount, sinceTimestamp, c
// eslint-disable-next-line consistent-return
dockerContainer.logs(logOptions, (err, mystream) => {
if (err) {
console.error('Error fetching logs:', err);
log.error('Error fetching logs:', err);
if (callback) {
callback(err);
}
Expand All @@ -472,21 +467,20 @@ async function dockerContainerLogsPolling(idOrName, lineCount, sinceTimestamp, c
logStream.end();
}, 1500);
mystream.on('end', () => {
console.log('mystream ended');
logStream.end();
resolve();
});

mystream.on('error', (error) => {
console.error('Stream error:', error);
log.error('Stream error:', error);
logStream.end();
if (callback) {
callback(error);
}
reject(error);
});
} catch (error) {
console.error('Error during stream processing:', error);
log.error('Error during stream processing:', error);
if (callback) {
callback(new Error('An error occurred while processing the log stream'));
}
Expand All @@ -495,7 +489,7 @@ async function dockerContainerLogsPolling(idOrName, lineCount, sinceTimestamp, c
});
});
} catch (error) {
console.error('Error in dockerContainerLogsPolling:', error);
log.error('Error in dockerContainerLogsPolling:', error);
if (callback) {
callback(error);
}
Expand Down Expand Up @@ -663,6 +657,8 @@ async function appDockerCreate(appSpecifications, appName, isComponent, fullAppS
Tty: false,
ExposedPorts: exposedPorts,
HostConfig: {
CpuQuota: Math.round(appSpecifications.cpu * 100000),
CpuPeriod: 100000,
NanoCPUs: Math.round(appSpecifications.cpu * 1e9),
Memory: Math.round(appSpecifications.ram * 1024 * 1024),
MemorySwap: Math.round((appSpecifications.ram + (config.fluxapps.defaultSwap * 1000)) * 1024 * 1024), // default 2GB swap
Expand Down
4 changes: 2 additions & 2 deletions services/serviceManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ async function startFluxFunctions() {
appsService.checkApplicationsCpuUSage();
setInterval(() => {
appsService.checkApplicationsCpuUSage();
}, 60 * 60 * 1000);
}, 65 * 60 * 1000);
}, 15 * 60 * 1000);
}, 15 * 60 * 1000);
setTimeout(() => {
// appsService.checkForNonAllowedAppsOnLocalNetwork();
appsService.checkMyAppsAvailability(); // periodically checks
Expand Down
98 changes: 51 additions & 47 deletions services/syncthingService.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,39 +51,38 @@ const stc = new FluxController();
* @returns {Promise<(string | null)>} config gile (XML).
*/
async function getConfigFile() {
// const homedir = os.homedir();
// const configDir = path.join(homedir, '.config');
// const syncthingDir = path.join(configDir, 'syncthing');
// const configFile = path.join(syncthingDir, 'config.xml');
const configFile = '/dat/usr/lib/syncthing/config.xml';
const homedir = os.homedir();
const configDir = path.join(homedir, '.config');
const syncthingDir = path.join(configDir, 'syncthing');
const configFile = path.join(syncthingDir, 'config.xml');

// const user = os.userInfo().username;
// const owner = `${user}:${user}`;
const user = os.userInfo().username;
const owner = `${user}:${user}`;

// As syncthing is running as root, we need to change owenership to running user
// const { error: chownConfigDirError } = await serviceHelper.runCommand('chown', {
// runAsRoot: true,
// logError: false,
// params: [owner, configDir],
// });
const { error: chownConfigDirError } = await serviceHelper.runCommand('chown', {
runAsRoot: true,
logError: false,
params: [owner, configDir],
});

// if (chownConfigDirError) return null;
if (chownConfigDirError) return null;

// const { error: chownSyncthingError } = await serviceHelper.runCommand('chown', {
// runAsRoot: true,
// logError: false,
// params: [owner, syncthingDir],
// });
const { error: chownSyncthingError } = await serviceHelper.runCommand('chown', {
runAsRoot: true,
logError: false,
params: [owner, syncthingDir],
});

// if (chownSyncthingError) return null;
if (chownSyncthingError) return null;

// const { error: chmodError } = await serviceHelper.runCommand('chmod', {
// runAsRoot: true,
// logError: false,
// params: ['644', configFile],
// });
const { error: chmodError } = await serviceHelper.runCommand('chmod', {
runAsRoot: true,
logError: false,
params: ['644', configFile],
});

// if (chmodError) return null;
if (chmodError) return null;

let result = null;
// this should never reject as chown would error first but just in case
Expand Down Expand Up @@ -2495,11 +2494,10 @@ async function runSyncthingSentinel() {
log.error('Unable to get syncthing deviceId. Reconfiguring syncthing.');
await stopSyncthing();
await installSyncthingIdempotently();
// await configureDirectories();
await configureDirectories();

// const homedir = os.homedir();
// const syncthingHome = path.join(homedir, '.config/syncthing');
const syncthingHome = '/dat/usr/lib/syncthing';
const homedir = os.homedir();
const syncthingHome = path.join(homedir, '.config/syncthing');
const logFile = path.join(syncthingHome, 'syncthing.log');

if (stc.aborted) return 0;
Expand All @@ -2517,26 +2515,32 @@ async function runSyncthingSentinel() {
// adding old spawn with shell in the interim.

childProcess.spawn(
'nohup',
[
'syncthing',
'--logfile',
logFile,
'--logflags=3',
'--log-max-old-files=2',
'--log-max-size=26214400',
'--allow-newer-config',
'--no-browser',
'--home',
syncthingHome,
],
{
detached: true,
stdio: 'ignore',
env: { HOME: syncthingHome },
},
`sudo nohup syncthing --logfile ${logFile} --logflags=3 --log-max-old-files=2 --log-max-size=26214400 --allow-newer-config --no-browser --home ${syncthingHome} >/dev/null 2>&1 </dev/null &`,
{ shell: true },
).unref();

// childProcess.spawn(
// 'sudo',
// [
// 'nohup',
// 'syncthing',
// '--logfile',
// logFile,
// '--logflags=3',
// '--log-max-old-files=2',
// '--log-max-size=26214400',
// '--allow-newer-config',
// '--no-browser',
// '--home',
// syncthingHome,
// ],
// {
// detached: true,
// stdio: 'ignore',
// // uid: 0,
// },
// ).unref();

// let syncthing set itself up
await stc.sleep(5 * 1000);
}
Expand Down
6 changes: 1 addition & 5 deletions services/systemService.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,11 +521,7 @@ async function mongoDBConfig() {
log.info('MongoDB file config verification...');
try {
const hashCurrent = hash(await fs.readFile('/etc/mongod.conf'));
const vailidHashes = [
'cdf6ed6515ac061f5e6b6084af91767f42cd79d7',
'4646c649230b8125c7894d618313039f20d1901b',
'1b20cbacf63c4400d0bf90188615db78b9a7602e',
];
const vailidHashes = ['4646c649230b8125c7894d618313039f20d1901b', '1b20cbacf63c4400d0bf90188615db78b9a7602e'];
if (vailidHashes.indexOf(hashCurrent) !== -1) {
log.info('MongoDB config verification passed.');
return;
Expand Down
8 changes: 5 additions & 3 deletions services/utils/daemonConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const os = require('node:os');
class DaemonConfig {
static daemonConfigPaths = {
linux: {
zelPath: 'flux.conf',
fluxPath: 'flux.conf',
zelPath: '.zelcash/zelcash.conf',
fluxPath: '.flux/flux.conf',
},
darwin: {
zelPath: 'Zelcash/zelcash.conf',
Expand All @@ -24,14 +24,16 @@ class DaemonConfig {

platform = process.platform;

homedir = os.homedir();

raw = '';

configElements = {};

constructor() {
switch (this.platform) {
case 'linux':
this.baseDir = '/dat/var/lib/fluxd';
this.baseDir = os.homedir();
break;
case 'darwin':
this.baseDir = path.join(os.homedir(), 'Library/Application Support');
Expand Down

0 comments on commit 3b06346

Please sign in to comment.