Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 4b9e8c93

History | View | Annotate | Download (36.1 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 687c10d9 Iustin Pop
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Google Inc.
5 a8083063 Iustin Pop
#
6 a8083063 Iustin Pop
# This program is free software; you can redistribute it and/or modify
7 a8083063 Iustin Pop
# it under the terms of the GNU General Public License as published by
8 a8083063 Iustin Pop
# the Free Software Foundation; either version 2 of the License, or
9 a8083063 Iustin Pop
# (at your option) any later version.
10 a8083063 Iustin Pop
#
11 a8083063 Iustin Pop
# This program is distributed in the hope that it will be useful, but
12 a8083063 Iustin Pop
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 a8083063 Iustin Pop
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 a8083063 Iustin Pop
# General Public License for more details.
15 a8083063 Iustin Pop
#
16 a8083063 Iustin Pop
# You should have received a copy of the GNU General Public License
17 a8083063 Iustin Pop
# along with this program; if not, write to the Free Software
18 a8083063 Iustin Pop
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 a8083063 Iustin Pop
# 02110-1301, USA.
20 a8083063 Iustin Pop
21 a8083063 Iustin Pop
22 a8083063 Iustin Pop
"""OpCodes module
23 a8083063 Iustin Pop

24 a8083063 Iustin Pop
This module implements the data structures which define the cluster
25 a8083063 Iustin Pop
operations - the so-called opcodes.
26 a8083063 Iustin Pop

27 0e46916d Iustin Pop
Every operation which modifies the cluster state is expressed via
28 0e46916d Iustin Pop
opcodes.
29 a8083063 Iustin Pop

30 a8083063 Iustin Pop
"""
31 a8083063 Iustin Pop
32 a8083063 Iustin Pop
# this are practically structures, so disable the message about too
33 a8083063 Iustin Pop
# few public methods:
34 a8083063 Iustin Pop
# pylint: disable-msg=R0903
35 a8083063 Iustin Pop
36 1cbef6d8 Michael Hanselmann
import logging
37 ff0d18e6 Iustin Pop
import re
38 1cbef6d8 Michael Hanselmann
39 65e183af Michael Hanselmann
from ganeti import constants
40 65e183af Michael Hanselmann
from ganeti import errors
41 65e183af Michael Hanselmann
from ganeti import ht
42 65e183af Michael Hanselmann
43 65e183af Michael Hanselmann
44 65e183af Michael Hanselmann
# Common opcode attributes
45 65e183af Michael Hanselmann
46 65e183af Michael Hanselmann
#: output fields for a query operation
47 65e183af Michael Hanselmann
_POutputFields = ("output_fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString))
48 65e183af Michael Hanselmann
49 65e183af Michael Hanselmann
#: the shutdown timeout
50 65e183af Michael Hanselmann
_PShutdownTimeout = ("shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
51 65e183af Michael Hanselmann
                     ht.TPositiveInt)
52 65e183af Michael Hanselmann
53 65e183af Michael Hanselmann
#: the force parameter
54 65e183af Michael Hanselmann
_PForce = ("force", False, ht.TBool)
55 65e183af Michael Hanselmann
56 65e183af Michael Hanselmann
#: a required instance name (for single-instance LUs)
57 65e183af Michael Hanselmann
_PInstanceName = ("instance_name", ht.NoDefault, ht.TNonEmptyString)
58 65e183af Michael Hanselmann
59 65e183af Michael Hanselmann
#: Whether to ignore offline nodes
60 65e183af Michael Hanselmann
_PIgnoreOfflineNodes = ("ignore_offline_nodes", False, ht.TBool)
61 65e183af Michael Hanselmann
62 65e183af Michael Hanselmann
#: a required node name (for single-node LUs)
63 65e183af Michael Hanselmann
_PNodeName = ("node_name", ht.NoDefault, ht.TNonEmptyString)
64 65e183af Michael Hanselmann
65 65e183af Michael Hanselmann
#: a required node group name (for single-group LUs)
66 65e183af Michael Hanselmann
_PGroupName = ("group_name", ht.NoDefault, ht.TNonEmptyString)
67 65e183af Michael Hanselmann
68 65e183af Michael Hanselmann
#: Migration type (live/non-live)
69 65e183af Michael Hanselmann
_PMigrationMode = ("mode", None,
70 65e183af Michael Hanselmann
                   ht.TOr(ht.TNone, ht.TElemOf(constants.HT_MIGRATION_MODES)))
71 65e183af Michael Hanselmann
72 65e183af Michael Hanselmann
#: Obsolete 'live' migration mode (boolean)
73 65e183af Michael Hanselmann
_PMigrationLive = ("live", None, ht.TMaybeBool)
74 65e183af Michael Hanselmann
75 65e183af Michael Hanselmann
#: Tag type
76 65e183af Michael Hanselmann
_PTagKind = ("kind", ht.NoDefault, ht.TElemOf(constants.VALID_TAG_TYPES))
77 65e183af Michael Hanselmann
78 65e183af Michael Hanselmann
#: List of tag strings
79 65e183af Michael Hanselmann
_PTags = ("tags", ht.NoDefault, ht.TListOf(ht.TNonEmptyString))
80 65e183af Michael Hanselmann
81 4b9e8c93 Iustin Pop
#: Ignore consistency
82 4b9e8c93 Iustin Pop
_PIgnoreConsistency = ("ignore_consistency", False, ht.TBool)
83 4b9e8c93 Iustin Pop
84 ff0d18e6 Iustin Pop
#: OP_ID conversion regular expression
85 ff0d18e6 Iustin Pop
_OPID_RE = re.compile("([a-z])([A-Z])")
86 ff0d18e6 Iustin Pop
87 ff0d18e6 Iustin Pop
88 ff0d18e6 Iustin Pop
def _NameToId(name):
89 ff0d18e6 Iustin Pop
  """Convert an opcode class name to an OP_ID.
90 ff0d18e6 Iustin Pop

91 ff0d18e6 Iustin Pop
  @type name: string
92 ff0d18e6 Iustin Pop
  @param name: the class name, as OpXxxYyy
93 ff0d18e6 Iustin Pop
  @rtype: string
94 ff0d18e6 Iustin Pop
  @return: the name in the OP_XXXX_YYYY format
95 ff0d18e6 Iustin Pop

96 ff0d18e6 Iustin Pop
  """
97 ff0d18e6 Iustin Pop
  if not name.startswith("Op"):
98 ff0d18e6 Iustin Pop
    return None
99 ff0d18e6 Iustin Pop
  # Note: (?<=[a-z])(?=[A-Z]) would be ideal, since it wouldn't
100 ff0d18e6 Iustin Pop
  # consume any input, and hence we would just have all the elements
101 ff0d18e6 Iustin Pop
  # in the list, one by one; but it seems that split doesn't work on
102 ff0d18e6 Iustin Pop
  # non-consuming input, hence we have to process the input string a
103 ff0d18e6 Iustin Pop
  # bit
104 ff0d18e6 Iustin Pop
  name = _OPID_RE.sub(r"\1,\2", name)
105 ff0d18e6 Iustin Pop
  elems = name.split(",")
106 ff0d18e6 Iustin Pop
  return "_".join(n.upper() for n in elems)
107 ff0d18e6 Iustin Pop
108 65e183af Michael Hanselmann
109 65e183af Michael Hanselmann
def RequireFileStorage():
110 65e183af Michael Hanselmann
  """Checks that file storage is enabled.
111 65e183af Michael Hanselmann

112 65e183af Michael Hanselmann
  While it doesn't really fit into this module, L{utils} was deemed too large
113 65e183af Michael Hanselmann
  of a dependency to be imported for just one or two functions.
114 65e183af Michael Hanselmann

115 65e183af Michael Hanselmann
  @raise errors.OpPrereqError: when file storage is disabled
116 65e183af Michael Hanselmann

117 65e183af Michael Hanselmann
  """
118 65e183af Michael Hanselmann
  if not constants.ENABLE_FILE_STORAGE:
119 65e183af Michael Hanselmann
    raise errors.OpPrereqError("File storage disabled at configure time",
120 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
121 65e183af Michael Hanselmann
122 65e183af Michael Hanselmann
123 65e183af Michael Hanselmann
def _CheckDiskTemplate(template):
124 65e183af Michael Hanselmann
  """Ensure a given disk template is valid.
125 65e183af Michael Hanselmann

126 65e183af Michael Hanselmann
  """
127 65e183af Michael Hanselmann
  if template not in constants.DISK_TEMPLATES:
128 65e183af Michael Hanselmann
    # Using str.join directly to avoid importing utils for CommaJoin
129 65e183af Michael Hanselmann
    msg = ("Invalid disk template name '%s', valid templates are: %s" %
130 65e183af Michael Hanselmann
           (template, ", ".join(constants.DISK_TEMPLATES)))
131 65e183af Michael Hanselmann
    raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
132 65e183af Michael Hanselmann
  if template == constants.DT_FILE:
133 65e183af Michael Hanselmann
    RequireFileStorage()
134 65e183af Michael Hanselmann
  return True
135 65e183af Michael Hanselmann
136 65e183af Michael Hanselmann
137 65e183af Michael Hanselmann
def _CheckStorageType(storage_type):
138 65e183af Michael Hanselmann
  """Ensure a given storage type is valid.
139 65e183af Michael Hanselmann

140 65e183af Michael Hanselmann
  """
141 65e183af Michael Hanselmann
  if storage_type not in constants.VALID_STORAGE_TYPES:
142 65e183af Michael Hanselmann
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
143 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
144 65e183af Michael Hanselmann
  if storage_type == constants.ST_FILE:
145 65e183af Michael Hanselmann
    RequireFileStorage()
146 65e183af Michael Hanselmann
  return True
147 65e183af Michael Hanselmann
148 65e183af Michael Hanselmann
149 65e183af Michael Hanselmann
#: Storage type parameter
150 65e183af Michael Hanselmann
_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType)
151 65e183af Michael Hanselmann
152 65e183af Michael Hanselmann
153 65e183af Michael Hanselmann
class _AutoOpParamSlots(type):
154 65e183af Michael Hanselmann
  """Meta class for opcode definitions.
155 65e183af Michael Hanselmann

156 65e183af Michael Hanselmann
  """
157 65e183af Michael Hanselmann
  def __new__(mcs, name, bases, attrs):
158 65e183af Michael Hanselmann
    """Called when a class should be created.
159 65e183af Michael Hanselmann

160 65e183af Michael Hanselmann
    @param mcs: The meta class
161 65e183af Michael Hanselmann
    @param name: Name of created class
162 65e183af Michael Hanselmann
    @param bases: Base classes
163 65e183af Michael Hanselmann
    @type attrs: dict
164 65e183af Michael Hanselmann
    @param attrs: Class attributes
165 65e183af Michael Hanselmann

166 65e183af Michael Hanselmann
    """
167 65e183af Michael Hanselmann
    assert "__slots__" not in attrs, \
168 65e183af Michael Hanselmann
      "Class '%s' defines __slots__ when it should use OP_PARAMS" % name
169 e89a9021 Iustin Pop
    assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
170 ff0d18e6 Iustin Pop
171 e89a9021 Iustin Pop
    attrs["OP_ID"] = _NameToId(name)
172 65e183af Michael Hanselmann
173 65e183af Michael Hanselmann
    # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
174 65e183af Michael Hanselmann
    params = attrs.setdefault("OP_PARAMS", [])
175 65e183af Michael Hanselmann
176 65e183af Michael Hanselmann
    # Use parameter names as slots
177 65e183af Michael Hanselmann
    slots = [pname for (pname, _, _) in params]
178 65e183af Michael Hanselmann
179 65e183af Michael Hanselmann
    assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
180 65e183af Michael Hanselmann
      "Class '%s' uses unknown field in OP_DSC_FIELD" % name
181 65e183af Michael Hanselmann
182 65e183af Michael Hanselmann
    attrs["__slots__"] = slots
183 65e183af Michael Hanselmann
184 65e183af Michael Hanselmann
    return type.__new__(mcs, name, bases, attrs)
185 65e183af Michael Hanselmann
186 df458e0b Iustin Pop
187 0e46916d Iustin Pop
class BaseOpCode(object):
188 df458e0b Iustin Pop
  """A simple serializable object.
189 df458e0b Iustin Pop

190 0e46916d Iustin Pop
  This object serves as a parent class for OpCode without any custom
191 0e46916d Iustin Pop
  field handling.
192 0e46916d Iustin Pop

193 df458e0b Iustin Pop
  """
194 e89a9021 Iustin Pop
  # pylint: disable-msg=E1101
195 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
196 65e183af Michael Hanselmann
  __metaclass__ = _AutoOpParamSlots
197 65e183af Michael Hanselmann
198 a8083063 Iustin Pop
  def __init__(self, **kwargs):
199 a7399f66 Iustin Pop
    """Constructor for BaseOpCode.
200 a7399f66 Iustin Pop

201 a7399f66 Iustin Pop
    The constructor takes only keyword arguments and will set
202 a7399f66 Iustin Pop
    attributes on this object based on the passed arguments. As such,
203 a7399f66 Iustin Pop
    it means that you should not pass arguments which are not in the
204 a7399f66 Iustin Pop
    __slots__ attribute for this class.
205 a7399f66 Iustin Pop

206 a7399f66 Iustin Pop
    """
207 adf385c7 Iustin Pop
    slots = self._all_slots()
208 a8083063 Iustin Pop
    for key in kwargs:
209 adf385c7 Iustin Pop
      if key not in slots:
210 df458e0b Iustin Pop
        raise TypeError("Object %s doesn't support the parameter '%s'" %
211 3ecf6786 Iustin Pop
                        (self.__class__.__name__, key))
212 a8083063 Iustin Pop
      setattr(self, key, kwargs[key])
213 a8083063 Iustin Pop
214 df458e0b Iustin Pop
  def __getstate__(self):
215 a7399f66 Iustin Pop
    """Generic serializer.
216 a7399f66 Iustin Pop

217 a7399f66 Iustin Pop
    This method just returns the contents of the instance as a
218 a7399f66 Iustin Pop
    dictionary.
219 a7399f66 Iustin Pop

220 a7399f66 Iustin Pop
    @rtype:  C{dict}
221 a7399f66 Iustin Pop
    @return: the instance attributes and their values
222 a7399f66 Iustin Pop

223 a7399f66 Iustin Pop
    """
224 df458e0b Iustin Pop
    state = {}
225 adf385c7 Iustin Pop
    for name in self._all_slots():
226 df458e0b Iustin Pop
      if hasattr(self, name):
227 df458e0b Iustin Pop
        state[name] = getattr(self, name)
228 df458e0b Iustin Pop
    return state
229 df458e0b Iustin Pop
230 df458e0b Iustin Pop
  def __setstate__(self, state):
231 a7399f66 Iustin Pop
    """Generic unserializer.
232 a7399f66 Iustin Pop

233 a7399f66 Iustin Pop
    This method just restores from the serialized state the attributes
234 a7399f66 Iustin Pop
    of the current instance.
235 a7399f66 Iustin Pop

236 a7399f66 Iustin Pop
    @param state: the serialized opcode data
237 a7399f66 Iustin Pop
    @type state:  C{dict}
238 a7399f66 Iustin Pop

239 a7399f66 Iustin Pop
    """
240 df458e0b Iustin Pop
    if not isinstance(state, dict):
241 df458e0b Iustin Pop
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
242 df458e0b Iustin Pop
                       type(state))
243 df458e0b Iustin Pop
244 adf385c7 Iustin Pop
    for name in self._all_slots():
245 44db3a6f Iustin Pop
      if name not in state and hasattr(self, name):
246 df458e0b Iustin Pop
        delattr(self, name)
247 df458e0b Iustin Pop
248 df458e0b Iustin Pop
    for name in state:
249 df458e0b Iustin Pop
      setattr(self, name, state[name])
250 df458e0b Iustin Pop
251 adf385c7 Iustin Pop
  @classmethod
252 adf385c7 Iustin Pop
  def _all_slots(cls):
253 adf385c7 Iustin Pop
    """Compute the list of all declared slots for a class.
254 adf385c7 Iustin Pop

255 adf385c7 Iustin Pop
    """
256 adf385c7 Iustin Pop
    slots = []
257 adf385c7 Iustin Pop
    for parent in cls.__mro__:
258 adf385c7 Iustin Pop
      slots.extend(getattr(parent, "__slots__", []))
259 adf385c7 Iustin Pop
    return slots
260 adf385c7 Iustin Pop
261 65e183af Michael Hanselmann
  @classmethod
262 65e183af Michael Hanselmann
  def GetAllParams(cls):
263 65e183af Michael Hanselmann
    """Compute list of all parameters for an opcode.
264 65e183af Michael Hanselmann

265 65e183af Michael Hanselmann
    """
266 65e183af Michael Hanselmann
    slots = []
267 65e183af Michael Hanselmann
    for parent in cls.__mro__:
268 65e183af Michael Hanselmann
      slots.extend(getattr(parent, "OP_PARAMS", []))
269 65e183af Michael Hanselmann
    return slots
270 65e183af Michael Hanselmann
271 1cbef6d8 Michael Hanselmann
  def Validate(self, set_defaults):
272 1cbef6d8 Michael Hanselmann
    """Validate opcode parameters, optionally setting default values.
273 1cbef6d8 Michael Hanselmann

274 1cbef6d8 Michael Hanselmann
    @type set_defaults: bool
275 1cbef6d8 Michael Hanselmann
    @param set_defaults: Whether to set default values
276 1cbef6d8 Michael Hanselmann
    @raise errors.OpPrereqError: When a parameter value doesn't match
277 1cbef6d8 Michael Hanselmann
                                 requirements
278 1cbef6d8 Michael Hanselmann

279 1cbef6d8 Michael Hanselmann
    """
280 1cbef6d8 Michael Hanselmann
    for (attr_name, default, test) in self.GetAllParams():
281 1cbef6d8 Michael Hanselmann
      assert test == ht.NoType or callable(test)
282 1cbef6d8 Michael Hanselmann
283 1cbef6d8 Michael Hanselmann
      if not hasattr(self, attr_name):
284 1cbef6d8 Michael Hanselmann
        if default == ht.NoDefault:
285 1cbef6d8 Michael Hanselmann
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
286 1cbef6d8 Michael Hanselmann
                                     (self.OP_ID, attr_name),
287 1cbef6d8 Michael Hanselmann
                                     errors.ECODE_INVAL)
