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 opcodes
33 from ganeti import utils
36 def ListOS(opts, args):
37 """List the valid OSes in the cluster.
39 @param opts: the command line options selected by the user
41 @param args: should be an empty list
43 @return: the desired exit code
46 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants"],
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, valid, variants) in result:
62 os_names.extend([[n] for n in CalculateOSNames(name, variants)])
64 data = GenerateTable(separator=None, headers=headers, fields=["name"],
65 data=os_names, units=None)
73 def ShowOSInfo(opts, args):
74 """List detailed information about OSes in the cluster.
76 @param opts: the command line options selected by the user
78 @param args: should be an empty list
80 @return: the desired exit code
83 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants",
84 "parameters", "api_versions"],
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) in result:
100 ToStdout("%s:", name)
101 ToStdout(" - valid: %s", valid)
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
138 def DiagnoseOS(opts, args):
139 """Analyse all OSes on this cluster.
141 @param opts: the command line options selected by the user
143 @param args: should be an empty list
145 @return: the desired exit code
148 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants",
149 "node_status"], names=[])
150 result = SubmitOpCode(op, opts=opts)
153 ToStderr("Can't get the OS list")
158 for os_name, _, os_variants, node_data in result:
162 for node_name, node_info in node_data.iteritems():
163 nodes_hidden[node_name] = []
164 if node_info: # at least one entry in the per-node list
165 (first_os_path, first_os_status, first_os_msg,
166 first_os_variants, _, first_os_api) = node_info.pop(0)
167 if not first_os_variants:
168 first_os_variants = []
169 first_os_msg = ("%s (path: %s) [variants: %s] [api: %s]" %
170 (_OsStatus(first_os_status, first_os_msg),
171 first_os_path, utils.CommaJoin(first_os_variants),
172 utils.CommaJoin(first_os_api)))
174 nodes_valid[node_name] = first_os_msg
176 nodes_bad[node_name] = first_os_msg
177 for hpath, hstatus, hmsg, _, _, _ in node_info:
178 nodes_hidden[node_name].append(" [hidden] path: %s, status: %s" %
179 (hpath, _OsStatus(hstatus, hmsg)))
181 nodes_bad[node_name] = "OS not found"
183 if nodes_valid and not nodes_bad:
185 elif not nodes_valid and nodes_bad:
189 status = "partial valid"
192 def _OutputPerNodeOSStatus(msg_map):
193 map_k = utils.NiceSort(msg_map.keys())
194 for node_name in map_k:
195 ToStdout(" Node: %s, status: %s", node_name, msg_map[node_name])
196 for msg in nodes_hidden[node_name]:
199 ToStdout("OS: %s [global status: %s]", os_name, status)
201 ToStdout(" Variants: [%s]" % utils.CommaJoin(os_variants))
202 _OutputPerNodeOSStatus(nodes_valid)
203 _OutputPerNodeOSStatus(nodes_bad)
209 def ModifyOS(opts, args):
210 """Modify OS parameters for one OS.
212 @param opts: the command line options selected by the user
214 @param args: should be a list with one entry
216 @return: the desired exit code
222 os_hvp = {os: dict(opts.hvparams)}
227 osp = {os: opts.osparams}
231 if not (os_hvp or osp):
232 ToStderr("At least one of OS parameters or hypervisor parameters"
236 op = opcodes.OpSetClusterParams(vg_name=None,
237 enabled_hypervisors=None,
241 candidate_pool_size=None,
251 ListOS, ARGS_NONE, [NOHDR_OPT], "", "Lists all valid operating systems"
254 DiagnoseOS, ARGS_NONE, [], "", "Diagnose all operating systems"),
256 ShowOSInfo, [ArgOs()], [], "", "Show detailed information about "
257 "operating systems"),
259 ModifyOS, ARGS_ONE_OS, [HVLIST_OPT, OSPARAMS_OPT], "",
260 "Modify the OS parameters"),
263 if __name__ == '__main__':
264 sys.exit(GenericMain(commands))