insert or update home-community on start dht-node

This commit is contained in:
Claus-Peter Hübner 2023-04-26 23:14:31 +02:00
parent 7a91bf4be2
commit 37298febd6
6 changed files with 87 additions and 10 deletions

View File

@ -8,6 +8,10 @@ DB_PASSWORD=$DB_PASSWORD
DB_DATABASE=gradido_community DB_DATABASE=gradido_community
TYPEORM_LOGGING_RELATIVE_PATH=$TYPEORM_LOGGING_RELATIVE_PATH TYPEORM_LOGGING_RELATIVE_PATH=$TYPEORM_LOGGING_RELATIVE_PATH
# Community
COMMUNITY_NAME=$COMMUNITY_NAME
COMMUNITY_DESCRIPTION=$COMMUNITY_DESCRIPTION
# Federation # Federation
FEDERATION_DHT_CONFIG_VERSION=$FEDERATION_DHT_CONFIG_VERSION FEDERATION_DHT_CONFIG_VERSION=$FEDERATION_DHT_CONFIG_VERSION
# if you set the value of FEDERATION_DHT_TOPIC, the DHT hyperswarm will start to announce and listen # if you set the value of FEDERATION_DHT_TOPIC, the DHT hyperswarm will start to announce and listen

View File

@ -23,7 +23,8 @@
"nodemon": "^2.0.20", "nodemon": "^2.0.20",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",
"tsconfig-paths": "^4.1.2", "tsconfig-paths": "^4.1.2",
"typescript": "^4.9.4" "typescript": "^4.9.4",
"uuid": "^8.3.2"
}, },
"devDependencies": { "devDependencies": {
"@types/dotenv": "^8.2.0", "@types/dotenv": "^8.2.0",
@ -31,6 +32,7 @@
"@types/node": "^18.11.18", "@types/node": "^18.11.18",
"@typescript-eslint/eslint-plugin": "^5.48.0", "@typescript-eslint/eslint-plugin": "^5.48.0",
"@typescript-eslint/parser": "^5.48.0", "@typescript-eslint/parser": "^5.48.0",
"@types/uuid": "^8.3.4",
"eslint": "^8.31.0", "eslint": "^8.31.0",
"eslint-config-prettier": "^8.3.0", "eslint-config-prettier": "^8.3.0",
"eslint-config-standard": "^17.0.0", "eslint-config-standard": "^17.0.0",

View File

@ -9,7 +9,7 @@ const constants = {
LOG_LEVEL: process.env.LOG_LEVEL || 'info', LOG_LEVEL: process.env.LOG_LEVEL || 'info',
CONFIG_VERSION: { CONFIG_VERSION: {
DEFAULT: 'DEFAULT', DEFAULT: 'DEFAULT',
EXPECTED: 'v2.2023-02-07', EXPECTED: 'v3.2023-04-26',
CURRENT: '', CURRENT: '',
}, },
} }
@ -28,6 +28,12 @@ const database = {
process.env.TYPEORM_LOGGING_RELATIVE_PATH || 'typeorm.dht-node.log', process.env.TYPEORM_LOGGING_RELATIVE_PATH || 'typeorm.dht-node.log',
} }
const community = {
COMMUNITY_NAME: process.env.COMMUNITY_NAME || 'Gradido Entwicklung',
COMMUNITY_DESCRIPTION:
process.env.COMMUNITY_DESCRIPTION || 'Gradido-Community einer lokalen Entwicklungsumgebung.',
}
const federation = { const federation = {
FEDERATION_DHT_TOPIC: process.env.FEDERATION_DHT_TOPIC || 'GRADIDO_HUB', FEDERATION_DHT_TOPIC: process.env.FEDERATION_DHT_TOPIC || 'GRADIDO_HUB',
FEDERATION_DHT_SEED: process.env.FEDERATION_DHT_SEED || null, FEDERATION_DHT_SEED: process.env.FEDERATION_DHT_SEED || null,
@ -51,6 +57,7 @@ const CONFIG = {
...constants, ...constants,
...server, ...server,
...database, ...database,
...community,
...federation, ...federation,
} }

View File

@ -4,8 +4,10 @@ import DHT from '@hyperswarm/dht'
import { logger } from '@/server/logger' 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 DEVOP from '@/config/devop' import DEVOP from '@/config/devop'
import { setDevOpEnvValue } from '@/config/tools' import { setDevOpEnvValue } from '@/config/tools'
import { v4 as uuidv4 } from 'uuid'
const KEY_SECRET_SEEDBYTES = 32 const KEY_SECRET_SEEDBYTES = 32
const getSeed = (): Buffer | null => { const getSeed = (): Buffer | null => {
@ -42,8 +44,9 @@ export const startDHT = async (topic: string): Promise<void> => {
// insert or update keyPair in .env.devop file // insert or update keyPair in .env.devop file
setDevOpEnvValue('HOME_COMMUNITY_PUBLICKEY', keyPair.publicKey.toString('hex')) setDevOpEnvValue('HOME_COMMUNITY_PUBLICKEY', keyPair.publicKey.toString('hex'))
setDevOpEnvValue('HOME_COMMUNITY_PRIVATEKEY', keyPair.secretKey.toString('hex')) setDevOpEnvValue('HOME_COMMUNITY_PRIVATEKEY', keyPair.secretKey.toString('hex'))
await writeHomeCommunityEntry(keyPair.publicKey)
const ownApiVersions = await writeFederatedHomeCommunityEnries(keyPair.publicKey) const ownApiVersions = await writeFederatedHomeCommunityEntries(keyPair.publicKey)
logger.info(`ApiList: ${JSON.stringify(ownApiVersions)}`) logger.info(`ApiList: ${JSON.stringify(ownApiVersions)}`)
const node = new DHT({ keyPair }) const node = new DHT({ keyPair })
@ -191,7 +194,7 @@ export const startDHT = async (topic: string): Promise<void> => {
} }
} }
async function writeFederatedHomeCommunityEnries(pubKey: any): Promise<CommunityApi[]> { async function writeFederatedHomeCommunityEntries(pubKey: any): Promise<CommunityApi[]> {
const homeApiVersions: CommunityApi[] = Object.values(ApiVersionType).map(function (apiEnum) { const homeApiVersions: CommunityApi[] = Object.values(ApiVersionType).map(function (apiEnum) {
const comApi: CommunityApi = { const comApi: CommunityApi = {
api: apiEnum, api: apiEnum,
@ -215,7 +218,56 @@ async function writeFederatedHomeCommunityEnries(pubKey: any): Promise<Community
logger.info(`federation home-community inserted successfully: ${JSON.stringify(homeCom)}`) logger.info(`federation home-community inserted successfully: ${JSON.stringify(homeCom)}`)
}) })
} catch (err) { } catch (err) {
throw new Error(`Federation: Error writing HomeCommunity-Entries: ${err}`) throw new Error(`Federation: Error writing federated HomeCommunity-Entries: ${err}`)
} }
return homeApiVersions return homeApiVersions
} }
async function writeHomeCommunityEntry(pubKey: any): Promise<void> {
try {
// check for existing homeCommunity entry
let homeCom = await DbCommunity.findOne({ foreign: false, publicKey: pubKey })
if (!homeCom) {
// check if a homecommunity with a different publicKey still exists
homeCom = await DbCommunity.findOne({ foreign: false })
}
if (homeCom) {
// simply update the existing entry, but it MUST keep the ID and UUID because of possible relations
homeCom.publicKey = pubKey.toString('hex')
homeCom.url = CONFIG.FEDERATION_COMMUNITY_URL
homeCom.name = CONFIG.COMMUNITY_NAME
homeCom.description = CONFIG.COMMUNITY_DESCRIPTION
// this will NOT update the updatedAt column, to distingue between a normal update and the last announcement
await DbCommunity.save(homeCom)
logger.info(`home-community updated successfully: ${JSON.stringify(homeCom)}`)
} else {
// insert a new homecommunity entry including a new ID and UUID
homeCom = new DbCommunity()
homeCom.foreign = false
homeCom.publicKey = pubKey.toString('hex')
homeCom.communityUuid = await newCommunityUuid()
homeCom.url = CONFIG.FEDERATION_COMMUNITY_URL
homeCom.name = CONFIG.COMMUNITY_NAME
homeCom.description = CONFIG.COMMUNITY_DESCRIPTION
homeCom.creationDate = new Date()
// this will NOT update the updatedAt column, to distingue between a normal update and the last announcement
await DbCommunity.insert(homeCom)
logger.info(`home-community inserted successfully: ${JSON.stringify(homeCom)}`)
}
} catch (err) {
throw new Error(`Federation: Error writing HomeCommunity-Entry: ${err}`)
}
}
const newCommunityUuid = async (): Promise<string> => {
let uuid: string
let countIds: number
do {
uuid = uuidv4()
countIds = await DbCommunity.count({ where: { communityUuid: uuid } })
if (countIds > 0) {
logger.info('CommunityUuid creation conflict...')
}
} while (countIds > 0)
return uuid
}

View File

@ -27,7 +27,10 @@ async function main() {
logger.debug('dhttopic set by DEVOP.FEDERATION_DHT_TOPIC={}', DEVOP.FEDERATION_DHT_TOPIC) logger.debug('dhttopic set by DEVOP.FEDERATION_DHT_TOPIC={}', DEVOP.FEDERATION_DHT_TOPIC)
if (!dhttopic) { if (!dhttopic) {
dhttopic = CONFIG.FEDERATION_DHT_TOPIC dhttopic = CONFIG.FEDERATION_DHT_TOPIC
logger.debug('dhttopic overwritten by CONFIG.FEDERATION_DHT_TOPIC={}', CONFIG.FEDERATION_DHT_TOPIC) logger.debug(
'dhttopic overwritten by CONFIG.FEDERATION_DHT_TOPIC={}',
CONFIG.FEDERATION_DHT_TOPIC,
)
} }
let dhtseed = DEVOP.FEDERATION_DHT_SEED let dhtseed = DEVOP.FEDERATION_DHT_SEED
logger.debug('dhtseed set by DEVOP.FEDERATION_DHT_SEED={}', DEVOP.FEDERATION_DHT_SEED) logger.debug('dhtseed set by DEVOP.FEDERATION_DHT_SEED={}', DEVOP.FEDERATION_DHT_SEED)
@ -35,8 +38,7 @@ async function main() {
dhtseed = CONFIG.FEDERATION_DHT_SEED dhtseed = CONFIG.FEDERATION_DHT_SEED
logger.debug('dhtseed overwritten by CONFIG.FEDERATION_DHT_SEED={}', CONFIG.FEDERATION_DHT_SEED) logger.debug('dhtseed overwritten by CONFIG.FEDERATION_DHT_SEED={}', CONFIG.FEDERATION_DHT_SEED)
} }
logger.info( logger.info(`starting Federation on ${dhttopic} ${dhtseed ? 'with seed...' : 'without seed...'}`)
`starting Federation on ${dhttopic} ${dhtseed ? 'with seed...' : 'without seed...'}`)
await startDHT(dhttopic) await startDHT(dhttopic)
} }

View File

@ -769,6 +769,11 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c" resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c"
integrity sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw== integrity sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==
"@types/uuid@^8.3.4":
version "8.3.4"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.4.tgz#bd86a43617df0594787d38b735f55c805becf1bc"
integrity sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==
"@types/yargs-parser@*": "@types/yargs-parser@*":
version "21.0.0" version "21.0.0"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.0.tgz#0c60e537fa790f5f9472ed2776c2b71ec117351b" resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-21.0.0.tgz#0c60e537fa790f5f9472ed2776c2b71ec117351b"
@ -4138,6 +4143,11 @@ url-parse@^1.5.3:
querystringify "^2.1.1" querystringify "^2.1.1"
requires-port "^1.0.0" requires-port "^1.0.0"
uuid@^8.3.2:
version "8.3.2"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
v8-compile-cache-lib@^3.0.1: v8-compile-cache-lib@^3.0.1:
version "3.0.1" version "3.0.1"
resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf" resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf"