Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-os @ 2f31098c

History | View | Annotate | Download (5.1 kB)

1 a8083063 Iustin Pop
#!/usr/bin/python
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 a8083063 Iustin Pop
# Copyright (C) 2006, 2007 Google Inc.
5 a8083063 Iustin Pop
#
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
#
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
#
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 a8083063 Iustin Pop
import sys
23 a8083063 Iustin Pop
from optparse import make_option
24 a8083063 Iustin Pop
25 a8083063 Iustin Pop
from ganeti.cli import *
26 a8083063 Iustin Pop
from ganeti import opcodes
27 a8083063 Iustin Pop
from ganeti import logger
28 a8083063 Iustin Pop
from ganeti import objects
29 a8083063 Iustin Pop
from ganeti import utils
30 a8083063 Iustin Pop
from ganeti import errors
31 a8083063 Iustin Pop
32 a8083063 Iustin Pop
def ListOS(opts, args):
33 a8083063 Iustin Pop
  """List the OSes existing on this node.
34 a8083063 Iustin Pop
35 a8083063 Iustin Pop
  """
36 a8083063 Iustin Pop
  op = opcodes.OpDiagnoseOS()
37 a8083063 Iustin Pop
  result = SubmitOpCode(op)
38 a8083063 Iustin Pop
39 a8083063 Iustin Pop
  if not result:
40 a8083063 Iustin Pop
    logger.ToStdout("Can't get the OS list")
41 a8083063 Iustin Pop
    return 1
42 a8083063 Iustin Pop
43 a8083063 Iustin Pop
  # filter non-valid OS-es
44 a8083063 Iustin Pop
  oses = {}
45 a8083063 Iustin Pop
  for node_name in result:
46 a8083063 Iustin Pop
    oses[node_name] = [obj for obj in result[node_name]
47 a8083063 Iustin Pop
                       if isinstance(obj, objects.OS)]
48 a8083063 Iustin Pop
49 606d909d Michael Hanselmann
  # Get intersection of all OSes
50 a8083063 Iustin Pop
  fnode = oses.keys()[0]
51 a8083063 Iustin Pop
  os_set = set([os_inst.name for os_inst in oses[fnode]])
52 a8083063 Iustin Pop
  del oses[fnode]
53 a8083063 Iustin Pop
  for node in oses:
54 a8083063 Iustin Pop
    os_set &= set([os_inst.name for os_inst in oses[node]])
55 a8083063 Iustin Pop
56 a8083063 Iustin Pop
  if not opts.no_headers:
57 606d909d Michael Hanselmann
    headers = {"name": "Name"}
58 606d909d Michael Hanselmann
  else:
59 606d909d Michael Hanselmann
    headers = None
60 a8083063 Iustin Pop
61 16be8703 Iustin Pop
  data = GenerateTable(separator=None, headers=headers, fields=["name"],
62 16be8703 Iustin Pop
                       data=[[os] for os in os_set])
63 16be8703 Iustin Pop
64 16be8703 Iustin Pop
  for line in data:
65 16be8703 Iustin Pop
    logger.ToStdout(line)
66 a8083063 Iustin Pop
67 a8083063 Iustin Pop
  return 0
68 a8083063 Iustin Pop
69 b07a9f05 Guido Trotter
def _DiagnoseOSValid(obj):
70 b07a9f05 Guido Trotter
  """Verify whether an OS diagnose object represents a valid OS
71 b07a9f05 Guido Trotter
72 b07a9f05 Guido Trotter
    Args:
73 b07a9f05 Guido Trotter
      obj: an diagnostic object as returned by OpDiagnoseOS
74 b07a9f05 Guido Trotter
75 b07a9f05 Guido Trotter
    Returns:
76 b07a9f05 Guido Trotter
      bool: OS validity status
77 b07a9f05 Guido Trotter
  """
78 b07a9f05 Guido Trotter
79 b07a9f05 Guido Trotter
  if isinstance(obj, objects.OS):
80 b07a9f05 Guido Trotter
    return True
81 b07a9f05 Guido Trotter
  elif isinstance(obj, errors.InvalidOS):
82 b07a9f05 Guido Trotter
    return False
83 b07a9f05 Guido Trotter
  else:
84 b07a9f05 Guido Trotter
    raise errors.ProgrammerError('unknown OS diagnose type')
85 b07a9f05 Guido Trotter
86 b07a9f05 Guido Trotter
def _DiagnoseOSStatus(obj):
87 b07a9f05 Guido Trotter
  """Generate a status message for an OS diagnose object.
88 b07a9f05 Guido Trotter
89 b07a9f05 Guido Trotter
    Args:
90 b07a9f05 Guido Trotter
      obj: an diagnostic object as returned by OpDiagnoseOS
91 b07a9f05 Guido Trotter
92 b07a9f05 Guido Trotter
    Returns:
93 b07a9f05 Guido Trotter
      string: a description of the OS status
94 b07a9f05 Guido Trotter
  """
95 b07a9f05 Guido Trotter
96 b07a9f05 Guido Trotter
  if _DiagnoseOSValid(obj):
97 b07a9f05 Guido Trotter
    return "valid (path: %s)" % obj.path
98 b07a9f05 Guido Trotter
  else:
99 b07a9f05 Guido Trotter
    return "%s (path: %s)" % (obj.args[2], obj.args[1])
100 b07a9f05 Guido Trotter
101 a8083063 Iustin Pop
def DiagnoseOS(opts, args):
102 a8083063 Iustin Pop
  """Analyse all OSes on this cluster.
103 a8083063 Iustin Pop
104 a8083063 Iustin Pop
  """
105 a8083063 Iustin Pop
  op = opcodes.OpDiagnoseOS()
106 a8083063 Iustin Pop
  result = SubmitOpCode(op)
