Add support for modifying cluster OS parameters
[ganeti-local] / scripts / gnt-os
index f3667c4..e7177c3 100755 (executable)
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 # 02110-1301, USA.
 
+"""OS scripts related commands"""
 
-# pylint: disable-msg=W0401,W0614
+# pylint: disable-msg=W0401,W0613,W0614,C0103
 # W0401: Wildcard import ganeti.cli
+# W0613: Unused argument, since all functions follow the same API
 # W0614: Unused import %s from wildcard import (since we need cli)
+# C0103: Invalid name gnt-os
 
 import sys
 
 from ganeti.cli import *
 from ganeti import opcodes
 from ganeti import utils
-from ganeti import constants
 
 
 def ListOS(opts, args):
@@ -43,7 +45,7 @@ def ListOS(opts, args):
   """
   op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants"],
                             names=[])
-  result = SubmitOpCode(op)
+  result = SubmitOpCode(op, opts=opts)
 
   if not result:
     ToStderr("Can't get the OS list")
@@ -96,7 +98,7 @@ def DiagnoseOS(opts, args):
   """
   op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants",
                                            "node_status"], names=[])
-  result = SubmitOpCode(op)
+  result = SubmitOpCode(op, opts=opts)
 
   if not result:
     ToStderr("Can't get the OS list")
@@ -104,7 +106,7 @@ def DiagnoseOS(opts, args):
 
   has_bad = False
 
-  for os_name, os_valid, os_variants, node_data in result:
+  for os_name, _, os_variants, node_data in result:
     nodes_valid = {}
     nodes_bad = {}
     nodes_hidden = {}
@@ -112,7 +114,7 @@ def DiagnoseOS(opts, args):
       nodes_hidden[node_name] = []
       if node_info: # at least one entry in the per-node list
         (first_os_path, first_os_status, first_os_msg,
-         first_os_variants) = node_info.pop(0)
+         first_os_variants, _) = node_info.pop(0)
         if not first_os_variants:
           first_os_variants = []
         first_os_msg = ("%s (path: %s) [variants: %s]" %
@@ -154,11 +156,55 @@ def DiagnoseOS(opts, args):
   return int(has_bad)
 
 
+def ModifyOS(opts, args):
+  """Modify OS parameters for one OS.
+
+  @param opts: the command line options selected by the user
+  @type args: list
+  @param args: should be a list with one entry
+  @rtype: int
+  @return: the desired exit code
+
+  """
+  os = args[0]
+
+  if opts.hvparams:
+    os_hvp = {os: dict(opts.hvparams)}
+  else:
+    os_hvp = None
+
+  if opts.osparams:
+    osp = {os: opts.osparams}
+  else:
+    osp = None
+
+  if not (os_hvp or osp):
+    ToStderr("At least one of OS parameters or hypervisor parameters"
+             " must be passed")
+    return 1
+
+  op = opcodes.OpSetClusterParams(vg_name=None,
+                                  enabled_hypervisors=None,
+                                  hvparams=None,
+                                  beparams=None,
+                                  nicparams=None,
+                                  candidate_pool_size=None,
+                                  os_hvp=os_hvp,
+                                  osparams=osp)
+  SubmitOpCode(op)
+
+  return 0
+
+
 commands = {
   'list': (
-    ListOS, ARGS_NONE, [NOHDR_OPT], "", "Lists all valid OSes on the master"),
+    ListOS, ARGS_NONE, [NOHDR_OPT], "", "Lists all valid operating systems"
+    " on the cluster"),
   'diagnose': (
-    DiagnoseOS, ARGS_NONE, [], "", "Diagnose all OSes"),
+    DiagnoseOS, ARGS_NONE, [], "", "Diagnose all operating systems"),
+  'modify': (
+    ModifyOS, ARGS_ONE_OS, [HVLIST_OPT, OSPARAMS_OPT], "",
+    "Modify the OS parameters"),
   }
 
 if __name__ == '__main__':