Пример #1
0
 def __init__(self, parser={}, *args, **kwargs):
     # Always set the defaults via the @property
     super().__init__(parser, *args, **kwargs)
     # We connect with the help of the PostgreSQL URL
     # postgresql://federer:grandestslam@localhost:5432/tennis
     url = 'postgresql://{}:{}@{}:{}/{}'
     url = url.format(self.user, self.password, self.host, self.port,
                      self.database)
     # The return value of create_engine() is our connection object
     try:
         self.engine = sqlalchemy.create_engine(url, client_encoding='utf8')
     except Exception as e:
         err = "Unable to create Postgres engine with  user = '******', password = '******',  host= '{H}', port = '{O}', database = '{D}'".format(
             U=self.user,
             P=obfpwd(self.password),
             H=self.host,
             O=self.port,
             D=self.database)
         log.error(err)
         raise RuntimeError(err)
     with warnings.catch_warnings():
         warnings.simplefilter("ignore", category=sa_exc.SAWarning)
         try:
             self.meta = sqlalchemy.MetaData(bind=self.engine, reflect=True)
         except Exception as e:
             err = "Unable to create Postgres Meta object from SQLAlchemy engine '{E}'".format(
                 E=str(self.engine))
             log.error(err)
             raise RuntimeError(err)
Пример #2
0
 def database(self):
     try:
         return self.DATABASE
     except (AttributeError, KeyError, ValueError) as e:
         err = "Attribute {A} is not set. ".format(A=str(stack()[0][3]))
         log.error(err)
         raise ValueError(err)
Пример #3
0
    def main(self):
        """"""
        if self.directory:      log.info("directory = '{D}'".format(D = self.DIRECTORY))
        if self.director_path:  log.info("director_path = '{D}'".format(D = self.DIRECTORPATH))
        if self.remove:         log.info("remove = '{D}'".format(D = self.REMOVE))
        if self.generate:       log.info("generate = '{D}'".format(D = self.GENERATE))
        log.info("test = '{D}'".format(D = self.test))

        if self.generate:
            self.dir_list = self.get_dir_list()
            self.gen_jobdefs()
            self.gen_filesets()
            self.gen_jobs()

        elif self.remove: 
            self.rem_jobdefs()
            self.rem_filesets()
            self.rem_jobs()
            
        else:
            err = ''.join(["No main command action called. \n", 
                           "Please use '--generate' to create the job files. \n",
                           "or         '--remove' to delete the job files. \n",
                           ])
            log.error(err)
            raise RuntimeError(err)
Пример #4
0
 def someparam(self):
     try:
         return self.SOMEPARAM
     except (AttributeError, KeyError, ValueError) as e:
         err = "Attribute {A} is not set. ".format(A=str(stack()[0][3]))
         log.error(err)
         raise ValueError(err)
Пример #5
0
 def log_level(self):
     try:
         return self.LOGLEVEL
     except (AttributeError, KeyError, ValueError) as e:
         err = "Attribute {A} is not set. ".format(A=str(stack()[0][3]))
         log.error(err)
         raise ValueError(err)
Пример #6
0
 def port(self):
     try:
         return self.PORT
     except (AttributeError, KeyError, ValueError) as e:
         err = "Attribute {A} is not set. ".format(A=str(stack()[0][3]))
         log.error(err)
         raise ValueError(err)
Пример #7
0
 def screendump(self):
     try:
         return self.SCREENDUMP
     except (AttributeError, KeyError, ValueError) as e:
         err = "Attribute {A} is not set. ".format(A=str(stack()[0][3]))
         log.error(err)
         raise ValueError(err)
Пример #8
0
 def create_paths(self):
     try:
         return self.CREATEPATHS
     except (AttributeError, KeyError, ValueError) as e:
         err = "Attribute {A} is not set. ".format(A=str(stack()[0][3]))
         log.error(err)
         raise ValueError(err)
Пример #9
0
 def meta(self, value):
     if isinstance(value, sqlalchemy.sql.schema.MetaData):
         self.META = value
     else:
         err = "Value '{V}'does not appear to be a valid 'sqlalchemy.sql.schema.MetaData' object".format(
             V=type(value))
         log.error(err)
         raise ValueError(err)
Пример #10
0
 def meta(self):
     try:
         return self.META
     except (AttributeError, KeyError, ValueError) as e:
         err = "Attribute {A} is not set. Try the 'connection' method. ".format(
             A=str(stack()[0][3]))
         log.error(err)
         raise ValueError(err)
