gnt-instance 55.2 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/python
#

# Copyright (C) 2006, 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


22 23 24 25
# pylint: disable-msg=W0401,W0614
# W0401: Wildcard import ganeti.cli
# W0614: Unused import %s from wildcard import (since we need cli)

Iustin Pop's avatar
Iustin Pop committed
26 27
import sys
import os
28
import itertools
29
import simplejson
30
import time
Iustin Pop's avatar
Iustin Pop committed
31 32 33
from cStringIO import StringIO

from ganeti.cli import *
34
from ganeti import cli
Iustin Pop's avatar
Iustin Pop committed
35 36 37
from ganeti import opcodes
from ganeti import constants
from ganeti import utils
38 39 40 41 42 43 44 45 46
from ganeti import errors


_SHUTDOWN_CLUSTER = "cluster"
_SHUTDOWN_NODES_BOTH = "nodes"
_SHUTDOWN_NODES_PRI = "nodes-pri"
_SHUTDOWN_NODES_SEC = "nodes-sec"
_SHUTDOWN_INSTANCES = "instances"

47

48 49
_VALUE_TRUE = "true"

50
#: default list of options for L{ListInstances}
51
_LIST_DEF_FIELDS = [
52
  "name", "hypervisor", "os", "pnode", "status", "oper_ram",
53 54
  ]

55

56
def _ExpandMultiNames(mode, names, client=None):
57 58 59 60
  """Expand the given names using the passed mode.

  For _SHUTDOWN_CLUSTER, all instances will be returned. For
  _SHUTDOWN_NODES_PRI/SEC, all instances having those nodes as
61
  primary/secondary will be returned. For _SHUTDOWN_NODES_BOTH, all
62 63 64 65
  instances having those nodes as either primary or secondary will be
  returned. For _SHUTDOWN_INSTANCES, the given instances will be
  returned.

66 67 68 69 70 71 72 73 74 75 76 77
  @param mode: one of L{_SHUTDOWN_CLUSTER}, L{_SHUTDOWN_NODES_BOTH},
      L{_SHUTDOWN_NODES_PRI}, L{_SHUTDOWN_NODES_SEC} or
      L{_SHUTDOWN_INSTANCES}
  @param names: a list of names; for cluster, it must be empty,
      and for node and instance it must be a list of valid item
      names (short names are valid as usual, e.g. node1 instead of
      node1.example.com)
  @rtype: list
  @return: the list of names after the expansion
  @raise errors.ProgrammerError: for unknown selection type
  @raise errors.OpPrereqError: for invalid input parameters

78
  """
79 80
  if client is None:
    client = GetClient()
81 82 83
  if mode == _SHUTDOWN_CLUSTER:
    if names:
      raise errors.OpPrereqError("Cluster filter mode takes no arguments")
84
    idata = client.QueryInstances([], ["name"], False)
85 86 87 88 89 90 91
    inames = [row[0] for row in idata]

  elif mode in (_SHUTDOWN_NODES_BOTH,
                _SHUTDOWN_NODES_PRI,
                _SHUTDOWN_NODES_SEC):
    if not names:
      raise errors.OpPrereqError("No node names passed")
92
    ndata = client.QueryNodes(names, ["name", "pinst_list", "sinst_list"],
93
                              False)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    ipri = [row[1] for row in ndata]
    pri_names = list(itertools.chain(*ipri))
    isec = [row[2] for row in ndata]
    sec_names = list(itertools.chain(*isec))
    if mode == _SHUTDOWN_NODES_BOTH:
      inames = pri_names + sec_names
    elif mode == _SHUTDOWN_NODES_PRI:
      inames = pri_names
    elif mode == _SHUTDOWN_NODES_SEC:
      inames = sec_names
    else:
      raise errors.ProgrammerError("Unhandled shutdown type")

  elif mode == _SHUTDOWN_INSTANCES:
    if not names:
      raise errors.OpPrereqError("No instance names passed")
110
    idata = client.QueryInstances(names, ["name"], False)
111 112 113 114 115 116
    inames = [row[0] for row in idata]

  else:
    raise errors.OpPrereqError("Unknown mode '%s'" % mode)

  return inames
Iustin Pop's avatar
Iustin Pop committed
117 118


119
def _ConfirmOperation(inames, text, extra=""):
120 121 122 123 124
  """Ask the user to confirm an operation on a list of instances.

  This function is used to request confirmation for doing an operation
  on a given list of instances.

125 126 127 128 129 130 131 132
  @type inames: list
  @param inames: the list of names that we display when
      we ask for confirmation
  @type text: str
  @param text: the operation that the user should confirm
      (e.g. I{shutdown} or I{startup})
  @rtype: boolean
  @return: True or False depending on user's confirmation.
133 134 135

  """
  count = len(inames)
