From afd303f110d8104969cb3c86037457d02ca520c1 Mon Sep 17 00:00:00 2001 From: instamenta Date: Fri, 10 Jan 2025 14:47:25 +0200 Subject: [PATCH] polishing implementation and removing obsolete values from values.yaml Signed-off-by: instamenta --- .../scripts/init-001.sh | 27 +++++++--- .../custom-mirror-node-database/values.yaml | 3 +- src/commands/mirror_node.ts | 53 ++++--------------- src/core/k8.ts | 22 ++++---- 4 files changed, 40 insertions(+), 65 deletions(-) diff --git a/examples/custom-mirror-node-database/scripts/init-001.sh b/examples/custom-mirror-node-database/scripts/init-001.sh index 789327c3e..70c27b821 100644 --- a/examples/custom-mirror-node-database/scripts/init-001.sh +++ b/examples/custom-mirror-node-database/scripts/init-001.sh @@ -1,18 +1,25 @@ +# Example if initialization script for the external database, +# values must match those inside values.yaml if used + cat > init1.sh << 'EOF' #!/bin/bash set -e +export HEDERA_MIRROR_DATABASE_NAME="mirror_node" + export HEDERA_MIRROR_GRAPHQL_DB_HOST="localhost" -export HEDERA_MIRROR_GRAPHQL_DB_NAME="mirror_node" +export HEDERA_MIRROR_GRAPHQL_DB_NAME="${HEDERA_MIRROR_DATABASE_NAME}" export HEDERA_MIRROR_GRAPHQL_DB_PASSWORD="XXXXXXXXXXXX" export HEDERA_MIRROR_GRAPHQL_DB_USERNAME="mirror_graphql" + export HEDERA_MIRROR_GRPC_DB_HOST="localhost" -export HEDERA_MIRROR_GRPC_DB_NAME="mirror_node" +export HEDERA_MIRROR_GRPC_DB_NAME="${HEDERA_MIRROR_DATABASE_NAME}" export HEDERA_MIRROR_GRPC_DB_PASSWORD="XXXXXXXXXXXX" export HEDERA_MIRROR_GRPC_DB_USERNAME="mirror_grpc" + export HEDERA_MIRROR_IMPORTER_DB_HOST="localhost" -export HEDERA_MIRROR_IMPORTER_DB_NAME="mirror_node" -export HEDERA_MIRROR_IMPORTER_DB_OWNER="mirror_node" +export HEDERA_MIRROR_IMPORTER_DB_NAME="${HEDERA_MIRROR_DATABASE_NAME}" +export HEDERA_MIRROR_IMPORTER_DB_OWNER="${HEDERA_MIRROR_DATABASE_NAME}" export HEDERA_MIRROR_IMPORTER_DB_OWNERPASSWORD="XXXXXXXXXXXX" export HEDERA_MIRROR_IMPORTER_DB_PASSWORD="XXXXXXXXXXXX" export HEDERA_MIRROR_IMPORTER_DB_RESTPASSWORD="XXXXXXXXXXXX" @@ -20,20 +27,24 @@ export HEDERA_MIRROR_IMPORTER_DB_RESTUSERNAME="mirror_rest" export HEDERA_MIRROR_IMPORTER_DB_SCHEMA="public" export HEDERA_MIRROR_IMPORTER_DB_TEMPSCHEMA="temporary" export HEDERA_MIRROR_IMPORTER_DB_USERNAME="mirror_importer" + export HEDERA_MIRROR_RESTJAVA_DB_HOST="localhost" -export HEDERA_MIRROR_RESTJAVA_DB_NAME="mirror_node" +export HEDERA_MIRROR_RESTJAVA_DB_NAME="${HEDERA_MIRROR_DATABASE_NAME}" export HEDERA_MIRROR_RESTJAVA_DB_PASSWORD="XXXXXXXXXXXX" export HEDERA_MIRROR_RESTJAVA_DB_USERNAME="mirror_rest_java" + export HEDERA_MIRROR_REST_DB_HOST="localhost" -export HEDERA_MIRROR_REST_DB_NAME="mirror_node" +export HEDERA_MIRROR_REST_DB_NAME="${HEDERA_MIRROR_DATABASE_NAME}" export HEDERA_MIRROR_REST_DB_PASSWORD="XXXXXXXXXXXX" export HEDERA_MIRROR_REST_DB_USERNAME="mirror_rest" + export HEDERA_MIRROR_ROSETTA_DB_HOST="localhost" -export HEDERA_MIRROR_ROSETTA_DB_NAME="mirror_node" +export HEDERA_MIRROR_ROSETTA_DB_NAME="${HEDERA_MIRROR_DATABASE_NAME}" export HEDERA_MIRROR_ROSETTA_DB_PASSWORD="XXXXXXXXXXXX" export HEDERA_MIRROR_ROSETTA_DB_USERNAME="mirror_rosetta" + export HEDERA_MIRROR_WEB3_DB_HOST="localhost" -export HEDERA_MIRROR_WEB3_DB_NAME="mirror_node" +export HEDERA_MIRROR_WEB3_DB_NAME="${HEDERA_MIRROR_DATABASE_NAME}" export HEDERA_MIRROR_WEB3_DB_PASSWORD="XXXXXXXXXXXX" export HEDERA_MIRROR_WEB3_DB_USERNAME="mirror_web3" diff --git a/examples/custom-mirror-node-database/values.yaml b/examples/custom-mirror-node-database/values.yaml index 619f50e93..c2a3deaad 100644 --- a/examples/custom-mirror-node-database/values.yaml +++ b/examples/custom-mirror-node-database/values.yaml @@ -1,12 +1,11 @@ db: host: "my-postgresql.mirror-db.svc.cluster.local" + name: mirror_node enabled: true owner: username: postgres password: "XXXXXXXXXXXX" -postgresPodName: 'my-postgresql-0' - stackgres: enabled: false diff --git a/src/commands/mirror_node.ts b/src/commands/mirror_node.ts index b2caded10..b020c0b2c 100644 --- a/src/commands/mirror_node.ts +++ b/src/commands/mirror_node.ts @@ -24,14 +24,13 @@ import {BaseCommand} from './base.js'; import {Flags as flags} from './flags.js'; import {getEnvValue} from '../core/helpers.js'; import {RemoteConfigTasks} from '../core/config/remote/remote_config_tasks.js'; -import { type AnyObject, type CommandBuilder, type PodName } from '../types/aliases.js' +import {type CommandBuilder, type PodName} from '../types/aliases.js'; import {type Opts} from '../types/command_types.js'; import {ListrLease} from '../core/lease/listr_lease.js'; import {ComponentType} from '../core/config/remote/enumerations.js'; import {MirrorNodeComponent} from '../core/config/remote/components/mirror_node_component.js'; import * as fs from 'node:fs'; import * as path from 'node:path'; -import * as yaml from 'yaml'; import type {Optional, SoloListrTask} from '../types/index.js'; import type {Namespace} from '../core/config/remote/types.js'; @@ -376,7 +375,7 @@ export class MirrorNodeCommand extends BaseCommand { constants.PODS_READY_MAX_ATTEMPTS, constants.PODS_READY_DELAY, ), - skip: ctx => ctx.config.customMirrorNodeDatabaseValuePath, + skip: ctx => !!ctx.config.customMirrorNodeDatabaseValuePath, }, { title: 'Check REST API', @@ -445,7 +444,7 @@ export class MirrorNodeCommand extends BaseCommand { { title: 'Insert data in public.file_data', task: async ctx => { - let namespace = self.configManager.getFlag(flags.namespace) as Namespace; + const namespace = self.configManager.getFlag(flags.namespace) as string; const feesFileIdNum = 111; const exchangeRatesFileIdNum = 112; @@ -460,36 +459,20 @@ export class MirrorNodeCommand extends BaseCommand { }, ${exchangeRatesFileIdNum}, 17);`; const sqlQuery = [importFeesQuery, importExchangeRatesQuery].join('\n'); - let postgresPodName: PodName; - let postgresContainerName: string; - if (ctx.config.customMirrorNodeDatabaseValuePath) { - const data = fs.readFileSync(ctx.config.customMirrorNodeDatabaseValuePath).toString(); - const mirrorNodeValues = yaml.parse(data) as Record; - - if (mirrorNodeValues.db.host.split('.')?.[1]) { - namespace = mirrorNodeValues.db.host.split('.')[1]; - } - - postgresPodName = mirrorNodeValues.postgresPodName as PodName; - const pod = await this.k8.getPodByName(postgresPodName, namespace); - - postgresContainerName = pod.spec.containers[0].name; - } else { - const pods = await this.k8.getPodsByLabel(['app.kubernetes.io/name=postgres'], namespace); - if (pods.length === 0) { - throw new SoloError('postgres pod not found'); - } - - postgresPodName = pods[0].metadata.name as PodName; - postgresContainerName = 'postgresql'; + return; } + const pods = await this.k8.getPodsByLabel(['app.kubernetes.io/name=postgres']); + if (pods.length === 0) { + throw new SoloError('postgres pod not found'); + } + const postgresPodName = pods[0].metadata.name as PodName; + const postgresContainerName = 'postgresql'; const mirrorEnvVars = await self.k8.execContainer( postgresPodName, postgresContainerName, '/bin/bash -c printenv', - namespace, ); const mirrorEnvVarsArray = mirrorEnvVars.split('\n'); const HEDERA_MIRROR_IMPORTER_DB_OWNER = getEnvValue( @@ -505,25 +488,11 @@ export class MirrorNodeCommand extends BaseCommand { 'HEDERA_MIRROR_IMPORTER_DB_NAME', ); - let connectionString: string; - - if (ctx.config.customMirrorNodeDatabaseValuePath) { - const HEDERA_MIRROR_IMPORTER_DB_HOST = getEnvValue( - mirrorEnvVarsArray, - 'HEDERA_MIRROR_IMPORTER_DB_HOST', - ); - - connectionString = `postgresql://${HEDERA_MIRROR_IMPORTER_DB_OWNER}:${HEDERA_MIRROR_IMPORTER_DB_OWNERPASSWORD}@${HEDERA_MIRROR_IMPORTER_DB_HOST}:5432/${HEDERA_MIRROR_IMPORTER_DB_NAME}`; - } else { - connectionString = `postgresql://${HEDERA_MIRROR_IMPORTER_DB_OWNER}:${HEDERA_MIRROR_IMPORTER_DB_OWNERPASSWORD}@localhost:5432/${HEDERA_MIRROR_IMPORTER_DB_NAME}`; - } - await self.k8.execContainer(postgresPodName, postgresContainerName, [ 'psql', - connectionString, + `postgresql://${HEDERA_MIRROR_IMPORTER_DB_OWNER}:${HEDERA_MIRROR_IMPORTER_DB_OWNERPASSWORD}@localhost:5432/${HEDERA_MIRROR_IMPORTER_DB_NAME}`, '-c', sqlQuery, - namespace, ]); }, }, diff --git a/src/core/k8.ts b/src/core/k8.ts index d55ba89d4..d2ee3694a 100644 --- a/src/core/k8.ts +++ b/src/core/k8.ts @@ -195,14 +195,12 @@ export class K8 { /** * Get a podName by name * @param name - podName name - * @param namespace - optional namespace overrides the one provided via commandline */ - async getPodByName(name: string, namespace?: Namespace): Promise { - if (!namespace) namespace = this._getNamespace(); + async getPodByName(name: string): Promise { + const ns = this._getNamespace(); const fieldSelector = `metadata.name=${name}`; - const resp = await this.kubeClient.listNamespacedPod( - namespace, + ns, undefined, undefined, undefined, @@ -221,13 +219,12 @@ export class K8 { /** * Get pods by labels * @param labels - list of labels - * @param namespace - overrides namespace provided via command line */ - async getPodsByLabel(labels: string[] = [], namespace?: Namespace): Promise { - if (!namespace) namespace = this._getNamespace(); + async getPodsByLabel(labels: string[] = []) { + const ns = this._getNamespace(); const labelSelector = labels.join(','); const result = await this.kubeClient.listNamespacedPod( - namespace, + ns, undefined, undefined, undefined, @@ -804,16 +801,15 @@ export class K8 { * @param podName * @param containerName * @param command - sh commands as an array to be run within the containerName (e.g 'ls -la /opt/hgcapp') - * @param namespace - optional namespace overrides the one provided from config manager * @returns console output as string */ - async execContainer(podName: string, containerName: string, command: string | string[], namespace?: Namespace) { + async execContainer(podName: string, containerName: string, command: string | string[]) { const self = this; - if (!namespace) namespace = self._getNamespace(); + const namespace = self._getNamespace(); const guid = uuid4(); const messagePrefix = `execContainer[${podName},${guid}]:`; - if (!(await self.getPodByName(podName, namespace))) throw new IllegalArgumentError(`Invalid pod ${podName}`); + if (!(await self.getPodByName(podName))) throw new IllegalArgumentError(`Invalid pod ${podName}`); if (!command) throw new MissingArgumentError('command cannot be empty'); if (!Array.isArray(command)) {