def execExternalCmd(args, env=None, dry_run=False): """Method for executing external scripts. @param args: List of command line arguments including the executable as the first argument. @param env: Dictionary for overriding environment for executing the external scripts. The environment of the backup process is used as-is by default. @param dry_run: If True, execute a Test Run without actually executing the external script. Used for testing purposes. (Default: False) @return: Tuple of standard output text, standard error text. """ if dry_run: logger.debug("Fake execution of command: %s", ' '.join(args)) return (0, '', '') logger.debug("Executing command: %s", ' '.join(args)) try: cmd = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=bufferSize, env = env) except Exception, e: raise errors.ExternalCmdError("External script execution failed.", "Command: %s" % ' '.join(args), "Error Message: %s" % str(e))
def loadPlugin(self, plugin, module): """ @param plugin: Plugin name. @param module: Module name. @return: Module object. """ if not self._plugins.has_key(module): # Fast path: see if the module has already been imported. if sys.modules.has_key(module): modobj = sys.modules[module] else: try: modobj = loadModule(module) except ImportError, e: raise errors.BackupConfigError( "Failed loading backup plugin: %s Module: %s" % (plugin, module), str(e)) self._plugins[plugin] = {'module': module, 'desc': '', 'methods': []} logger.debug("Backup plugin loaded: %s Module: %s" % (plugin, module)) if hasattr(modobj, 'methodList'): for (name, cls, func) in modobj.methodList: try: if issubclass(cls, BackupPluginBase): try: attr = getattr(cls, func) if not isinstance(attr, types.UnboundMethodType): raise except: raise errors.BackupBadPluginError( "Function for backup method %s is not a valid " "method name for class %s in plugin %s (%s)." % (name, cls.__name__, plugin, module)) self._methodDict[name] = (cls, func) self._plugins[plugin]['methods'].append(name) logger.debug("Registered backup method %s from" " plugin %s.", name, plugin) else: raise errors.BackupBadPluginError( "Class for backup method %s in plugin %s (%s)" " is not a subclass of BackupPluginBase." % (name, plugin, module)) except TypeError: raise errors.BackupBadPluginError( "The class for backup method %s in plugin" " %s (%s) is not a valid class." % (name, plugin, module)) else: raise errors.BackupBadPluginError("Plugin %s (%s) does not define" " any methods to be registered." % (plugin, module)) if hasattr(modobj, 'description'): self._plugins[plugin]['description'] = modobj.description return modobj
def initUmask(self): """Sets umask if the umask general option is defined in the configuration file. """ umask = self._globalConf.get('umask') if umask is not None: os.umask(int(umask, 8)) logger.debug("OS umask set to: %s", umask)
def initJobDir(self): """Creates the directory for storing backup files for job. """ path = self._jobConf['job_path'] if not os.path.isdir(path): try: os.makedirs(path) except: raise errors.BackupEnvironmentError("Creation of backup job " "directory (%s) failed." % path) logger.debug("Backup job directory (%s) created.", path)
def createBaseDir(self): """Creates the base directory defined by backup_path general option in configuration file. """ backup_path = self._globalConf['backup_path'] if not os.path.isdir(backup_path): try: os.makedirs(backup_path) except: raise errors.BackupFatalEnvironmentError("Creation of backup base " "directory (%s) failed." % backup_path) logger.debug("Backup base directory (%s) created.", backup_path)
def parseConfFile(self): """Parses and validates configuration file. """ confmgr = ConfigParser.SafeConfigParser() read_paths = confmgr.read(self._globalConf['config_path']) if not read_paths: raise errors.BackupFatalConfigError("Configuration file not found in any" " of the following locations: %s" % ' '.join(self._globalConf['config_path'])) logger.debug("Parsing configuration file: %s" % ', '. join(read_paths)) if not confmgr.has_section('general'): raise errors.BackupFatalConfigError("Missing mandatory section 'general' " "in configuration file(s): %s" % ' '.join(self._configPath)) self._jobsConf = {} for section in confmgr.sections(): if section == 'general': options = confmgr.items('general') global_conf = dict(options) elif section == 'plugins': self._plugins = dict(confmgr.items('plugins')) else: options = confmgr.items(section) self._jobsConf[section] = dict(options) if self._jobs is None: self._jobs = self._jobsConf.keys() for (k,v) in global_conf.items(): if self._globalOpts.has_key(k): self._globalConf[k] = self._cmdConf.get(k) or v else: raise errors.BackupFatalConfigError("Invalid general option %s " "in configuration file." % k) for k in self._reqGlobalOpts: if not self._globalConf.has_key(k): raise errors.BackupFatalConfigError("Required option %s is not" "defined in configuration " "file.", k) self._globalConf['backup_root'] = os.path.normpath( self._globalConf['backup_root']) backup_path_elem = [self._globalConf['backup_root'], ] if self._globalConf.has_key('hostname_dir'): backup_path_elem.append(str(platform.node()).split('.')[0]) backup_path_elem.append(date.today().strftime('%Y-%m-%d')) self._globalConf['backup_path'] = os.path.join(*backup_path_elem)
def loggingConfig(self): """Configures logging depending on the settings from configuration files and command line options. """ console_level = logmgr.getLogLevel(self._globalConf['console_loglevel']) if console_level is None: raise errors.BackupFatalConfigError("Invalid log level set in " "configuration file for option: " "console_loglevel") logfile_level = logmgr.getLogLevel(self._globalConf['logfile_loglevel']) if logfile_level is None: raise errors.BackupFatalConfigError("Invalid log level set in " "configuration file for option: " "logfile_loglevel") logmgr.configConsole(console_level) backup_path = self._globalConf['backup_path'] self.createBaseDir() filename_logfile = self._globalConf['filename_logfile'] log_path = os.path.join(backup_path, filename_logfile) logmgr.configLogFile(logfile_level, log_path) logger.debug("Activated logging to file: %s" % log_path)