288 1cbef6d8 Michael Hanselmann
        elif set_defaults:
289 1cbef6d8 Michael Hanselmann
          if callable(default):
290 1cbef6d8 Michael Hanselmann
            dval = default()
291 1cbef6d8 Michael Hanselmann
          else:
292 1cbef6d8 Michael Hanselmann
            dval = default
293 1cbef6d8 Michael Hanselmann
          setattr(self, attr_name, dval)
294 1cbef6d8 Michael Hanselmann
295 1cbef6d8 Michael Hanselmann
      if test == ht.NoType:
296 1cbef6d8 Michael Hanselmann
        # no tests here
297 1cbef6d8 Michael Hanselmann
        continue
298 1cbef6d8 Michael Hanselmann
299 1cbef6d8 Michael Hanselmann
      if set_defaults or hasattr(self, attr_name):
300 1cbef6d8 Michael Hanselmann
        attr_val = getattr(self, attr_name)
301 1cbef6d8 Michael Hanselmann
        if not test(attr_val):
302 1cbef6d8 Michael Hanselmann
          logging.error("OpCode %s, parameter %s, has invalid type %s/value %s",
303 1cbef6d8 Michael Hanselmann
                        self.OP_ID, attr_name, type(attr_val), attr_val)
304 1cbef6d8 Michael Hanselmann
          raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
305 1cbef6d8 Michael Hanselmann
                                     (self.OP_ID, attr_name),
306 1cbef6d8 Michael Hanselmann
                                     errors.ECODE_INVAL)
307 1cbef6d8 Michael Hanselmann
308 df458e0b Iustin Pop
309 0e46916d Iustin Pop
class OpCode(BaseOpCode):
310 a7399f66 Iustin Pop
  """Abstract OpCode.
311 a7399f66 Iustin Pop

312 a7399f66 Iustin Pop
  This is the root of the actual OpCode hierarchy. All clases derived
313 a7399f66 Iustin Pop
  from this class should override OP_ID.
314 a7399f66 Iustin Pop

315 a7399f66 Iustin Pop
  @cvar OP_ID: The ID of this opcode. This should be unique amongst all
316 20777413 Iustin Pop
               children of this class.
317 bde8f481 Adeodato Simo
  @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
318 bde8f481 Adeodato Simo
                      string returned by Summary(); see the docstring of that
319 bde8f481 Adeodato Simo
                      method for details).
320 65e183af Michael Hanselmann
  @cvar OP_PARAMS: List of opcode attributes, the default values they should
321 65e183af Michael Hanselmann
                   get if not already defined, and types they must match.
322 687c10d9 Iustin Pop
  @cvar WITH_LU: Boolean that specifies whether this should be included in
323 687c10d9 Iustin Pop
      mcpu's dispatch table
324 20777413 Iustin Pop
  @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
325 20777413 Iustin Pop
                 the check steps
326 8f5c488d Michael Hanselmann
  @ivar priority: Opcode priority for queue
327 a7399f66 Iustin Pop

328 a7399f66 Iustin Pop
  """
