• File: open_case.py
  • Full Path: /home3/farmfre2/public_html/ALFA_DATA/alfasymlink/root/usr/lib/python2.7/site-packages/redhat_support_tool/plugins/open_case.py
  • Date Modified: 03/28/2024 3:28 PM
  • File size: 22.64 KB
  • MIME-type: text/x-script.python
  • Charset: utf-8
# -*- coding: utf-8 -*-

#
# Copyright (c) 2012 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#           http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from optparse import Option
from redhat_support_lib.infrastructure.errors import RequestError, \
    ConnectionError
from redhat_support_tool.helpers.confighelper import EmptyValueError, _
from redhat_support_tool.helpers.launchhelper import LaunchHelper
from redhat_support_tool.helpers.constants import Constants
from redhat_support_tool.helpers.genericinteractiveprompt import GenericPrompt
from redhat_support_tool.plugins import Plugin
from redhat_support_tool.plugins.add_attachment import AddAttachment
import os
import redhat_support_tool.helpers.apihelper as apihelper
import redhat_support_tool.helpers.common as common
import redhat_support_tool.helpers.recommendationprompter as \
                                        recommendationprompter
import redhat_support_tool.helpers.confighelper as confighelper
import subprocess as sub
import logging
import sys


__author__ = 'Spenser Shumaker <sshumake@redhat.com>'
logger = logging.getLogger("redhat_support_tool.plugins.add_case")


