Exemple #1
0
class Repair(Command):
  """Repair the cache.

  Verify that the configuration is correct, that the source is
  reachable, then perform a full synchronisation of the cache.
  """

  def Run(self, conf, args):
    """Run the Repair command.

    See Command.Run() for full documentation on the Run() method.

    Args:
      conf: nss_cache.config.Config object
      args: list of arguments to be parsed by this command

    Returns:
      0 on success, nonzero on error
    """
    try:
      (options, args) = self.parser.parse_args(args)
    except SystemExit, e:
      return e.code

    if options.maps:
      self.log.info('Setting configured maps to %s', options.maps)
      conf.maps = options.maps

    (warnings, errors) = (0, 0)

    self.log.info('Verifying program and system configuration.')
    (config_warnings, config_errors) = config.VerifyConfiguration(conf)
    warnings += config_warnings
    errors += config_errors

    self.log.info('Verifying data sources.')
    errors += Verify().VerifySources(conf)

    self.log.info('verification: %d warnings, %d errors', warnings, errors)

    # Exit and report if config or source failed verification, because
    # we cannot reliably build a cache if either of these are faulty.
    if errors > 0:
      self.log.error('Too many errors in verification tests failed;'
                     ' repair aborted!')
      return 1

    # Rebuild local cache in full, which also verifies each cache.
    self.log.info('Rebuilding and verifying caches: %s.', conf.maps)
    return Update().UpdateMaps(conf=conf, incremental=False)
Exemple #2
0
class Verify(Command):
  """Verify the cache and configuration.

  Perform verification of the built caches and validation of the
  system NSS configuration.
  """

  def Run(self, conf, args):
    """Run the Verify command.

    See Command.Run() for full documentation on the Run() method.

    Args:
      conf: nss_cache.config.Config object
      args: list of arguments to be parsed

    Returns:
      count of warnings and errors detected when verifying
    """
    try:
      (options, args) = self.parser.parse_args(args)
    except SystemExit, e:
      return e.code

    if options.maps:
      self.log.info('Setting configured maps to %s', options.maps)
      conf.maps = options.maps

    (warnings, errors) = (0, 0)
    self.log.info('Verifying program and system configuration.')
    (config_warnings, config_errors) = config.VerifyConfiguration(conf)
    warnings += config_warnings
    errors += config_errors

    self.log.info('Verifying data sources.')
    errors += self.VerifySources(conf)

    self.log.info('Verifying data caches.')
    errors += self.VerifyMaps(conf)

    self.log.info('Verification result: %d warnings, %d errors',
                  warnings, errors)
    if warnings + errors:
      self.log.info('Verification failed!')
    else:
      self.log.info('Verification passed!')

    return warnings + errors
Exemple #3
0
 def testVerifyNoMapConfigurationIsError(self):
   conf_file = open(self.conf_filename, 'w')
   conf_file.write('[DEFAULT]\n'
                   'source = foo\n'
                   'cache = foo\n'
                   'maps = \n'
                   'timestamp_dir = foo\n')
   conf_file.close()
   config.LoadConfig(self.conf)
   nsswitch_filename = os.path.join(self.workdir, 'nsswitch.conf')
   nsswitch_file = open(nsswitch_filename, 'w')
   nsswitch_file.write('passwd: files ldap\n')
   nsswitch_file.close()
   self.assertEquals((0, 1),
                     config.VerifyConfiguration(self.conf,
                                                nsswitch_filename))
   os.unlink(nsswitch_filename)
Exemple #4
0
 def testVerifyBadConfigurationIncrementsWarningCount(self):
   conf_file = open(self.conf_filename, 'w')
   conf_file.write('[DEFAULT]\n'
                   'source = foo\n'
                   'cache = foo\n'
                   'maps = passwd, group, shadow\n'
                   'timestamp_dir = foo\n')
   conf_file.close()
   config.LoadConfig(self.conf)
   nsswitch_filename = os.path.join(self.workdir, 'nsswitch.conf')
   nsswitch_file = open(nsswitch_filename, 'w')
   nsswitch_file.write('passwd: files ldap\n')
   nsswitch_file.write('group: files db\n')
   nsswitch_file.write('shadow: files db\n')
   nsswitch_file.close()
   self.assertEquals((1, 0),
                     config.VerifyConfiguration(self.conf,
                                                nsswitch_filename))
   os.unlink(nsswitch_filename)
Exemple #5
0
 def testVerifyBadConfigurationWithCache(self):
   conf_file = open(self.conf_filename, 'w')
   conf_file.write('[DEFAULT]\n'
                   'source = foo\n'
                   'cache = files\n'
                   'maps = passwd, group, shadow\n'
                   'timestamp_dir = foo\n'
                   'files_cache_filename_suffix = cache')
   conf_file.close()
   config.LoadConfig(self.conf)
   nsswitch_filename = os.path.join(self.workdir, 'nsswitch.conf')
   nsswitch_file = open(nsswitch_filename, 'w')
   nsswitch_file.write('passwd: files\n')
   nsswitch_file.write('group: files\n')
   nsswitch_file.write('shadow: files\n')
   nsswitch_file.close()
   self.assertEquals((3, 0),
                     config.VerifyConfiguration(self.conf,
                                                nsswitch_filename))
   os.unlink(nsswitch_filename)