Another fix for 'too-many-branches' for 'cfgupgrade'
authorHelga Velroyen <helgav@google.com>
Thu, 22 May 2014 12:44:18 +0000 (14:44 +0200)
committerHelga Velroyen <helgav@google.com>
Thu, 22 May 2014 15:16:31 +0000 (17:16 +0200)
This 'main' function was too big anyway.

Signed-off-by: Helga Velroyen <helgav@google.com>
Reviewed-by: Jose Lopes <jabolopes@google.com>

tools/cfgupgrade

index 5eeb1bb..c256130 100755 (executable)
@@ -543,13 +543,7 @@ def DowngradeAll(config_data):
   DowngradeInstances(config_data)
 
 
-def main():
-  """Main program.
-
-  """
-  global options, args # pylint: disable=W0603
-
-  # Option parsing
+def _ParseOptions():
   parser = optparse.OptionParser(usage="%prog [--debug|--verbose] [--force]")
   parser.add_option("--dry-run", dest="dry_run",
                     action="store_true",
@@ -574,8 +568,10 @@ def main():
   parser.add_option("--downgrade",
                     help="Downgrade to the previous stable version",
                     action="store_true", dest="downgrade", default=False)
-  (options, args) = parser.parse_args()
+  return parser.parse_args()
+
 
+def _ComposePaths():
   # We need to keep filenames locally because they might be renamed between
   # versions.
   options.data_dir = os.path.abspath(options.data_dir)
@@ -594,19 +590,8 @@ def main():
   options.WATCHER_STATEFILE = options.data_dir + "/watcher.data"
   options.FILE_STORAGE_PATHS_FILE = options.conf_dir + "/file-storage-paths"
 
-  SetupLogging()
-
-  # Option checking
-  if args:
-    raise Error("No arguments expected")
-  if options.downgrade and not options.no_verify:
-    options.no_verify = True
-
-  # Check master name
-  if not (CheckHostname(options.SSCONF_MASTER_NODE) or options.ignore_hostname):
-    logging.error("Aborting due to hostname mismatch")
-    sys.exit(constants.EXIT_FAILURE)
 
+def _AskUser():
   if not options.force:
     if options.downgrade:
       usertext = ("The configuration is going to be DOWNGRADED to version %s.%s"
@@ -624,6 +609,76 @@ def main():
     if not cli.AskUser(usertext):
       sys.exit(constants.EXIT_FAILURE)
 
+
+def _Downgrade(config_major, config_minor, config_version, config_data,
+               config_revision):
+  if not ((config_major == TARGET_MAJOR and config_minor == TARGET_MINOR) or
+          (config_major == DOWNGRADE_MAJOR and
+           config_minor == DOWNGRADE_MINOR)):
+    raise Error("Downgrade supported only from the latest version (%s.%s),"
+                " found %s (%s.%s.%s) instead" %
+                (TARGET_MAJOR, TARGET_MINOR, config_version, config_major,
+                 config_minor, config_revision))
+  DowngradeAll(config_data)
+
+
+def _TestLoadingConfigFile():
+  # test loading the config file
+  all_ok = True
+  if not (options.dry_run or options.no_verify):
+    logging.info("Testing the new config file...")
+    cfg = config.ConfigWriter(cfg_file=options.CONFIG_DATA_PATH,
+                              accept_foreign=options.ignore_hostname,
+                              offline=True)
+    # if we reached this, it's all fine
+    vrfy = cfg.VerifyConfig()
+    if vrfy:
+      logging.error("Errors after conversion:")
+      for item in vrfy:
+        logging.error(" - %s", item)
+      all_ok = False
+    else:
+      logging.info("File loaded successfully after upgrading")
+    del cfg
+
+  if options.downgrade:
+    action = "downgraded"
+    out_ver = "%s.%s" % (DOWNGRADE_MAJOR, DOWNGRADE_MINOR)
+  else:
+    action = "upgraded"
+    out_ver = constants.RELEASE_VERSION
+  if all_ok:
+    cli.ToStderr("Configuration successfully %s to version %s.",
+                 action, out_ver)
+  else:
+    cli.ToStderr("Configuration %s to version %s, but there are errors."
+                 "\nPlease review the file.", action, out_ver)
+
+
+def main():
+  """Main program.
+
+  """
+  global options, args # pylint: disable=W0603
+
+  (options, args) = _ParseOptions()
+  _ComposePaths()
+
+  SetupLogging()
+
+  # Option checking
+  if args:
+    raise Error("No arguments expected")
+  if options.downgrade and not options.no_verify:
+    options.no_verify = True
+
+  # Check master name
+  if not (CheckHostname(options.SSCONF_MASTER_NODE) or options.ignore_hostname):
+    logging.error("Aborting due to hostname mismatch")
+    sys.exit(constants.EXIT_FAILURE)
+
+  _AskUser()
+
   # Check whether it's a Ganeti configuration directory
   if not (os.path.isfile(options.CONFIG_DATA_PATH) and
           os.path.isfile(options.SERVER_PEM_PATH) and
@@ -653,14 +708,8 @@ def main():
 
   # Downgrade to the previous stable version
   if options.downgrade:
-    if not ((config_major == TARGET_MAJOR and config_minor == TARGET_MINOR) or
-            (config_major == DOWNGRADE_MAJOR and
-             config_minor == DOWNGRADE_MINOR)):
-      raise Error("Downgrade supported only from the latest version (%s.%s),"
-                  " found %s (%s.%s.%s) instead" %
-                  (TARGET_MAJOR, TARGET_MINOR, config_version, config_major,
-                   config_minor, config_revision))
-    DowngradeAll(config_data)
+    _Downgrade(config_major, config_minor, config_version, config_data,
+               config_revision)
 
   # Upgrade from 2.{0..10} to 2.12
   elif config_major == 2 and config_minor in range(0, 12):
@@ -698,36 +747,7 @@ def main():
                      " inconsistent state and needs manual intervention.")
     raise
 
-  # test loading the config file
-  all_ok = True
-  if not (options.dry_run or options.no_verify):
-    logging.info("Testing the new config file...")
-    cfg = config.ConfigWriter(cfg_file=options.CONFIG_DATA_PATH,
-                              accept_foreign=options.ignore_hostname,
-                              offline=True)
-    # if we reached this, it's all fine
-    vrfy = cfg.VerifyConfig()
-    if vrfy:
-      logging.error("Errors after conversion:")
-      for item in vrfy:
-        logging.error(" - %s", item)
-      all_ok = False
-    else:
-      logging.info("File loaded successfully after upgrading")
-    del cfg
-
-  if options.downgrade:
-    action = "downgraded"
-    out_ver = "%s.%s" % (DOWNGRADE_MAJOR, DOWNGRADE_MINOR)
-  else:
-    action = "upgraded"
-    out_ver = constants.RELEASE_VERSION
-  if all_ok:
-    cli.ToStderr("Configuration successfully %s to version %s.",
-                 action, out_ver)
-  else:
-    cli.ToStderr("Configuration %s to version %s, but there are errors."
-                 "\nPlease review the file.", action, out_ver)
+  _TestLoadingConfigFile()
 
 
 if __name__ == "__main__":