#!/usr/bin/env python
'''
Install storage drivers for Linux
==================================
- Python 2.6 compatible for RHEL6.

https://github.rackspace.com/SupportTools/rs-managed-storage

@copyright: Copyright (c) 2016 Rackspace US, Inc.
All rights reserved - Do Not Redistribute
'''

import sys
import optparse
import logging
import re
import ConfigParser
import platform
import shlex
import glob
import urllib2
import urlparse
import time
import os
import signal
from pipes import quote
from subprocess import Popen, PIPE
from distutils.version import LooseVersion
from shutil import copyfile

__version__ = '3.2.0'
RAX_MIRROR_HOST = 'https://rax.mirror.rackspace.com'
PUBIP_CHECK_HOST = 'https://ipv4.icanhazip.com'

# CLI Color Codes
OKGREEN = '\033[32m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'


class RExit(object):
    ''' Bash-style exit codes for rs-managed-storage
    '''
    # Normal exit
    OK                     = 0   # noqa: E221
    # Unspecified Error
    ERR_GENERAL            = 1   # noqa: E221
    # Kernel upgraded, reboot required
    ERR_REBOOT_NEEDED      = 2   # noqa: E221
    # Named configuration file missing
    ERR_CFG_MISSING        = 3   # noqa: E221
    # Malformed configuration file
    ERR_CFG_BAD            = 4   # noqa: E221
    # Unsupported Linux distribution
    ERR_DISTRO             = 5   # noqa: E221
    # Supported HBA hardware not found
    ERR_HW_NONE            = 6   # noqa: E221
    # Both Emulex and Brocade HBAs found (Unsupported)
    ERR_HW_MULTIPLE        = 7   # noqa: E221
    # Error downloading from upstream
    ERR_DOWNLOAD           = 8   # noqa: E221
    # Error writing downloaded files
    ERR_WRITE              = 9   # noqa: E221
    # Unable to stop PowerPath
    ERR_STOP_PP            = 10  # noqa: E221
    # Unable to stop Services (generic)
    ERR_STOP_SVC           = 11  # noqa: E221
    # Error running Brocade uninstall script
    ERR_BCU_UNINSTALL      = 12  # noqa: E221
    # Error running Brocade installation script
    ERR_BCU_INSTALL        = 13  # noqa: E221
    # Correct storage software already installed
    ERR_ALREADY_INSTALLED  = 14  # noqa: E221
    # Not enough space free in /boot to install
    ERR_SPACE_BOOT         = 15  # noqa: E221
    # Cannot find best kernel suitable for this configuration
    ERR_BAD_KERNEL         = 16  # noqa: E221
    # Unusual Brocade module found, manual intervention required
    ERR_BAD_MODULE         = 17  # noqa: E221
    # Error finding vmlinuz kernel just installed
    ERR_BAD_VMLINUZ        = 18  # noqa: E221
    # Error using grubby to set kernel just installed to boot
    ERR_GRUBBY             = 19  # noqa: E221
    # Unable to start PowerPath
    ERR_START_PP           = 20  # noqa: E221
    # Bad commandline options / combinations
    ERR_USAGE              = 21  # noqa: E221
    # User cancelled from keyboard (SIGINT, SIGQUIT)
    ERR_USR_CANCELLED      = 22  # noqa: E221
    # Process terminated (SIGTERM)
    ERR_TERMINATED         = 23  # noqa: E221


class StorageConfig(object):
    ''' INI file parser
    '''

    def __init__(self, distro, config_file=None, cache=True):
        self.distro = distro
        self.use_cache = cache
        self.config_parser = ConfigParser.SafeConfigParser()
        self.version = None
        self.latest = None
        self.latest_kernel = None
        self.config_file = config_file
        self.Host = RAX_MIRROR_HOST
        self.Path = "/storagecenter/emcClariion/Installation/Linux/"
        self.baseURL = urlparse.urljoin(self.Host, self.Path)
        self.configURL = urlparse.urljoin(self.baseURL,
                                          "rs-managed-storage-config.ini")
        self.hba_driver = None

        if not config_file:
            logging.debug("Downloading config %s" % self.configURL)
            self.config_file = self.download_config()
        else:
            logging.debug("Using config file %s" % self.config_file)
            self.config_file = config_file

        self.config_parser.read(self.config_file)
        self.version = self.__config_parser_get("GENERAL", "VERSION")
        if not self.version:
            logging.error("Malformed config file. Cannot find version number.")
            sys.exit(RExit.ERR_CFG_BAD)
        logging.debug("Loaded config file %s version %s" % (self.config_file,
                                                            self.version))
        self.latest = self.__config_parser_get(
            "LATEST-BROCADE", "%s-LATEST" % self.distro)
        if not self.latest:
            logging.error("Malformed config file. Cannot find release number.")
            sys.exit(RExit.ERR_CFG_BAD)
        self.latest_kernel = self.latest.replace(self.distro + "-", "")
        # Blacklist is optional, missing in config is OK
        #  [BLACKLIST]
        #  KERNELS=3.10.0-693.2.1.el7, 3.10.0-693.1.1.el7
        self.blacklist = []
        __tmp = self.__config_parser_get('BLACKLIST', 'KERNELS')
        if __tmp:
            self.blacklist = [k.strip() for k in __tmp.split(',')]

    def download_config(self):
        base_url = urlparse.urljoin(self.Host, self.Path)
        config_url = urlparse.urljoin(
            base_url, "rs-managed-storage-config.ini")

        config_file = self.download_url(config_url)
        return os.path.abspath(config_file)

    def get_best_kernel(self, server_kernels, target):
        ''' Given a high-low sorted list of server kernel versions, return the
            highest version number in that list that matches target.

            Example:
            Target kernel: [2.6.32-642]
            Server kernels: [2.6.32-696.1.1.el6, 2.6.32-696.el6,
            2.6.32-642.15.1.el6]

            Should set the best kernel to 2.6.32-642.15.1.el6 and best target
            to 2.6.32-642
        '''
        if not isinstance(target, list):
            target = [str(target)]
        # Sort target high-low.
        target = sorted(target, reverse=True)
        # Find the first kernel in config that is in server_kernels.
        best_target = best_kernel = None
        try:
            for version in target:
                logging.debug("Finding best kernel for target %s" % version)
                for kernel in server_kernels:
                    logging.debug("- Checking kernel %s" % kernel)
                    if kernel in self.blacklist:
                        logging.debug("Kernel %s is blacklisted, skipping"
                                      % kernel)
                        continue
                    if kernel.startswith(version):
                        best_target = version
                        best_kernel = kernel
                        logging.debug(("Found best kernel %s " % best_kernel +
                                       "in target %s" % best_target))
                        raise StopIteration
        except StopIteration:
            pass

        return (best_target, best_kernel)

    def __config_parser_get(self, section, option):
        try:
            return self.config_parser.get(section, option)
        except ConfigParser.NoOptionError:
            return None
        except ConfigParser.NoSectionError:
            return None

    def __config_items(self, section):
        return self.config_parser.items(section)

    def get_package_list(self, kernel):
        section = self.distro + '-' + kernel
        return self.__config_items(section)

    def download_url(self, url, filename=None):
        if not filename:
            # urlparse.urlsplit().path was added in python 2.5
            path = urlparse.urlsplit(url)[2]
            filename = path.split('/')[-1]
        if self.use_cache and os.path.exists(filename):
            cache_expire_seconds = (60 * 60 * 24)  # 24 hours
            file_age = time.time() - os.path.getmtime(filename)
            if file_age < cache_expire_seconds:
                logging.debug("Using cached copy of %s" % filename)
                return filename
            else:
                logging.debug("Cached copy of %s has expired" % filename)
        logging.debug("Attempting to download %s to %s" % (url, filename))
        try:
            u = urllib2.urlopen(url)
        except urllib2.URLError, e:  # noqa: E999 (trips flake8 on py3 only)
            logging.error("Error downloading url %s" % url)
            logging.error(e)
            sys.exit(RExit.ERR_DOWNLOAD)
        try:
            fh = open(filename, "wb")
            fh.write(u.read())
            fh.close()
        except IOError, e:
            logging.error("Error writing file %s: %s" % (filename, e))
            sys.exit(RExit.ERR_WRITE)
        return filename

    def download_package(self, package):
        ''' Return filename of locally downloaded package file
        '''
        packageURL = urlparse.urljoin(self.baseURL, package)
        return self.download_url(packageURL)

    def get_kernel_targets(self, filterby=None, hbadriver=False):
        ''' Return a list of kernel targets from the configs
        '''
        distro = filterby
        if not distro:
            distro = self.distro
        ret = list()
        sections = self.config_parser.sections()
        sections.remove("LATEST-BROCADE")
        for section in sections:
            if section.startswith(distro):
                if (self.__config_parser_get(section, self.hba_driver) !=
                        "Unsupported"):
                    # Remove distro to get kernel target version.
                    section = section.replace(distro + "-", "")
                    ret.append(section)
                else:
                    logging.debug('%s is Unsupported for %s' %
                                  (section, self.hba_driver))
        return ret

    def kernel_matches_target_kernel(self, server_kernel, target_kernel):
        return server_kernel.startswith(target_kernel)


