def init(applicationResourceFile, loggerName):
    if os.geteuid() != 0:
        print RED + 'You must be root to run this program.' + RESETCOLORS
        exit(1)
    usage = 'usage: %prog [[-a] [-k] [-o] [-p]] [-v] [-h]'
    parser = optparse.OptionParser(usage=usage)
    parser.add_option(
        '-a',
        action='store_true',
        default=False,
        help=
        'This option will result in the application of both OS and Kernel patches.'
    )
    parser.add_option(
        '-k',
        action='store_true',
        default=False,
        help='This option will result in the application of Kernel patches.')
    parser.add_option(
        '-o',
        action='store_true',
        default=False,
        help='This option will result in the application of OS patches.')
    parser.add_option(
        '-p',
        action='store_true',
        default=False,
        help='This option is used to perform the post update tasks.')
    parser.add_option(
        '-v',
        action='store_true',
        default=False,
        help="This option is used to display the application's version.")
    options, args = parser.parse_args()
    applicationName = os.path.basename(sys.argv[0])
    applicationVersion = 'v1.8-10'
    if options.v:
        print applicationName + ' ' + applicationVersion
        exit(0)
    if options.a and options.k or options.a and options.o or options.k and options.o or options.a and options.p or options.o and options.p or options.k and options.p:
        parser.error('Options -a, -k, -o and -p are mutually exclusive.')
    if not options.a and not options.k and not options.o and not options.p:
        parser.error("Try '" + applicationName +
                     " -h' to get command specific help.")
    if options.p:
        print GREEN + 'Phase 1: Initializing for the post system update.' + RESETCOLORS
    else:
        print GREEN + 'Phase 1: Initializing for the system update.' + RESETCOLORS
    patchResourceDict = {}
    patchResourceDict['options'] = options
    try:
        with open(applicationResourceFile) as f:
            for line in f:
                line = line.strip()
                line = re.sub('[\'"]', '', line)
                if len(line) == 0 or re.match('^\\s*#', line) or re.match(
                        '^\\s+$', line):
                    continue
                else:
                    key, val = line.split('=')
                    key = key.strip()
                    patchResourceDict[key] = val.strip()

    except IOError as err:
        print RED + "Unable to access the application's resource file " + applicationResourceFile + '.\n' + str(
            err) + '\n' + RESETCOLORS
        exit(1)

    try:
        logBaseDir = re.sub('\\s+', '',
                            patchResourceDict['logBaseDir']).rstrip('/')
        patchApplicationLog = re.sub('\\s+', '',
                                     patchResourceDict['patchApplicationLog'])
        patchApplicationLog = logBaseDir + '/' + patchApplicationLog
    except KeyError as err:
        print RED + 'The resource key (' + str(
            err
        ) + ') was not present in the resource file; exiting program execution.' + '\n' + RESETCOLORS
        exit(1)

    if not options.p:
        try:
            logList = os.listdir(logBaseDir)
            for log in logList:
                os.remove(logBaseDir + '/' + log)

        except OSError as err:
            print RED + 'Unable to remove old logs in ' + logBaseDir + '; exiting program execution.\n' + str(
                err) + '\n' + RESETCOLORS
            exit(1)

    handler = logging.FileHandler(patchApplicationLog)
    logger = logging.getLogger(loggerName)
    logger.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(message)s',
                                  datefmt='%m/%d/%Y %H:%M:%S')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    if options.p:
        return patchResourceDict
    logger.info(applicationName + ' ' + applicationVersion)
    try:
        if len(patchResourceDict['rpmsToRemove']) != 0:
            patchResourceDict['removeRPMs'] = True
        else:
            patchResourceDict['removeRPMs'] = False
        if len(patchResourceDict['rpmsToAdd']) != 0:
            patchResourceDict['addRPMs'] = True
        else:
            patchResourceDict['addRPMs'] = False
    except KeyError as err:
        logger.error('The resource key (' + str(err) +
                     ') was not present in the resource file.')
        print RED + 'A resource key was not present in the resource file, check the log file for errors; exiting program execution.' + '\n' + RESETCOLORS
        exit(1)

    checkDiskSpace(loggerName)
    osDistLevel = checkOSVersion(patchResourceDict.copy(), loggerName)
    patchResourceDict['osDistLevel'] = osDistLevel
    command = 'dmidecode -s system-product-name'
    result = subprocess.Popen(command,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE,
                              shell=True)
    out, err = result.communicate()
    logger.info('The output of the command (' + command +
                ") used to get the system's model was: " + out.strip())
    if result.returncode != 0:
        logger.error("Unable to get the system's model information.\n" + err)
        print RED + "\nUnable to get the system's model information; check the log file for errors; exiting program execution.\n" + RESETCOLORS
        exit(1)
    systemModel = re.match('[a-z,0-9]+\\s+(.*)', out,
                           re.IGNORECASE).group(1).replace(' ', '')
    logger.info("The system's model was determined to be: " + systemModel +
                '.')
    patchResourceDict['systemModel'] = systemModel
    if osDistLevel == 'SLES_SP1':
        sles12MultipathFix(loggerName)
    if systemModel == 'DL580Gen9' and osDistLevel == 'SLES_SP1':
        if checkStorage(loggerName) and checkForMellanox(loggerName):
            removeMellanoxDriver(loggerName)
            mlx4Configure(loggerName)
    if '16s' in systemModel:
        cpuModel = getProcessorType(loggerName)
        if cpuModel == 'broadwell' and not osDistLevel == 'SLES_SP4':
            updateFstabTimeout(loggerName)
    if options.a or options.k:
        if systemModel == 'DL580G7' or systemModel == 'DL980G7' or systemModel == 'DL380pGen8' or systemModel == 'DL360pGen8' or systemModel == 'DL360Gen9':
            patchResourceDict['postUpdateRequired'] = 'yes'
            try:
                logBaseDir = re.sub(
                    '\\s+', '', patchResourceDict['logBaseDir']).rstrip('/')
                postUpdateResumeLog = re.sub(
                    '\\s+', '', patchResourceDict['postUpdateResumeLog'])
                postUpdateResumeLog = logBaseDir + '/' + postUpdateResumeLog
            except KeyError as err:
                logger.error('The resource key (' + str(err) +
                             ') was not present in the resource file.')
                print RED + 'The resource key for the post update resume log was not present in the resource file; check the log file for errors; exiting program execution.' + RESETCOLORS
                exit(1)

            try:
                f = open(postUpdateResumeLog, 'a')
                if systemModel == 'DL380pGen8' or systemModel == 'DL360pGen8' or systemModel == 'DL360Gen9':
                    f.write('isServiceguardSystem = yes\n')
                else:
                    f.write('isServiceguardSystem = no\n')
                if systemModel == 'DL580G7' or systemModel == 'DL980G7':
                    f.write('isFusionIOSystem = yes\n')
                    removeFusionIOPackages(patchResourceDict.copy(),
                                           loggerName)
                else:
                    f.write('isFusionIOSystem = no\n')
                f.close()
            except IOError as err:
                logger.error('Unable to access the post update resume log.\n' +
                             str(err))
                print RED + 'Unable to access the post update resume log; check the log file for errors; exiting program execution.' + RESETCOLORS
                exit(1)

        else:
            patchResourceDict['postUpdateRequired'] = 'no'
    else:
        patchResourceDict['postUpdateRequired'] = 'no'
    if (options.a
            or options.o) and patchResourceDict['suseSLESSAPReleaseRPM'] != '':
        command = 'rpm -q SUSE_SLES_SAP-release'
        result = subprocess.Popen(command,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE,
                                  shell=True)
        out, err = result.communicate()
        if result.returncode == 0:
            updateSUSE_SLES_SAPRelease(patchResourceDict.copy(), loggerName)
        elif 'package SUSE_SLES_SAP-release is not installed' not in out:
            logger.error(
                'Unable to determine if the SUSE_SLES_SAP-release RPM is installed.\n'
                + out)
            print RED + 'Unable to determine if the SUSE_SLES_SAP-release RPM is installed; check the log file for errors; exiting program execution.' + RESETCOLORS
            exit(1)
    patchDirList = setPatchDirectories(patchResourceDict.copy(), loggerName)
    configureRepositories(patchDirList[:], loggerName)
    repositoryList = []
    for dir in patchDirList:
        repositoryList.append(dir.split('/').pop())

    patchResourceDict['repositoryList'] = repositoryList
    updateZyppConf(loggerName)
    if osDistLevel == 'SLES_SP4':
        updateBootloaderCFG(loggerName)
    return patchResourceDict
Exemple #2
0
    configureRepositories(patchDirList[:], loggerName)  # configureRepository 
    repositoryList = []
    for dir in patchDirList:
        repositoryList.append(dir.split('/').pop())

    patchResourceDict['repositoryList'] = repositoryList

	
#from modules.updateZyppConf import updateZyppConf

	updateZyppConf(loggerName)  # updateZyppConf
    if osDistLevel == 'SLES_SP4':
	

        updateBootloaderCFG(loggerName)   # Preupgrade 5
    return patchResourceDict


def main():
    applicationResourceFile = '/hp/support/patches/resourceFiles/patchResourceFile'
    loggerName = 'patchLogger'
    patchResourceDict = init(applicationResourceFile, loggerName)
    logger = logging.getLogger(loggerName)
    options = patchResourceDict['options']
    if not options.p:
        print GREEN + 'Phase 2: Updating system with patches.' + RESETCOLORS
        osDistLevel = patchResourceDict['osDistLevel']
        if not options.k:
            if patchResourceDict['removeRPMs'] or patchResourceDict['addRPMs']:
#from modules.oneOffs import OneOffs