Revision 39dfd93e scripts/gnt-instance

b/scripts/gnt-instance
42 42
_SHUTDOWN_NODES_BOTH = "nodes"
43 43
_SHUTDOWN_NODES_PRI = "nodes-pri"
44 44
_SHUTDOWN_NODES_SEC = "nodes-sec"
45
_SHUTDOWN_NODES_BOTH_BY_TAGS = "nodes-by-tags"
46
_SHUTDOWN_NODES_PRI_BY_TAGS = "nodes-pri-by-tags"
47
_SHUTDOWN_NODES_SEC_BY_TAGS = "nodes-sec-by-tags"
45 48
_SHUTDOWN_INSTANCES = "instances"
49
_SHUTDOWN_INSTANCES_BY_TAGS = "instances-by-tags"
50

  
51
_SHUTDOWN_NODES_TAGS_MODES = (
52
    _SHUTDOWN_NODES_BOTH_BY_TAGS,
53
    _SHUTDOWN_NODES_PRI_BY_TAGS,
54
    _SHUTDOWN_NODES_SEC_BY_TAGS)
46 55

  
47 56

  
48 57
_VALUE_TRUE = "true"
......
77 86

  
78 87
  """
79 88
  # pylint: disable-msg=W0142
89

  
80 90
  if client is None:
81 91
    client = GetClient()
82 92
  if mode == _SHUTDOWN_CLUSTER:
......
88 98

  
89 99
  elif mode in (_SHUTDOWN_NODES_BOTH,
90 100
                _SHUTDOWN_NODES_PRI,
91
                _SHUTDOWN_NODES_SEC):
92
    if not names:
93
      raise errors.OpPrereqError("No node names passed", errors.ECODE_INVAL)
94
    ndata = client.QueryNodes(names, ["name", "pinst_list", "sinst_list"],
101
                _SHUTDOWN_NODES_SEC) + _SHUTDOWN_NODES_TAGS_MODES:
102
    if mode in _SHUTDOWN_NODES_TAGS_MODES:
103
      if not names:
104
        raise errors.OpPrereqError("No node tags passed", errors.ECODE_INVAL)
105
      ndata = client.QueryNodes([], ["name", "pinst_list",
106
                                     "sinst_list", "tags"], False)
107
      ndata = [row for row in ndata if set(row[3]).intersection(names)]
108
    else:
109
      if not names:
110
        raise errors.OpPrereqError("No node names passed", errors.ECODE_INVAL)
111
      ndata = client.QueryNodes(names, ["name", "pinst_list", "sinst_list"],
95 112
                              False)
113

  
96 114
    ipri = [row[1] for row in ndata]
97 115
    pri_names = list(itertools.chain(*ipri))
98 116
    isec = [row[2] for row in ndata]
99 117
    sec_names = list(itertools.chain(*isec))
100
    if mode == _SHUTDOWN_NODES_BOTH:
118
    if mode in (_SHUTDOWN_NODES_BOTH, _SHUTDOWN_NODES_BOTH_BY_TAGS):
101 119
      inames = pri_names + sec_names
102
    elif mode == _SHUTDOWN_NODES_PRI:
120
    elif mode in (_SHUTDOWN_NODES_PRI, _SHUTDOWN_NODES_PRI_BY_TAGS):
103 121
      inames = pri_names
104
    elif mode == _SHUTDOWN_NODES_SEC:
122
    elif mode in (_SHUTDOWN_NODES_SEC, _SHUTDOWN_NODES_SEC_BY_TAGS):
105 123
      inames = sec_names
106 124
    else:
107 125
      raise errors.ProgrammerError("Unhandled shutdown type")
108

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

  
132
  elif mode == _SHUTDOWN_INSTANCES_BY_TAGS:
133
    if not names:
134
      raise errors.OpPrereqError("No instance tags passed",
135
                                 errors.ECODE_INVAL)
136
    idata = client.QueryInstances([], ["name", "tags"], False)
137
    inames = [row[0] for row in idata if set(row[1]).intersection(names)]
116 138
  else:
117 139
    raise errors.OpPrereqError("Unknown mode '%s'" % mode, errors.ECODE_INVAL)
118 140

  
......
1269 1291
                        help="Filter by instance name [default]",
1270 1292
                        const=_SHUTDOWN_INSTANCES, action="store_const")
1271 1293

  
1294
m_node_tags_opt = cli_option("--node-tags", dest="multi_mode",
1295
                             help="Filter by node tag",
1296
                             const=_SHUTDOWN_NODES_BOTH_BY_TAGS,
1297
                             action="store_const")
1298

  
1299
m_pri_node_tags_opt = cli_option("--pri-node-tags", dest="multi_mode",
1300
                                 help="Filter by primary node tag",
1301
                                 const=_SHUTDOWN_NODES_PRI_BY_TAGS,
1302
                                 action="store_const")
1303

  
1304
m_sec_node_tags_opt = cli_option("--sec-node-tags", dest="multi_mode",
1305
                                 help="Filter by secondary node tag",
1306
                                 const=_SHUTDOWN_NODES_SEC_BY_TAGS,
1307
                                 action="store_const")
1308

  
1309
m_inst_tags_opt = cli_option("--tags", dest="multi_mode",
1310
                             help="Filter by instance tag",
1311
                             const=_SHUTDOWN_INSTANCES_BY_TAGS,
1312
                             action="store_const")
1272 1313

  
1273 1314
# this is defined separately due to readability only
1274 1315
add_opts = [
......
1344 1385
  'reinstall': (
1345 1386
    ReinstallInstance, [ArgInstance()],
1346 1387
    [FORCE_OPT, OS_OPT, FORCE_VARIANT_OPT, m_force_multi, m_node_opt,
1347
     m_pri_node_opt, m_sec_node_opt, m_clust_opt, m_inst_opt, SELECT_OS_OPT,
1388
     m_pri_node_opt, m_sec_node_opt, m_clust_opt, m_inst_opt, m_node_tags_opt,
1389
     m_pri_node_tags_opt, m_sec_node_tags_opt, m_inst_tags_opt, SELECT_OS_OPT,
1348 1390
     SUBMIT_OPT],
1349 1391
    "[-f] <instance>", "Reinstall a stopped instance"),
1350 1392
  'remove': (
......
1369 1411
  'shutdown': (
1370 1412
    GenericManyOps("shutdown", _ShutdownInstance), [ArgInstance()],
1371 1413
    [m_node_opt, m_pri_node_opt, m_sec_node_opt, m_clust_opt,
1372
     m_inst_opt, m_force_multi, TIMEOUT_OPT, SUBMIT_OPT],
1414
     m_node_tags_opt, m_pri_node_tags_opt, m_sec_node_tags_opt,
1415
     m_inst_tags_opt, m_inst_opt, m_force_multi, TIMEOUT_OPT, SUBMIT_OPT],
1373 1416
    "<instance>", "Stops an instance"),
1374 1417
  'startup': (
1375 1418
    GenericManyOps("startup", _StartupInstance), [ArgInstance()],
1376
    [FORCE_OPT, m_force_multi, m_node_opt, m_pri_node_opt,
1377
     m_sec_node_opt, m_clust_opt, m_inst_opt, SUBMIT_OPT, HVOPTS_OPT,
1419
    [FORCE_OPT, m_force_multi, m_node_opt, m_pri_node_opt, m_sec_node_opt,
1420
     m_node_tags_opt, m_pri_node_tags_opt, m_sec_node_tags_opt,
1421
     m_inst_tags_opt, m_clust_opt, m_inst_opt, SUBMIT_OPT, HVOPTS_OPT,
1378 1422
     BACKEND_OPT],
1379 1423
    "<instance>", "Starts an instance"),
1380 1424
  'reboot': (
1381 1425
    GenericManyOps("reboot", _RebootInstance), [ArgInstance()],
1382 1426
    [m_force_multi, REBOOT_TYPE_OPT, IGNORE_SECONDARIES_OPT, m_node_opt,
1383 1427
     m_pri_node_opt, m_sec_node_opt, m_clust_opt, m_inst_opt, SUBMIT_OPT,
1384
     SHUTDOWN_TIMEOUT_OPT],
1428
     m_node_tags_opt, m_pri_node_tags_opt, m_sec_node_tags_opt,
1429
     m_inst_tags_opt, SHUTDOWN_TIMEOUT_OPT],
1385 1430
    "<instance>", "Reboots an instance"),
1386 1431
  'activate-disks': (
1387 1432
    ActivateDisks, ARGS_ONE_INSTANCE, [SUBMIT_OPT, IGNORE_SIZE_OPT],

Also available in: Unified diff