class Server(object):
    ''' (Mostly) abstract class for gathering details and running commands on a
    server.
    '''

    def __init__(self):
        self.running_kernel = self.get_running_kernel()
        self.storage_details = {
            'brocade': False,
            'emulex': False
        }
        # These keys are not secret.
        # Plaintext keys can be obtained using: emcpreg -list
        # PowerPath core feature enablement for all HBAs and paths
        self.ppkey = "BXP2-9B4S-LF69-QOXM-MM9O-WXWM"
        # PowerPath feature enablement for live LUN migrations between arrays
        self.livekey = "9VB2-MIJQ-M723-MZX2-HZMC-VAM6"
        self.dry_run = False
        self.detect_storage_hardware()

        # The kernel only exposes this if currently booted into EFI mode;
        # grub / grubby do not understand the concept of operating in EFI
        # *and* BIOS mode at the same time, it's one or the other only
        self.grubby_args = ''
        if os.path.isdir('/sys/firmware/efi'):
            self.grubby_args = '--efi'

        # Find all real GRUB config files for use later
        self.grub_cfglist = []
        self.find_grub_configs()

    def find_grub_configs(self):
        ''' Find all real GRUB config files on disk out of the possibilities
        '''
        # We're following Red Hat's lead from /sbin/new-kernel-pkg which is
        # run after every kernel RPM upgrade; it looks for both files and
        # operates on both (either/and) and not just one (either/or); as well
        # as pulling from the grubby source code list of configs
        grub_candidates = ['/etc/grub2-efi.cfg', '/etc/grub2.cfg',
                           '/boot/grub2/grub.cfg', '/boot/grub2-efi/grub.cfg',
                           '/boot/grub/grub.cfg', '/boot/grub/grub.conf']
        # loop through all candidates, resolve symlinks and append if not
        # already in the valid list of real files on disk
        for gfile in grub_candidates:
            # realpath() will resolve all symlinks into their fully proper
            # target path for us, real files are fine too. It does not
            # validate the file/path actually exist, though. so...
            gpath = os.path.realpath(gfile)
            # ...we test if it's actually there by hand
            if os.path.isfile(gpath) and gpath not in self.grub_cfglist:
                self.grub_cfglist.append(gpath)

    def detect_storage_hardware(self):
        ''' Uses lspci output to detect whether the server has Brocade or
        Emulex storage devices.
        '''
        brocade_id = '1657:00'
        emulex_id = '10df:e3'

        lspci = self.run_command("lspci -n")
        if emulex_id in lspci and brocade_id in lspci:
            # I don't know if this situation will ever happen, but I will code
            # for it just in case.
            logging.error("Both Emulex and Brocade hardware were found.")
            logging.error("This is an unusual situation. Exiting.")
            sys.exit(RExit.ERR_HW_MULTIPLE)
        if brocade_id in lspci:
            self.storage_details['brocade'] = True
        elif emulex_id in lspci:
            self.storage_details['emulex'] = True

    @classmethod
    def factory(self, platform):
        server = None

        (distro, version) = platform[0:2]
        distro = distro.lower()

        if distro in ['redhat', 'centos', 'oracle']:
            server = RedHat.factory(version)
        else:
            msg = "Platform '%s' is unsupported." % distro
            logging.error(msg)
            raise Exception(msg)

        return server

    def get_running_kernel(self):
        ''' Python's platform library supports getting the OS release, so this
        is a general class method.
        '''
        kernel = platform.release()
        if not kernel:
            msg = "Server release could not be determined."
            logging.error(msg)
            raise Exception(msg)
        logging.debug("Detected running kernel %s" % kernel)
        return kernel

    def install_storage(self, install_data):
        if self.storage_details['brocade']:
            result = self.install_brocade(install_data)
        elif self.storage_details['emulex']:
            result = self.install_emulex(install_data)
        return result

    def decom_storage(self):
        if self.storage_details['brocade']:
            result = self.decom_brocade()
        elif self.storage_details['emulex']:
            result = self.decom_emulex()
        return result

    def get_available_kernels(self):
        raise NotImplementedError("Subclass needs to implement this method.")

    def install_kernel(self):
        raise NotImplementedError("Subclass needs to implement this method.")

    def get_storage_details(self):
        raise NotImplementedError("Subclass needs to implement this method.")

    def install_emulex(self):
        raise NotImplementedError("Subclass needs to implement this method.")

    def install_brocade(self):
        raise NotImplementedError("Subclass needs to implement this method.")

    def decom_emulex(self):
        raise NotImplementedError("Subclass needs to implement this method.")

    def decom_brocade(self):
        raise NotImplementedError("Subclass needs to implement this method.")

    def manage_services(self, action, service):
        raise NotImplementedError("Subclass needs to implement this method.")

    def install_powerpath(self):
        raise NotImplementedError("Subclass needs to implement this method.")

    def decom_powerpath(self):
        raise NotImplementedError("Subclass needs to implement this method.")

    def update_rdblacklist(self):
        raise NotImplementedError("Subclass needs to implement this method.")

    def run_command(self, command, sanitize=True, shell=False,
                    check_dry_run=False):
        ''' Run command and capture the output
        '''
        if sanitize:
            command = shlex.split(command)
            if shell:
                if sanitize:
                    command = " ".join([quote(w) for w in command])
                else:
                    command = " ".join(command)
        logging.debug("Running command: %s" % repr(command))
        if check_dry_run and self.dry_run:
            logging.debug("=== Dry run === No output.")
            stdout = ""
        else:
            try:
                p = Popen(command, stdout=PIPE, stderr=PIPE, shell=shell)
                (stdout, stderr) = p.communicate()
            except OSError, e:
                if re.search(r"No such file or directory", str(e)):
                    raise RuntimeError("Command not found.")
            if p.returncode > 0:
                if stderr:
                    logging.debug(stdout)
                    raise RuntimeError(stderr)
                else:
                    logging.debug("stderr is blank. Sending stdout instead.")
                    raise RuntimeError(stdout)
            logging.debug("Command output:\n%s" % stdout)
        return stdout.strip()


