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( 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(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: # pylint: disable=W0703 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