Пример #11
0
 def outfile(self):
     try:
         return self.OUTFILE
     except (AttributeError, KeyError, ValueError) as e:
         err = "Attribute {A} is not set and cannot be null. ".format(
             A=str(stack()[0][3]))
         log.error(err)
         raise ValueError(err)
Пример #12
0
    def engine(self, value):
        if isinstance(value, sqlalchemy.engine.base.Engine):
            self.ENGINE = value

        else:
            err = "Value '{V}'does not appear to be a valid 'sqlalchemy.engine.base.Engine' object".format(
                V=type(value))
            log.error(err)
            raise ValueError(err)
Пример #13
0
 def generate(self, value):
     if value is None: 
         if self.directory is None or self.directory == "":
             err = "The Attribute 'directory' cannot be 'None' when using the 'generate' switch. Please specify the directory from which to generate backup config files. "
             log.error(err)
             raise RuntimeError(err)
         
     if value:   self.GENERATE = True
     else:       self.GENERATE = False
Пример #14
0
 def director_path(self, value):
     if value is None: value = "/etc/bareos/bareos-dir.d" 
     _value = str(value)
     _value = _value if _value.endswith(_delim) else _value + _delim
     # Do checks and such here
     if (not os.path.isdir(_value)):
         err = "Attribute '{A}. '{V}' does not appear to exist or is not readable.".format(A = str(stack()[0][3]), V = _value)
         log.error(err)
         raise ValueError(err)
     else:
         self.DIRECTORPATH = _value
Пример #15
0
 def port(self, value):
     if value is None: value = "5432"
     _value = str(value)
     # Do checks and such here
     if (not _value):
         err = "Attribute '{A} = {V}' does not appear to be valid.".format(
             A=str(stack()[0][3]), V=_value)
         log.error(err)
         raise ValueError(err)
     else:
         self.PORT = _value
Пример #16
0
 def user(self, value):
     if value is None: value = "bareospostgresro"
     _value = str(value)
     # Do checks and such here
     if (not _value):
         err = "Attribute '{A} = {V}' does not appear to be valid.".format(
             A=str(stack()[0][3]), V=_value)
         log.error(err)
         raise ValueError(err)
     else:
         self.USER = _value
Пример #17
0
 def someparam(self, value):
     if value is None: value = "MyParam"
     _value = str(value)
     # Do checks and such here
     if (not _value):
         err = "Attribute '{A} = {V}' does not appear to be valid for reason.".format(
             A=str(stack()[0][3]), V=_value)
         log.error(err)
         raise ValueError(err)
     else:
         self.SOMEPARAM = _value
Пример #18
0
 def database(self, value):
     if value is None: value = "bareos"
     _value = str(value)
     # Do checks and such here
     if (not _value):
         err = "Attribute '{A} = {V}' does not appear to be valid.".format(
             A=str(stack()[0][3]), V=_value)
         log.error(err)
         raise ValueError(err)
     else:
         self.DATABASE = _value
Пример #19
0
    def handler(service, *args, **kwargs):
        # Microsoft SQL (mssql)
        if re.match("^(\s*)ms(\s*)sql.*$", str(service.lower())):
            return DBHandler_MSSQL(*args, **kwargs)

        # MySQL
        elif re.match("^(\s*)my(\s*)sql.*$", str(service.lower())):
            return DBHandler_MYSQL(*args, **kwargs)

        else:
            log.error(MSG.DBHandlerNotProvided())
            raise TypeError('DBHandler service not provided.')
Пример #20
0
    def directory(self, value):
        if value is None: 
            err = "Parameter '{A}' has been set to 'None'. This may cause an error for certain operations.".format(A = str(stack()[0][3]))
            log.warning(err)
            self.DIRECTORY = value
            return
#             raise ValueError(err)
         
        _value = str(value)
        # Do checks and such here
        if (not os.path.isdir(_value)):
            err = "Attribute '{A}. '{V}' does not appear to exist or is not readable.".format(A = str(stack()[0][3]), V = _value)
            log.error(err)
            raise ValueError(err)
        else:
            self.DIRECTORY = _value
Пример #21
0
    def _check_db_name(self):
        try:
            # Errors if it doesnt exist
            self.config.db_name
            # Overrides self.config.db_host if it was passed in
            self.config.db_name = self.kwargs.pop("db_name",
                                                  self.config.db_name)

        except AttributeError as e:
            log.error(MSG.DBNameNotConfigured())
            self.config.db_name = self.default.db_name

        if (checkString(self.config.db_name) is False):
            log.error(MSG.DBNameImproperlyConfigured())
            raise AttributeError("Invalid database name '" +
                                 str(self.config.db_name) + "'")

        log.debug('DBHandler.db_name: ' + str(self.config.db_name))