329 e89a9021 Iustin Pop
  # pylint: disable-msg=E1101
330 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
331 687c10d9 Iustin Pop
  WITH_LU = True
332 65e183af Michael Hanselmann
  OP_PARAMS = [
333 65e183af Michael Hanselmann
    ("dry_run", None, ht.TMaybeBool),
334 65e183af Michael Hanselmann
    ("debug_level", None, ht.TOr(ht.TNone, ht.TPositiveInt)),
335 65e183af Michael Hanselmann
    ("priority", constants.OP_PRIO_DEFAULT,
336 65e183af Michael Hanselmann
     ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID)),
337 65e183af Michael Hanselmann
    ]
338 df458e0b Iustin Pop
339 df458e0b Iustin Pop
  def __getstate__(self):
340 df458e0b Iustin Pop
    """Specialized getstate for opcodes.
341 df458e0b Iustin Pop

342 a7399f66 Iustin Pop
    This method adds to the state dictionary the OP_ID of the class,
343 a7399f66 Iustin Pop
    so that on unload we can identify the correct class for
344 a7399f66 Iustin Pop
    instantiating the opcode.
345 a7399f66 Iustin Pop

346 a7399f66 Iustin Pop
    @rtype:   C{dict}
347 a7399f66 Iustin Pop
    @return:  the state as a dictionary
348 a7399f66 Iustin Pop

349 df458e0b Iustin Pop
    """
350 0e46916d Iustin Pop
    data = BaseOpCode.__getstate__(self)
351 df458e0b Iustin Pop
    data["OP_ID"] = self.OP_ID
352 df458e0b Iustin Pop
    return data
353 df458e0b Iustin Pop
354 df458e0b Iustin Pop
  @classmethod
355 00abdc96 Iustin Pop
  def LoadOpCode(cls, data):
356 df458e0b Iustin Pop
    """Generic load opcode method.
357 df458e0b Iustin Pop

358 a7399f66 Iustin Pop
    The method identifies the correct opcode class from the dict-form
359 a7399f66 Iustin Pop
    by looking for a OP_ID key, if this is not found, or its value is
360 a7399f66 Iustin Pop
    not available in this module as a child of this class, we fail.
361 a7399f66 Iustin Pop

362 a7399f66 Iustin Pop
    @type data:  C{dict}
363 a7399f66 Iustin Pop
    @param data: the serialized opcode
364 a7399f66 Iustin Pop

365 df458e0b Iustin Pop
    """
366 df458e0b Iustin Pop
    if not isinstance(data, dict):
367 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
368 df458e0b Iustin Pop
    if "OP_ID" not in data:
369 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
370 df458e0b Iustin Pop
    op_id = data["OP_ID"]
371 df458e0b Iustin Pop
    op_class = None
372 363acb1e Iustin Pop
    if op_id in OP_MAPPING:
373 363acb1e Iustin Pop
      op_class = OP_MAPPING[op_id]
374 363acb1e Iustin Pop
    else:
375 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
376 df458e0b Iustin Pop
                       op_id)
377 df458e0b Iustin Pop
    op = op_class()
378 df458e0b Iustin Pop
    new_data = data.copy()
379 df458e0b Iustin Pop
    del new_data["OP_ID"]
380 df458e0b Iustin Pop
    op.__setstate__(new_data)
381 df458e0b Iustin Pop
    return op
382 df458e0b Iustin Pop
383 60dd1473 Iustin Pop
  def Summary(self):
384 60dd1473 Iustin Pop
    """Generates a summary description of this opcode.
385 60dd1473 Iustin Pop

386 ff0d18e6 Iustin Pop
    The summary is the value of the OP_ID attribute (without the "OP_"
387 ff0d18e6 Iustin Pop
    prefix), plus the value of the OP_DSC_FIELD attribute, if one was
388 ff0d18e6 Iustin Pop
    defined; this field should allow to easily identify the operation
389 ff0d18e6 Iustin Pop
    (for an instance creation job, e.g., it would be the instance
390 ff0d18e6 Iustin Pop
    name).
391 bde8f481 Adeodato Simo

392 60dd1473 Iustin Pop
    """
393 ff0d18e6 Iustin Pop
    assert self.OP_ID is not None and len(self.OP_ID) > 3
394 60dd1473 Iustin Pop
    # all OP_ID start with OP_, we remove that
395 60dd1473 Iustin Pop
    txt = self.OP_ID[3:]
396 60dd1473 Iustin Pop
    field_name = getattr(self, "OP_DSC_FIELD", None)
397 60dd1473 Iustin Pop
    if field_name:
398 60dd1473 Iustin Pop
      field_value = getattr(self, field_name, None)
399 bc8bbda1 Iustin Pop
      if isinstance(field_value, (list, tuple)):
400 bc8bbda1 Iustin Pop
        field_value = ",".join(str(i) for i in field_value)
401 60dd1473 Iustin Pop
      txt = "%s(%s)" % (txt, field_value)
402 60dd1473 Iustin Pop
    return txt
403 60dd1473 Iustin Pop
404 a8083063 Iustin Pop
405 afee0879 Iustin Pop
# cluster opcodes
406 afee0879 Iustin Pop
407 bc84ffa7 Iustin Pop
class OpClusterPostInit(OpCode):
408 b5f5fae9 Luca Bigliardi
  """Post cluster initialization.
409 b5f5fae9 Luca Bigliardi

410 b5f5fae9 Luca Bigliardi
  This opcode does not touch the cluster at all. Its purpose is to run hooks
411 b5f5fae9 Luca Bigliardi
  after the cluster has been initialized.
412 b5f5fae9 Luca Bigliardi

413 b5f5fae9 Luca Bigliardi
  """
414 b5f5fae9 Luca Bigliardi
415 b5f5fae9 Luca Bigliardi
416 c6d43e9e Iustin Pop
class OpClusterDestroy(OpCode):
417 a7399f66 Iustin Pop
  """Destroy the cluster.
418 a7399f66 Iustin Pop

419 a7399f66 Iustin Pop
  This opcode has no other parameters. All the state is irreversibly
420 a7399f66 Iustin Pop
  lost after the execution of this opcode.
421 a7399f66 Iustin Pop

422 a7399f66 Iustin Pop
  """
423 a8083063 Iustin Pop
424 a8083063 Iustin Pop
425 a2f7ab92 Iustin Pop
class OpClusterQuery(OpCode):
426 fdc267f4 Iustin Pop
  """Query cluster information."""
427 a8083063 Iustin Pop
428 a8083063 Iustin Pop
429 a3d32770 Iustin Pop
class OpClusterVerify(OpCode):
430 a7399f66 Iustin Pop
  """Verify the cluster state.
431 a7399f66 Iustin Pop

432 a7399f66 Iustin Pop
  @type skip_checks: C{list}
433 a7399f66 Iustin Pop
  @ivar skip_checks: steps to be skipped from the verify process; this
434 a7399f66 Iustin Pop
                     needs to be a subset of
435 a7399f66 Iustin Pop
                     L{constants.VERIFY_OPTIONAL_CHECKS}; currently
436 a7399f66 Iustin Pop
                     only L{constants.VERIFY_NPLUSONE_MEM} can be passed
437 a7399f66 Iustin Pop

438 a7399f66 Iustin Pop
  """
439 65e183af Michael Hanselmann
  OP_PARAMS = [
440 65e183af Michael Hanselmann
    ("skip_checks", ht.EmptyList,
441 65e183af Michael Hanselmann
     ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS))),
442 65e183af Michael Hanselmann
    ("verbose", False, ht.TBool),
443 65e183af Michael Hanselmann
    ("error_codes", False, ht.TBool),
444 65e183af Michael Hanselmann
    ("debug_simulate_errors", False, ht.TBool),
445 65e183af Michael Hanselmann
    ]
446 a8083063 Iustin Pop
447 a8083063 Iustin Pop
448 bd8210a7 Iustin Pop
class OpClusterVerifyDisks(OpCode):
449 150e978f Iustin Pop
  """Verify the cluster disks.
450 150e978f Iustin Pop

451 150e978f Iustin Pop
  Parameters: none
452 150e978f Iustin Pop

453 5188ab37 Iustin Pop
  Result: a tuple of four elements:
454 150e978f Iustin Pop
    - list of node names with bad data returned (unreachable, etc.)
455 a7399f66 Iustin Pop
    - dict of node names with broken volume groups (values: error msg)
456 150e978f Iustin Pop
    - list of instances with degraded disks (that should be activated)
457 b63ed789 Iustin Pop
    - dict of instances with missing logical volumes (values: (node, vol)
458 b63ed789 Iustin Pop
      pairs with details about the missing volumes)
459 150e978f Iustin Pop

460 b63ed789 Iustin Pop
  In normal operation, all lists should be empty. A non-empty instance
461 b63ed789 Iustin Pop
  list (3rd element of the result) is still ok (errors were fixed) but
462 b63ed789 Iustin Pop
  non-empty node list means some node is down, and probably there are
463 b63ed789 Iustin Pop
  unfixable drbd errors.
464 150e978f Iustin Pop

465 150e978f Iustin Pop
  Note that only instances that are drbd-based are taken into
466 150e978f Iustin Pop
  consideration. This might need to be revisited in the future.
467 150e978f Iustin Pop

468 150e978f Iustin Pop
  """
469 150e978f Iustin Pop
470 150e978f Iustin Pop
471 5d01aca3 Iustin Pop
class OpClusterRepairDiskSizes(OpCode):
472 60975797 Iustin Pop
  """Verify the disk sizes of the instances and fixes configuration
473 60975797 Iustin Pop
  mimatches.
474 60975797 Iustin Pop

475 60975797 Iustin Pop
  Parameters: optional instances list, in case we want to restrict the
476 60975797 Iustin Pop
  checks to only a subset of the instances.
477 60975797 Iustin Pop

478 60975797 Iustin Pop
  Result: a list of tuples, (instance, disk, new-size) for changed
479 60975797 Iustin Pop
  configurations.
480 60975797 Iustin Pop

481 60975797 Iustin Pop
  In normal operation, the list should be empty.
482 60975797 Iustin Pop

483 60975797 Iustin Pop
  @type instances: list
484 60975797 Iustin Pop
  @ivar instances: the list of instances to check, or empty for all instances
485 60975797 Iustin Pop

486 60975797 Iustin Pop
  """
