Revision c20efaa8

b/lib/client/gnt_instance.py
41 41
from ganeti import objects
42 42

  
43 43

  
44
_SHUTDOWN_CLUSTER = "cluster"
45
_SHUTDOWN_NODES_BOTH = "nodes"
46
_SHUTDOWN_NODES_PRI = "nodes-pri"
47
_SHUTDOWN_NODES_SEC = "nodes-sec"
48
_SHUTDOWN_NODES_BOTH_BY_TAGS = "nodes-by-tags"
49
_SHUTDOWN_NODES_PRI_BY_TAGS = "nodes-pri-by-tags"
50
_SHUTDOWN_NODES_SEC_BY_TAGS = "nodes-sec-by-tags"
51
_SHUTDOWN_INSTANCES = "instances"
52
_SHUTDOWN_INSTANCES_BY_TAGS = "instances-by-tags"
53

  
54
_SHUTDOWN_NODES_TAGS_MODES = (
55
    _SHUTDOWN_NODES_BOTH_BY_TAGS,
56
    _SHUTDOWN_NODES_PRI_BY_TAGS,
57
    _SHUTDOWN_NODES_SEC_BY_TAGS)
44
_EXPAND_CLUSTER = "cluster"
45
_EXPAND_NODES_BOTH = "nodes"
46
_EXPAND_NODES_PRI = "nodes-pri"
47
_EXPAND_NODES_SEC = "nodes-sec"
48
_EXPAND_NODES_BOTH_BY_TAGS = "nodes-by-tags"
49
_EXPAND_NODES_PRI_BY_TAGS = "nodes-pri-by-tags"
50
_EXPAND_NODES_SEC_BY_TAGS = "nodes-sec-by-tags"
51
_EXPAND_INSTANCES = "instances"
52
_EXPAND_INSTANCES_BY_TAGS = "instances-by-tags"
53

  
54
_EXPAND_NODES_TAGS_MODES = frozenset([
55
  _EXPAND_NODES_BOTH_BY_TAGS,
56
  _EXPAND_NODES_PRI_BY_TAGS,
57
  _EXPAND_NODES_SEC_BY_TAGS,
58
  ])
