Package ganeti :: Package tools :: Module node_cleanup
[hide private]
[frames] | no frames]

Source Code for Module ganeti.tools.node_cleanup

  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(opts.debug, opts.verbose) 84 85 try: 86 # List of files to delete. Contains tuples consisting of the absolute path 87 # and a boolean denoting whether a backup copy should be created before 88 # deleting. 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: # pylint: disable=W0703 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