class RedHat(Server):
    ''' Subclass for Redhat servers commands regardless of version.
        Release specific commands (like systemctl for RHEL7)
        are contained in a distro subclass.
    '''

    def __init__(self, version):
        Server.__init__(self)

        # Typically on RHEL or CentOS, the standard kernel package is used
        # named 'kernel'; however on Oracle Linux, it might be 'kernel-uek'
        # Reading this bash var tells us what it's supposed to be on a
        # this RH-like system
        self.kernelpkg = 'kernel'
        with open('/etc/sysconfig/kernel') as kp:
            for kline in kp:
                if kline.startswith('DEFAULTKERNEL'):
                    try:
                        self.kernelpkg = kline.split("=", 1)[1].strip()
                        break
                    except StandardError:
                        pass

        # This will use self.kernelpkg defined above
        self.available_kernels = self.get_available_kernels()
        self.brocade_details = self.brocade_details()

        # Break apart '3.10.0-327.36.3.el7.x86_64'
        # - almost identical to get_kernel_family() but sets discrete class
        #   level attributes which are used for other purposes later
        kparts = self.running_kernel.split("-", 1)
        self.running_major = kparts[0]
        self.running_family = int(kparts[1].split(".")[0])
        self.running_pattern = '{0}-{1}'.format(self.running_major,
                                                self.running_family)

    @classmethod
    def factory(self, version):
        server = None
        version = version.split('.')
        major = version[0]
        minor = version[1]

        logging.debug("Major version: %s, minor: %s" % (major, minor))

        if major == '6':
            server = RHEL6(minor)
        elif major == '7':
            server = RHEL7(minor)
        else:
            msg = "RHEL%d is not supported." % major
            logging.error(msg)
            raise Exception(msg)
        return server

    def get_kernel_family(self, target_kernel):
        ''' Returns the parent family of a specific kernel
        3.10.0-327.36.3.el7.x86_64 -> 3.10.0-327
        '''
        kparts = target_kernel.split("-", 1)
        if len(kparts) == 2:
            target_major = kparts[0]
            target_family = int(kparts[1].split(".")[0])
            return '{0}-{1}'.format(target_major, target_family)
        else:
            # Input has no '-' or split unexpectedly, bad input
            return target_kernel

    def get_available_kernels(self):
        ''' Returns a list of available kernels sorted high-to-low.
        Example output from yum:

        Loaded plugins: rhnplugin
        This system is receiving updates from RHN Classic or Red Hat Satellite.
        Installed Packages
        kernel.x86_64  3.10.0-229.24.2.el7  @base
        kernel.x86_64  3.10.0-229.40.1.el7  @rhel-x86_64-server-7.1.eus
        kernel.x86_64  3.10.0-229.42.2.el7  @rhel-x86_64-server-7.1.eus
        Available Packages
        kernel.x86_64  3.10.0-123.el7       rhel-x86_64-server-7.1.eus
        kernel.x86_64  3.10.0-123.1.2.el7   rhel-x86_64-server-7.1.eus
        kernel.x86_64  3.10.0-123.4.2.el7   rhel-x86_64-server-7.1.eus
        kernel.x86_64  3.10.0-123.4.4.el7   rhel-x86_64-server-7.1.eus
        kernel.x86_64  3.10.0-123.6.3.el7   rhel-x86_64-server-7.1.eus
        kernel.x86_64  3.10.0-123.8.1.el7   rhel-x86_64-server-7.1.eus
        kernel.x86_64  3.10.0-123.9.2.el7   rhel-x86_64-server-7.1.eus

        Example of correctly sorted kernels:

        3.10.0-229.20.1,
        3.10.0-229.14.1,
        3.10.0-229.11.1,
        3.10.0-229.7.2,
        3.10.0-229.4.2,
        3.10.0-229.1.2,
        3.10.0-229
        '''

        cmd = (
            "yum list available %s --showduplicates --disableexcludes=all" %
            self.kernelpkg)
        output = self.run_command(cmd)
        output = [line.strip() for line in output.split("\n")]
        kernels = [line for line in output if 'kernel' in line]
        # Grab just the kernel version and strip ".elX"
        kernels = [line.split()[1] for line in kernels]
        tail = kernels[0][-4:]
        # Reverse sort for high-to-low
        sorted_kernels = sorted(
            [LooseVersion(kernel.strip(tail)) for kernel in kernels],
            reverse=True)
        # Re-append the "tail" (e.g. ".el7")
        sorted_kernels = ["%s%s" % (kernel, tail) for kernel in sorted_kernels]
        logging.debug("Found available kernels: %s" % (
            ", ".join(sorted_kernels)))

        return sorted_kernels

    def brocade_details(self):
        ''' This function populates self.storage_details with information
        relating to the kmod-bfa package, bfa module, and bfa_driver_linux
        package.
        '''

        details = {
            'kmod_bfa': {},
            'bfa_driver_linux': {},
            'bfa_module': {},
        }

        # Check for kmod-bfa package
        cmd = "rpm -q kmod-bfa"
        try:
            self.run_command(cmd)
        except RuntimeError, e:
            logging.debug("Package not found: %s" % e)
            details['kmod_bfa']['rpm'] = False
        else:
            details['kmod_bfa']['rpm'] = True

        # Check for bfa_driver_linux package
        cmd = "rpm -q bfa_driver_linux"
        try:
            self.run_command(cmd)
        except RuntimeError, e:
            logging.debug("Package not found: %s" % e)
            details['bfa_driver_linux']['rpm'] = False
        else:
            details['bfa_driver_linux']['rpm'] = True

        # Query for the bfa driver
        cmd = "modinfo bfa -F filename"
        try:
            bfa_filename = self.run_command(cmd)
        except RuntimeError, e:
            logging.debug("No bfa modules found: %s" % e)
            details['bfa_module']['filename'] = None
            details['bfa_module']['type'] = None
        else:
            details['bfa_module']['filename'] = bfa_filename
            kmod_locations = ['weak-updates', '/extra/bfa/bfa.ko']
            # Set the type of bfa module (custom, missing, other)
            if '/scsi/bfa/bfa.ko' in bfa_filename:
                details['bfa_module']['type'] = 'stock'
                logging.debug("Found stock bfa driver: %s" % bfa_filename)
            elif any(x in bfa_filename for x in kmod_locations):
                details['bfa_module']['type'] = 'custom'
                logging.debug("Found kmod-bfa bfa driver: %s" % bfa_filename)
            else:
                details['bfa_module']['type'] = 'other'
        return details

    def brocade_sanity_check(self):
        ''' Checks for unusual or dangerous situations that this script cannot
        handle.
        '''

        result = None

        try:
            kmod_bfa_pkg = self.brocade_details['kmod_bfa']['rpm']
        except KeyError:
            kmod_bfa_pkg = False

        try:
            bfa_driver_pkg = self.brocade_details['bfa_driver_linux']['rpm']
        except KeyError:
            bfa_driver_pkg = False

        try:
            bfa_mod = self.brocade_details['bfa_module']
        except KeyError:
            bfa_mod = False

        if kmod_bfa_pkg or bfa_driver_pkg:
            rpm = True
        else:
            rpm = False

        # 1 - kmod package is found and expected custom bfa module is found
        if rpm and bfa_mod['type'] == 'custom':
            result = True

        # 2 - kmod package is found and bfa module is not found
        elif rpm and not bfa_mod:
            logging.debug("kmod package present but bfa module is not "
                          "present.")
            result = False
        # 3 - bfa module found is from stock
        elif rpm and bfa_mod['type'] == 'stock':
            result = False
        # 4 - no kmod package but custom bfa module found
        elif not rpm and bfa_mod['type'] == 'custom':
            logging.error("This is an unusual situation, the bfa package "
                          "is missing but somehow the kmod-bfa driver is "
                          "still loaded. Document and Escalate.")
            sys.exit(RExit.ERR_BAD_MODULE)
        # 5 - no kmod package, and stock bfa module found
        elif not rpm and bfa_mod['type'] == 'stock':
            result = False
        # 6 - no kmod package, bfa module is missing
        elif not rpm and not bfa_mod:
            result = False
        # 7 - kmod package is found but 'other' bfa module found
        elif rpm and bfa_mod['type'] == 'other':
            logging.error("This is an unusual situation, the bfa package "
                          "is installed but an unknown driver was found. "
                          "Document and Escalate.")
            sys.exit(RExit.ERR_BAD_MODULE)
        # 8 - no kmod package, other bfa module installed
        elif not rpm and bfa_mod['type'] == 'other':
            logging.error("This is an unusual situation, the bfa package "
                          "is not installed and an unknown bfa driver was "
                          "found. Document and Escalate.")
            sys.exit(RExit.ERR_BAD_MODULE)

        # For 6.9+ and 7.3+ rpm bfa utility is installed
        if self.is_rpm_installed('bfa_util_linux_noioctl'):
            result = True

        return result

    def emulex_sanity_check(self):
        result = None
        installed = []
        for rpm in ['elxocmcore', 'elxocmcorelibs', 'hbaapiwrapper']:
            installed.append(self.is_rpm_installed(rpm))
        if all(installed):
            result = True
        else:
            result = False
        return result

    def install_kernel(self, new_kernel, running_kernel):
        packages = []
        for package in [self.kernelpkg, "%s-devel" % self.kernelpkg]:
            packages.append("%s-%s" % (package, new_kernel))
        logging.info("Installing kernel packages %s" % " ".join(packages))
        cmd = (
            "yum install -y --showduplicates --disableexcludes=all %s" %
            " ".join(packages)
            )
        self.run_command(cmd, check_dry_run=True)

        # Handle upgrading/downgrading all kernel dependencies.
        rhel6_dependencies = ["%s-abi-whitelists" % self.kernelpkg,
                              "%s-debug" % self.kernelpkg,
                              "%s-debug-devel" % self.kernelpkg,
                              "%s-doc" % self.kernelpkg,
                              "%s-firmware" % self.kernelpkg,
                              "%s-headers" % self.kernelpkg]
        rhel7_dependencies = ["%s-abi-whitelists" % self.kernelpkg,
                              "%s-debug" % self.kernelpkg,
                              "%s-debug-devel" % self.kernelpkg,
                              "%s-doc" % self.kernelpkg,
                              "%s-headers" % self.kernelpkg,
                              "%s-tools" % self.kernelpkg,
                              "%s-tools-libs" % self.kernelpkg]
        dependencies = []
        for dep in set(rhel6_dependencies + rhel7_dependencies):
            dependencies.append("%s-%s" % (dep, new_kernel))
        if self.is_kernel_less_than(new_kernel, running_kernel):
            yum_action = "downgrade"
        else:
            yum_action = "upgrade-to"
        logging.info("Try yum %s all kernel dependencies: %s" % (yum_action,
                                                                 dependencies))
        cmd = (
            "yum %s -y --showduplicates --disableexcludes=all %s"
            % (yum_action, " ".join(dependencies))
            )
        self.run_command(cmd, check_dry_run=True)

        # Check the boot kernel
        if not self.is_kernel_boot_default(new_kernel):
            if self.dry_run:
                logging.debug(("=== Dry run === "
                               "Not setting boot kernel version"))
                return
            logging.info("Setting default boot kernel to %s" % new_kernel)
            # Get vmlinuz info needed by grubby.
            if self.dry_run:
                logging.debug("=== Dry run === Will not change boot kernel.")
            else:
                vmlinuz = glob.glob("/boot/vmlinuz-%s*" % new_kernel)
                if not vmlinuz:
                    # TODO: Use rpm -q --list to find the vmlinuz file.
                    logging.error(("Cannot find vmlinuz file %s " % vmlinuz +
                                   "to set boot kernel using grubby"))
                    sys.exit(RExit.ERR_BAD_VMLINUZ)
                vmlinuz = vmlinuz[0]
                cmd = 'grubby {0} --set-default={1}'.format(self.grubby_args,
                                                            vmlinuz)
                self.run_command(cmd, check_dry_run=True)
                if not self.is_kernel_boot_default(new_kernel):
                    logging.error("Cannot set boot kernel to %s" % new_kernel)
                    sys.exit(RExit.ERR_GRUBBY)

    def is_kernel_boot_default(self, kernel):
        ''' Is this kernel set to run on next boot?
        '''
        cmd = 'grubby {0} --default-kernel'.format(self.grubby_args)
        boot_kernel = self.run_command(cmd)
        if str(boot_kernel).find(kernel) >= 0:
            return True
        return False

    def is_kernel_less_than(self, kernel_A, kernel_B):
        ''' Is kernel_A less than kernel_B? '''
        '''
            Example RHEL kernel versions numbers:
                "3.10.0-229.4.2.el7.x86_64",
                "3.10.0-229.4.2.el7",
                "3.10.0-229.1.2.el7",
                "3.10.0-229.el7",
        '''
        # Either input may or may not contain the machine architecture on the
        # right, we'll strip inputs of that as it messes up LooseVersion()
        if kernel_A.endswith('.x86_64'):
            kernel_A = ".".join(kernel_A.split(".")[0:-1])
        if kernel_B.endswith('.x86_64'):
            kernel_B = ".".join(kernel_B.split(".")[0:-1])

        # Take the last element off; ".el7" in the example.
        A_version = ".".join(kernel_A.split(".")[0:-1])
        B_version = ".".join(kernel_B.split(".")[0:-1])
        return LooseVersion(A_version) < LooseVersion(B_version)

    def is_rpm_installed(self, packagename):
        '''Check if a given RPM package is installed'''
        try:
            pkg = self.run_command("rpm -q %s" % packagename)
        except RuntimeError, e:
            logging.debug("Package not found: %s" % e)
            return False
        else:
            logging.debug("Package found: %s" % pkg)
            return True

    def get_rpm_version(self, packagename):
        '''Retrieve the version of a given RPM package'''
        try:
            pkg = self.run_command("rpm -q %s" % packagename)
        except RuntimeError, e:
            logging.debug("Package not found: %s" % e)
            return None
        else:
            logging.debug("Package found: %s" % pkg)
            return pkg

    def _decom_brocade(self):
        # Prior to 6.9 and 7.3, the installation was removed via a script
        # which removed kernel modules and RPMs; starting with 6.9/7.3 the
        # kernel module is Inbox, so just remove the utility RPM (like ELX)
        if self.brocade_legacy_decom:
            logging.info("Uninstalling Brocade drivers")
            # Previously depending on the custom module a script was used:
            #     kmod - brocade_install_rhel.sh
            # compiled - brocade_install.sh
            # After testing the brocade_install_rhel.sh has been found to
            # be calling the brocade_install.sh and removing the modules
            # successfully
            script = "brocade_install_rhel.sh"
            cmd = "bash %s --force-uninstall -n" % script
            try:
                self.run_command(cmd, check_dry_run=True)
            except RuntimeError, e:
                if 'No such file or directory' in str(e):
                    logging.error("%s is not installed." % script)
                    logging.error("Is the Brocade HBA driver unpacked?")
                    sys.exit(RExit.ERR_BCU_UNINSTALL)
                else:
                    raise
            # On RHEL6 the vendor uninstalled this, but does not restore it
            # when they uninstall. On RHEL7 the firmware is owned by a bigger
            # package (linux-firmware) so they can't have uninstalled it
            if self.major_version == 6:
                logging.info("Reinstalling Brocade firmware Inbox RPM")
                cmd = "yum install -y bfa-firmware"
                self.run_command(cmd, check_dry_run=True)
        else:
            logging.info("Uninstalling Brocade utility RPM")
            cmd = "yum remove -y bfa_util_linux_noioctl"
            self.run_command(cmd, check_dry_run=True)

        return True

    def decom_emulex(self):
        cmd = "yum remove -y elxocmcore elxocmcorelibs hbaapiwrapper"
        self.run_command(cmd, check_dry_run=True)
        cmd = "rm -f /etc/yum.repos.d/emulex.repo"
        self.run_command(cmd, check_dry_run=True)
        for rpm in ["kmod-lpfc", "kmod-elx-lpfc"]:
            if self.is_rpm_installed(rpm):
                self.run_command("yum remove -y %s" % rpm, check_dry_run=True)

    def _install_emulex(self, install_data, modprobe=False):
        host = RAX_MIRROR_HOST
        path = "/storagecenter/emcClariion/Installation/Linux/Emulex/"
        base_url = urlparse.urljoin(host, path)
        url = urlparse.urljoin(base_url, "emulex.repo")
        cmd = "curl -o /etc/yum.repos.d/emulex.repo %s" % url
        self.run_command(cmd, check_dry_run=True)
        cmd = "yum install -y elxocmcore elxocmcorelibs hbaapiwrapper"
        try:
            extra_packages = install_data["emulexdriver"]["file"]
        except KeyError:
            pass
        else:
            cmd = cmd + ' %s' % extra_packages
        self.run_command(cmd, check_dry_run=True)

        if modprobe:
            cmd = "modprobe lpfc"
            self.run_command(cmd, check_dry_run=True)

    def _install_brocade(self, install_data, brocade_script):
        hbadriver = install_data["brocadedriver"]["file"]

        # bcu general testing commands
        '''
            bcu port --list
            bcu adapter --list
            bcu adapter --query 1
            bcu adapter --query 2
            bcu diag --sfpshow 1/1
            bcu diag --sfpshow 2/1
        '''

        # Prior to 6.9 and 7.3, the value of hbadriver is a tarball with a
        # script to run (brocade_script); starting with 6.9/7.3 the content
        # is the name of the utility RPM to just install
        if self.brocade_legacy_install or self.brocade_legacy_migration:
            logging.info("Unpacking Brocade HBA driver")
            if 'gz' in hbadriver:
                cmd = "tar -zvxf %s" % hbadriver
            else:
                cmd = "tar -vxf %s" % hbadriver
            self.run_command(cmd, check_dry_run=True)

            logging.info("Installing Brocade HBA driver")
            # EL 6.6 and 6.7 used to install the compiled bfa driver,
            # now 6.7 will use kmod driver, making only 6.6 the only version
            # using the compiled driver.
            # EL 6.6 compiles kernel driver, using:  brocade_install.sh
            # EL <= 6.5 or >= 6.7 uses kmod-bfa rpm, using:
            #  brocade_install_rhel.sh
            cmd = "bash %s -n" % brocade_script
            out = self.run_command(cmd, check_dry_run=True)
            # brocade_install.sh always returns 0 so parse
            # the output for errors.
            if 'ERROR' in out:
                logging.error("Brocade install had an error. Full output:")
                sys.stderr.write("%s\n" % out)
                sys.exit(RExit.ERR_BCU_INSTALL)

            return True
        else:
            # On RHEL6 the vendor uninstalled this, but does not restore it
            # when they uninstall. On RHEL7 the firmware is owned by a bigger
            # package (linux-firmware) so they can't have uninstalled it
            if self.major_version == 6:
                logging.info("Installing Brocade firmware Inbox RPM")
                cmd = "yum install -y bfa-firmware"
                self.run_command(cmd, check_dry_run=True)

            logging.info("Installing Brocade utility RPM")
            cmd = "yum -y localinstall %s" % (hbadriver)
            self.run_command(cmd, check_dry_run=True)
            return True

    def _install_powerpath(self, install_data, services):
        emcpp = install_data["emcpp"]["file"]
        hostagent = install_data["hostagent"]["file"]

        # powerpath general testing commands
        '''
            powermt display dev=all
        '''

        logging.info("Installing PowerPath and HostAgent RPMs")
        cmd = "yum -y localinstall %s %s" % (emcpp, hostagent)
        self.run_command(cmd, check_dry_run=True)

        logging.info("Registering keys")
        cmd = "emcpreg -add %s %s" % (self.ppkey, self.livekey)
        try:
            self.run_command(cmd, check_dry_run=True)
        except RuntimeError, e:
            # Ignore if the key is already installed.
            if "is already present, ignored" in str(e):
                logging.debug("Key already installed.")
                pass
            elif 'registration file is corrupt' in str(e):
                logging.error(("EMCPP registration file is corrupt, renaming "
                              "/etc/emcp_registration and retrying."))
                os.rename('/etc/emcp_registration',
                          '/etc/emcp_registration.corrupt')
                try:
                    self.run_command(cmd, check_dry_run=True)
                except RuntimeError, e:
                    logging.error(("Cannot add PowerPath registration keys, "
                                   "run emcpreg manually to add the keys."))
                    pass
            else:
                raise

        # create AgentID file: /agentID.txt
        agentID = '/agentID.txt'
        # if it's already there, don't touch it (already configured)
        if not os.path.isfile(agentID):
            logging.info("Creating /agentID.txt for PowerPath")
            rsacct = rssrvr = rspubip = None

            # first check if logged in using modern Hammertime which injects
            # the public IP into the env
            rspubip = os.environ.get('RS_LOGIN_IP')
            if not rspubip:
                # try an external service, may be egress blocked
                try:
                    u = urllib2.urlopen(PUBIP_CHECK_HOST)
                    # IPv6 = 39c, >40 means wasn't an IP
                    rspubip = u.read(40).strip()
                except urllib2.URLError:
                    pass
            # grab the account and server from kick cookies
            try:
                with open('/root/.rackspace/customer_number', 'rb') as fh:
                    # no account should be >40c
                    rsacct = fh.read(40).strip()
                with open('/root/.rackspace/server_number', 'rb') as fh:
                    # no device should be >40c
                    rssrvr = fh.read(40).strip()
            except IOError:
                pass
            # if we have all three bits of data, write the file
            if rsacct and rssrvr and rspubip:
                try:
                    with open(agentID, 'wb') as fh:
                        fh.write('{0}-{1}\n{2}\n'.format(rsacct, rssrvr,
                                 rspubip))
                    os.chmod(agentID, 0o644)
                    os.chown(agentID, 0, 0)
                except IOError:
                    logging.error(("Unable to write /agentID.txt, configure "
                                   "manually"))
            else:
                logging.error(("Unable to obtain data to write /agendID.txt, "
                               "configure manually"))

        # LTC-113 - Disable 'smartd' when installing Managed Storage
        logging.info("Disabling smartd service")
        try:
            self.manage_services("stop", ['smartd'])
        except RuntimeError, e:
            logging.debug("Unable to disable smartd service, non-fatal")
            pass

        logging.info("Starting PowerPath services")
        try:
            self.manage_services("start", services)
        except RuntimeError, e:
            logging.error(("Error starting PowerPath services, ensure "
                           "built-in kernel Multipath is not running then "
                           "attempt to start manually. Error: {0}").format(e))
            logging.error(("Be sure and run 'powermt set "
                           "auto_host_registration=on' followed by 'powermt "
                           "save' after starting PowerPath successfully."))
            sys.exit(RExit.ERR_START_PP)

        # The below commands require PowerPath to be running
        logging.debug("Sleeping 5 seconds to let services settle")
        time.sleep(5)

        # The commandline options changed from PP 6.3 -> 6.4
        #  ex1: "EMC powermt for PowerPath (c) Version 6.3 P 01 (build 1)"
        #  ex2: "Dell EMC powermt for PowerPath (c) Version 6.4 (build 95)"
        logging.info("Configuring PowerPath")
        pp_legacy = False
        try:
            cmd = "powermt version"
            out = self.run_command(cmd, check_dry_run=True)
            # str.find() is -1 on fail, 0 would be if found at start of line
            if out.find("(c) Version ") > 0:
                spt = out.split("(c) Version ", 2)
                # only one element (spt[0]) means it failed the split
                if len(spt) > 1:
                    ver = spt[1].split()
                    if LooseVersion(ver[0]) < LooseVersion('6.4'):
                        pp_legacy = True
        except Exception, e:
            logging.error(("Unable to obtain version of powermt, enable "
                           "auto host registrations manually: {0}").format(e))
            return

        # 6.3 all array types with one command; 6.4 requires work
        if pp_legacy:
            # powermt set auto_host_registration=on
            cmd = "powermt set auto_host_registration=on"
            self.run_command(cmd, check_dry_run=True)
        else:
            # powermt set auto_host_registration=on class=vnx
            storage_arrays = ['vnx', 'clariion', 'unity']
            for stype in storage_arrays:
                # these update the True/False for each one, not overwrite all
                cmd = ("powermt set auto_host_registration=on "
                       "class={0}".format(stype))
                self.run_command(cmd, check_dry_run=True)

        # powermt save
        cmd = "powermt save"
        self.run_command(cmd, check_dry_run=True)

    def _decom_powerpath(self, services):
        hostagent = "HostAgent-Linux-64-x86-en_US"
        emcpp = "EMCpower.LINUX"

        logging.info("Removing keys")
        # emcpreg -remove $key
        cmd = 'emcpreg -remove %s %s' % (self.ppkey, self.livekey)
        try:
            self.run_command(cmd, check_dry_run=True)
        except RuntimeError, e:
            # Ignore if the key was not found.
            if 'Key' in str(e) and 'was not found' in str(e):
                logging.debug(str(e))
                pass
            # Ignore if emcpreg is not installed.
            elif 'Command not found' in str(e):
                logging.debug("emcpreg is not installed.")
                pass
            elif 'registration file is corrupt' in str(e):
                logging.info(("EMCPP registration file is corrupt, renaming "
                              "/etc/emcp_registration as a precaution."))
                os.rename('/etc/emcp_registration',
                          '/etc/emcp_registration.corrupt')
                pass
            else:
                raise

        logging.info("Stopping services")
        try:
            self.manage_services('stop', services)
        except RuntimeError, e:
            if 'Failed' in str(e) and 'not loaded' in str(e):
                logging.debug("Service not loaded")
                pass
            if 'Failed' in str(e) and 'No such file' in str(e):
                logging.debug("Service does not need disabled")
                pass
            else:
                logging.error("Unable to stop services, paths might be in "
                              "use:  %s" % e)
                sys.exit(RExit.ERR_STOP_PP)

        logging.info("Removing RPMs")
        cmd = "yum -y remove %s %s" % (emcpp, hostagent)
        self.run_command(cmd, check_dry_run=True)

        return True

    def _update_rdblacklist(self, module):
        rdblacklist = None
        cmd = 'grubby {0} --info=DEFAULT'.format(self.grubby_args)

        # The /boot/*/grubenv environment may be corrupt or missing a
        # default entry. We will not fix it, but will report it.
        try:
            grubbyArgs = self.run_command(cmd)
        except RuntimeError, e:
            if "kernel not found" in str(e):
                logging.error(("Unable to determine the default kernel for "
                               "this system to update the 'rdblacklist' "
                               "configuration. Manually add %s to the kernel "
                               "rdblacklist boot parameters.") % module)
                return

        for arg in grubbyArgs.split():
            if "rdblacklist" in arg:
                rdblacklist = arg.strip('"')

        if rdblacklist:
            if module not in rdblacklist:
                logging.debug("Will add %s to blacklist" % module)
                rdblacklist = "%s,%s" % (rdblacklist, module)
            else:
                # module already in rdblacklist, exit
                logging.debug("%s is already blacklisted" % module)
                return
        else:
            logging.debug("Nothing in rdblacklist, adding default")
            rdblacklist = "rdblacklist=%s" % module

        if not self.dry_run:
            # grub_cfglist is an already-vetted list of all existing GRUB
            # configs present on the filesystem, de-referenced and de-duped
            for grubfile in self.grub_cfglist:
                bakfile = "%s.rmsbak" % grubfile
                io_err_msg = ("This indicates an unrelated issue regarding "
                              "this file which needs investigated.")
                try:
                    copyfile(grubfile, bakfile)
                except IOError:
                    logging.log(31,
                                "Error backing up the grub config due to an "
                                "IO issue.")
                    logging.log(31, io_err_msg)
                    continue
                except Exception:
                    logging.log(31,
                                "Error backing up the grub config due to an "
                                "unknown issue.")
                    logging.log(31, io_err_msg)
                    continue
                else:
                    logging.debug("Backed up %s to %s" % (grubfile, bakfile))
                    cmd = ' '.join(['grubby', self.grubby_args, '-c',
                                    grubfile, '--update-kernel=ALL',
                                    '--args={0}'.format(rdblacklist)])
                    self.run_command(cmd, check_dry_run=True)
                    logging.debug('Updated {0} via grubby'.format(grubfile))
        else:
            logging.debug("Would have backed up grub configs")


