Statistics
| Branch: | Tag: | Revision:

root / scripts / gnt-instance @ e69d05fd

History | View | Annotate | Download (43.6 kB)

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