487 65e183af Michael Hanselmann
  OP_PARAMS = [
488 65e183af Michael Hanselmann
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
489 65e183af Michael Hanselmann
    ]
490 60975797 Iustin Pop
491 60975797 Iustin Pop
492 2f093ea0 Iustin Pop
class OpClusterConfigQuery(OpCode):
493 ae5849b5 Michael Hanselmann
  """Query cluster configuration values."""
494 65e183af Michael Hanselmann
  OP_PARAMS = [
495 65e183af Michael Hanselmann
    _POutputFields
496 65e183af Michael Hanselmann
    ]
497 a8083063 Iustin Pop
498 a8083063 Iustin Pop
499 e126df25 Iustin Pop
class OpClusterRename(OpCode):
500 a7399f66 Iustin Pop
  """Rename the cluster.
501 a7399f66 Iustin Pop

502 a7399f66 Iustin Pop
  @type name: C{str}
503 a7399f66 Iustin Pop
  @ivar name: The new name of the cluster. The name and/or the master IP
504 a7399f66 Iustin Pop
              address will be changed to match the new name and its IP
505 a7399f66 Iustin Pop
              address.
506 a7399f66 Iustin Pop

507 a7399f66 Iustin Pop
  """
508 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
509 65e183af Michael Hanselmann
  OP_PARAMS = [
510 65e183af Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString),
511 65e183af Michael Hanselmann
    ]
512 07bd8a51 Iustin Pop
513 07bd8a51 Iustin Pop
514 a6682fdc Iustin Pop
class OpClusterSetParams(OpCode):
515 a7399f66 Iustin Pop
  """Change the parameters of the cluster.
516 a7399f66 Iustin Pop

517 a7399f66 Iustin Pop
  @type vg_name: C{str} or C{None}
518 a7399f66 Iustin Pop
  @ivar vg_name: The new volume group name or None to disable LVM usage.
519 a7399f66 Iustin Pop

520 a7399f66 Iustin Pop
  """
521 65e183af Michael Hanselmann
  OP_PARAMS = [
522 65e183af Michael Hanselmann
    ("vg_name", None, ht.TMaybeString),
523 65e183af Michael Hanselmann
    ("enabled_hypervisors", None,
524 65e183af Michael Hanselmann
     ht.TOr(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)), ht.TTrue),
525 65e183af Michael Hanselmann
            ht.TNone)),
526 65e183af Michael Hanselmann
    ("hvparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
527 65e183af Michael Hanselmann
                              ht.TNone)),
528 65e183af Michael Hanselmann
    ("beparams", None, ht.TOr(ht.TDict, ht.TNone)),
529 65e183af Michael Hanselmann
    ("os_hvp", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
530 65e183af Michael Hanselmann
                            ht.TNone)),
531 65e183af Michael Hanselmann
    ("osparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
532 65e183af Michael Hanselmann
                              ht.TNone)),
533 65e183af Michael Hanselmann
    ("candidate_pool_size", None, ht.TOr(ht.TStrictPositiveInt, ht.TNone)),
534 65e183af Michael Hanselmann
    ("uid_pool", None, ht.NoType),
535 65e183af Michael Hanselmann
    ("add_uids", None, ht.NoType),
536 65e183af Michael Hanselmann
    ("remove_uids", None, ht.NoType),
537 65e183af Michael Hanselmann
    ("maintain_node_health", None, ht.TMaybeBool),
538 65e183af Michael Hanselmann
    ("prealloc_wipe_disks", None, ht.TMaybeBool),
539 5f074973 Michael Hanselmann
    ("nicparams", None, ht.TMaybeDict),
540 5f074973 Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict),
541 65e183af Michael Hanselmann
    ("drbd_helper", None, ht.TOr(ht.TString, ht.TNone)),
542 65e183af Michael Hanselmann
    ("default_iallocator", None, ht.TOr(ht.TString, ht.TNone)),
543 65e183af Michael Hanselmann
    ("master_netdev", None, ht.TOr(ht.TString, ht.TNone)),
544 65e183af Michael Hanselmann
    ("reserved_lvs", None, ht.TOr(ht.TListOf(ht.TNonEmptyString), ht.TNone)),
545 65e183af Michael Hanselmann
    ("hidden_os", None, ht.TOr(ht.TListOf(
546 65e183af Michael Hanselmann
          ht.TAnd(ht.TList,
547 65e183af Michael Hanselmann
                ht.TIsLength(2),
548 65e183af Michael Hanselmann
                ht.TMap(lambda v: v[0], ht.TElemOf(constants.DDMS_VALUES)))),
549 65e183af Michael Hanselmann
          ht.TNone)),
550 65e183af Michael Hanselmann
    ("blacklisted_os", None, ht.TOr(ht.TListOf(
551 65e183af Michael Hanselmann
          ht.TAnd(ht.TList,
552 65e183af Michael Hanselmann
                ht.TIsLength(2),
553 65e183af Michael Hanselmann
                ht.TMap(lambda v: v[0], ht.TElemOf(constants.DDMS_VALUES)))),
554 65e183af Michael Hanselmann
          ht.TNone)),
555 4b7735f9 Iustin Pop
    ]
556 12515db7 Manuel Franceschini
557 12515db7 Manuel Franceschini
558 d1240007 Iustin Pop
class OpClusterRedistConf(OpCode):
559 afee0879 Iustin Pop
  """Force a full push of the cluster configuration.
560 afee0879 Iustin Pop

561 afee0879 Iustin Pop
  """
562 afee0879 Iustin Pop
563 83f72637 Michael Hanselmann
564 83f72637 Michael Hanselmann
class OpQuery(OpCode):
565 83f72637 Michael Hanselmann
  """Query for resources/items.
566 83f72637 Michael Hanselmann

567 83f72637 Michael Hanselmann
  @ivar what: Resources to query for, must be one of L{constants.QR_OP_QUERY}
568 83f72637 Michael Hanselmann
  @ivar fields: List of fields to retrieve
569 83f72637 Michael Hanselmann
  @ivar filter: Query filter
570 83f72637 Michael Hanselmann

571 83f72637 Michael Hanselmann
  """
572 65e183af Michael Hanselmann
  OP_PARAMS = [
573 65e183af Michael Hanselmann
    ("what", ht.NoDefault, ht.TElemOf(constants.QR_OP_QUERY)),
574 65e183af Michael Hanselmann
    ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString)),
575 65e183af Michael Hanselmann
    ("filter", None, ht.TOr(ht.TNone,
576 65e183af Michael Hanselmann
                            ht.TListOf(ht.TOr(ht.TNonEmptyString, ht.TList)))),
577 83f72637 Michael Hanselmann
    ]
578 83f72637 Michael Hanselmann
579 83f72637 Michael Hanselmann
580 83f72637 Michael Hanselmann
class OpQueryFields(OpCode):
581 83f72637 Michael Hanselmann
  """Query for available resource/item fields.
582 83f72637 Michael Hanselmann

583 83f72637 Michael Hanselmann
  @ivar what: Resources to query for, must be one of L{constants.QR_OP_QUERY}
584 83f72637 Michael Hanselmann
  @ivar fields: List of fields to retrieve
585 83f72637 Michael Hanselmann

586 83f72637 Michael Hanselmann
  """
587 65e183af Michael Hanselmann
  OP_PARAMS = [
588 65e183af Michael Hanselmann
    ("what", ht.NoDefault, ht.TElemOf(constants.QR_OP_QUERY)),
589 65e183af Michael Hanselmann
    ("fields", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString))),
590 83f72637 Michael Hanselmann
    ]
591 83f72637 Michael Hanselmann
592 83f72637 Michael Hanselmann
593 792af3ad Renรฉ Nussbaumer
class OpOobCommand(OpCode):
594 eb64da59 Renรฉ Nussbaumer
  """Interact with OOB."""
595 65e183af Michael Hanselmann
  OP_PARAMS = [
596 b04808ea Renรฉ Nussbaumer
    ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
597 65e183af Michael Hanselmann
    ("command", None, ht.TElemOf(constants.OOB_COMMANDS)),
598 65e183af Michael Hanselmann
    ("timeout", constants.OOB_TIMEOUT, ht.TInt),
599 eb64da59 Renรฉ Nussbaumer
    ]
600 eb64da59 Renรฉ Nussbaumer
601 eb64da59 Renรฉ Nussbaumer
602 07bd8a51 Iustin Pop
# node opcodes
603 07bd8a51 Iustin Pop
604 73d565a3 Iustin Pop
class OpNodeRemove(OpCode):
605 a7399f66 Iustin Pop
  """Remove a node.
606 a7399f66 Iustin Pop

607 a7399f66 Iustin Pop
  @type node_name: C{str}
608 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to remove. If the node still has
609 a7399f66 Iustin Pop
                   instances on it, the operation will fail.
610 a7399f66 Iustin Pop

611 a7399f66 Iustin Pop
  """
612 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
613 65e183af Michael Hanselmann
  OP_PARAMS = [
614 65e183af Michael Hanselmann
    _PNodeName,
615 65e183af Michael Hanselmann
    ]
616 a8083063 Iustin Pop
617 a8083063 Iustin Pop
618 d817d49f Iustin Pop
class OpNodeAdd(OpCode):
619 a7399f66 Iustin Pop
  """Add a node to the cluster.
620 a7399f66 Iustin Pop

621 a7399f66 Iustin Pop
  @type node_name: C{str}
622 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to add. This can be a short name,
623 a7399f66 Iustin Pop
                   but it will be expanded to the FQDN.
624 a7399f66 Iustin Pop
  @type primary_ip: IP address
625 a7399f66 Iustin Pop
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
626 a7399f66 Iustin Pop
                    opcode is submitted, but will be filled during the node
627 a7399f66 Iustin Pop
                    add (so it will be visible in the job query).
628 a7399f66 Iustin Pop
  @type secondary_ip: IP address
629 a7399f66 Iustin Pop
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
630 a7399f66 Iustin Pop
                      if the cluster has been initialized in 'dual-network'
631 a7399f66 Iustin Pop
                      mode, otherwise it must not be given.
632 a7399f66 Iustin Pop
  @type readd: C{bool}
633 a7399f66 Iustin Pop
  @ivar readd: Whether to re-add an existing node to the cluster. If
634 a7399f66 Iustin Pop
               this is not passed, then the operation will abort if the node
635 a7399f66 Iustin Pop
               name is already in the cluster; use this parameter to 'repair'
636 a7399f66 Iustin Pop
               a node that had its configuration broken, or was reinstalled
637 a7399f66 Iustin Pop
               without removal from the cluster.
638 f936c153 Iustin Pop
  @type group: C{str}
639 f936c153 Iustin Pop
  @ivar group: The node group to which this node will belong.
640 fd3d37b6 Iustin Pop
  @type vm_capable: C{bool}
641 fd3d37b6 Iustin Pop
  @ivar vm_capable: The vm_capable node attribute
642 fd3d37b6 Iustin Pop
  @type master_capable: C{bool}
643 fd3d37b6 Iustin Pop
  @ivar master_capable: The master_capable node attribute
644 a7399f66 Iustin Pop

645 a7399f66 Iustin Pop
  """