136 137
  msg = ("The %s will operate on %d instances.\n%s"
         "Do you want to continue?" % (text, count, extra))
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  affected = ("\nAffected instances:\n" +
              "\n".join(["  %s" % name for name in inames]))

  choices = [('y', True, 'Yes, execute the %s' % text),
             ('n', False, 'No, abort the %s' % text)]

  if count > 20:
    choices.insert(1, ('v', 'v', 'View the list of affected instances'))
    ask = msg
  else:
    ask = msg + affected

  choice = AskUser(ask, choices)
  if choice == 'v':
    choices.pop(1)
153
    choice = AskUser(msg + affected, choices)
154 155 156
  return choice


157 158 159 160 161 162
def _EnsureInstancesExist(client, names):
  """Check for and ensure the given instance names exist.

  This function will raise an OpPrereqError in case they don't
  exist. Otherwise it will exit cleanly.

Iustin Pop's avatar
Iustin Pop committed
163
  @type client: L{ganeti.luxi.Client}
164 165 166 167 168 169 170 171
  @param client: the client to use for the query
  @type names: list
  @param names: the list of instance names to query
  @raise errors.OpPrereqError: in case any instance is missing

  """
  # TODO: change LUQueryInstances to that it actually returns None
  # instead of raising an exception, or devise a better mechanism
172
  result = client.QueryInstances(names, ["name"], False)
173 174 175 176 177
  for orig_name, row in zip(names, result):
    if row[0] is None:
      raise errors.OpPrereqError("Instance '%s' does not exist" % orig_name)


Iustin Pop's avatar
Iustin Pop committed
178
def ListInstances(opts, args):
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
179
  """List instances and their properties.
Iustin Pop's avatar
Iustin Pop committed
180

181 182 183 184 185 186
  @param opts: the command line options selected by the user
  @type args: list
  @param args: should be an empty list
  @rtype: int
  @return: the desired exit code

Iustin Pop's avatar
Iustin Pop committed
187 188
  """
  if opts.output is None:
189 190 191
    selected_fields = _LIST_DEF_FIELDS
  elif opts.output.startswith("+"):
    selected_fields = _LIST_DEF_FIELDS + opts.output[1:].split(",")
Iustin Pop's avatar
Iustin Pop committed
192 193 194
  else:
    selected_fields = opts.output.split(",")

195
  output = GetClient().QueryInstances(args, selected_fields, opts.do_locking)
Iustin Pop's avatar
Iustin Pop committed
196 197

  if not opts.no_headers:
198 199 200
    headers = {
      "name": "Instance", "os": "OS", "pnode": "Primary_node",
      "snodes": "Secondary_Nodes", "admin_state": "Autostart",
Iustin Pop's avatar
Iustin Pop committed
201
      "oper_state": "Running",
202
      "oper_ram": "Memory", "disk_template": "Disk_template",
203
      "ip": "IP_address", "mac": "MAC_address",
204
      "nic_mode": "NIC_Mode", "nic_link": "NIC_Link",
Iustin Pop's avatar
Iustin Pop committed
205
      "bridge": "Bridge",
206
      "sda_size": "Disk/0", "sdb_size": "Disk/1",
207
      "disk_usage": "DiskUsage",
208
      "status": "Status", "tags": "Tags",
209
      "network_port": "Network_port",
210 211 212 213 214 215 216 217
      "hv/kernel_path": "Kernel_path",
      "hv/initrd_path": "Initrd_path",
      "hv/boot_order": "HVM_boot_order",
      "hv/acpi": "HVM_ACPI",
      "hv/pae": "HVM_PAE",
      "hv/cdrom_image_path": "HVM_CDROM_image_path",
      "hv/nic_type": "HVM_NIC_type",
      "hv/disk_type": "HVM_Disk_type",
218
      "hv/vnc_bind_address": "VNC_bind_address",
219
      "serial_no": "SerialNo", "hypervisor": "Hypervisor",
220
      "hvparams": "Hypervisor_parameters",
Iustin Pop's avatar
Iustin Pop committed
221 222
      "be/memory": "Configured_memory",
      "be/vcpus": "VCPUs",
223
      "vcpus": "VCPUs",
224
      "be/auto_balance": "Auto_balance",
Iustin Pop's avatar
Iustin Pop committed
225 226
      "disk.count": "Disks", "disk.sizes": "Disk_sizes",
      "nic.count": "NICs", "nic.ips": "NIC_IPs",
227
      "nic.modes": "NIC_modes", "nic.links": "NIC_links",
Iustin Pop's avatar
Iustin Pop committed
228
      "nic.bridges": "NIC_bridges", "nic.macs": "NIC_MACs",
229
      "ctime": "CTime", "mtime": "MTime",
230
      }
231 232 233
  else:
    headers = None

234
  unitfields = ["be/memory", "oper_ram", "sd(a|b)_size", "disk\.size/.*"]