class RHEL7(RedHat):
    ''' Subclass for RHEL7 specific commands.
    '''

    def __init__(self, minor):
        RedHat.__init__(self, 7)
        self.major_version = 7
        self.minor_version = int(minor)
        self.distro = "RHEL7"
        # UEK kernels need their own INI blocks (versions differ)
        if self.kernelpkg == 'kernel-uek':
            self.distro = "UEK7"
        self.emcServices = ['PowerPath', 'PowerPathPost-start', 'EmcpMond']
        # Starting with 7.3, Brocade Inbox (RH kernel) is used (3.10.0-514.*)
        self.brocade_legacy_decom = False
        self.brocade_legacy_install = False
        self.brocade_legacy_migration = False
        # UEK kernel support starts with Emulex only on Oracle 7.4
        if self.kernelpkg == 'kernel':
            # Decom compares the running kernel
            if self.running_family < 514:
                logging.debug("Enabling Brocade Legacy mode for decom")
                self.brocade_legacy_decom = True
            # Install compares the point release
            if self.minor_version < 3:
                logging.debug("Enabling Brocade Legacy mode for install")
                self.brocade_legacy_install = True
            # Legacy Migration; this happens if this code is updated but
            # the older INI is being used (cache, etc.), install mode only
            if self.brocade_legacy_decom and not self.brocade_legacy_install:
                logging.debug("Setting Migration cookie internally")
                self.brocade_legacy_migration = True

        logging.debug("Created RHEL7 object with minor version: %s" % minor)

    def manage_services(self, action, services):
        if action not in ['start', 'stop', 'restart']:
            raise ValueError(("Action must be either 'start', 'stop', or"
                              " 'restart'."))
        if not isinstance(services, list):
            services = [services]

        if action in ['start', 'restart']:
            services = " ".join(services)
            cmd = "systemctl enable %s" % services
            self.run_command(cmd, check_dry_run=True)
            cmd = "systemctl %s %s" % (action, services)
            self.run_command(cmd, check_dry_run=True)
        elif action == 'stop':
            for service in services:
                try:
                    cmd = "systemctl status %s" % service
                    self.run_command(cmd, check_dry_run=True)
                except RuntimeError:
                    logging.debug(
                        "Service %s not running or not installed" % service)
                else:
                    cmd = "systemctl disable %s" % service
                    self.run_command(cmd, check_dry_run=True)
                    cmd = "systemctl %s %s" % (action, service)
                    self.run_command(cmd, check_dry_run=True)

        return True

    def install_brocade(self, install_data):
        ''' Run the installation commands '''
        if self.kernelpkg == "kernel-uek":
            logging.error("UEK kernels with Brocade hardware are not "
                          "supported, manual installation required.")
            sys.exit(RExit.ERR_DISTRO)

        script = "brocade_install_rhel.sh"
        return self._install_brocade(install_data, script)

    def install_emulex(self, install_data):
        modprobe = False
        if self.minor_version <= 2:
            modprobe = True
        return self._install_emulex(install_data, modprobe)

    def decom_brocade(self):
        if self.kernelpkg == "kernel-uek":
            logging.error("UEK kernels with Brocade hardware are not "
                          "supported, manual decom required.")
            return False

        return self._decom_brocade()

    def install_powerpath(self, install_data):
        return self._install_powerpath(install_data, self.emcServices)

    def decom_powerpath(self):
        return self._decom_powerpath(self.emcServices)

    def update_rdblacklist(self, module):
        logging.debug("Checking /etc/default/grub")
        rdblacklist = None
        updated_grub = []
        update_needed = False

        with open("/etc/default/grub", "r") as f:
            for line in f:
                if line.startswith("GRUB_CMDLINE_LINUX="):
                    args = (
                        line.lstrip("GRUB_CMDLINE_LINUX=")
                        .strip('\n').strip('"').split(" ")
                    )

                    for index, arg in enumerate(args):
                        if "rdblacklist" in arg:
                            rdblacklist = arg
                            if module not in rdblacklist:
                                logging.debug(
                                    "%s will be added to /etc/default/grub"
                                    % module)
                                update_needed = True
                                rdblacklist = "%s,%s" % (rdblacklist, module)
                                args[index] = rdblacklist
                            else:
                                logging.debug(
                                    "%s already in rdblacklist for "
                                    "/etc/default/grub, no changes needed."
                                    % module)
                    if not rdblacklist:
                        update_needed = True
                        rdblacklist = "rdblacklist=%s" % module
                        logging.debug(
                            "rdblacklist not found in args, adding: %s"
                            % rdblacklist)
                        if len(args) == 1 and len(args[0]) == 0:
                            args = [rdblacklist]
                        else:
                            args.append(rdblacklist)

                    args = " ".join(args)
                    line = 'GRUB_CMDLINE_LINUX="%s"\n' % args

                updated_grub.append(line)

        if update_needed and not self.dry_run:
            logging.debug("Backing up /etc/default/grub")
            copyfile("/etc/default/grub", "/etc/default/grub.rmsbak")
            try:
                with open("/etc/default/grub", "w+") as f:
                    for line in updated_grub:
                        f.write(line)
                logging.debug("New /etc/default/grub written.")
            except Exception as e:
                logging.log(31,
                            "Unknown error trying to write to "
                            "/etc/default/grub: %s" % str(e))
                logging.log(31,
                            "Please verify that '%s' is added to rdblacklist"
                            "in /etc/default/grub or escalate as "
                            "appropriate." % module)
                logging.log(31,
                            "This indicates an unrelated issue regarding this "
                            "file which needs investigated.")
        elif update_needed and self.dry_run:
            logging.debug("Would have updated /etc/default/grub")

        return self._update_rdblacklist(module)