107 a8083063 Iustin Pop
108 a8083063 Iustin Pop
  if not result:
109 a8083063 Iustin Pop
    logger.ToStdout("Can't get the OS list")
110 a8083063 Iustin Pop
    return 1
111 a8083063 Iustin Pop
112 a8083063 Iustin Pop
  format = "%-*s %-*s %s"
113 a8083063 Iustin Pop
114 a8083063 Iustin Pop
  node_data = result
115 a8083063 Iustin Pop
  all_os = {}
116 a8083063 Iustin Pop
  for node_name in node_data:
117 a8083063 Iustin Pop
    nr = node_data[node_name]
118 a8083063 Iustin Pop
    if nr:
119 a8083063 Iustin Pop
      for obj in nr:
120 a8083063 Iustin Pop
        if isinstance(obj, objects.OS):
121 a8083063 Iustin Pop
          os_name = obj.name
122 a8083063 Iustin Pop
        else:
123 a8083063 Iustin Pop
          os_name = obj.args[0]
124 a8083063 Iustin Pop
        if os_name not in all_os:
125 a8083063 Iustin Pop
          all_os[os_name] = {}
126 694e2444 Guido Trotter
        if node_name not in all_os[os_name]:
127 694e2444 Guido Trotter
          all_os[os_name][node_name] = []
128 694e2444 Guido Trotter
        all_os[os_name][node_name].append(obj)
129 a8083063 Iustin Pop
130 a8083063 Iustin Pop
  max_name = len('Name')
131 a8083063 Iustin Pop
  if all_os:
132 a8083063 Iustin Pop
    max_name = max(max_name, max([len(name) for name in all_os]))
133 a8083063 Iustin Pop
134 a8083063 Iustin Pop
  max_node = len('Status/Node')
135 a8083063 Iustin Pop
  max_node = max(max_node, max([len(name) for name in node_data]))
136 a8083063 Iustin Pop
137 a8083063 Iustin Pop
  logger.ToStdout(format % (max_name, 'Name', max_node, 'Status/Node',
138 a8083063 Iustin Pop
                            'Details'))
139 a8083063 Iustin Pop
140 a8083063 Iustin Pop
  for os_name in all_os:
141 b07a9f05 Guido Trotter
    nodes_valid = {}
142 a8083063 Iustin Pop
    nodes_bad = {}
143 a8083063 Iustin Pop
    for node_name in node_data:
144 694e2444 Guido Trotter
      if node_name in all_os[os_name]:
145 b07a9f05 Guido Trotter
        first_os = all_os[os_name][node_name].pop(0)
146 b07a9f05 Guido Trotter
        first_os_status = _DiagnoseOSStatus(first_os)
147 b07a9f05 Guido Trotter
        if _DiagnoseOSValid(first_os):
148 b07a9f05 Guido Trotter
          nodes_valid[node_name] = first_os_status
149 b07a9f05 Guido Trotter
        else:
150 b07a9f05 Guido Trotter
          nodes_bad[node_name] = first_os_status
151 a8083063 Iustin Pop
      else:
152 b07a9f05 Guido Trotter
        nodes_bad[node_name] = "OS not found"
153 a8083063 Iustin Pop
154 a8083063 Iustin Pop
    if nodes_valid and not nodes_bad:
155 a8083063 Iustin Pop
      status = "valid"
156 a8083063 Iustin Pop
    elif not nodes_valid and nodes_bad:
157 a8083063 Iustin Pop
      status = "invalid"
158 a8083063 Iustin Pop
    else:
159 a8083063 Iustin Pop
      status = "partial valid"
160 694e2444 Guido Trotter
161 b07a9f05 Guido Trotter
    def _OutputNodeHiddenOSStatus(dobj_list):
162 b07a9f05 Guido Trotter
      for dobj in dobj_list:
163 b07a9f05 Guido Trotter
        logger.ToStdout(format % (max_name, "", max_node, "",
164 b07a9f05 Guido Trotter
                                  "[hidden] %s" %
165 b07a9f05 Guido Trotter
                                  _DiagnoseOSStatus(dobj)))
166 b07a9f05 Guido Trotter
167 b07a9f05 Guido Trotter
    def _OutputPerNodeOSStatus(status_map):
168 b07a9f05 Guido Trotter
      map_k = utils.NiceSort(status_map.keys())
169 b07a9f05 Guido Trotter
      for node_name in map_k:
170 b07a9f05 Guido Trotter
        logger.ToStdout(format % (max_name, "", max_node,
171 b07a9f05 Guido Trotter
                                  node_name, status_map[node_name]))
172 b07a9f05 Guido Trotter
        if node_name in all_os[os_name]:
173 b07a9f05 Guido Trotter
          _OutputNodeHiddenOSStatus(all_os[os_name][node_name])
174 b07a9f05 Guido Trotter
175 a8083063 Iustin Pop
    logger.ToStdout(format % (max_name, os_name, max_node, status, ""))
176 b07a9f05 Guido Trotter
    _OutputPerNodeOSStatus(nodes_valid)
177 b07a9f05 Guido Trotter
    _OutputPerNodeOSStatus(nodes_bad)
178 a8083063 Iustin Pop
179 a8083063 Iustin Pop
180 a8083063 Iustin Pop
commands = {
181 a8083063 Iustin Pop
  'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
182 a8083063 Iustin Pop
           "Lists all valid OSes on the master"),
183 a8083063 Iustin Pop
  'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
184 a8083063 Iustin Pop
               "Diagnose all OSes"),
185 a8083063 Iustin Pop
  }
186 a8083063 Iustin Pop
187 a8083063 Iustin Pop
if __name__ == '__main__':
188 3ecf6786 Iustin Pop
  sys.exit(GenericMain(commands))