Revision d0c8c01d lib/client/gnt_instance.py

b/lib/client/gnt_instance.py
287 287
                    "hvparams": {},
288 288
                    "file_storage_dir": None,
289 289
                    "force_variant": False,
290
                    "file_driver": 'loop'}
290
                    "file_driver": "loop"}
291 291

  
292 292
  def _PopulateWithDefaults(spec):
293 293
    """Returns a new hash combined with default values."""
......
298 298
  def _Validate(spec):
299 299
    """Validate the instance specs."""
300 300
    # Validate fields required under any circumstances
301
    for required_field in ('os', 'template'):
301
    for required_field in ("os", "template"):
302 302
      if required_field not in spec:
303 303
        raise errors.OpPrereqError('Required field "%s" is missing.' %
304 304
                                   required_field, errors.ECODE_INVAL)
305 305
    # Validate special fields
306
    if spec['primary_node'] is not None:
307
      if (spec['template'] in constants.DTS_INT_MIRROR and
308
          spec['secondary_node'] is None):
309
        raise errors.OpPrereqError('Template requires secondary node, but'
310
                                   ' there was no secondary provided.',
306
    if spec["primary_node"] is not None:
307
      if (spec["template"] in constants.DTS_INT_MIRROR and
308
          spec["secondary_node"] is None):
309
        raise errors.OpPrereqError("Template requires secondary node, but"
310
                                   " there was no secondary provided.",
311 311
                                   errors.ECODE_INVAL)
312
    elif spec['iallocator'] is None:
313
      raise errors.OpPrereqError('You have to provide at least a primary_node'
314
                                 ' or an iallocator.',
312
    elif spec["iallocator"] is None:
313
      raise errors.OpPrereqError("You have to provide at least a primary_node"
314
                                 " or an iallocator.",
315 315
                                 errors.ECODE_INVAL)
316 316

  
317
    if (spec['hvparams'] and
318
        not isinstance(spec['hvparams'], dict)):
319
      raise errors.OpPrereqError('Hypervisor parameters must be a dict.',
317
    if (spec["hvparams"] and
318
        not isinstance(spec["hvparams"], dict)):
319
      raise errors.OpPrereqError("Hypervisor parameters must be a dict.",
320 320
                                 errors.ECODE_INVAL)
321 321

  
322 322
  json_filename = args[0]
......
341 341
    specs = _PopulateWithDefaults(specs)
342 342
    _Validate(specs)
343 343

  
344
    hypervisor = specs['hypervisor']
345
    hvparams = specs['hvparams']
344
    hypervisor = specs["hypervisor"]
345
    hvparams = specs["hvparams"]
346 346

  
347 347
    disks = []
348
    for elem in specs['disk_size']:
348
    for elem in specs["disk_size"]:
349 349
      try:
350 350
        size = utils.ParseUnit(elem)
351 351
      except (TypeError, ValueError), err:
......
354 354
                                   (elem, name, err), errors.ECODE_INVAL)
355 355
      disks.append({"size": size})
356 356

  
357
    utils.ForceDictType(specs['backend'], constants.BES_PARAMETER_TYPES)
357
    utils.ForceDictType(specs["backend"], constants.BES_PARAMETER_TYPES)
358 358
    utils.ForceDictType(hvparams, constants.HVS_PARAMETER_TYPES)
359 359

  
360 360
    tmp_nics = []
......
364 364
          tmp_nics.append({})
365 365
        tmp_nics[0][field] = specs[field]
366 366

  
367
    if specs['nics'] is not None and tmp_nics:
367
    if specs["nics"] is not None and tmp_nics:
368 368
      raise errors.OpPrereqError("'nics' list incompatible with using"
369 369
                                 " individual nic fields as well",
370 370
                                 errors.ECODE_INVAL)
371
    elif specs['nics'] is not None:
372
      tmp_nics = specs['nics']
371
    elif specs["nics"] is not None:
372
      tmp_nics = specs["nics"]
373 373
    elif not tmp_nics:
374 374
      tmp_nics = [{}]
375 375

  
376 376
    op = opcodes.OpInstanceCreate(instance_name=name,
377 377
                                  disks=disks,
378
                                  disk_template=specs['template'],
378
                                  disk_template=specs["template"],
379 379
                                  mode=constants.INSTANCE_CREATE,
380
                                  os_type=specs['os'],
380
                                  os_type=specs["os"],
381 381
                                  force_variant=specs["force_variant"],
382
                                  pnode=specs['primary_node'],
383
                                  snode=specs['secondary_node'],
382
                                  pnode=specs["primary_node"],
383
                                  snode=specs["secondary_node"],
384 384
                                  nics=tmp_nics,
385
                                  start=specs['start'],
386
                                  ip_check=specs['ip_check'],
387
                                  name_check=specs['name_check'],
385
                                  start=specs["start"],
386
                                  ip_check=specs["ip_check"],
387
                                  name_check=specs["name_check"],
388 388
                                  wait_for_sync=True,
389
                                  iallocator=specs['iallocator'],
389
                                  iallocator=specs["iallocator"],
390 390
                                  hypervisor=hypervisor,
391 391
                                  hvparams=hvparams,
392
                                  beparams=specs['backend'],
393
                                  file_storage_dir=specs['file_storage_dir'],
394
                                  file_driver=specs['file_driver'])
392
                                  beparams=specs["backend"],
393
                                  file_storage_dir=specs["file_storage_dir"],
394
                                  file_driver=specs["file_driver"])
395 395

  
396 396
    jex.QueueJob(name, op)
397 397
  # we never want to wait, just show the submitted job IDs
......
438 438
        choices.append(("%s" % number, entry, entry))
439 439
        number += 1
440 440

  
441
    choices.append(('x', 'exit', 'Exit gnt-instance reinstall'))
441
    choices.append(("x", "exit", "Exit gnt-instance reinstall"))
442 442
    selected = AskUser("Enter OS template number (or x to abort):",
443 443
                       choices)
444 444

  
445
    if selected == 'exit':
445
    if selected == "exit":
446 446
      ToStderr("User aborted reinstall, exiting")
447 447
      return 1
448 448

  
......
1225 1225
      _FormatList(buf, _FormatBlockDevInfo(idx, True, device,
1226 1226
                  opts.roman_integers), 2)
1227 1227

  
1228
  ToStdout(buf.getvalue().rstrip('\n'))
1228
  ToStdout(buf.getvalue().rstrip("\n"))
1229 1229
  return retcode
1230 1230

  
1231 1231

  
......
1276 1276
    except (TypeError, ValueError):
1277 1277
      pass
1278 1278
    if disk_op == constants.DDM_ADD:
1279
      if 'size' not in disk_dict:
1279
      if "size" not in disk_dict:
1280 1280
        raise errors.OpPrereqError("Missing required parameter 'size'",
1281 1281
                                   errors.ECODE_INVAL)
1282
      disk_dict['size'] = utils.ParseUnit(disk_dict['size'])
1282
      disk_dict["size"] = utils.ParseUnit(disk_dict["size"])
1283 1283

  
1284 1284
  if (opts.disk_template and
1285 1285
      opts.disk_template in constants.DTS_INT_MIRROR and
......
1368 1368
  ]
1369 1369

  
1370 1370
commands = {
1371
  'add': (
1371
  "add": (
1372 1372
    AddInstance, [ArgHost(min=1, max=1)], COMMON_CREATE_OPTS + add_opts,
1373 1373
    "[...] -t disk-type -n node[:secondary-node] -o os-type <name>",
1374 1374
    "Creates and adds a new instance to the cluster"),
1375
  'batch-create': (
1375
  "batch-create": (
1376 1376
    BatchCreate, [ArgFile(min=1, max=1)], [DRY_RUN_OPT, PRIORITY_OPT],
1377 1377
    "<instances.json>",
1378 1378
    "Create a bunch of instances based on specs in the file."),
1379
  'console': (
1379
  "console": (
1380 1380
    ConnectToInstanceConsole, ARGS_ONE_INSTANCE,
1381 1381
    [SHOWCMD_OPT, PRIORITY_OPT],
1382 1382
    "[--show-cmd] <instance>", "Opens a console on the specified instance"),
1383
  'failover': (
1383
  "failover": (
1384 1384
    FailoverInstance, ARGS_ONE_INSTANCE,
1385 1385
    [FORCE_OPT, IGNORE_CONSIST_OPT, SUBMIT_OPT, SHUTDOWN_TIMEOUT_OPT,
1386 1386
     DRY_RUN_OPT, PRIORITY_OPT, DST_NODE_OPT, IALLOCATOR_OPT],
1387 1387
    "[-f] <instance>", "Stops the instance and starts it on the backup node,"
1388 1388
    " using the remote mirror (only for mirrored instances)"),
1389
  'migrate': (
1389
  "migrate": (
1390 1390
    MigrateInstance, ARGS_ONE_INSTANCE,
1391 1391
    [FORCE_OPT, NONLIVE_OPT, MIGRATION_MODE_OPT, CLEANUP_OPT, DRY_RUN_OPT,
1392 1392
     PRIORITY_OPT, DST_NODE_OPT, IALLOCATOR_OPT, ALLOW_FAILOVER_OPT],
1393 1393
    "[-f] <instance>", "Migrate instance to its secondary node"
1394 1394
    " (only for mirrored instances)"),
1395
  'move': (
1395
  "move": (
1396 1396
    MoveInstance, ARGS_ONE_INSTANCE,
1397 1397
    [FORCE_OPT, SUBMIT_OPT, SINGLE_NODE_OPT, SHUTDOWN_TIMEOUT_OPT,
1398 1398
     DRY_RUN_OPT, PRIORITY_OPT, IGNORE_CONSIST_OPT],
1399 1399
    "[-f] <instance>", "Move instance to an arbitrary node"
1400 1400
    " (only for instances of type file and lv)"),
1401
  'info': (
1401
  "info": (
1402 1402
    ShowInstanceConfig, ARGS_MANY_INSTANCES,
1403 1403
    [STATIC_OPT, ALL_OPT, ROMAN_OPT, PRIORITY_OPT],
1404 1404
    "[-s] {--all | <instance>...}",
1405 1405
    "Show information on the specified instance(s)"),
1406
  'list': (
1406
  "list": (
1407 1407
    ListInstances, ARGS_MANY_INSTANCES,
1408 1408
    [NOHDR_OPT, SEP_OPT, USEUNITS_OPT, FIELDS_OPT, VERBOSE_OPT,
1409 1409
     FORCE_FILTER_OPT],
......
1418 1418
    [NOHDR_OPT, SEP_OPT],
1419 1419
    "[fields...]",
1420 1420
    "Lists all available fields for instances"),
1421
  'reinstall': (
1421
  "reinstall": (
1422 1422
    ReinstallInstance, [ArgInstance()],
1423 1423
    [FORCE_OPT, OS_OPT, FORCE_VARIANT_OPT, m_force_multi, m_node_opt,
1424 1424
     m_pri_node_opt, m_sec_node_opt, m_clust_opt, m_inst_opt, m_node_tags_opt,
1425 1425
     m_pri_node_tags_opt, m_sec_node_tags_opt, m_inst_tags_opt, SELECT_OS_OPT,
1426 1426
     SUBMIT_OPT, DRY_RUN_OPT, PRIORITY_OPT, OSPARAMS_OPT],
1427 1427
    "[-f] <instance>", "Reinstall a stopped instance"),
1428
  'remove': (
1428
  "remove": (
1429 1429
    RemoveInstance, ARGS_ONE_INSTANCE,
1430 1430
    [FORCE_OPT, SHUTDOWN_TIMEOUT_OPT, IGNORE_FAILURES_OPT, SUBMIT_OPT,
1431 1431
     DRY_RUN_OPT, PRIORITY_OPT],
1432 1432
    "[-f] <instance>", "Shuts down the instance and removes it"),
1433
  'rename': (
1433
  "rename": (
1434 1434
    RenameInstance,
1435 1435
    [ArgInstance(min=1, max=1), ArgHost(min=1, max=1)],
1436 1436
    [NOIPCHECK_OPT, NONAMECHECK_OPT, SUBMIT_OPT, DRY_RUN_OPT, PRIORITY_OPT],
1437 1437
    "<instance> <new_name>", "Rename the instance"),
1438
  'replace-disks': (
1438
  "replace-disks": (
1439 1439
    ReplaceDisks, ARGS_ONE_INSTANCE,
1440 1440
    [AUTO_REPLACE_OPT, DISKIDX_OPT, IALLOCATOR_OPT, EARLY_RELEASE_OPT,
1441 1441
     NEW_SECONDARY_OPT, ON_PRIMARY_OPT, ON_SECONDARY_OPT, SUBMIT_OPT,
1442 1442
     DRY_RUN_OPT, PRIORITY_OPT],
1443 1443
    "[-s|-p|-n NODE|-I NAME] <instance>",
1444 1444
    "Replaces all disks for the instance"),
1445
  'modify': (
1445
  "modify": (
1446 1446
    SetInstanceParams, ARGS_ONE_INSTANCE,
1447 1447
    [BACKEND_OPT, DISK_OPT, FORCE_OPT, HVOPTS_OPT, NET_OPT, SUBMIT_OPT,
1448 1448
     DISK_TEMPLATE_OPT, SINGLE_NODE_OPT, OS_OPT, FORCE_VARIANT_OPT,
1449 1449
     OSPARAMS_OPT, DRY_RUN_OPT, PRIORITY_OPT, NWSYNC_OPT],
1450 1450
    "<instance>", "Alters the parameters of an instance"),
1451
  'shutdown': (
1451
  "shutdown": (
1452 1452
    GenericManyOps("shutdown", _ShutdownInstance), [ArgInstance()],
1453 1453
    [m_node_opt, m_pri_node_opt, m_sec_node_opt, m_clust_opt,
1454 1454
     m_node_tags_opt, m_pri_node_tags_opt, m_sec_node_tags_opt,
1455 1455
     m_inst_tags_opt, m_inst_opt, m_force_multi, TIMEOUT_OPT, SUBMIT_OPT,
1456 1456
     DRY_RUN_OPT, PRIORITY_OPT, IGNORE_OFFLINE_OPT, NO_REMEMBER_OPT],
1457 1457
    "<instance>", "Stops an instance"),
1458
  'startup': (
1458
  "startup": (
1459 1459
    GenericManyOps("startup", _StartupInstance), [ArgInstance()],
1460 1460
    [FORCE_OPT, m_force_multi, m_node_opt, m_pri_node_opt, m_sec_node_opt,
1461 1461
     m_node_tags_opt, m_pri_node_tags_opt, m_sec_node_tags_opt,
......
1463 1463
     BACKEND_OPT, DRY_RUN_OPT, PRIORITY_OPT, IGNORE_OFFLINE_OPT,
1464 1464
     NO_REMEMBER_OPT, STARTUP_PAUSED_OPT],
1465 1465
    "<instance>", "Starts an instance"),
1466
  'reboot': (
1466
  "reboot": (
1467 1467
    GenericManyOps("reboot", _RebootInstance), [ArgInstance()],
1468 1468
    [m_force_multi, REBOOT_TYPE_OPT, IGNORE_SECONDARIES_OPT, m_node_opt,
1469 1469
     m_pri_node_opt, m_sec_node_opt, m_clust_opt, m_inst_opt, SUBMIT_OPT,
1470 1470
     m_node_tags_opt, m_pri_node_tags_opt, m_sec_node_tags_opt,
1471 1471
     m_inst_tags_opt, SHUTDOWN_TIMEOUT_OPT, DRY_RUN_OPT, PRIORITY_OPT],
1472 1472
    "<instance>", "Reboots an instance"),
1473
  'activate-disks': (
1473
  "activate-disks": (
1474 1474
    ActivateDisks, ARGS_ONE_INSTANCE,
1475 1475
    [SUBMIT_OPT, IGNORE_SIZE_OPT, PRIORITY_OPT],
1476 1476
    "<instance>", "Activate an instance's disks"),
1477
  'deactivate-disks': (
1477
  "deactivate-disks": (
1478 1478
    DeactivateDisks, ARGS_ONE_INSTANCE,
1479 1479
    [FORCE_OPT, SUBMIT_OPT, DRY_RUN_OPT, PRIORITY_OPT],
1480 1480
    "[-f] <instance>", "Deactivate an instance's disks"),
1481
  'recreate-disks': (
1481
  "recreate-disks": (
1482 1482
    RecreateDisks, ARGS_ONE_INSTANCE,
1483 1483
    [SUBMIT_OPT, DISKIDX_OPT, NODE_PLACEMENT_OPT, DRY_RUN_OPT, PRIORITY_OPT],
1484 1484
    "<instance>", "Recreate an instance's disks"),
1485
  'grow-disk': (
1485
  "grow-disk": (
1486 1486
    GrowDisk,
1487 1487
    [ArgInstance(min=1, max=1), ArgUnknown(min=1, max=1),
1488 1488
     ArgUnknown(min=1, max=1)],
1489 1489
    [SUBMIT_OPT, NWSYNC_OPT, DRY_RUN_OPT, PRIORITY_OPT],
1490 1490
    "<instance> <disk> <size>", "Grow an instance's disk"),
1491
  'list-tags': (
1491
  "list-tags": (
1492 1492
    ListTags, ARGS_ONE_INSTANCE, [PRIORITY_OPT],
1493 1493
    "<instance_name>", "List the tags of the given instance"),
1494
  'add-tags': (
1494
  "add-tags": (
1495 1495
    AddTags, [ArgInstance(min=1, max=1), ArgUnknown()],
1496 1496
    [TAG_SRC_OPT, PRIORITY_OPT],
1497 1497
    "<instance_name> tag...", "Add tags to the given instance"),
1498
  'remove-tags': (
1498
  "remove-tags": (
1499 1499
    RemoveTags, [ArgInstance(min=1, max=1), ArgUnknown()],
1500 1500
    [TAG_SRC_OPT, PRIORITY_OPT],
1501 1501
    "<instance_name> tag...", "Remove tags from given instance"),
......
1503 1503

  
1504 1504
#: dictionary with aliases for commands
1505 1505
aliases = {
1506
  'start': 'startup',
1507
  'stop': 'shutdown',
1506
  "start": "startup",
1507
  "stop": "shutdown",
1508 1508
  }
1509 1509

  
1510 1510

  

Also available in: Unified diff