mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
rework PR-comments
This commit is contained in:
parent
348c3fcce8
commit
ffc64a717f
@ -1,13 +0,0 @@
|
|||||||
// ATTENTION: DO NOT PUT ANY SECRETS IN HERE (or the .env)
|
|
||||||
import dotenv from 'dotenv'
|
|
||||||
import { getDevOpEnvValue } from './tools'
|
|
||||||
dotenv.config()
|
|
||||||
|
|
||||||
const DEVOP = {
|
|
||||||
FEDERATION_DHT_TOPIC: getDevOpEnvValue('FEDERATION_DHT_TOPIC') || null,
|
|
||||||
FEDERATION_DHT_SEED: getDevOpEnvValue('FEDERATION_DHT_SEED') || null,
|
|
||||||
HOME_COMMUNITY_PUBLICKEY: getDevOpEnvValue('HOME_COMMUNITY_PUBLICKEY') || null,
|
|
||||||
HOME_COMMUNITY_PRIVATEKEY: getDevOpEnvValue('HOME_COMMUNITY_PRIVATEKEY') || null,
|
|
||||||
}
|
|
||||||
|
|
||||||
export default DEVOP
|
|
||||||
@ -1,53 +0,0 @@
|
|||||||
/** eslint-disable n/no-sync */
|
|
||||||
import { logger } from '@/server/logger'
|
|
||||||
import fs from 'fs'
|
|
||||||
import os from 'os'
|
|
||||||
import path from 'path'
|
|
||||||
|
|
||||||
const envFilePath = path.resolve(__dirname, './../../.env.devop')
|
|
||||||
|
|
||||||
// read .env file & convert to array
|
|
||||||
const readEnvVars = () => {
|
|
||||||
if (!fs.existsSync(envFilePath)) {
|
|
||||||
logger.info(`devop config file ${envFilePath} will be created...`)
|
|
||||||
fs.writeFileSync(envFilePath, '', 'utf8')
|
|
||||||
}
|
|
||||||
return fs.readFileSync(envFilePath, 'utf-8').split(os.EOL)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Finds the key in .env files and returns the corresponding value
|
|
||||||
*
|
|
||||||
* @param {string} key Key to find
|
|
||||||
* @returns {string|null} Value of the key
|
|
||||||
*/
|
|
||||||
export const getDevOpEnvValue = (key: string): string | null => {
|
|
||||||
// find the line that contains the key (exact match)
|
|
||||||
const matchedLine = readEnvVars().find((line) => line.split('=')[0] === key)
|
|
||||||
// split the line (delimiter is '=') and return the item at index 2
|
|
||||||
return matchedLine !== undefined ? matchedLine.split('=')[1] : null
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates value for existing key or creates a new key=value line
|
|
||||||
*
|
|
||||||
* This function is a modified version of https://stackoverflow.com/a/65001580/3153583
|
|
||||||
*
|
|
||||||
* @param {string} key Key to update/insert
|
|
||||||
* @param {string} value Value to update/insert
|
|
||||||
*/
|
|
||||||
export const setDevOpEnvValue = (key: string, value: string): void => {
|
|
||||||
const envVars = readEnvVars()
|
|
||||||
const targetLine = envVars.find((line) => line.split('=')[0] === key)
|
|
||||||
if (targetLine !== undefined) {
|
|
||||||
// update existing line
|
|
||||||
const targetLineIndex = envVars.indexOf(targetLine)
|
|
||||||
// replace the key/value with the new value
|
|
||||||
envVars.splice(targetLineIndex, 1, `${key}="${value}"`)
|
|
||||||
} else {
|
|
||||||
// create new key value
|
|
||||||
envVars.push(`${key}="${value}"`)
|
|
||||||
}
|
|
||||||
// write everything back to the file system
|
|
||||||
fs.writeFileSync(envFilePath, envVars.join(os.EOL))
|
|
||||||
}
|
|
||||||
@ -5,19 +5,13 @@ import { logger } from '@/server/logger'
|
|||||||
import CONFIG from '@/config'
|
import CONFIG from '@/config'
|
||||||
import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity'
|
import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity'
|
||||||
import { Community as DbCommunity } from '@entity/Community'
|
import { Community as DbCommunity } from '@entity/Community'
|
||||||
import DEVOP from '@/config/devop'
|
|
||||||
import { setDevOpEnvValue } from '@/config/tools'
|
|
||||||
import { v4 as uuidv4 } from 'uuid'
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
|
|
||||||
const KEY_SECRET_SEEDBYTES = 32
|
const KEY_SECRET_SEEDBYTES = 32
|
||||||
const getSeed = (): Buffer | null => {
|
const getSeed = (): Buffer | null => {
|
||||||
let dhtseed = DEVOP.FEDERATION_DHT_SEED
|
return CONFIG.FEDERATION_DHT_SEED
|
||||||
logger.debug('dhtseed set by DEVOP.FEDERATION_DHT_SEED={}', DEVOP.FEDERATION_DHT_SEED)
|
? Buffer.alloc(KEY_SECRET_SEEDBYTES, CONFIG.FEDERATION_DHT_SEED)
|
||||||
if (!dhtseed) {
|
: null
|
||||||
dhtseed = CONFIG.FEDERATION_DHT_SEED
|
|
||||||
logger.debug(`dhtseed overwritten by CONFIG.FEDERATION_DHT_SEED=${CONFIG.FEDERATION_DHT_SEED}`)
|
|
||||||
}
|
|
||||||
return dhtseed ? Buffer.alloc(KEY_SECRET_SEEDBYTES, dhtseed) : null
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const POLLTIME = 20000
|
const POLLTIME = 20000
|
||||||
@ -41,9 +35,6 @@ export const startDHT = async (topic: string): Promise<void> => {
|
|||||||
const keyPair = DHT.keyPair(getSeed())
|
const keyPair = DHT.keyPair(getSeed())
|
||||||
logger.info(`keyPairDHT: publicKey=${keyPair.publicKey.toString('hex')}`)
|
logger.info(`keyPairDHT: publicKey=${keyPair.publicKey.toString('hex')}`)
|
||||||
logger.debug(`keyPairDHT: secretKey=${keyPair.secretKey.toString('hex')}`)
|
logger.debug(`keyPairDHT: secretKey=${keyPair.secretKey.toString('hex')}`)
|
||||||
// insert or update keyPair in .env.devop file
|
|
||||||
setDevOpEnvValue('HOME_COMMUNITY_PUBLICKEY', keyPair.publicKey.toString('hex'))
|
|
||||||
setDevOpEnvValue('HOME_COMMUNITY_PRIVATEKEY', keyPair.secretKey.toString('hex'))
|
|
||||||
await writeHomeCommunityEntry(keyPair.publicKey.toString('hex'))
|
await writeHomeCommunityEntry(keyPair.publicKey.toString('hex'))
|
||||||
|
|
||||||
const ownApiVersions = await writeFederatedHomeCommunityEntries(
|
const ownApiVersions = await writeFederatedHomeCommunityEntries(
|
||||||
@ -207,16 +198,13 @@ export async function writeFederatedHomeCommunityEntries(pubKey: string): Promis
|
|||||||
try {
|
try {
|
||||||
// first remove privious existing homeCommunity entries
|
// first remove privious existing homeCommunity entries
|
||||||
DbFederatedCommunity.createQueryBuilder().delete().where({ foreign: false }).execute()
|
DbFederatedCommunity.createQueryBuilder().delete().where({ foreign: false }).execute()
|
||||||
// homeApiVersions.forEach(async function (homeApi) {
|
|
||||||
for (let i = 0; i < homeApiVersions.length; i++) {
|
for (let i = 0; i < homeApiVersions.length; i++) {
|
||||||
await createFederatedCommunityEntity(homeApiVersions[i], pubKey)
|
await createFederatedCommunityEntity(homeApiVersions[i], pubKey)
|
||||||
// console.log(`ApiVersion:${JSON.stringify(homeApiVersions[i])}`, JSON.stringify(result))
|
|
||||||
logger.info(
|
logger.info(
|
||||||
`federation home-community inserted successfully: ${JSON.stringify(homeApiVersions[i])}`,
|
`federation home-community inserted successfully: ${JSON.stringify(homeApiVersions[i])}`,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// console.log('Error1:', err)
|
|
||||||
throw new Error(`Federation: Error writing federated HomeCommunity-Entries: ${err}`)
|
throw new Error(`Federation: Error writing federated HomeCommunity-Entries: ${err}`)
|
||||||
}
|
}
|
||||||
return homeApiVersions
|
return homeApiVersions
|
||||||
@ -226,7 +214,6 @@ async function createFederatedCommunityEntity(
|
|||||||
homeApi: CommunityApi,
|
homeApi: CommunityApi,
|
||||||
pubKey: string,
|
pubKey: string,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
// let result: InsertResult
|
|
||||||
try {
|
try {
|
||||||
const homeCom = DbFederatedCommunity.create()
|
const homeCom = DbFederatedCommunity.create()
|
||||||
homeCom.foreign = false
|
homeCom.foreign = false
|
||||||
@ -236,17 +223,14 @@ async function createFederatedCommunityEntity(
|
|||||||
|
|
||||||
// this will NOT update the updatedAt column, to distingue between a normal update and the last announcement
|
// this will NOT update the updatedAt column, to distingue between a normal update and the last announcement
|
||||||
await DbFederatedCommunity.insert(homeCom)
|
await DbFederatedCommunity.insert(homeCom)
|
||||||
logger.info(`federation home-community inserted successfully: ${JSON.stringify(homeCom)}`)
|
logger.debug(`federation home-community inserted successfully: ${JSON.stringify(homeCom)}`)
|
||||||
// console.log(`result: ${JSON.stringify(result)}`)
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// console.log('Error2:', err)
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function writeHomeCommunityEntry(pubKey: string): Promise<void> {
|
export async function writeHomeCommunityEntry(pubKey: string): Promise<void> {
|
||||||
// console.log(`pubKey = `, pubKey)
|
|
||||||
try {
|
try {
|
||||||
// check for existing homeCommunity entry
|
// check for existing homeCommunity entry
|
||||||
let homeCom = await DbCommunity.findOne({
|
let homeCom = await DbCommunity.findOne({
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import { startDHT } from '@/dht_node/index'
|
|||||||
|
|
||||||
// config
|
// config
|
||||||
import CONFIG from './config'
|
import CONFIG from './config'
|
||||||
import DEVOP from './config/devop'
|
|
||||||
import { logger } from './server/logger'
|
import { logger } from './server/logger'
|
||||||
import connection from './typeorm/connection'
|
import connection from './typeorm/connection'
|
||||||
import { checkDBVersion } from './typeorm/DBVersion'
|
import { checkDBVersion } from './typeorm/DBVersion'
|
||||||
@ -22,23 +21,13 @@ async function main() {
|
|||||||
logger.fatal('Fatal: Database Version incorrect')
|
logger.fatal('Fatal: Database Version incorrect')
|
||||||
throw new Error('Fatal: Database Version incorrect')
|
throw new Error('Fatal: Database Version incorrect')
|
||||||
}
|
}
|
||||||
// first read from .env.devop if exist
|
logger.debug(`dhtseed set by CONFIG.FEDERATION_DHT_SEED=${CONFIG.FEDERATION_DHT_SEED}`)
|
||||||
let dhttopic = DEVOP.FEDERATION_DHT_TOPIC
|
logger.info(
|
||||||
logger.debug(`dhttopic set by DEVOP.FEDERATION_DHT_TOPIC=${DEVOP.FEDERATION_DHT_TOPIC}`)
|
`starting Federation on ${CONFIG.FEDERATION_DHT_TOPIC} ${
|
||||||
if (!dhttopic) {
|
CONFIG.FEDERATION_DHT_SEED ? 'with seed...' : 'without seed...'
|
||||||
dhttopic = CONFIG.FEDERATION_DHT_TOPIC
|
}`,
|
||||||
logger.debug(
|
)
|
||||||
`dhttopic overwritten by CONFIG.FEDERATION_DHT_TOPIC=${CONFIG.FEDERATION_DHT_TOPIC}`,
|
await startDHT(CONFIG.FEDERATION_DHT_TOPIC)
|
||||||
)
|
|
||||||
}
|
|
||||||
let dhtseed = DEVOP.FEDERATION_DHT_SEED
|
|
||||||
logger.debug(`dhtseed set by DEVOP.FEDERATION_DHT_SEED=${DEVOP.FEDERATION_DHT_SEED}`)
|
|
||||||
if (!dhtseed) {
|
|
||||||
dhtseed = CONFIG.FEDERATION_DHT_SEED
|
|
||||||
logger.debug(`dhtseed overwritten by CONFIG.FEDERATION_DHT_SEED=${CONFIG.FEDERATION_DHT_SEED}`)
|
|
||||||
}
|
|
||||||
logger.info(`starting Federation on ${dhttopic} ${dhtseed ? 'with seed...' : 'without seed...'}`)
|
|
||||||
await startDHT(dhttopic)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
main().catch((e) => {
|
main().catch((e) => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user