Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_group.py @ 66e884e1

History | View | Annotate | Download (4.5 kB)

1 667dbd6b Adeodato Simo
#
2 667dbd6b Adeodato Simo
#
3 667dbd6b Adeodato Simo
4 667dbd6b Adeodato Simo
# Copyright (C) 2010 Google Inc.
5 667dbd6b Adeodato Simo
#
6 667dbd6b Adeodato Simo
# This program is free software; you can redistribute it and/or modify
7 667dbd6b Adeodato Simo
# it under the terms of the GNU General Public License as published by
8 667dbd6b Adeodato Simo
# the Free Software Foundation; either version 2 of the License, or
9 667dbd6b Adeodato Simo
# (at your option) any later version.
10 667dbd6b Adeodato Simo
#
11 667dbd6b Adeodato Simo
# This program is distributed in the hope that it will be useful, but
12 667dbd6b Adeodato Simo
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 667dbd6b Adeodato Simo
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 667dbd6b Adeodato Simo
# General Public License for more details.
15 667dbd6b Adeodato Simo
#
16 667dbd6b Adeodato Simo
# You should have received a copy of the GNU General Public License
17 667dbd6b Adeodato Simo
# along with this program; if not, write to the Free Software
18 667dbd6b Adeodato Simo
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 667dbd6b Adeodato Simo
# 02110-1301, USA.
20 667dbd6b Adeodato Simo
21 667dbd6b Adeodato Simo
"""Node group related commands"""
22 667dbd6b Adeodato Simo
23 667dbd6b Adeodato Simo
# pylint: disable-msg=W0401,W0614
24 667dbd6b Adeodato Simo
# W0401: Wildcard import ganeti.cli
25 667dbd6b Adeodato Simo
# W0614: Unused import %s from wildcard import (since we need cli)
26 667dbd6b Adeodato Simo
27 667dbd6b Adeodato Simo
from ganeti.cli import *
28 667dbd6b Adeodato Simo
from ganeti import compat
29 66e884e1 Adeodato Simo
from ganeti import opcodes
30 4edc512c Adeodato Simo
from ganeti import utils
31 667dbd6b Adeodato Simo
32 667dbd6b Adeodato Simo
33 667dbd6b Adeodato Simo
#: default list of fields for L{ListGroups}
34 667dbd6b Adeodato Simo
_LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt"]
35 667dbd6b Adeodato Simo
36 667dbd6b Adeodato Simo
37 667dbd6b Adeodato Simo
#: headers (and full field list) for L{ListGroups}
38 667dbd6b Adeodato Simo
_LIST_HEADERS = {
39 667dbd6b Adeodato Simo
  "name": "Group", "uuid": "UUID",
40 667dbd6b Adeodato Simo
  "node_cnt": "Nodes", "node_list": "NodeList",
41 667dbd6b Adeodato Simo
  "pinst_cnt": "Instances", "pinst_list": "InstanceList",
42 4edc512c Adeodato Simo
  "ctime": "CTime", "mtime": "MTime", "serial_no": "SerialNo",
43 667dbd6b Adeodato Simo
}
44 667dbd6b Adeodato Simo
45 667dbd6b Adeodato Simo
46 66e884e1 Adeodato Simo
def AddGroup(opts, args):
47 66e884e1 Adeodato Simo
  """Add a node group to the cluster.
48 66e884e1 Adeodato Simo

49 66e884e1 Adeodato Simo
  @param opts: the command line options selected by the user
50 66e884e1 Adeodato Simo
  @type args: list
51 66e884e1 Adeodato Simo
  @param args: a list of length 1 with the name of the group to create
52 66e884e1 Adeodato Simo
  @rtype: int
53 66e884e1 Adeodato Simo
  @return: the desired exit code
54 66e884e1 Adeodato Simo

55 66e884e1 Adeodato Simo
  """
56 66e884e1 Adeodato Simo
  (group_name,) = args
57 66e884e1 Adeodato Simo
  op = opcodes.OpAddGroup(group_name=group_name)
58 66e884e1 Adeodato Simo
  SubmitOpCode(op, opts=opts)
59 66e884e1 Adeodato Simo
60 66e884e1 Adeodato Simo
61 667dbd6b Adeodato Simo
def ListGroups(opts, args):
62 667dbd6b Adeodato Simo
  """List node groups and their properties.
63 667dbd6b Adeodato Simo

64 667dbd6b Adeodato Simo
  @param opts: the command line options selected by the user
65 667dbd6b Adeodato Simo
  @type args: list
66 667dbd6b Adeodato Simo
  @param args: groups to list, or empty for all
67 667dbd6b Adeodato Simo
  @rtype: int
68 667dbd6b Adeodato Simo
  @return: the desired exit code
69 667dbd6b Adeodato Simo

70 667dbd6b Adeodato Simo
  """
71 667dbd6b Adeodato Simo
  desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
72 667dbd6b Adeodato Simo
73 667dbd6b Adeodato Simo
  output = GetClient().QueryGroups(args, desired_fields, opts.do_locking)
74 667dbd6b Adeodato Simo
75 667dbd6b Adeodato Simo
  if opts.no_headers:
76 667dbd6b Adeodato Simo
    headers = None
77 667dbd6b Adeodato Simo
  else:
78 667dbd6b Adeodato Simo
    headers = _LIST_HEADERS
79 667dbd6b Adeodato Simo
80 4edc512c Adeodato Simo
  int_type_fields = frozenset(["node_cnt", "pinst_cnt", "serial_no"])
