4 # Copyright (C) 2006, 2007, 2010, 2013 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=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
29 from ganeti.cli import *
30 from ganeti import constants
31 from ganeti import opcodes
32 from ganeti import utils
35 def ListOS(opts, args):
36 """List the valid OSes in the cluster.
38 @param opts: the command line options selected by the user
40 @param args: should be an empty list
42 @return: the desired exit code
45 op = opcodes.OpOsDiagnose(output_fields=["name", "variants"], names=[])
46 result = SubmitOpCode(op, opts=opts)
48 if not opts.no_headers:
49 headers = {"name": "Name"}
54 for (name, variants) in result:
55 os_names.extend([[n] for n in CalculateOSNames(name, variants)])
57 data = GenerateTable(separator=None, headers=headers, fields=["name"],
58 data=os_names, units=None)
66 def ShowOSInfo(opts, args):
67 """List detailed information about OSes in the cluster.
69 @param opts: the command line options selected by the user
71 @param args: should be an empty list
73 @return: the desired exit code
76 op = opcodes.OpOsDiagnose(output_fields=["name", "valid", "variants",
77 "parameters", "api_versions",
78 "blacklisted", "hidden"],
80 result = SubmitOpCode(op, opts=opts)
83 ToStderr("Can't get the OS list")
86 do_filter = bool(args)
88 for (name, valid, variants, parameters, api_versions, blk, hid) in result:
95 ToStdout(" - valid: %s", valid)
96 ToStdout(" - hidden: %s", hid)
97 ToStdout(" - blacklisted: %s", blk)
99 ToStdout(" - API versions:")
100 for version in sorted(api_versions):
101 ToStdout(" - %s", version)
102 ToStdout(" - variants:")
103 for vname in variants:
104 ToStdout(" - %s", vname)
105 ToStdout(" - parameters:")
106 for pname, pdesc in parameters:
107 ToStdout(" - %s: %s", pname, pdesc)
112 ToStdout("%s: ", name)
118 def _OsStatus(status, diagnose):
119 """Beautifier function for OS status.
121 @type status: boolean
122 @param status: is the OS valid
123 @type diagnose: string
124 @param diagnose: the error message for invalid OSes
126 @return: a formatted status
132 return "invalid - %s" % diagnose
135 def DiagnoseOS(opts, args):
136 """Analyse all OSes on this cluster.
138 @param opts: the command line options selected by the user
140 @param args: should be an empty list
142 @return: the desired exit code
145 op = opcodes.OpOsDiagnose(output_fields=["name", "valid", "variants",
146 "node_status", "hidden",
147 "blacklisted"], names=[])
148 result = SubmitOpCode(op, opts=opts)
151 ToStderr("Can't get the OS list")
156 for os_name, _, os_variants, node_data, hid, blk in result:
160 for node_name, node_info in node_data.iteritems():
161 nodes_hidden[node_name] = []
162 if node_info: # at least one entry in the per-node list
163 (fo_path, fo_status, fo_msg, fo_variants,
164 fo_params, fo_api) = node_info.pop(0)
165 fo_msg = "%s (path: %s)" % (_OsStatus(fo_status, fo_msg), fo_path)
167 max_os_api = max(fo_api)
168 fo_msg += " [API versions: %s]" % utils.CommaJoin(fo_api)
171 fo_msg += " [no API versions declared]"
173 if max_os_api >= constants.OS_API_V15:
175 fo_msg += " [variants: %s]" % utils.CommaJoin(fo_variants)
177 fo_msg += " [no variants]"
178 if max_os_api >= constants.OS_API_V20:
180 fo_msg += (" [parameters: %s]" %
181 utils.CommaJoin([v[0] for v in fo_params]))
183 fo_msg += " [no parameters]"
185 nodes_valid[node_name] = fo_msg
187 nodes_bad[node_name] = fo_msg
188 for hpath, hstatus, hmsg, _, _, _ in node_info:
189 nodes_hidden[node_name].append(" [hidden] path: %s, status: %s" %
190 (hpath, _OsStatus(hstatus, hmsg)))
192 nodes_bad[node_name] = "OS not found"
194 # TODO: Shouldn't the global status be calculated by the LU?
195 if nodes_valid and not nodes_bad:
197 elif not nodes_valid and nodes_bad:
201 status = "partial valid"
204 def _OutputPerNodeOSStatus(msg_map):
205 map_k = utils.NiceSort(msg_map.keys())
206 for node_name in map_k:
207 ToStdout(" Node: %s, status: %s", node_name, msg_map[node_name])
208 for msg in nodes_hidden[node_name]:
211 st_msg = "OS: %s [global status: %s]" % (os_name, status)
213 st_msg += " [hidden]"
215 st_msg += " [blacklisted]"
218 ToStdout(" Variants: [%s]" % utils.CommaJoin(os_variants))
219 _OutputPerNodeOSStatus(nodes_valid)
220 _OutputPerNodeOSStatus(nodes_bad)
226 def ModifyOS(opts, args):
227 """Modify OS parameters for one OS.
229 @param opts: the command line options selected by the user
231 @param args: should be a list with one entry
233 @return: the desired exit code
239 os_hvp = {os: dict(opts.hvparams)}
244 osp = {os: opts.osparams}
248 if opts.hidden is not None:
250 ohid = [(constants.DDM_ADD, os)]
252 ohid = [(constants.DDM_REMOVE, os)]
256 if opts.blacklisted is not None:
258 oblk = [(constants.DDM_ADD, os)]
260 oblk = [(constants.DDM_REMOVE, os)]
264 if not (os_hvp or osp or ohid or oblk):
265 ToStderr("At least one of OS parameters or hypervisor parameters"
269 op = opcodes.OpClusterSetParams(os_hvp=os_hvp,
273 SubmitOrSend(op, opts)
280 ListOS, ARGS_NONE, [NOHDR_OPT, PRIORITY_OPT],
281 "", "Lists all valid operating systems on the cluster"),
283 DiagnoseOS, ARGS_NONE, [PRIORITY_OPT],
284 "", "Diagnose all operating systems"),
286 ShowOSInfo, [ArgOs()], [PRIORITY_OPT],
287 "", "Show detailed information about "
288 "operating systems"),
290 ModifyOS, ARGS_ONE_OS,
291 [HVLIST_OPT, OSPARAMS_OPT, DRY_RUN_OPT, PRIORITY_OPT,
292 HID_OS_OPT, BLK_OS_OPT, SUBMIT_OPT],
293 "", "Modify the OS parameters"),
296 #: dictionary with aliases for commands
303 return GenericMain(commands, aliases=aliases)