Use new query function for exports in gnt-backup
[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 import sys
23 from optparse import make_option
24
25 from ganeti.cli import *
26 from ganeti import opcodes
27 from ganeti import logger
28 from ganeti import objects
29 from ganeti import utils
30 from ganeti import errors
31 from ganeti import constants
32
33
34 def ListOS(opts, args):
35   """List the OSes existing on this node.
36
37   """
38   op = opcodes.OpDiagnoseOS(output_fields=["name", "valid"], names=[])
39   result = SubmitOpCode(op)
40
41   if not result:
42     logger.ToStdout("Can't get the OS list")
43     return 1
44
45   if not opts.no_headers:
46     headers = {"name": "Name"}
47   else:
48     headers = None
49
50   data = GenerateTable(separator=None, headers=headers, fields=["name"],
51                        data=[[row[0]] for row in result if row[1]])
52
53   for line in data:
54     logger.ToStdout(line)
55
56   return 0
57
58
59 def DiagnoseOS(opts, args):
60   """Analyse all OSes on this cluster.
61
62   """
63   op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "node_status"],
64                             names=[])
65   result = SubmitOpCode(op)
66
67   if not result:
68     logger.ToStdout("Can't get the OS list")
69     return 1
70
71   has_bad = False
72
73   for os_name, os_valid, node_data in result:
74     nodes_valid = {}
75     nodes_bad = {}
76     nodes_hidden = {}
77     for node_name, node_info in node_data.iteritems():
78       nodes_hidden[node_name] = []
79       if node_info: # at least one entry in the per-node list
80         first_os_status, first_os_path = node_info.pop(0)
81         first_os_msg = ("%s (path: %s)" %
82                         (first_os_status, first_os_path))
83         if first_os_status == constants.OS_VALID_STATUS:
84           nodes_valid[node_name] = first_os_msg
85         else:
86           nodes_bad[node_name] = first_os_msg
87         for hstatus, hpath in node_info:
88           nodes_hidden[node_name].append("    [hidden] path: %s, status: %s" %
89                                          (hpath, hstatus))
90       else:
91         nodes_bad[node_name] = "OS not found"
92
93     if nodes_valid and not nodes_bad:
94       status = "valid"
95     elif not nodes_valid and nodes_bad:
96       status = "invalid"
97       has_bad = True
98     else:
99       status = "partial valid"
100       has_bad = True
101
102     def _OutputPerNodeOSStatus(msg_map):
103       map_k = utils.NiceSort(msg_map.keys())
104       for node_name in map_k:
105         logger.ToStdout("  Node: %s, status: %s" %
106                         (node_name, msg_map[node_name]))
107         for msg in nodes_hidden[node_name]:
108           logger.ToStdout(msg)
109
110     logger.ToStdout("OS: %s [global status: %s]" % (os_name, status))
111     _OutputPerNodeOSStatus(nodes_valid)
112     _OutputPerNodeOSStatus(nodes_bad)
113     logger.ToStdout("")
114
115   return int(has_bad)
116
117
118 commands = {
119   'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
120            "Lists all valid OSes on the master"),
121   'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
122                "Diagnose all OSes"),
123   }
124
125 if __name__ == '__main__':
126   sys.exit(GenericMain(commands))