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

Source Code for Module ganeti.tools.node_daemon_setup

  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 errors 
 43  from ganeti import pathutils 
 44  from ganeti import utils 
 45  from ganeti import runtime 
 46  from ganeti import ht 
 47  from ganeti import ssconf 
 48  from ganeti.tools import common 
 49   
 50   
 51  _DATA_CHECK = ht.TStrictDict(False, True, { 
 52    constants.NDS_CLUSTER_NAME: ht.TNonEmptyString, 
 53    constants.NDS_NODE_DAEMON_CERTIFICATE: ht.TNonEmptyString, 
 54    constants.NDS_SSCONF: ht.TDictOf(ht.TNonEmptyString, ht.TString), 
 55    constants.NDS_START_NODE_DAEMON: ht.TBool, 
 56    constants.NDS_NODE_NAME: ht.TString, 
 57    }) 
 58   
 59   
60 -class SetupError(errors.GenericError):
61 """Local class for reporting errors. 62 63 """
64 65
66 -def ParseOptions():
67 """Parses the options passed to the program. 68 69 @return: Options and arguments 70 71 """ 72 parser = optparse.OptionParser(usage="%prog [--dry-run]", 73 prog=os.path.basename(sys.argv[0])) 74 parser.add_option(cli.DEBUG_OPT) 75 parser.add_option(cli.VERBOSE_OPT) 76 parser.add_option(cli.DRY_RUN_OPT) 77 78 (opts, args) = parser.parse_args() 79 80 return VerifyOptions(parser, opts, args)
81 82
83 -def VerifyOptions(parser, opts, args):
84 """Verifies options and arguments for correctness. 85 86 """ 87 if args: 88 parser.error("No arguments are expected") 89 90 return opts
91 92
93 -def VerifySsconf(data, cluster_name, _verify_fn=ssconf.VerifyKeys):
94 """Verifies ssconf names. 95 96 @type data: dict 97 98 """ 99 items = data.get(constants.NDS_SSCONF) 100 101 if not items: 102 raise SetupError("Ssconf values must be specified") 103 104 # TODO: Should all keys be required? Right now any subset of valid keys is 105 # accepted. 106 _verify_fn(items.keys()) 107 108 if items.get(constants.SS_CLUSTER_NAME) != cluster_name: 109 raise SetupError("Cluster name in ssconf does not match") 110 111 return items
112 113
114 -def Main():
115 """Main routine. 116 117 """ 118 opts = ParseOptions() 119 120 utils.SetupToolLogging(opts.debug, opts.verbose) 121 122 try: 123 getent = runtime.GetEnts() 124 125 data = common.LoadData(sys.stdin.read(), SetupError) 126 127 cluster_name = common.VerifyClusterName(data, SetupError, 128 constants.NDS_CLUSTER_NAME) 129 cert_pem = common.VerifyCertificateStrong(data, SetupError) 130 ssdata = VerifySsconf(data, cluster_name) 131 132 logging.info("Writing ssconf files ...") 133 ssconf.WriteSsconfFiles(ssdata, dry_run=opts.dry_run) 134 135 logging.info("Writing node daemon certificate ...") 136 utils.WriteFile(pathutils.NODED_CERT_FILE, data=cert_pem, 137 mode=pathutils.NODED_CERT_MODE, 138 uid=getent.masterd_uid, gid=getent.masterd_gid, 139 dry_run=opts.dry_run) 140 common.GenerateClientCertificate(data, SetupError) 141 142 if (data.get(constants.NDS_START_NODE_DAEMON) and # pylint: disable=E1103 143 not opts.dry_run): 144 logging.info("Restarting node daemon ...") 145 146 stop_cmd = "%s stop-all" % pathutils.DAEMON_UTIL 147 noded_cmd = "%s start %s" % (pathutils.DAEMON_UTIL, constants.NODED) 148 mond_cmd = "" 149 if constants.ENABLE_MOND: 150 mond_cmd = "%s start %s" % (pathutils.DAEMON_UTIL, constants.MOND) 151 152 cmd = "; ".join([stop_cmd, noded_cmd, mond_cmd]) 153 154 result = utils.RunCmd(cmd, interactive=True) 155 if result.failed: 156 raise SetupError("Could not start the node daemons, command '%s'" 157 " failed: %s" % (result.cmd, result.fail_reason)) 158 159 logging.info("Node daemon successfully configured") 160 except Exception, err: # pylint: disable=W0703 161 logging.debug("Caught unhandled exception", exc_info=True) 162 163 (retcode, message) = cli.FormatError(err) 164 logging.error(message) 165 166 return retcode 167 else: 168 return constants.EXIT_SUCCESS
169