root / scripts / gnt-os @ fdad8c4d
History | View | Annotate | Download (5.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 | 7260cfbe | Iustin Pop | """OS scripts related commands""" |
22 | a8083063 | Iustin Pop | |
23 | 2d54e29c | Iustin Pop | # pylint: disable-msg=W0401,W0613,W0614,C0103 |
24 | 2f79bd34 | Iustin Pop | # W0401: Wildcard import ganeti.cli |
25 | 2d54e29c | Iustin Pop | # W0613: Unused argument, since all functions follow the same API |
26 | 2f79bd34 | Iustin Pop | # W0614: Unused import %s from wildcard import (since we need cli) |
27 | 7260cfbe | Iustin Pop | # C0103: Invalid name gnt-os |
28 | 2f79bd34 | Iustin Pop | |
29 | a8083063 | Iustin Pop | import sys |
30 | a8083063 | Iustin Pop | |
31 | a8083063 | Iustin Pop | from ganeti.cli import * |
32 | a8083063 | Iustin Pop | from ganeti import opcodes |
33 | a8083063 | Iustin Pop | from ganeti import utils |
34 | 216fe2f3 | Guido Trotter | |
35 | fbdb07b9 | Guido Trotter | |
36 | a8083063 | Iustin Pop | def ListOS(opts, args): |
37 | 6099bfcf | Iustin Pop | """List the valid OSes in the cluster. |
38 | 6099bfcf | Iustin Pop | |
39 | 6099bfcf | Iustin Pop | @param opts: the command line options selected by the user |
40 | 6099bfcf | Iustin Pop | @type args: list |
41 | 6099bfcf | Iustin Pop | @param args: should be an empty list |
42 | 6099bfcf | Iustin Pop | @rtype: int |
43 | 6099bfcf | Iustin Pop | @return: the desired exit code |
44 | a8083063 | Iustin Pop | |
45 | a8083063 | Iustin Pop | """ |
46 | e3ac208c | Guido Trotter | op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants"], |
47 | e3ac208c | Guido Trotter | names=[]) |
48 | 400ca2f7 | Iustin Pop | result = SubmitOpCode(op, opts=opts) |
49 | a8083063 | Iustin Pop | |
50 | a8083063 | Iustin Pop | if not result: |
51 | 3a24c527 | Iustin Pop | ToStderr("Can't get the OS list") |
52 | a8083063 | Iustin Pop | return 1 |
53 | a8083063 | Iustin Pop | |
54 | a8083063 | Iustin Pop | if not opts.no_headers: |
55 | 606d909d | Michael Hanselmann | headers = {"name": "Name"} |
56 | 606d909d | Michael Hanselmann | else: |
57 | 606d909d | Michael Hanselmann | headers = None |
58 | a8083063 | Iustin Pop | |
59 | e3ac208c | Guido Trotter | os_names = [] |
60 | e3ac208c | Guido Trotter | for (name, valid, variants) in result: |
61 | e3ac208c | Guido Trotter | if valid: |
62 | e3ac208c | Guido Trotter | os_names.extend([[n] for n in CalculateOSNames(name, variants)]) |
63 | e3ac208c | Guido Trotter | |
64 | 16be8703 | Iustin Pop | data = GenerateTable(separator=None, headers=headers, fields=["name"], |
65 | e3ac208c | Guido Trotter | data=os_names, units=None) |
66 | 16be8703 | Iustin Pop | |
67 | 16be8703 | Iustin Pop | for line in data: |
68 | 3a24c527 | Iustin Pop | ToStdout(line) |
69 | a8083063 | Iustin Pop | |
70 | a8083063 | Iustin Pop | return 0 |
71 | a8083063 | Iustin Pop | |
72 | b07a9f05 | Guido Trotter | |
73 | 255dcebd | Iustin Pop | def _OsStatus(status, diagnose): |
74 | 255dcebd | Iustin Pop | """Beautifier function for OS status. |
75 | 255dcebd | Iustin Pop | |
76 | 255dcebd | Iustin Pop | @type status: boolean |
77 | 255dcebd | Iustin Pop | @param status: is the OS valid |
78 | 255dcebd | Iustin Pop | @type diagnose: string |
79 | 255dcebd | Iustin Pop | @param diagnose: the error message for invalid OSes |
80 | 255dcebd | Iustin Pop | @rtype: string |
81 | 255dcebd | Iustin Pop | @return: a formatted status |
82 | 255dcebd | Iustin Pop | |
83 | 255dcebd | Iustin Pop | """ |
84 | 255dcebd | Iustin Pop | if status: |
85 | 255dcebd | Iustin Pop | return "valid" |
86 | 255dcebd | Iustin Pop | else: |
87 | 255dcebd | Iustin Pop | return "invalid - %s" % diagnose |
88 | 255dcebd | Iustin Pop | |
89 | a8083063 | Iustin Pop | def DiagnoseOS(opts, args): |
90 | a8083063 | Iustin Pop | """Analyse all OSes on this cluster. |
91 | a8083063 | Iustin Pop | |
92 | 6099bfcf | Iustin Pop | @param opts: the command line options selected by the user |
93 | 6099bfcf | Iustin Pop | @type args: list |
94 | 6099bfcf | Iustin Pop | @param args: should be an empty list |
95 | 6099bfcf | Iustin Pop | @rtype: int |
96 | 6099bfcf | Iustin Pop | @return: the desired exit code |
97 | 6099bfcf | Iustin Pop | |
98 | a8083063 | Iustin Pop | """ |
99 | e16dfb5b | Guido Trotter | op = opcodes.OpDiagnoseOS(output_fields=["name", "valid", "variants", |
100 | e16dfb5b | Guido Trotter | "node_status"], names=[]) |
101 | 400ca2f7 | Iustin Pop | result = SubmitOpCode(op, opts=opts) |
102 | a8083063 | Iustin Pop | |
103 | a8083063 | Iustin Pop | if not result: |
104 | 3a24c527 | Iustin Pop | ToStderr("Can't get the OS list") |
105 | a8083063 | Iustin Pop | return 1 |
106 | a8083063 | Iustin Pop | |
107 | a2656173 | Michael Hanselmann | has_bad = False |
108 | a2656173 | Michael Hanselmann | |
109 | f4ad2ef0 | Iustin Pop | for os_name, _, os_variants, node_data in result: |
110 | b07a9f05 | Guido Trotter | nodes_valid = {} |
111 | a8083063 | Iustin Pop | nodes_bad = {} |
112 | 1f9430d6 | Iustin Pop | nodes_hidden = {} |
113 | 1f9430d6 | Iustin Pop | for node_name, node_info in node_data.iteritems(): |
114 | 1f9430d6 | Iustin Pop | nodes_hidden[node_name] = [] |
115 | 1f9430d6 | Iustin Pop | if node_info: # at least one entry in the per-node list |
116 | ba00557a | Guido Trotter | (first_os_path, first_os_status, first_os_msg, |
117 | ba00557a | Guido Trotter | first_os_variants) = node_info.pop(0) |
118 | ba00557a | Guido Trotter | if not first_os_variants: |
119 | ba00557a | Guido Trotter | first_os_variants = [] |
120 | ba00557a | Guido Trotter | first_os_msg = ("%s (path: %s) [variants: %s]" % |
121 | ba00557a | Guido Trotter | (_OsStatus(first_os_status, first_os_msg), |
122 | 1f864b60 | Iustin Pop | first_os_path, utils.CommaJoin(first_os_variants))) |
123 | 255dcebd | Iustin Pop | if first_os_status: |
124 | 48f85f75 | Guido Trotter | nodes_valid[node_name] = first_os_msg |
125 | b07a9f05 | Guido Trotter | else: |
126 | 48f85f75 | Guido Trotter | nodes_bad[node_name] = first_os_msg |
127 | 255dcebd | Iustin Pop | for hpath, hstatus, hmsg in node_info: |
128 | 1f9430d6 | Iustin Pop | nodes_hidden[node_name].append(" [hidden] path: %s, status: %s" % |
129 | 255dcebd | Iustin Pop | (hpath, _OsStatus(hstatus, hmsg))) |
130 | a8083063 | Iustin Pop | else: |
131 | b07a9f05 | Guido Trotter | nodes_bad[node_name] = "OS not found" |
132 | a8083063 | Iustin Pop | |
133 | a8083063 | Iustin Pop | if nodes_valid and not nodes_bad: |
134 | a8083063 | Iustin Pop | status = "valid" |
135 | a8083063 | Iustin Pop | elif not nodes_valid and nodes_bad: |
136 | a8083063 | Iustin Pop | status = "invalid" |
137 | a2656173 | Michael Hanselmann | has_bad = True |
138 | a8083063 | Iustin Pop | else: |
139 | a8083063 | Iustin Pop | status = "partial valid" |
140 | a2656173 | Michael Hanselmann | has_bad = True |
141 | 694e2444 | Guido Trotter | |
142 | 48f85f75 | Guido Trotter | def _OutputPerNodeOSStatus(msg_map): |
143 | 48f85f75 | Guido Trotter | map_k = utils.NiceSort(msg_map.keys()) |
144 | b07a9f05 | Guido Trotter | for node_name in map_k: |
145 | 3a24c527 | Iustin Pop | ToStdout(" Node: %s, status: %s", node_name, msg_map[node_name]) |
146 | 1f9430d6 | Iustin Pop | for msg in nodes_hidden[node_name]: |
147 | 3a24c527 | Iustin Pop | ToStdout(msg) |
148 | b07a9f05 | Guido Trotter | |
149 | 3a24c527 | Iustin Pop | ToStdout("OS: %s [global status: %s]", os_name, status) |
150 | e16dfb5b | Guido Trotter | if os_variants: |
151 | 1f864b60 | Iustin Pop | ToStdout(" Variants: [%s]" % utils.CommaJoin(os_variants)) |
152 | b07a9f05 | Guido Trotter | _OutputPerNodeOSStatus(nodes_valid) |
153 | b07a9f05 | Guido Trotter | _OutputPerNodeOSStatus(nodes_bad) |
154 | 3a24c527 | Iustin Pop | ToStdout("") |
155 | a8083063 | Iustin Pop | |
156 | a2656173 | Michael Hanselmann | return int(has_bad) |
157 | a2656173 | Michael Hanselmann | |
158 | a8083063 | Iustin Pop | |
159 | 429ae766 | René Nussbaumer | def ModifyOS(opts, args): |
160 | 429ae766 | René Nussbaumer | """Modify OS parameters for one OS. |
161 | 429ae766 | René Nussbaumer | |
162 | 429ae766 | René Nussbaumer | @param opts: the command line options selected by the user |
163 | 429ae766 | René Nussbaumer | @type args: list |
164 | 429ae766 | René Nussbaumer | @param args: should be a list with one entry |
165 | 429ae766 | René Nussbaumer | @rtype: int |
166 | 429ae766 | René Nussbaumer | @return: the desired exit code |
167 | 429ae766 | René Nussbaumer | |
168 | 429ae766 | René Nussbaumer | """ |
169 | 429ae766 | René Nussbaumer | os = args[0] |
170 | 429ae766 | René Nussbaumer | |
171 | 429ae766 | René Nussbaumer | op = opcodes.OpSetClusterParams(vg_name=None, |
172 | 429ae766 | René Nussbaumer | enabled_hypervisors=None, |
173 | 429ae766 | René Nussbaumer | hvparams=None, |
174 | 429ae766 | René Nussbaumer | beparams=None, |
175 | 429ae766 | René Nussbaumer | nicparams=None, |
176 | 429ae766 | René Nussbaumer | candidate_pool_size=None, |
177 | 429ae766 | René Nussbaumer | os_hvp={ |
178 | 429ae766 | René Nussbaumer | os: dict(opts.hvparams) |
179 | 429ae766 | René Nussbaumer | }) |
180 | 429ae766 | René Nussbaumer | SubmitOpCode(op) |
181 | 429ae766 | René Nussbaumer | |
182 | 429ae766 | René Nussbaumer | return 0 |
183 | 429ae766 | René Nussbaumer | |
184 | 429ae766 | René Nussbaumer | |
185 | a8083063 | Iustin Pop | commands = { |
186 | 6ea815cf | Iustin Pop | 'list': ( |
187 | 064c21f8 | Iustin Pop | ListOS, ARGS_NONE, [NOHDR_OPT], "", "Lists all valid OSes on the master"), |
188 | 6ea815cf | Iustin Pop | 'diagnose': ( |
189 | 064c21f8 | Iustin Pop | DiagnoseOS, ARGS_NONE, [], "", "Diagnose all OSes"), |
190 | 429ae766 | René Nussbaumer | 'modify': ( |
191 | 429ae766 | René Nussbaumer | ModifyOS, ARGS_ONE_OS, [HVLIST_OPT], "", "Modify os parameters"), |
192 | a8083063 | Iustin Pop | } |
193 | a8083063 | Iustin Pop | |
194 | a8083063 | Iustin Pop | if __name__ == '__main__': |
195 | 3ecf6786 | Iustin Pop | sys.exit(GenericMain(commands)) |