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
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", "variants"], names=[])
48 result = SubmitOpCode(op, opts=opts)
51 ToStderr("Can't get the OS list")
54 if not opts.no_headers:
55 headers = {"name": "Name"}
60 for (name, variants) in result:
61 os_names.extend([[n] for n in CalculateOSNames(name, variants)])
63 data = GenerateTable(separator=None, headers=headers, fields=["name"],
64 data=os_names, units=None)
72 def ShowOSInfo(opts, args):
73 """List detailed information about OSes in the cluster.
75 @param opts: the command line options selected by the user
77 @param args: should be an empty list
79 @return: the desired exit code
82 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants",
83 "parameters", "api_versions",
84 "blacklisted", "hidden"],
86 result = SubmitOpCode(op, opts=opts)
89 ToStderr("Can't get the OS list")
92 do_filter = bool(args)
94 for (name, valid, variants, parameters, api_versions, blk, hid) in result:
100 ToStdout("%s:", name)
101 ToStdout(" - valid: %s", valid)
102 ToStdout(" - hidden: %s", hid)
103 ToStdout(" - blacklisted: %s", blk)
105 ToStdout(" - API versions:")
106 for version in sorted(api_versions):
107 ToStdout(" - %s", version)
108 ToStdout(" - variants:")
109 for vname in variants:
110 ToStdout(" - %s", vname)
111 ToStdout(" - parameters:")
112 for pname, pdesc in parameters:
113 ToStdout(" - %s: %s", pname, pdesc)
118 ToStdout("%s: ", name)
124 def _OsStatus(status, diagnose):
125 """Beautifier function for OS status.
127 @type status: boolean
128 @param status: is the OS valid
129 @type diagnose: string
130 @param diagnose: the error message for invalid OSes
132 @return: a formatted status
138 return "invalid - %s" % diagnose
141 def DiagnoseOS(opts, args):
142 """Analyse all OSes on this cluster.
144 @param opts: the command line options selected by the user
146 @param args: should be an empty list
148 @return: the desired exit code
151 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants",
152 "node_status", "hidden",
153 "blacklisted"], names=[])
154 result = SubmitOpCode(op, opts=opts)
157 ToStderr("Can't get the OS list")
162 for os_name, _, os_variants, node_data, hid, blk in result:
166 for node_name, node_info in node_data.iteritems():
167 nodes_hidden[node_name] = []
168 if node_info: # at least one entry in the per-node list
169 (fo_path, fo_status, fo_msg, fo_variants,
170 fo_params, fo_api) = node_info.pop(0)
171 fo_msg = "%s (path: %s)" % (_OsStatus(fo_status, fo_msg), fo_path)
173 max_os_api = max(fo_api)
174 fo_msg += " [API versions: %s]" % utils.CommaJoin(fo_api)
177 fo_msg += " [no API versions declared]"
179 if max_os_api >= constants.OS_API_V15:
181 fo_msg += " [variants: %s]" % utils.CommaJoin(fo_variants)
183 fo_msg += " [no variants]"
184 if max_os_api >= constants.OS_API_V20:
186 fo_msg += (" [parameters: %s]" %
187 utils.CommaJoin([v[0] for v in fo_params]))
189 fo_msg += " [no parameters]"
191 nodes_valid[node_name] = fo_msg
193 nodes_bad[node_name] = fo_msg
194 for hpath, hstatus, hmsg, _, _, _ in node_info:
195 nodes_hidden[node_name].append(" [hidden] path: %s, status: %s" %
196 (hpath, _OsStatus(hstatus, hmsg)))
198 nodes_bad[node_name] = "OS not found"
200 if nodes_valid and not nodes_bad:
202 elif not nodes_valid and nodes_bad:
206 status = "partial valid"
209 def _OutputPerNodeOSStatus(msg_map):
210 map_k = utils.NiceSort(msg_map.keys())
211 for node_name in map_k:
212 ToStdout(" Node: %s, status: %s", node_name, msg_map[node_name])
213 for msg in nodes_hidden[node_name]:
216 st_msg = "OS: %s [global status: %s]" % (os_name, status)
218 st_msg += " [hidden]"
220 st_msg += " [blacklisted]"
223 ToStdout(" Variants: [%s]" % utils.CommaJoin(os_variants))
224 _OutputPerNodeOSStatus(nodes_valid)
225 _OutputPerNodeOSStatus(nodes_bad)
231 def ModifyOS(opts, args):
232 """Modify OS parameters for one OS.
234 @param opts: the command line options selected by the user
236 @param args: should be a list with one entry
238 @return: the desired exit code
244 os_hvp = {os: dict(opts.hvparams)}
249 osp = {os: opts.osparams}
253 if opts.hidden is not None:
255 ohid = [(constants.DDM_ADD, os)]
257 ohid = [(constants.DDM_REMOVE, os)]
261 if opts.blacklisted is not None:
263 oblk = [(constants.DDM_ADD, os)]
265 oblk = [(constants.DDM_REMOVE, os)]
269 if not (os_hvp or osp or ohid or oblk):
270 ToStderr("At least one of OS parameters or hypervisor parameters"
274 op = opcodes.OpSetClusterParams(os_hvp=os_hvp,
278 SubmitOpCode(op, opts=opts)
285 ListOS, ARGS_NONE, [NOHDR_OPT, PRIORITY_OPT],
286 "", "Lists all valid operating systems on the cluster"),
288 DiagnoseOS, ARGS_NONE, [PRIORITY_OPT],
289 "", "Diagnose all operating systems"),
291 ShowOSInfo, [ArgOs()], [PRIORITY_OPT],
292 "", "Show detailed information about "
293 "operating systems"),
295 ModifyOS, ARGS_ONE_OS,
296 [HVLIST_OPT, OSPARAMS_OPT, DRY_RUN_OPT, PRIORITY_OPT,
297 HID_OS_OPT, BLK_OS_OPT],
298 "", "Modify the OS parameters"),
301 if __name__ == '__main__':
302 sys.exit(GenericMain(commands))