646 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
647 65e183af Michael Hanselmann
  OP_PARAMS = [
648 65e183af Michael Hanselmann
    _PNodeName,
649 65e183af Michael Hanselmann
    ("primary_ip", None, ht.NoType),
650 65e183af Michael Hanselmann
    ("secondary_ip", None, ht.TMaybeString),
651 65e183af Michael Hanselmann
    ("readd", False, ht.TBool),
652 65e183af Michael Hanselmann
    ("group", None, ht.TMaybeString),
653 65e183af Michael Hanselmann
    ("master_capable", None, ht.TMaybeBool),
654 65e183af Michael Hanselmann
    ("vm_capable", None, ht.TMaybeBool),
655 5f074973 Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict),
656 65e183af Michael Hanselmann
    ]
657 a8083063 Iustin Pop
658 a8083063 Iustin Pop
659 2237687b Iustin Pop
class OpNodeQuery(OpCode):
660 a8083063 Iustin Pop
  """Compute the list of nodes."""
661 65e183af Michael Hanselmann
  OP_PARAMS = [
662 65e183af Michael Hanselmann
    _POutputFields,
663 65e183af Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
664 65e183af Michael Hanselmann
    ("use_locking", False, ht.TBool),
665 65e183af Michael Hanselmann
    ]
666 a8083063 Iustin Pop
667 a8083063 Iustin Pop
668 8ed55bfd Iustin Pop
class OpNodeQueryvols(OpCode):
669 dcb93971 Michael Hanselmann
  """Get list of volumes on node."""
670 65e183af Michael Hanselmann
  OP_PARAMS = [
671 65e183af Michael Hanselmann
    _POutputFields,
672 65e183af Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
673 65e183af Michael Hanselmann
    ]
674 dcb93971 Michael Hanselmann
675 dcb93971 Michael Hanselmann
676 ad8d0595 Iustin Pop
class OpNodeQueryStorage(OpCode):
677 9e5442ce Michael Hanselmann
  """Get information on storage for node(s)."""
678 65e183af Michael Hanselmann
  OP_PARAMS = [
679 65e183af Michael Hanselmann
    _POutputFields,
680 65e183af Michael Hanselmann
    _PStorageType,
681 65e183af Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
682 65e183af Michael Hanselmann
    ("name", None, ht.TMaybeString),
683 9e5442ce Michael Hanselmann
    ]
684 9e5442ce Michael Hanselmann
685 9e5442ce Michael Hanselmann
686 2cee4077 Iustin Pop
class OpNodeModifyStorage(OpCode):
687 099c52ad Iustin Pop
  """Modifies the properies of a storage unit"""
688 65e183af Michael Hanselmann
  OP_PARAMS = [
689 65e183af Michael Hanselmann
    _PNodeName,
690 65e183af Michael Hanselmann
    _PStorageType,
691 65e183af Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString),
692 65e183af Michael Hanselmann
    ("changes", ht.NoDefault, ht.TDict),
693 efb8da02 Michael Hanselmann
    ]
694 efb8da02 Michael Hanselmann
695 efb8da02 Michael Hanselmann
696 76aef8fc Michael Hanselmann
class OpRepairNodeStorage(OpCode):
697 76aef8fc Michael Hanselmann
  """Repairs the volume group on a node."""
698 76aef8fc Michael Hanselmann
  OP_DSC_FIELD = "node_name"
699 65e183af Michael Hanselmann
  OP_PARAMS = [
700 65e183af Michael Hanselmann
    _PNodeName,
701 65e183af Michael Hanselmann
    _PStorageType,
702 4b9e8c93 Iustin Pop
    _PIgnoreConsistency,
703 65e183af Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString),
704 76aef8fc Michael Hanselmann
    ]
705 76aef8fc Michael Hanselmann
706 76aef8fc Michael Hanselmann
707 f13973c4 Iustin Pop
class OpNodeSetParams(OpCode):
708 b31c8676 Iustin Pop
  """Change the parameters of a node."""
709 b31c8676 Iustin Pop
  OP_DSC_FIELD = "node_name"
710 65e183af Michael Hanselmann
  OP_PARAMS = [
711 65e183af Michael Hanselmann
    _PNodeName,
712 65e183af Michael Hanselmann
    _PForce,
713 65e183af Michael Hanselmann
    ("master_candidate", None, ht.TMaybeBool),
714 65e183af Michael Hanselmann
    ("offline", None, ht.TMaybeBool),
715 65e183af Michael Hanselmann
    ("drained", None, ht.TMaybeBool),
716 65e183af Michael Hanselmann
    ("auto_promote", False, ht.TBool),
717 65e183af Michael Hanselmann
    ("master_capable", None, ht.TMaybeBool),
718 65e183af Michael Hanselmann
    ("vm_capable", None, ht.TMaybeBool),
719 65e183af Michael Hanselmann
    ("secondary_ip", None, ht.TMaybeString),
720 5f074973 Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict),
721 65e183af Michael Hanselmann
    ("powered", None, ht.TMaybeBool),
722 b31c8676 Iustin Pop
    ]
723 b31c8676 Iustin Pop
724 f5118ade Iustin Pop
725 e0d4735f Iustin Pop
class OpNodePowercycle(OpCode):
726 f5118ade Iustin Pop
  """Tries to powercycle a node."""
727 f5118ade Iustin Pop
  OP_DSC_FIELD = "node_name"
728 65e183af Michael Hanselmann
  OP_PARAMS = [
729 65e183af Michael Hanselmann
    _PNodeName,
730 65e183af Michael Hanselmann
    _PForce,
731 f5118ade Iustin Pop
    ]
732 f5118ade Iustin Pop
733 7ffc5a86 Michael Hanselmann
734 5b14a488 Iustin Pop
class OpNodeMigrate(OpCode):
735 80cb875c Michael Hanselmann
  """Migrate all instances from a node."""
736 80cb875c Michael Hanselmann
  OP_DSC_FIELD = "node_name"
737 65e183af Michael Hanselmann
  OP_PARAMS = [
738 65e183af Michael Hanselmann
    _PNodeName,
739 65e183af Michael Hanselmann
    _PMigrationMode,
740 65e183af Michael Hanselmann
    _PMigrationLive,
741 80cb875c Michael Hanselmann
    ]
742 80cb875c Michael Hanselmann
743 80cb875c Michael Hanselmann
744 0ae89533 Iustin Pop
class OpNodeEvacStrategy(OpCode):
745 d6aaa598 Iustin Pop
  """Compute the evacuation strategy for a list of nodes."""
746 d6aaa598 Iustin Pop
  OP_DSC_FIELD = "nodes"
747 65e183af Michael Hanselmann
  OP_PARAMS = [
748 65e183af Michael Hanselmann
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString)),
749 65e183af Michael Hanselmann
    ("remote_node", None, ht.TMaybeString),
750 65e183af Michael Hanselmann
    ("iallocator", None, ht.TMaybeString),
751 65e183af Michael Hanselmann
    ]
752 d6aaa598 Iustin Pop
753 d6aaa598 Iustin Pop
754 a8083063 Iustin Pop
# instance opcodes
755 a8083063 Iustin Pop
756 e1530b10 Iustin Pop
class OpInstanceCreate(OpCode):
757 9bf56d77 Michael Hanselmann
  """Create an instance.
758 9bf56d77 Michael Hanselmann

759 9bf56d77 Michael Hanselmann
  @ivar instance_name: Instance name
760 9bf56d77 Michael Hanselmann
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
761 9bf56d77 Michael Hanselmann
  @ivar source_handshake: Signed handshake from source (remote import only)
762 9bf56d77 Michael Hanselmann
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
763 9bf56d77 Michael Hanselmann
  @ivar source_instance_name: Previous name of instance (remote import only)
764 dae91d02 Michael Hanselmann
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
765 dae91d02 Michael Hanselmann
    (remote import only)
766 9bf56d77 Michael Hanselmann

767 9bf56d77 Michael Hanselmann
  """
768 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
769 65e183af Michael Hanselmann
  OP_PARAMS = [
770 65e183af Michael Hanselmann
    _PInstanceName,
771 65e183af Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict),
772 65e183af Michael Hanselmann
    ("disks", ht.NoDefault, ht.TListOf(ht.TDict)),
773 65e183af Michael Hanselmann
    ("disk_template", ht.NoDefault, _CheckDiskTemplate),
774 65e183af Michael Hanselmann
    ("file_driver", None, ht.TOr(ht.TNone, ht.TElemOf(constants.FILE_DRIVER))),
775 65e183af Michael Hanselmann
    ("file_storage_dir", None, ht.TMaybeString),
776 65e183af Michael Hanselmann
    ("force_variant", False, ht.TBool),
777 65e183af Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict),
778 65e183af Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString),
779 65e183af Michael Hanselmann
    ("iallocator", None, ht.TMaybeString),
780 65e183af Michael Hanselmann
    ("identify_defaults", False, ht.TBool),
781 65e183af Michael Hanselmann
    ("ip_check", True, ht.TBool),
782 65e183af Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.INSTANCE_CREATE_MODES)),
783 65e183af Michael Hanselmann
    ("name_check", True, ht.TBool),
784 65e183af Michael Hanselmann
    ("nics", ht.NoDefault, ht.TListOf(ht.TDict)),
785 65e183af Michael Hanselmann
    ("no_install", None, ht.TMaybeBool),
786 65e183af Michael Hanselmann
    ("osparams", ht.EmptyDict, ht.TDict),
787 65e183af Michael Hanselmann
    ("os_type", None, ht.TMaybeString),
788 65e183af Michael Hanselmann
    ("pnode", None, ht.TMaybeString),
789 65e183af Michael Hanselmann
    ("snode", None, ht.TMaybeString),