235
  numfields = ["be/memory", "oper_ram", "sd(a|b)_size", "be/vcpus",
Iustin Pop's avatar
Iustin Pop committed
236
               "serial_no", "(disk|nic)\.count", "disk\.size/.*"]
237

238 239
  list_type_fields = ("tags", "disk.sizes", "nic.macs", "nic.ips",
                      "nic.modes", "nic.links", "nic.bridges")
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  # change raw values to nicer strings
  for row in output:
    for idx, field in enumerate(selected_fields):
      val = row[idx]
      if field == "snodes":
        val = ",".join(val) or "-"
      elif field == "admin_state":
        if val:
          val = "yes"
        else:
          val = "no"
      elif field == "oper_state":
        if val is None:
          val = "(node down)"
        elif val: # True
          val = "running"
        else:
          val = "stopped"
      elif field == "oper_ram":
        if val is None:
          val = "(node down)"
      elif field == "sda_size" or field == "sdb_size":
        if val is None:
          val = "N/A"
264 265
      elif field == "ctime" or field == "mtime":
        val = utils.FormatTime(val)
266
      elif field in list_type_fields:
Iustin Pop's avatar
Iustin Pop committed
267
        val = ",".join(str(item) for item in val)
268 269
      elif val is None:
        val = "-"
270 271
      row[idx] = str(val)

272 273
  data = GenerateTable(separator=opts.separator, headers=headers,
                       fields=selected_fields, unitfields=unitfields,
274
                       numfields=numfields, data=output, units=opts.units)
275 276

  for line in data:
277
    ToStdout(line)
Iustin Pop's avatar
Iustin Pop committed
278 279 280 281 282 283 284

  return 0


def AddInstance(opts, args):
  """Add an instance to the cluster.

285 286 287 288 289
  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain only one element, the new instance name
  @rtype: int
  @return: the desired exit code
Iustin Pop's avatar
Iustin Pop committed
290 291 292 293

  """
  instance = args[0]

294 295
  (pnode, snode) = SplitNodeOption(opts.node)

296 297 298 299
  hypervisor = None
  hvparams = {}
  if opts.hypervisor:
    hypervisor, hvparams = opts.hypervisor
300

301 302 303 304 305 306
  if opts.nics:
    try:
      nic_max = max(int(nidx[0])+1 for nidx in opts.nics)
    except ValueError, err:
      raise errors.OpPrereqError("Invalid NIC index passed: %s" % str(err))
    nics = [{}] * nic_max
307
    for nidx, ndict in opts.nics:
308
      nidx = int(nidx)
309 310 311
      if not isinstance(ndict, dict):
        msg = "Invalid nic/%d value: expected dict, got %s" % (nidx, ndict)
        raise errors.OpPrereqError(msg)
312
      nics[nidx] = ndict
Iustin Pop's avatar
Iustin Pop committed
313 314 315
  elif opts.no_nics:
    # no nics
    nics = []
316 317 318 319
  else:
    # default of one nic, all auto
    nics = [{}]

Iustin Pop's avatar
Iustin Pop committed
320
  if opts.disk_template == constants.DT_DISKLESS:
321
    if opts.disks or opts.sd_size is not None:
Iustin Pop's avatar
Iustin Pop committed
322 323 324
      raise errors.OpPrereqError("Diskless instance but disk"
                                 " information passed")
    disks = []
325
  else:
326
    if not opts.disks and not opts.sd_size:
Iustin Pop's avatar
Iustin Pop committed
327
      raise errors.OpPrereqError("No disk information specified")
328 329 330 331 332
    if opts.disks and opts.sd_size is not None:
      raise errors.OpPrereqError("Please use either the '--disk' or"
                                 " '-s' option")
    if opts.sd_size is not None:
      opts.disks = [(0, {"size": opts.sd_size})]
333 334 335 336 337 338 339
    try:
      disk_max = max(int(didx[0])+1 for didx in opts.disks)
    except ValueError, err:
      raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err))
    disks = [{}] * disk_max
    for didx, ddict in opts.disks:
      didx = int(didx)
340 341 342 343
      if not isinstance(ddict, dict):
        msg = "Invalid disk/%d value: expected dict, got %s" % (didx, ddict)
        raise errors.OpPrereqError(msg)
      elif "size" not in ddict:
344 345 346 347 348 349 350 351
        raise errors.OpPrereqError("Missing size for disk %d" % didx)
      try:
        ddict["size"] = utils.ParseUnit(ddict["size"])
      except ValueError, err:
        raise errors.OpPrereqError("Invalid disk size for disk %d: %s" %
                                   (didx, err))
      disks[didx] = ddict

352
  utils.ForceDictType(opts.beparams, constants.BES_PARAMETER_TYPES)
353
  utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
354

