Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-os @ 59885275

History | View | Annotate | Download (4.3 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 _DiagnoseByOS(rlist):
34
  """Remap an OpDiagnoseOS() return list into an a per-os per-node dictionary
35

    
36
    Args:
37
      rlist: a map with nodes as keys and diagnoseobjects as values
38

    
39
    Returns:
40
      map: a map with osnames as keys and as value another map, with nodes as
41
           keys and diagnoseobjects as values
42
           e.g. {"debian-etch": {"node1": <object>, "node2": <object>}}
43

    
44
  """
45
  all_os = {}
46
  for node_name, nr in rlist.iteritems():
47
    if not nr:
48
      continue
49
    for os in nr:
50
      if os.name not in all_os:
51
        all_os[os.name] = {}
52
      if node_name not in all_os[os.name]:
53
        all_os[os.name][node_name] = []
54
      all_os[os.name][node_name].append(os)
55

    
56
  return all_os
57

    
58

    
59
def ListOS(opts, args):
60
  """List the OSes existing on this node.
61

    
62
  """
63
  op = opcodes.OpDiagnoseOS()
64
  result = SubmitOpCode(op)
65

    
66
  if not result:
67
    logger.ToStdout("Can't get the OS list")
68
    return 1
69

    
70
  node_data = result
71
  num_nodes = len(node_data)
72
  all_os = _DiagnoseByOS(node_data)
73

    
74
  valid_os = []
75
  for os_name, os_node_data in all_os.iteritems():
76
    if len(os_node_data) != num_nodes:
77
      continue
78

    
79
    if utils.all(os_node_data.values(), lambda l: l[0]):
80
      valid_os.append(os_name)
81

    
82
  if not opts.no_headers:
83
    headers = {"name": "Name"}
84
  else:
85
    headers = None
86

    
87
  data = GenerateTable(separator=None, headers=headers, fields=["name"],
88
                       data=[[os] for os in valid_os])
89

    
90
  for line in data:
91
    logger.ToStdout(line)
92

    
93
  return 0
94

    
95

    
96
def DiagnoseOS(opts, args):
97
  """Analyse all OSes on this cluster.
98

    
99
  """
100
  op = opcodes.OpDiagnoseOS()
101
  result = SubmitOpCode(op)
102

    
103
  if not result:
104
    logger.ToStdout("Can't get the OS list")
105
    return 1
106

    
107
  node_data = result
108
  all_os = _DiagnoseByOS(node_data)
109

    
110
  has_bad = False
111

    
112
  for os_name in all_os:
113
    nodes_valid = {}
114
    nodes_bad = {}
115
    for node_name in node_data:
116
      if node_name in all_os[os_name]:
117
        first_os = all_os[os_name][node_name].pop(0)
118
        first_os_msg = ("%s (path: %s)" %
119
                        (first_os.status, first_os.path))
120
        if first_os:
121
          nodes_valid[node_name] = first_os_msg
122
        else:
123
          nodes_bad[node_name] = first_os_msg
124
      else:
125
        nodes_bad[node_name] = "OS not found"
126

    
127
    if nodes_valid and not nodes_bad:
128
      status = "valid"
129
    elif not nodes_valid and nodes_bad:
130
      status = "invalid"
131
      has_bad = True
132
    else:
133
      status = "partial valid"
134
      has_bad = True
135

    
136
    def _OutputNodeHiddenOSStatus(dobj_list):
137
      for dobj in dobj_list:
138
        logger.ToStdout("    [hidden] path: %s, status: %s" %
139
                        (dobj.path, dobj.status))
140

    
141
    def _OutputPerNodeOSStatus(msg_map):
142
      map_k = utils.NiceSort(msg_map.keys())
143
      for node_name in map_k:
144
        logger.ToStdout("  Node: %s, status: %s" %
145
                        (node_name, msg_map[node_name]))
146
        if node_name in all_os[os_name]:
147
          _OutputNodeHiddenOSStatus(all_os[os_name][node_name])
148

    
149
    logger.ToStdout("OS: %s [global status: %s]" % (os_name, status))
150
    _OutputPerNodeOSStatus(nodes_valid)
151
    _OutputPerNodeOSStatus(nodes_bad)
152
    logger.ToStdout("")
153

    
154
  return int(has_bad)
155

    
156

    
157
commands = {
158
  'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
159
           "Lists all valid OSes on the master"),
160
  'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
161
               "Diagnose all OSes"),
162
  }
163

    
164
if __name__ == '__main__':
165
  sys.exit(GenericMain(commands))