Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Live Preproduction QA #1377

Open
wants to merge 17 commits into
base: development
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
6 changes: 6 additions & 0 deletions ZelBack/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ module.exports = {
fluxTeamFluxID: '1hjy4bCYBJr4mny4zCE85J94RXa8W6q37',
fluxSupportTeamFluxID: '16iJqiVbHptCx87q6XQwNpKdgEZnFtKcyP',
deterministicNodesStart: 558000,
preProd: {
probability: 0.07,
remote: 'https://github.com/RunOnFlux/flux.git',
branch: 'preprod',
daysToNextEval: 30,
},
fluxapps: {
// in flux main chain per month (blocksLasting)
price: [
Expand Down
19 changes: 12 additions & 7 deletions ZelBack/src/services/dbHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
* @module Helper module used for all interactions with database
*/

/**
* @import { MongoClient } from "mongodb"
*/

const mongodb = require('mongodb');
const config = require('config');

Expand All @@ -23,7 +27,7 @@ function databaseConnection() {
*
* @param {string} [url]
*
* @returns {object} mongodb.MongoClient
* @returns {MongoClient}
*/
async function connectMongoDb(url) {
const connectUrl = url || mongoUrl;
Expand All @@ -32,21 +36,22 @@ async function connectMongoDb(url) {
useUnifiedTopology: true,
maxPoolSize: 100,
};
const db = await MongoClient.connect(connectUrl, mongoSettings);
return db;
const client = await MongoClient.connect(connectUrl, mongoSettings);
return client;
}

/**
* Initiates default db connection.
* @returns true
* @returns {MongoClient}
*/
async function initiateDB() {
if (!openDBConnection) openDBConnection = await connectMongoDb();
return true;
return openDBConnection;
}

/**
* Closes DB connection if exists.
* @returns {Promise<void>}
*/
async function closeDbConnection() {
if (openDBConnection) {
Expand All @@ -63,7 +68,7 @@ async function closeDbConnection() {
* @param {string} distinct - field name
* @param {object} [query]
*
* @returns array
* @returns {Proimise<Array>}
*/
async function distinctDatabase(database, collection, distinct, query) {
const results = await database.collection(collection).distinct(distinct, query);
Expand All @@ -78,7 +83,7 @@ async function distinctDatabase(database, collection, distinct, query) {
* @param {object} query
* @param {object} [projection]
*
* @returns array
* @returns {Promise<Array>}
*/
async function findInDatabase(database, collection, query, projection) {
const results = await database.collection(collection).find(query, projection).toArray();
Expand Down
35 changes: 21 additions & 14 deletions ZelBack/src/services/fluxService.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const fluxNetworkHelper = require('./fluxNetworkHelper');
const geolocationService = require('./geolocationService');
const syncthingService = require('./syncthingService');
const dockerService = require('./dockerService');
const fluxRepository = require('./utils/fluxRepository');

// for streamChain endpoint
const zlib = require('node:zlib');
Expand All @@ -32,6 +33,8 @@ const tar = require('tar-fs');
// const stream = require('node:stream/promises');
const stream = require('node:stream');

const fluxRepo = new fluxRepository.FluxRepository({ repoDir: process.cwd() });

/**
* Stream chain lock, so only one request at a time
*/
Expand Down Expand Up @@ -103,7 +106,6 @@ async function getCurrentCommitId(req, res) {
* @returns {Promise<object>} Message.
*/
async function getCurrentBranch(req, res) {
// ToDo: Fix - this breaks if head in detached state (or something similar)
if (req) {
const authorized = await verificationHelper.verifyPrivilege('adminandfluxteam', req);
if (authorized !== true) {
Expand All @@ -112,23 +114,24 @@ async function getCurrentBranch(req, res) {
}
}

const { stdout: commitId, error } = await serviceHelper.runCommand('git', {
logError: false, params: ['rev-parse', '--abbrev-ref', 'HEAD'],
});

if (error) {
const errMsg = messageHelper.createErrorMessage(
`Error getting current branch of Flux: ${error.message}`,
error.name,
error.code,
);
return res ? res.json(errMsg) : errMsg;
}
// null branch is detached HEAD, or error
const branch = await fluxRepo.currentBranch();

const successMsg = messageHelper.createSuccessMessage(commitId.trim());
const successMsg = messageHelper.createSuccessMessage(branch);
return res ? res.json(successMsg) : successMsg;
}

/**
* If this node is on the preprod branch
* @returns {Promise<boolean>}
*/
async function isPreProdNode() {
const currentBranch = await fluxRepo.currentBranch();
const { preProd: { branch: preProdBranch } } = config;

return currentBranch === preProdBranch;
}

/**
* Check out branch if it exists locally
* @param {string} branch The branch to checkout
Expand Down Expand Up @@ -779,6 +782,7 @@ async function tailDaemonDebug(req, res) {
}

const defaultDir = daemonServiceUtils.getFluxdDir();

const datadir = daemonServiceUtils.getConfigValue('datadir') || defaultDir;
const filepath = path.join(datadir, 'debug.log');

Expand Down Expand Up @@ -1090,6 +1094,8 @@ async function getFluxInfo(req, res) {
}
info.flux.ip = ipRes.data;
info.flux.staticIp = geolocationService.isStaticIP();
const preProdNode = await isPreProdNode();
info.flux.preProdNode = preProdNode;
info.flux.maxNumberOfIpChanges = fluxNetworkHelper.getMaxNumberOfIpChanges();
const zelidRes = await getFluxZelID();
if (zelidRes.status === 'error') {
Expand Down Expand Up @@ -1741,6 +1747,7 @@ module.exports = {
getRouterIP,
hardUpdateFlux,
installFluxWatchTower,
isPreProdNode,
isStaticIPapi,
lockStreamLock,
rebuildHome,
Expand Down
78 changes: 78 additions & 0 deletions ZelBack/src/services/utils/fluxRepository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const path = require('node:path');
const os = require('node:os');
const sg = require('simple-git');

class FluxRepository {
// this may not exist
defaultRepoDir = path.join(os.homedir(), 'zelflux');

constructor(options = {}) {
this.repoPath = options.repoDir || this.defaultRepoDir;

const gitOptions = {
baseDir: this.repoPath,
binary: 'git',
maxConcurrentProcesses: 6,
trimmed: true,
};

this.git = sg.simpleGit(gitOptions);
}

async remotes() {
return this.git.getRemotes(true).catch(() => []);
}

async addRemote(name, url) {
await this.git.addRemote(name, url).catch(() => { });
}

async currentCommitId() {
const id = await this.git.revparse('HEAD').catch(() => null);
return id;
}

async currentBranch() {
const branches = await this.git.branch().catch(() => null);
if (!branches) return null;

const { current, detached } = branches;

return detached ? null : current;
}

async resetToCommitId(id) {
await this.git.reset(sg.ResetMode.HARD, [id]).catch(() => { });
}

async switchBranch(branch, options = {}) {
const forceClean = options.forceClean || false;
const reset = options.reset || false;
const remote = options.remote || 'origin';
const remoteBranch = `${remote}/${branch}`;

// fetch first incase there are errors.
await this.git.fetch(remote, branch);

if (forceClean) {
await this.git.clean(sg.CleanOptions.FORCE + sg.CleanOptions.RECURSIVE);
}

if (reset) {
await this.git.reset(sg.ResetMode.HARD, [remoteBranch]);
}

const exists = await this.git.revparse(['--verify', branch]).catch(() => false);

if (exists) {
await this.git.checkout(branch);
// don't think we need to reset here
await this.git.merge(['--ff-only']);
return;
}

await this.git.checkout(['--track', remoteBranch]);
}
}

module.exports = { FluxRepository };
Loading
Loading