1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
69 """Main routine.
70
71 """
72 opts = ParseOptions()
73
74 utils.SetupToolLogging(opts.debug, opts.verbose)
75
76 try:
77
78
79
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:
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
120