diff --git a/lib/addNewMask.js b/lib/addNewMask.js index a0b98b1..0ce39e1 100644 --- a/lib/addNewMask.js +++ b/lib/addNewMask.js @@ -1,30 +1,24 @@ -const { secretsServerAddress } = require('./logger'); +const { getServerAddress } = require('./helpers'); async function updateMasks(secret) { try { - const serverAddress = await secretsServerAddress; - console.log(`server address: ${serverAddress}`); + const serverAddress = await getServerAddress(); + console.debug(`server address: ${serverAddress}`); + const url = new URL('secrets', serverAddress); - const { default: got } = await import('got'); - /** @type {import('got').Got} */ - const httpClient = got.extend({ - prefixUrl: serverAddress, - responseType: 'json', - throwHttpErrors: false, + const { default: httpClient } = await import('got'); + const response = await httpClient.post(url, { + json: secret, }); - const response = await httpClient.post( - 'secrets', - { json: secret }, - ); - if (!response.ok) { + if (response.statusCode !== 201) { console.error(`could not create mask for secret: ${secret.key}, because server responded with: ${response.statusCode}\n\n${JSON.stringify(response.body)}`); process.exit(1); } console.log(`successfully updated masks with secret: ${secret.key}`); process.exit(0); } catch (error) { - console.log(`could not create mask for secret: ${secret.key}, due to error: ${error}`); + console.error(`could not create mask for secret: ${secret.key}, due to error: ${error}`); process.exit(1); } } diff --git a/lib/const.js b/lib/const.js new file mode 100644 index 0000000..ab0eac5 --- /dev/null +++ b/lib/const.js @@ -0,0 +1,5 @@ +const serverAddressPath = '/root/cf-runtime/SERVER_ADDRESS'; + +module.exports = { + serverAddressPath, +}; diff --git a/lib/helpers.js b/lib/helpers.js index d30ca9a..b721717 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -1,8 +1,9 @@ -const { stat } = require('fs/promises'); +const { stat, writeFile, readFile } = require('fs/promises'); const path = require('path'); const logger = require('cf-logs').Logger('codefresh:containerLogger'); const getPromiseWithResolvers = require('core-js-pure/es/promise/with-resolvers'); const { BuildFinishedSignalFilename } = require('./enums'); +const { serverAddressPath } = require('./const'); const checkFileInterval = 1000; @@ -34,6 +35,25 @@ function watchForBuildFinishedSignal() { return deferred.promise; } +const persistServerAddress = async (serverAddress) => { + try { + await writeFile(serverAddressPath, serverAddress, { encoding: 'utf8' }); + } catch (error) { + logger.error(`Failed to persist server address: ${error}`); + throw error; + } +}; + +const getServerAddress = async () => { + try { + const serverAddress = await readFile(serverAddressPath, { encoding: 'utf8' }); + return serverAddress; + } catch (error) { + logger.error(`Failed to read server address: ${error}`); + throw error; + } +}; + module.exports = { /** * Polyfill of `Promise.withResolvers`, TC39 Stage 4 proposal. @@ -41,4 +61,6 @@ module.exports = { */ getPromiseWithResolvers, watchForBuildFinishedSignal, + persistServerAddress, + getServerAddress, }; diff --git a/lib/index.js b/lib/index.js index 1ed69f2..f3cb2d6 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,4 @@ -const path = require('path'); +const path = require('node:path'); const cflogs = require('cf-logs'); const loggerOptions = { @@ -12,7 +12,7 @@ const loggerOptions = { }; cflogs.init(loggerOptions); -const { Logger } = require('./logger'); +const Logger = require('./logger'); const { watchForBuildFinishedSignal } = require('./helpers'); const taskLoggerConfig = JSON.parse(process.env.TASK_LOGGER_CONFIG); diff --git a/lib/logger.js b/lib/logger.js index bd97679..ddc1473 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -11,14 +11,12 @@ const { ContainerStatus } = require('./enums'); const { LoggerStrategy } = require('./enums'); const { ContainerHandlingStatus } = require('./enums'); const ContainerLogger = require('./ContainerLogger'); -const { getPromiseWithResolvers } = require('./helpers'); +const { getPromiseWithResolvers, persistServerAddress } = require('./helpers'); const initialState = { pid: process.pid, status: 'init', lastLogsDate: new Date(), failedHealthChecks: [], restartCounter: 0, containers: {} }; -const deferredServerAddress = getPromiseWithResolvers(); - class Logger { constructor({ @@ -384,11 +382,10 @@ class Logger { }); const address = await server.listen({ host, port }); - deferredServerAddress.resolve(address); + await persistServerAddress(address); logger.info(`listening for engine updates on ${address}`); } catch (error) { logger.error(`could not start server for engine updates due to error: ${error}`); - deferredServerAddress.reject(error); throw error; } } @@ -432,7 +429,4 @@ class Logger { } } -module.exports = { - Logger, - secretsServerAddress: deferredServerAddress.promise, -}; +module.exports = Logger; diff --git a/test/logger.unit.spec.js b/test/logger.unit.spec.js index 8dea4e2..a272657 100644 --- a/test/logger.unit.spec.js +++ b/test/logger.unit.spec.js @@ -35,7 +35,7 @@ describe('Logger tests', () => { describe('constructor', () => { it('should define workflow log size limit default in case of non provided value', () => { - const { Logger } = proxyquire('../lib/logger', {}); + const Logger = proxyquire('../lib/logger', {}); const loggerId = 'loggerId'; const firebaseAuthUrl = 'firebaseAuthUrl'; @@ -54,7 +54,7 @@ describe('Logger tests', () => { }); it('should use passed workflow log size limit in case passed', () => { - const { Logger } = proxyquire('../lib/logger', {}); + const Logger = proxyquire('../lib/logger', {}); const loggerId = 'loggerId'; const firebaseAuthUrl = 'firebaseAuthUrl'; @@ -91,7 +91,7 @@ describe('Logger tests', () => { return Q.resolve(taskLogger); }); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { '@codefresh-io/task-logger': { TaskLogger: TaskLoggerFactory }, 'express': expressMock, }); @@ -135,7 +135,7 @@ describe('Logger tests', () => { return Q.resolve(taskLogger); }); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { '@codefresh-io/task-logger': { TaskLogger: TaskLoggerFactory }, 'express': expressMock, }); @@ -177,7 +177,7 @@ describe('Logger tests', () => { return Q.resolve(taskLogger); }); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { '@codefresh-io/task-logger': { TaskLogger: TaskLoggerFactory }, 'express': expressMock, }); @@ -218,7 +218,7 @@ describe('Logger tests', () => { return Q.resolve(taskLogger); }); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { '@codefresh-io/task-logger': { TaskLogger: TaskLoggerFactory }, 'express': expressMock, }); @@ -264,7 +264,7 @@ describe('Logger tests', () => { return Q.reject(new Error('my error')); }); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { '@codefresh-io/task-logger': { TaskLogger: TaskLoggerFactory }, 'express': expressMock, }); @@ -294,7 +294,7 @@ describe('Logger tests', () => { callback(null, [{}, {}]); }); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'dockerode': function () { return { listContainers: listContainersSpy @@ -317,7 +317,7 @@ describe('Logger tests', () => { callback(null, []); }); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'dockerode': function () { return { listContainers: listContainersSpy @@ -344,7 +344,7 @@ describe('Logger tests', () => { callback(new Error('getting containers error')); }); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'dockerode': function () { return { listContainers: listContainersSpy @@ -373,7 +373,7 @@ describe('Logger tests', () => { it('should call handleContainer in case of an create event', () => { const startSpy = sinon.spy(); const onSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'docker-events': function () { return { start: startSpy, @@ -400,7 +400,7 @@ describe('Logger tests', () => { callback(); }); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'fs': { writeFile: writeFileSpy, readFileSync: sinon.spy(() => { @@ -420,7 +420,7 @@ describe('Logger tests', () => { callback(new Error('write error')); }); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'fs': { writeFile: writeFileSpy, readFileSync: sinon.spy(() => { @@ -440,7 +440,7 @@ describe('Logger tests', () => { describe('validate', () => { it('should call process exit in case firebase authentication url was not provided', () => { - const { Logger } = proxyquire('../lib/logger', {}); + const Logger = proxyquire('../lib/logger', {}); const loggerId = 'loggerId'; const firebaseAuthUrl = 'firebaseAuthUrl'; @@ -461,7 +461,7 @@ describe('Logger tests', () => { }); process.exit = processExitSpy; - const { Logger } = proxyquire('../lib/logger', {}); + const Logger = proxyquire('../lib/logger', {}); const loggerId = 'loggerId'; const firebaseAuthUrl = ''; @@ -482,7 +482,7 @@ describe('Logger tests', () => { }); process.exit = processExitSpy; - const { Logger } = proxyquire('../lib/logger', {}); + const Logger = proxyquire('../lib/logger', {}); const loggerId = 'loggerId'; const firebaseAuthUrl = 'firebaseAuthUrl'; @@ -503,7 +503,7 @@ describe('Logger tests', () => { }); process.exit = processExitSpy; - const { Logger } = proxyquire('../lib/logger', {}); + const Logger = proxyquire('../lib/logger', {}); const loggerId = ''; const firebaseAuthUrl = 'firebaseAuthUrl'; @@ -538,7 +538,7 @@ describe('Logger tests', () => { emitter.start = startSpy; return emitter; }); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -586,7 +586,7 @@ describe('Logger tests', () => { it('should pass received step log size limit to ContainerLogger', async () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -633,7 +633,7 @@ describe('Logger tests', () => { it('should pass undefined step log size limit to ContainerLogger in case of no label', async () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -676,7 +676,7 @@ describe('Logger tests', () => { it('should update total log size when new log message event was sent', async () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -735,7 +735,7 @@ describe('Logger tests', () => { }); const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -804,7 +804,7 @@ describe('Logger tests', () => { it('was previously handled', () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -849,7 +849,7 @@ describe('Logger tests', () => { it('no loggerId', () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -889,7 +889,7 @@ describe('Logger tests', () => { it('no containerId', () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -922,7 +922,7 @@ describe('Logger tests', () => { it('no container status', () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -955,7 +955,7 @@ describe('Logger tests', () => { it('no step name', () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -988,7 +988,7 @@ describe('Logger tests', () => { it('no strategy provided', () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -1022,7 +1022,7 @@ describe('Logger tests', () => { it('provided strategy does not exist', () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -1057,7 +1057,7 @@ describe('Logger tests', () => { it('container status is create and strategy is logs', () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -1103,7 +1103,7 @@ describe('Logger tests', () => { it('should return true in case log limit exceeded', async () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -1151,7 +1151,7 @@ describe('Logger tests', () => { it('should return false in case log limit not exceeded', async () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -1207,7 +1207,7 @@ describe('Logger tests', () => { it('should return false in case log limit was not defined', async () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -1258,7 +1258,7 @@ describe('Logger tests', () => { it('should return true in case log limit exceeded for one container logger', async () => { const infoSpy = sinon.spy(); const errorSpy = sinon.spy(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -1309,7 +1309,7 @@ describe('Logger tests', () => { emitter.start = sinon.spy(() => Q.resolve()); return emitter; }); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { 'cf-logs': { Logger: () => { return { @@ -1376,7 +1376,7 @@ describe('Logger tests', () => { const containerLogger = new EventEmitter(); containerLogger.start = () => Q.resolve(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { '@codefresh-io/task-logger': { TaskLogger: () => Q.resolve(taskLogger) }, 'docker-events': function () { return dockerEvents; }, './ContainerLogger': function () { return containerLogger; }, @@ -1466,7 +1466,7 @@ describe('Logger tests', () => { const containerLogger = new EventEmitter(); containerLogger.start = () => Q.resolve(); - const { Logger } = proxyquire('../lib/logger', { + const Logger = proxyquire('../lib/logger', { '@codefresh-io/task-logger': { TaskLogger: () => Q.resolve(taskLogger) }, 'docker-events': function () { return dockerEvents; }, './ContainerLogger': function () { return containerLogger; }, @@ -1506,7 +1506,7 @@ describe('Logger tests', () => { // return Q.resolve(taskLogger); // }); - // const { Logger } = proxyquire('../lib/logger', { + // const Logger = proxyquire('../lib/logger', { // '@codefresh-io/task-logger': { TaskLogger: TaskLoggerFactory }, // 'express': expressMock, // });