Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-os @ 936f3c59

History | View | Annotate | Download (4.6 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 2f79bd34 Iustin Pop
# pylint: disable-msg=W0401,W0614
23 2f79bd34 Iustin Pop
# W0401: Wildcard import ganeti.cli
24 2f79bd34 Iustin Pop
# W0614: Unused import %s from wildcard import (since we need cli)
25 2f79bd34 Iustin Pop
26 a8083063 Iustin Pop
import sys
27 a8083063 Iustin Pop
28 a8083063 Iustin Pop
from ganeti.cli import *
29 a8083063 Iustin Pop
from ganeti import opcodes
30 a8083063 Iustin Pop
from ganeti import utils
31 1f9430d6 Iustin Pop
from ganeti import constants
32 216fe2f3 Guido Trotter
33 fbdb07b9 Guido Trotter
34 a8083063 Iustin Pop
def ListOS(opts, args):
35 6099bfcf Iustin Pop
  """List the valid OSes in the cluster.
36 6099bfcf Iustin Pop
37 6099bfcf Iustin Pop
  @param opts: the command line options selected by the user
38 6099bfcf Iustin Pop
  @type args: list
39 6099bfcf Iustin Pop
  @param args: should be an empty list
40 6099bfcf Iustin Pop
  @rtype: int
41 6099bfcf Iustin Pop
  @return: the desired exit code
42 a8083063 Iustin Pop
43 a8083063 Iustin Pop
  """
44 e3ac208c Guido Trotter
  op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants"],
45 e3ac208c Guido Trotter
                            names=[])
46 a8083063 Iustin Pop
  result = SubmitOpCode(op)
47 a8083063 Iustin Pop
48 a8083063 Iustin Pop
  if not result:
49 3a24c527 Iustin Pop
    ToStderr("Can't get the OS list")
50 a8083063 Iustin Pop
    return 1
51 a8083063 Iustin Pop
52 a8083063 Iustin Pop
  if not opts.no_headers:
53 606d909d Michael Hanselmann
    headers = {"name": "Name"}
54 606d909d Michael Hanselmann
  else:
55 606d909d Michael Hanselmann
    headers = None
56 a8083063 Iustin Pop
57 e3ac208c Guido Trotter
  os_names = []
58 e3ac208c Guido Trotter
  for (name, valid, variants) in result:
59 e3ac208c Guido Trotter
    if valid:
60 e3ac208c Guido Trotter
      os_names.extend([[n] for n in CalculateOSNames(name, variants)])
61 e3ac208c Guido Trotter
62 16be8703 Iustin Pop
  data = GenerateTable(separator=None, headers=headers, fields=["name"],
63 e3ac208c Guido Trotter
                       data=os_names, units=None)
64 16be8703 Iustin Pop
65 16be8703 Iustin Pop
  for line in data:
66 3a24c527 Iustin Pop
    ToStdout(line)
67 a8083063 Iustin Pop
68 a8083063 Iustin Pop
  return 0
69 a8083063 Iustin Pop
70 b07a9f05 Guido Trotter
71 255dcebd Iustin Pop
def _OsStatus(status, diagnose):
72 255dcebd Iustin Pop
  """Beautifier function for OS status.
73 255dcebd Iustin Pop
74 255dcebd Iustin Pop
  @type status: boolean
75 255dcebd Iustin Pop
  @param status: is the OS valid
76 255dcebd Iustin Pop
  @type diagnose: string
77 255dcebd Iustin Pop
  @param diagnose: the error message for invalid OSes
78 255dcebd Iustin Pop
  @rtype: string
79 255dcebd Iustin Pop
  @return: a formatted status
80 255dcebd Iustin Pop
81 255dcebd Iustin Pop
  """
82 255dcebd Iustin Pop
  if status:
83 255dcebd Iustin Pop
    return "valid"
84 255dcebd Iustin Pop
  else:
85 255dcebd Iustin Pop
    return "invalid - %s" % diagnose
86 255dcebd Iustin Pop
87 a8083063 Iustin Pop
def DiagnoseOS(opts, args):
88 a8083063 Iustin Pop
  """Analyse all OSes on this cluster.
89 a8083063 Iustin Pop
90 6099bfcf Iustin Pop
  @param opts: the command line options selected by the user
91 6099bfcf Iustin Pop
  @type args: list
92 6099bfcf Iustin Pop
  @param args: should be an empty list
93 6099bfcf Iustin Pop
  @rtype: int
94 6099bfcf Iustin Pop
  @return: the desired exit code
95 6099bfcf Iustin Pop
96 a8083063 Iustin Pop
  """
