Merge branch 'stable-2.16' into stable-2.17
[ganeti-github.git] / lib / tools / node_cleanup.py
1 #
2 #
3
4 # Copyright (C) 2012 Google Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 #
11 # 1. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 #
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19 # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 """Script to configure the node daemon.
31
32 """
33
34 import os
35 import os.path
36 import optparse
37 import sys
38 import logging
39
40 from ganeti import cli
41 from ganeti import constants
42 from ganeti import pathutils
43 from ganeti import ssconf
44 from ganeti import utils
45
46
47 def ParseOptions():
48 """Parses the options passed to the program.
49
50 @return: Options and arguments
51
52 """
53 parser = optparse.OptionParser(usage="%prog [--no-backup]",
54 prog=os.path.basename(sys.argv[0]))
55 parser.add_option(cli.DEBUG_OPT)
56 parser.add_option(cli.VERBOSE_OPT)
57 parser.add_option(cli.YES_DOIT_OPT)
58 parser.add_option("--no-backup", dest="backup", default=True,
59 action="store_false",
60 help="Whether to create backup copies of deleted files")
61
62 (opts, args) = parser.parse_args()
63
64 return VerifyOptions(parser, opts, args)
65
66
67 def VerifyOptions(parser, opts, args):
68 """Verifies options and arguments for correctness.
69
70 """
71 if args:
72 parser.error("No arguments are expected")
73
74 return opts
75
76
77 def Main():
78 """Main routine.
79
80 """
81 opts = ParseOptions()
82
83 utils.SetupToolLogging(
84 opts.debug, opts.verbose,
85 toolname=os.path.splitext(os.path.basename(__file__))[0])
86
87 try:
88 # List of files to delete. Contains tuples consisting of the absolute path
89 # and a boolean denoting whether a backup copy should be created before
90 # deleting.
91 clean_files = [
92 (pathutils.CONFD_HMAC_KEY, True),
93 (pathutils.CLUSTER_CONF_FILE, True),
94 (pathutils.CLUSTER_DOMAIN_SECRET_FILE, True),
95 ]
96 clean_files.extend((f, True) for f in pathutils.ALL_CERT_FILES)
97 clean_files.extend((f, False) for f in ssconf.SimpleStore().GetFileList())
98
99 if not opts.yes_do_it:
100 cli.ToStderr("Cleaning a node is irreversible. If you really want to"
101 " clean this node, supply the --yes-do-it option.")
102 return constants.EXIT_FAILURE
103
104 logging.info("Stopping daemons")
105 result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-all"],
106 interactive=True)
107 if result.failed:
108 raise Exception("Could not stop daemons, command '%s' failed: %s" %
109 (result.cmd, result.fail_reason))
110
111 for (filename, backup) in clean_files:
112 if os.path.exists(filename):
113 if opts.backup and backup:
114 logging.info("Backing up %s", filename)
115 utils.CreateBackup(filename)
116
117 logging.info("Removing %s", filename)
118 utils.RemoveFile(filename)
119
120 logging.info("Node successfully cleaned")
121 except Exception, err: # pylint: disable=W0703
122 logging.debug("Caught unhandled exception", exc_info=True)
123
124 (retcode, message) = cli.FormatError(err)
125 logging.error(message)
126
127 return retcode
128 else:
129 return constants.EXIT_SUCCESS