Iustin Pop's avatar
Iustin Pop committed
355
  op = opcodes.OpCreateInstance(instance_name=instance,
356
                                disks=disks,
Iustin Pop's avatar
Iustin Pop committed
357
                                disk_template=opts.disk_template,
358
                                nics=nics,
Iustin Pop's avatar
Iustin Pop committed
359
                                mode=constants.INSTANCE_CREATE,
360
                                os_type=opts.os, pnode=pnode,
Iustin Pop's avatar
Iustin Pop committed
361
                                snode=snode,
362
                                start=opts.start, ip_check=opts.ip_check,
363
                                wait_for_sync=opts.wait_for_sync,
364 365
                                hypervisor=hypervisor,
                                hvparams=hvparams,
Iustin Pop's avatar
Iustin Pop committed
366
                                beparams=opts.beparams,
367
                                iallocator=opts.iallocator,
368 369
                                file_storage_dir=opts.file_storage_dir,
                                file_driver=opts.file_driver,
370
                                )
371

372
  SubmitOrSend(op, opts)
Iustin Pop's avatar
Iustin Pop committed
373 374 375
  return 0


376
def BatchCreate(opts, args):
377 378 379 380 381 382
  """Create instances using a definition file.

  This function reads a json file with instances defined
  in the form::

    {"instance-name":{
383
      "disk_size": [20480],
384 385 386 387
      "template": "drbd",
      "backend": {
        "memory": 512,
        "vcpus": 1 },
388
      "os": "debootstrap",
389 390 391 392 393 394 395 396 397 398 399 400 401
      "primary_node": "firstnode",
      "secondary_node": "secondnode",
      "iallocator": "dumb"}
    }

  Note that I{primary_node} and I{secondary_node} have precedence over
  I{iallocator}.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain one element, the json filename
  @rtype: int
  @return: the desired exit code
402 403

  """
404
  _DEFAULT_SPECS = {"disk_size": [20 * 1024],
405 406 407 408
                    "backend": {},
                    "iallocator": None,
                    "primary_node": None,
                    "secondary_node": None,
409
                    "nics": None,
410 411 412
                    "start": True,
                    "ip_check": True,
                    "hypervisor": None,
Iustin Pop's avatar
Iustin Pop committed
413
                    "hvparams": {},
414 415 416 417 418
                    "file_storage_dir": None,
                    "file_driver": 'loop'}

  def _PopulateWithDefaults(spec):
    """Returns a new hash combined with default values."""
419 420 421
    mydict = _DEFAULT_SPECS.copy()
    mydict.update(spec)
    return mydict
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

  def _Validate(spec):
    """Validate the instance specs."""
    # Validate fields required under any circumstances
    for required_field in ('os', 'template'):
      if required_field not in spec:
        raise errors.OpPrereqError('Required field "%s" is missing.' %
                                   required_field)
    # Validate special fields
    if spec['primary_node'] is not None:
      if (spec['template'] in constants.DTS_NET_MIRROR and
          spec['secondary_node'] is None):
        raise errors.OpPrereqError('Template requires secondary node, but'
                                   ' there was no secondary provided.')
    elif spec['iallocator'] is None:
      raise errors.OpPrereqError('You have to provide at least a primary_node'
                                 ' or an iallocator.')

Iustin Pop's avatar
Iustin Pop committed
440 441
    if (spec['hvparams'] and
        not isinstance(spec['hvparams'], dict)):
442 443 444 445
      raise errors.OpPrereqError('Hypervisor parameters must be a dict.')

  json_filename = args[0]
  try:
446
    instance_data = simplejson.loads(utils.ReadFile(json_filename))
Iustin Pop's avatar
Iustin Pop committed
447 448 449
  except Exception, err:
    ToStderr("Can't parse the instance definition file: %s" % str(err))
    return 1
450

451 452
  jex = JobExecutor()

453 454 455
  # Iterate over the instances and do:
  #  * Populate the specs with default value
  #  * Validate the instance specs
456 457 458
  i_names = utils.NiceSort(instance_data.keys())
  for name in i_names:
    specs = instance_data[name]
459 460 461
    specs = _PopulateWithDefaults(specs)
    _Validate(specs)

Iustin Pop's avatar
Iustin Pop committed
462 463
    hypervisor = specs['hypervisor']
    hvparams = specs['hvparams']
464

465 466 467 468 469 470 471 472 473 474
    disks = []
    for elem in specs['disk_size']:
      try:
        size = utils.ParseUnit(elem)
      except ValueError, err:
        raise errors.OpPrereqError("Invalid disk size '%s' for"
                                   " instance %s: %s" %
                                   (elem, name, err))
      disks.append({"size": size})