97 e16dfb5b Guido Trotter
  op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants",
98 e16dfb5b Guido Trotter
                                           "node_status"], names=[])
99 a8083063 Iustin Pop
  result = SubmitOpCode(op)
100 a8083063 Iustin Pop
101 a8083063 Iustin Pop
  if not result:
102 3a24c527 Iustin Pop
    ToStderr("Can't get the OS list")
103 a8083063 Iustin Pop
    return 1
104 a8083063 Iustin Pop
105 a2656173 Michael Hanselmann
  has_bad = False
106 a2656173 Michael Hanselmann
107 e16dfb5b Guido Trotter
  for os_name, os_valid, os_variants, node_data in result:
108 b07a9f05 Guido Trotter
    nodes_valid = {}
109 a8083063 Iustin Pop
    nodes_bad = {}
110 1f9430d6 Iustin Pop
    nodes_hidden = {}
111 1f9430d6 Iustin Pop
    for node_name, node_info in node_data.iteritems():
112 1f9430d6 Iustin Pop
      nodes_hidden[node_name] = []
113 1f9430d6 Iustin Pop
      if node_info: # at least one entry in the per-node list
114 ba00557a Guido Trotter
        (first_os_path, first_os_status, first_os_msg,
115 ba00557a Guido Trotter
         first_os_variants) = node_info.pop(0)
116 ba00557a Guido Trotter
        if not first_os_variants:
117 ba00557a Guido Trotter
          first_os_variants = []
118 ba00557a Guido Trotter
        first_os_msg = ("%s (path: %s) [variants: %s]" %
119 ba00557a Guido Trotter
                        (_OsStatus(first_os_status, first_os_msg),
120 ba00557a Guido Trotter
                         first_os_path, ', '.join(first_os_variants)))
121 255dcebd Iustin Pop
        if first_os_status:
122 48f85f75 Guido Trotter
          nodes_valid[node_name] = first_os_msg
123 b07a9f05 Guido Trotter
        else:
124 48f85f75 Guido Trotter
          nodes_bad[node_name] = first_os_msg
125 255dcebd Iustin Pop
        for hpath, hstatus, hmsg in node_info:
126 1f9430d6 Iustin Pop
          nodes_hidden[node_name].append("    [hidden] path: %s, status: %s" %
127 255dcebd Iustin Pop
                                         (hpath, _OsStatus(hstatus, hmsg)))
128 a8083063 Iustin Pop
      else:
129 b07a9f05 Guido Trotter
        nodes_bad[node_name] = "OS not found"
130 a8083063 Iustin Pop
131 a8083063 Iustin Pop
    if nodes_valid and not nodes_bad:
132 a8083063 Iustin Pop
      status = "valid"
133 a8083063 Iustin Pop
    elif not nodes_valid and nodes_bad:
134 a8083063 Iustin Pop
      status = "invalid"
135 a2656173 Michael Hanselmann
      has_bad = True
136 a8083063 Iustin Pop
    else:
137 a8083063 Iustin Pop
      status = "partial valid"
138 a2656173 Michael Hanselmann
      has_bad = True
139 694e2444 Guido Trotter
140 48f85f75 Guido Trotter
    def _OutputPerNodeOSStatus(msg_map):
141 48f85f75 Guido Trotter
      map_k = utils.NiceSort(msg_map.keys())
142 b07a9f05 Guido Trotter
      for node_name in map_k:
143 3a24c527 Iustin Pop
        ToStdout("  Node: %s, status: %s", node_name, msg_map[node_name])
144 1f9430d6 Iustin Pop
        for msg in nodes_hidden[node_name]:
145 3a24c527 Iustin Pop
          ToStdout(msg)
146 b07a9f05 Guido Trotter
147 3a24c527 Iustin Pop
    ToStdout("OS: %s [global status: %s]", os_name, status)
148 e16dfb5b Guido Trotter
    if os_variants:
149 e16dfb5b Guido Trotter
      ToStdout("  Variants: [%s]" % ', '.join(os_variants))
150 b07a9f05 Guido Trotter
    _OutputPerNodeOSStatus(nodes_valid)
151 b07a9f05 Guido Trotter
    _OutputPerNodeOSStatus(nodes_bad)
152 3a24c527 Iustin Pop
    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 6ea815cf Iustin Pop
  'list': (
159 064c21f8 Iustin Pop
    ListOS, ARGS_NONE, [NOHDR_OPT], "", "Lists all valid OSes on the master"),
160 6ea815cf Iustin Pop
  'diagnose': (
161 064c21f8 Iustin Pop
    DiagnoseOS, ARGS_NONE, [], "", "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))