class RHEL6(RedHat):
    ''' Subclass for RHEL6 specific commands
    '''

    def __init__(self, minor):
        RedHat.__init__(self, 6)

        # UEK support starts with EL7 7.4
        if self.kernelpkg == "kernel-uek":
            logging.error("UEK kernels on EL6 are not supported, manual "
                          "install is required.")
            sys.exit(RExit.ERR_DISTRO)

        self.major_version = 6
        self.minor_version = int(minor)
        self.distro = "RHEL6.%d" % self.minor_version
        self.emcServices = ['PowerPath', 'PowerPathPost']
        # Starting with 6.9, Brocade Inbox (RH kernel) is used (2.6.32-696.*)
        self.brocade_legacy_decom = False
        self.brocade_legacy_install = False
        self.brocade_legacy_migration = False
        # Decom compares the running kernel
        if self.running_family < 696:
            logging.debug("Enabling Brocade Legacy mode for decom")
            self.brocade_legacy_decom = True
        # Install compares the point release
        if self.minor_version < 9:
            logging.debug("Enabling Brocade Legacy mode for install")
            self.brocade_legacy_install = True
        # Legacy Migration; this happens if this code is updated but
        # the older INI is being used (cache, etc.), install mode only
        if self.brocade_legacy_decom and not self.brocade_legacy_install:
            logging.debug("Setting Migration cookie internally")
            self.brocade_legacy_migration = True

        logging.debug("Created RHEL6 object with minor version: %s" % minor)

    def manage_services(self, action, services):
        if action not in ['start', 'stop', 'restart']:
            raise ValueError(("Action must be either 'start', 'stop', or"
                              " 'restart'."))
        if not isinstance(services, list):
            services = [services]

        # service takes "start, stop" but chkconfig needs "on, off"
        chkstate = 'on'
        if action == 'stop':
            chkstate = 'off'

        # RHEL6 can only handle one named item per chkconfig/service call
        for service in services:
            try:
                # service foo start/stop must be run before chkconfig, as
                # some services (PowerPath) are not chkconfig compliant
                cmd = "service %s %s" % (service, action)
                self.run_command(cmd, check_dry_run=True)
                cmd = "chkconfig %s %s" % (service, chkstate)
                self.run_command(cmd, check_dry_run=True)
            except RuntimeError, e:
                if 'error reading information' in str(e):
                    # 'chkconfig foo ...' not found error text
                    logging.debug("Skipping service %s: %s" % (service, e))
                    pass
                elif 'unrecognized service' in str(e):
                    # 'service foo ...' not found error text
                    logging.debug("Skipping service %s: %s" % (service, e))
                    pass
                elif 'does not support chkconfig' in str(e):
                    # PowerPath fails the chkconfig commands
                    logging.debug("Skipping service %s: %s" % (service, e))
                    pass
                else:
                    msg = "Unable to %s service %s: %s" % (action, service, e)
                    logging.error(msg)
                    raise RuntimeError(msg)

        return True

    def install_brocade(self, install_data):
        if int(self.minor_version) == 6:
            script = 'brocade_install.sh'

            logging.debug("Verify gcc is installed")
            cmd = "yum -y install gcc"
            self.run_command(cmd, check_dry_run=True)

            logging.debug("Verify kernel-devel is installed")
            kernel = self.get_running_kernel()
            cmd = ("yum -y --showduplicates --disableexcludes=all "
                   "install "
                   "%s-devel-%s" % (self.kernelpkg, kernel))
            self.run_command(cmd, check_dry_run=True)
        else:
            script = 'brocade_install_rhel.sh'
        return self._install_brocade(install_data, script)

    def install_emulex(self, install_data):
        return self._install_emulex(install_data, modprobe=True)

    def decom_brocade(self):
        return self._decom_brocade()

    def install_powerpath(self, install_data):
        return self._install_powerpath(install_data, self.emcServices)

    def decom_powerpath(self):
        return self._decom_powerpath(self.emcServices)

    def update_rdblacklist(self, module):
        return self._update_rdblacklist(module)