475 476 477
    utils.ForceDictType(specs['backend'], constants.BES_PARAMETER_TYPES)
    utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    tmp_nics = []
    for field in ('ip', 'mac', 'mode', 'link', 'bridge'):
      if field in specs:
        if not tmp_nics:
          tmp_nics.append({})
        tmp_nics[0][field] = specs[field]

    if specs['nics'] is not None and tmp_nics:
      raise errors.OpPrereqError("'nics' list incompatible with using"
                                 " individual nic fields as well")
    elif specs['nics'] is not None:
      tmp_nics = specs['nics']
    elif not tmp_nics:
      tmp_nics = [{}]

493
    op = opcodes.OpCreateInstance(instance_name=name,
494
                                  disks=disks,
495 496 497 498 499
                                  disk_template=specs['template'],
                                  mode=constants.INSTANCE_CREATE,
                                  os_type=specs['os'],
                                  pnode=specs['primary_node'],
                                  snode=specs['secondary_node'],
500
                                  nics=tmp_nics,
501 502 503 504 505 506 507 508 509 510
                                  start=specs['start'],
                                  ip_check=specs['ip_check'],
                                  wait_for_sync=True,
                                  iallocator=specs['iallocator'],
                                  hypervisor=hypervisor,
                                  hvparams=hvparams,
                                  beparams=specs['backend'],
                                  file_storage_dir=specs['file_storage_dir'],
                                  file_driver=specs['file_driver'])

511 512 513
    jex.QueueJob(name, op)
  # we never want to wait, just show the submitted job IDs
  jex.WaitOrShow(False)
514 515 516 517

  return 0


518 519 520
def ReinstallInstance(opts, args):
  """Reinstall an instance.

521 522 523 524 525 526
  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain only one element, the name of the
      instance to be reinstalled
  @rtype: int
  @return: the desired exit code
527 528

  """
529 530 531 532 533 534 535
  # first, compute the desired name list
  if opts.multi_mode is None:
    opts.multi_mode = _SHUTDOWN_INSTANCES

  inames = _ExpandMultiNames(opts.multi_mode, args)
  if not inames:
    raise errors.OpPrereqError("Selection filter does not match any instances")
536

537
  # second, if requested, ask for an OS
538 539 540 541 542
  if opts.select_os is True:
    op = opcodes.OpDiagnoseOS(output_fields=["name", "valid"], names=[])
    result = SubmitOpCode(op)

    if not result:
543
      ToStdout("Can't get the OS list")
544 545
      return 1

546
    ToStdout("Available OS templates:")
547 548 549
    number = 0
    choices = []
    for entry in result:
550
      ToStdout("%3s: %s", number, entry[0])
551 552 553 554
      choices.append(("%s" % number, entry[0], entry[0]))
      number = number + 1

    choices.append(('x', 'exit', 'Exit gnt-instance reinstall'))
555
    selected = AskUser("Enter OS template number (or x to abort):",
556 557 558
                       choices)

    if selected == 'exit':
559
      ToStderr("User aborted reinstall, exiting")
560 561
      return 1

562
    os_name = selected
563
  else:
564
    os_name = opts.os
565

566 567 568 569 570 571 572
  # third, get confirmation: multi-reinstall requires --force-multi
  # *and* --force, single-reinstall just --force
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
  if multi_on:
    warn_msg = "Note: this will remove *all* data for the below instances!\n"
    if not ((opts.force_multi and opts.force) or
            _ConfirmOperation(inames, "reinstall", extra=warn_msg)):
573
      return 1
574 575 576
  else:
    if not opts.force:
      usertext = ("This will reinstall the instance %s and remove"
Iustin Pop's avatar
Iustin Pop committed
577
                  " all data. Continue?") % inames[0]
578 579 580 581 582 583 584 585
      if not AskUser(usertext):
        return 1

  jex = JobExecutor(verbose=multi_on)
  for instance_name in inames:
    op = opcodes.OpReinstallInstance(instance_name=instance_name,
                                     os_type=os_name)
    jex.QueueJob(instance_name, op)
586

587
  jex.WaitOrShow(not opts.submit_only)
588 589 590
  return 0


Iustin Pop's avatar
Iustin Pop committed
591 592 593
def RemoveInstance(opts, args):
  """Remove an instance.

594 595 596 597 598 599
  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain only one element, the name of
      the instance to be removed
  @rtype: int
  @return: the desired exit code
Iustin Pop's avatar
Iustin Pop committed
600 601 602 603

  """
  instance_name = args[0]
  force = opts.force
604
  cl = GetClient()
Iustin Pop's avatar
Iustin Pop committed
605 606

  if not force:
607 608
    _EnsureInstancesExist(cl, [instance_name])

Iustin Pop's avatar
Iustin Pop committed
609 610 611
    usertext = ("This will remove the volumes of the instance %s"
                " (including mirrors), thus removing all the data"
                " of the instance. Continue?") % instance_name
612
    if not AskUser(usertext):
Iustin Pop's avatar
Iustin Pop committed
613 614
      return 1

