gnt-os 3.47 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/python
#

# Copyright (C) 2006, 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


import sys
from optparse import make_option

from ganeti.cli import *
from ganeti import opcodes
from ganeti import objects
from ganeti import utils
from ganeti import errors
30
from ganeti import constants
Guido Trotter's avatar
Guido Trotter committed
31

32

Iustin Pop's avatar
Iustin Pop committed
33
34
35
36
def ListOS(opts, args):
  """List the OSes existing on this node.

  """
37
  op = opcodes.OpDiagnoseOS(output_fields=["name", "valid"], names=[])
Iustin Pop's avatar
Iustin Pop committed
38
39
40
  result = SubmitOpCode(op)

  if not result:
41
    ToStderr("Can't get the OS list")
Iustin Pop's avatar
Iustin Pop committed
42
43
44
    return 1

  if not opts.no_headers:
45
46
47
    headers = {"name": "Name"}
  else:
    headers = None
Iustin Pop's avatar
Iustin Pop committed
48

49
  data = GenerateTable(separator=None, headers=headers, fields=["name"],
50
                       data=[[row[0]] for row in result if row[1]])
51
52

  for line in data:
53
    ToStdout(line)
Iustin Pop's avatar
Iustin Pop committed
54
55
56

  return 0

Guido Trotter's avatar
Guido Trotter committed
57

Iustin Pop's avatar
Iustin Pop committed
58
59
60
61
def DiagnoseOS(opts, args):
  """Analyse all OSes on this cluster.

  """
62
63
  op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "node_status"],
                            names=[])
Iustin Pop's avatar
Iustin Pop committed
64
65
66
  result = SubmitOpCode(op)

  if not result:
67
    ToStderr("Can't get the OS list")
Iustin Pop's avatar
Iustin Pop committed
68
69
    return 1

70
71
  has_bad = False

72
  for os_name, os_valid, node_data in result:
Guido Trotter's avatar
Guido Trotter committed
73
    nodes_valid = {}
Iustin Pop's avatar
Iustin Pop committed
74
    nodes_bad = {}
75
76
77
78
79
    nodes_hidden = {}
    for node_name, node_info in node_data.iteritems():
      nodes_hidden[node_name] = []
      if node_info: # at least one entry in the per-node list
        first_os_status, first_os_path = node_info.pop(0)
80
        first_os_msg = ("%s (path: %s)" %
81
82
                        (first_os_status, first_os_path))
        if first_os_status == constants.OS_VALID_STATUS:
83
          nodes_valid[node_name] = first_os_msg
Guido Trotter's avatar
Guido Trotter committed
84
        else:
85
          nodes_bad[node_name] = first_os_msg
86
87
88
        for hstatus, hpath in node_info:
          nodes_hidden[node_name].append("    [hidden] path: %s, status: %s" %
                                         (hpath, hstatus))
Iustin Pop's avatar
Iustin Pop committed
89
      else:
Guido Trotter's avatar
Guido Trotter committed
90
        nodes_bad[node_name] = "OS not found"
Iustin Pop's avatar
Iustin Pop committed
91
92
93
94
95

    if nodes_valid and not nodes_bad:
      status = "valid"
    elif not nodes_valid and nodes_bad:
      status = "invalid"
96
      has_bad = True
Iustin Pop's avatar
Iustin Pop committed
97
98
    else:
      status = "partial valid"
99
      has_bad = True
100

101
102
    def _OutputPerNodeOSStatus(msg_map):
      map_k = utils.NiceSort(msg_map.keys())
Guido Trotter's avatar
Guido Trotter committed
103
      for node_name in map_k:
104
        ToStdout("  Node: %s, status: %s", node_name, msg_map[node_name])
105
        for msg in nodes_hidden[node_name]:
106
          ToStdout(msg)
Guido Trotter's avatar
Guido Trotter committed
107

108
    ToStdout("OS: %s [global status: %s]", os_name, status)
Guido Trotter's avatar
Guido Trotter committed
109
110
    _OutputPerNodeOSStatus(nodes_valid)
    _OutputPerNodeOSStatus(nodes_bad)
111
    ToStdout("")
Iustin Pop's avatar
Iustin Pop committed
112

113
114
  return int(has_bad)

Iustin Pop's avatar
Iustin Pop committed
115
116

commands = {
117
  'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
Iustin Pop's avatar
Iustin Pop committed
118
           "Lists all valid OSes on the master"),
119
  'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
Iustin Pop's avatar
Iustin Pop committed
120
121
122
123
               "Diagnose all OSes"),
  }

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