58 59

  
59 60

  
60 61
#: default list of options for L{ListInstances}
......
66 67
def _ExpandMultiNames(mode, names, client=None):
67 68
  """Expand the given names using the passed mode.
68 69

  
69
  For _SHUTDOWN_CLUSTER, all instances will be returned. For
70
  _SHUTDOWN_NODES_PRI/SEC, all instances having those nodes as
71
  primary/secondary will be returned. For _SHUTDOWN_NODES_BOTH, all
70
  For _EXPAND_CLUSTER, all instances will be returned. For
71
  _EXPAND_NODES_PRI/SEC, all instances having those nodes as
72
  primary/secondary will be returned. For _EXPAND_NODES_BOTH, all
72 73
  instances having those nodes as either primary or secondary will be
73
  returned. For _SHUTDOWN_INSTANCES, the given instances will be
74
  returned. For _EXPAND_INSTANCES, the given instances will be
74 75
  returned.
75 76

  
76
  @param mode: one of L{_SHUTDOWN_CLUSTER}, L{_SHUTDOWN_NODES_BOTH},
77
      L{_SHUTDOWN_NODES_PRI}, L{_SHUTDOWN_NODES_SEC} or
78
      L{_SHUTDOWN_INSTANCES}
77
  @param mode: one of L{_EXPAND_CLUSTER}, L{_EXPAND_NODES_BOTH},
78
      L{_EXPAND_NODES_PRI}, L{_EXPAND_NODES_SEC} or
79
      L{_EXPAND_INSTANCES}
79 80
  @param names: a list of names; for cluster, it must be empty,
80 81
      and for node and instance it must be a list of valid item
81 82
      names (short names are valid as usual, e.g. node1 instead of
......
90 91

  
91 92
  if client is None:
92 93
    client = GetClient()
93
  if mode == _SHUTDOWN_CLUSTER:
94
  if mode == _EXPAND_CLUSTER:
94 95
    if names:
95 96
      raise errors.OpPrereqError("Cluster filter mode takes no arguments",
96 97
                                 errors.ECODE_INVAL)
97 98
    idata = client.QueryInstances([], ["name"], False)
98 99
    inames = [row[0] for row in idata]
99 100

  
100
  elif mode in (_SHUTDOWN_NODES_BOTH,
101
                _SHUTDOWN_NODES_PRI,
102
                _SHUTDOWN_NODES_SEC) + _SHUTDOWN_NODES_TAGS_MODES:
103
    if mode in _SHUTDOWN_NODES_TAGS_MODES:
101
  elif (mode in _EXPAND_NODES_TAGS_MODES or
102
        mode in (_EXPAND_NODES_BOTH, _EXPAND_NODES_PRI, _EXPAND_NODES_SEC)):
103
    if mode in _EXPAND_NODES_TAGS_MODES:
104 104
      if not names:
105 105
        raise errors.OpPrereqError("No node tags passed", errors.ECODE_INVAL)
106 106
      ndata = client.QueryNodes([], ["name", "pinst_list",
......
116 116
    pri_names = list(itertools.chain(*ipri))
117 117
    isec = [row[2] for row in ndata]
118 118
    sec_names = list(itertools.chain(*isec))
119
    if mode in (_SHUTDOWN_NODES_BOTH, _SHUTDOWN_NODES_BOTH_BY_TAGS):
119
    if mode in (_EXPAND_NODES_BOTH, _EXPAND_NODES_BOTH_BY_TAGS):
120 120
      inames = pri_names + sec_names
121
    elif mode in (_SHUTDOWN_NODES_PRI, _SHUTDOWN_NODES_PRI_BY_TAGS):
121
    elif mode in (_EXPAND_NODES_PRI, _EXPAND_NODES_PRI_BY_TAGS):
122 122
      inames = pri_names
123
    elif mode in (_SHUTDOWN_NODES_SEC, _SHUTDOWN_NODES_SEC_BY_TAGS):
123
    elif mode in (_EXPAND_NODES_SEC, _EXPAND_NODES_SEC_BY_TAGS):
124 124
      inames = sec_names
125 125
    else:
126 126
      raise errors.ProgrammerError("Unhandled shutdown type")
127
  elif mode == _SHUTDOWN_INSTANCES:
127
  elif mode == _EXPAND_INSTANCES:
128 128
    if not names:
129 129
      raise errors.OpPrereqError("No instance names passed",
130 130
                                 errors.ECODE_INVAL)
131 131
    idata = client.QueryInstances(names, ["name"], False)
132 132
    inames = [row[0] for row in idata]
133
  elif mode == _SHUTDOWN_INSTANCES_BY_TAGS:
133
  elif mode == _EXPAND_INSTANCES_BY_TAGS:
134 134
    if not names:
135 135
      raise errors.OpPrereqError("No instance tags passed",
136 136
                                 errors.ECODE_INVAL)
......
175 175
  """
176 176
  def realfn(opts, args):
177 177
    if opts.multi_mode is None:
178
      opts.multi_mode = _SHUTDOWN_INSTANCES
178
      opts.multi_mode = _EXPAND_INSTANCES
179 179
    cl = GetClient()
180 180
    inames = _ExpandMultiNames(opts.multi_mode, args, client=cl)
181 181
    if not inames:
182
      if opts.multi_mode == _SHUTDOWN_CLUSTER:
182
      if opts.multi_mode == _EXPAND_CLUSTER:
183 183
        ToStdout("Cluster is empty, no instances to shutdown")
184 184
        return 0
185 185
      raise errors.OpPrereqError("Selection filter does not match"
186 186
                                 " any instances", errors.ECODE_INVAL)
187
    multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
187
    multi_on = opts.multi_mode != _EXPAND_INSTANCES or len(inames) > 1
188 188
    if not (opts.force_multi or not multi_on
189 189
            or ConfirmOperation(inames, "instances", operation)):
190 190
      return 1
