Change scripts to use new argument definitions
[ganeti-local] / scripts / gnt-os
1 #!/usr/bin/python
2 #
3
4 # Copyright (C) 2006, 2007 Google Inc.
5 #
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.
10 #
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.
15 #
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
19 # 02110-1301, USA.
20
21
22 # pylint: disable-msg=W0401,W0614
23 # W0401: Wildcard import ganeti.cli
24 # W0614: Unused import %s from wildcard import (since we need cli)
25
26 import sys
27 from optparse import make_option
28
29 from ganeti.cli import *
30 from ganeti import opcodes
31 from ganeti import utils
32 from ganeti import constants
33
34
35 def ListOS(opts, args):
36   """List the valid OSes in the cluster.
37
38   @param opts: the command line options selected by the user
39   @type args: list
40   @param args: should be an empty list
41   @rtype: int
42   @return: the desired exit code
43
44   """
45   op = opcodes.OpDiagnoseOS(output_fields=["name", "valid"], names=[])
46   result = SubmitOpCode(op)
47
48   if not result:
49     ToStderr("Can't get the OS list")
50     return 1
51
52   if not opts.no_headers:
53     headers = {"name": "Name"}
54   else:
55     headers = None
56
57   data = GenerateTable(separator=None, headers=headers, fields=["name"],
58                        data=[[row[0]] for row in result if row[1]],
59                        units=None)
60
61   for line in data:
62     ToStdout(line)
63
64   return 0
65
66
67 def _OsStatus(status, diagnose):
68   """Beautifier function for OS status.
69
70   @type status: boolean
71   @param status: is the OS valid
72   @type diagnose: string
73   @param diagnose: the error message for invalid OSes
74   @rtype: string
75   @return: a formatted status
76
77   """
78   if status:
79     return "valid"
80   else:
81     return "invalid - %s" % diagnose
82
83 def DiagnoseOS(opts, args):
84   """Analyse all OSes on this cluster.
85
86   @param opts: the command line options selected by the user
87   @type args: list
88   @param args: should be an empty list
89   @rtype: int
90   @return: the desired exit code
91
92   """
93   op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "node_status"],
94                             names=[])
95   result = SubmitOpCode(op)
96
97   if not result:
98     ToStderr("Can't get the OS list")
99     return 1
100
101   has_bad = False
102
103   for os_name, os_valid, node_data in result:
104     nodes_valid = {}
105     nodes_bad = {}
106     nodes_hidden = {}
107     for node_name, node_info in node_data.iteritems():
108       nodes_hidden[node_name] = []
109       if node_info: # at least one entry in the per-node list
110         first_os_path, first_os_status, first_os_msg = node_info.pop(0)
111         first_os_msg = ("%s (path: %s)" % (_OsStatus(first_os_status,
112                                                      first_os_msg),
113                                            first_os_path))
114         if first_os_status:
115           nodes_valid[node_name] = first_os_msg
116         else:
117           nodes_bad[node_name] = first_os_msg
118         for hpath, hstatus, hmsg in node_info:
119           nodes_hidden[node_name].append("    [hidden] path: %s, status: %s" %
120                                          (hpath, _OsStatus(hstatus, hmsg)))
121       else:
122         nodes_bad[node_name] = "OS not found"
123
124     if nodes_valid and not nodes_bad:
125       status = "valid"
126     elif not nodes_valid and nodes_bad:
127       status = "invalid"
128       has_bad = True
129     else:
130       status = "partial valid"
131       has_bad = True
132
133     def _OutputPerNodeOSStatus(msg_map):
134       map_k = utils.NiceSort(msg_map.keys())
135       for node_name in map_k:
136         ToStdout("  Node: %s, status: %s", node_name, msg_map[node_name])
137         for msg in nodes_hidden[node_name]:
138           ToStdout(msg)
139
140     ToStdout("OS: %s [global status: %s]", os_name, status)
141     _OutputPerNodeOSStatus(nodes_valid)
142     _OutputPerNodeOSStatus(nodes_bad)
143     ToStdout("")
144
145   return int(has_bad)
146
147
148 commands = {
149   'list': (ListOS, [], [DEBUG_OPT, NOHDR_OPT], "",
150            "Lists all valid OSes on the master"),
151   'diagnose': (DiagnoseOS, [], [DEBUG_OPT], "",
152                "Diagnose all OSes"),
153   }
154
155 if __name__ == '__main__':
156   sys.exit(GenericMain(commands))