All files / src/state CleanUpState.ts

95.23% Statements 40/42
66.66% Branches 2/3
100% Functions 6/6
95.23% Lines 40/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127    1x 1x 1x   1x 1x 1x   1x   1x 1x                   1x                                                 7x 7x 7x 7x                 1x                     5x 5x 5x 5x 1x                     2x 2x 2x 1x 1x   1x 1x 1x 1x   1x 1x 1x                     1x 1x 1x       1x 1x 15x     1x   1x      
// SPDX-License-Identifier: Apache-2.0
 
import { readFileSync, writeFileSync, existsSync } from 'fs';
import yaml from 'js-yaml';
import { join } from 'path';
import { IOBserver } from '../controller/IObserver';
import originalNodeConfiguration from '../configuration/originalNodeConfiguration.json';
import { LoggerService } from '../services/LoggerService';
import { ServiceLocator } from '../services/ServiceLocator';
import { IState } from './IState';
import { CLIService } from '../services/CLIService';
import { CLIOptions } from '../types/CLIOptions';
import { EventType } from '../types/EventType';
import { CHECK_SUCCESS, LOADING } from '../constants';
 
/**
 * The `CleanUpState` class is responsible for cleaning up and reverting unneeded changes to files.
 * 
 * It implements the `IState` interface and provides methods to subscribe an observer, start the cleanup process, and revert properties of the mirror node and the consensus node.
 * 
 * @class
 * @implements {IState}
 */
export class CleanUpState implements IState{
    /**
     * The logger service used for logging messages.
     */
    private logger: LoggerService;
 
    /**
     * The observer for the CleanUpState.
     */
    private observer: IOBserver | undefined;
 
    /**
     * The CLI options for the initialization state.
     */
    private cliOptions: CLIOptions;
 
    /**
     * The name of the state.
     */
    private stateName: string;
    
    /**
     * Initializes a new instance of the CleanUpState class.
     */
    constructor() {
        this.stateName = CleanUpState.name;
        this.cliOptions = ServiceLocator.Current.get<CLIService>(CLIService.name).getCurrentArgv();
        this.logger = ServiceLocator.Current.get<LoggerService>(LoggerService.name);
        this.logger.trace('Clean Up State Initialized!', this.stateName);
    }
 
    /**
     * Subscribes an observer to the `CleanUpState`.
     * 
     * @param {IObserver} observer - The observer to subscribe.
     */
    public subscribe(observer: IOBserver): void {
        this.observer = observer;
    }
 
    /**
     * Starts the cleanup process.
     * 
     * This method initiates the cleanup procedure, tries to revert unneeded changes to files, and updates the observer when the cleanup is finished.
     * 
     * @returns {Promise<void>}
     */
    public async onStart(): Promise<void> {
        this.logger.info(`${LOADING} Initiating clean up procedure. Trying to revert unneeded changes to files...`, this.stateName);
        this.revertNodeProperties();
        this.revertMirrorNodeProperties();
        if (this.observer) {
            this.observer!.update(EventType.Finish);
        }
    }
 
    /**
     * Reverts the properties of the mirror node.
     * 
     * This method cleans up unneeded mirror node properties and writes the updated properties back to the file.
     * @private
     */
    private revertMirrorNodeProperties() {
        this.logger.trace('Clean up unneeded mirror node properties...', this.stateName);
        const propertiesFilePath = join(this.cliOptions.workDir, 'compose-network/mirror-node/application.yml');
        if (!existsSync(propertiesFilePath)) {
            this.logger.trace(`Mirror Node Properties File doesn't exist at path ${propertiesFilePath}`,this.stateName);
            return;
        }
        const application = yaml.load(readFileSync(propertiesFilePath).toString()) as any;
        delete application.hedera.mirror.importer.dataPath;
        delete application.hedera.mirror.importer.downloader.sources;
        delete application.hedera.mirror.importer.downloader.local
 
        application.hedera.mirror.monitor.nodes = originalNodeConfiguration.fullNodeProperties;
        writeFileSync(propertiesFilePath, yaml.dump(application, { lineWidth: 256 }));
        this.logger.info(`${CHECK_SUCCESS} Clean up of mirror node properties finished.`, this.stateName);
    }
 
    /**
     * Reverts the properties of the consensus node.
     * 
     * This method cleans up unneeded bootstrap properties and writes the original properties back to the file.
     * 
     * @private
     */
    private revertNodeProperties(): void {
        this.logger.trace('Clean up unneeded bootstrap properties.', this.stateName);
        const propertiesFilePath = join(this.cliOptions.workDir, 'compose-network/network-node/data/config/bootstrap.properties');
        Iif (!existsSync(propertiesFilePath)) {
            this.logger.trace(`Node Properties File doesn't exist at path ${propertiesFilePath}`,this.stateName);
            return;
        }
        let originalProperties = '';
        originalNodeConfiguration.bootsrapProperties.forEach(property => {
            originalProperties = originalProperties.concat(`${property.key}=${property.value}\n`);
        });
 
        writeFileSync(propertiesFilePath, originalProperties, { flag: 'w' });
 
        this.logger.info(`${CHECK_SUCCESS} Clean up of consensus node properties finished.`, this.stateName);
    }
}