790 65e183af Michael Hanselmann
    ("source_handshake", None, ht.TOr(ht.TList, ht.TNone)),
791 65e183af Michael Hanselmann
    ("source_instance_name", None, ht.TMaybeString),
792 65e183af Michael Hanselmann
    ("source_shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
793 65e183af Michael Hanselmann
     ht.TPositiveInt),
794 65e183af Michael Hanselmann
    ("source_x509_ca", None, ht.TMaybeString),
795 65e183af Michael Hanselmann
    ("src_node", None, ht.TMaybeString),
796 65e183af Michael Hanselmann
    ("src_path", None, ht.TMaybeString),
797 65e183af Michael Hanselmann
    ("start", True, ht.TBool),
798 65e183af Michael Hanselmann
    ("wait_for_sync", True, ht.TBool),
799 3b6d8c9b Iustin Pop
    ]
800 a8083063 Iustin Pop
801 a8083063 Iustin Pop
802 5073fd8f Iustin Pop
class OpInstanceReinstall(OpCode):
803 fdc267f4 Iustin Pop
  """Reinstall an instance's OS."""
804 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
805 65e183af Michael Hanselmann
  OP_PARAMS = [
806 65e183af Michael Hanselmann
    _PInstanceName,
807 65e183af Michael Hanselmann
    ("os_type", None, ht.TMaybeString),
808 65e183af Michael Hanselmann
    ("force_variant", False, ht.TBool),
809 5f074973 Michael Hanselmann
    ("osparams", None, ht.TMaybeDict),
810 65e183af Michael Hanselmann
    ]
811 fe7b0351 Michael Hanselmann
812 fe7b0351 Michael Hanselmann
813 3cd2d4b1 Iustin Pop
class OpInstanceRemove(OpCode):
814 a8083063 Iustin Pop
  """Remove an instance."""
815 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
816 65e183af Michael Hanselmann
  OP_PARAMS = [
817 65e183af Michael Hanselmann
    _PInstanceName,
818 65e183af Michael Hanselmann
    _PShutdownTimeout,
819 65e183af Michael Hanselmann
    ("ignore_failures", False, ht.TBool),
820 fc1baca9 Michael Hanselmann
    ]
821 a8083063 Iustin Pop
822 a8083063 Iustin Pop
823 5659e2e2 Iustin Pop
class OpInstanceRename(OpCode):
824 decd5f45 Iustin Pop
  """Rename an instance."""
825 65e183af Michael Hanselmann
  OP_PARAMS = [
826 65e183af Michael Hanselmann
    _PInstanceName,
827 65e183af Michael Hanselmann
    ("new_name", ht.NoDefault, ht.TNonEmptyString),
828 65e183af Michael Hanselmann
    ("ip_check", False, ht.TBool),
829 65e183af Michael Hanselmann
    ("name_check", True, ht.TBool),
830 4f05fd3b Iustin Pop
    ]
831 decd5f45 Iustin Pop
832 decd5f45 Iustin Pop
833 c873d91c Iustin Pop
class OpInstanceStartup(OpCode):
834 fdc267f4 Iustin Pop
  """Startup an instance."""
835 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
836 65e183af Michael Hanselmann
  OP_PARAMS = [
837 65e183af Michael Hanselmann
    _PInstanceName,
838 65e183af Michael Hanselmann
    _PForce,
839 65e183af Michael Hanselmann
    _PIgnoreOfflineNodes,
840 65e183af Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict),
841 65e183af Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict),
842 4f05fd3b Iustin Pop
    ]
843 a8083063 Iustin Pop
844 a8083063 Iustin Pop
845 ee3e37a7 Iustin Pop
class OpInstanceShutdown(OpCode):
846 fdc267f4 Iustin Pop
  """Shutdown an instance."""
847 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
848 65e183af Michael Hanselmann
  OP_PARAMS = [
849 65e183af Michael Hanselmann
    _PInstanceName,
850 65e183af Michael Hanselmann
    _PIgnoreOfflineNodes,
851 65e183af Michael Hanselmann
    ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TPositiveInt),
852 b44bd844 Michael Hanselmann
    ]
853 a8083063 Iustin Pop
854 a8083063 Iustin Pop
855 90ab1a95 Iustin Pop
class OpInstanceReboot(OpCode):
856 bf6929a2 Alexander Schreiber
  """Reboot an instance."""
857 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
858 65e183af Michael Hanselmann
  OP_PARAMS = [
859 65e183af Michael Hanselmann
    _PInstanceName,
860 65e183af Michael Hanselmann
    _PShutdownTimeout,
861 65e183af Michael Hanselmann
    ("ignore_secondaries", False, ht.TBool),
862 65e183af Michael Hanselmann
    ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES)),
863 4f05fd3b Iustin Pop
    ]
864 bf6929a2 Alexander Schreiber
865 bf6929a2 Alexander Schreiber
866 668f755d Iustin Pop
class OpInstanceReplaceDisks(OpCode):
867 fdc267f4 Iustin Pop
  """Replace the disks of an instance."""
868 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
869 65e183af Michael Hanselmann
  OP_PARAMS = [
870 65e183af Michael Hanselmann
    _PInstanceName,
871 65e183af Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES)),
872 65e183af Michael Hanselmann
    ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt)),
873 65e183af Michael Hanselmann
    ("remote_node", None, ht.TMaybeString),
874 65e183af Michael Hanselmann
    ("iallocator", None, ht.TMaybeString),
875 65e183af Michael Hanselmann
    ("early_release", False, ht.TBool),
876 4f05fd3b Iustin Pop
    ]
877 a8083063 Iustin Pop
878 a8083063 Iustin Pop
879 019dbee1 Iustin Pop
class OpInstanceFailover(OpCode):
880 a8083063 Iustin Pop
  """Failover an instance."""
881 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
882 65e183af Michael Hanselmann
  OP_PARAMS = [
883 65e183af Michael Hanselmann
    _PInstanceName,
884 65e183af Michael Hanselmann
    _PShutdownTimeout,
885 4b9e8c93 Iustin Pop
    _PIgnoreConsistency,
886 17c3f802 Guido Trotter
    ]
887 a8083063 Iustin Pop
888 a8083063 Iustin Pop
889 75c866c2 Iustin Pop
class OpInstanceMigrate(OpCode):
890 53c776b5 Iustin Pop
  """Migrate an instance.
891 53c776b5 Iustin Pop

892 53c776b5 Iustin Pop
  This migrates (without shutting down an instance) to its secondary
893 53c776b5 Iustin Pop
  node.
894 53c776b5 Iustin Pop

895 2f907a8c Iustin Pop
  @ivar instance_name: the name of the instance
896 8c35561f Iustin Pop
  @ivar mode: the migration mode (live, non-live or None for auto)
897 53c776b5 Iustin Pop

898 53c776b5 Iustin Pop
  """
899 ee69c97f Iustin Pop
  OP_DSC_FIELD = "instance_name"
900 65e183af Michael Hanselmann
  OP_PARAMS = [
901 65e183af Michael Hanselmann
    _PInstanceName,
902 65e183af Michael Hanselmann
    _PMigrationMode,
903 65e183af Michael Hanselmann
    _PMigrationLive,
904 65e183af Michael Hanselmann
    ("cleanup", False, ht.TBool),
905 65e183af Michael Hanselmann
    ]
906 53c776b5 Iustin Pop
907 53c776b5 Iustin Pop
908 0091b480 Iustin Pop
class OpInstanceMove(OpCode):
909 313bcead Iustin Pop
  """Move an instance.
910 313bcead Iustin Pop

911 313bcead Iustin Pop
  This move (with shutting down an instance and data copying) to an
912 313bcead Iustin Pop
  arbitrary node.
913 313bcead Iustin Pop

914 313bcead Iustin Pop
  @ivar instance_name: the name of the instance
915 313bcead Iustin Pop
  @ivar target_node: the destination node
916 313bcead Iustin Pop

917 313bcead Iustin Pop
  """
918 313bcead Iustin Pop
  OP_DSC_FIELD = "instance_name"
919 65e183af Michael Hanselmann
  OP_PARAMS = [
920 65e183af Michael Hanselmann
    _PInstanceName,
921 65e183af Michael Hanselmann
    _PShutdownTimeout,
922 65e183af Michael Hanselmann
    ("target_node", ht.NoDefault, ht.TNonEmptyString),
923 154b9580 Balazs Lecz
    ]
924 313bcead Iustin Pop
925 313bcead Iustin Pop
926 cc0dec7b Iustin Pop
class OpInstanceConsole(OpCode):
927 fdc267f4 Iustin Pop
  """Connect to an instance's console."""
928 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
929 65e183af Michael Hanselmann
  OP_PARAMS = [
930 65e183af Michael Hanselmann
    _PInstanceName
931 65e183af Michael Hanselmann
    ]
932 a8083063 Iustin Pop
933 a8083063 Iustin Pop
934 83f5d475 Iustin Pop
class OpInstanceActivateDisks(OpCode):
935 fdc267f4 Iustin Pop
  """Activate an instance's disks."""
936 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
937 65e183af Michael Hanselmann
  OP_PARAMS = [
938 65e183af Michael Hanselmann
    _PInstanceName,
939 65e183af Michael Hanselmann
    ("ignore_size", False, ht.TBool),
940 65e183af Michael Hanselmann
    ]
941 a8083063 Iustin Pop
942 a8083063 Iustin Pop
943 e176281f Iustin Pop
class OpInstanceDeactivateDisks(OpCode):
944 fdc267f4 Iustin Pop
  """Deactivate an instance's disks."""
945 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
946 65e183af Michael Hanselmann
  OP_PARAMS = [
947 c9c41373 Iustin Pop
    _PInstanceName,
948 c9c41373 Iustin Pop
    _PForce,
949 65e183af Michael Hanselmann
    ]
950 a8083063 Iustin Pop
951 a8083063 Iustin Pop
952 6b273e78 Iustin Pop
class OpInstanceRecreateDisks(OpCode):
953 bd315bfa Iustin Pop
  """Deactivate an instance's disks."""
954 bd315bfa Iustin Pop
  OP_DSC_FIELD = "instance_name"
955 65e183af Michael Hanselmann
  OP_PARAMS = [
956 65e183af Michael Hanselmann
    _PInstanceName,
957 65e183af Michael Hanselmann
    ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt)),
958 c8a96ae7 Iustin Pop
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
959 65e183af Michael Hanselmann
    ]
960 bd315bfa Iustin Pop
961 bd315bfa Iustin Pop
962 f2af0bec Iustin Pop
class OpInstanceQuery(OpCode):
963 a8083063 Iustin Pop
  """Compute the list of instances."""
