4 # Copyright (C) 2006, 2007 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 """OS scripts related commands"""
23 # pylint: disable-msg=W0401,W0613,W0614,C0103
24 # W0401: Wildcard import ganeti.cli
25 # W0613: Unused argument, since all functions follow the same API
26 # W0614: Unused import %s from wildcard import (since we need cli)
27 # C0103: Invalid name gnt-os
31 from ganeti.cli import *
32 from ganeti import constants
33 from ganeti import opcodes
34 from ganeti import utils
37 def ListOS(opts, args):
38 """List the valid OSes in the cluster.
40 @param opts: the command line options selected by the user
42 @param args: should be an empty list
44 @return: the desired exit code
47 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants"],
49 result = SubmitOpCode(op, opts=opts)
52 ToStderr("Can't get the OS list")
55 if not opts.no_headers:
56 headers = {"name": "Name"}
61 for (name, valid, variants) in result:
63 os_names.extend([[n] for n in CalculateOSNames(name, variants)])
65 data = GenerateTable(separator=None, headers=headers, fields=["name"],
66 data=os_names, units=None)
74 def ShowOSInfo(opts, args):
75 """List detailed information about OSes in the cluster.
77 @param opts: the command line options selected by the user
79 @param args: should be an empty list
81 @return: the desired exit code
84 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants",
85 "parameters", "api_versions"],
87 result = SubmitOpCode(op, opts=opts)
90 ToStderr("Can't get the OS list")
93 do_filter = bool(args)
95 for (name, valid, variants, parameters, api_versions) in result:
101 ToStdout("%s:", name)
102 ToStdout(" - valid: %s", valid)
104 ToStdout(" - API versions:")
105 for version in sorted(api_versions):
106 ToStdout(" - %s", version)
107 ToStdout(" - variants:")
108 for vname in variants:
109 ToStdout(" - %s", vname)
110 ToStdout(" - parameters:")
111 for pname, pdesc in parameters:
112 ToStdout(" - %s: %s", pname, pdesc)
117 ToStdout("%s: ", name)
123 def _OsStatus(status, diagnose):
124 """Beautifier function for OS status.
126 @type status: boolean
127 @param status: is the OS valid
128 @type diagnose: string
129 @param diagnose: the error message for invalid OSes
131 @return: a formatted status
137 return "invalid - %s" % diagnose
139 def DiagnoseOS(opts, args):
140 """Analyse all OSes on this cluster.
142 @param opts: the command line options selected by the user
144 @param args: should be an empty list
146 @return: the desired exit code
149 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants",
150 "node_status"], names=[])
151 result = SubmitOpCode(op, opts=opts)
154 ToStderr("Can't get the OS list")
159 for os_name, _, os_variants, node_data in result:
163 for node_name, node_info in node_data.iteritems():
164 nodes_hidden[node_name] = []
165 if node_info: # at least one entry in the per-node list
166 (fo_path, fo_status, fo_msg, fo_variants,
167 fo_params, fo_api) = node_info.pop(0)
168 fo_msg = "%s (path: %s)" % (_OsStatus(fo_status, fo_msg), fo_path)
170 max_os_api = max(fo_api)
171 fo_msg += " [API versions: %s]" % utils.CommaJoin(fo_api)
174 fo_msg += " [no API versions declared]"
175 if max_os_api >= constants.OS_API_V15:
177 fo_msg += " [variants: %s]" % utils.CommaJoin(fo_variants)
179 fo_msg += " [no variants]"
180 if max_os_api >= constants.OS_API_V20:
182 fo_msg += (" [parameters: %s]" %
183 utils.CommaJoin([v[0] for v in fo_params]))
185 fo_msg += " [no parameters]"
187 nodes_valid[node_name] = fo_msg
189 nodes_bad[node_name] = fo_msg
190 for hpath, hstatus, hmsg, _, _, _ in node_info:
191 nodes_hidden[node_name].append(" [hidden] path: %s, status: %s" %
192 (hpath, _OsStatus(hstatus, hmsg)))
194 nodes_bad[node_name] = "OS not found"
196 if nodes_valid and not nodes_bad:
198 elif not nodes_valid and nodes_bad:
202 status = "partial valid"
205 def _OutputPerNodeOSStatus(msg_map):
206 map_k = utils.NiceSort(msg_map.keys())
207 for node_name in map_k:
208 ToStdout(" Node: %s, status: %s", node_name, msg_map[node_name])
209 for msg in nodes_hidden[node_name]:
212 ToStdout("OS: %s [global status: %s]", os_name, status)
214 ToStdout(" Variants: [%s]" % utils.CommaJoin(os_variants))
215 _OutputPerNodeOSStatus(nodes_valid)
216 _OutputPerNodeOSStatus(nodes_bad)
222 def ModifyOS(opts, args):
223 """Modify OS parameters for one OS.
225 @param opts: the command line options selected by the user
227 @param args: should be a list with one entry
229 @return: the desired exit code
235 os_hvp = {os: dict(opts.hvparams)}
240 osp = {os: opts.osparams}
244 if not (os_hvp or osp):
245 ToStderr("At least one of OS parameters or hypervisor parameters"
249 op = opcodes.OpSetClusterParams(vg_name=None,
250 enabled_hypervisors=None,
254 candidate_pool_size=None,
264 ListOS, ARGS_NONE, [NOHDR_OPT], "", "Lists all valid operating systems"
267 DiagnoseOS, ARGS_NONE, [], "", "Diagnose all operating systems"),
269 ShowOSInfo, [ArgOs()], [], "", "Show detailed information about "
270 "operating systems"),
272 ModifyOS, ARGS_ONE_OS, [HVLIST_OPT, OSPARAMS_OPT], "",
273 "Modify the OS parameters"),
276 if __name__ == '__main__':
277 sys.exit(GenericMain(commands))