#!/usr/bin/python3.11
__AUTHOR__ = "Sean Watkins : swatkins@cohesity.com"
__COPYRIGHT__ = "Copyright 2025, Cohesity Inc"
# This script installs the contents of the manifest.json

import argparse
import hashlib
import os
import sys
import subprocess
import time
import logging
import re
import json
from getpass import getuser
from multiprocessing import pool
import multiprocessing as mp
from signal import signal, SIGTSTP, SIGINT, SIGKILL

sys.pycache_prefix = '/tmp'

#Constants
COHESITY_BIN_PATH="/home/cohesity/bin"
CRED = '\033[91m'
CEND = '\033[0m'
CGREEN = '\033[92m'
CYELLOW = '\033[33m'
CBLUE = '\033[34m'
FILENAME=''
VERSION=1
MANIFEST='manifest.json'
TIMEOUT=180

logging.basicConfig(
    filename='support_toolbox_installer.INFO',level=logging.DEBUG,filemode="w")

def get_cluster_ips(ipv6_check:bool=False) -> list:
    """
    get_cluster_ips collects the IP addresses of the Cohesity cluster nodes.

    This function runs the `hostips` command to retrieve the IP addresses of the nodes in the cluster.

    Args:
        ipv6_check (bool, optional): defines if ipv6 addresses are to be returned. Defaults to False.

    Returns:
        list: collection of all IP addresses of the Cohesity cluster nodes as strings
    """
    isV6 = False
    cmd = f"{COHESITY_BIN_PATH}/hostips"
    result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,timeout=TIMEOUT)
    if result.stdout != None:
        stdOut = result.stdout.decode("utf-8")
    else:
        stdOut = ""
    if result.stderr != None:
        stdErr = result.stderr.decode("utf-8")
    else:
        stdErr = ""
    retCode = result.returncode
    if retCode != 0:
        logging.critical(f"Error in get_cluster_ips : {cmd}")
        logging.critical(f"Output : {stdOut}")
        logging.critical(f"Error : {stdErr}")
        print(f"{CRED}Error{CEND} : {CYELLOW}Unable to get cluster IPs, please ensure that the user has permissions to run hostips{CEND}")
        exit(1)
    ipList = stdOut.split()
    for ip in ipList:
        if ':' in ip:
            isV6 = True
    return ipList, isV6

