|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +from datetime import datetime |
| 4 | +import tarfile |
| 5 | +import sys |
| 6 | +import argparse |
| 7 | +import shutil |
| 8 | +import logging |
| 9 | + |
| 10 | +class SoftwareUpgrade: |
| 11 | + def __init__(self): |
| 12 | + now = datetime.now() |
| 13 | + timestamp = now.strftime("%Y%m%d%H%M%S") |
| 14 | + log_filename = 'upgrade_sw_{}.log'.format(timestamp) |
| 15 | + logging.basicConfig(filename=log_filename, filemode='w', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') |
| 16 | + |
| 17 | + def run(self, args): |
| 18 | + swtype = args.swtype |
| 19 | + installation_dir = args.installdir |
| 20 | + softwarepath = args.softwarepath |
| 21 | + backupdir = args.backupdir |
| 22 | + now = datetime.now() |
| 23 | + timestamp = now.strftime("%Y-%m-%d %H:%M:%S") |
| 24 | + logging.info('Starting Upgrade of {} at:{}'.format(swtype, timestamp)) |
| 25 | + logging.info('Software to Upgrade:{}'.format(swtype)) |
| 26 | + logging.info('Software Path:{}'.format(softwarepath)) |
| 27 | + logging.info('Backup Directory:{}'.format(backupdir)) |
| 28 | + logging.info('Installation Directory:{}'.format(installation_dir)) |
| 29 | + |
| 30 | + self.verify_version(installation_dir, swtype) |
| 31 | + self.stop_engine(installation_dir, swtype) |
| 32 | + self.tar_gz_folders(installation_dir, swtype, backupdir) |
| 33 | + self.upgrade_software(installation_dir, softwarepath, backupdir, swtype) |
| 34 | + self.start_engine(installation_dir, swtype) |
| 35 | + logging.info("Version check after upgrade") |
| 36 | + self.verify_version(installation_dir, swtype) |
| 37 | + |
| 38 | + def execute_command(self, command, cwd=None): |
| 39 | + if cwd: |
| 40 | + os.chdir(cwd) |
| 41 | + process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) |
| 42 | + output, error_output = process.communicate() |
| 43 | + return output, error_output |
| 44 | + |
| 45 | + def stop_engine(self, installation_dir, swtype): |
| 46 | + cwd = os.path.join(installation_dir, "bin") |
| 47 | + if swtype == "ssp": |
| 48 | + command = "./stopEngine.sh mode=auto" |
| 49 | + elif swtype == "sspcm": |
| 50 | + command = "./stopCM.sh mode=auto" |
| 51 | + elif swtype == "seas": |
| 52 | + command = "./stopSeas.sh mode=auto" |
| 53 | + elif swtype == "rps": |
| 54 | + command = "./stopPs.sh" |
| 55 | + output, error_output = self.execute_command(command, cwd) |
| 56 | + logging.info('Output:\n{}'.format(output)) |
| 57 | + logging.info('Error if any:\n{}'.format(error_output)) |
| 58 | + |
| 59 | + def tar_gz_folders(self, installation_dir, swtype, backupdir): |
| 60 | + logging.info("Creating a software backup:{}".format(swtype.upper())) |
| 61 | + current_datetime = datetime.now() |
| 62 | + formatted_datetime = current_datetime.strftime("%y%m%d%H%M%S%f") |
| 63 | + tar_filename = "bkp_{}_{}.tar.gz".format(swtype, formatted_datetime) |
| 64 | + with tarfile.open(os.path.join(backupdir, tar_filename), "w:gz") as tar: |
| 65 | + tar.add(installation_dir, arcname=".") |
| 66 | + logging.info("Software backup created:{}".format(swtype)) |
| 67 | + |
| 68 | + def upgrade_software(self, installation_dir, softwarepath, backupdir, swtype): |
| 69 | + filename = os.path.basename(softwarepath) |
| 70 | + swdirectory = os.path.dirname(softwarepath) |
| 71 | + stdout_logfile = "{}_upgrade_out.log".format(swtype) |
| 72 | + stderr_logfile = "{}_upgrade_err.log".format(swtype) |
| 73 | + os.chdir(swdirectory) |
| 74 | + try: |
| 75 | + with open(os.path.join(backupdir, stdout_logfile), 'w') as stdout_file, open(os.path.join(backupdir, stderr_logfile), 'w') as stderr_file: |
| 76 | + command = './{}'.format(filename) |
| 77 | + process = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE, stdout=stdout_file, stderr=stderr_file) |
| 78 | + input_values = "\n1\n{}\nY\nC\n\n\n\n".format(installation_dir) if swtype != "rps" else "{}\nY\n1\n\n\n\n".format(installation_dir) |
| 79 | + process.communicate(input=input_values) |
| 80 | + if process.returncode == 0: |
| 81 | + logging.info('Upgradation Completed ') |
| 82 | + except Exception as e: |
| 83 | + logging.error("Error: {}".format(e)) |
| 84 | + |
| 85 | + def start_engine(self, installation_dir, swtype): |
| 86 | + cwd = os.path.join(installation_dir, "bin") |
| 87 | + if swtype == "ssp": |
| 88 | + command = "./startEngine.sh" |
| 89 | + elif swtype == "sspcm": |
| 90 | + command = "./startCM.sh" |
| 91 | + elif swtype == "seas": |
| 92 | + command = "./startSeas.sh" |
| 93 | + elif swtype == "rps": |
| 94 | + cwd = installation_dir |
| 95 | + command = "./startupPs.sh" |
| 96 | + output, error_output = self.execute_command(command, cwd) |
| 97 | + logging.info('Output:\n{}'.format(output)) |
| 98 | + logging.info('Error if any:\n{}'.format(error_output)) |
| 99 | + |
| 100 | + def verify_version(self, installation_dir, swtype): |
| 101 | + cwd = os.path.join(installation_dir, "conf") if swtype in ['ssp', 'sspcm', 'seas'] else installation_dir |
| 102 | + command = 'cat version.xml' |
| 103 | + output, error_output = self.execute_command(command, cwd) |
| 104 | + logging.info('Current Version of {}'.format(swtype)) |
| 105 | + logging.info(output) |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + parser = argparse.ArgumentParser(description="Automation of Software Upgrade") |
| 109 | + parser.add_argument("-swtype", "-t", type=str, help="Name of Application") |
| 110 | + parser.add_argument("-installdir", "-i", type=str, help="Installation Path") |
| 111 | + parser.add_argument("-softwarepath", "-s", type=str, help="Software Path") |
| 112 | + parser.add_argument("-backupdir", "-b", type=str, help="Backup Path") |
| 113 | + parser.add_argument("-verbose", "-v", action="store_true", help="Verbose") |
| 114 | + args = parser.parse_args() |
| 115 | + swupgrade = SoftwareUpgrade() |
| 116 | + swupgrade.run(args) |
0 commit comments