Skip to content

Commit

Permalink
polishing implementation and removing obsolete values from values.yaml
Browse files Browse the repository at this point in the history
Signed-off-by: instamenta <[email protected]>
  • Loading branch information
instamenta committed Jan 10, 2025
1 parent d72c705 commit afd303f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 65 deletions.
27 changes: 19 additions & 8 deletions examples/custom-mirror-node-database/scripts/init-001.sh
Original file line number Diff line number Diff line change
@@ -1,39 +1,50 @@
# Example if initialization script for the external database,

Check failure on line 1 in examples/custom-mirror-node-database/scripts/init-001.sh

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

examples/custom-mirror-node-database/scripts/init-001.sh#L1

Tips depend on target shell and yours is unknown. Add a shebang or a 'shell' directive.
# 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"
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"
Expand Down
3 changes: 1 addition & 2 deletions examples/custom-mirror-node-database/values.yaml
Original file line number Diff line number Diff line change
@@ -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

Expand Down
53 changes: 11 additions & 42 deletions src/commands/mirror_node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -445,7 +444,7 @@ export class MirrorNodeCommand extends BaseCommand {
{
title: 'Insert data in public.file_data',
task: async ctx => {
let namespace = self.configManager.getFlag<Namespace>(flags.namespace) as Namespace;
const namespace = self.configManager.getFlag<string>(flags.namespace) as string;

const feesFileIdNum = 111;
const exchangeRatesFileIdNum = 112;
Expand All @@ -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<string, any>;

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(
Expand All @@ -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,
]);
},
},
Expand Down
22 changes: 9 additions & 13 deletions src/core/k8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<k8s.V1Pod> {
if (!namespace) namespace = this._getNamespace();
async getPodByName(name: string): Promise<k8s.V1Pod> {
const ns = this._getNamespace();
const fieldSelector = `metadata.name=${name}`;

const resp = await this.kubeClient.listNamespacedPod(
namespace,
ns,
undefined,
undefined,
undefined,
Expand All @@ -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<k8s.V1Pod[]> {
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,
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit afd303f

Please sign in to comment.