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