Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_group.py @ 080fbeea

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 b459a848 Andrea Spadaccini
# pylint: disable=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 8e47b5da Michael Hanselmann
  if opts.ndparams is None and opts.alloc_policy is None:
134 4da7909a Adeodato Simo
    ToStderr("Please give at least one of the parameters.")
135 4da7909a Adeodato Simo
    return 1
136 4da7909a Adeodato Simo
137 8e47b5da Michael Hanselmann
  op = opcodes.OpGroupSetParams(group_name=args[0],
138 8e47b5da Michael Hanselmann
                                ndparams=opts.ndparams,
139 8e47b5da Michael Hanselmann
                                alloc_policy=opts.alloc_policy)
140 4da7909a Adeodato Simo
  result = SubmitOrSend(op, opts)
141 4da7909a Adeodato Simo
142 4da7909a Adeodato Simo
  if result:
143 4da7909a Adeodato Simo
    ToStdout("Modified node group %s", args[0])
144 4da7909a Adeodato Simo
    for param, data in result:
145 4da7909a Adeodato Simo
      ToStdout(" - %-5s -> %s", param, data)
146 4da7909a Adeodato Simo
147 4da7909a Adeodato Simo
  return 0
148 4da7909a Adeodato Simo
149 4da7909a Adeodato Simo
150 66e884e1 Adeodato Simo
def RemoveGroup(opts, args):
151 66e884e1 Adeodato Simo
  """Remove a node group from the cluster.
152 66e884e1 Adeodato Simo

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

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

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

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

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