Iustin Pop's avatar
Iustin Pop committed
615 616
  op = opcodes.OpRemoveInstance(instance_name=instance_name,
                                ignore_failures=opts.ignore_failures)
617
  SubmitOrSend(op, opts, cl=cl)
Iustin Pop's avatar
Iustin Pop committed
618 619 620
  return 0


621
def RenameInstance(opts, args):
Guido Trotter's avatar
Guido Trotter committed
622
  """Rename an instance.
623

624 625 626 627 628 629
  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain two elements, the old and the
      new instance names
  @rtype: int
  @return: the desired exit code
630 631 632 633 634

  """
  op = opcodes.OpRenameInstance(instance_name=args[0],
                                new_name=args[1],
                                ignore_ip=opts.ignore_ip)
635
  SubmitOrSend(op, opts)
636 637 638
  return 0


Iustin Pop's avatar
Iustin Pop committed
639 640 641 642
def ActivateDisks(opts, args):
  """Activate an instance's disks.

  This serves two purposes:
643 644
    - it allows (as long as the instance is not running)
      mounting the disks and modifying them from the node
Iustin Pop's avatar
Iustin Pop committed
645 646
    - it repairs inactive secondary drbds

647 648 649 650 651 652
  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain only one element, the instance name
  @rtype: int
  @return: the desired exit code

Iustin Pop's avatar
Iustin Pop committed
653 654
  """
  instance_name = args[0]
655 656
  op = opcodes.OpActivateInstanceDisks(instance_name=instance_name,
                                       ignore_size=opts.ignore_size)
657
  disks_info = SubmitOrSend(op, opts)
Iustin Pop's avatar
Iustin Pop committed
658
  for host, iname, nname in disks_info:
659
    ToStdout("%s:%s:%s", host, iname, nname)
Iustin Pop's avatar
Iustin Pop committed
660 661 662 663
  return 0


def DeactivateDisks(opts, args):
Iustin Pop's avatar
Iustin Pop committed
664
  """Deactivate an instance's disks.
Iustin Pop's avatar
Iustin Pop committed
665 666 667 668

  This function takes the instance name, looks for its primary node
  and the tries to shutdown its block devices on that node.

669 670 671 672 673 674
  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain only one element, the instance name
  @rtype: int
  @return: the desired exit code

Iustin Pop's avatar
Iustin Pop committed
675 676 677
  """
  instance_name = args[0]
  op = opcodes.OpDeactivateInstanceDisks(instance_name=instance_name)
678
  SubmitOrSend(op, opts)
Iustin Pop's avatar
Iustin Pop committed
679 680 681
  return 0


Iustin Pop's avatar
Iustin Pop committed
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
def RecreateDisks(opts, args):
  """Recreate an instance's disks.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain only one element, the instance name
  @rtype: int
  @return: the desired exit code

  """
  instance_name = args[0]
  if opts.disks:
    try:
      opts.disks = [int(v) for v in opts.disks.split(",")]
    except (ValueError, TypeError), err:
      ToStderr("Invalid disks value: %s" % str(err))
      return 1
  else:
    opts.disks = []

  op = opcodes.OpRecreateInstanceDisks(instance_name=instance_name,
                                       disks=opts.disks)
  SubmitOrSend(op, opts)
  return 0


Iustin Pop's avatar
Iustin Pop committed
708
def GrowDisk(opts, args):
709
  """Grow an instance's disks.
Iustin Pop's avatar
Iustin Pop committed
710

711 712 713 714 715 716
  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain two elements, the instance name
      whose disks we grow and the disk name, e.g. I{sda}
  @rtype: int
  @return: the desired exit code
Iustin Pop's avatar
Iustin Pop committed
717 718 719 720

  """
  instance = args[0]
  disk = args[1]
721 722 723 724
  try:
    disk = int(disk)
  except ValueError, err:
    raise errors.OpPrereqError("Invalid disk index: %s" % str(err))
Iustin Pop's avatar
Iustin Pop committed
725
  amount = utils.ParseUnit(args[2])
726 727
  op = opcodes.OpGrowDisk(instance_name=instance, disk=disk, amount=amount,
                          wait_for_sync=opts.wait_for_sync)
728
  SubmitOrSend(op, opts)
Iustin Pop's avatar
Iustin Pop committed
729 730 731
  return 0


Iustin Pop's avatar
Iustin Pop committed
732
def StartupInstance(opts, args):
733
  """Startup instances.
Iustin Pop's avatar
Iustin Pop committed
734

735 736 737 738 739 740 741 742 743 744
  Depending on the options given, this will start one or more
  instances.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: the instance or node names based on which we
      create the final selection (in conjunction with the
      opts argument)
  @rtype: int
  @return: the desired exit code
Iustin Pop's avatar
Iustin Pop committed
745 746

  """
