Fix gnt-cluster init to set cluster defaults.
[ganeti-local] / scripts / gnt-cluster
index 435e1cb..1c3fca8 100755 (executable)
@@ -50,6 +50,54 @@ def InitCluster(opts, args):
   if opts.lvm_storage and not opts.vg_name:
     vg_name = constants.DEFAULT_VG
 
+  hvlist = opts.enabled_hypervisors
+  if hvlist is not None:
+    hvlist = hvlist.split(",")
+  else:
+    hvlist = constants.DEFAULT_ENABLED_HYPERVISOR
+
+  hvparams = opts.hvparams
+  if hvparams:
+    # a list of (name, dict) we can pass directly to dict()
+    hvparams = dict(opts.hvparams)
+  else:
+    # otherwise init as empty dict
+    hvparams = {}
+
+  beparams = opts.beparams
+  # check for invalid parameters
+  for parameter in beparams:
+    if parameter not in constants.BES_PARAMETERS:
+      print "Invalid backend parameter: %s" % parameter
+      return 1
+
+  # prepare beparams dict
+  for parameter in constants.BES_PARAMETERS:
+    if parameter not in beparams:
+      beparams[parameter] = constants.BEC_DEFAULTS[parameter]
+
+  # type wrangling
+  try:
+    beparams[constants.BE_VCPUS] = int(beparams[constants.BE_VCPUS])
+  except ValueError:
+    print "%s must be an integer" % constants.BE_VCPUS
+    return 1
+
+  beparams[constants.BE_MEMORY] = utils.ParseUnit(beparams[constants.BE_MEMORY])
+
+  # prepare hvparams dict
+  for hv in constants.HYPER_TYPES:
+    if hv not in hvparams:
+      hvparams[hv] = {}
+    for parameter in constants.HVC_DEFAULTS[hv]:
+      if parameter not in hvparams[hv]:
+        hvparams[hv][parameter] = constants.HVC_DEFAULTS[hv][parameter]
+
+  for hv in hvlist:
+    if hv not in constants.HYPER_TYPES:
+      print "invalid hypervisor: %s" % hv
+      return 1
+
   bootstrap.InitCluster(cluster_name=args[0],
                         secondary_ip=opts.secondary_ip,
                         hypervisor_type=opts.hypervisor_type,
@@ -57,7 +105,10 @@ def InitCluster(opts, args):
                         mac_prefix=opts.mac_prefix,
                         def_bridge=opts.def_bridge,
                         master_netdev=opts.master_netdev,
-                        file_storage_dir=opts.file_storage_dir)
+                        file_storage_dir=opts.file_storage_dir,
+                        enabled_hypervisors=hvlist,
+                        hvparams=hvparams,
+                        beparams=beparams)
   return 0
 
 
@@ -145,7 +196,20 @@ def ShowClusterConfig(opts, args):
   print ("Architecture (this node): %s (%s)" %
          (result["architecture"][0], result["architecture"][1]))
 
-  print ("Cluster hypervisor: %s" % result["hypervisor_type"])
+  print ("Default hypervisor: %s" % result["hypervisor_type"])
+  print ("Enabled hypervisors: %s" % ", ".join(result["enabled_hypervisors"]))
+
+  print "Hypervisor parameters:"
+  for hv_name, hv_dict in result["hvparams"].items():
+    print "  - %s:" % hv_name
+    for item, val in hv_dict.iteritems():
+      print "      %s: %s" % (item, val)
+
+  print "Cluster parameters:"
+  for gr_name, gr_dict in result["beparams"].items():
+    print "  - %s:" % gr_name
+    for item, val in gr_dict.iteritems():
+      print "      %s: %s" % (item, val)
 
   return 0
 
@@ -329,7 +393,9 @@ def SetClusterParams(opts, args):
     opts - class with options as members
 
   """
-  if not (not opts.lvm_storage or opts.vg_name):
+  if not (not opts.lvm_storage or opts.vg_name or
+          opts.enabled_hypervisors or opts.hvparams or
+          opts.beparams):
     print "Please give at least one of the parameters."
     return 1
 
@@ -338,7 +404,21 @@ def SetClusterParams(opts, args):
     print ("Options --no-lvm-storage and --vg-name conflict.")
     return 1
 
-  op = opcodes.OpSetClusterParams(vg_name=opts.vg_name)
+  hvlist = opts.enabled_hypervisors
+  if hvlist is not None:
+    hvlist = hvlist.split(",")
+
+  hvparams = opts.hvparams
+  if hvparams:
+    # a list of (name, dict) we can pass directly to dict()
+    hvparams = dict(opts.hvparams)
+
+  beparams = opts.beparams
+
+  op = opcodes.OpSetClusterParams(vg_name=opts.vg_name,
+                                  enabled_hypervisors=hvlist,
+                                  hvparams=hvparams,
+                                  beparams=beparams)
   SubmitOpCode(op)
   return 0
 
@@ -416,6 +496,19 @@ commands = {
                         help="No support for lvm based instances"
                              " (cluster-wide)",
                         action="store_false", default=True,),
+            make_option("--enabled-hypervisors", dest="enabled_hypervisors",
+                        help="Comma-separated list of hypervisors",
+                        type="string", default=None),
+            ikv_option("-H", "--hypervisor-parameters", dest="hvparams",
+                       help="Hypervisor and hypervisor options, in the"
+                         " format"
+                       " hypervisor:option=value,option=value,...",
+                       default=[],
+                       action="append",
+                       type="identkeyval"),
+            keyval_option("-B", "--backend-parameters", dest="beparams",
+                          type="keyval", default={},
+                          help="Backend parameters"),
             ],
            "[opts...] <cluster_name>",
            "Initialises a new cluster configuration"),
@@ -474,6 +567,19 @@ commands = {
                           help="Disable support for lvm based instances"
                                " (cluster-wide)",
                           action="store_false", default=True,),
+              make_option("--enabled-hypervisors", dest="enabled_hypervisors",
+                          help="Comma-separated list of hypervisors",
+                          type="string", default=None),
+              ikv_option("-H", "--hypervisor-parameters", dest="hvparams",
+                         help="Hypervisor and hypervisor options, in the"
+                         " format"
+                         " hypervisor:option=value,option=value,...",
+                         default=[],
+                         action="append",
+                         type="identkeyval"),
+              keyval_option("-B", "--backend-parameters", dest="beparams",
+                            type="keyval", default={},
+                            help="Backend parameters"),
               ],
              "[opts...]",
              "Alters the parameters of the cluster"),