예제 #1
0
    def _TestLoadingConfigFile(self):
        # test loading the config file
        all_ok = True
        if not (self.opts.dry_run or self.opts.no_verify):
            logging.info("Testing the new config file...")
            cfg = config.ConfigWriter(cfg_file=self.opts.CONFIG_DATA_PATH,
                                      accept_foreign=self.opts.ignore_hostname,
                                      offline=True)
            # if we reached this, it's all fine
            vrfy = cfg.VerifyConfig()
            if vrfy:
                logging.error("Errors after conversion:")
                for item in vrfy:
                    logging.error(" - %s", item)
                all_ok = False
            else:
                logging.info("File loaded successfully after upgrading")
            del cfg

        if self.opts.downgrade:
            action = "downgraded"
            out_ver = "%s.%s" % (DOWNGRADE_MAJOR, DOWNGRADE_MINOR)
        else:
            action = "upgraded"
            out_ver = constants.RELEASE_VERSION
        if all_ok:
            cli.ToStderr("Configuration successfully %s to version %s.",
                         action, out_ver)
        else:
            cli.ToStderr(
                "Configuration %s to version %s, but there are errors."
                "\nPlease review the file.", action, out_ver)
예제 #2
0
def Main():
    """Main routine.

  """
    opts = ParseOptions()

    utils.SetupToolLogging(opts.debug,
                           opts.verbose,
                           toolname=os.path.splitext(
                               os.path.basename(__file__))[0])

    try:
        # List of files to delete. Contains tuples consisting of the absolute path
        # and a boolean denoting whether a backup copy should be created before
        # deleting.
        clean_files = [
            (pathutils.CONFD_HMAC_KEY, True),
            (pathutils.CLUSTER_CONF_FILE, True),
            (pathutils.CLUSTER_DOMAIN_SECRET_FILE, True),
        ]
        clean_files.extend(map(lambda s: (s, True), pathutils.ALL_CERT_FILES))
        clean_files.extend(
            map(lambda s: (s, False),
                ssconf.SimpleStore().GetFileList()))

        if not opts.yes_do_it:
            cli.ToStderr(
                "Cleaning a node is irreversible. If you really want to"
                " clean this node, supply the --yes-do-it option.")
            return constants.EXIT_FAILURE

        logging.info("Stopping daemons")
        result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-all"],
                              interactive=True)
        if result.failed:
            raise Exception("Could not stop daemons, command '%s' failed: %s" %
                            (result.cmd, result.fail_reason))

        for (filename, backup) in clean_files:
            if os.path.exists(filename):
                if opts.backup and backup:
                    logging.info("Backing up %s", filename)
                    utils.CreateBackup(filename)

                logging.info("Removing %s", filename)
                utils.RemoveFile(filename)

        logging.info("Node successfully cleaned")
    except Exception, err:  # pylint: disable=W0703
        logging.debug("Caught unhandled exception", exc_info=True)

        (retcode, message) = cli.FormatError(err)
        logging.error(message)

        return retcode