Merge remote-tracking branch 'origin/stable-2.8'
[ganeti-local] / lib / tools / node_cleanup.py
1 #
2 #
3
4 # Copyright (C) 2012 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21 """Script to configure the node daemon.
22
23 """
24
25 import os
26 import os.path
27 import optparse
28 import sys
29 import logging
30
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
36
37
38 def ParseOptions():
39   """Parses the options passed to the program.
40
41   @return: Options and arguments
42
43   """
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,
50                     action="store_false",
51                     help="Whether to create backup copies of deleted files")
52
53   (opts, args) = parser.parse_args()
54
55   return VerifyOptions(parser, opts, args)
56
57
58 def VerifyOptions(parser, opts, args):
59   """Verifies options and arguments for correctness.
60
61   """
62   if args:
63     parser.error("No arguments are expected")
64
65   return opts
66
67
68 def Main():
69   """Main routine.
70
71   """
72   opts = ParseOptions()
73
74   utils.SetupToolLogging(opts.debug, opts.verbose)
75
76   try:
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
79     # deleting.
80     clean_files = [
81       (pathutils.CONFD_HMAC_KEY, True),
82       (pathutils.CLUSTER_CONF_FILE, True),
83       (pathutils.CLUSTER_DOMAIN_SECRET_FILE, True),
84       ]
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()))
88
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
93
94     logging.info("Stopping daemons")
95     result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-all"],
96                           interactive=True)
97     if result.failed:
98       raise Exception("Could not stop daemons, command '%s' failed: %s" %
99                       (result.cmd, result.fail_reason))
100
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)
106
107         logging.info("Removing %s", filename)
108         utils.RemoveFile(filename)
109
110     logging.info("Node successfully cleaned")
111   except Exception, err: # pylint: disable=W0703
112     logging.debug("Caught unhandled exception", exc_info=True)
113
114     (retcode, message) = cli.FormatError(err)
115     logging.error(message)
116
117     return retcode
118   else:
119     return constants.EXIT_SUCCESS