Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-os @ cc0451f3

History | View | Annotate | Download (5.8 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: '%s'" % type(obj))
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

    
130
    if utils.all(os_node_data.values(), lambda l: _DiagnoseOSValid(l[0])):
131
      valid_os.append(os_name)
132

    
133
  if not opts.no_headers:
134
    headers = {"name": "Name"}
135
  else:
136
    headers = None
137

    
138
  data = GenerateTable(separator=None, headers=headers, fields=["name"],
139
                       data=[[os] for os in valid_os])
140

    
141
  for line in data:
142
    logger.ToStdout(line)
143

    
144
  return 0
145

    
146

    
147
def DiagnoseOS(opts, args):
148
  """Analyse all OSes on this cluster.
149

    
150
  """
151
  op = opcodes.OpDiagnoseOS()
152
  result = SubmitOpCode(op)
153

    
154
  if not result:
155
    logger.ToStdout("Can't get the OS list")
156
    return 1
157

    
158
  node_data = result
159
  all_os = _DiagnoseByOS(node_data)
160

    
161
  format = "%-*s %-*s %s"
162

    
163
  max_name = len('Name')
164
  if all_os:
165
    max_name = max(max_name, max([len(name) for name in all_os]))
166

    
167
  max_node = len('Status/Node')
168
  max_node = max(max_node, max([len(name) for name in node_data]))
169

    
170
  logger.ToStdout(format % (max_name, 'Name', max_node, 'Status/Node',
171
                            'Details'))
172

    
173
  has_bad = False
174

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

    
189
    if nodes_valid and not nodes_bad:
190
      status = "valid"
191
    elif not nodes_valid and nodes_bad:
192
      status = "invalid"
193
      has_bad = True
194
    else:
195
      status = "partial valid"
196
      has_bad = True
197

    
198
    def _OutputNodeHiddenOSStatus(dobj_list):
199
      for dobj in dobj_list:
200
        logger.ToStdout(format % (max_name, "", max_node, "",
201
                                  "[hidden] %s" %
202
                                  _DiagnoseOSStatus(dobj)))
203

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

    
212
    logger.ToStdout(format % (max_name, os_name, max_node, status, ""))
213
    _OutputPerNodeOSStatus(nodes_valid)
214
    _OutputPerNodeOSStatus(nodes_bad)
215

    
216
  return int(has_bad)
217

    
218

    
219
commands = {
220
  'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
221
           "Lists all valid OSes on the master"),
222
  'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
223
               "Diagnose all OSes"),
224
  }
225

    
226
if __name__ == '__main__':
227
  sys.exit(GenericMain(commands))