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
34 from ganeti import constants
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)
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 _OsStatus(status, diagnose):
75 """Beautifier function for OS status.
78 @param status: is the OS valid
79 @type diagnose: string
80 @param diagnose: the error message for invalid OSes
82 @return: a formatted status
88 return "invalid - %s" % diagnose
90 def DiagnoseOS(opts, args):
91 """Analyse all OSes on this cluster.
93 @param opts: the command line options selected by the user
95 @param args: should be an empty list
97 @return: the desired exit code
100 op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants",
101 "node_status"], names=[])
102 result = SubmitOpCode(op)
105 ToStderr("Can't get the OS list")
110 for os_name, _, os_variants, node_data in result:
114 for node_name, node_info in node_data.iteritems():
115 nodes_hidden[node_name] = []
116 if node_info: # at least one entry in the per-node list
117 (first_os_path, first_os_status, first_os_msg,
118 first_os_variants) = node_info.pop(0)
119 if not first_os_variants:
120 first_os_variants = []
121 first_os_msg = ("%s (path: %s) [variants: %s]" %
122 (_OsStatus(first_os_status, first_os_msg),
123 first_os_path, utils.CommaJoin(first_os_variants)))
125 nodes_valid[node_name] = first_os_msg
127 nodes_bad[node_name] = first_os_msg
128 for hpath, hstatus, hmsg in node_info:
129 nodes_hidden[node_name].append(" [hidden] path: %s, status: %s" %
130 (hpath, _OsStatus(hstatus, hmsg)))
132 nodes_bad[node_name] = "OS not found"
134 if nodes_valid and not nodes_bad:
136 elif not nodes_valid and nodes_bad:
140 status = "partial valid"
143 def _OutputPerNodeOSStatus(msg_map):
144 map_k = utils.NiceSort(msg_map.keys())
145 for node_name in map_k:
146 ToStdout(" Node: %s, status: %s", node_name, msg_map[node_name])
147 for msg in nodes_hidden[node_name]:
150 ToStdout("OS: %s [global status: %s]", os_name, status)
152 ToStdout(" Variants: [%s]" % utils.CommaJoin(os_variants))
153 _OutputPerNodeOSStatus(nodes_valid)
154 _OutputPerNodeOSStatus(nodes_bad)
162 ListOS, ARGS_NONE, [NOHDR_OPT], "", "Lists all valid OSes on the master"),
164 DiagnoseOS, ARGS_NONE, [], "", "Diagnose all OSes"),
167 if __name__ == '__main__':
168 sys.exit(GenericMain(commands))