class StorageHealth(object):
    """Check the health of Managed Storage"""

    def __init__(self, server, config, options):
        self.problems = 0
        self.powermt = {}
        self.server = server
        self.config = config
        self.options = options
        self.logok = "[" + OKGREEN + "  OK  " + ENDC + "]"
        self.logfail = "[" + FAIL + " FAIL " + ENDC + "]"
        self.logwarn = "[" + WARNING + " WARN " + ENDC + "]"

        method_list = [self.prechecks,
                       self.extract_lun_data,
                       self.check_ghost_luns,
                       self.check_rc_sysinit,
                       self.check_next_free_pseudo,
                       self.check_lun_health,
                       self.check_hba_vendor,
                       self.check_software_versions,
                       self.check_lvm_filter,
                       self.check_hba_boot_blacklist,
                       self.check_matching_sfp_vendors]

        for method in method_list:
            try:
                method()
            except RuntimeError, e:
                continue
            if self.problems >= 1:
                return

    def prechecks(self):
        """Preliminary Checks that must pass before others can be done"""

        if self.server.storage_details['brocade']:
            logging.log(32, "%s Brocade HBAs detected" % self.logok)
        elif self.server.storage_details['emulex']:
            logging.log(32, "%s Emulex HBAs detected" % self.logok)
        else:
            logging.log(32, "%s No Supported HBAs detected" % self.logfail)
            self.problems += 1

        if not os.path.isfile('/sbin/powermt'):
            logging.log(32, "%s PowerPath not installed, exiting" %
                        self.logfail)
            self.problems += 1
        else:
            logging.log(32, "%s PowerPath installed" % self.logok)

        try:
            lsmod = self.server.run_command('/sbin/lsmod')
        except RuntimeError:
            raise RuntimeError("lsmod command failed. Check OS for issues")

        if 'emcp' not in lsmod:
            logging.log(32, "%s EMC kernel modules not loaded, exiting" %
                        self.logfail)
            self.problems += 1
        else:
            logging.log(32, "%s EMC kernel modules loaded" % self.logok)

        if self.problems >= 1:
            logging.log(32, "%s Preflight checks failed" % self.logfail)
            return self.problems

    def extract_lun_data(self):
        """Create List of LUN information for consumption"""
        usedpseudos = []

        try:
            emcpadm = self.server.run_command('/sbin/emcpadm '
                                              'getusedpseudos')
        except RuntimeError:
            raise RuntimeError("Could not pull data from emcpadm")

        emcpadm = emcpadm.splitlines()
        for line in emcpadm:
            if line.strip().startswith('emcpower'):
                usedpseudos.append(line.split()[0])

        for dev in usedpseudos:
            self.powermt[dev] = {}
            self.powermt[dev]["Paths"] = {}
            try:
                out = self.server.run_command('/sbin/powermt display dev=%s'
                                              % dev)
            except RuntimeError:
                raise RuntimeError("Could not generate proper LUN Data. Check "
                                   "for errors using powermt.")
            platform = ["Symmetrix", "VNX", "Unity", "CLARiiON"]
            drivers = ["lpfc", "bfa", "qla2xxx"]
            for line in out.splitlines():
                currline = line.strip()
                if len(currline) < 1:
                    # These are blank lines from the output. Skipping
                    continue
                if re.match(r'^(#|-|=)', currline):
                    # These are Formatting lines from the output. Skipping
                    continue
                else:
                    vals = re.split(" +", currline)
                    if len(vals) > 8:
                        if any(x in vals for x in drivers):
                            self.powermt[dev]["Paths"][vals[2]] = {
                                "Driver": vals[1], "Mode": vals[5]}
                    else:
                        speql = line.split('=')
                        if "Logical" in vals:
                            self.powermt[dev]["Logical Device ID"] = speql[1]
                        if "Device" in vals:
                            self.powermt[dev]["Device WWN"] = speql[1]
                        if any(x in vals for x in platform):
                            self.powermt[dev]["SAN type"] = vals[0]
                        if "state" in vals[0]:
                            state = vals[0].split('=')[1]
                            self.powermt[dev]["State"] = state.rstrip(';')
                        if "policy" in vals[1]:
                            policy = vals[1].split('=')[1]
                            self.powermt[dev]["Policy"] = policy.rstrip(';')

    def check_ghost_luns(self):
        """Checks for the presence of ghost LUNS"""
        found = False
        prog = re.compile("Vendor: DGC[[:space:]]*Model:[[:space:]]*Rev")
        with open('/proc/scsi/scsi', 'r') as scsifile:
            line = scsifile.readline().strip()
            if prog.match(line):
                logging.log(32, "%s There are ghost LUNs" % self.logfail)
                found = True
                self.problems += 1
        if not found:
            logging.log(32, "%s No ghost LUNs found" % self.logok)

    def check_rc_sysinit(self):
        """Check rc.sysinit file on systems lower than RHEL/CentOS/Oracle 7"""
        osrelease = self.server.major_version
        if osrelease < 7:
            with open('/etc/rc.sysinit', "r") as rcfile:
                if "###BEGINPP" not in rcfile.read():
                    logging.log(32, "%s PowerPath block BEGINPP/ENDPP not "
                                    "present in rc.sysinit" % self.logfail)
                    self.problems += 1
                else:
                    logging.log(32, "%s PowerPath block BEGINPP/ENDPP present "
                                    "in rc.sysinit" % self.logok)

    def check_next_free_pseudo(self):
        """Get the next pseudo name that will be used"""
        try:
            out = self.server.run_command('/sbin/emcpadm '
                                          'getfreepseudos').splitlines()
        except RuntimeError:
            logging.debug(("Was not able to obtain data from powermt to "
                           "check for the next available LUN"))
            return
        for line in out:
            if line.strip().startswith('emcpower'):
                logging.log(32, "%s Next pseudo device is: %s" %
                            (self.logok, line.split()[0]))

    def check_lun_health(self):
        """Gets and provides information for the individual LUNs"""
        arraytype = None
        exppaths = None
        exporpaths = None
        expol = None
        ppcmd = "emcpreg -list"
        try:
            ppkeys = self.server.run_command(ppcmd)
        except RuntimeError:
            logging.debug(("Unable to pull Powerpath keys. Manual checks "
                           "are required"))

        ppkeys = ppkeys.splitlines()
        keydic = dict()
        keyenum = 0

        for line in ppkeys:
            if line.startswith('Key'):
                keyentry = re.split(' ', line)
                keydic[keyentry[0] + str(keyenum)] = keyentry[1]
                keyenum += 1
        if len(keydic) < 2:
            logging.log(32, "Found less than 2 license keys. 2 or more keys "
                        "are expected" % (self.logwarn))
        else:
            ppkeycheck = 0
            ppcopykey = 0
            for key in keydic:
                if self.server.ppkey in keydic[key]:
                    ppkeycheck += 1
                elif self.server.livekey in keydic[key]:
                    ppcopykey += 1
            if ppkeycheck == 1:
                logging.log(32, "%s Powerpath License Key in place" %
                            self.logok)
            else:
                logging.log(32, "%s Powerpath License Key is missing. "
                            "Skipping LUN Health Checks as data will not be "
                            "consistent" % self.logfail)
                return
            if ppcopykey == 1:
                logging.log(32, "%s Powerpath HOSTCOPY Key in place" %
                            self.logok)
            else:
                logging.log(32, "%s Powerpath HOSTCOPY Key is missing" %
                            self.logfail)

        for device in self.powermt:
            if "SAN type" not in self.powermt[device]:
                logging.log(32, "Device %s has no powermt configuration "
                            "but is marked as a used pseudo device"
                            % (self.logfail, device))
                continue
            elif self.powermt[device]["SAN type"] == "Symmetrix":
                logging.info("LUN %s as %s is on Symmetrix SAN" %
                             (self.powermt[device]["Logical Device ID"],
                              device))
                arraytype = "Symmetrix"
                exppaths = 2
                exporpaths = 4
                exppol = "SymmOpt"
            elif self.powermt[device]["SAN type"] == "VNX":
                logging.info("LUN %s as %s is on VNX SAN" %
                             (self.powermt[device]["Logical Device ID"],
                              device))
                arraytype = "VNX"
                exppaths = 4
                exporpaths = 8
                exppol = "CLAROpt"
            elif self.powermt[device]["SAN type"] == "Unity":
                logging.info("LUN %s as %s is on Unity SAN" %
                             (self.powermt[device]["Logical Device ID"],
                              device))
                arraytype = "Unity"
                exppaths = 4
                exporpaths = 8
                exppol = "CLAROpt"
            elif self.powermt[device]["SAN type"] == "CLARiiON":
                logging.info("LUN %s as %s is on CLARiiON SAN" %
                             (self.powermt[device]["Logical Device ID"],
                              device))
                arraytype = "CLARiiON"
                exppaths = 4
                exporpaths = 4
                exppol = "CLAROpt"

            numactivepaths = 0
            for path in self.powermt[device]["Paths"]:
                if self.powermt[device]["Paths"][path]["Mode"] == "active":
                    numactivepaths += 1

            if (numactivepaths >= exppaths) and (numactivepaths % 2 == 0):
                logging.info("%s has correct number of active paths - %s" %
                             (device, numactivepaths))
            else:
                logging.log(32, "%s %s has wrong number of active paths - %s"
                            % (self.logfail, device, numactivepaths))

            if self.powermt[device]["Policy"] == exppol:
                logging.info("%s has correct load balancing policy - %s" %
                             (device, self.powermt[device]["Policy"]))
            else:
                logging.log(32, "%s %s has wrong load balancing policy - "
                            "%s instead of %s" % (self.logfail, device,
                                                  self.powermt[device]
                                                  ["Policy"], exppol))

            logging.log(32, "%s LUN %s as %s (%s) with policy %s and %s "
                        "paths" % (self.logok, self.powermt[device]
                                   ["Logical Device ID"], device,
                                   self.powermt[device]["SAN type"],
                                   self.powermt[device]["Policy"],
                                   numactivepaths))

    def check_software_versions(self):
        current_packages = self.package_dictionary()
        desired_family = self.server.running_pattern
        latest_kernel = self.server.running_kernel
        if self.options.force_kernel_upgrade:
            target_kernels = self.config.get_kernel_targets()
            (desired_family, latest_kernel) = self.config.get_best_kernel(
                                              self.server.available_kernels,
                                              target=target_kernels)
            if self.server.is_kernel_less_than(self.server.running_kernel,
                                               latest_kernel):
                logging.log(32, "%s Newer kernel available, using latest "
                            "kernel: %s. Package version check failures "
                            "should be expected" % (self.logwarn,
                                                    latest_kernel))
            else:
                logging.log(32, "%s No updated kernel found. Using installed "
                            "kernel: %s" % (self.logok,
                                            self.server.running_kernel))
        packages = self.config.get_package_list(desired_family)
        self.server.minor_version

        if self.server.major_version < 6:
            logging.log(32, "%s Unable to check software versions on distros "
                        "before RHEL/CentOS/Oracle 6. Manual checks are "
                        "required." % self.logfail)
            return
        for (label, package) in packages:
            for key in current_packages:
                if label.lower() in key.lower():
                    logging.debug("Comparing versions for %s" % key)
                    logging.debug("Found %s as target" % package)
                    logging.debug("Found %s as current" %
                                  current_packages[key])
                    if current_packages[key] is None:
                        if label in "emulexdriver":
                            missing = "elxocmcore"
                        else:
                            missing = label
                        logging.log(32, "%s %s RPM expected but not found." %
                                    (self.logfail, missing))
                    else:
                        if key.lower() in "emulexdriver":
                            currpkg = current_packages[key]
                            if package not in "Inbox":
                                if package in currpkg:
                                    logging.log(32, "%s %s is installed as "
                                                "expected." % (self.logok,
                                                               currpkg))
                                else:
                                    logging.log(32, "%s %s expected but not "
                                                "found." % (self.logfail,
                                                            currpkg))
                            else:
                                logging.log(32, "%s %s is installed as "
                                            "expected" % (self.logok,
                                                          currpkg))
                        else:
                            curr = re.sub(".el7|.x86_64|.el6|.noarch", "",
                                          current_packages[key])
                            target = re.sub(".RHEL6|.RHEL7|.x86_64|.rpm|"
                                            ".noarch", "", package)
                            if curr.lower() not in target.lower():
                                logging.log(32, "%s Installed version: %s "
                                            "does not match latest version:"
                                            " %s" %
                                            (self.logfail, curr, target))
                                self.problems += 1
                            else:
                                logging.log(32, "%s %s is fully up to date."
                                            % (self.logok, curr))

    def package_dictionary(self):

        major = self.server.major_version
        minor = self.server.minor_version
        if self.server.storage_details['emulex']:
            self.config.hba_driver = "EMULEXDRIVER"
        elif self.server.storage_details['brocade']:
            self.config.hba_driver = "BROCADEDRIVER"

        # TODO: Feature request to add check for bfa-firmware
        if "BROCADE" in self.config.hba_driver:
            if major == 7:
                if minor <= 2:
                    hbadriver = self.server.get_rpm_version("brocade_driver_"
                                                            "linux_rhel7_v3")
                else:
                    hbadriver = self.server.get_rpm_version("bfa_util_linux_"
                                                            "noioctl")
            elif major == 6:
                if minor <= 8:
                    hbadriver = self.server.get_rpm_version("brocade_driver_"
                                                            "linux_rhel6_v3")
                else:
                    hbadriver = self.server.get_rpm_version("bfa_util_linux_"
                                                            "noioctl")
            else:
                logging.log(32, "%s RHEL%s is not supported"
                            % (self.logfail, major))

        elif "EMULEX" in self.config.hba_driver:
            if major == 7:
                if minor <= 2:
                    hbadriver = self.server.get_rpm_version("kmod-lpfc")
                else:
                    hbadriver = self.server.get_rpm_version("elxocmcore")
            elif major == 6:
                if minor <= 7:
                    hbadriver = self.server.get_rpm_version("kmod-elx-lpfc")
            else:
                logging.log(32, "%s RHEL%s is not supported"
                            % (self.logfail, major))

        pkgdic = {}
        for storagepkg in ("emcpp", "hostagent", self.config.hba_driver):
            try:
                emcpp = self.server.get_rpm_version("EMCpower.LINUX")
            except RuntimeError, e:
                logging.debug("Package not found: %s" % e)
                emcpp = None
            try:
                hostagent = self.server.get_rpm_version("HostAgent-Linux-"
                                                        "64-x86-en_US")
            except RuntimeError, e:
                logging.debug("Package not found: %s" % e)
                hostagent = None

            if storagepkg in "emcpp":
                pkgdic[storagepkg] = emcpp
            elif storagepkg in "hostagent":
                pkgdic[storagepkg] = hostagent
            elif storagepkg in self.config.hba_driver:
                pkgdic[storagepkg] = hbadriver
        return pkgdic

    def active_hba_driver(self):
        inuse = None

        # TODO: Need to rethink logic to account for different HBAs
        # should be a failure as best practice is to have matching
        for driver in self.powermt:
            for path in self.powermt[driver]["Paths"]:
                if "lpfc" in self.powermt[driver]["Paths"][path]["Driver"]:
                    inuse = "lpfc"
                    break
                if "bfa" in self.powermt[driver]["Paths"][path]["Driver"]:
                    inuse = "bfa"
                    break
                if "qla2xxx" in self.powermt[driver]["Paths"][path]["Driver"]:
                    inuse = "qla2xxx"
                    break
        return inuse

    def check_hba_vendor(self):
        driver = self.active_hba_driver()

        if driver == "qla2xxx":
            logging.log(32, "%s Legacy qla2xxx driver in use" % self.logfail)
            return

        logging.log(32, "%s %s driver in use" % (self.logok, driver))

    def check_lvm_filter(self):
        lvmfilter = ""
        lvmglobalfilter = ""

        try:
            currfilter = self.server.run_command('/sbin/lvm dumpconfig')
        except RuntimeError:
            raise RuntimeError("lvm command failed. Check OS for issues")

        currfilter = currfilter.splitlines()
        for line in currfilter:
            line = line.strip()
            if line.startswith("filter"):
                if self.server.major_version > 6:
                    logging.log(32, "%s LVM \"filter\" line exists but "
                                "should not exist. \"global_filter\" should "
                                "be used" % self.logfail)
                logging.info("FILTER LINE: %s" % line)
                lvmfilter = line.split("=")[1]
            elif line.startswith("global_filter"):
                logging.info("GLOBAL_FILTER LINE: %s" % line)
                lvmglobalfilter = line.split("=")[1]
        if (len(lvmglobalfilter) <= 0) and (len(lvmfilter) <= 0):
            logging.log(32, "%s No global_filter or filter line found in "
                        "the lvm configuration. Please evaluate the "
                        "server manually" % self.logfail)
            return
        if len(lvmglobalfilter) > 0:
            lvmfilter = lvmglobalfilter
        disks = ["/dev/sd", "/dev/emcpower"]
        if all(x in lvmfilter for x in disks):
            logging.log(32, "%s LVM filter is correct" % self.logok)
        else:
            logging.log(32, "%s LVM filter line may not be correct. Please "
                        "check the wiki for the proper filter" % self.logwarn)

    def check_hba_boot_blacklist(self):
        rdbl = 0
        bloptions = ['bfa', 'lpfc']
        logging.info("Checking for the following blacklist items: %s" %
                     bloptions)
        with open("/proc/cmdline") as cmdline:
            lines = cmdline.read().splitlines()
            for line in lines:
                line = re.split(r'\s+', line)
                for option in line:
                    if option.startswith("rdblacklist"):
                        rdbl += 1
                        if all(x in option for x in bloptions):
                            logging.log(32, "%s Appropriate kernel blacklist "
                                        "items found" % self.logok)
                        else:
                            logging.log(32, "%s Kernel blacklist items "
                                        "missing. Please manually check the "
                                        "system items found" % self.logfail)
        if rdbl < 1:
            logging.log(32, "%s No rdblacklist entry found in the kernel "
                        "parameters. Please verify the system configuration"
                        % self.logfail)

    def check_matching_sfp_vendors(self):
        ports = []
        vendorlocations = []

        if self.server.storage_details['brocade']:
            try:
                port_list = self.server.run_command('bcu port --list -terse')
            except RuntimeError:
                raise RuntimeError("bcu command missing or not working. "
                                   "Please check the system manually")
            port_list = port_list.splitlines()
            for line in port_list:
                sline = line.strip()
                if len(sline) < 1:
                    # skip blank lines
                    continue
                if re.match(r'^(#|-|=)', sline):
                    # skip formatting lines
                    continue
                if sline.startswith(("Port#", "Eth")):
                    # skip header line
                    continue
                else:
                    ports.append(sline.split()[0])
        else:
            return

        for port in ports:
            try:
                sfpinfo = self.server.run_command("bcu diag --sfpshow %s" %
                                                  port)
            except RuntimeError:
                raise RuntimeError("bcu command missing or not working. "
                                   "Please check the system manually")
            sfpinfo = sfpinfo.splitlines()
            for line in sfpinfo:
                if line.startswith('Transceiver'):
                    try:
                        vendorlocations.append(line.split(",")[-2])
                    except ValueError:
                        logging.log(32, "%s Returned data is malformed. "
                                    "Manually check the SFP using: "
                                    "\'bcu diag --sfpshow PORT\'" %
                                    self.logfail)
                        return

        notmatched = 0
        for location in vendorlocations:
            if location in vendorlocations[0]:
                continue
            else:
                notmatched += 1
        if notmatched >= 1:
            logging.log(32, "%s SFP manufacturer locations are mixed. Check "
                        "with the SAN team for possible resolutions" %
                        self.logfail)
            return
        else:
            if "I" in vendorlocations[0]:
                logging.log(32, "%s SFP manufacturer location matches: I - "
                            "China" % self.logok)
            elif "S" in vendorlocations[0]:
                logging.log(32, "%s SFP manufacturer location matches: S - "
                            "Malaysia" % self.logok)
            else:
                logging.log(32, "%s SFP manufacturer location matches, but "
                            "not a known location: %s" % (self.logfail,
                                                          vendorlocations[0]))


