root / scripts / gnt-instance @ 4300c4b6
History | View | Annotate | Download (41.3 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 | import os |
24 | 312ac745 | Iustin Pop | import itertools |
25 | a8083063 | Iustin Pop | from optparse import make_option |
26 | a8083063 | Iustin Pop | from cStringIO import StringIO |
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 logger |
31 | a8083063 | Iustin Pop | from ganeti import constants |
32 | a8083063 | Iustin Pop | from ganeti import utils |
33 | 312ac745 | Iustin Pop | from ganeti import errors |
34 | 312ac745 | Iustin Pop | |
35 | 312ac745 | Iustin Pop | |
36 | 312ac745 | Iustin Pop | _SHUTDOWN_CLUSTER = "cluster" |
37 | 312ac745 | Iustin Pop | _SHUTDOWN_NODES_BOTH = "nodes" |
38 | 312ac745 | Iustin Pop | _SHUTDOWN_NODES_PRI = "nodes-pri" |
39 | 312ac745 | Iustin Pop | _SHUTDOWN_NODES_SEC = "nodes-sec" |
40 | 312ac745 | Iustin Pop | _SHUTDOWN_INSTANCES = "instances" |
41 | 312ac745 | Iustin Pop | |
42 | 7c0d6283 | Michael Hanselmann | |
43 | 31a853d2 | Iustin Pop | _VALUE_TRUE = "true" |
44 | 31a853d2 | Iustin Pop | |
45 | 48c4dfa8 | Iustin Pop | _LIST_DEF_FIELDS = [ |
46 | 48c4dfa8 | Iustin Pop | "name", "os", "pnode", "status", "oper_ram", |
47 | 48c4dfa8 | Iustin Pop | ] |
48 | 48c4dfa8 | Iustin Pop | |
49 | bdb7d4e8 | Michael Hanselmann | |
50 | 312ac745 | Iustin Pop | def _ExpandMultiNames(mode, names): |
51 | 312ac745 | Iustin Pop | """Expand the given names using the passed mode. |
52 | 312ac745 | Iustin Pop | |
53 | 312ac745 | Iustin Pop | Args: |
54 | 312ac745 | Iustin Pop | - mode, which can be one of _SHUTDOWN_CLUSTER, _SHUTDOWN_NODES_BOTH, |
55 | 312ac745 | Iustin Pop | _SHUTDOWN_NODES_PRI, _SHUTDOWN_NODES_SEC or _SHUTDOWN_INSTANCES |
56 | 312ac745 | Iustin Pop | - names, which is a list of names; for cluster, it must be empty, |
57 | 312ac745 | Iustin Pop | and for node and instance it must be a list of valid item |
58 | 312ac745 | Iustin Pop | names (short names are valid as usual, e.g. node1 instead of |
59 | 312ac745 | Iustin Pop | node1.example.com) |
60 | 312ac745 | Iustin Pop | |
61 | 312ac745 | Iustin Pop | For _SHUTDOWN_CLUSTER, all instances will be returned. For |
62 | 312ac745 | Iustin Pop | _SHUTDOWN_NODES_PRI/SEC, all instances having those nodes as |
63 | 312ac745 | Iustin Pop | primary/secondary will be shutdown. For _SHUTDOWN_NODES_BOTH, all |
64 | 312ac745 | Iustin Pop | instances having those nodes as either primary or secondary will be |
65 | 312ac745 | Iustin Pop | returned. For _SHUTDOWN_INSTANCES, the given instances will be |
66 | 312ac745 | Iustin Pop | returned. |
67 | 312ac745 | Iustin Pop | |
68 | 312ac745 | Iustin Pop | """ |
69 | 312ac745 | Iustin Pop | if mode == _SHUTDOWN_CLUSTER: |
70 | 312ac745 | Iustin Pop | if names: |
71 | 312ac745 | Iustin Pop | raise errors.OpPrereqError("Cluster filter mode takes no arguments") |
72 | 312ac745 | Iustin Pop | op = opcodes.OpQueryInstances(output_fields=["name"], names=[]) |
73 | 312ac745 | Iustin Pop | idata = SubmitOpCode(op) |
74 | 312ac745 | Iustin Pop | inames = [row[0] for row in idata] |
75 | 312ac745 | Iustin Pop | |
76 | 312ac745 | Iustin Pop | elif mode in (_SHUTDOWN_NODES_BOTH, |
77 | 312ac745 | Iustin Pop | _SHUTDOWN_NODES_PRI, |
78 | 312ac745 | Iustin Pop | _SHUTDOWN_NODES_SEC): |
79 | 312ac745 | Iustin Pop | if not names: |
80 | 312ac745 | Iustin Pop | raise errors.OpPrereqError("No node names passed") |
81 | 312ac745 | Iustin Pop | op = opcodes.OpQueryNodes(output_fields=["name", "pinst_list", |
82 | 312ac745 | Iustin Pop | "sinst_list"], names=names) |
83 | 312ac745 | Iustin Pop | ndata = SubmitOpCode(op) |
84 | 312ac745 | Iustin Pop | ipri = [row[1] for row in ndata] |
85 | 312ac745 | Iustin Pop | pri_names = list(itertools.chain(*ipri)) |
86 | 312ac745 | Iustin Pop | isec = [row[2] for row in ndata] |
87 | 312ac745 | Iustin Pop | sec_names = list(itertools.chain(*isec)) |
88 | 312ac745 | Iustin Pop | if mode == _SHUTDOWN_NODES_BOTH: |
89 | 312ac745 | Iustin Pop | inames = pri_names + sec_names |
90 | 312ac745 | Iustin Pop | elif mode == _SHUTDOWN_NODES_PRI: |
91 | 312ac745 | Iustin Pop | inames = pri_names |
92 | 312ac745 | Iustin Pop | elif mode == _SHUTDOWN_NODES_SEC: |
93 | 312ac745 | Iustin Pop | inames = sec_names |
94 | 312ac745 | Iustin Pop | else: |
95 | 312ac745 | Iustin Pop | raise errors.ProgrammerError("Unhandled shutdown type") |
96 | 312ac745 | Iustin Pop | |
97 | 312ac745 | Iustin Pop | elif mode == _SHUTDOWN_INSTANCES: |
98 | 312ac745 | Iustin Pop | if not names: |
99 | 312ac745 | Iustin Pop | raise errors.OpPrereqError("No instance names passed") |
100 | 312ac745 | Iustin Pop | op = opcodes.OpQueryInstances(output_fields=["name"], names=names) |
101 | 312ac745 | Iustin Pop | idata = SubmitOpCode(op) |
102 | 312ac745 | Iustin Pop | inames = [row[0] for row in idata] |
103 | 312ac745 | Iustin Pop | |
104 | 312ac745 | Iustin Pop | else: |
105 | 312ac745 | Iustin Pop | raise errors.OpPrereqError("Unknown mode '%s'" % mode) |
106 | 312ac745 | Iustin Pop | |
107 | 312ac745 | Iustin Pop | return inames |
108 | a8083063 | Iustin Pop | |
109 | a8083063 | Iustin Pop | |
110 | 804a1e8e | Iustin Pop | def _ConfirmOperation(inames, text): |
111 | 804a1e8e | Iustin Pop | """Ask the user to confirm an operation on a list of instances. |
112 | 804a1e8e | Iustin Pop | |
113 | 804a1e8e | Iustin Pop | This function is used to request confirmation for doing an operation |
114 | 804a1e8e | Iustin Pop | on a given list of instances. |
115 | 804a1e8e | Iustin Pop | |
116 | 804a1e8e | Iustin Pop | The inames argument is what the selection algorithm computed, and |
117 | 804a1e8e | Iustin Pop | the text argument is the operation we should tell the user to |
118 | 804a1e8e | Iustin Pop | confirm (e.g. 'shutdown' or 'startup'). |
119 | 804a1e8e | Iustin Pop | |
120 | 804a1e8e | Iustin Pop | Returns: boolean depending on user's confirmation. |
121 | 804a1e8e | Iustin Pop | |
122 | 804a1e8e | Iustin Pop | """ |
123 | 804a1e8e | Iustin Pop | count = len(inames) |
124 | 804a1e8e | Iustin Pop | msg = ("The %s will operate on %d instances.\n" |
125 | 804a1e8e | Iustin Pop | "Do you want to continue?" % (text, count)) |
126 | 804a1e8e | Iustin Pop | affected = ("\nAffected instances:\n" + |
127 | 804a1e8e | Iustin Pop | "\n".join([" %s" % name for name in inames])) |
128 | 804a1e8e | Iustin Pop | |
129 | 804a1e8e | Iustin Pop | choices = [('y', True, 'Yes, execute the %s' % text), |
130 | 804a1e8e | Iustin Pop | ('n', False, 'No, abort the %s' % text)] |
131 | 804a1e8e | Iustin Pop | |
132 | 804a1e8e | Iustin Pop | if count > 20: |
133 | 804a1e8e | Iustin Pop | choices.insert(1, ('v', 'v', 'View the list of affected instances')) |
134 | 804a1e8e | Iustin Pop | ask = msg |
135 | 804a1e8e | Iustin Pop | else: |
136 | 804a1e8e | Iustin Pop | ask = msg + affected |
137 | 804a1e8e | Iustin Pop | |
138 | 804a1e8e | Iustin Pop | choice = AskUser(ask, choices) |
139 | 804a1e8e | Iustin Pop | if choice == 'v': |
140 | 804a1e8e | Iustin Pop | choices.pop(1) |
141 | 5e66b7e6 | Iustin Pop | choice = AskUser(msg + affected, choices) |
142 | 804a1e8e | Iustin Pop | return choice |
143 | 804a1e8e | Iustin Pop | |
144 | 804a1e8e | Iustin Pop | |
145 | 973d7867 | Iustin Pop | def _TransformPath(user_input): |
146 | 973d7867 | Iustin Pop | """Transform a user path into a canonical value. |
147 | 973d7867 | Iustin Pop | |
148 | 973d7867 | Iustin Pop | This function transforms the a path passed as textual information |
149 | 973d7867 | Iustin Pop | into the constants that the LU code expects. |
150 | 973d7867 | Iustin Pop | |
151 | 973d7867 | Iustin Pop | """ |
152 | 973d7867 | Iustin Pop | if user_input: |
153 | 973d7867 | Iustin Pop | if user_input.lower() == "default": |
154 | 973d7867 | Iustin Pop | result_path = constants.VALUE_DEFAULT |
155 | 973d7867 | Iustin Pop | elif user_input.lower() == "none": |
156 | 973d7867 | Iustin Pop | result_path = constants.VALUE_NONE |
157 | 973d7867 | Iustin Pop | else: |
158 | 973d7867 | Iustin Pop | if not os.path.isabs(user_input): |
159 | 973d7867 | Iustin Pop | raise errors.OpPrereqError("Path '%s' is not an absolute filename" % |
160 | 973d7867 | Iustin Pop | user_input) |
161 | 973d7867 | Iustin Pop | result_path = user_input |
162 | 973d7867 | Iustin Pop | else: |
163 | 973d7867 | Iustin Pop | result_path = constants.VALUE_DEFAULT |
164 | 973d7867 | Iustin Pop | |
165 | 973d7867 | Iustin Pop | return result_path |
166 | 973d7867 | Iustin Pop | |
167 | 973d7867 | Iustin Pop | |
168 | a8083063 | Iustin Pop | def ListInstances(opts, args): |
169 | f5abe9bd | Oleksiy Mishchenko | """List instances and their properties. |
170 | a8083063 | Iustin Pop | |
171 | a8083063 | Iustin Pop | """ |
172 | a8083063 | Iustin Pop | if opts.output is None: |
173 | 48c4dfa8 | Iustin Pop | selected_fields = _LIST_DEF_FIELDS |
174 | 48c4dfa8 | Iustin Pop | elif opts.output.startswith("+"): |
175 | 48c4dfa8 | Iustin Pop | selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",") |
176 | a8083063 | Iustin Pop | else: |
177 | a8083063 | Iustin Pop | selected_fields = opts.output.split(",") |
178 | a8083063 | Iustin Pop | |
179 | 1f05af2b | Michael Hanselmann | output = GetClient().QueryInstances([], selected_fields) |
180 | a8083063 | Iustin Pop | |
181 | a8083063 | Iustin Pop | if not opts.no_headers: |
182 | d8052456 | Iustin Pop | headers = { |
183 | d8052456 | Iustin Pop | "name": "Instance", "os": "OS", "pnode": "Primary_node", |
184 | d8052456 | Iustin Pop | "snodes": "Secondary_Nodes", "admin_state": "Autostart", |
185 | d8052456 | Iustin Pop | "oper_state": "Running", "admin_ram": "Configured_memory", |
186 | d8052456 | Iustin Pop | "oper_ram": "Memory", "disk_template": "Disk_template", |
187 | d8052456 | Iustin Pop | "ip": "IP Address", "mac": "MAC Address", |
188 | d8052456 | Iustin Pop | "bridge": "Bridge", "vcpus": "VCPUs", |
189 | d8052456 | Iustin Pop | "sda_size": "Disk/0", "sdb_size": "Disk/1", |
190 | 130a6a6f | Iustin Pop | "status": "Status", "tags": "Tags", |
191 | d8052456 | Iustin Pop | } |
192 | 137161c9 | Michael Hanselmann | else: |
193 | 137161c9 | Michael Hanselmann | headers = None |
194 | 137161c9 | Michael Hanselmann | |
195 | 137161c9 | Michael Hanselmann | if opts.human_readable: |
196 | 644eeef9 | Iustin Pop | unitfields = ["admin_ram", "oper_ram", "sda_size", "sdb_size"] |
197 | 137161c9 | Michael Hanselmann | else: |
198 | 137161c9 | Michael Hanselmann | unitfields = None |
199 | 137161c9 | Michael Hanselmann | |
200 | d6d415e8 | Iustin Pop | numfields = ["admin_ram", "oper_ram", "sda_size", "sdb_size", "vcpus"] |
201 | 137161c9 | Michael Hanselmann | |
202 | 130a6a6f | Iustin Pop | list_type_fields = ("tags",) |
203 | 8a23d2d3 | Iustin Pop | # change raw values to nicer strings |
204 | 8a23d2d3 | Iustin Pop | for row in output: |
205 | 8a23d2d3 | Iustin Pop | for idx, field in enumerate(selected_fields): |
206 | 8a23d2d3 | Iustin Pop | val = row[idx] |
207 | 8a23d2d3 | Iustin Pop | if field == "snodes": |
208 | 8a23d2d3 | Iustin Pop | val = ",".join(val) or "-" |
209 | 8a23d2d3 | Iustin Pop | elif field == "admin_state": |
210 | 8a23d2d3 | Iustin Pop | if val: |
211 | 8a23d2d3 | Iustin Pop | val = "yes" |
212 | 8a23d2d3 | Iustin Pop | else: |
213 | 8a23d2d3 | Iustin Pop | val = "no" |
214 | 8a23d2d3 | Iustin Pop | elif field == "oper_state": |
215 | 8a23d2d3 | Iustin Pop | if val is None: |
216 | 8a23d2d3 | Iustin Pop | val = "(node down)" |
217 | 8a23d2d3 | Iustin Pop | elif val: # True |
218 | 8a23d2d3 | Iustin Pop | val = "running" |
219 | 8a23d2d3 | Iustin Pop | else: |
220 | 8a23d2d3 | Iustin Pop | val = "stopped" |
221 | 8a23d2d3 | Iustin Pop | elif field == "oper_ram": |
222 | 8a23d2d3 | Iustin Pop | if val is None: |
223 | 8a23d2d3 | Iustin Pop | val = "(node down)" |
224 | 8a23d2d3 | Iustin Pop | elif field == "sda_size" or field == "sdb_size": |
225 | 8a23d2d3 | Iustin Pop | if val is None: |
226 | 8a23d2d3 | Iustin Pop | val = "N/A" |
227 | 130a6a6f | Iustin Pop | elif field in list_type_fields: |
228 | 130a6a6f | Iustin Pop | val = ",".join(val) |
229 | 8a23d2d3 | Iustin Pop | row[idx] = str(val) |
230 | 8a23d2d3 | Iustin Pop | |
231 | 16be8703 | Iustin Pop | data = GenerateTable(separator=opts.separator, headers=headers, |
232 | 16be8703 | Iustin Pop | fields=selected_fields, unitfields=unitfields, |
233 | 16be8703 | Iustin Pop | numfields=numfields, data=output) |
234 | 16be8703 | Iustin Pop | |
235 | 16be8703 | Iustin Pop | for line in data: |
236 | 16be8703 | Iustin Pop | logger.ToStdout(line) |
237 | a8083063 | Iustin Pop | |
238 | a8083063 | Iustin Pop | return 0 |
239 | a8083063 | Iustin Pop | |
240 | a8083063 | Iustin Pop | |
241 | a8083063 | Iustin Pop | def AddInstance(opts, args): |
242 | a8083063 | Iustin Pop | """Add an instance to the cluster. |
243 | a8083063 | Iustin Pop | |
244 | a8083063 | Iustin Pop | Args: |
245 | a8083063 | Iustin Pop | opts - class with options as members |
246 | a8083063 | Iustin Pop | args - list with a single element, the instance name |
247 | a8083063 | Iustin Pop | Opts used: |
248 | a8083063 | Iustin Pop | mem - amount of memory to allocate to instance (MiB) |
249 | a8083063 | Iustin Pop | size - amount of disk space to allocate to instance (MiB) |
250 | a8083063 | Iustin Pop | os - which OS to run on instance |
251 | a8083063 | Iustin Pop | node - node to run new instance on |
252 | a8083063 | Iustin Pop | |
253 | a8083063 | Iustin Pop | """ |
254 | a8083063 | Iustin Pop | instance = args[0] |
255 | a8083063 | Iustin Pop | |
256 | 60d49723 | Michael Hanselmann | (pnode, snode) = SplitNodeOption(opts.node) |
257 | 60d49723 | Michael Hanselmann | |
258 | 3b6d8c9b | Iustin Pop | kernel_path = _TransformPath(opts.kernel_path) |
259 | 3b6d8c9b | Iustin Pop | initrd_path = _TransformPath(opts.initrd_path) |
260 | 3b6d8c9b | Iustin Pop | |
261 | 31a853d2 | Iustin Pop | hvm_acpi = opts.hvm_acpi == _VALUE_TRUE |
262 | 31a853d2 | Iustin Pop | hvm_pae = opts.hvm_pae == _VALUE_TRUE |
263 | 31a853d2 | Iustin Pop | |
264 | 31a853d2 | Iustin Pop | if ((opts.hvm_cdrom_image_path is not None) and |
265 | 31a853d2 | Iustin Pop | (opts.hvm_cdrom_image_path.lower() == constants.VALUE_NONE)): |
266 | 31a853d2 | Iustin Pop | hvm_cdrom_image_path = None |
267 | 31a853d2 | Iustin Pop | else: |
268 | 31a853d2 | Iustin Pop | hvm_cdrom_image_path = opts.hvm_cdrom_image_path |
269 | 31a853d2 | Iustin Pop | |
270 | a8083063 | Iustin Pop | op = opcodes.OpCreateInstance(instance_name=instance, mem_size=opts.mem, |
271 | a8083063 | Iustin Pop | disk_size=opts.size, swap_size=opts.swap, |
272 | a8083063 | Iustin Pop | disk_template=opts.disk_template, |
273 | a8083063 | Iustin Pop | mode=constants.INSTANCE_CREATE, |
274 | 60d49723 | Michael Hanselmann | os_type=opts.os, pnode=pnode, |
275 | 60d49723 | Michael Hanselmann | snode=snode, vcpus=opts.vcpus, |
276 | bdd55f71 | Iustin Pop | ip=opts.ip, bridge=opts.bridge, |
277 | bdd55f71 | Iustin Pop | start=opts.start, ip_check=opts.ip_check, |
278 | 3b6d8c9b | Iustin Pop | wait_for_sync=opts.wait_for_sync, |
279 | 3b6d8c9b | Iustin Pop | mac=opts.mac, |
280 | 3b6d8c9b | Iustin Pop | kernel_path=kernel_path, |
281 | 25c5878d | Alexander Schreiber | initrd_path=initrd_path, |
282 | 31a853d2 | Iustin Pop | iallocator=opts.iallocator, |
283 | 45873083 | Manuel Franceschini | hvm_boot_order=opts.hvm_boot_order, |
284 | 35a0d128 | Iustin Pop | file_storage_dir=opts.file_storage_dir, |
285 | 35a0d128 | Iustin Pop | file_driver=opts.file_driver, |
286 | 31a853d2 | Iustin Pop | hvm_acpi=hvm_acpi, hvm_pae=hvm_pae, |
287 | 31a853d2 | Iustin Pop | hvm_cdrom_image_path=hvm_cdrom_image_path, |
288 | ad2023a3 | Alexander Schreiber | vnc_bind_address=opts.vnc_bind_address, |
289 | ad2023a3 | Alexander Schreiber | hvm_nic_type=opts.hvm_nic_type, |
290 | ad2023a3 | Alexander Schreiber | hvm_disk_type=opts.hvm_disk_type) |
291 | 31a853d2 | Iustin Pop | |
292 | a8083063 | Iustin Pop | SubmitOpCode(op) |
293 | a8083063 | Iustin Pop | return 0 |
294 | a8083063 | Iustin Pop | |
295 | a8083063 | Iustin Pop | |
296 | fe7b0351 | Michael Hanselmann | def ReinstallInstance(opts, args): |
297 | fe7b0351 | Michael Hanselmann | """Reinstall an instance. |
298 | fe7b0351 | Michael Hanselmann | |
299 | fe7b0351 | Michael Hanselmann | Args: |
300 | fe7b0351 | Michael Hanselmann | opts - class with options as members |
301 | fe7b0351 | Michael Hanselmann | args - list containing a single element, the instance name |
302 | fe7b0351 | Michael Hanselmann | |
303 | fe7b0351 | Michael Hanselmann | """ |
304 | fe7b0351 | Michael Hanselmann | instance_name = args[0] |
305 | fe7b0351 | Michael Hanselmann | |
306 | fe7b0351 | Michael Hanselmann | if not opts.force: |
307 | f4bc1f2c | Michael Hanselmann | usertext = ("This will reinstall the instance %s and remove" |
308 | f4bc1f2c | Michael Hanselmann | " all data. Continue?") % instance_name |
309 | 47988778 | Iustin Pop | if not AskUser(usertext): |
310 | fe7b0351 | Michael Hanselmann | return 1 |
311 | fe7b0351 | Michael Hanselmann | |
312 | d0834de3 | Michael Hanselmann | op = opcodes.OpReinstallInstance(instance_name=instance_name, |
313 | d0834de3 | Michael Hanselmann | os_type=opts.os) |
314 | fe7b0351 | Michael Hanselmann | SubmitOpCode(op) |
315 | fe7b0351 | Michael Hanselmann | |
316 | fe7b0351 | Michael Hanselmann | return 0 |
317 | fe7b0351 | Michael Hanselmann | |
318 | fe7b0351 | Michael Hanselmann | |
319 | a8083063 | Iustin Pop | def RemoveInstance(opts, args): |
320 | a8083063 | Iustin Pop | """Remove an instance. |
321 | a8083063 | Iustin Pop | |
322 | a8083063 | Iustin Pop | Args: |
323 | a8083063 | Iustin Pop | opts - class with options as members |
324 | a8083063 | Iustin Pop | args - list containing a single element, the instance name |
325 | a8083063 | Iustin Pop | |
326 | a8083063 | Iustin Pop | """ |
327 | a8083063 | Iustin Pop | instance_name = args[0] |
328 | a8083063 | Iustin Pop | force = opts.force |
329 | a8083063 | Iustin Pop | |
330 | a8083063 | Iustin Pop | if not force: |
331 | a8083063 | Iustin Pop | usertext = ("This will remove the volumes of the instance %s" |
332 | a8083063 | Iustin Pop | " (including mirrors), thus removing all the data" |
333 | a8083063 | Iustin Pop | " of the instance. Continue?") % instance_name |
334 | 47988778 | Iustin Pop | if not AskUser(usertext): |
335 | a8083063 | Iustin Pop | return 1 |
336 | a8083063 | Iustin Pop | |
337 | 1d67656e | Iustin Pop | op = opcodes.OpRemoveInstance(instance_name=instance_name, |
338 | 1d67656e | Iustin Pop | ignore_failures=opts.ignore_failures) |
339 | a8083063 | Iustin Pop | SubmitOpCode(op) |
340 | a8083063 | Iustin Pop | return 0 |
341 | a8083063 | Iustin Pop | |
342 | a8083063 | Iustin Pop | |
343 | decd5f45 | Iustin Pop | def RenameInstance(opts, args): |
344 | 4ab0b9e3 | Guido Trotter | """Rename an instance. |
345 | decd5f45 | Iustin Pop | |
346 | decd5f45 | Iustin Pop | Args: |
347 | decd5f45 | Iustin Pop | opts - class with options as members |
348 | decd5f45 | Iustin Pop | args - list containing two elements, the instance name and the new name |
349 | decd5f45 | Iustin Pop | |
350 | decd5f45 | Iustin Pop | """ |
351 | decd5f45 | Iustin Pop | op = opcodes.OpRenameInstance(instance_name=args[0], |
352 | decd5f45 | Iustin Pop | new_name=args[1], |
353 | decd5f45 | Iustin Pop | ignore_ip=opts.ignore_ip) |
354 | decd5f45 | Iustin Pop | SubmitOpCode(op) |
355 | decd5f45 | Iustin Pop | |
356 | decd5f45 | Iustin Pop | return 0 |
357 | decd5f45 | Iustin Pop | |
358 | decd5f45 | Iustin Pop | |
359 | a8083063 | Iustin Pop | def ActivateDisks(opts, args): |
360 | a8083063 | Iustin Pop | """Activate an instance's disks. |
361 | a8083063 | Iustin Pop | |
362 | a8083063 | Iustin Pop | This serves two purposes: |
363 | a8083063 | Iustin Pop | - it allows one (as long as the instance is not running) to mount |
364 | a8083063 | Iustin Pop | the disks and modify them from the node |
365 | a8083063 | Iustin Pop | - it repairs inactive secondary drbds |
366 | a8083063 | Iustin Pop | |
367 | a8083063 | Iustin Pop | """ |
368 | a8083063 | Iustin Pop | instance_name = args[0] |
369 | a8083063 | Iustin Pop | op = opcodes.OpActivateInstanceDisks(instance_name=instance_name) |
370 | a8083063 | Iustin Pop | disks_info = SubmitOpCode(op) |
371 | a8083063 | Iustin Pop | for host, iname, nname in disks_info: |
372 | a8083063 | Iustin Pop | print "%s:%s:%s" % (host, iname, nname) |
373 | a8083063 | Iustin Pop | return 0 |
374 | a8083063 | Iustin Pop | |
375 | a8083063 | Iustin Pop | |
376 | a8083063 | Iustin Pop | def DeactivateDisks(opts, args): |
377 | a8083063 | Iustin Pop | """Command-line interface for _ShutdownInstanceBlockDevices. |
378 | a8083063 | Iustin Pop | |
379 | a8083063 | Iustin Pop | This function takes the instance name, looks for its primary node |
380 | a8083063 | Iustin Pop | and the tries to shutdown its block devices on that node. |
381 | a8083063 | Iustin Pop | |
382 | a8083063 | Iustin Pop | """ |
383 | a8083063 | Iustin Pop | instance_name = args[0] |
384 | a8083063 | Iustin Pop | op = opcodes.OpDeactivateInstanceDisks(instance_name=instance_name) |
385 | a8083063 | Iustin Pop | SubmitOpCode(op) |
386 | a8083063 | Iustin Pop | return 0 |
387 | a8083063 | Iustin Pop | |
388 | a8083063 | Iustin Pop | |
389 | c6e911bc | Iustin Pop | def GrowDisk(opts, args): |
390 | c6e911bc | Iustin Pop | """Command-line interface for _ShutdownInstanceBlockDevices. |
391 | c6e911bc | Iustin Pop | |
392 | c6e911bc | Iustin Pop | This function takes the instance name, looks for its primary node |
393 | c6e911bc | Iustin Pop | and the tries to shutdown its block devices on that node. |
394 | c6e911bc | Iustin Pop | |
395 | c6e911bc | Iustin Pop | """ |
396 | c6e911bc | Iustin Pop | instance = args[0] |
397 | c6e911bc | Iustin Pop | disk = args[1] |
398 | c6e911bc | Iustin Pop | amount = utils.ParseUnit(args[2]) |
399 | c6e911bc | Iustin Pop | op = opcodes.OpGrowDisk(instance_name=instance, disk=disk, amount=amount) |
400 | c6e911bc | Iustin Pop | SubmitOpCode(op) |
401 | c6e911bc | Iustin Pop | return 0 |
402 | c6e911bc | Iustin Pop | |
403 | c6e911bc | Iustin Pop | |
404 | a8083063 | Iustin Pop | def StartupInstance(opts, args): |
405 | decd5f45 | Iustin Pop | """Startup an instance. |
406 | a8083063 | Iustin Pop | |
407 | a8083063 | Iustin Pop | Args: |
408 | a8083063 | Iustin Pop | opts - class with options as members |
409 | a8083063 | Iustin Pop | args - list containing a single element, the instance name |
410 | a8083063 | Iustin Pop | |
411 | a8083063 | Iustin Pop | """ |
412 | 312ac745 | Iustin Pop | if opts.multi_mode is None: |
413 | 312ac745 | Iustin Pop | opts.multi_mode = _SHUTDOWN_INSTANCES |
414 | 312ac745 | Iustin Pop | inames = _ExpandMultiNames(opts.multi_mode, args) |
415 | bcee9cb4 | Iustin Pop | if not inames: |
416 | bcee9cb4 | Iustin Pop | raise errors.OpPrereqError("Selection filter does not match any instances") |
417 | 804a1e8e | Iustin Pop | multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1 |
418 | 804a1e8e | Iustin Pop | if not (opts.force_multi or not multi_on |
419 | 804a1e8e | Iustin Pop | or _ConfirmOperation(inames, "startup")): |
420 | 804a1e8e | Iustin Pop | return 1 |
421 | 312ac745 | Iustin Pop | for name in inames: |
422 | 312ac745 | Iustin Pop | op = opcodes.OpStartupInstance(instance_name=name, |
423 | 312ac745 | Iustin Pop | force=opts.force, |
424 | 312ac745 | Iustin Pop | extra_args=opts.extra_args) |
425 | 312ac745 | Iustin Pop | if multi_on: |
426 | 312ac745 | Iustin Pop | logger.ToStdout("Starting up %s" % name) |
427 | 312ac745 | Iustin Pop | SubmitOpCode(op) |
428 | a8083063 | Iustin Pop | return 0 |
429 | a8083063 | Iustin Pop | |
430 | 7c0d6283 | Michael Hanselmann | |
431 | 579d4337 | Alexander Schreiber | def RebootInstance(opts, args): |
432 | 579d4337 | Alexander Schreiber | """Reboot an instance |
433 | 579d4337 | Alexander Schreiber | |
434 | 579d4337 | Alexander Schreiber | Args: |
435 | 579d4337 | Alexander Schreiber | opts - class with options as members |
436 | 579d4337 | Alexander Schreiber | args - list containing a single element, the instance name |
437 | 579d4337 | Alexander Schreiber | |
438 | 579d4337 | Alexander Schreiber | """ |
439 | 579d4337 | Alexander Schreiber | if opts.multi_mode is None: |
440 | 579d4337 | Alexander Schreiber | opts.multi_mode = _SHUTDOWN_INSTANCES |
441 | 579d4337 | Alexander Schreiber | inames = _ExpandMultiNames(opts.multi_mode, args) |
442 | bcee9cb4 | Iustin Pop | if not inames: |
443 | bcee9cb4 | Iustin Pop | raise errors.OpPrereqError("Selection filter does not match any instances") |
444 | 579d4337 | Alexander Schreiber | multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1 |
445 | 579d4337 | Alexander Schreiber | if not (opts.force_multi or not multi_on |
446 | 579d4337 | Alexander Schreiber | or _ConfirmOperation(inames, "reboot")): |
447 | 579d4337 | Alexander Schreiber | return 1 |
448 | 579d4337 | Alexander Schreiber | for name in inames: |
449 | 579d4337 | Alexander Schreiber | op = opcodes.OpRebootInstance(instance_name=name, |
450 | 579d4337 | Alexander Schreiber | reboot_type=opts.reboot_type, |
451 | 579d4337 | Alexander Schreiber | ignore_secondaries=opts.ignore_secondaries) |
452 | 579d4337 | Alexander Schreiber | |
453 | 579d4337 | Alexander Schreiber | SubmitOpCode(op) |
454 | 579d4337 | Alexander Schreiber | return 0 |
455 | a8083063 | Iustin Pop | |
456 | 7c0d6283 | Michael Hanselmann | |
457 | a8083063 | Iustin Pop | def ShutdownInstance(opts, args): |
458 | a8083063 | Iustin Pop | """Shutdown an instance. |
459 | a8083063 | Iustin Pop | |
460 | a8083063 | Iustin Pop | Args: |
461 | a8083063 | Iustin Pop | opts - class with options as members |
462 | a8083063 | Iustin Pop | args - list containing a single element, the instance name |
463 | a8083063 | Iustin Pop | |
464 | a8083063 | Iustin Pop | """ |
465 | 312ac745 | Iustin Pop | if opts.multi_mode is None: |
466 | 312ac745 | Iustin Pop | opts.multi_mode = _SHUTDOWN_INSTANCES |
467 | 312ac745 | Iustin Pop | inames = _ExpandMultiNames(opts.multi_mode, args) |
468 | bcee9cb4 | Iustin Pop | if not inames: |
469 | bcee9cb4 | Iustin Pop | raise errors.OpPrereqError("Selection filter does not match any instances") |
470 | 804a1e8e | Iustin Pop | multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1 |
471 | 804a1e8e | Iustin Pop | if not (opts.force_multi or not multi_on |
472 | 804a1e8e | Iustin Pop | or _ConfirmOperation(inames, "shutdown")): |
473 | 804a1e8e | Iustin Pop | return 1 |
474 | 312ac745 | Iustin Pop | for name in inames: |
475 | 312ac745 | Iustin Pop | op = opcodes.OpShutdownInstance(instance_name=name) |
476 | 312ac745 | Iustin Pop | if multi_on: |
477 | 312ac745 | Iustin Pop | logger.ToStdout("Shutting down %s" % name) |
478 | 312ac745 | Iustin Pop | SubmitOpCode(op) |
479 | a8083063 | Iustin Pop | return 0 |
480 | a8083063 | Iustin Pop | |
481 | a8083063 | Iustin Pop | |
482 | a8083063 | Iustin Pop | def ReplaceDisks(opts, args): |
483 | a8083063 | Iustin Pop | """Replace the disks of an instance |
484 | a8083063 | Iustin Pop | |
485 | a8083063 | Iustin Pop | Args: |
486 | a8083063 | Iustin Pop | opts - class with options as members |
487 | a8083063 | Iustin Pop | args - list with a single element, the instance name |
488 | a8083063 | Iustin Pop | |
489 | a8083063 | Iustin Pop | """ |
490 | a8083063 | Iustin Pop | instance_name = args[0] |
491 | a9e0c397 | Iustin Pop | new_2ndary = opts.new_secondary |
492 | b6e82a65 | Iustin Pop | iallocator = opts.iallocator |
493 | a9e0c397 | Iustin Pop | if opts.disks is None: |
494 | a9e0c397 | Iustin Pop | disks = ["sda", "sdb"] |
495 | a9e0c397 | Iustin Pop | else: |
496 | a9e0c397 | Iustin Pop | disks = opts.disks.split(",") |
497 | a9e0c397 | Iustin Pop | if opts.on_primary == opts.on_secondary: # no -p or -s passed, or both passed |
498 | a9e0c397 | Iustin Pop | mode = constants.REPLACE_DISK_ALL |
499 | a9e0c397 | Iustin Pop | elif opts.on_primary: # only on primary: |
500 | a9e0c397 | Iustin Pop | mode = constants.REPLACE_DISK_PRI |
501 | b6e82a65 | Iustin Pop | if new_2ndary is not None or iallocator is not None: |
502 | a9e0c397 | Iustin Pop | raise errors.OpPrereqError("Can't change secondary node on primary disk" |
503 | a9e0c397 | Iustin Pop | " replacement") |
504 | b6e82a65 | Iustin Pop | elif opts.on_secondary is not None or iallocator is not None: |
505 | b6e82a65 | Iustin Pop | # only on secondary |
506 | a9e0c397 | Iustin Pop | mode = constants.REPLACE_DISK_SEC |
507 | a9e0c397 | Iustin Pop | |
508 | a9e0c397 | Iustin Pop | op = opcodes.OpReplaceDisks(instance_name=args[0], disks=disks, |
509 | b6e82a65 | Iustin Pop | remote_node=new_2ndary, mode=mode, |
510 | b6e82a65 | Iustin Pop | iallocator=iallocator) |
511 | a8083063 | Iustin Pop | SubmitOpCode(op) |
512 | a8083063 | Iustin Pop | return 0 |
513 | a8083063 | Iustin Pop | |
514 | a8083063 | Iustin Pop | |
515 | a8083063 | Iustin Pop | def FailoverInstance(opts, args): |
516 | a8083063 | Iustin Pop | """Failover an instance. |
517 | a8083063 | Iustin Pop | |
518 | a8083063 | Iustin Pop | The failover is done by shutting it down on its present node and |
519 | a8083063 | Iustin Pop | starting it on the secondary. |
520 | a8083063 | Iustin Pop | |
521 | a8083063 | Iustin Pop | Args: |
522 | a8083063 | Iustin Pop | opts - class with options as members |
523 | a8083063 | Iustin Pop | args - list with a single element, the instance name |
524 | a8083063 | Iustin Pop | Opts used: |
525 | a8083063 | Iustin Pop | force - whether to failover without asking questions. |
526 | a8083063 | Iustin Pop | |
527 | a8083063 | Iustin Pop | """ |
528 | 80de0e3f | Iustin Pop | instance_name = args[0] |
529 | 80de0e3f | Iustin Pop | force = opts.force |
530 | a8083063 | Iustin Pop | |
531 | 80de0e3f | Iustin Pop | if not force: |
532 | 80de0e3f | Iustin Pop | usertext = ("Failover will happen to image %s." |
533 | 80de0e3f | Iustin Pop | " This requires a shutdown of the instance. Continue?" % |
534 | 80de0e3f | Iustin Pop | (instance_name,)) |
535 | 80de0e3f | Iustin Pop | if not AskUser(usertext): |
536 | 80de0e3f | Iustin Pop | return 1 |
537 | a8083063 | Iustin Pop | |
538 | 80de0e3f | Iustin Pop | op = opcodes.OpFailoverInstance(instance_name=instance_name, |
539 | 80de0e3f | Iustin Pop | ignore_consistency=opts.ignore_consistency) |
540 | 80de0e3f | Iustin Pop | SubmitOpCode(op) |
541 | 80de0e3f | Iustin Pop | return 0 |
542 | a8083063 | Iustin Pop | |
543 | a8083063 | Iustin Pop | |
544 | a8083063 | Iustin Pop | def ConnectToInstanceConsole(opts, args): |
545 | a8083063 | Iustin Pop | """Connect to the console of an instance. |
546 | a8083063 | Iustin Pop | |
547 | a8083063 | Iustin Pop | Args: |
548 | a8083063 | Iustin Pop | opts - class with options as members |
549 | a8083063 | Iustin Pop | args - list with a single element, the instance name |
550 | a8083063 | Iustin Pop | |
551 | a8083063 | Iustin Pop | """ |
552 | a8083063 | Iustin Pop | instance_name = args[0] |
553 | a8083063 | Iustin Pop | |
554 | a8083063 | Iustin Pop | op = opcodes.OpConnectConsole(instance_name=instance_name) |
555 | 0a80a26f | Michael Hanselmann | cmd = SubmitOpCode(op) |
556 | 51c6e7b5 | Michael Hanselmann | |
557 | 51c6e7b5 | Michael Hanselmann | if opts.show_command: |
558 | 51c6e7b5 | Michael Hanselmann | print utils.ShellQuoteArgs(cmd) |
559 | 51c6e7b5 | Michael Hanselmann | else: |
560 | 51c6e7b5 | Michael Hanselmann | try: |
561 | 51c6e7b5 | Michael Hanselmann | os.execvp(cmd[0], cmd) |
562 | 51c6e7b5 | Michael Hanselmann | finally: |
563 | 51c6e7b5 | Michael Hanselmann | sys.stderr.write("Can't run console command %s with arguments:\n'%s'" % |
564 | 51c6e7b5 | Michael Hanselmann | (cmd, " ".join(argv))) |
565 | 51c6e7b5 | Michael Hanselmann | os._exit(1) |
566 | a8083063 | Iustin Pop | |
567 | a8083063 | Iustin Pop | |
568 | a8083063 | Iustin Pop | def _FormatBlockDevInfo(buf, dev, indent_level): |
569 | a8083063 | Iustin Pop | """Show block device information. |
570 | a8083063 | Iustin Pop | |
571 | a8083063 | Iustin Pop | This is only used by ShowInstanceConfig(), but it's too big to be |
572 | a8083063 | Iustin Pop | left for an inline definition. |
573 | a8083063 | Iustin Pop | |
574 | a8083063 | Iustin Pop | """ |
575 | a8083063 | Iustin Pop | def helper(buf, dtype, status): |
576 | f2e9e0e8 | Alexander Schreiber | """Format one line for physical device status.""" |
577 | a8083063 | Iustin Pop | if not status: |
578 | a8083063 | Iustin Pop | buf.write("not active\n") |
579 | a8083063 | Iustin Pop | else: |
580 | 0834c866 | Iustin Pop | (path, major, minor, syncp, estt, degr, ldisk) = status |
581 | fd38ef95 | Manuel Franceschini | if major is None: |
582 | fd38ef95 | Manuel Franceschini | major_string = "N/A" |
583 | fd38ef95 | Manuel Franceschini | else: |
584 | fd38ef95 | Manuel Franceschini | major_string = str(major) |
585 | fd38ef95 | Manuel Franceschini | |
586 | fd38ef95 | Manuel Franceschini | if minor is None: |
587 | fd38ef95 | Manuel Franceschini | minor_string = "N/A" |
588 | fd38ef95 | Manuel Franceschini | else: |
589 | fd38ef95 | Manuel Franceschini | minor_string = str(minor) |
590 | fd38ef95 | Manuel Franceschini | |
591 | fd38ef95 | Manuel Franceschini | buf.write("%s (%s:%s)" % (path, major_string, minor_string)) |
592 | 00fb8246 | Michael Hanselmann | if dtype in (constants.LD_DRBD8, ): |
593 | a8083063 | Iustin Pop | if syncp is not None: |
594 | a8083063 | Iustin Pop | sync_text = "*RECOVERING* %5.2f%%," % syncp |
595 | a8083063 | Iustin Pop | if estt: |
596 | a8083063 | Iustin Pop | sync_text += " ETA %ds" % estt |
597 | a8083063 | Iustin Pop | else: |
598 | a8083063 | Iustin Pop | sync_text += " ETA unknown" |
599 | a8083063 | Iustin Pop | else: |
600 | a8083063 | Iustin Pop | sync_text = "in sync" |
601 | a8083063 | Iustin Pop | if degr: |
602 | a8083063 | Iustin Pop | degr_text = "*DEGRADED*" |
603 | a8083063 | Iustin Pop | else: |
604 | a8083063 | Iustin Pop | degr_text = "ok" |
605 | 0834c866 | Iustin Pop | if ldisk: |
606 | 0834c866 | Iustin Pop | ldisk_text = " *MISSING DISK*" |
607 | 0834c866 | Iustin Pop | else: |
608 | 0834c866 | Iustin Pop | ldisk_text = "" |
609 | 0834c866 | Iustin Pop | buf.write(" %s, status %s%s" % (sync_text, degr_text, ldisk_text)) |
610 | 9db6dbce | Iustin Pop | elif dtype == constants.LD_LV: |
611 | 0834c866 | Iustin Pop | if ldisk: |
612 | 0834c866 | Iustin Pop | ldisk_text = " *FAILED* (failed drive?)" |
613 | 9db6dbce | Iustin Pop | else: |
614 | 0834c866 | Iustin Pop | ldisk_text = "" |
615 | 0834c866 | Iustin Pop | buf.write(ldisk_text) |
616 | a8083063 | Iustin Pop | buf.write("\n") |
617 | a8083063 | Iustin Pop | |
618 | a8083063 | Iustin Pop | if dev["iv_name"] is not None: |
619 | a8083063 | Iustin Pop | data = " - %s, " % dev["iv_name"] |
620 | a8083063 | Iustin Pop | else: |
621 | a8083063 | Iustin Pop | data = " - " |
622 | a8083063 | Iustin Pop | data += "type: %s" % dev["dev_type"] |
623 | a8083063 | Iustin Pop | if dev["logical_id"] is not None: |
624 | a8083063 | Iustin Pop | data += ", logical_id: %s" % (dev["logical_id"],) |
625 | a8083063 | Iustin Pop | elif dev["physical_id"] is not None: |
626 | a8083063 | Iustin Pop | data += ", physical_id: %s" % (dev["physical_id"],) |
627 | a8083063 | Iustin Pop | buf.write("%*s%s\n" % (2*indent_level, "", data)) |
628 | a8083063 | Iustin Pop | buf.write("%*s primary: " % (2*indent_level, "")) |
629 | a8083063 | Iustin Pop | helper(buf, dev["dev_type"], dev["pstatus"]) |
630 | a8083063 | Iustin Pop | |
631 | a8083063 | Iustin Pop | if dev["sstatus"]: |
632 | a8083063 | Iustin Pop | buf.write("%*s secondary: " % (2*indent_level, "")) |
633 | a8083063 | Iustin Pop | helper(buf, dev["dev_type"], dev["sstatus"]) |
634 | a8083063 | Iustin Pop | |
635 | a8083063 | Iustin Pop | if dev["children"]: |
636 | a8083063 | Iustin Pop | for child in dev["children"]: |
637 | a8083063 | Iustin Pop | _FormatBlockDevInfo(buf, child, indent_level+1) |
638 | a8083063 | Iustin Pop | |
639 | a8083063 | Iustin Pop | |
640 | a8083063 | Iustin Pop | def ShowInstanceConfig(opts, args): |
641 | a8083063 | Iustin Pop | """Compute instance run-time status. |
642 | a8083063 | Iustin Pop | |
643 | a8083063 | Iustin Pop | """ |
644 | a8083063 | Iustin Pop | retcode = 0 |
645 | a8083063 | Iustin Pop | op = opcodes.OpQueryInstanceData(instances=args) |
646 | a8083063 | Iustin Pop | result = SubmitOpCode(op) |
647 | a8340917 | Iustin Pop | hvm_parameters = ("hvm_acpi", "hvm_pae", "hvm_cdrom_image_path", |
648 | ad2023a3 | Alexander Schreiber | "hvm_boot_order", "hvm_nic_type", "hvm_disk_type") |
649 | a8340917 | Iustin Pop | |
650 | a8340917 | Iustin Pop | pvm_parameters = ("kernel_path", "initrd_path") |
651 | a8083063 | Iustin Pop | |
652 | a8083063 | Iustin Pop | if not result: |
653 | a8083063 | Iustin Pop | logger.ToStdout("No instances.") |
654 | a8083063 | Iustin Pop | return 1 |
655 | a8083063 | Iustin Pop | |
656 | a8083063 | Iustin Pop | buf = StringIO() |
657 | a8083063 | Iustin Pop | retcode = 0 |
658 | a8083063 | Iustin Pop | for instance_name in result: |
659 | a8083063 | Iustin Pop | instance = result[instance_name] |
660 | a8083063 | Iustin Pop | buf.write("Instance name: %s\n" % instance["name"]) |
661 | a8083063 | Iustin Pop | buf.write("State: configured to be %s, actual state is %s\n" % |
662 | a8083063 | Iustin Pop | (instance["config_state"], instance["run_state"])) |
663 | a8083063 | Iustin Pop | buf.write(" Nodes:\n") |
664 | a8083063 | Iustin Pop | buf.write(" - primary: %s\n" % instance["pnode"]) |
665 | a8083063 | Iustin Pop | buf.write(" - secondaries: %s\n" % ", ".join(instance["snodes"])) |
666 | a8083063 | Iustin Pop | buf.write(" Operating system: %s\n" % instance["os"]) |
667 | a8340917 | Iustin Pop | if instance.has_key("network_port"): |
668 | a8340917 | Iustin Pop | buf.write(" Allocated network port: %s\n" % instance["network_port"]) |
669 | a8340917 | Iustin Pop | if False not in map(instance.has_key, pvm_parameters): |
670 | a8340917 | Iustin Pop | if instance["kernel_path"] in (None, constants.VALUE_DEFAULT): |
671 | a8340917 | Iustin Pop | kpath = "(default: %s)" % constants.XEN_KERNEL |
672 | a8340917 | Iustin Pop | else: |
673 | a8340917 | Iustin Pop | kpath = instance["kernel_path"] |
674 | a8340917 | Iustin Pop | buf.write(" Kernel path: %s\n" % kpath) |
675 | a8340917 | Iustin Pop | if instance["initrd_path"] in (None, constants.VALUE_DEFAULT): |
676 | a8340917 | Iustin Pop | initrd = "(default: %s)" % constants.XEN_INITRD |
677 | a8340917 | Iustin Pop | elif instance["initrd_path"] == constants.VALUE_NONE: |
678 | a8340917 | Iustin Pop | initrd = "(none)" |
679 | a8340917 | Iustin Pop | else: |
680 | a8340917 | Iustin Pop | initrd = instance["initrd_path"] |
681 | a8340917 | Iustin Pop | buf.write(" initrd: %s\n" % initrd) |
682 | a8340917 | Iustin Pop | if False not in map(instance.has_key, hvm_parameters): |
683 | a8340917 | Iustin Pop | buf.write(" HVM:\n") |
684 | a8340917 | Iustin Pop | buf.write(" - boot order: %s\n" % instance["hvm_boot_order"]) |
685 | a8340917 | Iustin Pop | buf.write(" - ACPI support: %s\n" % instance["hvm_acpi"]) |
686 | a8340917 | Iustin Pop | buf.write(" - PAE support: %s\n" % instance["hvm_pae"]) |
687 | a8340917 | Iustin Pop | buf.write(" - virtual CDROM: %s\n" % instance["hvm_cdrom_image_path"]) |
688 | ad2023a3 | Alexander Schreiber | buf.write(" - virtual NIC type: %s\n" % instance["hvm_nic_type"]) |
689 | ad2023a3 | Alexander Schreiber | buf.write(" - virtual disk type: %s\n" % instance["hvm_disk_type"]) |
690 | a8340917 | Iustin Pop | if instance.has_key("vnc_bind_address"): |
691 | a8340917 | Iustin Pop | buf.write(" VNC bind address: %s\n" % instance["vnc_bind_address"]) |
692 | a8083063 | Iustin Pop | buf.write(" Hardware:\n") |
693 | f55ff7ec | Iustin Pop | buf.write(" - VCPUs: %d\n" % instance["vcpus"]) |
694 | a8083063 | Iustin Pop | buf.write(" - memory: %dMiB\n" % instance["memory"]) |
695 | a8083063 | Iustin Pop | buf.write(" - NICs: %s\n" % |
696 | a8083063 | Iustin Pop | ", ".join(["{MAC: %s, IP: %s, bridge: %s}" % |
697 | a8083063 | Iustin Pop | (mac, ip, bridge) |
698 | a8083063 | Iustin Pop | for mac, ip, bridge in instance["nics"]])) |
699 | a8083063 | Iustin Pop | buf.write(" Block devices:\n") |
700 | a8083063 | Iustin Pop | |
701 | a8083063 | Iustin Pop | for device in instance["disks"]: |
702 | a8083063 | Iustin Pop | _FormatBlockDevInfo(buf, device, 1) |
703 | a8083063 | Iustin Pop | |
704 | a8083063 | Iustin Pop | logger.ToStdout(buf.getvalue().rstrip('\n')) |
705 | a8083063 | Iustin Pop | return retcode |
706 | a8083063 | Iustin Pop | |
707 | a8083063 | Iustin Pop | |
708 | 7767bbf5 | Manuel Franceschini | def SetInstanceParams(opts, args): |
709 | a8083063 | Iustin Pop | """Modifies an instance. |
710 | a8083063 | Iustin Pop | |
711 | a8083063 | Iustin Pop | All parameters take effect only at the next restart of the instance. |
712 | a8083063 | Iustin Pop | |
713 | a8083063 | Iustin Pop | Args: |
714 | a8083063 | Iustin Pop | opts - class with options as members |
715 | a8083063 | Iustin Pop | args - list with a single element, the instance name |
716 | a8083063 | Iustin Pop | Opts used: |
717 | a8083063 | Iustin Pop | memory - the new memory size |
718 | a8083063 | Iustin Pop | vcpus - the new number of cpus |
719 | 1862d460 | Alexander Schreiber | mac - the new MAC address of the instance |
720 | a8083063 | Iustin Pop | |
721 | a8083063 | Iustin Pop | """ |
722 | 973d7867 | Iustin Pop | if not (opts.mem or opts.vcpus or opts.ip or opts.bridge or opts.mac or |
723 | 31a853d2 | Iustin Pop | opts.kernel_path or opts.initrd_path or opts.hvm_boot_order or |
724 | 917f91d5 | Iustin Pop | opts.hvm_acpi or opts.hvm_pae or opts.hvm_cdrom_image_path or |
725 | ad2023a3 | Alexander Schreiber | opts.vnc_bind_address or opts.hvm_nic_type or opts.hvm_disk_type): |
726 | a8083063 | Iustin Pop | logger.ToStdout("Please give at least one of the parameters.") |
727 | a8083063 | Iustin Pop | return 1 |
728 | a8083063 | Iustin Pop | |
729 | 973d7867 | Iustin Pop | kernel_path = _TransformPath(opts.kernel_path) |
730 | 973d7867 | Iustin Pop | initrd_path = _TransformPath(opts.initrd_path) |
731 | 40cece88 | Iustin Pop | if opts.hvm_boot_order == 'default': |
732 | 25c5878d | Alexander Schreiber | hvm_boot_order = constants.VALUE_DEFAULT |
733 | 25c5878d | Alexander Schreiber | else: |
734 | 25c5878d | Alexander Schreiber | hvm_boot_order = opts.hvm_boot_order |
735 | 973d7867 | Iustin Pop | |
736 | 3fc175f0 | Alexander Schreiber | if opts.hvm_acpi is None: |
737 | 3fc175f0 | Alexander Schreiber | hvm_acpi = opts.hvm_acpi |
738 | 3fc175f0 | Alexander Schreiber | else: |
739 | 3fc175f0 | Alexander Schreiber | hvm_acpi = opts.hvm_acpi == _VALUE_TRUE |
740 | 31a853d2 | Iustin Pop | |
741 | 3fc175f0 | Alexander Schreiber | if opts.hvm_pae is None: |
742 | 3fc175f0 | Alexander Schreiber | hvm_pae = opts.hvm_pae |
743 | 31a853d2 | Iustin Pop | else: |
744 | 3fc175f0 | Alexander Schreiber | hvm_pae = opts.hvm_pae == _VALUE_TRUE |
745 | 31a853d2 | Iustin Pop | |
746 | ad2023a3 | Alexander Schreiber | if opts.hvm_nic_type == constants.VALUE_NONE: |
747 | ad2023a3 | Alexander Schreiber | hvm_nic_type = None |
748 | ad2023a3 | Alexander Schreiber | else: |
749 | ad2023a3 | Alexander Schreiber | hvm_nic_type = opts.hvm_nic_type |
750 | ad2023a3 | Alexander Schreiber | |
751 | ad2023a3 | Alexander Schreiber | if opts.hvm_disk_type == constants.VALUE_NONE: |
752 | ad2023a3 | Alexander Schreiber | hvm_disk_type = None |
753 | ad2023a3 | Alexander Schreiber | else: |
754 | ad2023a3 | Alexander Schreiber | hvm_disk_type = opts.hvm_disk_type |
755 | ad2023a3 | Alexander Schreiber | |
756 | 7767bbf5 | Manuel Franceschini | op = opcodes.OpSetInstanceParams(instance_name=args[0], mem=opts.mem, |
757 | 7767bbf5 | Manuel Franceschini | vcpus=opts.vcpus, ip=opts.ip, |
758 | 7767bbf5 | Manuel Franceschini | bridge=opts.bridge, mac=opts.mac, |
759 | 7767bbf5 | Manuel Franceschini | kernel_path=opts.kernel_path, |
760 | 7767bbf5 | Manuel Franceschini | initrd_path=opts.initrd_path, |
761 | 31a853d2 | Iustin Pop | hvm_boot_order=hvm_boot_order, |
762 | 31a853d2 | Iustin Pop | hvm_acpi=hvm_acpi, hvm_pae=hvm_pae, |
763 | 3fc175f0 | Alexander Schreiber | hvm_cdrom_image_path= |
764 | 3fc175f0 | Alexander Schreiber | opts.hvm_cdrom_image_path, |
765 | ad2023a3 | Alexander Schreiber | vnc_bind_address=opts.vnc_bind_address, |
766 | ad2023a3 | Alexander Schreiber | hvm_nic_type=hvm_nic_type, |
767 | 4300c4b6 | Guido Trotter | hvm_disk_type=hvm_disk_type, |
768 | 4300c4b6 | Guido Trotter | force=opts.force) |
769 | 31a853d2 | Iustin Pop | |
770 | a8083063 | Iustin Pop | result = SubmitOpCode(op) |
771 | a8083063 | Iustin Pop | |
772 | a8083063 | Iustin Pop | if result: |
773 | a8083063 | Iustin Pop | logger.ToStdout("Modified instance %s" % args[0]) |
774 | a8083063 | Iustin Pop | for param, data in result: |
775 | a8083063 | Iustin Pop | logger.ToStdout(" - %-5s -> %s" % (param, data)) |
776 | a8083063 | Iustin Pop | logger.ToStdout("Please don't forget that these parameters take effect" |
777 | a8083063 | Iustin Pop | " only at the next start of the instance.") |
778 | a8083063 | Iustin Pop | return 0 |
779 | a8083063 | Iustin Pop | |
780 | a8083063 | Iustin Pop | |
781 | a8083063 | Iustin Pop | # options used in more than one cmd |
782 | a8083063 | Iustin Pop | node_opt = make_option("-n", "--node", dest="node", help="Target node", |
783 | a8083063 | Iustin Pop | metavar="<node>") |
784 | a8083063 | Iustin Pop | |
785 | d0834de3 | Michael Hanselmann | os_opt = cli_option("-o", "--os-type", dest="os", help="What OS to run", |
786 | 739ecd56 | Michael Hanselmann | metavar="<os>") |
787 | d0834de3 | Michael Hanselmann | |
788 | 312ac745 | Iustin Pop | # multi-instance selection options |
789 | 804a1e8e | Iustin Pop | m_force_multi = make_option("--force-multiple", dest="force_multi", |
790 | 804a1e8e | Iustin Pop | help="Do not ask for confirmation when more than" |
791 | 804a1e8e | Iustin Pop | " one instance is affected", |
792 | 804a1e8e | Iustin Pop | action="store_true", default=False) |
793 | 804a1e8e | Iustin Pop | |
794 | 312ac745 | Iustin Pop | m_pri_node_opt = make_option("--primary", dest="multi_mode", |
795 | 312ac745 | Iustin Pop | help="Filter by nodes (primary only)", |
796 | 312ac745 | Iustin Pop | const=_SHUTDOWN_NODES_PRI, action="store_const") |
797 | 312ac745 | Iustin Pop | |
798 | 312ac745 | Iustin Pop | m_sec_node_opt = make_option("--secondary", dest="multi_mode", |
799 | 312ac745 | Iustin Pop | help="Filter by nodes (secondary only)", |
800 | 312ac745 | Iustin Pop | const=_SHUTDOWN_NODES_SEC, action="store_const") |
801 | 312ac745 | Iustin Pop | |
802 | 312ac745 | Iustin Pop | m_node_opt = make_option("--node", dest="multi_mode", |
803 | 312ac745 | Iustin Pop | help="Filter by nodes (primary and secondary)", |
804 | 312ac745 | Iustin Pop | const=_SHUTDOWN_NODES_BOTH, action="store_const") |
805 | 312ac745 | Iustin Pop | |
806 | 312ac745 | Iustin Pop | m_clust_opt = make_option("--all", dest="multi_mode", |
807 | 312ac745 | Iustin Pop | help="Select all instances in the cluster", |
808 | 312ac745 | Iustin Pop | const=_SHUTDOWN_CLUSTER, action="store_const") |
809 | 312ac745 | Iustin Pop | |
810 | 312ac745 | Iustin Pop | m_inst_opt = make_option("--instance", dest="multi_mode", |
811 | 312ac745 | Iustin Pop | help="Filter by instance name [default]", |
812 | 312ac745 | Iustin Pop | const=_SHUTDOWN_INSTANCES, action="store_const") |
813 | 312ac745 | Iustin Pop | |
814 | 312ac745 | Iustin Pop | |
815 | a8083063 | Iustin Pop | # this is defined separately due to readability only |
816 | a8083063 | Iustin Pop | add_opts = [ |
817 | a8083063 | Iustin Pop | DEBUG_OPT, |
818 | 60d49723 | Michael Hanselmann | make_option("-n", "--node", dest="node", |
819 | 60d49723 | Michael Hanselmann | help="Target node and optional secondary node", |
820 | 60d49723 | Michael Hanselmann | metavar="<pnode>[:<snode>]"), |
821 | b9ac33e9 | Iustin Pop | cli_option("-s", "--os-size", dest="size", help="Disk size, in MiB unless" |
822 | b9ac33e9 | Iustin Pop | " a suffix is used", |
823 | a8083063 | Iustin Pop | default=20 * 1024, type="unit", metavar="<size>"), |
824 | b9ac33e9 | Iustin Pop | cli_option("--swap-size", dest="swap", help="Swap size, in MiB unless a" |
825 | b9ac33e9 | Iustin Pop | " suffix is used", |
826 | a8083063 | Iustin Pop | default=4 * 1024, type="unit", metavar="<size>"), |
827 | d0834de3 | Michael Hanselmann | os_opt, |
828 | b9ac33e9 | Iustin Pop | cli_option("-m", "--memory", dest="mem", help="Memory size (in MiB)", |
829 | a8083063 | Iustin Pop | default=128, type="unit", metavar="<mem>"), |
830 | a8083063 | Iustin Pop | make_option("-p", "--cpu", dest="vcpus", help="Number of virtual CPUs", |
831 | a8083063 | Iustin Pop | default=1, type="int", metavar="<PROC>"), |
832 | a8083063 | Iustin Pop | make_option("-t", "--disk-template", dest="disk_template", |
833 | 45873083 | Manuel Franceschini | help="Custom disk setup (diskless, file, plain or drbd)", |
834 | f9193417 | Iustin Pop | default=None, metavar="TEMPL"), |
835 | a8083063 | Iustin Pop | make_option("-i", "--ip", dest="ip", |
836 | a8083063 | Iustin Pop | help="IP address ('none' [default], 'auto', or specify address)", |
837 | a8083063 | Iustin Pop | default='none', type="string", metavar="<ADDRESS>"), |
838 | 1862d460 | Alexander Schreiber | make_option("--mac", dest="mac", |
839 | 1862d460 | Alexander Schreiber | help="MAC address ('auto' [default], or specify address)", |
840 | 1862d460 | Alexander Schreiber | default='auto', type="string", metavar="<MACADDRESS>"), |
841 | a8083063 | Iustin Pop | make_option("--no-wait-for-sync", dest="wait_for_sync", default=True, |
842 | a8083063 | Iustin Pop | action="store_false", help="Don't wait for sync (DANGEROUS!)"), |
843 | a8083063 | Iustin Pop | make_option("-b", "--bridge", dest="bridge", |
844 | a8083063 | Iustin Pop | help="Bridge to connect this instance to", |
845 | bdd55f71 | Iustin Pop | default=None, metavar="<bridge>"), |
846 | bdd55f71 | Iustin Pop | make_option("--no-start", dest="start", default=True, |
847 | bdd55f71 | Iustin Pop | action="store_false", help="Don't start the instance after" |
848 | bdd55f71 | Iustin Pop | " creation"), |
849 | bdd55f71 | Iustin Pop | make_option("--no-ip-check", dest="ip_check", default=True, |
850 | bdd55f71 | Iustin Pop | action="store_false", help="Don't check that the instance's IP" |
851 | bdd55f71 | Iustin Pop | " is alive (only valid with --no-start)"), |
852 | 3b6d8c9b | Iustin Pop | make_option("--kernel", dest="kernel_path", |
853 | 3b6d8c9b | Iustin Pop | help="Path to the instances' kernel (or 'default')", |
854 | 3b6d8c9b | Iustin Pop | default=None, |
855 | 3b6d8c9b | Iustin Pop | type="string", metavar="<FILENAME>"), |
856 | 3b6d8c9b | Iustin Pop | make_option("--initrd", dest="initrd_path", |
857 | 3b6d8c9b | Iustin Pop | help="Path to the instances' initrd (or 'none', or 'default')", |
858 | 3b6d8c9b | Iustin Pop | default=None, |
859 | 3b6d8c9b | Iustin Pop | type="string", metavar="<FILENAME>"), |
860 | 25c5878d | Alexander Schreiber | make_option("--hvm-boot-order", dest="hvm_boot_order", |
861 | 538475ca | Iustin Pop | help="Boot device order for HVM (one or more of [acdn])", |
862 | 25c5878d | Alexander Schreiber | default=None, type="string", metavar="<BOOTORDER>"), |
863 | 45873083 | Manuel Franceschini | make_option("--file-storage-dir", dest="file_storage_dir", |
864 | 45873083 | Manuel Franceschini | help="Relative path under default cluster-wide file storage dir" |
865 | 45873083 | Manuel Franceschini | " to store file-based disks", default=None, |
866 | 45873083 | Manuel Franceschini | metavar="<DIR>"), |
867 | 45873083 | Manuel Franceschini | make_option("--file-driver", dest="file_driver", help="Driver to use" |
868 | 538475ca | Iustin Pop | " for image files", default="loop", metavar="<DRIVER>"), |
869 | 538475ca | Iustin Pop | make_option("--iallocator", metavar="<NAME>", |
870 | 538475ca | Iustin Pop | help="Select nodes for the instance automatically using the" |
871 | 538475ca | Iustin Pop | " <NAME> iallocator plugin", default=None, type="string"), |
872 | 31a853d2 | Iustin Pop | make_option("--hvm-acpi", dest="hvm_acpi", |
873 | 31a853d2 | Iustin Pop | help="ACPI support for HVM (true|false)", |
874 | 31a853d2 | Iustin Pop | metavar="<BOOL>", choices=["true", "false"]), |
875 | ad2023a3 | Alexander Schreiber | make_option("--hvm-nic-type", dest="hvm_nic_type", |
876 | ad2023a3 | Alexander Schreiber | help="Type of virtual NIC for HVM " |
877 | ad2023a3 | Alexander Schreiber | "(rtl8139,ne2k_pci,ne2k_isa,paravirtual)", |
878 | ad2023a3 | Alexander Schreiber | metavar="NICTYPE", choices=[constants.HT_HVM_NIC_RTL8139, |
879 | ad2023a3 | Alexander Schreiber | constants.HT_HVM_NIC_NE2K_PCI, |
880 | ad2023a3 | Alexander Schreiber | constants.HT_HVM_NIC_NE2K_ISA, |
881 | ad2023a3 | Alexander Schreiber | constants.HT_HVM_DEV_PARAVIRTUAL], |
882 | ad2023a3 | Alexander Schreiber | default=constants.HT_HVM_NIC_RTL8139), |
883 | ad2023a3 | Alexander Schreiber | make_option("--hvm-disk-type", dest="hvm_disk_type", |
884 | ad2023a3 | Alexander Schreiber | help="Type of virtual disks for HVM (ioemu,paravirtual)", |
885 | ad2023a3 | Alexander Schreiber | metavar="DISKTYPE", choices=[constants.HT_HVM_DEV_IOEMU, |
886 | ad2023a3 | Alexander Schreiber | constants.HT_HVM_DEV_PARAVIRTUAL], |
887 | ad2023a3 | Alexander Schreiber | default=constants.HT_HVM_DEV_IOEMU,), |
888 | 31a853d2 | Iustin Pop | make_option("--hvm-pae", dest="hvm_pae", |
889 | 31a853d2 | Iustin Pop | help="PAE support for HVM (true|false)", |
890 | 31a853d2 | Iustin Pop | metavar="<BOOL>", choices=["true", "false"]), |
891 | 31a853d2 | Iustin Pop | make_option("--hvm-cdrom-image-path", dest="hvm_cdrom_image_path", |
892 | 31a853d2 | Iustin Pop | help="CDROM image path for HVM (absolute path or None)", |
893 | 31a853d2 | Iustin Pop | default=None, type="string", metavar="<CDROMIMAGE>"), |
894 | 31a853d2 | Iustin Pop | make_option("--vnc-bind-address", dest="vnc_bind_address", |
895 | 31a853d2 | Iustin Pop | help="bind address for VNC (IP address)", |
896 | 31a853d2 | Iustin Pop | default=None, type="string", metavar="<VNCADDRESS>"), |
897 | a8083063 | Iustin Pop | ] |
898 | a8083063 | Iustin Pop | |
899 | a8083063 | Iustin Pop | commands = { |
900 | a8083063 | Iustin Pop | 'add': (AddInstance, ARGS_ONE, add_opts, |
901 | bdb7d4e8 | Michael Hanselmann | "[...] -t disk-type -n node[:secondary-node] -o os-type <name>", |
902 | a8083063 | Iustin Pop | "Creates and adds a new instance to the cluster"), |
903 | 51c6e7b5 | Michael Hanselmann | 'console': (ConnectToInstanceConsole, ARGS_ONE, |
904 | 51c6e7b5 | Michael Hanselmann | [DEBUG_OPT, |
905 | 51c6e7b5 | Michael Hanselmann | make_option("--show-cmd", dest="show_command", |
906 | 51c6e7b5 | Michael Hanselmann | action="store_true", default=False, |
907 | 51c6e7b5 | Michael Hanselmann | help=("Show command instead of executing it"))], |
908 | 9a033156 | Iustin Pop | "[--show-cmd] <instance>", |
909 | a8083063 | Iustin Pop | "Opens a console on the specified instance"), |
910 | 80de0e3f | Iustin Pop | 'failover': (FailoverInstance, ARGS_ONE, |
911 | fe7b0351 | Michael Hanselmann | [DEBUG_OPT, FORCE_OPT, |
912 | a8083063 | Iustin Pop | make_option("--ignore-consistency", dest="ignore_consistency", |
913 | a8083063 | Iustin Pop | action="store_true", default=False, |
914 | a8083063 | Iustin Pop | help="Ignore the consistency of the disks on" |
915 | a8083063 | Iustin Pop | " the secondary"), |
916 | a8083063 | Iustin Pop | ], |
917 | 9a033156 | Iustin Pop | "[-f] <instance>", |
918 | a8083063 | Iustin Pop | "Stops the instance and starts it on the backup node, using" |
919 | 99e2be3b | Guido Trotter | " the remote mirror (only for instances of type drbd)"), |
920 | 9a033156 | Iustin Pop | 'info': (ShowInstanceConfig, ARGS_ANY, [DEBUG_OPT], "[<instance>...]", |
921 | a8083063 | Iustin Pop | "Show information on the specified instance"), |
922 | a8083063 | Iustin Pop | 'list': (ListInstances, ARGS_NONE, |
923 | 9a033156 | Iustin Pop | [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], "", |
924 | d8052456 | Iustin Pop | "Lists the instances and their status. The available fields are" |
925 | d8052456 | Iustin Pop | " (see the man page for details): status, oper_state, oper_ram," |
926 | d8a4b51d | Iustin Pop | " name, os, pnode, snodes, admin_state, admin_ram, disk_template," |
927 | d8a4b51d | Iustin Pop | " ip, mac, bridge, sda_size, sdb_size, vcpus. The default field" |
928 | 48c4dfa8 | Iustin Pop | " list is (in order): %s." % ", ".join(_LIST_DEF_FIELDS), |
929 | 48c4dfa8 | Iustin Pop | ), |
930 | d0834de3 | Michael Hanselmann | 'reinstall': (ReinstallInstance, ARGS_ONE, [DEBUG_OPT, FORCE_OPT, os_opt], |
931 | 9a033156 | Iustin Pop | "[-f] <instance>", "Reinstall a stopped instance"), |
932 | 1d67656e | Iustin Pop | 'remove': (RemoveInstance, ARGS_ONE, |
933 | 1d67656e | Iustin Pop | [DEBUG_OPT, FORCE_OPT, |
934 | 1d67656e | Iustin Pop | make_option("--ignore-failures", dest="ignore_failures", |
935 | 1d67656e | Iustin Pop | action="store_true", default=False, |
936 | 1d67656e | Iustin Pop | help=("Remove the instance from the cluster even" |
937 | 1d67656e | Iustin Pop | " if there are failures during the removal" |
938 | 1d67656e | Iustin Pop | " process (shutdown, disk removal, etc.)")), |
939 | 1d67656e | Iustin Pop | ], |
940 | 9a033156 | Iustin Pop | "[-f] <instance>", "Shuts down the instance and removes it"), |
941 | decd5f45 | Iustin Pop | 'rename': (RenameInstance, ARGS_FIXED(2), |
942 | decd5f45 | Iustin Pop | [DEBUG_OPT, |
943 | decd5f45 | Iustin Pop | make_option("--no-ip-check", dest="ignore_ip", |
944 | decd5f45 | Iustin Pop | help="Do not check that the IP of the new name" |
945 | decd5f45 | Iustin Pop | " is alive", |
946 | decd5f45 | Iustin Pop | default=False, action="store_true"), |
947 | decd5f45 | Iustin Pop | ], |
948 | 9a033156 | Iustin Pop | "<instance> <new_name>", "Rename the instance"), |
949 | a8083063 | Iustin Pop | 'replace-disks': (ReplaceDisks, ARGS_ONE, |
950 | a8083063 | Iustin Pop | [DEBUG_OPT, |
951 | a8083063 | Iustin Pop | make_option("-n", "--new-secondary", dest="new_secondary", |
952 | a9e0c397 | Iustin Pop | help=("New secondary node (for secondary" |
953 | a9e0c397 | Iustin Pop | " node change)"), metavar="NODE"), |
954 | a9e0c397 | Iustin Pop | make_option("-p", "--on-primary", dest="on_primary", |
955 | a9e0c397 | Iustin Pop | default=False, action="store_true", |
956 | a9e0c397 | Iustin Pop | help=("Replace the disk(s) on the primary" |
957 | 12c3449a | Michael Hanselmann | " node (only for the drbd template)")), |
958 | a9e0c397 | Iustin Pop | make_option("-s", "--on-secondary", dest="on_secondary", |
959 | a9e0c397 | Iustin Pop | default=False, action="store_true", |
960 | a9e0c397 | Iustin Pop | help=("Replace the disk(s) on the secondary" |
961 | 12c3449a | Michael Hanselmann | " node (only for the drbd template)")), |
962 | a9e0c397 | Iustin Pop | make_option("--disks", dest="disks", default=None, |
963 | a9e0c397 | Iustin Pop | help=("Comma-separated list of disks" |
964 | a9e0c397 | Iustin Pop | " to replace (e.g. sda) (optional," |
965 | a9e0c397 | Iustin Pop | " defaults to all disks")), |
966 | b6e82a65 | Iustin Pop | make_option("--iallocator", metavar="<NAME>", |
967 | b6e82a65 | Iustin Pop | help="Select new secondary for the instance" |
968 | b6e82a65 | Iustin Pop | " automatically using the" |
969 | b6e82a65 | Iustin Pop | " <NAME> iallocator plugin (enables" |
970 | b6e82a65 | Iustin Pop | " secondary node replacement)", |
971 | b6e82a65 | Iustin Pop | default=None, type="string"), |
972 | a9e0c397 | Iustin Pop | ], |
973 | 9a033156 | Iustin Pop | "[-s|-p|-n NODE] <instance>", |
974 | a8083063 | Iustin Pop | "Replaces all disks for the instance"), |
975 | 7767bbf5 | Manuel Franceschini | 'modify': (SetInstanceParams, ARGS_ONE, |
976 | fe7b0351 | Michael Hanselmann | [DEBUG_OPT, FORCE_OPT, |
977 | a8083063 | Iustin Pop | cli_option("-m", "--memory", dest="mem", |
978 | a8083063 | Iustin Pop | help="Memory size", |
979 | a8083063 | Iustin Pop | default=None, type="unit", metavar="<mem>"), |
980 | a8083063 | Iustin Pop | make_option("-p", "--cpu", dest="vcpus", |
981 | a8083063 | Iustin Pop | help="Number of virtual CPUs", |
982 | a8083063 | Iustin Pop | default=None, type="int", metavar="<PROC>"), |
983 | a8083063 | Iustin Pop | make_option("-i", "--ip", dest="ip", |
984 | a8083063 | Iustin Pop | help="IP address ('none' or numeric IP)", |
985 | a8083063 | Iustin Pop | default=None, type="string", metavar="<ADDRESS>"), |
986 | a8083063 | Iustin Pop | make_option("-b", "--bridge", dest="bridge", |
987 | a8083063 | Iustin Pop | help="Bridge to connect this instance to", |
988 | decd5f45 | Iustin Pop | default=None, type="string", metavar="<bridge>"), |
989 | 1862d460 | Alexander Schreiber | make_option("--mac", dest="mac", |
990 | 1862d460 | Alexander Schreiber | help="MAC address", default=None, |
991 | 1862d460 | Alexander Schreiber | type="string", metavar="<MACADDRESS>"), |
992 | 973d7867 | Iustin Pop | make_option("--kernel", dest="kernel_path", |
993 | 973d7867 | Iustin Pop | help="Path to the instances' kernel (or" |
994 | 973d7867 | Iustin Pop | " 'default')", default=None, |
995 | 973d7867 | Iustin Pop | type="string", metavar="<FILENAME>"), |
996 | 973d7867 | Iustin Pop | make_option("--initrd", dest="initrd_path", |
997 | 973d7867 | Iustin Pop | help="Path to the instances' initrd (or 'none', or" |
998 | 973d7867 | Iustin Pop | " 'default')", default=None, |
999 | 973d7867 | Iustin Pop | type="string", metavar="<FILENAME>"), |
1000 | 25c5878d | Alexander Schreiber | make_option("--hvm-boot-order", dest="hvm_boot_order", |
1001 | 25c5878d | Alexander Schreiber | help="boot device order for HVM" |
1002 | 25c5878d | Alexander Schreiber | "(either one or more of [acdn] or 'default')", |
1003 | 25c5878d | Alexander Schreiber | default=None, type="string", metavar="<BOOTORDER>"), |
1004 | 31a853d2 | Iustin Pop | make_option("--hvm-acpi", dest="hvm_acpi", |
1005 | 31a853d2 | Iustin Pop | help="ACPI support for HVM (true|false)", |
1006 | 31a853d2 | Iustin Pop | metavar="<BOOL>", choices=["true", "false"]), |
1007 | 31a853d2 | Iustin Pop | make_option("--hvm-pae", dest="hvm_pae", |
1008 | 31a853d2 | Iustin Pop | help="PAE support for HVM (true|false)", |
1009 | 31a853d2 | Iustin Pop | metavar="<BOOL>", choices=["true", "false"]), |
1010 | 31a853d2 | Iustin Pop | make_option("--hvm-cdrom-image-path", |
1011 | 31a853d2 | Iustin Pop | dest="hvm_cdrom_image_path", |
1012 | 31a853d2 | Iustin Pop | help="CDROM image path for HVM" |
1013 | 31a853d2 | Iustin Pop | "(absolute path or None)", |
1014 | 31a853d2 | Iustin Pop | default=None, type="string", metavar="<CDROMIMAGE>"), |
1015 | ad2023a3 | Alexander Schreiber | make_option("--hvm-nic-type", dest="hvm_nic_type", |
1016 | ad2023a3 | Alexander Schreiber | help="Type of virtual NIC for HVM " |
1017 | ad2023a3 | Alexander Schreiber | "(rtl8139,ne2k_pci,ne2k_isa,paravirtual)", |
1018 | ad2023a3 | Alexander Schreiber | metavar="NICTYPE", |
1019 | ad2023a3 | Alexander Schreiber | choices=[constants.HT_HVM_NIC_RTL8139, |
1020 | ad2023a3 | Alexander Schreiber | constants.HT_HVM_NIC_NE2K_PCI, |
1021 | ad2023a3 | Alexander Schreiber | constants.HT_HVM_NIC_NE2K_ISA, |
1022 | ad2023a3 | Alexander Schreiber | constants.HT_HVM_DEV_PARAVIRTUAL], |
1023 | ad2023a3 | Alexander Schreiber | default=None), |
1024 | ad2023a3 | Alexander Schreiber | make_option("--hvm-disk-type", dest="hvm_disk_type", |
1025 | ad2023a3 | Alexander Schreiber | help="Type of virtual disks for HVM " |
1026 | ad2023a3 | Alexander Schreiber | "(ioemu,paravirtual)", |
1027 | ad2023a3 | Alexander Schreiber | metavar="DISKTYPE", |
1028 | ad2023a3 | Alexander Schreiber | choices=[constants.HT_HVM_DEV_IOEMU, |
1029 | ad2023a3 | Alexander Schreiber | constants.HT_HVM_DEV_PARAVIRTUAL], |
1030 | ad2023a3 | Alexander Schreiber | default=None), |
1031 | 31a853d2 | Iustin Pop | make_option("--vnc-bind-address", dest="vnc_bind_address", |
1032 | 31a853d2 | Iustin Pop | help="bind address for VNC (IP address)", |
1033 | 31a853d2 | Iustin Pop | default=None, type="string", metavar="<VNCADDRESS>"), |
1034 | a8083063 | Iustin Pop | ], |
1035 | 9a033156 | Iustin Pop | "<instance>", "Alters the parameters of an instance"), |
1036 | 312ac745 | Iustin Pop | 'shutdown': (ShutdownInstance, ARGS_ANY, |
1037 | 312ac745 | Iustin Pop | [DEBUG_OPT, m_node_opt, m_pri_node_opt, m_sec_node_opt, |
1038 | 804a1e8e | Iustin Pop | m_clust_opt, m_inst_opt, m_force_multi], |
1039 | 9a033156 | Iustin Pop | "<instance>", "Stops an instance"), |
1040 | 312ac745 | Iustin Pop | 'startup': (StartupInstance, ARGS_ANY, |
1041 | 804a1e8e | Iustin Pop | [DEBUG_OPT, FORCE_OPT, m_force_multi, |
1042 | a8083063 | Iustin Pop | make_option("-e", "--extra", dest="extra_args", |
1043 | a8083063 | Iustin Pop | help="Extra arguments for the instance's kernel", |
1044 | a8083063 | Iustin Pop | default=None, type="string", metavar="<PARAMS>"), |
1045 | 312ac745 | Iustin Pop | m_node_opt, m_pri_node_opt, m_sec_node_opt, |
1046 | 312ac745 | Iustin Pop | m_clust_opt, m_inst_opt, |
1047 | a8083063 | Iustin Pop | ], |
1048 | 9a033156 | Iustin Pop | "<instance>", "Starts an instance"), |
1049 | 579d4337 | Alexander Schreiber | |
1050 | 579d4337 | Alexander Schreiber | 'reboot': (RebootInstance, ARGS_ANY, |
1051 | 579d4337 | Alexander Schreiber | [DEBUG_OPT, m_force_multi, |
1052 | 579d4337 | Alexander Schreiber | make_option("-e", "--extra", dest="extra_args", |
1053 | 579d4337 | Alexander Schreiber | help="Extra arguments for the instance's kernel", |
1054 | 579d4337 | Alexander Schreiber | default=None, type="string", metavar="<PARAMS>"), |
1055 | 579d4337 | Alexander Schreiber | make_option("-t", "--type", dest="reboot_type", |
1056 | 579d4337 | Alexander Schreiber | help="Type of reboot: soft/hard/full", |
1057 | 579d4337 | Alexander Schreiber | default=constants.INSTANCE_REBOOT_SOFT, |
1058 | 579d4337 | Alexander Schreiber | type="string", metavar="<REBOOT>"), |
1059 | 579d4337 | Alexander Schreiber | make_option("--ignore-secondaries", dest="ignore_secondaries", |
1060 | 579d4337 | Alexander Schreiber | default=False, action="store_true", |
1061 | 579d4337 | Alexander Schreiber | help="Ignore errors from secondaries"), |
1062 | 579d4337 | Alexander Schreiber | m_node_opt, m_pri_node_opt, m_sec_node_opt, |
1063 | 579d4337 | Alexander Schreiber | m_clust_opt, m_inst_opt, |
1064 | 579d4337 | Alexander Schreiber | ], |
1065 | 9a033156 | Iustin Pop | "<instance>", "Reboots an instance"), |
1066 | a8083063 | Iustin Pop | 'activate-disks': (ActivateDisks, ARGS_ONE, [DEBUG_OPT], |
1067 | 9a033156 | Iustin Pop | "<instance>", |
1068 | a8083063 | Iustin Pop | "Activate an instance's disks"), |
1069 | a8083063 | Iustin Pop | 'deactivate-disks': (DeactivateDisks, ARGS_ONE, [DEBUG_OPT], |
1070 | 9a033156 | Iustin Pop | "<instance>", |
1071 | a8083063 | Iustin Pop | "Deactivate an instance's disks"), |
1072 | c6e911bc | Iustin Pop | 'grow-disk': (GrowDisk, ARGS_FIXED(3), [DEBUG_OPT], |
1073 | c6e911bc | Iustin Pop | "<instance> <disk> <size>", "Grow an instance's disk"), |
1074 | 846baef9 | Iustin Pop | 'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT], |
1075 | 9a033156 | Iustin Pop | "<node_name>", "List the tags of the given instance"), |
1076 | 810c50b7 | Iustin Pop | 'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT], |
1077 | 9a033156 | Iustin Pop | "<node_name> tag...", "Add tags to the given instance"), |
1078 | 810c50b7 | Iustin Pop | 'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT], |
1079 | 9a033156 | Iustin Pop | "<node_name> tag...", "Remove tags from given instance"), |
1080 | a8083063 | Iustin Pop | } |
1081 | a8083063 | Iustin Pop | |
1082 | dbfd89dd | Guido Trotter | aliases = { |
1083 | dbfd89dd | Guido Trotter | 'activate_block_devs': 'activate-disks', |
1084 | 00ce8b29 | Guido Trotter | 'replace_disks': 'replace-disks', |
1085 | 536fda25 | Guido Trotter | 'start': 'startup', |
1086 | 536fda25 | Guido Trotter | 'stop': 'shutdown', |
1087 | dbfd89dd | Guido Trotter | } |
1088 | dbfd89dd | Guido Trotter | |
1089 | a8083063 | Iustin Pop | if __name__ == '__main__': |
1090 | dbfd89dd | Guido Trotter | sys.exit(GenericMain(commands, aliases=aliases, |
1091 | 846baef9 | Iustin Pop | override={"tag_type": constants.TAG_INSTANCE})) |