964 65e183af Michael Hanselmann
  OP_PARAMS = [
965 65e183af Michael Hanselmann
    _POutputFields,
966 65e183af Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
967 65e183af Michael Hanselmann
    ("use_locking", False, ht.TBool),
968 65e183af Michael Hanselmann
    ]
969 a8083063 Iustin Pop
970 a8083063 Iustin Pop
971 dc28c4e4 Iustin Pop
class OpInstanceQueryData(OpCode):
972 a8083063 Iustin Pop
  """Compute the run-time status of instances."""
973 65e183af Michael Hanselmann
  OP_PARAMS = [
974 65e183af Michael Hanselmann
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
975 65e183af Michael Hanselmann
    ("static", False, ht.TBool),
976 dae661a4 Michael Hanselmann
    ("use_locking", False, ht.TBool),
977 65e183af Michael Hanselmann
    ]
978 a8083063 Iustin Pop
979 a8083063 Iustin Pop
980 9a3cc7ae Iustin Pop
class OpInstanceSetParams(OpCode):
981 a8083063 Iustin Pop
  """Change the parameters of an instance."""
982 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
983 65e183af Michael Hanselmann
  OP_PARAMS = [
984 65e183af Michael Hanselmann
    _PInstanceName,
985 65e183af Michael Hanselmann
    _PForce,
986 65e183af Michael Hanselmann
    ("nics", ht.EmptyList, ht.TList),
987 65e183af Michael Hanselmann
    ("disks", ht.EmptyList, ht.TList),
988 65e183af Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict),
989 65e183af Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict),
990 f7c8f153 Michael Hanselmann
    ("disk_template", None, ht.TOr(ht.TNone, _CheckDiskTemplate)),
991 65e183af Michael Hanselmann
    ("remote_node", None, ht.TMaybeString),
992 65e183af Michael Hanselmann
    ("os_name", None, ht.TMaybeString),
993 65e183af Michael Hanselmann
    ("force_variant", False, ht.TBool),
994 5f074973 Michael Hanselmann
    ("osparams", None, ht.TMaybeDict),
995 456798ab Iustin Pop
    ("wait_for_sync", True, ht.TBool),
996 973d7867 Iustin Pop
    ]
997 a8083063 Iustin Pop
998 a8083063 Iustin Pop
999 60472d29 Iustin Pop
class OpInstanceGrowDisk(OpCode):
1000 8729e0d7 Iustin Pop
  """Grow a disk of an instance."""
1001 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1002 65e183af Michael Hanselmann
  OP_PARAMS = [
1003 65e183af Michael Hanselmann
    _PInstanceName,
1004 65e183af Michael Hanselmann
    ("disk", ht.NoDefault, ht.TInt),
1005 65e183af Michael Hanselmann
    ("amount", ht.NoDefault, ht.TInt),
1006 65e183af Michael Hanselmann
    ("wait_for_sync", True, ht.TBool),
1007 4f05fd3b Iustin Pop
    ]
1008 8729e0d7 Iustin Pop
1009 8729e0d7 Iustin Pop
1010 70a6a926 Adeodato Simo
# Node group opcodes
1011 70a6a926 Adeodato Simo
1012 fabf1731 Iustin Pop
class OpGroupAdd(OpCode):
1013 b1ee5610 Adeodato Simo
  """Add a node group to the cluster."""
1014 b1ee5610 Adeodato Simo
  OP_DSC_FIELD = "group_name"
1015 65e183af Michael Hanselmann
  OP_PARAMS = [
1016 65e183af Michael Hanselmann
    _PGroupName,
1017 5f074973 Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict),
1018 65e183af Michael Hanselmann
    ("alloc_policy", None,
1019 65e183af Michael Hanselmann
     ht.TOr(ht.TNone, ht.TElemOf(constants.VALID_ALLOC_POLICIES))),
1020 483be60d Adeodato Simo
    ]
1021 b1ee5610 Adeodato Simo
1022 b1ee5610 Adeodato Simo
1023 934704ae Iustin Pop
class OpGroupAssignNodes(OpCode):
1024 96276ae7 Adeodato Simo
  """Assign nodes to a node group."""
1025 96276ae7 Adeodato Simo
  OP_DSC_FIELD = "group_name"
1026 96276ae7 Adeodato Simo
  OP_PARAMS = [
1027 96276ae7 Adeodato Simo
    _PGroupName,
1028 96276ae7 Adeodato Simo
    _PForce,
1029 96276ae7 Adeodato Simo
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString)),
1030 96276ae7 Adeodato Simo
    ]
1031 96276ae7 Adeodato Simo
1032 96276ae7 Adeodato Simo
1033 d4d654bd Iustin Pop
class OpGroupQuery(OpCode):
1034 70a6a926 Adeodato Simo
  """Compute the list of node groups."""
1035 65e183af Michael Hanselmann
  OP_PARAMS = [
1036 65e183af Michael Hanselmann
    _POutputFields,
1037 65e183af Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
1038 65e183af Michael Hanselmann
    ]
1039 70a6a926 Adeodato Simo
1040 70a6a926 Adeodato Simo
1041 7cbf74f0 Iustin Pop
class OpGroupSetParams(OpCode):
1042 4da7909a Adeodato Simo
  """Change the parameters of a node group."""
1043 4da7909a Adeodato Simo
  OP_DSC_FIELD = "group_name"
1044 65e183af Michael Hanselmann
  OP_PARAMS = [
1045 65e183af Michael Hanselmann
    _PGroupName,
1046 5f074973 Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict),
1047 65e183af Michael Hanselmann
    ("alloc_policy", None, ht.TOr(ht.TNone,
1048 65e183af Michael Hanselmann
                                  ht.TElemOf(constants.VALID_ALLOC_POLICIES))),
1049 4da7909a Adeodato Simo
    ]
1050 4da7909a Adeodato Simo
1051 4da7909a Adeodato Simo
1052 4d1baa51 Iustin Pop
class OpGroupRemove(OpCode):
1053 94bd652a Adeodato Simo
  """Remove a node group from the cluster."""
1054 94bd652a Adeodato Simo
  OP_DSC_FIELD = "group_name"
1055 65e183af Michael Hanselmann
  OP_PARAMS = [
1056 65e183af Michael Hanselmann
    _PGroupName,
1057 65e183af Michael Hanselmann
    ]
1058 94bd652a Adeodato Simo
1059 94bd652a Adeodato Simo
1060 a8173e82 Iustin Pop
class OpGroupRename(OpCode):
1061 4fe5cf90 Adeodato Simo
  """Rename a node group in the cluster."""
1062 4fe5cf90 Adeodato Simo
  OP_DSC_FIELD = "old_name"
1063 65e183af Michael Hanselmann
  OP_PARAMS = [
1064 65e183af Michael Hanselmann
    ("old_name", ht.NoDefault, ht.TNonEmptyString),
1065 65e183af Michael Hanselmann
    ("new_name", ht.NoDefault, ht.TNonEmptyString),
1066 65e183af Michael Hanselmann
    ]
1067 4fe5cf90 Adeodato Simo
1068 4fe5cf90 Adeodato Simo
1069 a8083063 Iustin Pop
# OS opcodes
1070 da2d02e7 Iustin Pop
class OpOsDiagnose(OpCode):
1071 a8083063 Iustin Pop
  """Compute the list of guest operating systems."""
1072 65e183af Michael Hanselmann
  OP_PARAMS = [
1073 65e183af Michael Hanselmann
    _POutputFields,
1074 65e183af Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
1075 65e183af Michael Hanselmann
    ]
1076 a8083063 Iustin Pop
1077 7c0d6283 Michael Hanselmann
1078 a8083063 Iustin Pop
# Exports opcodes
1079 7ca2d4d8 Iustin Pop
class OpBackupQuery(OpCode):
1080 a8083063 Iustin Pop
  """Compute the list of exported images."""
1081 65e183af Michael Hanselmann
  OP_PARAMS = [
1082 65e183af Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
1083 65e183af Michael Hanselmann
    ("use_locking", False, ht.TBool),
1084 65e183af Michael Hanselmann
    ]
1085 a8083063 Iustin Pop
1086 7c0d6283 Michael Hanselmann
1087 71910715 Iustin Pop
class OpBackupPrepare(OpCode):
1088 1410fa8d Michael Hanselmann
  """Prepares an instance export.
1089 1410fa8d Michael Hanselmann

1090 1410fa8d Michael Hanselmann
  @ivar instance_name: Instance name
1091 1410fa8d Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1092 1410fa8d Michael Hanselmann

1093 1410fa8d Michael Hanselmann
  """
1094 1410fa8d Michael Hanselmann
  OP_DSC_FIELD = "instance_name"
1095 65e183af Michael Hanselmann
  OP_PARAMS = [
1096 65e183af Michael Hanselmann
    _PInstanceName,
1097 65e183af Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES)),
1098 1410fa8d Michael Hanselmann
    ]
1099 1410fa8d Michael Hanselmann
1100 1410fa8d Michael Hanselmann
1101 4ff922a2 Iustin Pop
class OpBackupExport(OpCode):
1102 4a96f1d1 Michael Hanselmann
  """Export an instance.
1103 4a96f1d1 Michael Hanselmann

1104 4a96f1d1 Michael Hanselmann
  For local exports, the export destination is the node name. For remote
1105 4a96f1d1 Michael Hanselmann
  exports, the export destination is a list of tuples, each consisting of
1106 4a96f1d1 Michael Hanselmann
  hostname/IP address, port, HMAC and HMAC salt. The HMAC is calculated using
1107 4a96f1d1 Michael Hanselmann
  the cluster domain secret over the value "${index}:${hostname}:${port}". The
1108 4a96f1d1 Michael Hanselmann
  destination X509 CA must be a signed certificate.
1109 4a96f1d1 Michael Hanselmann

1110 4a96f1d1 Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1111 4a96f1d1 Michael Hanselmann
  @ivar target_node: Export destination
1112 4a96f1d1 Michael Hanselmann
  @ivar x509_key_name: X509 key to use (remote export only)
1113 4a96f1d1 Michael Hanselmann
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1114 4a96f1d1 Michael Hanselmann
                             only)
1115 4a96f1d1 Michael Hanselmann

1116 4a96f1d1 Michael Hanselmann
  """
1117 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1118 65e183af Michael Hanselmann
  OP_PARAMS = [
1119 65e183af Michael Hanselmann
    _PInstanceName,
1120 65e183af Michael Hanselmann
    _PShutdownTimeout,
1121 4a96f1d1 Michael Hanselmann
    # TODO: Rename target_node as it changes meaning for different export modes
1122 4a96f1d1 Michael Hanselmann
    # (e.g. "destination")
1123 65e183af Michael Hanselmann
    ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList)),
