Statistics
| Branch: | Tag: | Revision:

root / lib / client / gnt_group.py @ 9133387e

History | View | Annotate | Download (6.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 667dbd6b Adeodato Simo
commands = {
185 66e884e1 Adeodato Simo
  "add": (
186 90e99856 Adeodato Simo
    AddGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
187 66e884e1 Adeodato Simo
    "<group_name>", "Add a new node group to the cluster"),
188 919852da Adeodato Simo
  "assign-nodes": (
189 919852da Adeodato Simo
    AssignNodes, ARGS_ONE_GROUP + ARGS_MANY_NODES, [DRY_RUN_OPT, FORCE_OPT],
190 919852da Adeodato Simo
    "<group_name> <node>...", "Assign nodes to a group"),
191 667dbd6b Adeodato Simo
  "list": (
192 667dbd6b Adeodato Simo
    ListGroups, ARGS_MANY_GROUPS,
193 1b1a08e8 Michael Hanselmann
    [NOHDR_OPT, SEP_OPT, FIELDS_OPT, VERBOSE_OPT, FORCE_FILTER_OPT],
194 4edc512c Adeodato Simo
    "[<group_name>...]",
195 ca4ac9c9 Adeodato Simo
    "Lists the node groups in the cluster. The available fields can be shown"
196 ca4ac9c9 Adeodato Simo
    " using the \"list-fields\" command (see the man page for details)."
197 ca4ac9c9 Adeodato Simo
    " The default list is (in order): %s." % utils.CommaJoin(_LIST_DEF_FIELDS)),
198 ca4ac9c9 Adeodato Simo
  "list-fields": (
199 ca4ac9c9 Adeodato Simo
    ListGroupFields, [ArgUnknown()], [NOHDR_OPT, SEP_OPT], "[fields...]",
200 ca4ac9c9 Adeodato Simo
    "Lists all available fields for node groups"),
201 4da7909a Adeodato Simo
  "modify": (
202 4da7909a Adeodato Simo
    SetGroupParams, ARGS_ONE_GROUP,
203 90e99856 Adeodato Simo
    [DRY_RUN_OPT, SUBMIT_OPT, ALLOC_POLICY_OPT, NODE_PARAMS_OPT],
204 4da7909a Adeodato Simo
    "<group_name>", "Alters the parameters of a node group"),
205 66e884e1 Adeodato Simo
  "remove": (
206 66e884e1 Adeodato Simo
    RemoveGroup, ARGS_ONE_GROUP, [DRY_RUN_OPT],
207 12da663a Michael Hanselmann
    "[--dry-run] <group-name>",
208 66e884e1 Adeodato Simo
    "Remove an (empty) node group from the cluster"),
209 66e884e1 Adeodato Simo
  "rename": (
210 66e884e1 Adeodato Simo
    RenameGroup, [ArgGroup(min=2, max=2)], [DRY_RUN_OPT],
211 12da663a Michael Hanselmann
    "[--dry-run] <group-name> <new-name>", "Rename a node group"),
212 819cbfe5 Michael Hanselmann
  "list-tags": (
213 819cbfe5 Michael Hanselmann
    ListTags, ARGS_ONE_GROUP, [PRIORITY_OPT],
214 819cbfe5 Michael Hanselmann
    "<instance_name>", "List the tags of the given instance"),
215 819cbfe5 Michael Hanselmann
  "add-tags": (
216 819cbfe5 Michael Hanselmann
    AddTags, [ArgGroup(min=1, max=1), ArgUnknown()],
217 819cbfe5 Michael Hanselmann
    [TAG_SRC_OPT, PRIORITY_OPT],
218 819cbfe5 Michael Hanselmann
    "<instance_name> tag...", "Add tags to the given instance"),
219 819cbfe5 Michael Hanselmann
  "remove-tags": (
220 819cbfe5 Michael Hanselmann
    RemoveTags, [ArgGroup(min=1, max=1), ArgUnknown()],
221 819cbfe5 Michael Hanselmann
    [TAG_SRC_OPT, PRIORITY_OPT],
222 819cbfe5 Michael Hanselmann
    "<instance_name> tag...", "Remove tags from given instance"),
223 819cbfe5 Michael Hanselmann
  }
224 667dbd6b Adeodato Simo
225 667dbd6b Adeodato Simo
226 667dbd6b Adeodato Simo
def Main():
227 819cbfe5 Michael Hanselmann
  return GenericMain(commands,
228 819cbfe5 Michael Hanselmann
                     override={"tag_type": constants.TAG_NODEGROUP})