def run_decom(options):
    server = Server.factory(platform.dist())
    server.dry_run = options.dry_run
    # The user wants to override autodetect if =True (default =False)
    if options.brocade_legacy:
        server.brocade_legacy_decom = options.brocade_legacy

    # Uninstall EMC Powerpath
    logging.info("Uninstalling Powerpath")
    server.decom_powerpath()

    if server.storage_details['emulex']:
        logging.debug("Decomming Emulex hardware")
        server.decom_emulex()
    elif server.storage_details['brocade']:
        logging.debug("Decomming Brocade hardware")
        # Prior to 6.9/7.3, we needed to download the latest vendor script
        # out of the driver package to remove all the pieces. Starting with
        # 6.9/7.3, all we need to do is uninstall the utility RPM (like ELX)
        if server.brocade_legacy_decom:
            try:
                kmod_rpm = server.brocade_details['kmod_bfa']['rpm']
            except KeyError:
                kmod_rpm = False
            try:
                bfa_drv_rpm = server.brocade_details['bfa_driver_linux']['rpm']
            except KeyError:
                bfa_drv_rpm = False
            # Check for RPMs if none, then check for custom modules,
            # otherwise return
            if not (kmod_rpm or bfa_drv_rpm):
                if server.brocade_details['bfa_module']['type'] != 'custom':
                    logging.info("No Brocade module installed.")
                    return False

            # Depending on the installation method a specific script is needed
            # that script is provided in the brocade tarball, then is needed
            # to get the correct tarball to use.
            config = StorageConfig(
                server.distro,
                config_file=options.config_file,
                cache=options.use_cache
            )
            config.hba_driver = "BROCADEDRIVER"

            # Get the kernel pattern, either via cli option or by extracting
            # it from the currently running kernel
            # 3.10.0-327.36.3.el7 -> 3.10.0-327
            if options.target_kernel:
                target_kernels = server.get_kernel_family(options.target_kernel)  # noqa: E501
            else:
                target_kernels = server.running_pattern

            # This ensures we have a matching config section for the target
            # - it may not be in the current YUM repos (old kernel), we
            #   previously used that list; now, we just inject the running
            #   kernel directly to try and find itself in the config INI
            (best_target, best_kernel) = config.get_best_kernel(
                [server.running_kernel], target=target_kernels)
            if not best_kernel:
                logging.error(("Could not find config section compatible "
                               "with %s" % target_kernels))
                sys.exit(RExit.ERR_BAD_KERNEL)

            # Get the tarball to download now that we know a section exists
            for (label, package) in config.get_package_list(best_target):
                if label == 'brocadedriver':
                    brocade_tar = package
                    break

            # Download the tarball if is not available locally.
            if os.path.exists(brocade_tar):
                logging.debug("Found tarball locally: %s" % brocade_tar)
            else:
                config.download_package(brocade_tar)

            # Extract the tarball that contains the scripts to uninstall
            # the drivers.
            cmd = "tar xf %s" % brocade_tar
            server.run_command(cmd, check_dry_run=True)

        # Uninstall the driver or utility RPM
        server.decom_brocade()

    if options.dry_run:
        print "Storage drivers would've been uninstalled"
    else:
        print "Storage drivers are now uninstalled"


def run_auto(options):
    run_decom(options)
    main(options)


def check_boot():
    """Check /boot for a safe amount of free space
    1x initrd = ~25M, by default RHEL stores 3x kernels and the Brocade
    install makes a .bak file of each initrd as it adds it's module. While
    there may be existing .bak files present, if we say 100M as the limit
    it is low enough to work for >90% of the users; a standard 3-kernel
    install consumes about 140M so even on a 250M /boot that would just be
    enough to squeak by.
    """
    minfree = 104857600
    if os.path.ismount('/boot'):
        fst = os.statvfs('/boot')
        bfree = fst.f_bavail * fst.f_frsize
        if bfree < minfree:
            return False
    # unless we actually fail the test, thumbs up
    return True