81 667dbd6b Adeodato Simo
  list_type_fields = frozenset(["node_list", "pinst_list"])
82 4edc512c Adeodato Simo
  date_type_fields = frozenset(["mtime", "ctime"])
83 667dbd6b Adeodato Simo
84 667dbd6b Adeodato Simo
  for row in output:
85 667dbd6b Adeodato Simo
    for idx, field in enumerate(desired_fields):
86 667dbd6b Adeodato Simo
      val = row[idx]
87 667dbd6b Adeodato Simo
88 667dbd6b Adeodato Simo
      if field in list_type_fields:
89 667dbd6b Adeodato Simo
        val = ",".join(val)
90 667dbd6b Adeodato Simo
      elif opts.roman_integers and field in int_type_fields:
91 667dbd6b Adeodato Simo
        val = compat.TryToRoman(val)
92 4edc512c Adeodato Simo
      elif field in date_type_fields:
93 4edc512c Adeodato Simo
        val = utils.FormatTime(val)
94 667dbd6b Adeodato Simo
      elif val is None:
95 667dbd6b Adeodato Simo
        val = "?"
96 667dbd6b Adeodato Simo
97 667dbd6b Adeodato Simo
      row[idx] = str(val)
98 667dbd6b Adeodato Simo
99 667dbd6b Adeodato Simo
  data = GenerateTable(separator=opts.separator, headers=headers,
100 667dbd6b Adeodato Simo
                       fields=desired_fields, data=output)
101 667dbd6b Adeodato Simo
102 667dbd6b Adeodato Simo
  for line in data:
103 667dbd6b Adeodato Simo
    ToStdout(line)
104 667dbd6b Adeodato Simo
105 667dbd6b Adeodato Simo
  return 0
106 667dbd6b Adeodato Simo
107 667dbd6b Adeodato Simo
108 66e884e1 Adeodato Simo
def RemoveGroup(opts, args):
109 66e884e1 Adeodato Simo
  """Remove a node group from the cluster.
110 66e884e1 Adeodato Simo

111 66e884e1 Adeodato Simo
  @param opts: the command line options selected by the user
112 66e884e1 Adeodato Simo
  @type args: list
113 66e884e1 Adeodato Simo
  @param args: a list of length 1 with the name of the group to remove
114 66e884e1 Adeodato Simo
  @rtype: int
115 66e884e1 Adeodato Simo
  @return: the desired exit code
116 66e884e1 Adeodato Simo

117 66e884e1 Adeodato Simo
  """
118 66e884e1 Adeodato Simo
  (group_name,) = args
119 66e884e1 Adeodato Simo
  op = opcodes.OpRemoveGroup(group_name=group_name)
120 66e884e1 Adeodato Simo
  SubmitOpCode(op, opts=opts)
121 66e884e1 Adeodato Simo
122 66e884e1 Adeodato Simo
123 66e884e1 Adeodato Simo
def RenameGroup(opts, args):
124 66e884e1 Adeodato Simo
  """Rename a node group.
125 66e884e1 Adeodato Simo

126 66e884e1 Adeodato Simo
  @param opts: the command line options selected by the user
127 66e884e1 Adeodato Simo
  @type args: list
128 66e884e1 Adeodato Simo
  @param args: a list of length 2, [old_name, new_name]
129 66e884e1 Adeodato Simo
  @rtype: int
130 66e884e1 Adeodato Simo
  @return: the desired exit code
131 66e884e1 Adeodato Simo

132 66e884e1 Adeodato Simo
  """
133 66e884e1 Adeodato Simo
  old_name, new_name = args
134 66e884e1 Adeodato Simo
  op = opcodes.OpRenameGroup(old_name=old_name, new_name=new_name)
135 66e884e1 Adeodato Simo
  SubmitOpCode(op, opts=opts)
136 66e884e1 Adeodato Simo
137 66e884e1 Adeodato Simo
138 667dbd6b Adeodato Simo
commands = {
139 66e884e1 Adeodato Simo
  "add": (
140 66e884e1 Adeodato Simo
    AddGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
141 66e884e1 Adeodato Simo
    "<group_name>", "Add a new node group to the cluster"),
142 667dbd6b Adeodato Simo
  "list": (
143 667dbd6b Adeodato Simo
    ListGroups, ARGS_MANY_GROUPS,
144 667dbd6b Adeodato Simo
    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, SYNC_OPT, ROMAN_OPT],
145 4edc512c Adeodato Simo
    "[<group_name>...]",
146 4edc512c Adeodato Simo
    "Lists the node groups in the cluster. The available fields are (see"
147 4edc512c Adeodato Simo
    " the man page for details): %s. The default list is (in order): %s." %
148 4edc512c Adeodato Simo
    (utils.CommaJoin(_LIST_HEADERS), utils.CommaJoin(_LIST_DEF_FIELDS))),
149 66e884e1 Adeodato Simo
  "remove": (
150 66e884e1 Adeodato Simo
    RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
151 66e884e1 Adeodato Simo
    "[--dry-run] <group_name>",
152 66e884e1 Adeodato Simo
    "Remove an (empty) node group from the cluster"),
153 66e884e1 Adeodato Simo
  "rename": (
154 66e884e1 Adeodato Simo
    RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT],
155 66e884e1 Adeodato Simo
    "[--dry-run] <old_name> <new_name>", "Rename a node group"),
156 667dbd6b Adeodato Simo
}
157 667dbd6b Adeodato Simo
158 667dbd6b Adeodato Simo
159 667dbd6b Adeodato Simo
def Main():
160 667dbd6b Adeodato Simo
  return GenericMain(commands)