Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-os @ d0834de3

History | View | Annotate | Download (3.8 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 a8083063 Iustin Pop
def ListOS(opts, args):
33 a8083063 Iustin Pop
  """List the OSes existing on this node.
34 a8083063 Iustin Pop
35 a8083063 Iustin Pop
  """
36 a8083063 Iustin Pop
  op = opcodes.OpDiagnoseOS()
37 a8083063 Iustin Pop
  result = SubmitOpCode(op)
38 a8083063 Iustin Pop
39 a8083063 Iustin Pop
  if not result:
40 a8083063 Iustin Pop
    logger.ToStdout("Can't get the OS list")
41 a8083063 Iustin Pop
    return 1
42 a8083063 Iustin Pop
43 a8083063 Iustin Pop
  # filter non-valid OS-es
44 a8083063 Iustin Pop
  oses = {}
45 a8083063 Iustin Pop
  for node_name in result:
46 a8083063 Iustin Pop
    oses[node_name] = [obj for obj in result[node_name]
47 a8083063 Iustin Pop
                       if isinstance(obj, objects.OS)]
48 a8083063 Iustin Pop
49 a8083063 Iustin Pop
  fnode = oses.keys()[0]
50 a8083063 Iustin Pop
  os_set = set([os_inst.name for os_inst in oses[fnode]])
51 a8083063 Iustin Pop
  del oses[fnode]
52 a8083063 Iustin Pop
  for node in oses:
53 a8083063 Iustin Pop
    os_set &= set([os_inst.name for os_inst in oses[node]])
54 a8083063 Iustin Pop
55 a8083063 Iustin Pop
  format = "%s"
56 a8083063 Iustin Pop
57 a8083063 Iustin Pop
  if not opts.no_headers:
58 a8083063 Iustin Pop
    logger.ToStdout(format % 'Name')
59 a8083063 Iustin Pop
60 a8083063 Iustin Pop
  for os_name in os_set:
61 a8083063 Iustin Pop
    logger.ToStdout(format % os_name)
62 a8083063 Iustin Pop
63 a8083063 Iustin Pop
  return 0
64 a8083063 Iustin Pop
65 a8083063 Iustin Pop
def DiagnoseOS(opts, args):
66 a8083063 Iustin Pop
  """Analyse all OSes on this cluster.
67 a8083063 Iustin Pop
68 a8083063 Iustin Pop
  """
69 a8083063 Iustin Pop
  op = opcodes.OpDiagnoseOS()
70 a8083063 Iustin Pop
  result = SubmitOpCode(op)
71 a8083063 Iustin Pop
72 a8083063 Iustin Pop
  if not result:
73 a8083063 Iustin Pop
    logger.ToStdout("Can't get the OS list")
74 a8083063 Iustin Pop
    return 1
75 a8083063 Iustin Pop
76 a8083063 Iustin Pop
  format = "%-*s %-*s %s"
77 a8083063 Iustin Pop
78 a8083063 Iustin Pop
  node_data = result
79 a8083063 Iustin Pop
  all_os = {}
80 a8083063 Iustin Pop
  for node_name in node_data:
81 a8083063 Iustin Pop
    nr = node_data[node_name]
82 a8083063 Iustin Pop
    if nr:
83 a8083063 Iustin Pop
      for obj in nr:
84 a8083063 Iustin Pop
        if isinstance(obj, objects.OS):
85 a8083063 Iustin Pop
          os_name = obj.name
86 a8083063 Iustin Pop
        else:
87 a8083063 Iustin Pop
          os_name = obj.args[0]
88 a8083063 Iustin Pop
        if os_name not in all_os:
89 a8083063 Iustin Pop
          all_os[os_name] = {}
90 a8083063 Iustin Pop
        all_os[os_name][node_name] = obj
91 a8083063 Iustin Pop
92 a8083063 Iustin Pop
  max_name = len('Name')
93 a8083063 Iustin Pop
  if all_os:
94 a8083063 Iustin Pop
    max_name = max(max_name, max([len(name) for name in all_os]))
95 a8083063 Iustin Pop
96 a8083063 Iustin Pop
  max_node = len('Status/Node')
97 a8083063 Iustin Pop
  max_node = max(max_node, max([len(name) for name in node_data]))
98 a8083063 Iustin Pop
99 a8083063 Iustin Pop
  logger.ToStdout(format % (max_name, 'Name', max_node, 'Status/Node',
100 a8083063 Iustin Pop
                            'Details'))
101 a8083063 Iustin Pop
102 a8083063 Iustin Pop
  for os_name in all_os:
103 a8083063 Iustin Pop
    nodes_valid = []
104 a8083063 Iustin Pop
    nodes_bad = {}
105 a8083063 Iustin Pop
    for node_name in node_data:
106 a8083063 Iustin Pop
      nos = all_os[os_name].get(node_name, None)
107 a8083063 Iustin Pop
      if isinstance(nos, objects.OS):
108 a8083063 Iustin Pop
        nodes_valid.append(node_name)
109 a8083063 Iustin Pop
      elif isinstance(nos, errors.InvalidOS):
110 a8083063 Iustin Pop
        nodes_bad[node_name] = nos.args[1]
111 a8083063 Iustin Pop
      else:
112 a8083063 Iustin Pop
        nodes_bad[node_name] = "os dir not found"
113 a8083063 Iustin Pop
114 a8083063 Iustin Pop
    if nodes_valid and not nodes_bad:
115 a8083063 Iustin Pop
      status = "valid"
116 a8083063 Iustin Pop
    elif not nodes_valid and nodes_bad:
117 a8083063 Iustin Pop
      status = "invalid"
118 a8083063 Iustin Pop
    else:
119 a8083063 Iustin Pop
      status = "partial valid"
120 a8083063 Iustin Pop
    logger.ToStdout(format % (max_name, os_name, max_node, status, ""))
121 a8083063 Iustin Pop
    nodes_valid = utils.NiceSort(nodes_valid)
122 a8083063 Iustin Pop
    for node_name in nodes_valid:
123 a8083063 Iustin Pop
      logger.ToStdout(format % (max_name, "", max_node, node_name, "valid"))
124 a8083063 Iustin Pop
    nbk = utils.NiceSort(nodes_bad.keys())
125 a8083063 Iustin Pop
    for node_name in nbk:
126 a8083063 Iustin Pop
      logger.ToStdout(format % (max_name, "", max_node,
127 a8083063 Iustin Pop
                                node_name, nodes_bad[node_name]))
128 a8083063 Iustin Pop
129 a8083063 Iustin Pop
130 a8083063 Iustin Pop
commands = {
131 a8083063 Iustin Pop
  'list': (ListOS, ARGS_NONE, [DEBUG_OPT, NOHDR_OPT], "",
132 a8083063 Iustin Pop
           "Lists all valid OSes on the master"),
133 a8083063 Iustin Pop
  'diagnose': (DiagnoseOS, ARGS_NONE, [DEBUG_OPT], "",
134 a8083063 Iustin Pop
               "Diagnose all OSes"),
135 a8083063 Iustin Pop
  }
136 a8083063 Iustin Pop
137 a8083063 Iustin Pop
if __name__ == '__main__':
138 a8083063 Iustin Pop
  retcode = GenericMain(commands)
139 a8083063 Iustin Pop
  sys.exit(retcode)