def main(options):

    # Create our server based on the operating system
    server = Server.factory(platform.dist())
    server.dry_run = options.dry_run
    storage_already_configured = False

    # Set up the storage config object
    config = StorageConfig(server.distro, options.config_file,
                           options.use_cache)

    if not config.latest:
        logging.error("%s is not a supported distribution" % server.distro)
        sys.exit(RExit.ERR_DISTRO)
    logging.info("Distro %s is supported. Latest kernel in config: %s" % (
                 server.distro, config.latest_kernel))

    # Catches for tool only options
    if options.status:
        health = StorageHealth(server, config, options)
        sys.exit(RExit.OK)

    # If the storage software is already configured as expected, there is no
    # reason to install any software.
    if server.storage_details['emulex']:
        storage_already_configured = server.emulex_sanity_check()
    elif server.storage_details['brocade']:
        storage_already_configured = server.brocade_sanity_check()
    else:
        logging.error(
            "Neither Emulex nor Brocade hardware was detected on "
            "this server. Exiting.")
        sys.exit(RExit.ERR_HW_NONE)
    if storage_already_configured:
        if not options.auto:
            logging.log(31, ("Managed Software is already correctly "
                             "installed, exiting."))
            logging.log(31, ("If you wish to reinstall or upgrade, remove "
                             "the existing software using --decom first or"
                             " use --auto to remove the existing software "
                             "and install the newest software in one step."))
            logging.log(31, ("Please ensure the remote block storage is not "
                             "in use by:"))
            logging.log(31, "- Mounted filesystems")
            logging.log(31, "- LVM Physical/Group Volumes")
            logging.log(31, "- Oracle ASM Disks")
            sys.exit(RExit.ERR_ALREADY_INSTALLED)
    else:
        # Continue installing drivers
        logging.debug("Server is OK to have storage configured, continuing.")

        # Tell our storage config object which HBA driver's details we care
        # about in the ini file
        if server.storage_details['emulex']:
            config.hba_driver = "EMULEXDRIVER"
        elif server.storage_details['brocade']:
            config.hba_driver = "BROCADEDRIVER"
            # Only <6.9 and <7.3 use custom modules rebuilding initrd
            if server.brocade_legacy_install:
                # Check that /boot has sufficient free space
                if not options.skip_space:
                    if (
                        not options.decom and
                        not options.dry_run and
                        not check_boot()
                            ):
                        logging.error(
                            "Not enough space free on /boot, 100M required")
                        sys.exit(RExit.ERR_SPACE_BOOT)
                else:
                    options.verbose = True
                    # logging.INF0 = 20, logging.DEBUG = 10
                    if logging.getLogger().getEffectiveLevel() > logging.INFO:
                        log_level = logging.INFO
                        logging.getLogger().setLevel(log_level)
                        logging.info(("Skipping free space check, enabling "
                                      "verbose mode for safety"))
                    else:
                        logging.info("Skipping free space check")

        # Ensure approved kernel is being run
        if options.target_kernel:
            target_kernels = server.get_kernel_family(options.target_kernel)
        else:
            target_kernels = config.get_kernel_targets()

        (best_target, best_kernel) = config.get_best_kernel(
            server.available_kernels, target=target_kernels)
        if not best_kernel:
            logging.error(("Could not find kernel package compatible "
                           "with %s" % target_kernels))
            sys.exit(RExit.ERR_BAD_KERNEL)

        # If -k was supplied, it might already be running
        if options.target_kernel:
            logging.debug("Checking %s against %s" %
                          (server.running_kernel, options.target_kernel))
            is_running_best = server.running_kernel.startswith(options.target_kernel)  # noqa: E501
        else:
            logging.debug("Checking %s against %s" %
                          (server.running_kernel, best_kernel))
            is_running_best = server.running_kernel.startswith(best_kernel)

        if not is_running_best or options.force_kernel_upgrade:
            # If the server is not running the target kernel then install a
            # compatible kernel version.
            if options.force_kernel_upgrade:
                logging.info(("Installation of latest kernel forced, "
                              "checking for updates..."))
            else:
                logging.info(
                    "Kernel %s is better than %s" % (best_kernel,
                                                     server.running_kernel))
            # Install the new kernel.

            if options.target_kernel:
                server.install_kernel(options.target_kernel,
                                      server.running_kernel)
            else:
                server.install_kernel(best_kernel, server.running_kernel)
            print(("Please reboot into the new kernel, then run "
                   "rs-managed-storage again to install the vendor "
                   "software."))
            sys.exit(RExit.ERR_REBOOT_NEEDED)

        # Build install_data dict
        logging.debug("Get list of packages to install for %s" % best_target)
        packages = config.get_package_list(best_target)
        for (label, package) in packages:
            logging.debug("%s Found label %s and package %s" % (
                best_target, label, package))

        logging.info("Downloading packages")
        install_data = dict()
        '''
            Take each package label and download its url to a file.

            install_data[label]["url"] == url to download file
            install_data[label]["file"] == path to file on system
        '''
        for (label, package) in packages:
            install_data[label] = dict()
            install_data[label]["url"] = package
            if (package not in ["Inbox", "Unsupported"] and
                    label not in ["emulexdriver"]):
                logging.debug("Downloading package %s" % package)
                package_file = config.download_package(package)
                install_data[label]["file"] = package_file
            elif label == "emulexdriver" and package != "Inbox":
                install_data[label]["file"] = package

        if server.storage_details['emulex']:
            server.install_emulex(install_data)
            server.update_rdblacklist("lpfc")

        elif server.storage_details['brocade']:
            # The server is running the target kernel, install.
            logging.info("Installing Storage tools for %s" % best_target)
            logging.debug("Kernel %s is compatible with target %s" %
                          (server.running_kernel, best_target))
            server.install_storage(install_data)
            server.update_rdblacklist("bfa")

        # Install EMC Powerpath
        logging.info("Installing Powerpath")
        server.install_powerpath(install_data)

    if options.dry_run:
        print "Storage drivers would've been installed."
    else:
        print "Storage drivers are installed."


def signal_trap(sigtrap, frame):
    """Exit function triggered by caught signals"""
    if sigtrap == signal.SIGINT:
        print("Exiting. Received SIGINT.")
        sys.exit(RExit.ERR_USR_CANCELLED)
    elif sigtrap == signal.SIGQUIT:
        print("Exiting. Received SIGQUIT.")
        sys.exit(RExit.ERR_USR_CANCELLED)
    elif sigtrap == signal.SIGTERM:
        print("Exiting. Received SIGTERM.")
        sys.exit(RExit.ERR_TERMINATED)
    else:
        print("Exiting. Received undefined signal.")
        sys.exit(RExit.ERR_GENERAL)


if __name__ == '__main__':
    # register signal traps
    signal.signal(signal.SIGINT, signal_trap)
    signal.signal(signal.SIGQUIT, signal_trap)
    signal.signal(signal.SIGTERM, signal_trap)

    parser = optparse.OptionParser(version="%prog v{0}".format(__version__))
    parser.add_option("-i", "--install",
                      help="Install Managed Storage software packages.",
                      action="store_true", dest="install", default=False,)
    parser.add_option("-a", "--auto",
                      help="Automatically decom+install packages.",
                      action="store_true", dest="auto", default=False,)
    parser.add_option("-c", "--conf",
                      help="Config file to use instead of downloading it.",
                      action="store", dest="config_file", default=None)
    parser.add_option("-d", "--debug",
                      help="Run in debug mode.",
                      action="store_true", dest="debug", default=False,)
    parser.add_option("--decom",
                      help="Uninstall packages instead of install.",
                      action="store_true", dest="decom", default=False,)
    parser.add_option("-k", "--kernel",
                      help=("Use this kernel instead of autodetecting "
                            "kernel from the configs."),
                      action="store", dest="target_kernel", default=None,)
    parser.add_option("--nocache",
                      help="Force downloading of files.",
                      action="store_false", dest="use_cache", default=True,)
    parser.add_option("--dry-run",
                      help="Do not run commands that will change things.",
                      action="store_true", dest="dry_run", default=False,)
    parser.add_option("-v", "--verbose",
                      help="Show informational messages.",
                      action="store_true", dest="verbose", default=False,)
    parser.add_option("--latest-kernel",
                      help=("Upgrade to the latest kernel compatible with "
                            "LATEST from the configs."),
                      action="store_true", dest="force_kernel_upgrade",
                      default=False,)
    parser.add_option("--skip-spacecheck",
                      help=("Bypass the free space check of /boot - use with "
                            "extreme caution and care. Enables verbose mode."),
                      action="store_true", dest="skip_space", default=False,)
    parser.add_option("--brocade-legacy",
                      help=("Override the automatic detection of the platform "
                            "and force the use of the pre-6.9/7.3 actions "
                            "for Brocade decom mode."),
                      action="store_true", dest="brocade_legacy",
                      default=False,)
    toolgroup = optparse.OptionGroup(parser,
                                     "Storage Tools", "These options allow "
                                     "for tasks related to managing storage "
                                     "outside of the installation and "
                                     "removal of software.")
    toolgroup.add_option("-s", "--status",
                         help="Checks the health of the managed storage "
                         "implementation.", action="store_true",
                         dest="status", default=False,)
    parser.add_option_group(toolgroup)
    (options, args) = parser.parse_args()
    log_level = logging.WARN
    if options.debug:
        log_level = logging.DEBUG
    elif options.verbose:
        log_level = logging.INFO
    logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level)
    logging.addLevelName(31, "NOTE")
    logging.addLevelName(32, "HEALTH")

    # report what version is running in all use cases
    logging.info("Running version %s" % (__version__, ))

    # this is temporary for user migration; we're introducing the need to
    # explicitly pass -i / --install, instead of "just doing it" when no
    # option is passed today. This is to prepare for a future release adding
    # a new feature, which will then become mandatory to use. Users need
    # time to update scripts and stuff
    if not any([options.install, options.auto, options.decom,
                options.status]):
        logging.error("A commandline parameter is required to initiate "
                      "actions. Please use '-h' or '--help' to see available "
                      "flags.")
        sys.exit(RExit.ERR_USAGE)

    # due to re-using the INI key for both old and new Brocade, it's only
    # possible to use --brocade-legacy in decom; if attempted to install,
    # it downloads a RPM thinking it's a tarball and blows up
    if options.brocade_legacy and not options.decom:
        logging.error("Using --brocade-legacy requires --decom mode.")
        sys.exit(RExit.ERR_GENERAL)

    # we need to change to a safe working directory here, as several
    # paths of travel download files (even just an INI) and we can't be
    # sitting in /boot or whatnot
    cachedir = '/var/cache/rs-managed-storage'
    startdir = os.getcwd()
    if not os.path.exists(cachedir):
        orig_umask = os.umask(0)
        os.makedirs(cachedir, mode=0o755)
        os.umask(orig_umask)

    if options.config_file:
        if os.path.isfile(options.config_file):
            options.config_file = os.path.abspath(options.config_file)
        else:
            logging.error("Config file %s does not exist" %
                          options.config_file)
            sys.exit(RExit.ERR_CFG_MISSING)

    logging.info("Using cache directory %s" % cachedir)
    os.chdir(cachedir)

    # status needs to take priority and nullify any other options
    if options.status:
        options.decom = False
        options.install = False
        options.auto = False
        main(options)
    elif options.auto:
        run_auto(options)
    elif options.decom and not options.auto:
        run_decom(options)
    elif options.install:
        main(options)
    else:
        parser.print_help()
        sys.exit(RExit.ERR_USAGE)

    # restore the user's directory
    os.chdir(startdir)

    logging.info("Done")
    sys.exit(RExit.OK)
