Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_group.py @ f0b1bafe

History | View | Annotate | Download (6.2 kB)

1 667dbd6b Adeodato Simo
#
2 667dbd6b Adeodato Simo
#
3 667dbd6b Adeodato Simo
4 f0b1bafe Iustin Pop
# Copyright (C) 2010, 2011 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 ca4ac9c9 Adeodato Simo
from ganeti import constants
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 b288b6f3 René Nussbaumer
_LIST_DEF_FIELDS = ["name", "node_cnt", "pinst_cnt", "alloc_policy", "ndparams"]
35 667dbd6b Adeodato Simo
36 667dbd6b Adeodato Simo
37 66e884e1 Adeodato Simo
def AddGroup(opts, args):
38 66e884e1 Adeodato Simo
  """Add a node group to the cluster.
39 66e884e1 Adeodato Simo

40 66e884e1 Adeodato Simo
  @param opts: the command line options selected by the user
41 66e884e1 Adeodato Simo
  @type args: list
42 66e884e1 Adeodato Simo
  @param args: a list of length 1 with the name of the group to create
43 66e884e1 Adeodato Simo
  @rtype: int
44 66e884e1 Adeodato Simo
  @return: the desired exit code
45 66e884e1 Adeodato Simo

46 66e884e1 Adeodato Simo
  """
47 66e884e1 Adeodato Simo
  (group_name,) = args
48 fabf1731 Iustin Pop
  op = opcodes.OpGroupAdd(group_name=group_name, ndparams=opts.ndparams,
49 90e99856 Adeodato Simo
                          alloc_policy=opts.alloc_policy)
50 66e884e1 Adeodato Simo
  SubmitOpCode(op, opts=opts)
51 66e884e1 Adeodato Simo
52 66e884e1 Adeodato Simo
53 919852da Adeodato Simo
def AssignNodes(opts, args):
54 919852da Adeodato Simo
  """Assign nodes to a group.
55 919852da Adeodato Simo

56 919852da Adeodato Simo
  @param opts: the command line options selected by the user
57 919852da Adeodato Simo
  @type args: list
58 919852da Adeodato Simo
  @param args: args[0]: group to assign nodes to; args[1:]: nodes to assign
59 919852da Adeodato Simo
  @rtype: int
60 919852da Adeodato Simo
  @return: the desired exit code
61 919852da Adeodato Simo

62 919852da Adeodato Simo
  """
63 919852da Adeodato Simo
  group_name = args[0]
64 919852da Adeodato Simo
  node_names = args[1:]
65 919852da Adeodato Simo
66 934704ae Iustin Pop
  op = opcodes.OpGroupAssignNodes(group_name=group_name, nodes=node_names,
67 919852da Adeodato Simo
                                  force=opts.force)
68 919852da Adeodato Simo
  SubmitOpCode(op, opts=opts)
69 919852da Adeodato Simo
70 919852da Adeodato Simo
71 b288b6f3 René Nussbaumer
def _FmtDict(data):
72 b288b6f3 René Nussbaumer
  """Format dict data into command-line format.
73 b288b6f3 René Nussbaumer

74 b288b6f3 René Nussbaumer
  @param data: The input dict to be formatted
75 b288b6f3 René Nussbaumer
  @return: The formatted dict
76 b288b6f3 René Nussbaumer

77 b288b6f3 René Nussbaumer
  """
78 b288b6f3 René Nussbaumer
  if not data:
79 b288b6f3 René Nussbaumer
    return "(empty)"
80 b288b6f3 René Nussbaumer
81 b288b6f3 René Nussbaumer
  return utils.CommaJoin(["%s=%s" % (key, value)
82 b288b6f3 René Nussbaumer
                          for key, value in data.items()])
83 b288b6f3 René Nussbaumer
84 b288b6f3 René Nussbaumer
85 667dbd6b Adeodato Simo
def ListGroups(opts, args):
86 667dbd6b Adeodato Simo
  """List node groups and their properties.
87 667dbd6b Adeodato Simo

88 667dbd6b Adeodato Simo
  @param opts: the command line options selected by the user
89 667dbd6b Adeodato Simo
  @type args: list
90 667dbd6b Adeodato Simo
  @param args: groups to list, or empty for all
91 667dbd6b Adeodato Simo
  @rtype: int
92 667dbd6b Adeodato Simo
  @return: the desired exit code
93 667dbd6b Adeodato Simo

94 667dbd6b Adeodato Simo
  """