747
  cl = GetClient()
748 749
  if opts.multi_mode is None:
    opts.multi_mode = _SHUTDOWN_INSTANCES
750
  inames = _ExpandMultiNames(opts.multi_mode, args, client=cl)
751 752
  if not inames:
    raise errors.OpPrereqError("Selection filter does not match any instances")
753 754 755 756
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
  if not (opts.force_multi or not multi_on
          or _ConfirmOperation(inames, "startup")):
    return 1
757
  jex = cli.JobExecutor(verbose=multi_on, cl=cl)
758 759
  for name in inames:
    op = opcodes.OpStartupInstance(instance_name=name,
760
                                   force=opts.force)
761 762 763 764 765
    # do not add these parameters to the opcode unless they're defined
    if opts.hvparams:
      op.hvparams = opts.hvparams
    if opts.beparams:
      op.beparams = opts.beparams
766 767
    jex.QueueJob(name, op)
  jex.WaitOrShow(not opts.submit_only)
Iustin Pop's avatar
Iustin Pop committed
768 769
  return 0

770

771
def RebootInstance(opts, args):
772 773 774 775
  """Reboot instance(s).

  Depending on the parameters given, this will reboot one or more
  instances.
776

777 778 779 780 781 782 783
  @param opts: the command line options selected by the user
  @type args: list
  @param args: the instance or node names based on which we
      create the final selection (in conjunction with the
      opts argument)
  @rtype: int
  @return: the desired exit code
784 785

  """
786
  cl = GetClient()
787 788
  if opts.multi_mode is None:
    opts.multi_mode = _SHUTDOWN_INSTANCES
789
  inames = _ExpandMultiNames(opts.multi_mode, args, client=cl)
790 791
  if not inames:
    raise errors.OpPrereqError("Selection filter does not match any instances")
792 793 794 795
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
  if not (opts.force_multi or not multi_on
          or _ConfirmOperation(inames, "reboot")):
    return 1
796
  jex = JobExecutor(verbose=multi_on, cl=cl)
797 798 799 800
  for name in inames:
    op = opcodes.OpRebootInstance(instance_name=name,
                                  reboot_type=opts.reboot_type,
                                  ignore_secondaries=opts.ignore_secondaries)
801 802
    jex.QueueJob(name, op)
  jex.WaitOrShow(not opts.submit_only)
803
  return 0
Iustin Pop's avatar
Iustin Pop committed
804

805

Iustin Pop's avatar
Iustin Pop committed
806 807 808
def ShutdownInstance(opts, args):
  """Shutdown an instance.

809 810 811 812 813 814 815
  @param opts: the command line options selected by the user
  @type args: list
  @param args: the instance or node names based on which we
      create the final selection (in conjunction with the
      opts argument)
  @rtype: int
  @return: the desired exit code
Iustin Pop's avatar
Iustin Pop committed
816 817

  """
818
  cl = GetClient()
819 820
  if opts.multi_mode is None:
    opts.multi_mode = _SHUTDOWN_INSTANCES
821
  inames = _ExpandMultiNames(opts.multi_mode, args, client=cl)
822 823
  if not inames:
    raise errors.OpPrereqError("Selection filter does not match any instances")
824 825 826 827
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
  if not (opts.force_multi or not multi_on
          or _ConfirmOperation(inames, "shutdown")):
    return 1
828 829

  jex = cli.JobExecutor(verbose=multi_on, cl=cl)
830 831
  for name in inames:
    op = opcodes.OpShutdownInstance(instance_name=name)
832 833
    jex.QueueJob(name, op)
  jex.WaitOrShow(not opts.submit_only)
Iustin Pop's avatar
Iustin Pop committed
834 835 836 837 838 839
  return 0


def ReplaceDisks(opts, args):
  """Replace the disks of an instance

840 841 842 843 844
  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain only one element, the instance name
  @rtype: int
  @return: the desired exit code
Iustin Pop's avatar
Iustin Pop committed
845 846 847

  """
  instance_name = args[0]
848
  new_2ndary = opts.new_secondary
849
  iallocator = opts.iallocator
850
  if opts.disks is None:
851
    disks = []
852
  else:
853 854 855 856
    try:
      disks = [int(i) for i in opts.disks.split(",")]
    except ValueError, err:
      raise errors.OpPrereqError("Invalid disk index passed: %s" % str(err))
857
  cnt = [opts.on_primary, opts.on_secondary, opts.auto,
858 859
         new_2ndary is not None, iallocator is not None].count(True)
  if cnt != 1:
860
    raise errors.OpPrereqError("One and only one of the -p, -s, -a, -n and -i"
861 862
                               " options must be passed")
  elif opts.on_primary:
863
    mode = constants.REPLACE_DISK_PRI
864
  elif opts.on_secondary:
