Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-os @ f4bc1f2c

History | View | Annotate | Download (4.3 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 fbdb07b9 Guido Trotter
33 fbdb07b9 Guido Trotter
def _DiagnoseByOS(rlist):
34 216fe2f3 Guido Trotter
  """Remap an OpDiagnoseOS() return list into an a per-os per-node dictionary
35 216fe2f3 Guido Trotter
36 216fe2f3 Guido Trotter
    Args:
37 216fe2f3 Guido Trotter
      rlist: a map with nodes as keys and diagnoseobjects as values
38 216fe2f3 Guido Trotter
39 216fe2f3 Guido Trotter
    Returns:
40 216fe2f3 Guido Trotter
      map: a map with osnames as keys and as value another map, with nodes as
41 216fe2f3 Guido Trotter
           keys and diagnoseobjects as values
42 216fe2f3 Guido Trotter
           e.g. {"debian-etch": {"node1": <object>, "node2": <object>}}
43 216fe2f3 Guido Trotter
44 2527691a Guido Trotter
  """
45 216fe2f3 Guido Trotter
  all_os = {}
46 216fe2f3 Guido Trotter
  for node_name, nr in rlist.iteritems():
47 216fe2f3 Guido Trotter
    if not nr:
48 216fe2f3 Guido Trotter
      continue
49 8fa42c7c Guido Trotter
    for os in nr:
50 8fa42c7c Guido Trotter
      if os.name not in all_os:
51 8fa42c7c Guido Trotter
        all_os[os.name] = {}
52 8fa42c7c Guido Trotter
      if node_name not in all_os[os.name]:
53 8fa42c7c Guido Trotter
        all_os[os.name][node_name] = []
54 8fa42c7c Guido Trotter
      all_os[os.name][node_name].append(os)
55 216fe2f3 Guido Trotter
56 216fe2f3 Guido Trotter
  return all_os
57 216fe2f3 Guido Trotter
58 fbdb07b9 Guido Trotter
59 a8083063 Iustin Pop
def ListOS(opts, args):
60 a8083063 Iustin Pop
  """List the OSes existing on this node.
61 a8083063 Iustin Pop
62 a8083063 Iustin Pop
  """
63 a8083063 Iustin Pop
  op = opcodes.OpDiagnoseOS()
64 a8083063 Iustin Pop
  result = SubmitOpCode(op)
65 a8083063 Iustin Pop
66 a8083063 Iustin Pop
  if not result:
67 a8083063 Iustin Pop
    logger.ToStdout("Can't get the OS list")
68 a8083063 Iustin Pop
    return 1
69 a8083063 Iustin Pop
70 216fe2f3 Guido Trotter
  node_data = result
71 216fe2f3 Guido Trotter
  num_nodes = len(node_data)
72 fbdb07b9 Guido Trotter
  all_os = _DiagnoseByOS(node_data)
73 216fe2f3 Guido Trotter
74 216fe2f3 Guido Trotter
  valid_os = []
75 216fe2f3 Guido Trotter
  for os_name, os_node_data in all_os.iteritems():
76 216fe2f3 Guido Trotter
    if len(os_node_data) != num_nodes:
77 216fe2f3 Guido Trotter
      continue
78 cc0451f3 Guido Trotter
79 8fa42c7c Guido Trotter
    if utils.all(os_node_data.values(), lambda l: l[0]):
80 216fe2f3 Guido Trotter
      valid_os.append(os_name)
81 a8083063 Iustin Pop
82 a8083063 Iustin Pop
  if not opts.no_headers:
83 606d909d Michael Hanselmann
    headers = {"name": "Name"}
84 606d909d Michael Hanselmann
  else:
85 606d909d Michael Hanselmann
    headers = None
86 a8083063 Iustin Pop
87 16be8703 Iustin Pop
  data = GenerateTable(separator=None, headers=headers, fields=["name"],
88 216fe2f3 Guido Trotter
                       data=[[os] for os in valid_os])
89 16be8703 Iustin Pop
90 16be8703 Iustin Pop
  for line in data:
91 16be8703 Iustin Pop
    logger.ToStdout(line)
92 a8083063 Iustin Pop
93 a8083063 Iustin Pop
  return 0
94 a8083063 Iustin Pop
95 b07a9f05 Guido Trotter
96 a8083063 Iustin Pop
def DiagnoseOS(opts, args):
97 a8083063 Iustin Pop
  """Analyse all OSes on this cluster.
98 a8083063 Iustin Pop
99 a8083063 Iustin Pop
  """
100 a8083063 Iustin Pop
  op = opcodes.OpDiagnoseOS()
101 a8083063 Iustin Pop
  result = SubmitOpCode(op)
102 a8083063 Iustin Pop
103 a8083063 Iustin Pop
  if not result:
104 a8083063 Iustin Pop
    logger.ToStdout("Can't get the OS list")
105 a8083063 Iustin Pop
    return 1
106 a8083063 Iustin Pop
107 a8083063 Iustin Pop
  node_data = result
108 fbdb07b9 Guido Trotter
  all_os = _DiagnoseByOS(node_data)
109 216fe2f3 Guido Trotter
110 a2656173 Michael Hanselmann
  has_bad = False
111 a2656173 Michael Hanselmann
112 a8083063 Iustin Pop
  for os_name in all_os:
113 b07a9f05 Guido Trotter
    nodes_valid = {}
114 a8083063 Iustin Pop
    nodes_bad = {}
115 a8083063 Iustin Pop
    for node_name in node_data:
116 694e2444 Guido Trotter
      if node_name in all_os[os_name]:
117 b07a9f05 Guido Trotter
        first_os = all_os[os_name][node_name].pop(0)
118 48f85f75 Guido Trotter
        first_os_msg = ("%s (path: %s)" %
119 8fa42c7c Guido Trotter
                        (first_os.status, first_os.path))
120 8fa42c7c Guido Trotter
        if first_os:
121 48f85f75 Guido Trotter
          nodes_valid[node_name] = first_os_msg
122 b07a9f05 Guido Trotter
        else:
123 48f85f75 Guido Trotter
          nodes_bad[node_name] = first_os_msg
124 a8083063 Iustin Pop
      else:
125 b07a9f05 Guido Trotter
        nodes_bad[node_name] = "OS not found"
126 a8083063 Iustin Pop
127 a8083063 Iustin Pop
    if nodes_valid and not nodes_bad:
128 a8083063 Iustin Pop
      status = "valid"
129 a8083063 Iustin Pop
    elif not nodes_valid and nodes_bad:
130 a8083063 Iustin Pop
      status = "invalid"
131 a2656173 Michael Hanselmann
      has_bad = True
132 a8083063 Iustin Pop
    else:
133 a8083063 Iustin Pop
      status = "partial valid"
134 a2656173 Michael Hanselmann
      has_bad = True
135 694e2444 Guido Trotter
136 b07a9f05 Guido Trotter
    def _OutputNodeHiddenOSStatus(dobj_list):
137 b07a9f05 Guido Trotter
      for dobj in dobj_list:
138 d06565e0 Guido Trotter
        logger.ToStdout("    [hidden] path: %s, status: %s" %
139 8fa42c7c Guido Trotter
                        (dobj.path, dobj.status))
140 b07a9f05 Guido Trotter
141 48f85f75 Guido Trotter
    def _OutputPerNodeOSStatus(msg_map):
142 48f85f75 Guido Trotter
      map_k = utils.NiceSort(msg_map.keys())
143 b07a9f05 Guido Trotter
      for node_name in map_k:
144 d06565e0 Guido Trotter
        logger.ToStdout("  Node: %s, status: %s" %
145 107b0ccb Guido Trotter
                        (node_name, msg_map[node_name]))
146 b07a9f05 Guido Trotter
        if node_name in all_os[os_name]:
147 b07a9f05 Guido Trotter
          _OutputNodeHiddenOSStatus(all_os[os_name][node_name])
148 b07a9f05 Guido Trotter
149 107b0ccb Guido Trotter
    logger.ToStdout("OS: %s [global status: %s]" % (os_name, status))
150 b07a9f05 Guido Trotter
    _OutputPerNodeOSStatus(nodes_valid)
151 b07a9f05 Guido Trotter
    _OutputPerNodeOSStatus(nodes_bad)
152 dd96409a Guido Trotter
    logger.ToStdout("")
153 a8083063 Iustin Pop
154 a2656173 Michael Hanselmann
  return int(has_bad)
155 a2656173 Michael Hanselmann
156 a8083063 Iustin Pop
157 a8083063 Iustin Pop
commands = {
158 a8083063 Iustin Pop
  'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
159 a8083063 Iustin Pop
           "Lists all valid OSes on the master"),
160 a8083063 Iustin Pop
  'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
161 a8083063 Iustin Pop
               "Diagnose all OSes"),
162 a8083063 Iustin Pop
  }
163 a8083063 Iustin Pop
164 a8083063 Iustin Pop
if __name__ == '__main__':
165 3ecf6786 Iustin Pop
  sys.exit(GenericMain(commands))