95 667dbd6b Adeodato Simo
  desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
96 b288b6f3 René Nussbaumer
  fmtoverride = {
97 b288b6f3 René Nussbaumer
    "node_list": (",".join, False),
98 b288b6f3 René Nussbaumer
    "pinst_list": (",".join, False),
99 b288b6f3 René Nussbaumer
    "ndparams": (_FmtDict, False),
100 b288b6f3 René Nussbaumer
    }
101 667dbd6b Adeodato Simo
102 ca4ac9c9 Adeodato Simo
  return GenericList(constants.QR_GROUP, desired_fields, args, None,
103 ca4ac9c9 Adeodato Simo
                     opts.separator, not opts.no_headers,
104 f0b1bafe Iustin Pop
                     format_override=fmtoverride, verbose=opts.verbose)
105 667dbd6b Adeodato Simo
106 667dbd6b Adeodato Simo
107 ca4ac9c9 Adeodato Simo
def ListGroupFields(opts, args):
108 ca4ac9c9 Adeodato Simo
  """List node fields.
109 667dbd6b Adeodato Simo

110 ca4ac9c9 Adeodato Simo
  @param opts: the command line options selected by the user
111 ca4ac9c9 Adeodato Simo
  @type args: list
112 ca4ac9c9 Adeodato Simo
  @param args: fields to list, or empty for all
113 ca4ac9c9 Adeodato Simo
  @rtype: int
114 ca4ac9c9 Adeodato Simo
  @return: the desired exit code
115 667dbd6b Adeodato Simo

116 ca4ac9c9 Adeodato Simo
  """
117 ca4ac9c9 Adeodato Simo
  return GenericListFields(constants.QR_GROUP, args, opts.separator,
118 ca4ac9c9 Adeodato Simo
                           not opts.no_headers)
119 667dbd6b Adeodato Simo
120 667dbd6b Adeodato Simo
121 4da7909a Adeodato Simo
def SetGroupParams(opts, args):
122 4da7909a Adeodato Simo
  """Modifies a node group's parameters.
123 4da7909a Adeodato Simo

124 fecbc0b6 Stephen Shirley
  @param opts: the command line options selected by the user
125 4da7909a Adeodato Simo
  @type args: list
126 4da7909a Adeodato Simo
  @param args: should contain only one element, the node group name
127 4da7909a Adeodato Simo

128 4da7909a Adeodato Simo
  @rtype: int
129 4da7909a Adeodato Simo
  @return: the desired exit code
130 4da7909a Adeodato Simo

131 4da7909a Adeodato Simo
  """
132 4da7909a Adeodato Simo
  all_changes = {
133 4da7909a Adeodato Simo
    "ndparams": opts.ndparams,
134 90e99856 Adeodato Simo
    "alloc_policy": opts.alloc_policy,
135 4da7909a Adeodato Simo
  }
136 4da7909a Adeodato Simo
137 4da7909a Adeodato Simo
  if all_changes.values().count(None) == len(all_changes):
138 4da7909a Adeodato Simo
    ToStderr("Please give at least one of the parameters.")
139 4da7909a Adeodato Simo
    return 1
140 4da7909a Adeodato Simo
141 7cbf74f0 Iustin Pop
  op = opcodes.OpGroupSetParams(group_name=args[0], # pylint: disable-msg=W0142
142 90e99856 Adeodato Simo
                                **all_changes)
143 4da7909a Adeodato Simo
  result = SubmitOrSend(op, opts)
144 4da7909a Adeodato Simo
145 4da7909a Adeodato Simo
  if result:
146 4da7909a Adeodato Simo
    ToStdout("Modified node group %s", args[0])
147 4da7909a Adeodato Simo
    for param, data in result:
148 4da7909a Adeodato Simo
      ToStdout(" - %-5s -> %s", param, data)
149 4da7909a Adeodato Simo
150 4da7909a Adeodato Simo
  return 0
