Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-instance @ 20e23543

History | View | Annotate | Download (42.4 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 20e23543 Alexander Schreiber
  if opts.select_os is True:
307 20e23543 Alexander Schreiber
    op = opcodes.OpDiagnoseOS(output_fields=["name", "valid"], names=[])
308 20e23543 Alexander Schreiber
    result = SubmitOpCode(op)
309 20e23543 Alexander Schreiber
310 20e23543 Alexander Schreiber
    if not result:
311 20e23543 Alexander Schreiber
      logger.ToStdout("Can't get the OS list")
312 20e23543 Alexander Schreiber
      return 1
313 20e23543 Alexander Schreiber
314 20e23543 Alexander Schreiber
    logger.ToStdout("Available OS templates:")
315 20e23543 Alexander Schreiber
    number = 0
316 20e23543 Alexander Schreiber
    choices = []
317 20e23543 Alexander Schreiber
    for entry in result:
318 20e23543 Alexander Schreiber
      logger.ToStdout("%3s: %s" % (number, entry[0]))
319 20e23543 Alexander Schreiber
      choices.append(("%s" % number, entry[0], entry[0]))
320 20e23543 Alexander Schreiber
      number = number + 1
321 20e23543 Alexander Schreiber
322 20e23543 Alexander Schreiber
    choices.append(('x', 'exit', 'Exit gnt-instance reinstall'))
323 20e23543 Alexander Schreiber
    selected = AskUser("Enter OS template name or number (or x to abort):",
324 20e23543 Alexander Schreiber
                       choices)
325 20e23543 Alexander Schreiber
326 20e23543 Alexander Schreiber
    if selected == 'exit':
327 20e23543 Alexander Schreiber
      logger.ToStdout("User aborted reinstall, exiting")
328 20e23543 Alexander Schreiber
      return 1
329 20e23543 Alexander Schreiber
330 20e23543 Alexander Schreiber
    os = selected
331 20e23543 Alexander Schreiber
  else:
332 20e23543 Alexander Schreiber
    os = opts.os
333 20e23543 Alexander Schreiber
334 fe7b0351 Michael Hanselmann
  if not opts.force:
335 f4bc1f2c Michael Hanselmann
    usertext = ("This will reinstall the instance %s and remove"
336 f4bc1f2c Michael Hanselmann
                " all data. Continue?") % instance_name
337 47988778 Iustin Pop
    if not AskUser(usertext):
338 fe7b0351 Michael Hanselmann
      return 1
339 fe7b0351 Michael Hanselmann
340 d0834de3 Michael Hanselmann
  op = opcodes.OpReinstallInstance(instance_name=instance_name,
341 20e23543 Alexander Schreiber
                                   os_type=os)
342 fe7b0351 Michael Hanselmann
  SubmitOpCode(op)
343 fe7b0351 Michael Hanselmann
344 fe7b0351 Michael Hanselmann
  return 0
345 fe7b0351 Michael Hanselmann
346 fe7b0351 Michael Hanselmann
347 a8083063 Iustin Pop
def RemoveInstance(opts, args):
348 a8083063 Iustin Pop
  """Remove an instance.
349 a8083063 Iustin Pop
350 a8083063 Iustin Pop
  Args:
351 a8083063 Iustin Pop
    opts - class with options as members
352 a8083063 Iustin Pop
    args - list containing a single element, the instance name
353 a8083063 Iustin Pop
354 a8083063 Iustin Pop
  """
355 a8083063 Iustin Pop
  instance_name = args[0]
356 a8083063 Iustin Pop
  force = opts.force
357 a8083063 Iustin Pop
358 a8083063 Iustin Pop
  if not force:
359 a8083063 Iustin Pop
    usertext = ("This will remove the volumes of the instance %s"
360 a8083063 Iustin Pop
                " (including mirrors), thus removing all the data"
361 a8083063 Iustin Pop
                " of the instance. Continue?") % instance_name
362 47988778 Iustin Pop
    if not AskUser(usertext):
363 a8083063 Iustin Pop
      return 1
364 a8083063 Iustin Pop
365 1d67656e Iustin Pop
  op = opcodes.OpRemoveInstance(instance_name=instance_name,
366 1d67656e Iustin Pop
                                ignore_failures=opts.ignore_failures)
367 a8083063 Iustin Pop
  SubmitOpCode(op)
368 a8083063 Iustin Pop
  return 0
369 a8083063 Iustin Pop
370 a8083063 Iustin Pop
371 decd5f45 Iustin Pop
def RenameInstance(opts, args):
372 4ab0b9e3 Guido Trotter
  """Rename an instance.
373 decd5f45 Iustin Pop
374 decd5f45 Iustin Pop
  Args:
375 decd5f45 Iustin Pop
    opts - class with options as members
376 decd5f45 Iustin Pop
    args - list containing two elements, the instance name and the new name
377 decd5f45 Iustin Pop
378 decd5f45 Iustin Pop
  """
379 decd5f45 Iustin Pop
  op = opcodes.OpRenameInstance(instance_name=args[0],
380 decd5f45 Iustin Pop
                                new_name=args[1],
381 decd5f45 Iustin Pop
                                ignore_ip=opts.ignore_ip)
382 decd5f45 Iustin Pop
  SubmitOpCode(op)
383 decd5f45 Iustin Pop
384 decd5f45 Iustin Pop
  return 0
385 decd5f45 Iustin Pop
386 decd5f45 Iustin Pop
387 a8083063 Iustin Pop
def ActivateDisks(opts, args):
388 a8083063 Iustin Pop
  """Activate an instance's disks.
389 a8083063 Iustin Pop
390 a8083063 Iustin Pop
  This serves two purposes:
391 a8083063 Iustin Pop
    - it allows one (as long as the instance is not running) to mount
392 a8083063 Iustin Pop
    the disks and modify them from the node
393 a8083063 Iustin Pop
    - it repairs inactive secondary drbds
394 a8083063 Iustin Pop
395 a8083063 Iustin Pop
  """
396 a8083063 Iustin Pop
  instance_name = args[0]
397 a8083063 Iustin Pop
  op = opcodes.OpActivateInstanceDisks(instance_name=instance_name)
398 a8083063 Iustin Pop
  disks_info = SubmitOpCode(op)
399 a8083063 Iustin Pop
  for host, iname, nname in disks_info:
400 a8083063 Iustin Pop
    print "%s:%s:%s" % (host, iname, nname)
401 a8083063 Iustin Pop
  return 0
402 a8083063 Iustin Pop
403 a8083063 Iustin Pop
404 a8083063 Iustin Pop
def DeactivateDisks(opts, args):
405 a8083063 Iustin Pop
  """Command-line interface for _ShutdownInstanceBlockDevices.
406 a8083063 Iustin Pop
407 a8083063 Iustin Pop
  This function takes the instance name, looks for its primary node
408 a8083063 Iustin Pop
  and the tries to shutdown its block devices on that node.
409 a8083063 Iustin Pop
410 a8083063 Iustin Pop
  """
411 a8083063 Iustin Pop
  instance_name = args[0]
412 a8083063 Iustin Pop
  op = opcodes.OpDeactivateInstanceDisks(instance_name=instance_name)
413 a8083063 Iustin Pop
  SubmitOpCode(op)
414 a8083063 Iustin Pop
  return 0
415 a8083063 Iustin Pop
416 a8083063 Iustin Pop
417 c6e911bc Iustin Pop
def GrowDisk(opts, args):
418 c6e911bc Iustin Pop
  """Command-line interface for _ShutdownInstanceBlockDevices.
419 c6e911bc Iustin Pop
420 c6e911bc Iustin Pop
  This function takes the instance name, looks for its primary node
421 c6e911bc Iustin Pop
  and the tries to shutdown its block devices on that node.
422 c6e911bc Iustin Pop
423 c6e911bc Iustin Pop
  """
424 c6e911bc Iustin Pop
  instance = args[0]
425 c6e911bc Iustin Pop
  disk = args[1]
426 c6e911bc Iustin Pop
  amount = utils.ParseUnit(args[2])
427 c6e911bc Iustin Pop
  op = opcodes.OpGrowDisk(instance_name=instance, disk=disk, amount=amount)
428 c6e911bc Iustin Pop
  SubmitOpCode(op)
429 c6e911bc Iustin Pop
  return 0
430 c6e911bc Iustin Pop
431 c6e911bc Iustin Pop
432 a8083063 Iustin Pop
def StartupInstance(opts, args):
433 decd5f45 Iustin Pop
  """Startup an instance.
434 a8083063 Iustin Pop
435 a8083063 Iustin Pop
  Args:
436 a8083063 Iustin Pop
    opts - class with options as members
437 a8083063 Iustin Pop
    args - list containing a single element, the instance name
438 a8083063 Iustin Pop
439 a8083063 Iustin Pop
  """
440 312ac745 Iustin Pop
  if opts.multi_mode is None:
441 312ac745 Iustin Pop
    opts.multi_mode = _SHUTDOWN_INSTANCES
442 312ac745 Iustin Pop
  inames = _ExpandMultiNames(opts.multi_mode, args)
443 bcee9cb4 Iustin Pop
  if not inames:
444 bcee9cb4 Iustin Pop
    raise errors.OpPrereqError("Selection filter does not match any instances")
445 804a1e8e Iustin Pop
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
446 804a1e8e Iustin Pop
  if not (opts.force_multi or not multi_on
447 804a1e8e Iustin Pop
          or _ConfirmOperation(inames, "startup")):
448 804a1e8e Iustin Pop
    return 1
449 312ac745 Iustin Pop
  for name in inames:
450 312ac745 Iustin Pop
    op = opcodes.OpStartupInstance(instance_name=name,
451 312ac745 Iustin Pop
                                   force=opts.force,
452 312ac745 Iustin Pop
                                   extra_args=opts.extra_args)
453 312ac745 Iustin Pop
    if multi_on:
454 312ac745 Iustin Pop
      logger.ToStdout("Starting up %s" % name)
455 312ac745 Iustin Pop
    SubmitOpCode(op)
456 a8083063 Iustin Pop
  return 0
457 a8083063 Iustin Pop
458 7c0d6283 Michael Hanselmann
459 579d4337 Alexander Schreiber
def RebootInstance(opts, args):
460 579d4337 Alexander Schreiber
  """Reboot an instance
461 579d4337 Alexander Schreiber
462 579d4337 Alexander Schreiber
  Args:
463 579d4337 Alexander Schreiber
    opts - class with options as members
464 579d4337 Alexander Schreiber
    args - list containing a single element, the instance name
465 579d4337 Alexander Schreiber
466 579d4337 Alexander Schreiber
  """
467 579d4337 Alexander Schreiber
  if opts.multi_mode is None:
468 579d4337 Alexander Schreiber
    opts.multi_mode = _SHUTDOWN_INSTANCES
469 579d4337 Alexander Schreiber
  inames = _ExpandMultiNames(opts.multi_mode, args)
470 bcee9cb4 Iustin Pop
  if not inames:
471 bcee9cb4 Iustin Pop
    raise errors.OpPrereqError("Selection filter does not match any instances")
472 579d4337 Alexander Schreiber
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
473 579d4337 Alexander Schreiber
  if not (opts.force_multi or not multi_on
474 579d4337 Alexander Schreiber
          or _ConfirmOperation(inames, "reboot")):
475 579d4337 Alexander Schreiber
    return 1
476 579d4337 Alexander Schreiber
  for name in inames:
477 579d4337 Alexander Schreiber
    op = opcodes.OpRebootInstance(instance_name=name,
478 579d4337 Alexander Schreiber
                                  reboot_type=opts.reboot_type,
479 579d4337 Alexander Schreiber
                                  ignore_secondaries=opts.ignore_secondaries)
480 579d4337 Alexander Schreiber
481 579d4337 Alexander Schreiber
    SubmitOpCode(op)
482 579d4337 Alexander Schreiber
  return 0
483 a8083063 Iustin Pop
484 7c0d6283 Michael Hanselmann
485 a8083063 Iustin Pop
def ShutdownInstance(opts, args):
486 a8083063 Iustin Pop
  """Shutdown an instance.
487 a8083063 Iustin Pop
488 a8083063 Iustin Pop
  Args:
489 a8083063 Iustin Pop
    opts - class with options as members
490 a8083063 Iustin Pop
    args - list containing a single element, the instance name
491 a8083063 Iustin Pop
492 a8083063 Iustin Pop
  """
493 312ac745 Iustin Pop
  if opts.multi_mode is None:
494 312ac745 Iustin Pop
    opts.multi_mode = _SHUTDOWN_INSTANCES
495 312ac745 Iustin Pop
  inames = _ExpandMultiNames(opts.multi_mode, args)
496 bcee9cb4 Iustin Pop
  if not inames:
497 bcee9cb4 Iustin Pop
    raise errors.OpPrereqError("Selection filter does not match any instances")
498 804a1e8e Iustin Pop
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
499 804a1e8e Iustin Pop
  if not (opts.force_multi or not multi_on
500 804a1e8e Iustin Pop
          or _ConfirmOperation(inames, "shutdown")):
501 804a1e8e Iustin Pop
    return 1
502 312ac745 Iustin Pop
  for name in inames:
503 312ac745 Iustin Pop
    op = opcodes.OpShutdownInstance(instance_name=name)
504 312ac745 Iustin Pop
    if multi_on:
505 312ac745 Iustin Pop
      logger.ToStdout("Shutting down %s" % name)
506 312ac745 Iustin Pop
    SubmitOpCode(op)
507 a8083063 Iustin Pop
  return 0
508 a8083063 Iustin Pop
509 a8083063 Iustin Pop
510 a8083063 Iustin Pop
def ReplaceDisks(opts, args):
511 a8083063 Iustin Pop
  """Replace the disks of an instance
512 a8083063 Iustin Pop
513 a8083063 Iustin Pop
  Args:
514 a8083063 Iustin Pop
    opts - class with options as members
515 a8083063 Iustin Pop
    args - list with a single element, the instance name
516 a8083063 Iustin Pop
517 a8083063 Iustin Pop
  """
518 a8083063 Iustin Pop
  instance_name = args[0]
519 a9e0c397 Iustin Pop
  new_2ndary = opts.new_secondary
520 b6e82a65 Iustin Pop
  iallocator = opts.iallocator
521 a9e0c397 Iustin Pop
  if opts.disks is None:
522 a9e0c397 Iustin Pop
    disks = ["sda", "sdb"]
523 a9e0c397 Iustin Pop
  else:
524 a9e0c397 Iustin Pop
    disks = opts.disks.split(",")
525 a9e0c397 Iustin Pop
  if opts.on_primary == opts.on_secondary: # no -p or -s passed, or both passed
526 a9e0c397 Iustin Pop
    mode = constants.REPLACE_DISK_ALL
527 a9e0c397 Iustin Pop
  elif opts.on_primary: # only on primary:
528 a9e0c397 Iustin Pop
    mode = constants.REPLACE_DISK_PRI
529 b6e82a65 Iustin Pop
    if new_2ndary is not None or iallocator is not None:
530 a9e0c397 Iustin Pop
      raise errors.OpPrereqError("Can't change secondary node on primary disk"
531 a9e0c397 Iustin Pop
                                 " replacement")
532 b6e82a65 Iustin Pop
  elif opts.on_secondary is not None or iallocator is not None:
533 b6e82a65 Iustin Pop
    # only on secondary
534 a9e0c397 Iustin Pop
    mode = constants.REPLACE_DISK_SEC
535 a9e0c397 Iustin Pop
536 a9e0c397 Iustin Pop
  op = opcodes.OpReplaceDisks(instance_name=args[0], disks=disks,
537 b6e82a65 Iustin Pop
                              remote_node=new_2ndary, mode=mode,
538 b6e82a65 Iustin Pop
                              iallocator=iallocator)
539 a8083063 Iustin Pop
  SubmitOpCode(op)
540 a8083063 Iustin Pop
  return 0
541 a8083063 Iustin Pop
542 a8083063 Iustin Pop
543 a8083063 Iustin Pop
def FailoverInstance(opts, args):
544 a8083063 Iustin Pop
  """Failover an instance.
545 a8083063 Iustin Pop
546 a8083063 Iustin Pop
  The failover is done by shutting it down on its present node and
547 a8083063 Iustin Pop
  starting it on the secondary.
548 a8083063 Iustin Pop
549 a8083063 Iustin Pop
  Args:
550 a8083063 Iustin Pop
    opts - class with options as members
551 a8083063 Iustin Pop
    args - list with a single element, the instance name
552 a8083063 Iustin Pop
  Opts used:
553 a8083063 Iustin Pop
    force - whether to failover without asking questions.
554 a8083063 Iustin Pop
555 a8083063 Iustin Pop
  """
556 80de0e3f Iustin Pop
  instance_name = args[0]
557 80de0e3f Iustin Pop
  force = opts.force
558 a8083063 Iustin Pop
559 80de0e3f Iustin Pop
  if not force:
560 80de0e3f Iustin Pop
    usertext = ("Failover will happen to image %s."
561 80de0e3f Iustin Pop
                " This requires a shutdown of the instance. Continue?" %
562 80de0e3f Iustin Pop
                (instance_name,))
563 80de0e3f Iustin Pop
    if not AskUser(usertext):
564 80de0e3f Iustin Pop
      return 1
565 a8083063 Iustin Pop
566 80de0e3f Iustin Pop
  op = opcodes.OpFailoverInstance(instance_name=instance_name,
567 80de0e3f Iustin Pop
                                  ignore_consistency=opts.ignore_consistency)
568 80de0e3f Iustin Pop
  SubmitOpCode(op)
569 80de0e3f Iustin Pop
  return 0
570 a8083063 Iustin Pop
571 a8083063 Iustin Pop
572 a8083063 Iustin Pop
def ConnectToInstanceConsole(opts, args):
573 a8083063 Iustin Pop
  """Connect to the console of an instance.
574 a8083063 Iustin Pop
575 a8083063 Iustin Pop
  Args:
576 a8083063 Iustin Pop
    opts - class with options as members
577 a8083063 Iustin Pop
    args - list with a single element, the instance name
578 a8083063 Iustin Pop
579 a8083063 Iustin Pop
  """
580 a8083063 Iustin Pop
  instance_name = args[0]
581 a8083063 Iustin Pop
582 a8083063 Iustin Pop
  op = opcodes.OpConnectConsole(instance_name=instance_name)
583 0a80a26f Michael Hanselmann
  cmd = SubmitOpCode(op)
584 51c6e7b5 Michael Hanselmann
585 51c6e7b5 Michael Hanselmann
  if opts.show_command:
586 51c6e7b5 Michael Hanselmann
    print utils.ShellQuoteArgs(cmd)
587 51c6e7b5 Michael Hanselmann
  else:
588 51c6e7b5 Michael Hanselmann
    try:
589 51c6e7b5 Michael Hanselmann
      os.execvp(cmd[0], cmd)
590 51c6e7b5 Michael Hanselmann
    finally:
591 51c6e7b5 Michael Hanselmann
      sys.stderr.write("Can't run console command %s with arguments:\n'%s'" %
592 51c6e7b5 Michael Hanselmann
                       (cmd, " ".join(argv)))
593 51c6e7b5 Michael Hanselmann
      os._exit(1)
594 a8083063 Iustin Pop
595 a8083063 Iustin Pop
596 a8083063 Iustin Pop
def _FormatBlockDevInfo(buf, dev, indent_level):
597 a8083063 Iustin Pop
  """Show block device information.
598 a8083063 Iustin Pop
599 a8083063 Iustin Pop
  This is only used by ShowInstanceConfig(), but it's too big to be
600 a8083063 Iustin Pop
  left for an inline definition.
601 a8083063 Iustin Pop
602 a8083063 Iustin Pop
  """
603 a8083063 Iustin Pop
  def helper(buf, dtype, status):
604 f2e9e0e8 Alexander Schreiber
    """Format one line for physical device status."""
605 a8083063 Iustin Pop
    if not status:
606 a8083063 Iustin Pop
      buf.write("not active\n")
607 a8083063 Iustin Pop
    else:
608 0834c866 Iustin Pop
      (path, major, minor, syncp, estt, degr, ldisk) = status
609 fd38ef95 Manuel Franceschini
      if major is None:
610 fd38ef95 Manuel Franceschini
        major_string = "N/A"
611 fd38ef95 Manuel Franceschini
      else:
612 fd38ef95 Manuel Franceschini
        major_string = str(major)
613 fd38ef95 Manuel Franceschini
614 fd38ef95 Manuel Franceschini
      if minor is None:
615 fd38ef95 Manuel Franceschini
        minor_string = "N/A"
616 fd38ef95 Manuel Franceschini
      else:
617 fd38ef95 Manuel Franceschini
        minor_string = str(minor)
618 fd38ef95 Manuel Franceschini
619 fd38ef95 Manuel Franceschini
      buf.write("%s (%s:%s)" % (path, major_string, minor_string))
620 00fb8246 Michael Hanselmann
      if dtype in (constants.LD_DRBD8, ):
621 a8083063 Iustin Pop
        if syncp is not None:
622 a8083063 Iustin Pop
          sync_text = "*RECOVERING* %5.2f%%," % syncp
623 a8083063 Iustin Pop
          if estt:
624 a8083063 Iustin Pop
            sync_text += " ETA %ds" % estt
625 a8083063 Iustin Pop
          else:
626 a8083063 Iustin Pop
            sync_text += " ETA unknown"
627 a8083063 Iustin Pop
        else:
628 a8083063 Iustin Pop
          sync_text = "in sync"
629 a8083063 Iustin Pop
        if degr:
630 a8083063 Iustin Pop
          degr_text = "*DEGRADED*"
631 a8083063 Iustin Pop
        else:
632 a8083063 Iustin Pop
          degr_text = "ok"
633 0834c866 Iustin Pop
        if ldisk:
634 0834c866 Iustin Pop
          ldisk_text = " *MISSING DISK*"
635 0834c866 Iustin Pop
        else:
636 0834c866 Iustin Pop
          ldisk_text = ""
637 0834c866 Iustin Pop
        buf.write(" %s, status %s%s" % (sync_text, degr_text, ldisk_text))
638 9db6dbce Iustin Pop
      elif dtype == constants.LD_LV:
639 0834c866 Iustin Pop
        if ldisk:
640 0834c866 Iustin Pop
          ldisk_text = " *FAILED* (failed drive?)"
641 9db6dbce Iustin Pop
        else:
642 0834c866 Iustin Pop
          ldisk_text = ""
643 0834c866 Iustin Pop
        buf.write(ldisk_text)
644 a8083063 Iustin Pop
      buf.write("\n")
645 a8083063 Iustin Pop
646 a8083063 Iustin Pop
  if dev["iv_name"] is not None:
647 a8083063 Iustin Pop
    data = "  - %s, " % dev["iv_name"]
648 a8083063 Iustin Pop
  else:
649 a8083063 Iustin Pop
    data = "  - "
650 a8083063 Iustin Pop
  data += "type: %s" % dev["dev_type"]
651 a8083063 Iustin Pop
  if dev["logical_id"] is not None:
652 a8083063 Iustin Pop
    data += ", logical_id: %s" % (dev["logical_id"],)
653 a8083063 Iustin Pop
  elif dev["physical_id"] is not None:
654 a8083063 Iustin Pop
    data += ", physical_id: %s" % (dev["physical_id"],)
655 a8083063 Iustin Pop
  buf.write("%*s%s\n" % (2*indent_level, "", data))
656 a8083063 Iustin Pop
  buf.write("%*s    primary:   " % (2*indent_level, ""))
657 a8083063 Iustin Pop
  helper(buf, dev["dev_type"], dev["pstatus"])
658 a8083063 Iustin Pop
659 a8083063 Iustin Pop
  if dev["sstatus"]:
660 a8083063 Iustin Pop
    buf.write("%*s    secondary: " % (2*indent_level, ""))
661 a8083063 Iustin Pop
    helper(buf, dev["dev_type"], dev["sstatus"])
662 a8083063 Iustin Pop
663 a8083063 Iustin Pop
  if dev["children"]:
664 a8083063 Iustin Pop
    for child in dev["children"]:
665 a8083063 Iustin Pop
      _FormatBlockDevInfo(buf, child, indent_level+1)
666 a8083063 Iustin Pop
667 a8083063 Iustin Pop
668 a8083063 Iustin Pop
def ShowInstanceConfig(opts, args):
669 a8083063 Iustin Pop
  """Compute instance run-time status.
670 a8083063 Iustin Pop
671 a8083063 Iustin Pop
  """
672 a8083063 Iustin Pop
  retcode = 0
673 a8083063 Iustin Pop
  op = opcodes.OpQueryInstanceData(instances=args)
674 a8083063 Iustin Pop
  result = SubmitOpCode(op)
675 a8340917 Iustin Pop
  hvm_parameters = ("hvm_acpi", "hvm_pae", "hvm_cdrom_image_path",
676 ad2023a3 Alexander Schreiber
                    "hvm_boot_order", "hvm_nic_type", "hvm_disk_type")
677 a8340917 Iustin Pop
678 a8340917 Iustin Pop
  pvm_parameters = ("kernel_path", "initrd_path")
679 a8083063 Iustin Pop
680 a8083063 Iustin Pop
  if not result:
681 a8083063 Iustin Pop
    logger.ToStdout("No instances.")
682 a8083063 Iustin Pop
    return 1
683 a8083063 Iustin Pop
684 a8083063 Iustin Pop
  buf = StringIO()
685 a8083063 Iustin Pop
  retcode = 0
686 a8083063 Iustin Pop
  for instance_name in result:
687 a8083063 Iustin Pop
    instance = result[instance_name]
688 a8083063 Iustin Pop
    buf.write("Instance name: %s\n" % instance["name"])
689 a8083063 Iustin Pop
    buf.write("State: configured to be %s, actual state is %s\n" %
690 a8083063 Iustin Pop
              (instance["config_state"], instance["run_state"]))
691 a8083063 Iustin Pop
    buf.write("  Nodes:\n")
692 a8083063 Iustin Pop
    buf.write("    - primary: %s\n" % instance["pnode"])
693 a8083063 Iustin Pop
    buf.write("    - secondaries: %s\n" % ", ".join(instance["snodes"]))
694 a8083063 Iustin Pop
    buf.write("  Operating system: %s\n" % instance["os"])
695 a8340917 Iustin Pop
    if instance.has_key("network_port"):
696 a8340917 Iustin Pop
      buf.write("  Allocated network port: %s\n" % instance["network_port"])
697 a8340917 Iustin Pop
    if False not in map(instance.has_key, pvm_parameters):
698 a8340917 Iustin Pop
      if instance["kernel_path"] in (None, constants.VALUE_DEFAULT):
699 a8340917 Iustin Pop
        kpath = "(default: %s)" % constants.XEN_KERNEL
700 a8340917 Iustin Pop
      else:
701 a8340917 Iustin Pop
        kpath = instance["kernel_path"]
702 a8340917 Iustin Pop
      buf.write("  Kernel path: %s\n" % kpath)
703 a8340917 Iustin Pop
      if instance["initrd_path"] in (None, constants.VALUE_DEFAULT):
704 a8340917 Iustin Pop
        initrd = "(default: %s)" % constants.XEN_INITRD
705 a8340917 Iustin Pop
      elif instance["initrd_path"] == constants.VALUE_NONE:
706 a8340917 Iustin Pop
        initrd = "(none)"
707 a8340917 Iustin Pop
      else:
708 a8340917 Iustin Pop
        initrd = instance["initrd_path"]
709 a8340917 Iustin Pop
      buf.write("       initrd: %s\n" % initrd)
710 a8340917 Iustin Pop
    if False not in map(instance.has_key, hvm_parameters):
711 a8340917 Iustin Pop
      buf.write("  HVM:\n")
712 a8340917 Iustin Pop
      buf.write("    - boot order: %s\n" % instance["hvm_boot_order"])
713 a8340917 Iustin Pop
      buf.write("    - ACPI support: %s\n" % instance["hvm_acpi"])
714 a8340917 Iustin Pop
      buf.write("    - PAE support: %s\n" % instance["hvm_pae"])
715 a8340917 Iustin Pop
      buf.write("    - virtual CDROM: %s\n" % instance["hvm_cdrom_image_path"])
716 ad2023a3 Alexander Schreiber
      buf.write("    - virtual NIC type: %s\n" %  instance["hvm_nic_type"])
717 ad2023a3 Alexander Schreiber
      buf.write("    - virtual disk type: %s\n" %  instance["hvm_disk_type"])
718 a8340917 Iustin Pop
    if instance.has_key("vnc_bind_address"):
719 a8340917 Iustin Pop
      buf.write("  VNC bind address: %s\n" % instance["vnc_bind_address"])
720 34b6ab97 Alexander Schreiber
      buf.write("  VNC console port: %s\n" % instance["vnc_console_port"])
721 a8083063 Iustin Pop
    buf.write("  Hardware:\n")
722 f55ff7ec Iustin Pop
    buf.write("    - VCPUs: %d\n" % instance["vcpus"])
723 a8083063 Iustin Pop
    buf.write("    - memory: %dMiB\n" % instance["memory"])
724 a8083063 Iustin Pop
    buf.write("    - NICs: %s\n" %
725 a8083063 Iustin Pop
        ", ".join(["{MAC: %s, IP: %s, bridge: %s}" %
726 a8083063 Iustin Pop
                   (mac, ip, bridge)
727 a8083063 Iustin Pop
                     for mac, ip, bridge in instance["nics"]]))
728 a8083063 Iustin Pop
    buf.write("  Block devices:\n")
729 a8083063 Iustin Pop
730 a8083063 Iustin Pop
    for device in instance["disks"]:
731 a8083063 Iustin Pop
      _FormatBlockDevInfo(buf, device, 1)
732 a8083063 Iustin Pop
733 a8083063 Iustin Pop
  logger.ToStdout(buf.getvalue().rstrip('\n'))
734 a8083063 Iustin Pop
  return retcode
735 a8083063 Iustin Pop
736 a8083063 Iustin Pop
737 7767bbf5 Manuel Franceschini
def SetInstanceParams(opts, args):
738 a8083063 Iustin Pop
  """Modifies an instance.
739 a8083063 Iustin Pop
740 a8083063 Iustin Pop
  All parameters take effect only at the next restart of the instance.
741 a8083063 Iustin Pop
742 a8083063 Iustin Pop
  Args:
743 a8083063 Iustin Pop
    opts - class with options as members
744 a8083063 Iustin Pop
    args - list with a single element, the instance name
745 a8083063 Iustin Pop
  Opts used:
746 a8083063 Iustin Pop
    memory - the new memory size
747 a8083063 Iustin Pop
    vcpus - the new number of cpus
748 1862d460 Alexander Schreiber
    mac - the new MAC address of the instance
749 a8083063 Iustin Pop
750 a8083063 Iustin Pop
  """
751 973d7867 Iustin Pop
  if not (opts.mem or opts.vcpus or opts.ip or opts.bridge or opts.mac or
752 31a853d2 Iustin Pop
          opts.kernel_path or opts.initrd_path or opts.hvm_boot_order or
753 917f91d5 Iustin Pop
          opts.hvm_acpi or opts.hvm_pae or opts.hvm_cdrom_image_path or
754 ad2023a3 Alexander Schreiber
          opts.vnc_bind_address or opts.hvm_nic_type or opts.hvm_disk_type):
755 a8083063 Iustin Pop
    logger.ToStdout("Please give at least one of the parameters.")
756 a8083063 Iustin Pop
    return 1
757 a8083063 Iustin Pop
758 973d7867 Iustin Pop
  kernel_path = _TransformPath(opts.kernel_path)
759 973d7867 Iustin Pop
  initrd_path = _TransformPath(opts.initrd_path)
760 40cece88 Iustin Pop
  if opts.hvm_boot_order == 'default':
761 25c5878d Alexander Schreiber
    hvm_boot_order = constants.VALUE_DEFAULT
762 25c5878d Alexander Schreiber
  else:
763 25c5878d Alexander Schreiber
    hvm_boot_order = opts.hvm_boot_order
764 973d7867 Iustin Pop
765 3fc175f0 Alexander Schreiber
  if opts.hvm_acpi is None:
766 3fc175f0 Alexander Schreiber
    hvm_acpi = opts.hvm_acpi
767 3fc175f0 Alexander Schreiber
  else:
768 3fc175f0 Alexander Schreiber
    hvm_acpi = opts.hvm_acpi == _VALUE_TRUE
769 31a853d2 Iustin Pop
770 3fc175f0 Alexander Schreiber
  if opts.hvm_pae is None:
771 3fc175f0 Alexander Schreiber
    hvm_pae = opts.hvm_pae
772 31a853d2 Iustin Pop
  else:
773 3fc175f0 Alexander Schreiber
    hvm_pae = opts.hvm_pae == _VALUE_TRUE
774 31a853d2 Iustin Pop
775 ad2023a3 Alexander Schreiber
  if opts.hvm_nic_type == constants.VALUE_NONE:
776 ad2023a3 Alexander Schreiber
    hvm_nic_type = None
777 ad2023a3 Alexander Schreiber
  else:
778 ad2023a3 Alexander Schreiber
    hvm_nic_type = opts.hvm_nic_type
779 ad2023a3 Alexander Schreiber
780 ad2023a3 Alexander Schreiber
  if opts.hvm_disk_type == constants.VALUE_NONE:
781 ad2023a3 Alexander Schreiber
    hvm_disk_type = None
782 ad2023a3 Alexander Schreiber
  else:
783 ad2023a3 Alexander Schreiber
    hvm_disk_type = opts.hvm_disk_type
784 ad2023a3 Alexander Schreiber
785 7767bbf5 Manuel Franceschini
  op = opcodes.OpSetInstanceParams(instance_name=args[0], mem=opts.mem,
786 7767bbf5 Manuel Franceschini
                                   vcpus=opts.vcpus, ip=opts.ip,
787 7767bbf5 Manuel Franceschini
                                   bridge=opts.bridge, mac=opts.mac,
788 7767bbf5 Manuel Franceschini
                                   kernel_path=opts.kernel_path,
789 7767bbf5 Manuel Franceschini
                                   initrd_path=opts.initrd_path,
790 31a853d2 Iustin Pop
                                   hvm_boot_order=hvm_boot_order,
791 31a853d2 Iustin Pop
                                   hvm_acpi=hvm_acpi, hvm_pae=hvm_pae,
792 3fc175f0 Alexander Schreiber
                                   hvm_cdrom_image_path=
793 3fc175f0 Alexander Schreiber
                                   opts.hvm_cdrom_image_path,
794 ad2023a3 Alexander Schreiber
                                   vnc_bind_address=opts.vnc_bind_address,
795 ad2023a3 Alexander Schreiber
                                   hvm_nic_type=hvm_nic_type,
796 4300c4b6 Guido Trotter
                                   hvm_disk_type=hvm_disk_type,
797 4300c4b6 Guido Trotter
                                   force=opts.force)
798 31a853d2 Iustin Pop
799 a8083063 Iustin Pop
  result = SubmitOpCode(op)
800 a8083063 Iustin Pop
801 a8083063 Iustin Pop
  if result:
802 a8083063 Iustin Pop
    logger.ToStdout("Modified instance %s" % args[0])
803 a8083063 Iustin Pop
    for param, data in result:
804 a8083063 Iustin Pop
      logger.ToStdout(" - %-5s -> %s" % (param, data))
805 a8083063 Iustin Pop
    logger.ToStdout("Please don't forget that these parameters take effect"
806 a8083063 Iustin Pop
                    " only at the next start of the instance.")
807 a8083063 Iustin Pop
  return 0
808 a8083063 Iustin Pop
809 a8083063 Iustin Pop
810 a8083063 Iustin Pop
# options used in more than one cmd
811 a8083063 Iustin Pop
node_opt = make_option("-n", "--node", dest="node", help="Target node",
812 a8083063 Iustin Pop
                       metavar="<node>")
813 a8083063 Iustin Pop
814 d0834de3 Michael Hanselmann
os_opt = cli_option("-o", "--os-type", dest="os", help="What OS to run",
815 739ecd56 Michael Hanselmann
                    metavar="<os>")
816 d0834de3 Michael Hanselmann
817 312ac745 Iustin Pop
# multi-instance selection options
818 804a1e8e Iustin Pop
m_force_multi = make_option("--force-multiple", dest="force_multi",
819 804a1e8e Iustin Pop
                            help="Do not ask for confirmation when more than"
820 804a1e8e Iustin Pop
                            " one instance is affected",
821 804a1e8e Iustin Pop
                            action="store_true", default=False)
822 804a1e8e Iustin Pop
823 312ac745 Iustin Pop
m_pri_node_opt = make_option("--primary", dest="multi_mode",
824 312ac745 Iustin Pop
                             help="Filter by nodes (primary only)",
825 312ac745 Iustin Pop
                             const=_SHUTDOWN_NODES_PRI, action="store_const")
826 312ac745 Iustin Pop
827 312ac745 Iustin Pop
m_sec_node_opt = make_option("--secondary", dest="multi_mode",
828 312ac745 Iustin Pop
                             help="Filter by nodes (secondary only)",
829 312ac745 Iustin Pop
                             const=_SHUTDOWN_NODES_SEC, action="store_const")
830 312ac745 Iustin Pop
831 312ac745 Iustin Pop
m_node_opt = make_option("--node", dest="multi_mode",
832 312ac745 Iustin Pop
                         help="Filter by nodes (primary and secondary)",
833 312ac745 Iustin Pop
                         const=_SHUTDOWN_NODES_BOTH, action="store_const")
834 312ac745 Iustin Pop
835 312ac745 Iustin Pop
m_clust_opt = make_option("--all", dest="multi_mode",
836 312ac745 Iustin Pop
                          help="Select all instances in the cluster",
837 312ac745 Iustin Pop
                          const=_SHUTDOWN_CLUSTER, action="store_const")
838 312ac745 Iustin Pop
839 312ac745 Iustin Pop
m_inst_opt = make_option("--instance", dest="multi_mode",
840 312ac745 Iustin Pop
                         help="Filter by instance name [default]",
841 312ac745 Iustin Pop
                         const=_SHUTDOWN_INSTANCES, action="store_const")
842 312ac745 Iustin Pop
843 312ac745 Iustin Pop
844 a8083063 Iustin Pop
# this is defined separately due to readability only
845 a8083063 Iustin Pop
add_opts = [
846 a8083063 Iustin Pop
  DEBUG_OPT,
847 60d49723 Michael Hanselmann
  make_option("-n", "--node", dest="node",
848 60d49723 Michael Hanselmann
              help="Target node and optional secondary node",
849 60d49723 Michael Hanselmann
              metavar="<pnode>[:<snode>]"),
850 b9ac33e9 Iustin Pop
  cli_option("-s", "--os-size", dest="size", help="Disk size, in MiB unless"
851 b9ac33e9 Iustin Pop
             " a suffix is used",
852 a8083063 Iustin Pop
             default=20 * 1024, type="unit", metavar="<size>"),
853 b9ac33e9 Iustin Pop
  cli_option("--swap-size", dest="swap", help="Swap size, in MiB unless a"
854 b9ac33e9 Iustin Pop
             " suffix is used",
855 a8083063 Iustin Pop
             default=4 * 1024, type="unit", metavar="<size>"),
856 d0834de3 Michael Hanselmann
  os_opt,
857 b9ac33e9 Iustin Pop
  cli_option("-m", "--memory", dest="mem", help="Memory size (in MiB)",
858 a8083063 Iustin Pop
              default=128, type="unit", metavar="<mem>"),
859 a8083063 Iustin Pop
  make_option("-p", "--cpu", dest="vcpus", help="Number of virtual CPUs",
860 a8083063 Iustin Pop
              default=1, type="int", metavar="<PROC>"),
861 a8083063 Iustin Pop
  make_option("-t", "--disk-template", dest="disk_template",
862 45873083 Manuel Franceschini
              help="Custom disk setup (diskless, file, plain or drbd)",
863 f9193417 Iustin Pop
              default=None, metavar="TEMPL"),
864 a8083063 Iustin Pop
  make_option("-i", "--ip", dest="ip",
865 a8083063 Iustin Pop
              help="IP address ('none' [default], 'auto', or specify address)",
866 a8083063 Iustin Pop
              default='none', type="string", metavar="<ADDRESS>"),
867 1862d460 Alexander Schreiber
  make_option("--mac", dest="mac",
868 1862d460 Alexander Schreiber
              help="MAC address ('auto' [default], or specify address)",
869 1862d460 Alexander Schreiber
              default='auto', type="string", metavar="<MACADDRESS>"),
870 a8083063 Iustin Pop
  make_option("--no-wait-for-sync", dest="wait_for_sync", default=True,
871 a8083063 Iustin Pop
              action="store_false", help="Don't wait for sync (DANGEROUS!)"),
872 a8083063 Iustin Pop
  make_option("-b", "--bridge", dest="bridge",
873 a8083063 Iustin Pop
              help="Bridge to connect this instance to",
874 bdd55f71 Iustin Pop
              default=None, metavar="<bridge>"),
875 bdd55f71 Iustin Pop
  make_option("--no-start", dest="start", default=True,
876 bdd55f71 Iustin Pop
              action="store_false", help="Don't start the instance after"
877 bdd55f71 Iustin Pop
              " creation"),
878 bdd55f71 Iustin Pop
  make_option("--no-ip-check", dest="ip_check", default=True,
879 bdd55f71 Iustin Pop
              action="store_false", help="Don't check that the instance's IP"
880 bdd55f71 Iustin Pop
              " is alive (only valid with --no-start)"),
881 3b6d8c9b Iustin Pop
  make_option("--kernel", dest="kernel_path",
882 3b6d8c9b Iustin Pop
              help="Path to the instances' kernel (or 'default')",
883 3b6d8c9b Iustin Pop
              default=None,
884 3b6d8c9b Iustin Pop
              type="string", metavar="<FILENAME>"),
885 3b6d8c9b Iustin Pop
  make_option("--initrd", dest="initrd_path",
886 3b6d8c9b Iustin Pop
              help="Path to the instances' initrd (or 'none', or 'default')",
887 3b6d8c9b Iustin Pop
              default=None,
888 3b6d8c9b Iustin Pop
              type="string", metavar="<FILENAME>"),
889 25c5878d Alexander Schreiber
  make_option("--hvm-boot-order", dest="hvm_boot_order",
890 538475ca Iustin Pop
              help="Boot device order for HVM (one or more of [acdn])",
891 25c5878d Alexander Schreiber
              default=None, type="string", metavar="<BOOTORDER>"),
892 45873083 Manuel Franceschini
  make_option("--file-storage-dir", dest="file_storage_dir",
893 45873083 Manuel Franceschini
              help="Relative path under default cluster-wide file storage dir"
894 45873083 Manuel Franceschini
              " to store file-based disks", default=None,
895 45873083 Manuel Franceschini
              metavar="<DIR>"),
896 45873083 Manuel Franceschini
  make_option("--file-driver", dest="file_driver", help="Driver to use"
897 538475ca Iustin Pop
              " for image files", default="loop", metavar="<DRIVER>"),
898 538475ca Iustin Pop
  make_option("--iallocator", metavar="<NAME>",
899 538475ca Iustin Pop
              help="Select nodes for the instance automatically using the"
900 538475ca Iustin Pop
              " <NAME> iallocator plugin", default=None, type="string"),
901 31a853d2 Iustin Pop
  make_option("--hvm-acpi", dest="hvm_acpi",
902 31a853d2 Iustin Pop
              help="ACPI support for HVM (true|false)",
903 31a853d2 Iustin Pop
              metavar="<BOOL>", choices=["true", "false"]),
904 ad2023a3 Alexander Schreiber
  make_option("--hvm-nic-type", dest="hvm_nic_type",
905 ad2023a3 Alexander Schreiber
              help="Type of virtual NIC for HVM "
906 ad2023a3 Alexander Schreiber
              "(rtl8139,ne2k_pci,ne2k_isa,paravirtual)",
907 ad2023a3 Alexander Schreiber
              metavar="NICTYPE", choices=[constants.HT_HVM_NIC_RTL8139,
908 ad2023a3 Alexander Schreiber
                                          constants.HT_HVM_NIC_NE2K_PCI,
909 ad2023a3 Alexander Schreiber
                                          constants.HT_HVM_NIC_NE2K_ISA,
910 ad2023a3 Alexander Schreiber
                                          constants.HT_HVM_DEV_PARAVIRTUAL],
911 ad2023a3 Alexander Schreiber
              default=constants.HT_HVM_NIC_RTL8139),
912 ad2023a3 Alexander Schreiber
  make_option("--hvm-disk-type", dest="hvm_disk_type",
913 ad2023a3 Alexander Schreiber
              help="Type of virtual disks for HVM (ioemu,paravirtual)",
914 ad2023a3 Alexander Schreiber
              metavar="DISKTYPE", choices=[constants.HT_HVM_DEV_IOEMU,
915 ad2023a3 Alexander Schreiber
                                           constants.HT_HVM_DEV_PARAVIRTUAL],
916 ad2023a3 Alexander Schreiber
              default=constants.HT_HVM_DEV_IOEMU,),
917 31a853d2 Iustin Pop
  make_option("--hvm-pae", dest="hvm_pae",
918 31a853d2 Iustin Pop
              help="PAE support for HVM (true|false)",
919 31a853d2 Iustin Pop
              metavar="<BOOL>", choices=["true", "false"]),
920 31a853d2 Iustin Pop
  make_option("--hvm-cdrom-image-path", dest="hvm_cdrom_image_path",
921 31a853d2 Iustin Pop
              help="CDROM image path for HVM (absolute path or None)",
922 31a853d2 Iustin Pop
              default=None, type="string", metavar="<CDROMIMAGE>"),
923 31a853d2 Iustin Pop
  make_option("--vnc-bind-address", dest="vnc_bind_address",
924 31a853d2 Iustin Pop
              help="bind address for VNC (IP address)",
925 31a853d2 Iustin Pop
              default=None, type="string", metavar="<VNCADDRESS>"),
926 a8083063 Iustin Pop
  ]
927 a8083063 Iustin Pop
928 a8083063 Iustin Pop
commands = {
929 a8083063 Iustin Pop
  'add': (AddInstance, ARGS_ONE, add_opts,
930 bdb7d4e8 Michael Hanselmann
          "[...] -t disk-type -n node[:secondary-node] -o os-type <name>",
931 a8083063 Iustin Pop
          "Creates and adds a new instance to the cluster"),
932 51c6e7b5 Michael Hanselmann
  'console': (ConnectToInstanceConsole, ARGS_ONE,
933 51c6e7b5 Michael Hanselmann
              [DEBUG_OPT,
934 51c6e7b5 Michael Hanselmann
               make_option("--show-cmd", dest="show_command",
935 51c6e7b5 Michael Hanselmann
                           action="store_true", default=False,
936 51c6e7b5 Michael Hanselmann
                           help=("Show command instead of executing it"))],
937 9a033156 Iustin Pop
              "[--show-cmd] <instance>",
938 a8083063 Iustin Pop
              "Opens a console on the specified instance"),
939 80de0e3f Iustin Pop
  'failover': (FailoverInstance, ARGS_ONE,
940 fe7b0351 Michael Hanselmann
               [DEBUG_OPT, FORCE_OPT,
941 a8083063 Iustin Pop
                make_option("--ignore-consistency", dest="ignore_consistency",
942 a8083063 Iustin Pop
                            action="store_true", default=False,
943 a8083063 Iustin Pop
                            help="Ignore the consistency of the disks on"
944 a8083063 Iustin Pop
                            " the secondary"),
945 a8083063 Iustin Pop
                ],
946 9a033156 Iustin Pop
               "[-f] <instance>",
947 a8083063 Iustin Pop
               "Stops the instance and starts it on the backup node, using"
948 99e2be3b Guido Trotter
               " the remote mirror (only for instances of type drbd)"),
949 9a033156 Iustin Pop
  'info': (ShowInstanceConfig, ARGS_ANY, [DEBUG_OPT], "[<instance>...]",
950 a8083063 Iustin Pop
           "Show information on the specified instance"),
951 a8083063 Iustin Pop
  'list': (ListInstances, ARGS_NONE,
952 9a033156 Iustin Pop
           [DEBUG_OPT, NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT], "",
953 d8052456 Iustin Pop
           "Lists the instances and their status. The available fields are"
954 d8052456 Iustin Pop
           " (see the man page for details): status, oper_state, oper_ram,"
955 d8a4b51d Iustin Pop
           " name, os, pnode, snodes, admin_state, admin_ram, disk_template,"
956 d8a4b51d Iustin Pop
           " ip, mac, bridge, sda_size, sdb_size, vcpus. The default field"
957 48c4dfa8 Iustin Pop
           " list is (in order): %s." % ", ".join(_LIST_DEF_FIELDS),
958 48c4dfa8 Iustin Pop
           ),
959 20e23543 Alexander Schreiber
  'reinstall': (ReinstallInstance, ARGS_ONE,
960 20e23543 Alexander Schreiber
                [DEBUG_OPT, FORCE_OPT, os_opt,
961 20e23543 Alexander Schreiber
                 make_option("--select-os", dest="select_os",
962 20e23543 Alexander Schreiber
                             action="store_true", default=False,
963 20e23543 Alexander Schreiber
                             help="Interactive OS reinstall, lists available"
964 20e23543 Alexander Schreiber
                             " OS templates for selection")],
965 9a033156 Iustin Pop
                "[-f] <instance>", "Reinstall a stopped instance"),
966 1d67656e Iustin Pop
  'remove': (RemoveInstance, ARGS_ONE,
967 1d67656e Iustin Pop
             [DEBUG_OPT, FORCE_OPT,
968 1d67656e Iustin Pop
              make_option("--ignore-failures", dest="ignore_failures",
969 1d67656e Iustin Pop
                          action="store_true", default=False,
970 1d67656e Iustin Pop
                          help=("Remove the instance from the cluster even"
971 1d67656e Iustin Pop
                                " if there are failures during the removal"
972 1d67656e Iustin Pop
                                " process (shutdown, disk removal, etc.)")),
973 1d67656e Iustin Pop
              ],
974 9a033156 Iustin Pop
             "[-f] <instance>", "Shuts down the instance and removes it"),
975 decd5f45 Iustin Pop
  'rename': (RenameInstance, ARGS_FIXED(2),
976 decd5f45 Iustin Pop
             [DEBUG_OPT,
977 decd5f45 Iustin Pop
              make_option("--no-ip-check", dest="ignore_ip",
978 decd5f45 Iustin Pop
                          help="Do not check that the IP of the new name"
979 decd5f45 Iustin Pop
                          " is alive",
980 decd5f45 Iustin Pop
                          default=False, action="store_true"),
981 decd5f45 Iustin Pop
              ],
982 9a033156 Iustin Pop
             "<instance> <new_name>", "Rename the instance"),
983 a8083063 Iustin Pop
  'replace-disks': (ReplaceDisks, ARGS_ONE,
984 a8083063 Iustin Pop
                    [DEBUG_OPT,
985 a8083063 Iustin Pop
                     make_option("-n", "--new-secondary", dest="new_secondary",
986 a9e0c397 Iustin Pop
                                 help=("New secondary node (for secondary"
987 a9e0c397 Iustin Pop
                                       " node change)"), metavar="NODE"),
988 a9e0c397 Iustin Pop
                     make_option("-p", "--on-primary", dest="on_primary",
989 a9e0c397 Iustin Pop
                                 default=False, action="store_true",
990 a9e0c397 Iustin Pop
                                 help=("Replace the disk(s) on the primary"
991 12c3449a Michael Hanselmann
                                       " node (only for the drbd template)")),
992 a9e0c397 Iustin Pop
                     make_option("-s", "--on-secondary", dest="on_secondary",
993 a9e0c397 Iustin Pop
                                 default=False, action="store_true",
994 a9e0c397 Iustin Pop
                                 help=("Replace the disk(s) on the secondary"
995 12c3449a Michael Hanselmann
                                       " node (only for the drbd template)")),
996 a9e0c397 Iustin Pop
                     make_option("--disks", dest="disks", default=None,
997 a9e0c397 Iustin Pop
                                 help=("Comma-separated list of disks"
998 a9e0c397 Iustin Pop
                                       " to replace (e.g. sda) (optional,"
999 a9e0c397 Iustin Pop
                                       " defaults to all disks")),
1000 b6e82a65 Iustin Pop
                     make_option("--iallocator", metavar="<NAME>",
1001 b6e82a65 Iustin Pop
                                 help="Select new secondary for the instance"
1002 b6e82a65 Iustin Pop
                                 " automatically using the"
1003 b6e82a65 Iustin Pop
                                 " <NAME> iallocator plugin (enables"
1004 b6e82a65 Iustin Pop
                                 " secondary node replacement)",
1005 b6e82a65 Iustin Pop
                                 default=None, type="string"),
1006 a9e0c397 Iustin Pop
                     ],
1007 9a033156 Iustin Pop
                    "[-s|-p|-n NODE] <instance>",
1008 a8083063 Iustin Pop
                    "Replaces all disks for the instance"),
1009 7767bbf5 Manuel Franceschini
  'modify': (SetInstanceParams, ARGS_ONE,
1010 fe7b0351 Michael Hanselmann
             [DEBUG_OPT, FORCE_OPT,
1011 a8083063 Iustin Pop
              cli_option("-m", "--memory", dest="mem",
1012 a8083063 Iustin Pop
                         help="Memory size",
1013 a8083063 Iustin Pop
                         default=None, type="unit", metavar="<mem>"),
1014 a8083063 Iustin Pop
              make_option("-p", "--cpu", dest="vcpus",
1015 a8083063 Iustin Pop
                          help="Number of virtual CPUs",
1016 a8083063 Iustin Pop
                          default=None, type="int", metavar="<PROC>"),
1017 a8083063 Iustin Pop
              make_option("-i", "--ip", dest="ip",
1018 a8083063 Iustin Pop
                          help="IP address ('none' or numeric IP)",
1019 a8083063 Iustin Pop
                          default=None, type="string", metavar="<ADDRESS>"),
1020 a8083063 Iustin Pop
              make_option("-b", "--bridge", dest="bridge",
1021 a8083063 Iustin Pop
                          help="Bridge to connect this instance to",
1022 decd5f45 Iustin Pop
                          default=None, type="string", metavar="<bridge>"),
1023 1862d460 Alexander Schreiber
              make_option("--mac", dest="mac",
1024 1862d460 Alexander Schreiber
                          help="MAC address", default=None,
1025 1862d460 Alexander Schreiber
                          type="string", metavar="<MACADDRESS>"),
1026 973d7867 Iustin Pop
              make_option("--kernel", dest="kernel_path",
1027 973d7867 Iustin Pop
                          help="Path to the instances' kernel (or"
1028 973d7867 Iustin Pop
                          " 'default')", default=None,
1029 973d7867 Iustin Pop
                          type="string", metavar="<FILENAME>"),
1030 973d7867 Iustin Pop
              make_option("--initrd", dest="initrd_path",
1031 973d7867 Iustin Pop
                          help="Path to the instances' initrd (or 'none', or"
1032 973d7867 Iustin Pop
                          " 'default')", default=None,
1033 973d7867 Iustin Pop
                          type="string", metavar="<FILENAME>"),
1034 25c5878d Alexander Schreiber
              make_option("--hvm-boot-order", dest="hvm_boot_order",
1035 25c5878d Alexander Schreiber
                          help="boot device order for HVM"
1036 25c5878d Alexander Schreiber
                          "(either one or more of [acdn] or 'default')",
1037 25c5878d Alexander Schreiber
                          default=None, type="string", metavar="<BOOTORDER>"),
1038 31a853d2 Iustin Pop
              make_option("--hvm-acpi", dest="hvm_acpi",
1039 31a853d2 Iustin Pop
                          help="ACPI support for HVM (true|false)",
1040 31a853d2 Iustin Pop
                          metavar="<BOOL>", choices=["true", "false"]),
1041 31a853d2 Iustin Pop
              make_option("--hvm-pae", dest="hvm_pae",
1042 31a853d2 Iustin Pop
                          help="PAE support for HVM (true|false)",
1043 31a853d2 Iustin Pop
                          metavar="<BOOL>", choices=["true", "false"]),
1044 31a853d2 Iustin Pop
              make_option("--hvm-cdrom-image-path",
1045 31a853d2 Iustin Pop
                          dest="hvm_cdrom_image_path",
1046 31a853d2 Iustin Pop
                          help="CDROM image path for HVM"
1047 31a853d2 Iustin Pop
                          "(absolute path or None)",
1048 31a853d2 Iustin Pop
                          default=None, type="string", metavar="<CDROMIMAGE>"),
1049 ad2023a3 Alexander Schreiber
              make_option("--hvm-nic-type", dest="hvm_nic_type",
1050 ad2023a3 Alexander Schreiber
                          help="Type of virtual NIC for HVM "
1051 ad2023a3 Alexander Schreiber
                          "(rtl8139,ne2k_pci,ne2k_isa,paravirtual)",
1052 ad2023a3 Alexander Schreiber
                          metavar="NICTYPE",
1053 ad2023a3 Alexander Schreiber
                          choices=[constants.HT_HVM_NIC_RTL8139,
1054 ad2023a3 Alexander Schreiber
                                   constants.HT_HVM_NIC_NE2K_PCI,
1055 ad2023a3 Alexander Schreiber
                                   constants.HT_HVM_NIC_NE2K_ISA,
1056 ad2023a3 Alexander Schreiber
                                   constants.HT_HVM_DEV_PARAVIRTUAL],
1057 ad2023a3 Alexander Schreiber
                          default=None),
1058 ad2023a3 Alexander Schreiber
              make_option("--hvm-disk-type", dest="hvm_disk_type",
1059 ad2023a3 Alexander Schreiber
                          help="Type of virtual disks for HVM "
1060 ad2023a3 Alexander Schreiber
                          "(ioemu,paravirtual)",
1061 ad2023a3 Alexander Schreiber
                          metavar="DISKTYPE",
1062 ad2023a3 Alexander Schreiber
                          choices=[constants.HT_HVM_DEV_IOEMU,
1063 ad2023a3 Alexander Schreiber
                                   constants.HT_HVM_DEV_PARAVIRTUAL],
1064 ad2023a3 Alexander Schreiber
                          default=None),
1065 31a853d2 Iustin Pop
              make_option("--vnc-bind-address", dest="vnc_bind_address",
1066 31a853d2 Iustin Pop
                          help="bind address for VNC (IP address)",
1067 31a853d2 Iustin Pop
                          default=None, type="string", metavar="<VNCADDRESS>"),
1068 a8083063 Iustin Pop
              ],
1069 9a033156 Iustin Pop
             "<instance>", "Alters the parameters of an instance"),
1070 312ac745 Iustin Pop
  'shutdown': (ShutdownInstance, ARGS_ANY,
1071 312ac745 Iustin Pop
               [DEBUG_OPT, m_node_opt, m_pri_node_opt, m_sec_node_opt,
1072 804a1e8e Iustin Pop
                m_clust_opt, m_inst_opt, m_force_multi],
1073 9a033156 Iustin Pop
               "<instance>", "Stops an instance"),
1074 312ac745 Iustin Pop
  'startup': (StartupInstance, ARGS_ANY,
1075 804a1e8e Iustin Pop
              [DEBUG_OPT, FORCE_OPT, m_force_multi,
1076 a8083063 Iustin Pop
               make_option("-e", "--extra", dest="extra_args",
1077 a8083063 Iustin Pop
                           help="Extra arguments for the instance's kernel",
1078 a8083063 Iustin Pop
                           default=None, type="string", metavar="<PARAMS>"),
1079 312ac745 Iustin Pop
               m_node_opt, m_pri_node_opt, m_sec_node_opt,
1080 312ac745 Iustin Pop
               m_clust_opt, m_inst_opt,
1081 a8083063 Iustin Pop
               ],
1082 9a033156 Iustin Pop
            "<instance>", "Starts an instance"),
1083 579d4337 Alexander Schreiber
1084 579d4337 Alexander Schreiber
  'reboot': (RebootInstance, ARGS_ANY,
1085 579d4337 Alexander Schreiber
              [DEBUG_OPT, m_force_multi,
1086 579d4337 Alexander Schreiber
               make_option("-e", "--extra", dest="extra_args",
1087 579d4337 Alexander Schreiber
                           help="Extra arguments for the instance's kernel",
1088 579d4337 Alexander Schreiber
                           default=None, type="string", metavar="<PARAMS>"),
1089 579d4337 Alexander Schreiber
               make_option("-t", "--type", dest="reboot_type",
1090 579d4337 Alexander Schreiber
                           help="Type of reboot: soft/hard/full",
1091 579d4337 Alexander Schreiber
                           default=constants.INSTANCE_REBOOT_SOFT,
1092 579d4337 Alexander Schreiber
                           type="string", metavar="<REBOOT>"),
1093 579d4337 Alexander Schreiber
               make_option("--ignore-secondaries", dest="ignore_secondaries",
1094 579d4337 Alexander Schreiber
                           default=False, action="store_true",
1095 579d4337 Alexander Schreiber
                           help="Ignore errors from secondaries"),
1096 579d4337 Alexander Schreiber
               m_node_opt, m_pri_node_opt, m_sec_node_opt,
1097 579d4337 Alexander Schreiber
               m_clust_opt, m_inst_opt,
1098 579d4337 Alexander Schreiber
               ],
1099 9a033156 Iustin Pop
            "<instance>", "Reboots an instance"),
1100 a8083063 Iustin Pop
  'activate-disks': (ActivateDisks, ARGS_ONE, [DEBUG_OPT],
1101 9a033156 Iustin Pop
                     "<instance>",
1102 a8083063 Iustin Pop
                     "Activate an instance's disks"),
1103 a8083063 Iustin Pop
  'deactivate-disks': (DeactivateDisks, ARGS_ONE, [DEBUG_OPT],
1104 9a033156 Iustin Pop
                       "<instance>",
1105 a8083063 Iustin Pop
                       "Deactivate an instance's disks"),
1106 c6e911bc Iustin Pop
  'grow-disk': (GrowDisk, ARGS_FIXED(3), [DEBUG_OPT],
1107 c6e911bc Iustin Pop
                "<instance> <disk> <size>", "Grow an instance's disk"),
1108 846baef9 Iustin Pop
  'list-tags': (ListTags, ARGS_ONE, [DEBUG_OPT],
1109 9a033156 Iustin Pop
                "<node_name>", "List the tags of the given instance"),
1110 810c50b7 Iustin Pop
  'add-tags': (AddTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
1111 9a033156 Iustin Pop
               "<node_name> tag...", "Add tags to the given instance"),
1112 810c50b7 Iustin Pop
  'remove-tags': (RemoveTags, ARGS_ATLEAST(1), [DEBUG_OPT, TAG_SRC_OPT],
1113 9a033156 Iustin Pop
                  "<node_name> tag...", "Remove tags from given instance"),
1114 a8083063 Iustin Pop
  }
1115 a8083063 Iustin Pop
1116 dbfd89dd Guido Trotter
aliases = {
1117 dbfd89dd Guido Trotter
  'activate_block_devs': 'activate-disks',
1118 00ce8b29 Guido Trotter
  'replace_disks': 'replace-disks',
1119 536fda25 Guido Trotter
  'start': 'startup',
1120 536fda25 Guido Trotter
  'stop': 'shutdown',
1121 dbfd89dd Guido Trotter
  }
1122 dbfd89dd Guido Trotter
1123 a8083063 Iustin Pop
if __name__ == '__main__':
1124 dbfd89dd Guido Trotter
  sys.exit(GenericMain(commands, aliases=aliases,
1125 846baef9 Iustin Pop
                       override={"tag_type": constants.TAG_INSTANCE}))