865
    mode = constants.REPLACE_DISK_SEC
866 867 868 869 870
  elif opts.auto:
    mode = constants.REPLACE_DISK_AUTO
    if disks:
      raise errors.OpPrereqError("Cannot specify disks when using automatic"
                                 " mode")
871 872 873
  elif new_2ndary is not None or iallocator is not None:
    # replace secondary
    mode = constants.REPLACE_DISK_CHG
874 875

  op = opcodes.OpReplaceDisks(instance_name=args[0], disks=disks,
876 877
                              remote_node=new_2ndary, mode=mode,
                              iallocator=iallocator)
878
  SubmitOrSend(op, opts)
Iustin Pop's avatar
Iustin Pop committed
879 880 881 882 883 884 885 886 887
  return 0


def FailoverInstance(opts, args):
  """Failover an instance.

  The failover is done by shutting it down on its present node and
  starting it on the secondary.

888 889 890 891 892
  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain only one element, the instance name
  @rtype: int
  @return: the desired exit code
Iustin Pop's avatar
Iustin Pop committed
893 894

  """
895
  cl = GetClient()
896 897
  instance_name = args[0]
  force = opts.force
Iustin Pop's avatar
Iustin Pop committed
898

899
  if not force:
900 901
    _EnsureInstancesExist(cl, [instance_name])

902 903 904 905 906
    usertext = ("Failover will happen to image %s."
                " This requires a shutdown of the instance. Continue?" %
                (instance_name,))
    if not AskUser(usertext):
      return 1
Iustin Pop's avatar
Iustin Pop committed
907

908 909
  op = opcodes.OpFailoverInstance(instance_name=instance_name,
                                  ignore_consistency=opts.ignore_consistency)
910
  SubmitOrSend(op, opts, cl=cl)
911
  return 0
Iustin Pop's avatar
Iustin Pop committed
912 913


914 915 916 917 918
def MigrateInstance(opts, args):
  """Migrate an instance.

  The migrate is done without shutdown.

Iustin Pop's avatar
Iustin Pop committed
919 920 921 922 923
  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain only one element, the instance name
  @rtype: int
  @return: the desired exit code
924 925

  """
926
  cl = GetClient()
927 928 929 930
  instance_name = args[0]
  force = opts.force

  if not force:
931 932
    _EnsureInstancesExist(cl, [instance_name])

933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
    if opts.cleanup:
      usertext = ("Instance %s will be recovered from a failed migration."
                  " Note that the migration procedure (including cleanup)" %
                  (instance_name,))
    else:
      usertext = ("Instance %s will be migrated. Note that migration" %
                  (instance_name,))
    usertext += (" is **experimental** in this version."
                " This might impact the instance if anything goes wrong."
                " Continue?")
    if not AskUser(usertext):
      return 1

  op = opcodes.OpMigrateInstance(instance_name=instance_name, live=opts.live,
                                 cleanup=opts.cleanup)
948
  SubmitOpCode(op, cl=cl)
949 950 951
  return 0


Iustin Pop's avatar
Iustin Pop committed
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
def MoveInstance(opts, args):
  """Move an instance.

  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain only one element, the instance name
  @rtype: int
  @return: the desired exit code

  """
  cl = GetClient()
  instance_name = args[0]
  force = opts.force

  if not force:
    usertext = ("Instance %s will be moved."
                " This requires a shutdown of the instance. Continue?" %
                (instance_name,))
    if not AskUser(usertext):
      return 1

  op = opcodes.OpMoveInstance(instance_name=instance_name,
                              target_node=opts.target_node)
  SubmitOrSend(op, opts, cl=cl)
  return 0


Iustin Pop's avatar
Iustin Pop committed
979 980 981
def ConnectToInstanceConsole(opts, args):
  """Connect to the console of an instance.

982 983 984 985 986
  @param opts: the command line options selected by the user
  @type args: list
  @param args: should contain only one element, the instance name
  @rtype: int
  @return: the desired exit code
Iustin Pop's avatar
Iustin Pop committed
987 988 989 990 991

  """
  instance_name = args[0]

  op = opcodes.OpConnectConsole(instance_name=instance_name)
992
  cmd = SubmitOpCode(op)
993 994

  if opts.show_command:
995
    ToStdout("%s", utils.ShellQuoteArgs(cmd))
996 997 998 999
  else:
    try:
      os.execvp(cmd[0], cmd)
    finally:
1000
      ToStderr("Can't run console command %s with arguments:\n'%s'",
1001
               cmd[0], " ".join(cmd))
1002
      os._exit(1)
Iustin Pop's avatar
Iustin Pop committed
1003 1004


1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
def _FormatLogicalID(dev_type, logical_id):
  """Formats the logical_id of a disk.

  """
  if dev_type == constants.LD_DRBD8:
    node_a, node_b, port, minor_a, minor_b,