Beispiel #1
0
from redhat_support_tool.plugins.add_comment import AddComment
from redhat_support_tool.helpers import apihelper, common, confighelper
from redhat_support_tool.helpers.launchhelper import LaunchHelper
import redhat_support_lib.utils.reporthelper as reporthelper
import redhat_support_lib.utils.confighelper as libconfighelper
import redhat_support_lib.utils.ftphelper as ftphelper
import os
import sys
import shutil
import logging

__author__ = 'Keith Robertson <*****@*****.**>'
__author__ = 'Spenser Shumaker <*****@*****.**>'

logger = logging.getLogger("redhat_support_tool.plugins.add_attachment")
libconfig = libconfighelper.get_config_helper()


class AddAttachment(Plugin):
    plugin_name = 'addattachment'
    comment = None
    attachment = None
    compressed_attachment = None
    upload_file = None
    split_attachment = False
    use_ftp = False
    max_split_size = libconfig.attachment_max_size

    @classmethod
    def get_usage(cls):
        '''
    def __init__(self,
                 username,
                 password,
                 url='https://api.access.redhat.com',
                 key_file=None,
                 cert_file=None,
                 proxy_url=None,
                 proxy_user=None,
                 proxy_pass=None,
                 ftp_host='dropbox.redhat.com',
                 ftp_port=21,
                 ftp_user=None,
                 ftp_pass=None,
                 ftp_dir="/incoming",
                 timeout=None,
                 userAgent=None,
                 no_verify_ssl=False,
                 ssl_ca=None):

        """
        Initialize an instance of the Red Hat Support Library

        :param username: User name for Red Hat Customer Portal
        :type username: string
        :param password: Password for Red Hat Customer Portal
        :type password: string
        :param url:
            Strata REST URL (by default this is https://api.access.redhat.com)

        :type url: string
        :param key_file:
            SSL key location for SSL authentication (not implemented)

        :type key_file: string
        :param cert_file:
            SSL certificate location for SSL authentication (not implemented)

        :type cert_file: string
        :param proxy_url: URL for HTTP/HTTPS proxy server (optional)
        :type proxy_url: string
        :param proxy_user: User name for HTTP/HTTPS proxy server (optional)
        :type proxy_user: string
        :param proxy_pass: Password for HTTP/HTTPS proxy server (optional)
        :type proxy_pass: string
        :param timeout: Request timeout (optional)
        :type timeout: string
        :param userAgent: User agent to set for API communications (optional)
        :type userAgent: string
        :param no_verify_ssl: If True, don't verify server identity (optional)
        :type no_verify_ssl: boolean
        :param ssl_ca: Path to an alternative certificate authority to trust
        :type ssl_ca: string/filepath
        :returns: Strata API object
        """

        # Make sure logger is initialized
        if len(logging.getLogger().handlers) == 0:
            logging.basicConfig(level=logging.CRITICAL)
        httpdebug = False
        if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
            httpdebug = True

        self._ua = None
        if userAgent:
            ua = {'User-Agent': userAgent}
        else:
            ua = {'User-Agent': USER_AGENT}
        config = confighelper.get_config_helper()
        config.username = username
        config.password = password
        config.url = url
        config.key_file = key_file
        config.cert_file = cert_file
        config.proxy_url = proxy_url
        config.proxy_user = proxy_user
        config.proxy_pass = proxy_pass
        config.ftp_host = ftp_host
        config.ftp_port = ftp_port
        config.ftp_user = ftp_user
        config.ftp_pass = ftp_pass
        config.ftp_dir = ftp_dir
        config.timeout = timeout
        config.userAgent = ua
        config.http_debug = httpdebug
        config.no_verify_ssl = no_verify_ssl
        config.ssl_ca = ssl_ca
        self.config = config

        contextmanager.add('proxy',
                           Proxy(ConnectionsPool(url=config.url,
                                                 key_file=config.key_file,
                                                 cert_file=config.cert_file,
                                                 timeout=config.timeout,
                                                 username=config.username,
                                                 password=config.password,
                                                 proxy_url=config.proxy_url,
                                                 proxy_user=config.proxy_user,
                                                 proxy_pass=config.proxy_pass,
                                                 debug=config.http_debug,
                                                 noverify=config.no_verify_ssl,
                                                 ssl_ca=config.ssl_ca),
                                 config.userAgent),
                           Mode.R)

        # Initialize the container classes.
        self.solutions = solutions()
        self.articles = articles()
        self.cases = cases()
        self.groups = groups()
        self.users = users()
        self.comments = comments()
        self.attachments = attachments()
        self.problems = problems()
        self.entitlements = entitlements()
        self.products = products()
        self.symptoms = symptoms()
        self.values = values()
        self.search = search()
        self.im = InstanceMaker()
from redhat_support_tool.helpers import apihelper, common, confighelper
from redhat_support_tool.helpers.launchhelper import LaunchHelper
import redhat_support_lib.utils.reporthelper as reporthelper
import redhat_support_lib.utils.confighelper as libconfighelper
import redhat_support_lib.utils.ftphelper as ftphelper
import os
import sys
import shutil
import logging


__author__ = 'Keith Robertson <*****@*****.**>'
__author__ = 'Spenser Shumaker <*****@*****.**>'

logger = logging.getLogger("redhat_support_tool.plugins.add_attachment")
libconfig = libconfighelper.get_config_helper()


class AddAttachment(Plugin):
    plugin_name = 'addattachment'
    comment = None
    attachment = None
    compressed_attachment = None
    upload_file = None
    split_attachment = False
    use_ftp = False
    max_split_size = libconfig.attachment_max_size

    @classmethod
    def get_usage(cls):
        '''
