Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_group.py @ 6e80da8b

History | View | Annotate | Download (7.8 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 1b1a08e8 Michael Hanselmann
                     format_override=fmtoverride, verbose=opts.verbose,
105 1b1a08e8 Michael Hanselmann
                     force_filter=opts.force_filter)
106 667dbd6b Adeodato Simo
107 667dbd6b Adeodato Simo
108 ca4ac9c9 Adeodato Simo
def ListGroupFields(opts, args):
109 ca4ac9c9 Adeodato Simo
  """List node fields.
110 667dbd6b Adeodato Simo

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

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

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

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

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

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

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

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

178 66e884e1 Adeodato Simo
  """
179 12da663a Michael Hanselmann
  group_name, new_name = args
180 12da663a Michael Hanselmann
  op = opcodes.OpGroupRename(group_name=group_name, new_name=new_name)
181 66e884e1 Adeodato Simo
  SubmitOpCode(op, opts=opts)
182 66e884e1 Adeodato Simo
183 66e884e1 Adeodato Simo
184 f6eb380d Michael Hanselmann
def EvacuateGroup(opts, args):
185 f6eb380d Michael Hanselmann
  """Evacuate a node group.
186 f6eb380d Michael Hanselmann

187 f6eb380d Michael Hanselmann
  """
188 f6eb380d Michael Hanselmann
  (group_name, ) = args
189 f6eb380d Michael Hanselmann
190 f6eb380d Michael Hanselmann
  cl = GetClient()
191 f6eb380d Michael Hanselmann
192 f6eb380d Michael Hanselmann
  op = opcodes.OpGroupEvacuate(group_name=group_name,
193 f6eb380d Michael Hanselmann
                               iallocator=opts.iallocator,
194 f6eb380d Michael Hanselmann
                               target_groups=opts.to,
195 f6eb380d Michael Hanselmann
                               early_release=opts.early_release)
196 f6eb380d Michael Hanselmann
  result = SubmitOpCode(op, cl=cl, opts=opts)
197 f6eb380d Michael Hanselmann
198 f6eb380d Michael Hanselmann
  # Keep track of submitted jobs
199 f6eb380d Michael Hanselmann
  jex = JobExecutor(cl=cl, opts=opts)
200 f6eb380d Michael Hanselmann
201 f6eb380d Michael Hanselmann
  for (status, job_id) in result[constants.JOB_IDS_KEY]:
202 f6eb380d Michael Hanselmann
    jex.AddJobId(None, status, job_id)
203 f6eb380d Michael Hanselmann
204 f6eb380d Michael Hanselmann
  results = jex.GetResults()
205 f6eb380d Michael Hanselmann
  bad_cnt = len([row for row in results if not row[0]])
206 f6eb380d Michael Hanselmann
  if bad_cnt == 0:
207 f6eb380d Michael Hanselmann
    ToStdout("All instances evacuated successfully.")
208 f6eb380d Michael Hanselmann
    rcode = constants.EXIT_SUCCESS
209 f6eb380d Michael Hanselmann
  else:
210 f6eb380d Michael Hanselmann
    ToStdout("There were %s errors during the evacuation.", bad_cnt)
211 f6eb380d Michael Hanselmann
    rcode = constants.EXIT_FAILURE
212 f6eb380d Michael Hanselmann
213 f6eb380d Michael Hanselmann
  return rcode