Пример #22
0
    def _check_db_port(self):
        try:
            # Errors if it doesnt exist
            self.config.db_port
            # Overrides self.config.db_host if it was passed in
            self.config.db_port = self.kwargs.pop("db_port",
                                                  self.config.db_port)

        except AttributeError as e:
            log.error(MSG.DBPortNotConfigured())
            self.config.db_port = self.default.db_port

        if (checkInt(self.config.db_port) is False):
            log.error(MSG.DBPortImproperlyConfigured())
            raise AttributeError("Invalid database port '" +
                                 str(self.config.db_port) + "'")

        log.debug('DBHandler.db_port: ' + str(self.config.db_port))
Пример #23
0
    def _check_host(self):
        try:
            # Errors if it doesnt exist
            self.config.db_host
            # Overrides self.config.db_host if it was passed in
            self.config.db_host = self.kwargs.pop("db_host",
                                                  self.config.db_host)

        except AttributeError as e:
            log.error(MSG.DBHostNotConfigured())
            self.config.db_host = self.default.db_host

        if ((checkIP(self.config.db_host) is False)
                and (checkServername(self.config.db_host) is False)):
            log.error(MSG.DBHostImproperlyConfigured())
            raise AttributeError("Invalid database host name '" +
                                 str(self.config.db_host) + "'")

        log.debug('DBHandler.db_host: ' + str(self.config.db_host))
Пример #24
0
    def controlled_delete(self, p):
        _p = str(p)
        msg = "DELETING FILE: '{P}'".format(P = _p)
        if self.test: msg  += "(test only)" 
        log.warning(msg)

        filename = ntpath.basename(_p)
        lastdir  = ntpath.dirname(_p)
        lastdir  = lastdir.split(_delim)
        lastdir  = lastdir[len(lastdir)-1]
        tmpdir   = os.path.join(tempfile.gettempdir(), "bareos_director_deletes", lastdir)

        if not os.path.isdir(tmpdir):
            try: os.makedirs(tmpdir)
            except Exception as e:
                err = "Unable to create 'deletion' directory '{D}'. Aborting delete. Please remove files manually. (ERR: {E}".format(D = tmpdir, E = str(e))
                log.error(err)
                raise RuntimeError(err)

        dst = os.path.join(tmpdir, filename)
            
#         print("Copy from '{P}' to '{D}'".format(P = _p, D = dst) )
        msg = "copyfile({P}, {D})' ...".format(P = str(p), D = dst)
        if self.test: msg += "OK (test only)"
        else:
            try: 
                copyfile(str(p), dst)
                msg += "OK"
                log.debug(msg)        
            except Exception as e:
                msg += "FAILED! (ERROR: {E})".format(E=str(e))
                log.error(msg)        
                
        msg = "{P}.unlink ...".format(P = str(p))
        if self.test: msg += "OK (test only)"
        else:
            try: 
                p.unlink()
                msg += "OK"
                log.debug(msg)        
            except Exception as e:
                msg += "FAILED! (ERROR: {E})".format(E=str(e))
                log.error(msg)        
Пример #25
0
                            str(e), "). "
                            ]))
        # Sends the message and original error to the LOGGER
    #     _log_error(message, e)
        ### METHOD ACTIONS
#         raise FatalException(e) # Does not return
        return args, kwargs, None
  
  # FOR UNITTEST  
if __name__ == '__main__':
    import inspect
    from common.loghandler import log
    
    log.error("Starting handlers unittest. Ignore errors from 'handlers_unittest_ignore_me'", 
                app_name   = 'handlers_unittest_ignore_me',
                logfile    = 'system',
                log_level  = 10,
                screendump = False
                 )
    
    class test():
        def __init__(self):
            pass

    # Set 'e'
    try:
        this_error_was_passed_in_to_handler
    except Exception as pass_in_err:
        pass

    callobj = test()
    args = ('a', 0)
Пример #26
0
 def symlinks(self):
     try: return self.SYMLINKS
     except (AttributeError, KeyError, ValueError) as e:
         err = "Attribute {A} is not set. ".format(A = str(stack()[0][3]))
         log.error(err)
         raise ValueError(err)
