gnt-os 3.55 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
#!/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.


22
23
24
25
# pylint: disable-msg=W0401,W0614
# W0401: Wildcard import ganeti.cli
# W0614: Unused import %s from wildcard import (since we need cli)

Iustin Pop's avatar
Iustin Pop committed
26
27
28
29
30
31
import sys
from optparse import make_option

from ganeti.cli import *
from ganeti import opcodes
from ganeti import utils
32
from ganeti import constants
Guido Trotter's avatar
Guido Trotter committed
33

34

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

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

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

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

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

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

  return 0

Guido Trotter's avatar
Guido Trotter committed
59

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

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

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

72
73
  has_bad = False

74
  for os_name, os_valid, node_data in result:
Guido Trotter's avatar
Guido Trotter committed
75
    nodes_valid = {}
Iustin Pop's avatar
Iustin Pop committed
76
    nodes_bad = {}
77
78
79
80
81
    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)
82
        first_os_msg = ("%s (path: %s)" %
83
84
                        (first_os_status, first_os_path))
        if first_os_status == constants.OS_VALID_STATUS:
85
          nodes_valid[node_name] = first_os_msg
Guido Trotter's avatar
Guido Trotter committed
86
        else:
87
          nodes_bad[node_name] = first_os_msg
88
89
90
        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
91
      else:
Guido Trotter's avatar
Guido Trotter committed
92
        nodes_bad[node_name] = "OS not found"
Iustin Pop's avatar
Iustin Pop committed
93
94
95
96
97

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

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

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

115
116
  return int(has_bad)

Iustin Pop's avatar
Iustin Pop committed
117
118

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

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