......
413 413
  """
414 414
  # first, compute the desired name list
415 415
  if opts.multi_mode is None:
416
    opts.multi_mode = _SHUTDOWN_INSTANCES
416
    opts.multi_mode = _EXPAND_INSTANCES
417 417

  
418 418
  inames = _ExpandMultiNames(opts.multi_mode, args)
419 419
  if not inames:
......
458 458
  # third, get confirmation: multi-reinstall requires --force-multi,
459 459
  # single-reinstall either --force or --force-multi (--force-multi is
460 460
  # a stronger --force)
461
  multi_on = opts.multi_mode != _SHUTDOWN_INSTANCES or len(inames) > 1
461
  multi_on = opts.multi_mode != _EXPAND_INSTANCES or len(inames) > 1
462 462
  if multi_on:
463 463
    warn_msg = ("Note: this will remove *all* data for the"
464 464
                " below instances! It will %s.\n" % os_msg)
......
1354 1354

  
1355 1355
m_pri_node_opt = cli_option("--primary", dest="multi_mode",
1356 1356
                            help="Filter by nodes (primary only)",
1357
                            const=_SHUTDOWN_NODES_PRI, action="store_const")
1357
                            const=_EXPAND_NODES_PRI, action="store_const")
1358 1358

  
1359 1359
m_sec_node_opt = cli_option("--secondary", dest="multi_mode",
1360 1360
                            help="Filter by nodes (secondary only)",
1361
                            const=_SHUTDOWN_NODES_SEC, action="store_const")
1361
                            const=_EXPAND_NODES_SEC, action="store_const")
1362 1362

  
1363 1363
m_node_opt = cli_option("--node", dest="multi_mode",
1364 1364
                        help="Filter by nodes (primary and secondary)",
1365
                        const=_SHUTDOWN_NODES_BOTH, action="store_const")
1365
                        const=_EXPAND_NODES_BOTH, action="store_const")
1366 1366

  
1367 1367
m_clust_opt = cli_option("--all", dest="multi_mode",
1368 1368
                         help="Select all instances in the cluster",
1369
                         const=_SHUTDOWN_CLUSTER, action="store_const")
1369
                         const=_EXPAND_CLUSTER, action="store_const")
1370 1370

  
1371 1371
m_inst_opt = cli_option("--instance", dest="multi_mode",
1372 1372
                        help="Filter by instance name [default]",
1373
                        const=_SHUTDOWN_INSTANCES, action="store_const")
1373
                        const=_EXPAND_INSTANCES, action="store_const")
1374 1374

  
1375 1375
m_node_tags_opt = cli_option("--node-tags", dest="multi_mode",
1376 1376
                             help="Filter by node tag",
1377
                             const=_SHUTDOWN_NODES_BOTH_BY_TAGS,
1377
                             const=_EXPAND_NODES_BOTH_BY_TAGS,
1378 1378
                             action="store_const")
1379 1379

  
1380 1380
m_pri_node_tags_opt = cli_option("--pri-node-tags", dest="multi_mode",
1381 1381
                                 help="Filter by primary node tag",
1382
                                 const=_SHUTDOWN_NODES_PRI_BY_TAGS,
1382
                                 const=_EXPAND_NODES_PRI_BY_TAGS,
1383 1383
                                 action="store_const")
1384 1384

  
1385 1385
m_sec_node_tags_opt = cli_option("--sec-node-tags", dest="multi_mode",
1386 1386
                                 help="Filter by secondary node tag",
1387
                                 const=_SHUTDOWN_NODES_SEC_BY_TAGS,
1387
                                 const=_EXPAND_NODES_SEC_BY_TAGS,
1388 1388
                                 action="store_const")
1389 1389

  
1390 1390
m_inst_tags_opt = cli_option("--tags", dest="multi_mode",
1391 1391
                             help="Filter by instance tag",
1392
                             const=_SHUTDOWN_INSTANCES_BY_TAGS,
1392
                             const=_EXPAND_INSTANCES_BY_TAGS,
1393 1393
                             action="store_const")
1394 1394

  
1395 1395
# this is defined separately due to readability only

Also available in: Unified diff