| Branch: | Tag: | Revision:

root / lib / client / @ 57de31c0

History | View | Annotate | Download (7.9 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
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 ef9fa5b9 René Nussbaumer
_ENV_OVERRIDE = frozenset(["list"])
38 ef9fa5b9 René Nussbaumer
39 ef9fa5b9 René Nussbaumer
40 66e884e1 Adeodato Simo
def AddGroup(opts, args):
41 66e884e1 Adeodato Simo
  """Add a node group to the cluster.
42 66e884e1 Adeodato Simo

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

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

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

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

77 b288b6f3 René Nussbaumer
  @param data: The input dict to be formatted
78 b288b6f3 René Nussbaumer
  @return: The formatted dict
79 b288b6f3 René Nussbaumer

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

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

97 667dbd6b Adeodato Simo
98 667dbd6b Adeodato Simo
  desired_fields = ParseFields(opts.output, _LIST_DEF_FIELDS)
99 b288b6f3 René Nussbaumer
  fmtoverride = {
100 b288b6f3 René Nussbaumer
    "node_list": (",".join, False),
101 b288b6f3 René Nussbaumer
    "pinst_list": (",".join, False),
102 b288b6f3 René Nussbaumer
    "ndparams": (_FmtDict, False),
103 b288b6f3 René Nussbaumer
104 667dbd6b Adeodato Simo
105 ca4ac9c9 Adeodato Simo
  return GenericList(constants.QR_GROUP, desired_fields, args, None,
106 ca4ac9c9 Adeodato Simo
                     opts.separator, not opts.no_headers,
107 1b1a08e8 Michael Hanselmann
                     format_override=fmtoverride, verbose=opts.verbose,
108 1b1a08e8 Michael Hanselmann
109 667dbd6b Adeodato Simo
110 667dbd6b Adeodato Simo
111 ca4ac9c9 Adeodato Simo
def ListGroupFields(opts, args):
112 ca4ac9c9 Adeodato Simo
  """List node fields.
113 667dbd6b Adeodato Simo

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

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

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

132 4da7909a Adeodato Simo
  @rtype: int
133 4da7909a Adeodato Simo
  @return: the desired exit code
134 4da7909a Adeodato Simo

135 4da7909a Adeodato Simo
136 8e47b5da Michael Hanselmann
  if opts.ndparams is None and opts.alloc_policy is None:
137 4da7909a Adeodato Simo
    ToStderr("Please give at least one of the parameters.")
138 4da7909a Adeodato Simo
    return 1
139 4da7909a Adeodato Simo
140 8e47b5da Michael Hanselmann
  op = opcodes.OpGroupSetParams(group_name=args[0],
141 8e47b5da Michael Hanselmann
142 8e47b5da Michael Hanselmann
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 12da663a Michael Hanselmann
  group_name, new_name = args
179 12da663a Michael Hanselmann
  op = opcodes.OpGroupRename(group_name=group_name, new_name=new_name)
180 66e884e1 Adeodato Simo
  SubmitOpCode(op, opts=opts)
181 66e884e1 Adeodato Simo
182 66e884e1 Adeodato Simo
183 f6eb380d Michael Hanselmann
def EvacuateGroup(opts, args):
184 f6eb380d Michael Hanselmann
  """Evacuate a node group.
185 f6eb380d Michael Hanselmann

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