4 # Copyright (C) 2012 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 """Script to configure the node daemon.
31 from ganeti import cli
32 from ganeti import constants
33 from ganeti import pathutils
34 from ganeti import ssconf
35 from ganeti import utils
39 """Parses the options passed to the program.
41 @return: Options and arguments
44 parser = optparse.OptionParser(usage="%prog [--no-backup]",
45 prog=os.path.basename(sys.argv[0]))
46 parser.add_option(cli.DEBUG_OPT)
47 parser.add_option(cli.VERBOSE_OPT)
48 parser.add_option(cli.YES_DOIT_OPT)
49 parser.add_option("--no-backup", dest="backup", default=True,
51 help="Whether to create backup copies of deleted files")
53 (opts, args) = parser.parse_args()
55 return VerifyOptions(parser, opts, args)
58 def VerifyOptions(parser, opts, args):
59 """Verifies options and arguments for correctness.
63 parser.error("No arguments are expected")
74 utils.SetupToolLogging(opts.debug, opts.verbose)
77 # List of files to delete. Contains tuples consisting of the absolute path
78 # and a boolean denoting whether a backup copy should be created before
81 (pathutils.CONFD_HMAC_KEY, True),
82 (pathutils.CLUSTER_CONF_FILE, True),
83 (pathutils.CLUSTER_DOMAIN_SECRET_FILE, True),
85 clean_files.extend(map(lambda s: (s, True), pathutils.ALL_CERT_FILES))
86 clean_files.extend(map(lambda s: (s, False),
87 ssconf.SimpleStore().GetFileList()))
89 if not opts.yes_do_it:
90 cli.ToStderr("Cleaning a node is irreversible. If you really want to"
91 " clean this node, supply the --yes-do-it option.")
92 return constants.EXIT_FAILURE
94 logging.info("Stopping daemons")
95 result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-all"],
98 raise Exception("Could not stop daemons, command '%s' failed: %s" %
99 (result.cmd, result.fail_reason))
101 for (filename, backup) in clean_files:
102 if os.path.exists(filename):
103 if opts.backup and backup:
104 logging.info("Backing up %s", filename)
105 utils.CreateBackup(filename)
107 logging.info("Removing %s", filename)
108 utils.RemoveFile(filename)
110 logging.info("Node successfully cleaned")
111 except Exception, err: # pylint: disable=W0703
112 logging.debug("Caught unhandled exception", exc_info=True)
114 (retcode, message) = cli.FormatError(err)
115 logging.error(message)
119 return constants.EXIT_SUCCESS