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(
84 opts.debug, opts.verbose,
85 toolname=os.path.splitext(os.path.basename(__file__))[0])
86
87 try:
88
89
90
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(map(lambda s: (s, True), pathutils.ALL_CERT_FILES))
97 clean_files.extend(map(lambda s: (s, False),
98 ssconf.SimpleStore().GetFileList()))
99
100 if not opts.yes_do_it:
101 cli.ToStderr("Cleaning a node is irreversible. If you really want to"
102 " clean this node, supply the --yes-do-it option.")
103 return constants.EXIT_FAILURE
104
105 logging.info("Stopping daemons")
106 result = utils.RunCmd([pathutils.DAEMON_UTIL, "stop-all"],
107 interactive=True)
108 if result.failed:
109 raise Exception("Could not stop daemons, command '%s' failed: %s" %
110 (result.cmd, result.fail_reason))
111
112 for (filename, backup) in clean_files:
113 if os.path.exists(filename):
114 if opts.backup and backup:
115 logging.info("Backing up %s", filename)
116 utils.CreateBackup(filename)
117
118 logging.info("Removing %s", filename)
119 utils.RemoveFile(filename)
120
121 logging.info("Node successfully cleaned")
122 except Exception, err:
123 logging.debug("Caught unhandled exception", exc_info=True)
124
125 (retcode, message) = cli.FormatError(err)
126 logging.error(message)
127
128 return retcode
129 else:
130 return constants.EXIT_SUCCESS
131