Exemple #1
0
def Main():
    """Main routine.

  """
    opts = ParseOptions()

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

    try:
        data = common.LoadData(sys.stdin.read(), _DATA_CHECK)

        # Check if input data is correct
        common.VerifyClusterName(data, JoinError, constants.SSHS_CLUSTER_NAME)
        common.VerifyCertificateSoft(data, JoinError)

        # Update SSH files
        UpdateSshDaemon(data, opts.dry_run)
        UpdateSshRoot(data, opts.dry_run)

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

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

        return retcode
Exemple #2
0
def Main():
    """Main routine.

  """
    opts = ParseOptions()

    utils.SetupToolLogging(opts.debug, opts.verbose)

    try:
        data = common.LoadData(sys.stdin.read(), _DATA_CHECK)

        # Check if input data is correct
        common.VerifyClusterName(data, SshUpdateError,
                                 constants.SSHS_CLUSTER_NAME)
        common.VerifyCertificateSoft(data, SshUpdateError)

        # Update / Generate SSH files
        UpdateAuthorizedKeys(data, opts.dry_run)
        UpdatePubKeyFile(data, opts.dry_run)
        GenerateRootSshKeys(data, opts.dry_run)

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

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

        return retcode
Exemple #3
0
def Main():
    """Main routine.

  """
    opts = ParseOptions()

    utils.SetupToolLogging(opts.debug, opts.verbose)

    try:
        getent = runtime.GetEnts()

        data = common.LoadData(sys.stdin.read(), SetupError)

        cluster_name = common.VerifyClusterName(data, SetupError,
                                                constants.NDS_CLUSTER_NAME)
        cert_pem = common.VerifyCertificateStrong(data, SetupError)
        ssdata = VerifySsconf(data, cluster_name)

        logging.info("Writing ssconf files ...")
        ssconf.WriteSsconfFiles(ssdata, dry_run=opts.dry_run)

        logging.info("Writing node daemon certificate ...")
        utils.WriteFile(pathutils.NODED_CERT_FILE,
                        data=cert_pem,
                        mode=pathutils.NODED_CERT_MODE,
                        uid=getent.masterd_uid,
                        gid=getent.masterd_gid,
                        dry_run=opts.dry_run)
        common.GenerateClientCertificate(data, SetupError)

        if (data.get(constants.NDS_START_NODE_DAEMON) and  # pylint: disable=E1103
                not opts.dry_run):
            logging.info("Restarting node daemon ...")

            stop_cmd = "%s stop-all" % pathutils.DAEMON_UTIL
            noded_cmd = "%s start %s" % (pathutils.DAEMON_UTIL,
                                         constants.NODED)
            mond_cmd = ""
            if constants.ENABLE_MOND:
                mond_cmd = "%s start %s" % (pathutils.DAEMON_UTIL,
                                            constants.MOND)

            cmd = "; ".join([stop_cmd, noded_cmd, mond_cmd])

            result = utils.RunCmd(cmd, interactive=True)
            if result.failed:
                raise SetupError(
                    "Could not start the node daemons, command '%s'"
                    " failed: %s" % (result.cmd, result.fail_reason))

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

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

        return retcode
    else:
        return constants.EXIT_SUCCESS
def Main():
    """Main routine.

  """
    opts = ParseOptions()

    utils.SetupToolLogging(opts.debug, opts.verbose)

    try:
        data = LoadData(sys.stdin.read())

        # Check if input data is correct
        VerifyClusterName(data)
        VerifyCertificate(data)

        # Update SSH files
        UpdateSshDaemon(data, opts.dry_run)
        UpdateSshRoot(data, opts.dry_run)

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

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

        return retcode
Exemple #5
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
Exemple #6
0
 def GetState(self):
   """Read the cluster state from the master daemon."""
   if self.opts.nodes:
     names = self.opts.nodes.split(",")
   else:
     names = []
   try:
     qcl = GetClient()
     result = qcl.QueryNodes(names, ["name", "offline", "drained"], False)
   except errors.GenericError, err:
     err_code, msg = cli.FormatError(err)
     Err(msg, exit_code=err_code)
Exemple #7
0
def WaitJob(opts, args):
    """Wait for a job to finish, not producing any output.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: Contains the job ID
  @rtype: int
  @return: the desired exit code

  """
    job_id = args[0]

    retcode = 0
    try:
        cli.PollJob(job_id, feedback_fn=lambda _: None)
    except errors.GenericError, err:
        (retcode, job_result) = cli.FormatError(err)
        ToStderr("Job %s failed: %s", job_id, job_result)
Exemple #8
0
def WatchJob(opts, args):
    """Follow a job and print its output as it arrives.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: Contains the job ID
  @rtype: int
  @return: the desired exit code

  """
    job_id = args[0]

    msg = ("Output from job %s follows" % job_id)
    ToStdout(msg)
    ToStdout("-" * len(msg))

    retcode = 0
    try:
        cli.PollJob(job_id)
    except errors.GenericError, err:
        (retcode, job_result) = cli.FormatError(err)
        ToStderr("Job %s failed: %s", job_id, job_result)
Exemple #9
0
def Main():
    """Main routine.

  """
    opts = ParseOptions()

    utils.SetupToolLogging(opts.debug, opts.verbose)

    try:
        data = common.LoadData(sys.stdin.read(), _DATA_CHECK)

        common.VerifyClusterName(data, SslSetupError,
                                 constants.NDS_CLUSTER_NAME)

        # Verifies whether the server certificate of the caller
        # is the same as on this node.
        common.VerifyCertificateStrong(data, SslSetupError)

        action = data.get(constants.NDS_ACTION)
        if not action:
            raise SslSetupError("No Action specified.")

        if action == constants.CRYPTO_ACTION_CREATE:
            common.GenerateClientCertificate(data, SslSetupError)
        elif action == constants.CRYPTO_ACTION_DELETE:
            DeleteClientCertificate()
            ClearMasterCandidateSsconfList()
        else:
            raise SslSetupError("Unsupported action: %s." % action)

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

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

        return retcode
    else:
        return constants.EXIT_SUCCESS