Skip to content

Commit

Permalink
config reload [POC] (#1136)
Browse files Browse the repository at this point in the history
* added configReload

- interval check 2s

* Update appsService.js

* disabled userconfig local variable

* disabled userconfig local variable

* Update pgpService.js

* Update idService.js

* disabled userconfig local variable

* Update fluxNetworkHelper.js

* Update serviceManager.js

* Update syncthingService.js

* disabled userconfig local variable

* disabled userconfig local variable

* disabled userconfig local variable

* Update daemonrpcClient.js

* improved loading of ui call

* fix typo

* fix problem with button in new line

* added configReload

* added md5

* added ignore config

* added md5

* disabled old variable

* fix homeui build

* fix eslint

* fix problem with userconfig diuring ui build

* added eslint-disable-line

* improved

- used hash insted  of md5
- used log.info and log.error insted console.log

* improved configReaload

* removed md5

* added restartFluxOS

* added logs and delay 5s

* added authorized

* added restartFluxOS

* added restartFluxOS

* added FluxOS restart menu

* added gobal and undef tag

* Update apiServer.js

* fix build

* fix build

* fix build

* Update benchmarkService.js

* Update fluxNetworkHelper.js

* fix build

* Update idService.js

* Update pgpService.js

* Update upnpService.js

* Update daemonrpcClient.js

* Update serviceManager.js

* Update syncthingService.js

* removed md5

* added global variable

* Update appsService.js

* added global variable

* added global variable

* fix eslint

* fix eslint

* add comment

* Minor edit

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* fix eslint

* Updated message descriptions
  • Loading branch information
XK4MiLX authored Oct 29, 2023
1 parent cfd7fd8 commit 326dc5f
Show file tree
Hide file tree
Showing 23 changed files with 302 additions and 222 deletions.
305 changes: 115 additions & 190 deletions HomeUI/src/services/AppsService.js

Large diffs are not rendered by default.

41 changes: 40 additions & 1 deletion HomeUI/src/views/fluxadmin/ManageFlux.vue
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@
</b-button>
<confirm-dialog
target="update-blockedRepositories"
confirm-button="Update Blocked Repositories"
confirm-button="Update Repositories"
@confirm="adjustBlockedRepositories()"
/>
</div>
Expand Down Expand Up @@ -626,6 +626,32 @@
</div>
</b-card>
</b-col>
<b-col
xs="12"
lg="4"
>
<b-card title="FluxOS">
<b-card-text>
The restart button in FluxOS allows you to restart the application. Clicking it will close and immediately restart the application, but please note that this action requires refreshing the page and logging out and logging in again.
</b-card-text>
<div class="text-center">
<b-button
id="restart-fluxos"
v-ripple.400="'rgba(255, 255, 255, 0.15)'"
variant="success"
aria-label="Restart FluxOS"
class="mt-2"
>
Restart FluxOS
</b-button>
<confirm-dialog
target="restart-fluxos"
confirm-button="Restart"
@confirm="restartFluxOS()"
/>
</div>
</b-card>
</b-col>
</b-row>
</div>
</template>
Expand Down Expand Up @@ -713,6 +739,19 @@ export default {
this.getBlockedRepositories();
},
methods: {
async restartFluxOS() {
const zelidauth = localStorage.getItem('zelidauth');
try {
const response = await FluxService.restartFluxOS(zelidauth);
if (response.data.status === 'error') {
this.showToast('danger', response.data.data.message || response.data.data);
} else {
this.showToast('success', response.data.data.message || response.data.data);
}
} catch (error) {
this.showToast('danger', error.message || error);
}
},
async getKadenaAccount() {
const response = await FluxService.getKadenaAccount();
if (response.data.status === 'success' && response.data.data) {
Expand Down
4 changes: 2 additions & 2 deletions ZelBack/config/default.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const userconfig = require('../../config/userconfig');
let userconfig = require('../../config/userconfig'); // eslint-disable-line

const isDevelopment = userconfig.initial.development || false;
const isDevelopment = userconfig.initial.development || false; // eslint-disable-line

module.exports = {
development: isDevelopment,
Expand Down
3 changes: 3 additions & 0 deletions ZelBack/src/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ module.exports = (app, expressWs) => {
app.get('/flux/marketplaceurl', cache('1 day'), (req, res) => {
fluxService.getMarketplaceURL(req, res);
});
app.get('/flux/restart', cache('30 seconds'), (req, res) => {
fluxService.restartFluxOS(req, res);
});
app.get('/flux/dosstate', cache('30 seconds'), (req, res) => {
fluxNetworkHelper.getDOSState(req, res);
});
Expand Down
3 changes: 2 additions & 1 deletion ZelBack/src/services/appsService.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global userconfig */
const config = require('config');
const axios = require('axios');
const express = require('express');
Expand Down Expand Up @@ -33,7 +34,7 @@ const syncthingService = require('./syncthingService');
const pgpService = require('./pgpService');
const signatureVerifier = require('./signatureVerifier');
const log = require('../lib/log');
const userconfig = require('../../../config/userconfig');

const { invalidMessages } = require('./invalidMessages');

const fluxDirPath = path.join(__dirname, '../../../');
Expand Down
2 changes: 1 addition & 1 deletion ZelBack/src/services/benchmarkService.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global userconfig */
const benchmarkrpc = require('daemonrpc');
const config = require('config');
const path = require('path');
Expand All @@ -8,7 +9,6 @@ const verificationHelper = require('./verificationHelper');
const generalService = require('./generalService');
const upnpService = require('./upnpService');
const log = require('../lib/log');
const userconfig = require('../../../config/userconfig');

const isTestnet = userconfig.initial.testnet;

Expand Down
3 changes: 1 addition & 2 deletions ZelBack/src/services/daemonService/daemonServiceMiscRpcs.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* global userconfig */
const messageHelper = require('../messageHelper');
const daemonServiceUtils = require('./daemonServiceUtils');
const daemonServiceBlockchainRpcs = require('./daemonServiceBlockchainRpcs');

const log = require('../../lib/log');
const userconfig = require('../../../../config/userconfig');

const isTestnet = userconfig.initial.testnet;

Expand Down
2 changes: 1 addition & 1 deletion ZelBack/src/services/dockerService.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ async function dockerContainerExec(container, cmd, env, res, callback) {
callback(err);
}
mystream.on('data', (data) => {
resulttString = serviceHelper.dockerBufferToString(data);
resultString = serviceHelper.dockerBufferToString(data);
res.write(resultString);
});
mystream.on('end', () => callback(null));
Expand Down
2 changes: 1 addition & 1 deletion ZelBack/src/services/fluxNetworkHelper.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global userconfig */
/* eslint-disable no-underscore-dangle */
const config = require('config');
const zeltrezjs = require('zeltrezjs');
Expand All @@ -18,7 +19,6 @@ const daemonServiceWalletRpcs = require('./daemonService/daemonServiceWalletRpcs
const benchmarkService = require('./benchmarkService');
const verificationHelper = require('./verificationHelper');
const fluxCommunicationUtils = require('./fluxCommunicationUtils');
const userconfig = require('../../../config/userconfig');
const {
outgoingConnections, outgoingPeers, incomingPeers, incomingConnections,
} = require('./utils/establishedConnections');
Expand Down
40 changes: 34 additions & 6 deletions ZelBack/src/services/fluxService.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global userconfig */
const nodecmd = require('node-cmd');
const path = require('path');
const config = require('config');
Expand All @@ -23,7 +24,6 @@ const explorerService = require('./explorerService');
const fluxCommunication = require('./fluxCommunication');
const fluxNetworkHelper = require('./fluxNetworkHelper');
const geolocationService = require('./geolocationService');
const userconfig = require('../../../config/userconfig');

/**
* To show the directory on the node machine where FluxOS files are stored.
Expand Down Expand Up @@ -1124,7 +1124,7 @@ async function adjustKadenaAccount(req, res) {

await fsPromises.writeFile(fluxDirPath, dataToWrite);

const successMessage = messageHelper.createSuccessMessage('Kadena account adjusted, FluxOs is restarting');
const successMessage = messageHelper.createSuccessMessage('Kadena account adjusted');
res.json(successMessage);
} else {
const errMessage = messageHelper.errUnauthorizedMessage();
Expand Down Expand Up @@ -1167,7 +1167,7 @@ async function adjustRouterIP(req, res) {
const fluxDirPath = path.join(__dirname, '../../../config/userconfig.js');
await fsPromises.writeFile(fluxDirPath, dataToWrite);

const successMessage = messageHelper.createSuccessMessage('Router IP adjusted, FluxOs is restarting');
const successMessage = messageHelper.createSuccessMessage('Router IP adjusted');
res.json(successMessage);
} else {
const errMessage = messageHelper.errUnauthorizedMessage();
Expand Down Expand Up @@ -1220,7 +1220,7 @@ async function adjustBlockedPorts(req, res) {
}`;
const fluxDirPath = path.join(__dirname, '../../../config/userconfig.js');
await fsPromises.writeFile(fluxDirPath, dataToWrite);
const successMessage = messageHelper.createSuccessMessage('User Blocked Ports adjusted, FluxOs is restarting');
const successMessage = messageHelper.createSuccessMessage('User Blocked Ports adjusted');
res.json(successMessage);
});
} else {
Expand Down Expand Up @@ -1275,7 +1275,7 @@ async function adjustAPIPort(req, res) {
const fluxDirPath = path.join(__dirname, '../../../config/userconfig.js');
await fsPromises.writeFile(fluxDirPath, dataToWrite);

const successMessage = messageHelper.createSuccessMessage('API Port adjusted, FluxOs is restarting');
const successMessage = messageHelper.createSuccessMessage('API Port adjusted. A restart of FluxOS is necessary');
res.json(successMessage);
} else {
const errMessage = messageHelper.errUnauthorizedMessage();
Expand Down Expand Up @@ -1335,7 +1335,7 @@ async function adjustBlockedRepositories(req, res) {
}`;
const fluxDirPath = path.join(__dirname, '../../../config/userconfig.js');
await fsPromises.writeFile(fluxDirPath, dataToWrite);
const successMessage = messageHelper.createSuccessMessage('User Blocked Repositories adjusted, FluxOs is restarting');
const successMessage = messageHelper.createSuccessMessage('User Blocked Repositories adjusted');
res.json(successMessage);
});
} else {
Expand Down Expand Up @@ -1402,6 +1402,33 @@ async function installFluxWatchTower() {
}
}

/**
* Restart FluxOS via nodemon (executes the command `touch ` on package.json).
* @param {object} req Request.
* @param {object} res Response.
*/
async function restartFluxOS(req, res) {
try {
const authorized = await verificationHelper.verifyPrivilege('admin', req);
if (authorized === true) {
const nodedpath = path.join(__dirname, '../../../package.json');
const exec = `touch ${nodedpath}`;
const cmdAsync = util.promisify(nodecmd.get);
await cmdAsync(exec);
log.info('Restarting FluxOS..');
const response = messageHelper.createDataMessage('Restarting FluxOS');
res.json(response);
} else {
const errMessage = messageHelper.errUnauthorizedMessage();
res.json(errMessage);
}
} catch (error) {
log.error(error);
const errMessage = messageHelper.createErrorMessage(error.message, error.name, error.code);
res.json(errMessage);
}
}

module.exports = {
startDaemon,
updateFlux,
Expand Down Expand Up @@ -1453,6 +1480,7 @@ module.exports = {
getAPIPort,
getBlockedRepositories,
getMarketplaceURL,
restartFluxOS,

// Exports for testing purposes
fluxLog,
Expand Down
3 changes: 1 addition & 2 deletions ZelBack/src/services/idService.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* global userconfig */
const config = require('config');
const qs = require('qs');
const os = require('os');

const userconfig = require('../../../config/userconfig');
const log = require('../lib/log');
const serviceHelper = require('./serviceHelper');
const messageHelper = require('./messageHelper');
Expand Down
4 changes: 1 addition & 3 deletions ZelBack/src/services/pgpService.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/* global userconfig */
const config = require('config');
const path = require('path');
const fs = require('fs').promises;
const openpgp = require('openpgp');

const generalService = require('./generalService');

const log = require('../lib/log');
const userconfig = require('../../../config/userconfig');

/**
* To adjust PGP identity
Expand Down
2 changes: 1 addition & 1 deletion ZelBack/src/services/serviceManager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global userconfig */
const config = require('config');
const log = require('../lib/log');

Expand All @@ -12,7 +13,6 @@ const geolocationService = require('./geolocationService');
const upnpService = require('./upnpService');
const syncthingService = require('./syncthingService');
const pgpService = require('./pgpService');
const userconfig = require('../../../config/userconfig');

const apiPort = userconfig.initial.apiport || config.server.apiport;
const development = userconfig.initial.development || false;
Expand Down
2 changes: 1 addition & 1 deletion ZelBack/src/services/syncthingService.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global userconfig */
const config = require('config');
const nodecmd = require('node-cmd');
const axios = require('axios');
Expand All @@ -16,7 +17,6 @@ const fsPromises = fs.promises;
const messageHelper = require('./messageHelper');
const serviceHelper = require('./serviceHelper');
const log = require('../lib/log');
const userconfig = require('../../../config/userconfig');

const syncthingURL = `http://${config.syncthing.ip}:${config.syncthing.port}`;

Expand Down
2 changes: 1 addition & 1 deletion ZelBack/src/services/upnpService.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global userconfig */
const config = require('config');
const natUpnp = require('@runonflux/nat-upnp');
const serviceHelper = require('./serviceHelper');
Expand All @@ -8,7 +9,6 @@ const nodecmd = require('node-cmd');
const util = require('util');

const log = require('../lib/log');
const userconfig = require('../../../config/userconfig');

const client = new natUpnp.Client();

Expand Down
2 changes: 1 addition & 1 deletion ZelBack/src/services/utils/daemonrpcClient.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global userconfig */
const daemonrpc = require('daemonrpc');
const fullnode = require('fullnode');
const config = require('config');
const userconfig = require('../../../../config/userconfig');

const fnconfig = new fullnode.Config();
const isTestnet = userconfig.initial.testnet;
Expand Down
2 changes: 1 addition & 1 deletion ZelBack/src/services/verificationHelperUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global userconfig */
/**
* @module
* Contains utility functions to be used only by verificationHelper.
Expand All @@ -8,7 +9,6 @@ const config = require('config');
const signatureVerifier = require('./signatureVerifier');
const serviceHelper = require('./serviceHelper');
const dbHelper = require('./dbHelper');
const userconfig = require('../../../config/userconfig');

/**
* Verifies admin session
Expand Down
43 changes: 40 additions & 3 deletions apiServer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Flux configuration
/* global userconfig */
global.userconfig = require('./config/userconfig');

process.env.NODE_CONFIG_DIR = `${__dirname}/ZelBack/config/`;
// Flux configuration
const config = require('config');
const fs = require('fs');
const https = require('https');
Expand All @@ -10,12 +13,46 @@ const app = require('./ZelBack/src/lib/server');
const log = require('./ZelBack/src/lib/log');
const serviceManager = require('./ZelBack/src/services/serviceManager');
const upnpService = require('./ZelBack/src/services/upnpService');
const userconfig = require('./config/userconfig');
const hash = require('object-hash');
const { watch } = require("fs/promises");

const cmdAsync = util.promisify(nodecmd.get);

const apiPort = userconfig.initial.apiport || config.server.apiport;
const apiPortHttps = apiPort + 1;
let hashPrevious = null;
let initialHash = null;

async function configReload() {
try {
const watcher = watch(path.join(__dirname, '/config'));
// eslint-disable-next-line
for await (const event of watcher) {
if (event.eventType === 'change' && event.filename === 'userconfig.js') {
const hashCurrent = hash(fs.readFileSync(path.join(__dirname, '/config/userconfig.js')));
if (hashCurrent === hashPrevious) {
return;
}
hashPrevious = hashCurrent;
if (initialHash === null) {
initialHash = hashCurrent;
}
if (initialHash !== hashCurrent) {
initialHash = null;
log.info(`Config file changed, reloading ${event.filename}...`);
}
delete require.cache[require.resolve('./config/userconfig')];
// eslint-disable-next-line
userconfig = require('./config/userconfig');
}
}
} catch (error) {
log.error(`Error watching files: ${error}`);
}
}

setInterval(async () => {
configReload();
}, 2 * 1000);

async function initiate() {
if (!config.server.allowedPorts.includes(+apiPort)) {
Expand Down
Loading

0 comments on commit 326dc5f

Please sign in to comment.