1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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
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
78 """Main routine.
79
80 """
81 opts = ParseOptions()
82
83 utils.SetupToolLogging(opts.debug, opts.verbose)
84
85 try:
86
87
88
89 clean_files = [
90 (pathutils.CONFD_HMAC_KEY, True),
91 (pathutils.CLUSTER_CONF_FILE, True),
92 (pathutils.CLUSTER_DOMAIN_SECRET_FILE, True),
93 ]
94 clean_files.extend(map(lambda s: (s, True), pathutils.ALL_CERT_FILES))
95 clean_files.extend(map(lambda s: (s, False),
96 ssconf.SimpleStore().GetFileList()))
97
98 if not opts.yes_do_it:
99 cli.ToStderr("Cleaning a node is irreversible. If you really want to"
100 " clean this node, supply the --yes-do-it option.")
101 return constants.EXIT_FAILURE
102
103 logging.info("Stopping daemons")
104 result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-all"],
105 interactive=True)
106 if result.failed:
107 raise Exception("Could not stop daemons, command '%s' failed: %s" %
108 (result.cmd, result.fail_reason))
109
110 for (filename, backup) in clean_files:
111 if os.path.exists(filename):
112 if opts.backup and backup:
113 logging.info("Backing up %s", filename)
114 utils.CreateBackup(filename)
115
116 logging.info("Removing %s", filename)
117 utils.RemoveFile(filename)
118
119 logging.info("Node successfully cleaned")
120 except Exception, err:
121 logging.debug("Caught unhandled exception", exc_info=True)
122
123 (retcode, message) = cli.FormatError(err)
124 logging.error(message)
125
126 return retcode
127 else:
128 return constants.EXIT_SUCCESS
129