def ftp_attachment(fileName=None, caseNumber=None, fileChunk=None):

    config = confighelper.get_config_helper()

    if not fileName:
        raise Exception('ftp_file(%s) cannot be empty.' % fileName)
    logger.debug("Creating connection to FTP server %s" % config.ftp_host)
    if not caseNumber:
        caseNumber = 'RHST-upload'

    conn = None
    ftp = None
    fh = None
    # add http to host because if it is not prefixed it defaults to https
    try:
        if config.proxy_url != None:
            conn = Connection(url="http://" + config.ftp_host,
                                         manager=None,
                                         key_file=config.key_file,
                                         cert_file=config.cert_file,
                                         timeout=config.timeout,
                                         username=config.ftp_user,
                                         password=config.ftp_pass,
                                         proxy_url=config.proxy_url,
                                         proxy_user=config.proxy_user,
                                         proxy_pass=config.proxy_pass,
                                         debug=config.http_debug)
            httpconnection = conn.getConnection()

            hdr = {'Host': config.ftp_host,
                   'Proxy-Connection': 'Keep-Alive',
                   'Accept': 'application/xml'}
            if config.proxy_user and config.proxy_pass:
                auth = base64.encodestring("%s:%s" % \
                                           (config.proxy_user,
                                            config.proxy_pass)).strip()
                hdr['Proxy-authorization'] = "Basic %s" % auth
            # Critical step.  Proxy must know where to go.
            if sys.version_info[:2] == (2, 6):
                httpconnection._set_tunnel(config.ftp_host,
                                config.ftp_port,
                                hdr)
            else:
                httpconnection.set_tunnel(config.ftp_host,
                                config.ftp_port,
                                hdr)
            httpconnection.connect()

            ftp = FTP()
            ftp.host = config.ftp_host
            ftp.sock = httpconnection.sock
            ftp.af = ftp.sock.family
            ftp.file = ftp.sock.makefile('rb')
            ftp.welcome = ftp.getresp()
            ftp.login(user=config.ftp_user, passwd=config.ftp_pass)
        else:
            ftp = FTP(host=config.ftp_host, user=config.ftp_user,
                                            passwd=config.ftp_pass)
            ftp.login()
        if config.ftp_dir:
            ftp.cwd(config.ftp_dir)
        fh = open(fileName, 'rb')
        if fileChunk:
            fileSize = os.path.getsize(fileName)
            while fh.tell() < fileSize:
                chunkName = ("%s-%s.%03d" % (caseNumber,
                              os.path.basename(fileName), fileChunk['num']))
                fileChunk['names'].append(chunkName)
                logger.debug("Sending file %s over FTP" % chunkName)
                resp = _ftp_storbinary_chunk(ftp, 'STOR %s' % chunkName, fh,
                                             fileChunk['size'])
                if _ftp_error_return_code(resp):
                    raise Exception(resp)
                fileChunk['num'] += 1
        else:
            logger.debug("Sending file %s over FTP" % fileName)
            resp = ftp.storbinary('STOR %s-%s' % 
                                 (caseNumber, os.path.basename(fileName)), fh)
            if _ftp_error_return_code(resp):
                raise Exception(resp)
    finally:
        if fh: fh.close()
        if config.proxy_url != None:
            if conn: conn.close()
        else:
            if ftp: ftp.close()
    return resp