Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-os @ a2656173

History | View | Annotate | Download (5.9 kB)

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

    
32

    
33
def _DiagnoseOSValid(obj):
34
  """Verify whether an OS diagnose object represents a valid OS
35

    
36
    Args:
37
      obj: an diagnostic object as returned by OpDiagnoseOS
38

    
39
    Returns:
40
      bool: OS validity status
41
  """
42

    
43
  if isinstance(obj, objects.OS):
44
    return True
45
  elif isinstance(obj, errors.InvalidOS):
46
    return False
47
  else:
48
    raise errors.ProgrammerError('unknown OS diagnose type')
49

    
50

    
51
def _DiagnoseOSName(obj):
52
  """Generate a status message for an OS diagnose object.
53

    
54
    Args:
55
      obj: an diagnostic object as returned by OpDiagnoseOS
56

    
57
    Returns:
58
      string: the name of the OS in question
59
  """
60

    
61
  if _DiagnoseOSValid(obj):
62
    return obj.name
63
  else:
64
    return obj.args[0]
65

    
66

    
67
def _DiagnoseOSStatus(obj):
68
  """Generate a status message for an OS diagnose object.
69

    
70
    Args:
71
      obj: an diagnostic object as returned by OpDiagnoseOS
72

    
73
    Returns:
74
      string: a description of the OS status
75
  """
76

    
77
  if _DiagnoseOSValid(obj):
78
    return "valid (path: %s)" % obj.path
79
  else:
80
    return "%s (path: %s)" % (obj.args[2], obj.args[1])
81

    
82

    
83
def _DiagnoseByOS(rlist):
84
  """Remap an OpDiagnoseOS() return list into an a per-os per-node dictionary
85

    
86
    Args:
87
      rlist: a map with nodes as keys and diagnoseobjects as values
88

    
89
    Returns:
90
      map: a map with osnames as keys and as value another map, with nodes as
91
           keys and diagnoseobjects as values
92
           e.g. {"debian-etch": {"node1": <object>, "node2": <object>}}
93
  """
94

    
95
  all_os = {}
96
  for node_name, nr in rlist.iteritems():
97
    if not nr:
98
      continue
99
    for obj in nr:
100
      os_name = _DiagnoseOSName(obj)
101
      if os_name not in all_os:
102
        all_os[os_name] = {}
103
      if node_name not in all_os[os_name]:
104
        all_os[os_name][node_name] = []
105
      all_os[os_name][node_name].append(obj)
106

    
107
  return all_os
108

    
109

    
110
def ListOS(opts, args):
111
  """List the OSes existing on this node.
112

    
113
  """
114
  op = opcodes.OpDiagnoseOS()
115
  result = SubmitOpCode(op)
116

    
117
  if not result:
118
    logger.ToStdout("Can't get the OS list")
119
    return 1
120

    
121
  node_data = result
122
  num_nodes = len(node_data)
123
  all_os = _DiagnoseByOS(node_data)
124

    
125
  valid_os = []
126
  for os_name, os_node_data in all_os.iteritems():
127
    if len(os_node_data) != num_nodes:
128
      continue
129
    valid = True
130
    for l in os_node_data.values():
131
      if not _DiagnoseOSValid(l[0]):
132
        valid = False
133
        break
134
    if valid:
135
      valid_os.append(os_name)
136

    
137
  if not opts.no_headers:
138
    headers = {"name": "Name"}
139
  else:
140
    headers = None
141

    
142
  data = GenerateTable(separator=None, headers=headers, fields=["name"],
143
                       data=[[os] for os in valid_os])
144

    
145
  for line in data:
146
    logger.ToStdout(line)
147

    
148
  return 0
149

    
150

    
151
def DiagnoseOS(opts, args):
152
  """Analyse all OSes on this cluster.
153

    
154
  """
155
  op = opcodes.OpDiagnoseOS()
156
  result = SubmitOpCode(op)
157

    
158
  if not result:
159
    logger.ToStdout("Can't get the OS list")
160
    return 1
161

    
162
  node_data = result
163
  all_os = _DiagnoseByOS(node_data)
164

    
165
  format = "%-*s %-*s %s"
166

    
167
  max_name = len('Name')
168
  if all_os:
169
    max_name = max(max_name, max([len(name) for name in all_os]))
170

    
171
  max_node = len('Status/Node')
172
  max_node = max(max_node, max([len(name) for name in node_data]))
173

    
174
  logger.ToStdout(format % (max_name, 'Name', max_node, 'Status/Node',
175
                            'Details'))
176

    
177
  has_bad = False
178

    
179
  for os_name in all_os:
180
    nodes_valid = {}
181
    nodes_bad = {}
182
    for node_name in node_data:
183
      if node_name in all_os[os_name]:
184
        first_os = all_os[os_name][node_name].pop(0)
185
        first_os_status = _DiagnoseOSStatus(first_os)
186
        if _DiagnoseOSValid(first_os):
187
          nodes_valid[node_name] = first_os_status
188
        else:
189
          nodes_bad[node_name] = first_os_status
190
      else:
191
        nodes_bad[node_name] = "OS not found"
192

    
193
    if nodes_valid and not nodes_bad:
194
      status = "valid"
195
    elif not nodes_valid and nodes_bad:
196
      status = "invalid"
197
      has_bad = True
198
    else:
199
      status = "partial valid"
200
      has_bad = True
201

    
202
    def _OutputNodeHiddenOSStatus(dobj_list):
203
      for dobj in dobj_list:
204
        logger.ToStdout(format % (max_name, "", max_node, "",
205
                                  "[hidden] %s" %
206
                                  _DiagnoseOSStatus(dobj)))
207

    
208
    def _OutputPerNodeOSStatus(status_map):
209
      map_k = utils.NiceSort(status_map.keys())
210
      for node_name in map_k:
211
        logger.ToStdout(format % (max_name, "", max_node,
212
                                  node_name, status_map[node_name]))
213
        if node_name in all_os[os_name]:
214
          _OutputNodeHiddenOSStatus(all_os[os_name][node_name])
215

    
216
    logger.ToStdout(format % (max_name, os_name, max_node, status, ""))
217
    _OutputPerNodeOSStatus(nodes_valid)
218
    _OutputPerNodeOSStatus(nodes_bad)
219

    
220
  return int(has_bad)
221

    
222

    
223
commands = {
224
  'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
225
           "Lists all valid OSes on the master"),
226
  'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
227
               "Diagnose all OSes"),
228
  }
229

    
230
if __name__ == '__main__':
231
  sys.exit(GenericMain(commands))