1124 65e183af Michael Hanselmann
    ("shutdown", True, ht.TBool),
1125 65e183af Michael Hanselmann
    ("remove_instance", False, ht.TBool),
1126 65e183af Michael Hanselmann
    ("ignore_remove_failures", False, ht.TBool),
1127 65e183af Michael Hanselmann
    ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES)),
1128 65e183af Michael Hanselmann
    ("x509_key_name", None, ht.TOr(ht.TList, ht.TNone)),
1129 65e183af Michael Hanselmann
    ("destination_x509_ca", None, ht.TMaybeString),
1130 17c3f802 Guido Trotter
    ]
1131 5c947f38 Iustin Pop
1132 0a7bed64 Michael Hanselmann
1133 ca5890ad Iustin Pop
class OpBackupRemove(OpCode):
1134 9ac99fda Guido Trotter
  """Remove an instance's export."""
1135 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1136 65e183af Michael Hanselmann
  OP_PARAMS = [
1137 65e183af Michael Hanselmann
    _PInstanceName,
1138 65e183af Michael Hanselmann
    ]
1139 5c947f38 Iustin Pop
1140 0a7bed64 Michael Hanselmann
1141 5c947f38 Iustin Pop
# Tags opcodes
1142 c6afb1ca Iustin Pop
class OpTagsGet(OpCode):
1143 5c947f38 Iustin Pop
  """Returns the tags of the given object."""
1144 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
1145 65e183af Michael Hanselmann
  OP_PARAMS = [
1146 65e183af Michael Hanselmann
    _PTagKind,
1147 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1148 65e183af Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString),
1149 65e183af Michael Hanselmann
    ]
1150 5c947f38 Iustin Pop
1151 5c947f38 Iustin Pop
1152 715462e7 Iustin Pop
class OpTagsSearch(OpCode):
1153 73415719 Iustin Pop
  """Searches the tags in the cluster for a given pattern."""
1154 60dd1473 Iustin Pop
  OP_DSC_FIELD = "pattern"
1155 65e183af Michael Hanselmann
  OP_PARAMS = [
1156 65e183af Michael Hanselmann
    ("pattern", ht.NoDefault, ht.TNonEmptyString),
1157 65e183af Michael Hanselmann
    ]
1158 73415719 Iustin Pop
1159 73415719 Iustin Pop
1160 d1602edc Iustin Pop
class OpTagsSet(OpCode):
1161 f27302fa Iustin Pop
  """Add a list of tags on a given object."""
1162 65e183af Michael Hanselmann
  OP_PARAMS = [
1163 65e183af Michael Hanselmann
    _PTagKind,
1164 65e183af Michael Hanselmann
    _PTags,
1165 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1166 65e183af Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString),
1167 65e183af Michael Hanselmann
    ]
1168 5c947f38 Iustin Pop
1169 5c947f38 Iustin Pop
1170 3f0ab95f Iustin Pop
class OpTagsDel(OpCode):
1171 f27302fa Iustin Pop
  """Remove a list of tags from a given object."""
1172 65e183af Michael Hanselmann
  OP_PARAMS = [
1173 65e183af Michael Hanselmann
    _PTagKind,
1174 65e183af Michael Hanselmann
    _PTags,
1175 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1176 65e183af Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString),
1177 65e183af Michael Hanselmann
    ]
1178 06009e27 Iustin Pop
1179 06009e27 Iustin Pop
# Test opcodes
1180 06009e27 Iustin Pop
class OpTestDelay(OpCode):
1181 06009e27 Iustin Pop
  """Sleeps for a configured amount of time.
1182 06009e27 Iustin Pop

1183 06009e27 Iustin Pop
  This is used just for debugging and testing.
1184 06009e27 Iustin Pop

1185 06009e27 Iustin Pop
  Parameters:
1186 06009e27 Iustin Pop
    - duration: the time to sleep
1187 06009e27 Iustin Pop
    - on_master: if true, sleep on the master
1188 06009e27 Iustin Pop
    - on_nodes: list of nodes in which to sleep
1189 06009e27 Iustin Pop

1190 06009e27 Iustin Pop
  If the on_master parameter is true, it will execute a sleep on the
1191 06009e27 Iustin Pop
  master (before any node sleep).
1192 06009e27 Iustin Pop

1193 06009e27 Iustin Pop
  If the on_nodes list is not empty, it will sleep on those nodes
1194 06009e27 Iustin Pop
  (after the sleep on the master, if that is enabled).
1195 06009e27 Iustin Pop

1196 06009e27 Iustin Pop
  As an additional feature, the case of duration < 0 will be reported
1197 06009e27 Iustin Pop
  as an execution error, so this opcode can be used as a failure
1198 06009e27 Iustin Pop
  generator. The case of duration == 0 will not be treated specially.
1199 06009e27 Iustin Pop

1200 06009e27 Iustin Pop
  """
1201 60dd1473 Iustin Pop
  OP_DSC_FIELD = "duration"
1202 65e183af Michael Hanselmann
  OP_PARAMS = [
1203 65e183af Michael Hanselmann
    ("duration", ht.NoDefault, ht.TFloat),
1204 65e183af Michael Hanselmann
    ("on_master", True, ht.TBool),
1205 65e183af Michael Hanselmann
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
1206 65e183af Michael Hanselmann
    ("repeat", 0, ht.TPositiveInt)
1207 65e183af Michael Hanselmann
    ]
1208 d61df03e Iustin Pop
1209 d61df03e Iustin Pop
1210 d61df03e Iustin Pop
class OpTestAllocator(OpCode):
1211 d61df03e Iustin Pop
  """Allocator framework testing.
1212 d61df03e Iustin Pop

1213 d61df03e Iustin Pop
  This opcode has two modes:
1214 d61df03e Iustin Pop
    - gather and return allocator input for a given mode (allocate new
1215 d61df03e Iustin Pop
      or replace secondary) and a given instance definition (direction
1216 d61df03e Iustin Pop
      'in')
1217 d61df03e Iustin Pop
    - run a selected allocator for a given operation (as above) and
1218 d61df03e Iustin Pop
      return the allocator output (direction 'out')
1219 d61df03e Iustin Pop

1220 d61df03e Iustin Pop
  """
1221 60dd1473 Iustin Pop
  OP_DSC_FIELD = "allocator"
1222 65e183af Michael Hanselmann
  OP_PARAMS = [
1223 65e183af Michael Hanselmann
    ("direction", ht.NoDefault,
1224 65e183af Michael Hanselmann
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS)),
1225 65e183af Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES)),
1226 65e183af Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString),
1227 65e183af Michael Hanselmann
    ("nics", ht.NoDefault, ht.TOr(ht.TNone, ht.TListOf(
1228 65e183af Michael Hanselmann
      ht.TDictOf(ht.TElemOf(["mac", "ip", "bridge"]),
1229 65e183af Michael Hanselmann
               ht.TOr(ht.TNone, ht.TNonEmptyString))))),
1230 65e183af Michael Hanselmann
    ("disks", ht.NoDefault, ht.TOr(ht.TNone, ht.TList)),
1231 65e183af Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString),
1232 65e183af Michael Hanselmann
    ("allocator", None, ht.TMaybeString),
1233 65e183af Michael Hanselmann
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString)),
1234 65e183af Michael Hanselmann
    ("mem_size", None, ht.TOr(ht.TNone, ht.TPositiveInt)),
1235 65e183af Michael Hanselmann
    ("vcpus", None, ht.TOr(ht.TNone, ht.TPositiveInt)),
1236 65e183af Michael Hanselmann
    ("os", None, ht.TMaybeString),
1237 65e183af Michael Hanselmann
    ("disk_template", None, ht.TMaybeString),
1238 65e183af Michael Hanselmann
    ("evac_nodes", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString))),
1239 d61df03e Iustin Pop
    ]
1240 363acb1e Iustin Pop
1241 76aef8fc Michael Hanselmann
1242 b469eb4d Iustin Pop
class OpTestJqueue(OpCode):
1243 e58f87a9 Michael Hanselmann
  """Utility opcode to test some aspects of the job queue.
1244 e58f87a9 Michael Hanselmann

1245 e58f87a9 Michael Hanselmann
  """
1246 65e183af Michael Hanselmann
  OP_PARAMS = [
1247 65e183af Michael Hanselmann
    ("notify_waitlock", False, ht.TBool),
1248 65e183af Michael Hanselmann
    ("notify_exec", False, ht.TBool),
1249 65e183af Michael Hanselmann
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString)),
1250 65e183af Michael Hanselmann
    ("fail", False, ht.TBool),
1251 e58f87a9 Michael Hanselmann
    ]
1252 e58f87a9 Michael Hanselmann
1253 e58f87a9 Michael Hanselmann
1254 be760ba8 Michael Hanselmann
class OpTestDummy(OpCode):
1255 be760ba8 Michael Hanselmann
  """Utility opcode used by unittests.
1256 be760ba8 Michael Hanselmann

1257 be760ba8 Michael Hanselmann
  """
1258 65e183af Michael Hanselmann
  OP_PARAMS = [
1259 65e183af Michael Hanselmann
    ("result", ht.NoDefault, ht.NoType),
1260 65e183af Michael Hanselmann
    ("messages", ht.NoDefault, ht.NoType),
1261 65e183af Michael Hanselmann
    ("fail", ht.NoDefault, ht.NoType),
1262 be760ba8 Michael Hanselmann
    ]
1263 687c10d9 Iustin Pop
  WITH_LU = False
1264 be760ba8 Michael Hanselmann
1265 be760ba8 Michael Hanselmann
1266 dbc96028 Michael Hanselmann
def _GetOpList():
1267 dbc96028 Michael Hanselmann
  """Returns list of all defined opcodes.
1268 dbc96028 Michael Hanselmann

1269 dbc96028 Michael Hanselmann
  Does not eliminate duplicates by C{OP_ID}.
1270 dbc96028 Michael Hanselmann

1271 dbc96028 Michael Hanselmann
  """
1272 dbc96028 Michael Hanselmann
  return [v for v in globals().values()
1273 dbc96028 Michael Hanselmann
          if (isinstance(v, type) and issubclass(v, OpCode) and
1274 687c10d9 Iustin Pop
              hasattr(v, "OP_ID") and v is not OpCode)]
1275 dbc96028 Michael Hanselmann
1276 dbc96028 Michael Hanselmann
1277 dbc96028 Michael Hanselmann
OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())