refactor: update dependencies

This commit is contained in:
Michel Jonathan Schmitz
2020-11-03 12:52:54 +01:00
committed by Rainer Killinger
parent 9512bca329
commit 053a6ce23f
9 changed files with 3694 additions and 1689 deletions

View File

@@ -13,68 +13,57 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {Logger} from '@openstapps/logger';
import {execSync} from 'child_process';
import * as config from 'config';
import * as Dockerode from 'dockerode';
import {readFile, writeFileSync} from 'fs-extra';
import {render} from 'mustache';
import {ConfigFile, logger} from './common';
import {asyncReadFile, asyncWriteFile, configFile} from './common';
import {getContainers, getTemplateView} from './main';
// handle unhandled promise rejections
process.on('unhandledRejection', (error: Error) => {
logger.error(error.message);
logger.info(error.stack);
process.on('unhandledRejection', async (error) => {
await Logger.error(error);
process.exit(1);
});
let containerHashCache = '';
const configFile: ConfigFile = config.util.toObject();
/**
* Reads the container information from the docker socket and updates
* the nginx config if necessary
*
* The function will call itself again every 10s
* Reads the container information from the docker socket and updates the nginx config if necessary
*/
async function updateNginxConfig() {
const containers = await getContainers();
const containerHash = containers.map((container: Dockerode.ContainerInfo) => {
return container.Id;
}).join(',');
const containerHash = containers
.map((container: Dockerode.ContainerInfo) => {
return container.Id;
})
.join(',');
// if containers changed -> write config file, reload nginx
if (containerHash !== containerHashCache) {
logger.log('docker container changed');
logger.log('Generating new NGINX configuration');
Logger.log('Generating new NGINX configuration');
// render nginx config file
const nginxConfig = render(await readFile('nginx.conf.template', 'utf8'), await getTemplateView(containers));
const nginxConfig = render(await asyncReadFile('nginx.conf.template', 'utf8'), await getTemplateView(containers));
logger.log(`containers (${containerHash}) matched the configuration.`);
Logger.log(`containers (${containerHash}) matched the configuration.`);
containerHashCache = containerHash;
logger.log(`Writing new config file "${configFile.output}"`);
// overwrite nginx config file with our rendered one
writeFileSync(configFile.output, nginxConfig, 'utf8');
Logger.log(`Writing new config file "${configFile.output}"`);
// overwrite nginx config file with our rendered one
await asyncWriteFile(configFile.output, nginxConfig, 'utf8');
Logger.log('Executing "nginx -s reload" to tell nginx to reload the configuration file');
logger.log('Executing "nginx -s reload" to tell nginx to reload the configuration file');
execSync('nginx -s reload');
}
// tslint:disable-next-line:no-magic-numbers - set timeout to update configuration again in 30s
setTimeout(updateNginxConfig, 30000);
}
function forever() {
// start the process of dynamic nginx configuration
updateNginxConfig().then(() => {
// check for changes again in 10 seconds
setTimeout(forever, 10000);
}).catch((err) => {
throw err;
});
}
// start the process that checks the docker socket periodically
forever();
// tslint:disable-next-line:no-floating-promises - start the process that checks the docker socket periodically
updateNginxConfig();