4 # Copyright (C) 2006, 2007, 2010 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
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.OpDiagnoseOS(output_fields=["name", "variants"], names=[])
46 result = SubmitOpCode(op, opts=opts)
49 ToStderr("Can't get the OS list")
52 if not opts.no_headers:
53 headers = {"name": "Name"}
58 for (name, variants) in result:
59 os_names.extend([[n] for n in CalculateOSNames(name, variants)])
61 data = GenerateTable(separator=None, headers=headers, fields=["name"],
62 data=os_names, units=None)
70 def ShowOSInfo(opts, args):
71 """List detailed information about OSes in the cluster.
73 @param opts: the command line options selected by the user
75 @param args: should be an empty list
77 @return: the desired exit code
80 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants",
81 "parameters", "api_versions",
82 "blacklisted", "hidden"],
84 result = SubmitOpCode(op, opts=opts)
87 ToStderr("Can't get the OS list")
90 do_filter = bool(args)
92 for (name, valid, variants, parameters, api_versions, blk, hid) in result:
99 ToStdout(" - valid: %s", valid)
100 ToStdout(" - hidden: %s", hid)
101 ToStdout(" - blacklisted: %s", blk)
103 ToStdout(" - API versions:")
104 for version in sorted(api_versions):
105 ToStdout(" - %s", version)
106 ToStdout(" - variants:")
107 for vname in variants:
108 ToStdout(" - %s", vname)
109 ToStdout(" - parameters:")
110 for pname, pdesc in parameters:
111 ToStdout(" - %s: %s", pname, pdesc)
116 ToStdout("%s: ", name)
122 def _OsStatus(status, diagnose):
123 """Beautifier function for OS status.
125 @type status: boolean
126 @param status: is the OS valid
127 @type diagnose: string
128 @param diagnose: the error message for invalid OSes
130 @return: a formatted status
136 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", "hidden",
151 "blacklisted"], names=[])
152 result = SubmitOpCode(op, opts=opts)
155 ToStderr("Can't get the OS list")
160 for os_name, _, os_variants, node_data, hid, blk in result:
164 for node_name, node_info in node_data.iteritems():
165 nodes_hidden[node_name] = []
166 if node_info: # at least one entry in the per-node list
167 (fo_path, fo_status, fo_msg, fo_variants,
168 fo_params, fo_api) = node_info.pop(0)
169 fo_msg = "%s (path: %s)" % (_OsStatus(fo_status, fo_msg), fo_path)
171 max_os_api = max(fo_api)
172 fo_msg += " [API versions: %s]" % utils.CommaJoin(fo_api)
175 fo_msg += " [no API versions declared]"
177 if max_os_api >= constants.OS_API_V15:
179 fo_msg += " [variants: %s]" % utils.CommaJoin(fo_variants)
181 fo_msg += " [no variants]"
182 if max_os_api >= constants.OS_API_V20:
184 fo_msg += (" [parameters: %s]" %
185 utils.CommaJoin([v[0] for v in fo_params]))
187 fo_msg += " [no parameters]"
189 nodes_valid[node_name] = fo_msg
191 nodes_bad[node_name] = fo_msg
192 for hpath, hstatus, hmsg, _, _, _ in node_info:
193 nodes_hidden[node_name].append(" [hidden] path: %s, status: %s" %
194 (hpath, _OsStatus(hstatus, hmsg)))
196 nodes_bad[node_name] = "OS not found"
198 if nodes_valid and not nodes_bad:
200 elif not nodes_valid and nodes_bad:
204 status = "partial valid"
207 def _OutputPerNodeOSStatus(msg_map):
208 map_k = utils.NiceSort(msg_map.keys())
209 for node_name in map_k:
210 ToStdout(" Node: %s, status: %s", node_name, msg_map[node_name])
211 for msg in nodes_hidden[node_name]:
214 st_msg = "OS: %s [global status: %s]" % (os_name, status)
216 st_msg += " [hidden]"
218 st_msg += " [blacklisted]"
221 ToStdout(" Variants: [%s]" % utils.CommaJoin(os_variants))
222 _OutputPerNodeOSStatus(nodes_valid)
223 _OutputPerNodeOSStatus(nodes_bad)
229 def ModifyOS(opts, args):
230 """Modify OS parameters for one OS.
232 @param opts: the command line options selected by the user
234 @param args: should be a list with one entry
236 @return: the desired exit code
242 os_hvp = {os: dict(opts.hvparams)}
247 osp = {os: opts.osparams}
251 if opts.hidden is not None:
253 ohid = [(constants.DDM_ADD, os)]
255 ohid = [(constants.DDM_REMOVE, os)]
259 if opts.blacklisted is not None:
261 oblk = [(constants.DDM_ADD, os)]
263 oblk = [(constants.DDM_REMOVE, os)]
267 if not (os_hvp or osp or ohid or oblk):
268 ToStderr("At least one of OS parameters or hypervisor parameters"
272 op = opcodes.OpClusterSetParams(os_hvp=os_hvp,
276 SubmitOpCode(op, opts=opts)
283 ListOS, ARGS_NONE, [NOHDR_OPT, PRIORITY_OPT],
284 "", "Lists all valid operating systems on the cluster"),
286 DiagnoseOS, ARGS_NONE, [PRIORITY_OPT],
287 "", "Diagnose all operating systems"),
289 ShowOSInfo, [ArgOs()], [PRIORITY_OPT],
290 "", "Show detailed information about "
291 "operating systems"),
293 ModifyOS, ARGS_ONE_OS,
294 [HVLIST_OPT, OSPARAMS_OPT, DRY_RUN_OPT, PRIORITY_OPT,
295 HID_OS_OPT, BLK_OS_OPT],
296 "", "Modify the OS parameters"),
301 return GenericMain(commands)