151 4da7909a Adeodato Simo
152 4da7909a Adeodato Simo
153 66e884e1 Adeodato Simo
def RemoveGroup(opts, args):
154 66e884e1 Adeodato Simo
  """Remove a node group from the cluster.
155 66e884e1 Adeodato Simo

156 66e884e1 Adeodato Simo
  @param opts: the command line options selected by the user
157 66e884e1 Adeodato Simo
  @type args: list
158 66e884e1 Adeodato Simo
  @param args: a list of length 1 with the name of the group to remove
159 66e884e1 Adeodato Simo
  @rtype: int
160 66e884e1 Adeodato Simo
  @return: the desired exit code
161 66e884e1 Adeodato Simo

162 66e884e1 Adeodato Simo
  """
163 66e884e1 Adeodato Simo
  (group_name,) = args
164 4d1baa51 Iustin Pop
  op = opcodes.OpGroupRemove(group_name=group_name)
165 66e884e1 Adeodato Simo
  SubmitOpCode(op, opts=opts)
166 66e884e1 Adeodato Simo
167 66e884e1 Adeodato Simo
168 66e884e1 Adeodato Simo
def RenameGroup(opts, args):
169 66e884e1 Adeodato Simo
  """Rename a node group.
170 66e884e1 Adeodato Simo

171 66e884e1 Adeodato Simo
  @param opts: the command line options selected by the user
172 66e884e1 Adeodato Simo
  @type args: list
173 66e884e1 Adeodato Simo
  @param args: a list of length 2, [old_name, new_name]
174 66e884e1 Adeodato Simo
  @rtype: int
175 66e884e1 Adeodato Simo
  @return: the desired exit code
176 66e884e1 Adeodato Simo

177 66e884e1 Adeodato Simo
  """
178 66e884e1 Adeodato Simo
  old_name, new_name = args
179 a8173e82 Iustin Pop
  op = opcodes.OpGroupRename(old_name=old_name, new_name=new_name)
180 66e884e1 Adeodato Simo
  SubmitOpCode(op, opts=opts)
181 66e884e1 Adeodato Simo
182 66e884e1 Adeodato Simo
183 667dbd6b Adeodato Simo
commands = {
184 66e884e1 Adeodato Simo
  "add": (
185 90e99856 Adeodato Simo
    AddGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
186 66e884e1 Adeodato Simo
    "<group_name>", "Add a new node group to the cluster"),
187 919852da Adeodato Simo
  "assign-nodes": (
188 919852da Adeodato Simo
    AssignNodes, ARGS_ONE_GROUP + ARGS_MANY_NODES, [DRY_RUN_OPT, FORCE_OPT],
189 919852da Adeodato Simo
    "<group_name> <node>...", "Assign nodes to a group"),
190 667dbd6b Adeodato Simo
  "list": (
191 667dbd6b Adeodato Simo
    ListGroups, ARGS_MANY_GROUPS,
192 f0b1bafe Iustin Pop
    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT],
193 4edc512c Adeodato Simo
    "[<group_name>...]",
194 ca4ac9c9 Adeodato Simo
    "Lists the node groups in the cluster. The available fields can be shown"
195 ca4ac9c9 Adeodato Simo
    " using the \"list-fields\" command (see the man page for details)."
196 ca4ac9c9 Adeodato Simo
    " The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
197 ca4ac9c9 Adeodato Simo
  "list-fields": (
198 ca4ac9c9 Adeodato Simo
    ListGroupFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]",
199 ca4ac9c9 Adeodato Simo
    "Lists all available fields for node groups"),
200 4da7909a Adeodato Simo
  "modify": (
201 4da7909a Adeodato Simo
    SetGroupParams, ARGS_ONE_GROUP,
202 90e99856 Adeodato Simo
    [DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
203 4da7909a Adeodato Simo
    "<group_name>", "Alters the parameters of a node group"),
204 66e884e1 Adeodato Simo
  "remove": (
205 66e884e1 Adeodato Simo
    RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
206 66e884e1 Adeodato Simo
    "[--dry-run] <group_name>",
207 66e884e1 Adeodato Simo
    "Remove an (empty) node group from the cluster"),
208 66e884e1 Adeodato Simo
  "rename": (
209 66e884e1 Adeodato Simo
    RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT],
210 66e884e1 Adeodato Simo
    "[--dry-run] <old_name> <new_name>", "Rename a node group"),
211 667dbd6b Adeodato Simo
}
212 667dbd6b Adeodato Simo
213 667dbd6b Adeodato Simo
214 667dbd6b Adeodato Simo
def Main():
215 667dbd6b Adeodato Simo
  return GenericMain(commands)