class OpenCase(Plugin):
    plugin_name = 'opencase'
    comment = None
    attachment = None
    _productsAry = None
    _caseNumber = None
    default_product_name = 'Red Hat Enterprise Linux'

    @classmethod
    def get_usage(cls):
        '''
        The usage statement that will be printed by OptionParser.

        Example:
            - %prog -c CASENUMBER [options] <comment text here>
        Important: %prog is a OptionParser built-in.  Use it!
        '''
        return _('%prog -s SUMMARY -p PRODUCT -v VERSION -d DESCRIPTION '
                 '[options]')

    @classmethod
    def get_desc(cls):
        '''
        The description statement that will be printed by OptionParser.

        Example:
            - 'Use the \'%s\' command to add a comment to a case.'\
             % cls.plugin_name
        '''
        return _('Use the \'%s\' command to open a new support case.')\
             % cls.plugin_name

    @classmethod
    def get_epilog(cls):
        '''
        The epilog string that will be printed by OptionParser.  Usually
        used to print an example of how to use the program.

        Example:
         Examples:
          - %s -c 12345678 Lorem ipsum dolor sit amet, consectetur adipisicing
          - %s -c 12345678
        '''
        return _("""Examples:
  - %s -s "Case Summary" -p "Red Hat Virtualization" -v 3.0 -d "Case description"
""") % (cls.plugin_name)

    @classmethod
    def get_options(cls):
        '''
        Subclasses that need command line options should override this method
        and return an array of optparse.Option(s) to be used by the
        OptionParser.

        Example:
         return [Option("-f", "--file", action="store",
                        dest="filename", help='Some file'),
                 Option("-c", "--case",
                        action="store", dest="casenumber",
                        help='A case')]

         Would produce the following:
         Command (? for help): help mycommand

         Usage: mycommand [options]

         Use the 'mycommand' command to find a knowledge base solution by ID
         Options:
           -h, --help  show this help message and exit
           -f, --file  Some file
           -c, --case  A case
         Example:
          - mycommand -c 12345 -f abc.txt

        '''
        return [Option('-p', '--product', dest='product',
                        help=_('The product the case will be opened against. '
                                '(required)'), default=None),
                Option('-v', '--version', dest='version',
                        help=_('The version of the product the case '
                                'will be opened against. (required)'),
                       default=None),
                Option('-s', '--summary', dest='summary',
                        help=_('A summary for the case (required)'),
                        default=None),
                Option('-d', '--description', dest='description',
                        help=_('A description for the case. (required)'),
                        default=None),
                Option('-S', '--severity', dest='severity',
                        help=_('The severity of the case. (optional)'),
                        default=None),
                Option('-a', '--attachment', dest='attachment',
                        help=_('Add an attachment to the newly opened case. '
                               '(optional)'),
                        default=None),
                Option("-o", "--obfuscate", dest="obfuscate", action='store_true',
                        help=_('Obfuscate the attachment by running it through '
                               'soscleaner to clean hostnames and IP addresses '
                               '(requires root user privileges)'),
                        default=False),
                Option('-g', '--casegroup', dest='casegroup',
                        help=_('Add this case to the designated case group. '
                               '(optional)'),
                        default=None)]

    def _check_product(self):
        msg = _("ERROR: %s requires a product.")\
                    % self.plugin_name
        self._productsAry = None
        been_verified = False
        if not self._options['product']:
            if not common.is_interactive():
                print msg
                raise Exception(msg)
            been_verified = self._interactive_product_selection(been_verified)
        else:
            # User supplied a product
            self._productsAry = common.get_products()

        if not been_verified:
            inArray = False
            for product in self._productsAry:
                if product.get_name().lower() == self._options['product'].\
                        lower():
                    inArray = True
                    self._options['product'] = product.get_name()
                    break
            if not inArray:
                msg = _("ERROR: Invalid product provided.")
                print msg
                raise Exception(msg)

    def _interactive_product_selection(self, been_verified):
        self._productsAry = common.get_products()
        cfg = confighelper.get_config_helper()
        # Get Default product from configuration File
        default_product = cfg.get(option='default_product')
        needs_user_input = True
        if default_product is None or default_product == '':
            # If not in config, Default to RHEL
            default_product = self.default_product_name
        searched_item = next((val for val in self._productsAry if val.get_name() == default_product), None)
        if searched_item is not None and searched_item != '':
            line = raw_input(_('Do you want to use the default product - "%s" (y/N)?:')
                             % (default_product))
            if str(line).strip().lower() == 'y':
                self._options['product'] = searched_item.get_name()
                needs_user_input = False
                been_verified = True
            if needs_user_input:
                been_verified = self._prompt_for_product(been_verified, cfg, searched_item)
        return been_verified

    def _prompt_for_product(self, been_verified, cfg, searched_item):
        common.print_products(self._productsAry)
        while True:
            line = raw_input(_('Please select a product (or \'q\' '
                              'to exit): '))
            if str(line).strip() == 'q':
               raise Exception()
            try:
               line = int(line)
            # pylint: disable=W0702
            except:
                print _("ERROR: Invalid product selection.")
                continue
            if line in range(1, len(self._productsAry) + 1) and line != '':
                self._options['product'] = self._productsAry[line - 1].get_name()
                been_verified = True
                break
            else:
                print _("ERROR: Invalid product selection.")
        if self._options['product'] != searched_item.get_name():
            cfg._prompt_for_default_product(self._options['product'])
        return been_verified

    def _check_version(self):
        msg = _("ERROR: %s requires a version.") \
                % self.plugin_name

        beenVerified = False
        versions = None
        for product in self._productsAry:
            if self._options['product'] == product.get_name():
                versions = product.get_versions()
                break

        if not self._options['version']:
            if common.is_interactive():
                common.print_versions(versions)
                while True:
                    line = raw_input(_('Please select a version (or \'q\' '
                                       'to exit): '))
                    if str(line).strip() == 'q':
                        raise Exception()
                    try:
                        line = int(line)
                    # pylint: disable=W0702
                    except:
                        print _("ERROR: Invalid version selection.")
                        continue

                    if line in range(1, len(versions) + 1) and line != '':
                        self._options['version'] = versions[line - 1]
                        beenVerified = True
                        break
                    else:
                        print _("ERROR: Invalid version selection.")
            else:
                print msg
                raise Exception(msg)

        if not beenVerified:
            inArray = False
            for version in versions:
                if version.lower() == self._options['version'].lower():
                    inArray = True
                    self._options['version'] = version
                    break
            if not inArray:
                msg = _("ERROR: Invalid version provided.")
                print msg
                raise Exception(msg)

    def _check_summary(self):
        msg = _("ERROR: %s requires a summary.")\
                    % self.plugin_name

        if not self._options['summary']:
            if common.is_interactive():
                while True:
                    line = raw_input(_('Please enter a summary (or \'q\' '
                                       'to exit): '))
                    try:
                        line = str(line).strip()
                    # pylint: disable=W0702
                    except:
                        print _("ERROR: Invalid summary selection.")
                    if str(line).strip() == 'q':
                        raise Exception()
                    if(line == ''):
                        print _("ERROR: Invalid summary selection.")
                    else:
                        self._options['summary'] = line
                        break
            else:
                print msg
                raise Exception(msg)

    def _check_description(self):
        msg = _("ERROR: %s requires a description.")\
                    % self.plugin_name

        if not self._options['description']:
            if common.is_interactive():
                while True:
                    userinput = []
                    try:
                        print _('Please enter a description (Ctrl-D on an'
                                ' empty line when complete):')
                        while True:
                            userinput.append(raw_input())
                    except EOFError:
                        # User pressed Ctrl-d
                        description = str('\n'.join(userinput)).strip()
                        if description == '':
                            print _("ERROR: Invalid description.")
                        else:
                            self._options['description'] = description.decode(
                                                                    'utf_8')
                            break
            else:
                print msg
                raise Exception(msg)

    def _check_severity(self):
        msg = _("ERROR: Invalid severity selection")
        if not self._options['severity']:
            if common.is_interactive():
                severitiesAry = common.get_severities()
                common.print_severities(severitiesAry)
                while True:
                    line = raw_input(_('Please select a severity (or \'q\' '
                                       'to exit): '))
                    if str(line).strip() == 'q':
                        raise Exception()
                    try:
                        line = int(line)
                    # pylint: disable=W0702
                    except:
                        print msg
                        continue

                    if line in range(1, len(severitiesAry) + 1) and line != '':
                        self._options['severity'] = severitiesAry[line - 1]
                        break
                    else:
                        print msg
        else:
            match = False
            for severity in common.get_severities():
                if self._options['severity'].lower() in severity.lower():
                    match = True
                    self._options['severity'] = severity
                    break
            if(not match):
                msg = _("ERROR: Invalid severity specified.")
                print msg

    def _check_case_group(self):
        if not self._options['casegroup']:
            if common.is_interactive():
                self._interactive_case_group_action()
        else:
            match = False
            for group in common.get_groups():
                if self._options['casegroup'].lower() in \
                                        group.get_name().lower():
                    match = True
                    self._options['casegroup'] = group.get_name()
                    self._options['casegroupnumber'] = group.get_groupNum()
                    logger.log(logging.INFO,
                               'Casegroup(%s) casegroupnumber(%s)' % (
                               self._options['casegroup'],
                               self._options['casegroupnumber']))
                    break
            if(not match):
                msg = _("ERROR: Invalid case group specified.")
                print msg

    def _interactive_case_group_action(self):
        groupsAry = common.get_groups()
        # Try to get default case group for user
        default_case_group = next((val for val in groupsAry if val.get_isDefault()), None)
        if default_case_group is None or default_case_group == '':
            # If no default selected by user then use Ungrouped Case as default ( number always -1 )
            default_case_group = next((val for val in groupsAry if val.get_groupNum() == "-1"), None)
        if default_case_group is not None or default_case_group != '':
            line = raw_input(_('Would you like to use the default (%s) Case Group (y/N)? :')
                             % default_case_group.get_name())
            if str(line).strip().lower() == 'n':
                self._prompt_for_case_group(groupsAry)
            else:
                self._options['casegroup'] = default_case_group.get_name()
                self._options['casegroupnumber'] = default_case_group.get_groupNum()
        else:
            # Unable to find Default group for user or Ungrouped Case Group (Highly unlikely)
            # Need to ask user for case Group
            self._prompt_for_case_group(groupsAry)

    def _prompt_for_case_group(self, groupsAry):
        msg = _("ERROR: Invalid case group selection")
        common.print_groups(groupsAry)
        while True:
            line = raw_input(_('Please select a case group (or \'q\' '
                             'to exit): '))
            if str(line).strip() == 'q':
                raise Exception()
            try:
                line = int(line)
            # pylint: disable=W0702
            except:
                print msg
                continue

            if line in range(1, len(groupsAry) + 1) and line != '':
                self._options['casegroup'] = groupsAry[line - 1].get_name()
                self._options['casegroupnumber'] = groupsAry[line - 1].get_groupNum()
                logger.log(logging.INFO,
                    'Casegroup(%s) casegroupnumber(%s)' % (
                        self._options['casegroup'],
                        self._options['casegroupnumber']))
                break
            else:
                 print msg

    def validate_args(self):
        self._check_product()
        self._check_version()
        self._check_summary()
        self._check_description()
        self._check_severity()
        self._check_case_group()

    def non_interactive_action(self):
        api = None

        if self._options['obfuscate']:
            self._options['summary'] = common.obfuscate_text(self._options['summary'])
            self._options['description'] = common.obfuscate_text(self._options['description'])

        try:
            api = apihelper.get_api()

            case = api.im.makeCase()
            case.summary = self._options['summary']
            case.product = self._options['product']
            case.version = self._options['version']
            case.description = self._options['description']
            case.severity = self._options['severity']

            if self._options['casegroup']:
                case.groupNumber = self._options['casegroupnumber']

            if common.is_interactive():
                line = raw_input(_("Would you like to see if there's a solution"
                                   ' to this problem before opening a support '
                                   'case? (y/N) '))
                line = str(line).strip().lower()
                if line == 'y':
                    recommendations = api.problems.diagnoseCase(case)
                    recprompt, recdoc = \
                        recommendationprompter.generate_metadata(
                                                            recommendations)
                    lh = LaunchHelper(GenericPrompt)
                    lh.run('', recprompt, prompt=_(\
                           'Selection (q returns to case creation menu): '))
                    line = raw_input(\
                                _('Would you still like to open the support'
                                  ' case? (Y/n) '))
                    if line.lower() == 'n':
                        print _('Support case not created.')
                        return

            cs = api.cases.add(case)
            if cs.get_caseNumber() is None:
                msg = _("ERROR: There was a problem creating your case.")
                print msg
                raise Exception(msg)
            self._caseNumber = cs.get_caseNumber()
            print '%s%s%s' % (Constants.BOLD,
                                      str('-' * Constants.MAX_RULE),
                                      Constants.END)
            msg = _("Support case %s has successfully been opened.\n") % \
                self._caseNumber
            print msg
            logger.log(logging.INFO, msg)

            # Attach a file
            if self._options['attachment']:
                common.set_interactive(True)
                lh = LaunchHelper(AddAttachment)
                lh.run('-c %s -d \'[RHST] File %s \' %s %s' % (self._caseNumber,
                       os.path.basename(self._options['attachment']),
                       '-o' if self._options['obfuscate'] else '',
                       self._options['attachment']))
            elif (os.geteuid() == 0):
                sys.stdout.write(_(
    'Would you like Red Hat Support Tool to automatically generate and\n'
    'attach a SoS report to %s now? (y/N) ') % (self._caseNumber))
                line = raw_input()

                line = str(line).strip()
                if line == 'y':
                    # retval = os.system('sosreport')
                    p = sub.Popen(['sosreport', '--batch'],
                                  stdout=sub.PIPE, stderr=sub.STDOUT)
                    output = p.communicate()[0].split('\n')
                    for out in output:
                        if '.tar.' in out:
                            path = str(out).strip()
                            common.set_interactive(True)
                            lh = LaunchHelper(AddAttachment)
                            lh.run('-c %s %s %s' % (self._caseNumber,
                                   '-o' if self._options['obfuscate'] else '',
                                   path))
                            break
            else:
                print _(
   'Please attach a SoS report to support case %s. Create a SoS report as\n'
   'the root user and execute the following command to attach the SoS report\n'
   'directly to the case:\n'
   ' redhat-support-tool addattachment -c %s <path to sosreport>\n') % \
    (self._caseNumber, self._caseNumber)

            if not self._options['attachment']:
                line = raw_input(_('Would you like to attach a file to %s '
                                   'at this time? (y/N) ') % self._caseNumber)
                line = str(line).strip()
                if line == 'y':
                    common.set_interactive(True)
                    lh = LaunchHelper(AddAttachment)
                    lh.run('-c %s %s -r' % (self._caseNumber,
                           '-o' if self._options['obfuscate'] else ''))
        except EmptyValueError, eve:
            msg = _('ERROR: %s') % str(eve)
            print msg
            logger.log(logging.WARNING, msg)
            raise
        except RequestError, re:
            msg = _('Unable to fulfill request with support services API. '
                    'Reason: %s') % re.reason
            print msg
            logger.log(logging.WARNING, msg)
            if re.reason == "Unauthorized":
                common.remove_user()
            raise
        except ConnectionError, errmsg:
            msg = _('Problem connecting to the support services '
                    'API.  Is the service accessible from this host?')
            print msg
            emg = (str(errmsg)).split(', ')
            print(emg[1])
            logger.log(logging.WARNING, msg)
            raise
        except Exception:
            msg = _("Unable to create case.")
            print msg
            logger.log(logging.WARNING, msg)
            raise