def run_shell_command(command:list,to:int=TIMEOUT,test:bool=False) -> tuple:
    """
    run_shell_command wraps the subprocess.run function to execute a shell command.

    This function captures the standard output, standard error, and return code of the command execution.
    It also handles timeout exceptions and returns a tuple containing the output, error, return code, and the command itself.

    Args:
        command (list): List of strings representing the command and its arguments to be executed.
        to (int, optional): Timeout in seconds. Defaults to TIMEOUT.
        test (bool, optional): Defines if is being executes as unit test or no. Defaults to False.

    Returns:
        tuple: A tuple containing:
            - stdOut (str): Standard output of the command.
            - stdErr (str): Standard error of the command.
            - retCode (int): Return code of the command execution.
            - cmd (str): The command that was executed as a string.

    Raises:
        subprocess.TimeoutExpired: If the command execution exceeds the specified timeout.
        Exception: If any other error occurs during command execution.
    """
    try:
        result = subprocess.run(command,timeout=to,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
        if result.stdout != None:
            stdOut = result.stdout.decode("utf-8")
        else:
            stdOut = ""
        if result.stderr != None:
            stdErr = result.stderr.decode("utf-8")
        else:
            stdErr = ""
        retCode = result.returncode
    except subprocess.TimeoutExpired as te:
        logging.critical(f"Command Timed out \n Command : {te.cmd} \n Error : {te.stderr} \n Output : {te.stdout}")
        if te.stdout != None:
            stdOut = result.stdout.decode("utf-8")
        else:
            stdOut = ""
        if te.stderr != None:
            stdErr = result.stderr.decode("utf-8")
        else:
            stdErr = ""
        retCode = 666
    except Exception as e:
        stdOut = str(e)
        stdErr = "Timeout"
        retCode = 666
    return(stdOut,stdErr,retCode,' '.join(command))

def sigint_handler(signal_received, frame) -> None:
    """
    sigint_handler handles the SIGINT signal (Ctrl+C).
    """
    print(CGREEN + 'Stopping execution.' + CEND)
    pid = os.getpid()
    os.kill(pid, SIGKILL)
    exit(0)

def sigtstp_handler(signal_received, frame) -> None:
    """
    sigtstp_handler handles the SIGTSTP signal (Ctrl+Z).
    """
    print(CGREEN + 'Stopping execution.' + CEND)
    pid = os.getpid()
    os.kill(pid, SIGKILL)
    exit(0)

def check_sudo() -> bool:
    """
    check_sudo checks if the sudo command is available on the system.

    This function attempts to run a simple sudo command to verify if sudo is configured correctly.

    Returns:
        bool: True if sudo is available, False otherwise
    """
    try:
        result = subprocess.run(['sudo', '-n', 'true'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        if result.returncode == 0:
            return True
        else:
            return False
    except FileNotFoundError:
        return False
#End Common Functions

class Installer:

    def __init__(self,args:dict):
        """
        __init__ initializes the Installer class.

        This method reads the manifest file, checks for sudo availability, and sets up various attributes needed for installation.

        Args:
            args (dict): A dictionary containing command line arguments, such as the SSH port.
        """
        #read in manifest
        with open(MANIFEST, 'r') as f:
            self.manifest = json.load(f)
            self.manifest:dict
            f.close()
        #check sudo
        if not check_sudo():
            print(f"{CRED}Error{CEND} : {CYELLOW}Sudo is not available, please enable sudo and try again.{CEND}")
            exit(1)
        self.installdir = os.getcwd()
        self.hostips,self.v6 = get_cluster_ips()
        self.rpms = list(self.manifest["RPMS"].keys())
        self.rpmPackages = ' '.join(self.rpms).replace(".rpm","").split()
        self.tmpRpm = [f"/tmp/{x}" for x in self.rpms]
        self.rpmShort = []
        for f in self.rpmPackages:
            nameParts = f.split("-")
            self.rpmShort.append(f"{nameParts[0]}-{nameParts[1]}")
        self.package = self.manifest["Package"]
        self.version = self.manifest["Version"]
        self.user = getuser()
        self.sshPort = args['port']
        if not self.v6:
            self.sshOpts = [
                "/home/cohesity/software/crux/bin/ssh",
                "-p", str(self.sshPort),
                "-o", "ConnectTimeout=5",
                "-o", "PreferredAuthentications=publickey,password",
                "-o", "StrictHostKeyChecking=no",
                "-o", "UserKnownHostsFile=/dev/null",
                "-o", "LogLevel=ERROR"
            ]
            self.scpOpts = [
                "/home/cohesity/software/crux/bin/scp",
                "-P", str(self.sshPort),
                "-o", "StrictHostKeyChecking=no",
                "-o", "UserKnownHostsFile=/dev/null",
                "-o", "LogLevel=ERROR"
            ]
        else:
            self.sshOpts = [
                "/home/cohesity/software/crux/bin/ssh",
                "-6",
                "-p", str(self.sshPort),
                "-o", "ConnectTimeout=5",
                "-o", "PreferredAuthentications=publickey,password",
                "-o", "StrictHostKeyChecking=no",
                "-o", "UserKnownHostsFile=/dev/null",
                "-o", "LogLevel=ERROR"
            ]
            self.scpOpts = [
                "/home/cohesity/software/crux/bin/scp",
                "-6",
                "-P", str(self.sshPort),
                "-o", "StrictHostKeyChecking=no",
                "-o", "UserKnownHostsFile=/dev/null",
                "-o", "LogLevel=ERROR"
            ]
        self.here = os.getcwd()


    def pushPackages(self,test:bool=False) -> bool | list:
        """
        pushPackages collects all the RPMs from the manifest and pushes them to all nodes in the cluster.

        This method uses multiprocessing to push the RPMs to each node and verifies that the files were successfully copied.
        It returns a boolean indicating success or failure, and a list of failed commands if any.

        Args:
            test (bool, optional): Defines if unit testing in execution. Defaults to False.

        Returns:
            bool | list: Returns True if all packages were pushed successfully, otherwise returns a list of failed commands.
                         The list contains formatted strings with the command, output, and error messages for each failed command.
        """
        pushPool = mp.Pool()
        verifyPool = mp.Pool()
        pushCommands = []
        pushVerify = []
        failedCmd = []
        pushProc = []
        verifyProc = []
        state = True
        for ip in self.hostips:
            if self.v6:
                ip = f"[{ip}]"
            for rpm in self.rpms:
                command = []
                command.extend(self.scpOpts)
                command.extend(
                    [
                        f"{self.installdir}/{rpm}",
                        f"{ip}:/tmp/{rpm}"
                    ]
                )
                pushCommands.append(command)
            command = []
            command.extend(self.sshOpts)
            command.extend(
                [
                    f"{self.user}@{ip}",
                    f"'test -f /tmp/{' '.join(self.rpms)};echo $?'"
                ]
            )
            pushVerify.append(command)
            #Moving the remove to push
            command = []
            command.extend(self.sshOpts)
            command.extend(
                [
                    f"{self.user}@{ip}",
                    f"for i in `sudo rpm -q {' '.join(self.rpmShort)} support-toolbox-3.0.0` ; do sudo rpm -e $i --nodeps || true; done"
                ]
            )
            pushCommands.append(command)
        for command in pushCommands:
            pushProc.append(pushPool.map_async(run_shell_command,(command,)))
        try:
            [p.wait(TIMEOUT) for p in pushProc]
        except TimeoutError:
            return False
        results = [p.get() for p in pushProc]
        for result in results:
            out,err,code,cmd = result[0]
            if code == 1:
                logging.critical(f"Error in push : {' '.join(cmd)}")
                logging.critical(f"Output : {out}")
                logging.critical(f"Error : {err}")
                failedCmd.append(f"{CYELLOW}Command{CEND} : \n{cmd}\n")
                failedCmd.append(f"{CYELLOW}Output{CEND} : \n{out}\n")
                failedCmd.append(f"{CRED}Error{CEND} : \n{err}")
            else:
                logging.info(f"Command {cmd} successful")
        for command in pushVerify:
            verifyProc.append(verifyPool.map_async(run_shell_command,(command,)))
        try:
            [p.wait(TIMEOUT) for p in verifyProc]
        except TimeoutError:
            return False
        results = [p.get() for p in verifyProc]
        for result in results:
            out,err,code,cmd = result[0]
            if out == "1" or code == 1:
                logging.critical(f"Error in validation : {cmd}")
                logging.critical(f"Output : {out}")
                logging.critical(f"Error : {err}")
                failedCmd.append(f"{CYELLOW}Command{CEND} : \n{cmd}\n")
                failedCmd.append(f"{CYELLOW}Output{CEND} : \n{out}\n")
                failedCmd.append(f"{CRED}Error{CEND} : \n{err}")
            else:
                logging.info(f"Verification command {cmd} successful")
        if failedCmd:
            failedCmd.append(f"\n{CRED}Push Failures{CEND}\nCheck that /tmp is not full and that the user has write permissions to /tmp on all nodes")
            state = False
        return state,failedCmd

    def install(self,test:bool=False) -> bool | list:
        """
        install Installs the RPM packages on all nodes in the cluster.

        This method uses multiprocessing to run the installation commands on each node and verifies that the packages were installed successfully.
        It returns a boolean indicating success or failure, and a list of failed commands if any.
        The installation is done using the `rpm` command with options to force installation and ignore dependencies.
        It also verifies the installation by checking if the packages are listed as installed.

        Args:
            test (bool, optional): Defines if unit testing in execution. Defaults to False.

        Returns:
            bool | list: Returns True if all packages were installed successfully, otherwise returns a list of failed commands.
                         The list contains formatted strings with the command, output, and error messages for each failed command.
        """
        installPool = mp.Pool()
        verifyPool = mp.Pool()
        installCommands = []
        installVerify = []
        installProc = []
        verifyProc = []
        failedCmd = []
        state = True
        for ip in self.hostips:
            command = []
            command.extend(self.sshOpts)
            command.extend(
                [
                    f"{self.user}@{ip}",f'sudo rpm -ihv --nodeps --force {" ".join(self.tmpRpm)}'
                ]
            )
            installCommands.append(command)
            command = []
            command.extend(self.sshOpts)
            command.extend(
                [
                    f"{self.user}@{ip}",
                    'sudo rpm -q {'+','.join(self.rpmShort)+'}'
                ]
            )
            installVerify.append(command)
        for command in installCommands:
            installProc.append(installPool.map_async(run_shell_command,(command,)))
        try:
            [p.wait(TIMEOUT) for p in installProc]
        except TimeoutError:
            return False
        results = [p.get() for p in installProc]
        for result in results:
            out,err,code,cmd = result[0]
            if code == 1:
                logging.critical(f"Error in install : {cmd}")
                logging.critical(f"Output : {out}")
                logging.critical(f"Error : {err}")
                failedCmd.append(f"{CYELLOW}Command{CEND} : \n{cmd}\n")
                failedCmd.append(f"{CYELLOW}Output{CEND} : \n{out}\n")
                failedCmd.append(f"{CRED}Error{CEND} : \n{err}")
            else:
                logging.info(f"Command {cmd} successful")
        for command in installVerify:
            verifyProc.append(verifyPool.map_async(run_shell_command,(command,)))
        try:
            [p.wait(TIMEOUT) for p in verifyProc]
        except TimeoutError:
            return False
        results = [p.get() for p in verifyProc]
        for result in results:
            out,err,code,cmd = result[0]
            for line in out.splitlines():
                if 'not' in line:
                    logging.critical(f"Error in install : {' '.join(cmd)}")
                    logging.critical(f"Output : {out}")
                    logging.critical(f"Error : {err}")
                    failedCmd.append(f"{CYELLOW}Command{CEND} : \n{cmd}\n")
                    failedCmd.append(f"{CYELLOW}Output{CEND} : \n{out}\n")
                    failedCmd.append(f"{CRED}Error{CEND} : \n{err}")
            else:
                logging.info(f"Verification command {cmd} successful")
        if failedCmd:
            failedCmd.append(f"\n{CRED}Install Failures{CEND}\nCheck that the user has sudo permissions to install packages on all nodes and that the nodes are not out of disk space")
            state = False
        return state,failedCmd

    def cleanupPackages(self,test:bool=False) -> bool | list:
        """
        cleanupPackages cleans up the installation artifacts after the RPM packages have been installed.

        This method uses multiprocessing to run cleanup commands on each node to remove the RPM files and installation artifacts.
        It returns a boolean indicating success or failure, and a list of failed commands if any.

        Args:
            test (bool, optional): Defines if unit testing in execution. Defaults to False.

        Returns:
            bool | list: Returns True if all cleanup commands were executed successfully, otherwise returns a list of failed commands.
                         The list contains formatted strings with the command, output, and error messages for each failed command.
        """
        cleanupPool = mp.Pool()
        cleanupCommand = []
        cleanupProc = []
        state = True
        for ip in self.hostips:
            command = []
            command.extend(self.sshOpts)
            command.extend(
                [
                    f"{self.user}@{ip}",
                    'sudo rm /tmp/{'+",".join(self.rpms)+'}'
                ]
            )
            cleanupCommand.append(command)
            command = []
            command.extend(self.sshOpts)
            command.extend(
                [
                    f"{self.user}@{ip}",
                    'sudo rm -rf ./{manifest.json,install,st-*.rpm}'
                ]
            )
            cleanupCommand.append(command)
            command = []
            command.extend(self.sshOpts)
            command.extend(
                [
                    f"{self.user}@{ip}",
                    'mv ./support_toolbox_installer.INFO /cohesity_logs/support-toolbox/support_toolbox_installer.INFO;sudo chown cohesity: /cohesity_logs/support-toolbox/support_toolbox_installer.INFO'
                ]
            )
            cleanupCommand.append(command)
        for command in cleanupCommand:
            cleanupProc.append(cleanupPool.map_async(run_shell_command,(command,)))
        try:
            [p.wait(TIMEOUT) for p in cleanupProc]
        except TimeoutError:
            raise
        results = [p.get() for p in cleanupProc]
        return state,[]

    def check(self,test:bool=False) -> bool:
        """
        check Validates package integrity by checking the SHA256 hashes of the installed RPMs against the manifest.

        This method computes the SHA256 hash of each RPM file and compares it with the expected hash from the manifest.

        Args:
            test (bool, optional): Defines if unit testing in execution. Defaults to False.

        Returns:
            bool: Returns True if all hashes match, otherwise returns False.
        """
        sha256 = hashlib.sha256()
        for rpm in self.rpms:
            with open(rpm,"r") as r:
                data = r.read()
                hash = sha256.hexdigest(data)
                r.close()
            if hash != self.manifest[rpm]:
                print(f"{CRED}Error{CEND} : {CYELLOW}SHA256 verifcation failed{CEND}")
                print(f"{CYELLOW}Origin Hash{CEND} : {CBLUE}{self.manifest[rpm]}{CEND}")
                print(f"{CYELLOW}Local Hash{CEND} : {CRED}{hash}{CEND}")
                return False

#Function that provides the blinking cursor for the TUI
def blink(output:str,len:int,test:bool=False) -> None:
    """
    blink provides a blinking cursor for the TUI

    This function continuously updates the output string in the terminal to create a blinking effect.

    Args:
        output (str): Description of the output string to be displayed.
        len (int): Length of the output string to determine spacing.
        test (bool, optional): Defines if unit testing in execution. Defaults to False.
    """
    while True:
        sys.stdout.write(f'\r{output} ─ {"":>{87-len}}│')
        sys.stdout.flush()
        time.sleep(.5)
        sys.stdout.write(f'\r{output} ╲ {"":>{87-len}}│')
        sys.stdout.flush()
        time.sleep(.5)
        sys.stdout.write(f'\r{output} │ {"":>{87-len}}│')
        sys.stdout.flush()
        time.sleep(.5)
        sys.stdout.write(f'\r{output} ╱ {"":>{87-len}}│')
        sys.stdout.flush()
        time.sleep(.5)

#Defines color and pushes output to screen when we do not want to use print
def output(output:str,base:str='',new:bool=False,color:str='',end:bool=False,test:bool=False) -> None:
    """
    output Defines the output format and prints it to the console.

    This function formats the output string with specified colors and alignment, and prints it to the console.

    Args:
        output (str): Description of the output string to be displayed.
        base (str, optional): Base string to add or modify. Defaults to ''.
        new (bool, optional): Defines if a new line is needed at the end. Defaults to False.
        color (str, optional): Defines which color to convert output. Defaults to ''.
        end (bool, optional): Defines if the method has ended and to print - Defaults to False.
        test (bool, optional): Defines if unit testing in execution. Defaults to False.
    """
    if color == 'RED':
        output = f'{CRED} {output}{CEND}'
    if color == 'BLUE':
        output = f'{CBLUE} {output}{CEND}'
    if color == 'YELOW':
        output = f'{CYELLOW} {output}{CEND}'
    if color == 'GREEN':
        output = f'{CGREEN} {output}{CEND}'
    if end == True :
        i = 97 - len(base)
        output = f'\r{base} {output:->{i}}'
    sys.stdout.write(output)
    sys.stdout.flush()
    if new == True:
        print('')

def failInstall(failOutput:list,local:str) -> None:
    """
    failInstall handles the failure of the installation process.

    This function prints an error message indicating that one or more nodes have reported issues during installation.
    It also provides instructions to run the installation again after resolving the issues on the nodes.
    It skips the cleanup process and prints the log file location for further investigation.

    Args:
        failOutput (list): List of failure messages to be printed.
        local (str): Local directory where the log file is stored.
    """
    print('\nOne or more nodes have reported issues installing')
    print('Please run install again after resolving issues on the nodes')
    print(f'{CYELLOW}Skipping Cleanup! {CEND}')
    print(f'Log : {CRED} {local}/support_toolbox_installer.INFO {CEND}\n')
    print(f'\n'.join(failOutput))
    print(f'{CRED}Install Complete, with Errors{CEND}')
    exit(1)

#Fresh install main function
def main(**kwargs:dict) -> None:
    """
    main is the entry point for the installation process.

    This function initializes the installer, sets up the multiprocessing pool, and orchestrates the installation steps.
    It handles the pushing of packages, installation on nodes, and cleanup of installation artifacts.
    It also provides real-time feedback to the user through the console, including success and failure messages.
    """
    mainPool = pool.ThreadPool()
    installer = Installer(kwargs)
    divider = "─" * 80
    failOutput = []
    print(f'\n{CBLUE}Installing {installer.package}{CEND} : {CYELLOW}{installer.version}{CEND}')
    print(f'{CBLUE}Local Node IP {CEND}: {CYELLOW}{get_cluster_ips()[-1]}{CEND}')
    print(f'{CBLUE}SSH Port{CEND} : {CYELLOW}{kwargs["port"]}{CEND}')
    print(f'{"":{29}}{CYELLOW}Installing{CEND} :')
    for rpm in installer.rpmPackages:
        print(f'{"":{29}}{CYELLOW}{rpm}{CEND}')
    print(f'┌{"":─>{80}}┐')
    step_out = f'│ {CYELLOW}Copying package to all nodes in the cluster{CEND}'
    result = mainPool.apply_async(installer.pushPackages)
    dyn_out = mp.Process(target=blink, args=(step_out,len(step_out)))
    dyn_out.start()
    try:
        out,fail = result.get(timeout=TIMEOUT)
    except mp.TimeoutError:
        dyn_out.terminate()
        output('TIMEOUT',base=step_out,new=True,color='BLUE',end=True)
        print(f'└{"":─>80}┘')
        failInstall(['Timeout during package push'],installer.here)
    dyn_out.terminate()
    if out == False:
        failOutput.append(f"{CRED}Push Failures{CEND}")
        failOutput.append(divider)
        for f in fail:
            failOutput.append(f)
        failOutput.append(f"{divider}")
        output('FAIL',base=step_out,new=True,color='RED',end=True)
        print(f'└{"":─>80}┘')
        failInstall(failOutput,installer.here)
    else:
        output('OK',base=step_out,new=True,color='GREEN',end=True)
    print(f'├{"":─>{80}}┤')
    step_out = f'│ {CYELLOW}Deploying package on all nodes in the cluster{CEND}'
    result = mainPool.apply_async(installer.install)
    dyn_out = mp.Process(target=blink, args=(step_out,len(step_out)))
    dyn_out.start()
    try:
        out,fail = result.get(timeout=TIMEOUT)
    except mp.TimeoutError:
        dyn_out.terminate()
        output('TIMEOUT',base=step_out,new=True,color='BLUE',end=True)
        print(f'└{"":─>80}┘')
        failInstall(['Timeout during package install'],installer.here)
    dyn_out.terminate()
    if out == False:
        failOutput.append(f"{CRED}Install Failures{CEND}")
        failOutput.append(f"{divider}")
        for f in fail:
            failOutput.append(f)
        failOutput.append(f"{divider}")
        output('FAIL',base=step_out,new=True,color='RED',end=True)
        failInstall(failOutput,installer.here)
    else:
        output('OK',base=step_out,new=True,color='GREEN',end=True)
    print(f'├{"":─>{80}}┤')
    step_out = f'│ {CYELLOW}Cleaning up install artifacts{CEND}'
    result = mainPool.apply_async(installer.cleanupPackages)
    dyn_out = mp.Process(target=blink, args=(step_out,len(step_out)))
    dyn_out.start()
    try:
        out = result.get(timeout=TIMEOUT)
    except TimeoutError:
        dyn_out.terminate()
        output('TIMEOUT',base=step_out,new=True,color='BLUE',end=True)
        print(f'└{"":─>80}┘')
        failInstall(['Timeout during cleanup'],installer.here)
    dyn_out.terminate()
    output('OK',base=step_out,new=True,color='GREEN',end=True)
    print(f'└{"":─>80}┘')
    print(f'  {CGREEN}Install Complete{CEND}')
    print(f'  {CGREEN}Logs saved to {CEND}: /cohesity_logs/support-toolbox/support_toolbox_installer.INFO\n')
    print(f'  {CBLUE}Environment Variables may not be active{CEND}')
    print(f'  {CBLUE}To fix run{CEND} : {CYELLOW}source ~/.bashrc {CEND}\n')

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    portParser = parser.add_argument("-p","--port",default=2222)
    args, _ = parser.parse_known_args()
    args = vars(args)

    signal(SIGINT, sigint_handler)
    signal(SIGTSTP, sigtstp_handler)
    main(**args)