Skip to content
This repository has been archived by the owner on Nov 23, 2022. It is now read-only.

WIP: Feature: deploy history #38

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions src/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,56 @@ const Loki = require('lokijs');

// our packages
const {baseFolder} = require('../config');

// TTL for auth requests
const REQ_TTL =
process.env.NODE_ENV !== 'testing'
? 5 * 60 * 1000 // 5 mins for prod
: 0; // 0 for tests
const {reqCollection} = require('./requests');
const logger = require('../logger');

// init in-memory adapter for login requests
const memAdapter = new Loki.LokiMemoryAdapter();
const fsAdapter = new Loki.LokiFsAdapter();

// init in-memory requests db
const db = new Loki('requests.db', {adapter: memAdapter, autoload: true});
// init persistent tokens db
let dbLoadedResolve = () => {};
const dbLoaded = new Promise(r => {
dbLoadedResolve = r;
});

let tokenCollection = {};
const tokenDb = new Loki(path.join(baseFolder, 'auth.db'), {
let secretsCollection = {};
let deploysCollection = {};

// init persistent db
const db = new Loki(path.join(baseFolder, 'exoframe.db'), {
adapter: process.env.NODE_ENV !== 'testing' ? fsAdapter : memAdapter,
autoload: true,
autoloadCallback() {
// get of create tokens collection
tokenCollection = tokenDb.getCollection('tokens');
// get or create tokens collection
tokenCollection = db.getCollection('tokens');
if (tokenCollection === null) {
tokenCollection = tokenDb.addCollection('tokens');
tokenCollection = db.addCollection('tokens');
}

// get or create secrets collection
secretsCollection = db.getCollection('secrets');
if (secretsCollection === null) {
secretsCollection = db.addCollection('secrets');
}

// get or create deploys collection
deploysCollection = db.getCollection('deploys');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially though of using file-based catalogue of deploys.
I.e. you'd have a top folder projectName and nested 3-5 folders with dates as name corresponding to deployments.
This way you don't need to maintain a DB (and its relation to files) and can easily figure out when a deploy happened and what files need to be used to run it.
Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will first try whether it works with files. Adding data to the database later doesn't sound too complicated to me.

if (deploysCollection === null) {
deploysCollection = db.addCollection('deploys');
}

// resolve waiting load promises
dbLoadedResolve();
logger.info('Database loaded.');
},
autosave: process.env.NODE_ENV !== 'testing',
});

// create requests collection
const reqCollection = db.addCollection('requests', {
ttl: REQ_TTL,
ttlInterval: REQ_TTL,
});
// exports.mainDB = db;
exports.dbLoaded = dbLoaded;

exports.db = db;
exports.tokenDb = tokenDb;
exports.reqCollection = reqCollection;
exports.getTokenCollection = () => tokenCollection;
exports.getSecretsCollection = () => secretsCollection;
exports.getDeploysCollection = () => deploysCollection;
23 changes: 23 additions & 0 deletions src/db/requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// npm packages
const Loki = require('lokijs');

const memAdapter = new Loki.LokiMemoryAdapter();

// TTL for auth requests
const REQ_TTL =
process.env.NODE_ENV !== 'testing'
? 5 * 60 * 1000 // 5 mins for prod
: 0; // 0 for tests

// init in-memory requests db
const db = new Loki('requests.db', {adapter: memAdapter, autoload: true});

// create requests collection
const reqCollection = db.addCollection('requests', {
ttl: REQ_TTL,
ttlInterval: REQ_TTL,
});

module.exports = {
reqCollection,
};
34 changes: 0 additions & 34 deletions src/db/secrets.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const initFastify = require('fastify');
const fastifyAuth = require('fastify-auth');
const cors = require('cors');
const {dbLoaded} = require('./db');

// our packages
const logger = require('./logger');
Expand Down Expand Up @@ -56,6 +57,11 @@ exports.startServer = async (port = 8080) => {

// export start function
exports.start = async port => {
logger.info('Starting exoframe ...');

// load database
await dbLoaded;

// init plugins
await initPlugins();

Expand Down
10 changes: 1 addition & 9 deletions src/routes/secrets.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// our modules
const {secretsInited, getSecretsCollection} = require('../db/secrets');
const {getSecretsCollection} = require('../db');

module.exports = fastify => {
fastify.route({
Expand All @@ -9,8 +9,6 @@ module.exports = fastify => {
// get username
const {username} = request.user;

// wait for db to init if required
await secretsInited;
// find user secrets
const secrets = getSecretsCollection()
.find({user: username})
Expand All @@ -28,8 +26,6 @@ module.exports = fastify => {
const {username} = request.user;
const {secretName} = request.params;

// wait for db to init if required
await secretsInited;
// find user secrets
const secret = getSecretsCollection().findOne({user: username, name: secretName});

Expand All @@ -46,8 +42,6 @@ module.exports = fastify => {
// get secret data
const {secretName, secretValue} = request.body;

// wait for db to init if required
await secretsInited;
// create new secret for current user
const secret = {user: username, name: secretName, value: secretValue};
getSecretsCollection().insert(secret);
Expand All @@ -68,8 +62,6 @@ module.exports = fastify => {
reply.code(200).send({removed: false, reason: 'Secret does not exist'});
return;
}
// wait for db to init if required
await secretsInited;
// remove token from collection
getSecretsCollection().remove(existingSecret);
// send back to user
Expand Down
2 changes: 1 addition & 1 deletion src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {spawn} = require('child_process');
const tar = require('tar-fs');
const rimraf = require('rimraf');
const uuid = require('uuid');
const {getSecretsCollection} = require('../db/secrets');
const {getSecretsCollection} = require('../db');

// our modules
const {tempDockerDir: tempDir} = require('../config');
Expand Down
4 changes: 3 additions & 1 deletion test/login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const getPort = require('get-port');
// our packages
const {startServer} = require('../src');
const {auth: authConfig} = require('../config');
const {getTokenCollection} = require('../src/db');
const {dbLoaded, getTokenCollection} = require('../src/db');

let server;
let authToken = '';
Expand All @@ -25,6 +25,8 @@ jest.setTimeout(60000);
beforeAll(async () => {
const port = await getPort();
server = await startServer(port);
await dbLoaded;

return server;
});

Expand Down
4 changes: 2 additions & 2 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const getPort = require('get-port');

// our packages
const authToken = require('./fixtures/authToken');
const {secretsInited} = require('../src/db/secrets');
const {dbLoaded} = require('../src/db');
const {initPlugins, getPlugins} = require('../src/plugins');
const {initDocker} = require('../src/docker/init');
const {start, startFromParams} = require('../src/docker/start');
Expand All @@ -30,7 +30,7 @@ const options = {
};

beforeAll(async done => {
await secretsInited;
await dbLoaded;
await initPlugins();
done();
});
Expand Down
5 changes: 4 additions & 1 deletion test/secrets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const tar = require('tar-fs');
// our packages
const authToken = require('./fixtures/authToken');
const {startServer} = require('../src');
const {getSecretsCollection} = require('../src/db/secrets');
const {dbLoaded, getSecretsCollection} = require('../src/db');
const docker = require('../src/docker/docker');

// create tar streams
Expand All @@ -35,6 +35,9 @@ beforeAll(async () => {
// start server
const port = await getPort();
fastify = await startServer(port);

await dbLoaded;

return fastify;
});

Expand Down