214 f6eb380d Michael Hanselmann
215 6e80da8b Michael Hanselmann
216 667dbd6b Adeodato Simo
commands = {
217 66e884e1 Adeodato Simo
  "add": (
218 90e99856 Adeodato Simo
    AddGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
219 66e884e1 Adeodato Simo
    "<group_name>", "Add a new node group to the cluster"),
220 919852da Adeodato Simo
  "assign-nodes": (
221 919852da Adeodato Simo
    AssignNodes, ARGS_ONE_GROUP + ARGS_MANY_NODES, [DRY_RUN_OPT, FORCE_OPT],
222 919852da Adeodato Simo
    "<group_name> <node>...", "Assign nodes to a group"),
223 667dbd6b Adeodato Simo
  "list": (
224 667dbd6b Adeodato Simo
    ListGroups, ARGS_MANY_GROUPS,
225 1b1a08e8 Michael Hanselmann
    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT],
226 4edc512c Adeodato Simo
    "[<group_name>...]",
227 ca4ac9c9 Adeodato Simo
    "Lists the node groups in the cluster. The available fields can be shown"
228 ca4ac9c9 Adeodato Simo
    " using the \"list-fields\" command (see the man page for details)."
229 ca4ac9c9 Adeodato Simo
    " The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
230 ca4ac9c9 Adeodato Simo
  "list-fields": (
231 ca4ac9c9 Adeodato Simo
    ListGroupFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]",
232 ca4ac9c9 Adeodato Simo
    "Lists all available fields for node groups"),
233 4da7909a Adeodato Simo
  "modify": (
234 4da7909a Adeodato Simo
    SetGroupParams, ARGS_ONE_GROUP,
235 90e99856 Adeodato Simo
    [DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
236 4da7909a Adeodato Simo
    "<group_name>", "Alters the parameters of a node group"),
237 66e884e1 Adeodato Simo
  "remove": (
238 66e884e1 Adeodato Simo
    RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
239 12da663a Michael Hanselmann
    "[--dry-run] <group-name>",
240 66e884e1 Adeodato Simo
    "Remove an (empty) node group from the cluster"),
241 66e884e1 Adeodato Simo
  "rename": (
242 66e884e1 Adeodato Simo
    RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT],
243 12da663a Michael Hanselmann
    "[--dry-run] <group-name> <new-name>", "Rename a node group"),
244 f6eb380d Michael Hanselmann
  "evacuate": (
245 f6eb380d Michael Hanselmann
    EvacuateGroup, [ArgGroup(min=1, max=1)],
246 f6eb380d Michael Hanselmann
    [TO_GROUP_OPT, IALLOCATOR_OPT, EARLY_RELEASE_OPT],
247 6e80da8b Michael Hanselmann
    "[-I <iallocator>] [--to <group>]",
248 6e80da8b Michael Hanselmann
    "Evacuate all instances within a group"),
249 819cbfe5 Michael Hanselmann
  "list-tags": (
250 819cbfe5 Michael Hanselmann
    ListTags, ARGS_ONE_GROUP, [PRIORITY_OPT],
251 819cbfe5 Michael Hanselmann
    "<instance_name>", "List the tags of the given instance"),
252 819cbfe5 Michael Hanselmann
  "add-tags": (
253 819cbfe5 Michael Hanselmann
    AddTags, [ArgGroup(min=1, max=1), ArgUnknown()],
254 819cbfe5 Michael Hanselmann
    [TAG_SRC_OPT, PRIORITY_OPT],
255 819cbfe5 Michael Hanselmann
    "<instance_name> tag...", "Add tags to the given instance"),
256 819cbfe5 Michael Hanselmann
  "remove-tags": (
257 819cbfe5 Michael Hanselmann
    RemoveTags, [ArgGroup(min=1, max=1), ArgUnknown()],
258 819cbfe5 Michael Hanselmann
    [TAG_SRC_OPT, PRIORITY_OPT],
259 819cbfe5 Michael Hanselmann
    "<instance_name> tag...", "Remove tags from given instance"),
260 819cbfe5 Michael Hanselmann
  }
261 667dbd6b Adeodato Simo
262 667dbd6b Adeodato Simo
263 667dbd6b Adeodato Simo
def Main():
264 819cbfe5 Michael Hanselmann
  return GenericMain(commands,
265 819cbfe5 Michael Hanselmann
                     override={"tag_type": constants.TAG_NODEGROUP})