Пример #27
0
    def customErr(self, callobj, args, kwargs, e, source, frame):
        """
        NAME
            errorhandler

        FILE
            errorhandler.py

        DESCRIPTION
            Used for generating both custom unhandled exceptions, and
            for handling exceptions with specific actions. Creates the opportunity
            for cutom code to be attached to errors and resolve them in a clean
            format

            An existing 'self' object must be passed in with an existing self.log
            object associated with it!!
                Uses the self.log() parameter of an EXISTING instantiated log object
                to generate output. A logger has to be instantiated in the calling
                class for this method to be used.

        EXAMPLE (USAGE)
            from inspect import getmembers
            from inspect import stack
            import errorhandler

            customerr = errorhandler.ErrorHandler(self.log)
            self.err  = customerr.err()

            try:
                print this wont work
            except:
            e = "".join(["ErrorIdentifier: ", "Additional information."])
            self.lasterr = self.err(e, getmembers(self), stack())

        METHODS
            ErrorHandler(self, e, source, frame)
                Error message handler. Generates logfile output
                    self   = The class object using the error handler. Must contain
                             a self.log object.

                    e      = The error message passed from the calling object. I.e.
                             except Exception, e:

                    source = The inspect.getmembers(self) passed in from the error
                             call.

                    frame  = The inspect.stack() passed in from the error call.

            TEMPLATE(self, e)
                Create custom error message and handling code

        HIDDEN METHODS
            _format_original_error(e)
            _log_error(self, message, e)

        """
        ###############################################################################
        # FOR NOW, LEAVE 'e' ALONE
        #         # Source is inspect.getmembers(self)
        #         # EXAMPLE SOURCE (a list of tuples):
        #         #[('MAX_LENGTH', 16384),
        #         # ('TSTART', 'TWILIOSOCK'),
        #         # ('__doc__', None),
        #         # ('__implemented__', <implementedBy twisted.internet.protocol.Protocol>),
        #         # ('__init__', <bound method Handler.__init__ of <twistedlisten.Handler instance at 0x02E05EB8>>),
        #         # ('__module__', 'twistedlisten'),
        #         # ('__providedBy__', <implementedBy twistedlisten.Handler>),
        #         # ('__provides__', <implementedBy twistedlisten.Handler>),
        #         # ('_buffer', ''),
        #         # ('_busyReceiving', False),
        #         # ('_checkdata', <bound method Handler._checkdata of <twistedlisten.Handler instance at 0x02E05EB8>>),
        #         # ('_parsedata', <bound method Handler._parsedata of <twistedlisten.Handler instance at 0x02E05EB8>>),
        #         # ('<some_method>', <bound method <class>.<method> of <someinstance instance at 0x02E05EB8>>),
        #         #]
        #         errorin = str(source[6][1])
        #         errorin = errorin.replace("implementedBy", "")
        #         errorin = "".join(c for c in errorin if c not in "<>")
        #         errorin = errorin + "." + str(frame[0][3])
        #         errorin = errorin + "(line:" + str(frame[0][2]) + "): "
        #         e = errorin + str(e)
        #         import handlers
        #333

        # This calls the proper handler method from 'handlers'
        # NOTE: The error keyword MUST MATCH THE METHOD NAME
        # NOTE: A string search is used to match the method name to contents
        #       of the error string passed in.
        #       THIS MEANS if you have two methods, 'ERR1' AND 'ERR10', 'ERR1'
        #       will always be the one found...SO IT IS RECOMMENDED YOU NAME
        #       THE METHODS VERY UNIQUELY AND CAREFULLY.
        #       I.e. 'StringNotFoundInLogError'

        for key in Handlers.__dict__.keys():
            if (str(key).lower() in str(e).lower()):
                return Handlers.__dict__[key](callobj, args, kwargs, e)

        log.error(''.join([
            "errorhandler.customErr: ", "Didn't find a matching handler for ",
            str(callobj), ",",
            str(args), ",",
            str(kwargs), ",",
            str(e)
        ]))
        key = "UnknownException"
        #         return Handlers.UnknownException(callobj, args, kwargs, e)
        return Handlers.__dict__[key](callobj, args, kwargs, e)
Пример #28
0
 def director_path(self):
     try: return self.DIRECTORPATH
     except (AttributeError, KeyError, ValueError) as e:
         err = "Attribute {A} is not set. ".format(A = str(stack()[0][3]))
         log.error(err)
         raise ValueError(err)
Пример #29
0
def _log_error(message, e):
    # Format message
    e = "".join([str(_format_original_error(e)), message])
    # Send to log
    log.error(e)
    return