Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 3ce9a5e7

History | View | Annotate | Download (45.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 8c9ee749 Michael Hanselmann
import operator
39 1cbef6d8 Michael Hanselmann
40 65e183af Michael Hanselmann
from ganeti import constants
41 65e183af Michael Hanselmann
from ganeti import errors
42 65e183af Michael Hanselmann
from ganeti import ht
43 65e183af Michael Hanselmann
44 65e183af Michael Hanselmann
45 65e183af Michael Hanselmann
# Common opcode attributes
46 65e183af Michael Hanselmann
47 65e183af Michael Hanselmann
#: output fields for a query operation
48 197b323b Michael Hanselmann
_POutputFields = ("output_fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
49 45d4c81c Michael Hanselmann
                  "Selected output fields")
50 65e183af Michael Hanselmann
51 65e183af Michael Hanselmann
#: the shutdown timeout
52 45d4c81c Michael Hanselmann
_PShutdownTimeout = \
53 45d4c81c Michael Hanselmann
  ("shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TPositiveInt,
54 45d4c81c Michael Hanselmann
   "How long to wait for instance to shut down")
55 65e183af Michael Hanselmann
56 65e183af Michael Hanselmann
#: the force parameter
57 45d4c81c Michael Hanselmann
_PForce = ("force", False, ht.TBool, "Whether to force the operation")
58 65e183af Michael Hanselmann
59 65e183af Michael Hanselmann
#: a required instance name (for single-instance LUs)
60 45d4c81c Michael Hanselmann
_PInstanceName = ("instance_name", ht.NoDefault, ht.TNonEmptyString,
61 45d4c81c Michael Hanselmann
                  "Instance name")
62 65e183af Michael Hanselmann
63 65e183af Michael Hanselmann
#: Whether to ignore offline nodes
64 45d4c81c Michael Hanselmann
_PIgnoreOfflineNodes = ("ignore_offline_nodes", False, ht.TBool,
65 45d4c81c Michael Hanselmann
                        "Whether to ignore offline nodes")
66 65e183af Michael Hanselmann
67 65e183af Michael Hanselmann
#: a required node name (for single-node LUs)
68 45d4c81c Michael Hanselmann
_PNodeName = ("node_name", ht.NoDefault, ht.TNonEmptyString, "Node name")
69 65e183af Michael Hanselmann
70 65e183af Michael Hanselmann
#: a required node group name (for single-group LUs)
71 45d4c81c Michael Hanselmann
_PGroupName = ("group_name", ht.NoDefault, ht.TNonEmptyString, "Group name")
72 65e183af Michael Hanselmann
73 65e183af Michael Hanselmann
#: Migration type (live/non-live)
74 65e183af Michael Hanselmann
_PMigrationMode = ("mode", None,
75 197b323b Michael Hanselmann
                   ht.TOr(ht.TNone, ht.TElemOf(constants.HT_MIGRATION_MODES)),
76 45d4c81c Michael Hanselmann
                   "Migration mode")
77 65e183af Michael Hanselmann
78 65e183af Michael Hanselmann
#: Obsolete 'live' migration mode (boolean)
79 45d4c81c Michael Hanselmann
_PMigrationLive = ("live", None, ht.TMaybeBool,
80 45d4c81c Michael Hanselmann
                   "Legacy setting for live migration, do not use")
81 65e183af Michael Hanselmann
82 65e183af Michael Hanselmann
#: Tag type
83 197b323b Michael Hanselmann
_PTagKind = ("kind", ht.NoDefault, ht.TElemOf(constants.VALID_TAG_TYPES), None)
84 65e183af Michael Hanselmann
85 65e183af Michael Hanselmann
#: List of tag strings
86 197b323b Michael Hanselmann
_PTags = ("tags", ht.NoDefault, ht.TListOf(ht.TNonEmptyString), None)
87 65e183af Michael Hanselmann
88 45d4c81c Michael Hanselmann
_PForceVariant = ("force_variant", False, ht.TBool,
89 45d4c81c Michael Hanselmann
                  "Whether to force an unknown OS variant")
90 45d4c81c Michael Hanselmann
91 45d4c81c Michael Hanselmann
_PWaitForSync = ("wait_for_sync", True, ht.TBool,
92 45d4c81c Michael Hanselmann
                 "Whether to wait for the disk to synchronize")
93 45d4c81c Michael Hanselmann
94 45d4c81c Michael Hanselmann
_PIgnoreConsistency = ("ignore_consistency", False, ht.TBool,
95 45d4c81c Michael Hanselmann
                       "Whether to ignore disk consistency")
96 45d4c81c Michael Hanselmann
97 45d4c81c Michael Hanselmann
_PStorageName = ("name", ht.NoDefault, ht.TMaybeString, "Storage name")
98 45d4c81c Michael Hanselmann
99 45d4c81c Michael Hanselmann
_PUseLocking = ("use_locking", False, ht.TBool,
100 45d4c81c Michael Hanselmann
                "Whether to use synchronization")
101 45d4c81c Michael Hanselmann
102 45d4c81c Michael Hanselmann
_PNameCheck = ("name_check", True, ht.TBool, "Whether to check name")
103 45d4c81c Michael Hanselmann
104 45d4c81c Michael Hanselmann
_PNodeGroupAllocPolicy = \
105 45d4c81c Michael Hanselmann
  ("alloc_policy", None,
106 45d4c81c Michael Hanselmann
   ht.TOr(ht.TNone, ht.TElemOf(constants.VALID_ALLOC_POLICIES)),
107 45d4c81c Michael Hanselmann
   "Instance allocation policy")
108 45d4c81c Michael Hanselmann
109 45d4c81c Michael Hanselmann
_PGroupNodeParams = ("ndparams", None, ht.TMaybeDict,
110 45d4c81c Michael Hanselmann
                     "Default node parameters for group")
111 45d4c81c Michael Hanselmann
112 abd66bf8 Michael Hanselmann
_PQueryWhat = ("what", ht.NoDefault, ht.TElemOf(constants.QR_VIA_OP),
113 8e7078e0 Michael Hanselmann
               "Resource(s) to query for")
114 8e7078e0 Michael Hanselmann
115 45d4c81c Michael Hanselmann
_PIpCheckDoc = "Whether to ensure instance's IP address is inactive"
116 45d4c81c Michael Hanselmann
117 ff0d18e6 Iustin Pop
#: OP_ID conversion regular expression
118 ff0d18e6 Iustin Pop
_OPID_RE = re.compile("([a-z])([A-Z])")
119 ff0d18e6 Iustin Pop
120 8c9ee749 Michael Hanselmann
#: Utility function for L{OpClusterSetParams}
121 8c9ee749 Michael Hanselmann
_TestClusterOsList = ht.TOr(ht.TNone,
122 8c9ee749 Michael Hanselmann
  ht.TListOf(ht.TAnd(ht.TList, ht.TIsLength(2),
123 8c9ee749 Michael Hanselmann
    ht.TMap(ht.WithDesc("GetFirstItem")(operator.itemgetter(0)),
124 8c9ee749 Michael Hanselmann
            ht.TElemOf(constants.DDMS_VALUES)))))
125 8c9ee749 Michael Hanselmann
126 ff0d18e6 Iustin Pop
127 526a662a Michael Hanselmann
# TODO: Generate check from constants.INIC_PARAMS_TYPES
128 526a662a Michael Hanselmann
#: Utility function for testing NIC definitions
129 526a662a Michael Hanselmann
_TestNicDef = ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
130 526a662a Michael Hanselmann
                         ht.TOr(ht.TNone, ht.TNonEmptyString))
131 526a662a Michael Hanselmann
132 3ce9a5e7 Michael Hanselmann
_SUMMARY_PREFIX = {
133 3ce9a5e7 Michael Hanselmann
  "CLUSTER_": "C_",
134 3ce9a5e7 Michael Hanselmann
  "GROUP_": "G_",
135 3ce9a5e7 Michael Hanselmann
  "NODE_": "N_",
136 3ce9a5e7 Michael Hanselmann
  "INSTANCE_": "I_",
137 3ce9a5e7 Michael Hanselmann
  }
138 3ce9a5e7 Michael Hanselmann
139 526a662a Michael Hanselmann
140 ff0d18e6 Iustin Pop
def _NameToId(name):
141 ff0d18e6 Iustin Pop
  """Convert an opcode class name to an OP_ID.
142 ff0d18e6 Iustin Pop

143 ff0d18e6 Iustin Pop
  @type name: string
144 ff0d18e6 Iustin Pop
  @param name: the class name, as OpXxxYyy
145 ff0d18e6 Iustin Pop
  @rtype: string
146 ff0d18e6 Iustin Pop
  @return: the name in the OP_XXXX_YYYY format
147 ff0d18e6 Iustin Pop

148 ff0d18e6 Iustin Pop
  """
149 ff0d18e6 Iustin Pop
  if not name.startswith("Op"):
150 ff0d18e6 Iustin Pop
    return None
151 ff0d18e6 Iustin Pop
  # Note: (?<=[a-z])(?=[A-Z]) would be ideal, since it wouldn't
152 ff0d18e6 Iustin Pop
  # consume any input, and hence we would just have all the elements
153 ff0d18e6 Iustin Pop
  # in the list, one by one; but it seems that split doesn't work on
154 ff0d18e6 Iustin Pop
  # non-consuming input, hence we have to process the input string a
155 ff0d18e6 Iustin Pop
  # bit
156 ff0d18e6 Iustin Pop
  name = _OPID_RE.sub(r"\1,\2", name)
157 ff0d18e6 Iustin Pop
  elems = name.split(",")
158 ff0d18e6 Iustin Pop
  return "_".join(n.upper() for n in elems)
159 ff0d18e6 Iustin Pop
160 65e183af Michael Hanselmann
161 65e183af Michael Hanselmann
def RequireFileStorage():
162 65e183af Michael Hanselmann
  """Checks that file storage is enabled.
163 65e183af Michael Hanselmann

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

167 65e183af Michael Hanselmann
  @raise errors.OpPrereqError: when file storage is disabled
168 65e183af Michael Hanselmann

169 65e183af Michael Hanselmann
  """
170 65e183af Michael Hanselmann
  if not constants.ENABLE_FILE_STORAGE:
171 65e183af Michael Hanselmann
    raise errors.OpPrereqError("File storage disabled at configure time",
172 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
173 65e183af Michael Hanselmann
174 65e183af Michael Hanselmann
175 4b97f902 Apollon Oikonomopoulos
def RequireSharedFileStorage():
176 4b97f902 Apollon Oikonomopoulos
  """Checks that shared file storage is enabled.
177 4b97f902 Apollon Oikonomopoulos

178 4b97f902 Apollon Oikonomopoulos
  While it doesn't really fit into this module, L{utils} was deemed too large
179 4b97f902 Apollon Oikonomopoulos
  of a dependency to be imported for just one or two functions.
180 4b97f902 Apollon Oikonomopoulos

181 4b97f902 Apollon Oikonomopoulos
  @raise errors.OpPrereqError: when shared file storage is disabled
182 4b97f902 Apollon Oikonomopoulos

183 4b97f902 Apollon Oikonomopoulos
  """
184 4b97f902 Apollon Oikonomopoulos
  if not constants.ENABLE_SHARED_FILE_STORAGE:
185 4b97f902 Apollon Oikonomopoulos
    raise errors.OpPrereqError("Shared file storage disabled at"
186 4b97f902 Apollon Oikonomopoulos
                               " configure time", errors.ECODE_INVAL)
187 4b97f902 Apollon Oikonomopoulos
188 4b97f902 Apollon Oikonomopoulos
189 8c9ee749 Michael Hanselmann
@ht.WithDesc("CheckFileStorage")
190 8c9ee749 Michael Hanselmann
def _CheckFileStorage(value):
191 8c9ee749 Michael Hanselmann
  """Ensures file storage is enabled if used.
192 65e183af Michael Hanselmann

193 65e183af Michael Hanselmann
  """
194 8c9ee749 Michael Hanselmann
  if value == constants.DT_FILE:
195 65e183af Michael Hanselmann
    RequireFileStorage()
196 4b97f902 Apollon Oikonomopoulos
  elif value == constants.DT_SHARED_FILE:
197 4b97f902 Apollon Oikonomopoulos
    RequireSharedFileStorage()
198 65e183af Michael Hanselmann
  return True
199 65e183af Michael Hanselmann
200 65e183af Michael Hanselmann
201 8c9ee749 Michael Hanselmann
_CheckDiskTemplate = ht.TAnd(ht.TElemOf(constants.DISK_TEMPLATES),
202 8c9ee749 Michael Hanselmann
                             _CheckFileStorage)
203 8c9ee749 Michael Hanselmann
204 8c9ee749 Michael Hanselmann
205 65e183af Michael Hanselmann
def _CheckStorageType(storage_type):
206 65e183af Michael Hanselmann
  """Ensure a given storage type is valid.
207 65e183af Michael Hanselmann

208 65e183af Michael Hanselmann
  """
209 65e183af Michael Hanselmann
  if storage_type not in constants.VALID_STORAGE_TYPES:
210 65e183af Michael Hanselmann
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
211 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
212 65e183af Michael Hanselmann
  if storage_type == constants.ST_FILE:
213 65e183af Michael Hanselmann
    RequireFileStorage()
214 65e183af Michael Hanselmann
  return True
215 65e183af Michael Hanselmann
216 65e183af Michael Hanselmann
217 65e183af Michael Hanselmann
#: Storage type parameter
218 45d4c81c Michael Hanselmann
_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
219 45d4c81c Michael Hanselmann
                 "Storage type")
220 65e183af Michael Hanselmann
221 65e183af Michael Hanselmann
222 65e183af Michael Hanselmann
class _AutoOpParamSlots(type):
223 65e183af Michael Hanselmann
  """Meta class for opcode definitions.
224 65e183af Michael Hanselmann

225 65e183af Michael Hanselmann
  """
226 65e183af Michael Hanselmann
  def __new__(mcs, name, bases, attrs):
227 65e183af Michael Hanselmann
    """Called when a class should be created.
228 65e183af Michael Hanselmann

229 65e183af Michael Hanselmann
    @param mcs: The meta class
230 65e183af Michael Hanselmann
    @param name: Name of created class
231 65e183af Michael Hanselmann
    @param bases: Base classes
232 65e183af Michael Hanselmann
    @type attrs: dict
233 65e183af Michael Hanselmann
    @param attrs: Class attributes
234 65e183af Michael Hanselmann

235 65e183af Michael Hanselmann
    """
236 65e183af Michael Hanselmann
    assert "__slots__" not in attrs, \
237 65e183af Michael Hanselmann
      "Class '%s' defines __slots__ when it should use OP_PARAMS" % name
238 e89a9021 Iustin Pop
    assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
239 ff0d18e6 Iustin Pop
240 e89a9021 Iustin Pop
    attrs["OP_ID"] = _NameToId(name)
241 65e183af Michael Hanselmann
242 65e183af Michael Hanselmann
    # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
243 65e183af Michael Hanselmann
    params = attrs.setdefault("OP_PARAMS", [])
244 65e183af Michael Hanselmann
245 65e183af Michael Hanselmann
    # Use parameter names as slots
246 197b323b Michael Hanselmann
    slots = [pname for (pname, _, _, _) in params]
247 65e183af Michael Hanselmann
248 65e183af Michael Hanselmann
    assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
249 65e183af Michael Hanselmann
      "Class '%s' uses unknown field in OP_DSC_FIELD" % name
250 65e183af Michael Hanselmann
251 65e183af Michael Hanselmann
    attrs["__slots__"] = slots
252 65e183af Michael Hanselmann
253 65e183af Michael Hanselmann
    return type.__new__(mcs, name, bases, attrs)
254 65e183af Michael Hanselmann
255 df458e0b Iustin Pop
256 0e46916d Iustin Pop
class BaseOpCode(object):
257 df458e0b Iustin Pop
  """A simple serializable object.
258 df458e0b Iustin Pop

259 0e46916d Iustin Pop
  This object serves as a parent class for OpCode without any custom
260 0e46916d Iustin Pop
  field handling.
261 0e46916d Iustin Pop

262 df458e0b Iustin Pop
  """
263 e89a9021 Iustin Pop
  # pylint: disable-msg=E1101
264 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
265 65e183af Michael Hanselmann
  __metaclass__ = _AutoOpParamSlots
266 65e183af Michael Hanselmann
267 a8083063 Iustin Pop
  def __init__(self, **kwargs):
268 a7399f66 Iustin Pop
    """Constructor for BaseOpCode.
269 a7399f66 Iustin Pop

270 a7399f66 Iustin Pop
    The constructor takes only keyword arguments and will set
271 a7399f66 Iustin Pop
    attributes on this object based on the passed arguments. As such,
272 a7399f66 Iustin Pop
    it means that you should not pass arguments which are not in the
273 a7399f66 Iustin Pop
    __slots__ attribute for this class.
274 a7399f66 Iustin Pop

275 a7399f66 Iustin Pop
    """
276 adf385c7 Iustin Pop
    slots = self._all_slots()
277 a8083063 Iustin Pop
    for key in kwargs:
278 adf385c7 Iustin Pop
      if key not in slots:
279 df458e0b Iustin Pop
        raise TypeError("Object %s doesn't support the parameter '%s'" %
280 3ecf6786 Iustin Pop
                        (self.__class__.__name__, key))
281 a8083063 Iustin Pop
      setattr(self, key, kwargs[key])
282 a8083063 Iustin Pop
283 df458e0b Iustin Pop
  def __getstate__(self):
284 a7399f66 Iustin Pop
    """Generic serializer.
285 a7399f66 Iustin Pop

286 a7399f66 Iustin Pop
    This method just returns the contents of the instance as a
287 a7399f66 Iustin Pop
    dictionary.
288 a7399f66 Iustin Pop

289 a7399f66 Iustin Pop
    @rtype:  C{dict}
290 a7399f66 Iustin Pop
    @return: the instance attributes and their values
291 a7399f66 Iustin Pop

292 a7399f66 Iustin Pop
    """
293 df458e0b Iustin Pop
    state = {}
294 adf385c7 Iustin Pop
    for name in self._all_slots():
295 df458e0b Iustin Pop
      if hasattr(self, name):
296 df458e0b Iustin Pop
        state[name] = getattr(self, name)
297 df458e0b Iustin Pop
    return state
298 df458e0b Iustin Pop
299 df458e0b Iustin Pop
  def __setstate__(self, state):
300 a7399f66 Iustin Pop
    """Generic unserializer.
301 a7399f66 Iustin Pop

302 a7399f66 Iustin Pop
    This method just restores from the serialized state the attributes
303 a7399f66 Iustin Pop
    of the current instance.
304 a7399f66 Iustin Pop

305 a7399f66 Iustin Pop
    @param state: the serialized opcode data
306 a7399f66 Iustin Pop
    @type state:  C{dict}
307 a7399f66 Iustin Pop

308 a7399f66 Iustin Pop
    """
309 df458e0b Iustin Pop
    if not isinstance(state, dict):
310 df458e0b Iustin Pop
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
311 df458e0b Iustin Pop
                       type(state))
312 df458e0b Iustin Pop
313 adf385c7 Iustin Pop
    for name in self._all_slots():
314 44db3a6f Iustin Pop
      if name not in state and hasattr(self, name):
315 df458e0b Iustin Pop
        delattr(self, name)
316 df458e0b Iustin Pop
317 df458e0b Iustin Pop
    for name in state:
318 df458e0b Iustin Pop
      setattr(self, name, state[name])
319 df458e0b Iustin Pop
320 adf385c7 Iustin Pop
  @classmethod
321 adf385c7 Iustin Pop
  def _all_slots(cls):
322 adf385c7 Iustin Pop
    """Compute the list of all declared slots for a class.
323 adf385c7 Iustin Pop

324 adf385c7 Iustin Pop
    """
325 adf385c7 Iustin Pop
    slots = []
326 adf385c7 Iustin Pop
    for parent in cls.__mro__:
327 adf385c7 Iustin Pop
      slots.extend(getattr(parent, "__slots__", []))
328 adf385c7 Iustin Pop
    return slots
329 adf385c7 Iustin Pop
330 65e183af Michael Hanselmann
  @classmethod
331 65e183af Michael Hanselmann
  def GetAllParams(cls):
332 65e183af Michael Hanselmann
    """Compute list of all parameters for an opcode.
333 65e183af Michael Hanselmann

334 65e183af Michael Hanselmann
    """
335 65e183af Michael Hanselmann
    slots = []
336 65e183af Michael Hanselmann
    for parent in cls.__mro__:
337 65e183af Michael Hanselmann
      slots.extend(getattr(parent, "OP_PARAMS", []))
338 65e183af Michael Hanselmann
    return slots
339 65e183af Michael Hanselmann
340 1cbef6d8 Michael Hanselmann
  def Validate(self, set_defaults):
341 1cbef6d8 Michael Hanselmann
    """Validate opcode parameters, optionally setting default values.
342 1cbef6d8 Michael Hanselmann

343 1cbef6d8 Michael Hanselmann
    @type set_defaults: bool
344 1cbef6d8 Michael Hanselmann
    @param set_defaults: Whether to set default values
345 1cbef6d8 Michael Hanselmann
    @raise errors.OpPrereqError: When a parameter value doesn't match
346 1cbef6d8 Michael Hanselmann
                                 requirements
347 1cbef6d8 Michael Hanselmann

348 1cbef6d8 Michael Hanselmann
    """
349 197b323b Michael Hanselmann
    for (attr_name, default, test, _) in self.GetAllParams():
350 1cbef6d8 Michael Hanselmann
      assert test == ht.NoType or callable(test)
351 1cbef6d8 Michael Hanselmann
352 1cbef6d8 Michael Hanselmann
      if not hasattr(self, attr_name):
353 1cbef6d8 Michael Hanselmann
        if default == ht.NoDefault:
354 1cbef6d8 Michael Hanselmann
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
355 1cbef6d8 Michael Hanselmann
                                     (self.OP_ID, attr_name),
356 1cbef6d8 Michael Hanselmann
                                     errors.ECODE_INVAL)
357 1cbef6d8 Michael Hanselmann
        elif set_defaults:
358 1cbef6d8 Michael Hanselmann
          if callable(default):
359 1cbef6d8 Michael Hanselmann
            dval = default()
360 1cbef6d8 Michael Hanselmann
          else:
361 1cbef6d8 Michael Hanselmann
            dval = default
362 1cbef6d8 Michael Hanselmann
          setattr(self, attr_name, dval)
363 1cbef6d8 Michael Hanselmann
364 1cbef6d8 Michael Hanselmann
      if test == ht.NoType:
365 1cbef6d8 Michael Hanselmann
        # no tests here
366 1cbef6d8 Michael Hanselmann
        continue
367 1cbef6d8 Michael Hanselmann
368 1cbef6d8 Michael Hanselmann
      if set_defaults or hasattr(self, attr_name):
369 1cbef6d8 Michael Hanselmann
        attr_val = getattr(self, attr_name)
370 1cbef6d8 Michael Hanselmann
        if not test(attr_val):
371 1cbef6d8 Michael Hanselmann
          logging.error("OpCode %s, parameter %s, has invalid type %s/value %s",
372 1cbef6d8 Michael Hanselmann
                        self.OP_ID, attr_name, type(attr_val), attr_val)
373 1cbef6d8 Michael Hanselmann
          raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
374 1cbef6d8 Michael Hanselmann
                                     (self.OP_ID, attr_name),
375 1cbef6d8 Michael Hanselmann
                                     errors.ECODE_INVAL)
376 1cbef6d8 Michael Hanselmann
377 df458e0b Iustin Pop
378 0e46916d Iustin Pop
class OpCode(BaseOpCode):
379 a7399f66 Iustin Pop
  """Abstract OpCode.
380 a7399f66 Iustin Pop

381 a7399f66 Iustin Pop
  This is the root of the actual OpCode hierarchy. All clases derived
382 a7399f66 Iustin Pop
  from this class should override OP_ID.
383 a7399f66 Iustin Pop

384 a7399f66 Iustin Pop
  @cvar OP_ID: The ID of this opcode. This should be unique amongst all
385 20777413 Iustin Pop
               children of this class.
386 bde8f481 Adeodato Simo
  @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
387 bde8f481 Adeodato Simo
                      string returned by Summary(); see the docstring of that
388 bde8f481 Adeodato Simo
                      method for details).
389 65e183af Michael Hanselmann
  @cvar OP_PARAMS: List of opcode attributes, the default values they should
390 65e183af Michael Hanselmann
                   get if not already defined, and types they must match.
391 687c10d9 Iustin Pop
  @cvar WITH_LU: Boolean that specifies whether this should be included in
392 687c10d9 Iustin Pop
      mcpu's dispatch table
393 20777413 Iustin Pop
  @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
394 20777413 Iustin Pop
                 the check steps
395 8f5c488d Michael Hanselmann
  @ivar priority: Opcode priority for queue
396 a7399f66 Iustin Pop

397 a7399f66 Iustin Pop
  """
398 e89a9021 Iustin Pop
  # pylint: disable-msg=E1101
399 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
400 687c10d9 Iustin Pop
  WITH_LU = True
401 65e183af Michael Hanselmann
  OP_PARAMS = [
402 45d4c81c Michael Hanselmann
    ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
403 45d4c81c Michael Hanselmann
    ("debug_level", None, ht.TOr(ht.TNone, ht.TPositiveInt), "Debug level"),
404 65e183af Michael Hanselmann
    ("priority", constants.OP_PRIO_DEFAULT,
405 45d4c81c Michael Hanselmann
     ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
406 65e183af Michael Hanselmann
    ]
407 df458e0b Iustin Pop
408 df458e0b Iustin Pop
  def __getstate__(self):
409 df458e0b Iustin Pop
    """Specialized getstate for opcodes.
410 df458e0b Iustin Pop

411 a7399f66 Iustin Pop
    This method adds to the state dictionary the OP_ID of the class,
412 a7399f66 Iustin Pop
    so that on unload we can identify the correct class for
413 a7399f66 Iustin Pop
    instantiating the opcode.
414 a7399f66 Iustin Pop

415 a7399f66 Iustin Pop
    @rtype:   C{dict}
416 a7399f66 Iustin Pop
    @return:  the state as a dictionary
417 a7399f66 Iustin Pop

418 df458e0b Iustin Pop
    """
419 0e46916d Iustin Pop
    data = BaseOpCode.__getstate__(self)
420 df458e0b Iustin Pop
    data["OP_ID"] = self.OP_ID
421 df458e0b Iustin Pop
    return data
422 df458e0b Iustin Pop
423 df458e0b Iustin Pop
  @classmethod
424 00abdc96 Iustin Pop
  def LoadOpCode(cls, data):
425 df458e0b Iustin Pop
    """Generic load opcode method.
426 df458e0b Iustin Pop

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

431 a7399f66 Iustin Pop
    @type data:  C{dict}
432 a7399f66 Iustin Pop
    @param data: the serialized opcode
433 a7399f66 Iustin Pop

434 df458e0b Iustin Pop
    """
435 df458e0b Iustin Pop
    if not isinstance(data, dict):
436 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
437 df458e0b Iustin Pop
    if "OP_ID" not in data:
438 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
439 df458e0b Iustin Pop
    op_id = data["OP_ID"]
440 df458e0b Iustin Pop
    op_class = None
441 363acb1e Iustin Pop
    if op_id in OP_MAPPING:
442 363acb1e Iustin Pop
      op_class = OP_MAPPING[op_id]
443 363acb1e Iustin Pop
    else:
444 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
445 df458e0b Iustin Pop
                       op_id)
446 df458e0b Iustin Pop
    op = op_class()
447 df458e0b Iustin Pop
    new_data = data.copy()
448 df458e0b Iustin Pop
    del new_data["OP_ID"]
449 df458e0b Iustin Pop
    op.__setstate__(new_data)
450 df458e0b Iustin Pop
    return op
451 df458e0b Iustin Pop
452 60dd1473 Iustin Pop
  def Summary(self):
453 60dd1473 Iustin Pop
    """Generates a summary description of this opcode.
454 60dd1473 Iustin Pop

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

461 60dd1473 Iustin Pop
    """
462 ff0d18e6 Iustin Pop
    assert self.OP_ID is not None and len(self.OP_ID) > 3
463 60dd1473 Iustin Pop
    # all OP_ID start with OP_, we remove that
464 60dd1473 Iustin Pop
    txt = self.OP_ID[3:]
465 60dd1473 Iustin Pop
    field_name = getattr(self, "OP_DSC_FIELD", None)
466 60dd1473 Iustin Pop
    if field_name:
467 60dd1473 Iustin Pop
      field_value = getattr(self, field_name, None)
468 bc8bbda1 Iustin Pop
      if isinstance(field_value, (list, tuple)):
469 bc8bbda1 Iustin Pop
        field_value = ",".join(str(i) for i in field_value)
470 60dd1473 Iustin Pop
      txt = "%s(%s)" % (txt, field_value)
471 60dd1473 Iustin Pop
    return txt
472 60dd1473 Iustin Pop
473 3ce9a5e7 Michael Hanselmann
  def TinySummary(self):
474 3ce9a5e7 Michael Hanselmann
    """Generates a compact summary description of the opcode.
475 3ce9a5e7 Michael Hanselmann

476 3ce9a5e7 Michael Hanselmann
    """
477 3ce9a5e7 Michael Hanselmann
    assert self.OP_ID.startswith("OP_")
478 3ce9a5e7 Michael Hanselmann
479 3ce9a5e7 Michael Hanselmann
    text = self.OP_ID[3:]
480 3ce9a5e7 Michael Hanselmann
481 3ce9a5e7 Michael Hanselmann
    for (prefix, supplement) in _SUMMARY_PREFIX.items():
482 3ce9a5e7 Michael Hanselmann
      if text.startswith(prefix):
483 3ce9a5e7 Michael Hanselmann
        return supplement + text[len(prefix):]
484 3ce9a5e7 Michael Hanselmann
485 3ce9a5e7 Michael Hanselmann
    return text
486 3ce9a5e7 Michael Hanselmann
487 a8083063 Iustin Pop
488 afee0879 Iustin Pop
# cluster opcodes
489 afee0879 Iustin Pop
490 bc84ffa7 Iustin Pop
class OpClusterPostInit(OpCode):
491 b5f5fae9 Luca Bigliardi
  """Post cluster initialization.
492 b5f5fae9 Luca Bigliardi

493 b5f5fae9 Luca Bigliardi
  This opcode does not touch the cluster at all. Its purpose is to run hooks
494 b5f5fae9 Luca Bigliardi
  after the cluster has been initialized.
495 b5f5fae9 Luca Bigliardi

496 b5f5fae9 Luca Bigliardi
  """
497 b5f5fae9 Luca Bigliardi
498 b5f5fae9 Luca Bigliardi
499 c6d43e9e Iustin Pop
class OpClusterDestroy(OpCode):
500 a7399f66 Iustin Pop
  """Destroy the cluster.
501 a7399f66 Iustin Pop

502 a7399f66 Iustin Pop
  This opcode has no other parameters. All the state is irreversibly
503 a7399f66 Iustin Pop
  lost after the execution of this opcode.
504 a7399f66 Iustin Pop

505 a7399f66 Iustin Pop
  """
506 a8083063 Iustin Pop
507 a8083063 Iustin Pop
508 a2f7ab92 Iustin Pop
class OpClusterQuery(OpCode):
509 fdc267f4 Iustin Pop
  """Query cluster information."""
510 a8083063 Iustin Pop
511 a8083063 Iustin Pop
512 a3d32770 Iustin Pop
class OpClusterVerify(OpCode):
513 a7399f66 Iustin Pop
  """Verify the cluster state.
514 a7399f66 Iustin Pop

515 a7399f66 Iustin Pop
  @type skip_checks: C{list}
516 a7399f66 Iustin Pop
  @ivar skip_checks: steps to be skipped from the verify process; this
517 a7399f66 Iustin Pop
                     needs to be a subset of
518 a7399f66 Iustin Pop
                     L{constants.VERIFY_OPTIONAL_CHECKS}; currently
519 a7399f66 Iustin Pop
                     only L{constants.VERIFY_NPLUSONE_MEM} can be passed
520 a7399f66 Iustin Pop

521 a7399f66 Iustin Pop
  """
522 65e183af Michael Hanselmann
  OP_PARAMS = [
523 65e183af Michael Hanselmann
    ("skip_checks", ht.EmptyList,
524 197b323b Michael Hanselmann
     ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)), None),
525 197b323b Michael Hanselmann
    ("verbose", False, ht.TBool, None),
526 197b323b Michael Hanselmann
    ("error_codes", False, ht.TBool, None),
527 197b323b Michael Hanselmann
    ("debug_simulate_errors", False, ht.TBool, None),
528 65e183af Michael Hanselmann
    ]
529 a8083063 Iustin Pop
530 a8083063 Iustin Pop
531 bd8210a7 Iustin Pop
class OpClusterVerifyDisks(OpCode):
532 150e978f Iustin Pop
  """Verify the cluster disks.
533 150e978f Iustin Pop

534 150e978f Iustin Pop
  Parameters: none
535 150e978f Iustin Pop

536 5188ab37 Iustin Pop
  Result: a tuple of four elements:
537 150e978f Iustin Pop
    - list of node names with bad data returned (unreachable, etc.)
538 a7399f66 Iustin Pop
    - dict of node names with broken volume groups (values: error msg)
539 150e978f Iustin Pop
    - list of instances with degraded disks (that should be activated)
540 b63ed789 Iustin Pop
    - dict of instances with missing logical volumes (values: (node, vol)
541 b63ed789 Iustin Pop
      pairs with details about the missing volumes)
542 150e978f Iustin Pop

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

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

551 150e978f Iustin Pop
  """
552 150e978f Iustin Pop
553 150e978f Iustin Pop
554 5d01aca3 Iustin Pop
class OpClusterRepairDiskSizes(OpCode):
555 60975797 Iustin Pop
  """Verify the disk sizes of the instances and fixes configuration
556 60975797 Iustin Pop
  mimatches.
557 60975797 Iustin Pop

558 60975797 Iustin Pop
  Parameters: optional instances list, in case we want to restrict the
559 60975797 Iustin Pop
  checks to only a subset of the instances.
560 60975797 Iustin Pop

561 60975797 Iustin Pop
  Result: a list of tuples, (instance, disk, new-size) for changed
562 60975797 Iustin Pop
  configurations.
563 60975797 Iustin Pop

564 60975797 Iustin Pop
  In normal operation, the list should be empty.
565 60975797 Iustin Pop

566 60975797 Iustin Pop
  @type instances: list
567 60975797 Iustin Pop
  @ivar instances: the list of instances to check, or empty for all instances
568 60975797 Iustin Pop

569 60975797 Iustin Pop
  """
570 65e183af Michael Hanselmann
  OP_PARAMS = [
571 197b323b Michael Hanselmann
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
572 65e183af Michael Hanselmann
    ]
573 60975797 Iustin Pop
574 60975797 Iustin Pop
575 2f093ea0 Iustin Pop
class OpClusterConfigQuery(OpCode):
576 ae5849b5 Michael Hanselmann
  """Query cluster configuration values."""
577 65e183af Michael Hanselmann
  OP_PARAMS = [
578 65e183af Michael Hanselmann
    _POutputFields
579 65e183af Michael Hanselmann
    ]
580 a8083063 Iustin Pop
581 a8083063 Iustin Pop
582 e126df25 Iustin Pop
class OpClusterRename(OpCode):
583 a7399f66 Iustin Pop
  """Rename the cluster.
584 a7399f66 Iustin Pop

585 a7399f66 Iustin Pop
  @type name: C{str}
586 a7399f66 Iustin Pop
  @ivar name: The new name of the cluster. The name and/or the master IP
587 a7399f66 Iustin Pop
              address will be changed to match the new name and its IP
588 a7399f66 Iustin Pop
              address.
589 a7399f66 Iustin Pop

590 a7399f66 Iustin Pop
  """
591 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
592 65e183af Michael Hanselmann
  OP_PARAMS = [
593 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
594 65e183af Michael Hanselmann
    ]
595 07bd8a51 Iustin Pop
596 07bd8a51 Iustin Pop
597 a6682fdc Iustin Pop
class OpClusterSetParams(OpCode):
598 a7399f66 Iustin Pop
  """Change the parameters of the cluster.
599 a7399f66 Iustin Pop

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

603 a7399f66 Iustin Pop
  """
604 65e183af Michael Hanselmann
  OP_PARAMS = [
605 45d4c81c Michael Hanselmann
    ("vg_name", None, ht.TMaybeString, "Volume group name"),
606 65e183af Michael Hanselmann
    ("enabled_hypervisors", None,
607 65e183af Michael Hanselmann
     ht.TOr(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)), ht.TTrue),
608 45d4c81c Michael Hanselmann
            ht.TNone),
609 45d4c81c Michael Hanselmann
     "List of enabled hypervisors"),
610 65e183af Michael Hanselmann
    ("hvparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
611 45d4c81c Michael Hanselmann
                              ht.TNone),
612 45d4c81c Michael Hanselmann
     "Cluster-wide hypervisor parameter defaults, hypervisor-dependent"),
613 45d4c81c Michael Hanselmann
    ("beparams", None, ht.TOr(ht.TDict, ht.TNone),
614 45d4c81c Michael Hanselmann
     "Cluster-wide backend parameter defaults"),
615 65e183af Michael Hanselmann
    ("os_hvp", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
616 45d4c81c Michael Hanselmann
                            ht.TNone),
617 45d4c81c Michael Hanselmann
     "Cluster-wide per-OS hypervisor parameter defaults"),
618 65e183af Michael Hanselmann
    ("osparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
619 45d4c81c Michael Hanselmann
                              ht.TNone),
620 45d4c81c Michael Hanselmann
     "Cluster-wide OS parameter defaults"),
621 197b323b Michael Hanselmann
    ("candidate_pool_size", None, ht.TOr(ht.TStrictPositiveInt, ht.TNone),
622 45d4c81c Michael Hanselmann
     "Master candidate pool size"),
623 45d4c81c Michael Hanselmann
    ("uid_pool", None, ht.NoType,
624 45d4c81c Michael Hanselmann
     "Set UID pool, must be list of lists describing UID ranges (two items,"
625 45d4c81c Michael Hanselmann
     " start and end inclusive)"),
626 45d4c81c Michael Hanselmann
    ("add_uids", None, ht.NoType,
627 45d4c81c Michael Hanselmann
     "Extend UID pool, must be list of lists describing UID ranges (two"
628 45d4c81c Michael Hanselmann
     " items, start and end inclusive) to be added"),
629 45d4c81c Michael Hanselmann
    ("remove_uids", None, ht.NoType,
630 45d4c81c Michael Hanselmann
     "Shrink UID pool, must be list of lists describing UID ranges (two"
631 45d4c81c Michael Hanselmann
     " items, start and end inclusive) to be removed"),
632 45d4c81c Michael Hanselmann
    ("maintain_node_health", None, ht.TMaybeBool,
633 45d4c81c Michael Hanselmann
     "Whether to automatically maintain node health"),
634 45d4c81c Michael Hanselmann
    ("prealloc_wipe_disks", None, ht.TMaybeBool,
635 45d4c81c Michael Hanselmann
     "Whether to wipe disks before allocating them to instances"),
636 45d4c81c Michael Hanselmann
    ("nicparams", None, ht.TMaybeDict, "Cluster-wide NIC parameter defaults"),
637 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Cluster-wide node parameter defaults"),
638 45d4c81c Michael Hanselmann
    ("drbd_helper", None, ht.TOr(ht.TString, ht.TNone), "DRBD helper program"),
639 45d4c81c Michael Hanselmann
    ("default_iallocator", None, ht.TOr(ht.TString, ht.TNone),
640 45d4c81c Michael Hanselmann
     "Default iallocator for cluster"),
641 45d4c81c Michael Hanselmann
    ("master_netdev", None, ht.TOr(ht.TString, ht.TNone),
642 45d4c81c Michael Hanselmann
     "Master network device"),
643 45d4c81c Michael Hanselmann
    ("reserved_lvs", None, ht.TOr(ht.TListOf(ht.TNonEmptyString), ht.TNone),
644 45d4c81c Michael Hanselmann
     "List of reserved LVs"),
645 45d4c81c Michael Hanselmann
    ("hidden_os", None, _TestClusterOsList,
646 45d4c81c Michael Hanselmann
     "Modify list of hidden operating systems. Each modification must have"
647 45d4c81c Michael Hanselmann
     " two items, the operation and the OS name. The operation can be"
648 45d4c81c Michael Hanselmann
     " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
649 45d4c81c Michael Hanselmann
    ("blacklisted_os", None, _TestClusterOsList,
650 45d4c81c Michael Hanselmann
     "Modify list of blacklisted operating systems. Each modification must have"
651 45d4c81c Michael Hanselmann
     " two items, the operation and the OS name. The operation can be"
652 45d4c81c Michael Hanselmann
     " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
653 4b7735f9 Iustin Pop
    ]
654 12515db7 Manuel Franceschini
655 12515db7 Manuel Franceschini
656 d1240007 Iustin Pop
class OpClusterRedistConf(OpCode):
657 afee0879 Iustin Pop
  """Force a full push of the cluster configuration.
658 afee0879 Iustin Pop

659 afee0879 Iustin Pop
  """
660 afee0879 Iustin Pop
661 83f72637 Michael Hanselmann
662 83f72637 Michael Hanselmann
class OpQuery(OpCode):
663 83f72637 Michael Hanselmann
  """Query for resources/items.
664 83f72637 Michael Hanselmann

665 abd66bf8 Michael Hanselmann
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
666 83f72637 Michael Hanselmann
  @ivar fields: List of fields to retrieve
667 83f72637 Michael Hanselmann
  @ivar filter: Query filter
668 83f72637 Michael Hanselmann

669 83f72637 Michael Hanselmann
  """
670 65e183af Michael Hanselmann
  OP_PARAMS = [
671 8e7078e0 Michael Hanselmann
    _PQueryWhat,
672 45d4c81c Michael Hanselmann
    ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
673 45d4c81c Michael Hanselmann
     "Requested fields"),
674 b107fe05 Michael Hanselmann
    ("filter", None, ht.TOr(ht.TNone, ht.TListOf),
675 45d4c81c Michael Hanselmann
     "Query filter"),
676 83f72637 Michael Hanselmann
    ]
677 83f72637 Michael Hanselmann
678 83f72637 Michael Hanselmann
679 83f72637 Michael Hanselmann
class OpQueryFields(OpCode):
680 83f72637 Michael Hanselmann
  """Query for available resource/item fields.
681 83f72637 Michael Hanselmann

682 abd66bf8 Michael Hanselmann
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
683 83f72637 Michael Hanselmann
  @ivar fields: List of fields to retrieve
684 83f72637 Michael Hanselmann

685 83f72637 Michael Hanselmann
  """
686 65e183af Michael Hanselmann
  OP_PARAMS = [
687 8e7078e0 Michael Hanselmann
    _PQueryWhat,
688 8e7078e0 Michael Hanselmann
    ("fields", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
689 8e7078e0 Michael Hanselmann
     "Requested fields; if not given, all are returned"),
690 83f72637 Michael Hanselmann
    ]
691 83f72637 Michael Hanselmann
692 83f72637 Michael Hanselmann
693 792af3ad Renรฉ Nussbaumer
class OpOobCommand(OpCode):
694 eb64da59 Renรฉ Nussbaumer
  """Interact with OOB."""
695 65e183af Michael Hanselmann
  OP_PARAMS = [
696 c4ec0755 Renรฉ Nussbaumer
    ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
697 c4ec0755 Renรฉ Nussbaumer
     "List of nodes to run the OOB command against"),
698 c4ec0755 Renรฉ Nussbaumer
    ("command", None, ht.TElemOf(constants.OOB_COMMANDS),
699 c4ec0755 Renรฉ Nussbaumer
     "OOB command to be run"),
700 c4ec0755 Renรฉ Nussbaumer
    ("timeout", constants.OOB_TIMEOUT, ht.TInt,
701 c4ec0755 Renรฉ Nussbaumer
     "Timeout before the OOB helper will be terminated"),
702 c4ec0755 Renรฉ Nussbaumer
    ("ignore_status", False, ht.TBool,
703 c4ec0755 Renรฉ Nussbaumer
     "Ignores the node offline status for power off"),
704 beff3779 Renรฉ Nussbaumer
    ("power_delay", constants.OOB_POWER_DELAY, ht.TPositiveFloat,
705 beff3779 Renรฉ Nussbaumer
     "Time in seconds to wait between powering on nodes"),
706 eb64da59 Renรฉ Nussbaumer
    ]
707 eb64da59 Renรฉ Nussbaumer
708 eb64da59 Renรฉ Nussbaumer
709 07bd8a51 Iustin Pop
# node opcodes
710 07bd8a51 Iustin Pop
711 73d565a3 Iustin Pop
class OpNodeRemove(OpCode):
712 a7399f66 Iustin Pop
  """Remove a node.
713 a7399f66 Iustin Pop

714 a7399f66 Iustin Pop
  @type node_name: C{str}
715 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to remove. If the node still has
716 a7399f66 Iustin Pop
                   instances on it, the operation will fail.
717 a7399f66 Iustin Pop

718 a7399f66 Iustin Pop
  """
719 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
720 65e183af Michael Hanselmann
  OP_PARAMS = [
721 65e183af Michael Hanselmann
    _PNodeName,
722 65e183af Michael Hanselmann
    ]
723 a8083063 Iustin Pop
724 a8083063 Iustin Pop
725 d817d49f Iustin Pop
class OpNodeAdd(OpCode):
726 a7399f66 Iustin Pop
  """Add a node to the cluster.
727 a7399f66 Iustin Pop

728 a7399f66 Iustin Pop
  @type node_name: C{str}
729 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to add. This can be a short name,
730 a7399f66 Iustin Pop
                   but it will be expanded to the FQDN.
731 a7399f66 Iustin Pop
  @type primary_ip: IP address
732 a7399f66 Iustin Pop
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
733 a7399f66 Iustin Pop
                    opcode is submitted, but will be filled during the node
734 a7399f66 Iustin Pop
                    add (so it will be visible in the job query).
735 a7399f66 Iustin Pop
  @type secondary_ip: IP address
736 a7399f66 Iustin Pop
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
737 a7399f66 Iustin Pop
                      if the cluster has been initialized in 'dual-network'
738 a7399f66 Iustin Pop
                      mode, otherwise it must not be given.
739 a7399f66 Iustin Pop
  @type readd: C{bool}
740 a7399f66 Iustin Pop
  @ivar readd: Whether to re-add an existing node to the cluster. If
741 a7399f66 Iustin Pop
               this is not passed, then the operation will abort if the node
742 a7399f66 Iustin Pop
               name is already in the cluster; use this parameter to 'repair'
743 a7399f66 Iustin Pop
               a node that had its configuration broken, or was reinstalled
744 a7399f66 Iustin Pop
               without removal from the cluster.
745 f936c153 Iustin Pop
  @type group: C{str}
746 f936c153 Iustin Pop
  @ivar group: The node group to which this node will belong.
747 fd3d37b6 Iustin Pop
  @type vm_capable: C{bool}
748 fd3d37b6 Iustin Pop
  @ivar vm_capable: The vm_capable node attribute
749 fd3d37b6 Iustin Pop
  @type master_capable: C{bool}
750 fd3d37b6 Iustin Pop
  @ivar master_capable: The master_capable node attribute
751 a7399f66 Iustin Pop

752 a7399f66 Iustin Pop
  """
753 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
754 65e183af Michael Hanselmann
  OP_PARAMS = [
755 65e183af Michael Hanselmann
    _PNodeName,
756 45d4c81c Michael Hanselmann
    ("primary_ip", None, ht.NoType, "Primary IP address"),
757 45d4c81c Michael Hanselmann
    ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
758 45d4c81c Michael Hanselmann
    ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
759 45d4c81c Michael Hanselmann
    ("group", None, ht.TMaybeString, "Initial node group"),
760 45d4c81c Michael Hanselmann
    ("master_capable", None, ht.TMaybeBool,
761 45d4c81c Michael Hanselmann
     "Whether node can become master or master candidate"),
762 45d4c81c Michael Hanselmann
    ("vm_capable", None, ht.TMaybeBool,
763 45d4c81c Michael Hanselmann
     "Whether node can host instances"),
764 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Node parameters"),
765 65e183af Michael Hanselmann
    ]
766 a8083063 Iustin Pop
767 a8083063 Iustin Pop
768 2237687b Iustin Pop
class OpNodeQuery(OpCode):
769 a8083063 Iustin Pop
  """Compute the list of nodes."""
770 65e183af Michael Hanselmann
  OP_PARAMS = [
771 65e183af Michael Hanselmann
    _POutputFields,
772 45d4c81c Michael Hanselmann
    _PUseLocking,
773 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
774 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
775 65e183af Michael Hanselmann
    ]
776 a8083063 Iustin Pop
777 a8083063 Iustin Pop
778 8ed55bfd Iustin Pop
class OpNodeQueryvols(OpCode):
779 dcb93971 Michael Hanselmann
  """Get list of volumes on node."""
780 65e183af Michael Hanselmann
  OP_PARAMS = [
781 65e183af Michael Hanselmann
    _POutputFields,
782 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
783 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
784 65e183af Michael Hanselmann
    ]
785 dcb93971 Michael Hanselmann
786 dcb93971 Michael Hanselmann
787 ad8d0595 Iustin Pop
class OpNodeQueryStorage(OpCode):
788 9e5442ce Michael Hanselmann
  """Get information on storage for node(s)."""
789 65e183af Michael Hanselmann
  OP_PARAMS = [
790 65e183af Michael Hanselmann
    _POutputFields,
791 65e183af Michael Hanselmann
    _PStorageType,
792 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
793 45d4c81c Michael Hanselmann
    ("name", None, ht.TMaybeString, "Storage name"),
794 9e5442ce Michael Hanselmann
    ]
795 9e5442ce Michael Hanselmann
796 9e5442ce Michael Hanselmann
797 2cee4077 Iustin Pop
class OpNodeModifyStorage(OpCode):
798 099c52ad Iustin Pop
  """Modifies the properies of a storage unit"""
799 65e183af Michael Hanselmann
  OP_PARAMS = [
800 65e183af Michael Hanselmann
    _PNodeName,
801 65e183af Michael Hanselmann
    _PStorageType,
802 45d4c81c Michael Hanselmann
    _PStorageName,
803 45d4c81c Michael Hanselmann
    ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
804 efb8da02 Michael Hanselmann
    ]
805 efb8da02 Michael Hanselmann
806 efb8da02 Michael Hanselmann
807 76aef8fc Michael Hanselmann
class OpRepairNodeStorage(OpCode):
808 76aef8fc Michael Hanselmann
  """Repairs the volume group on a node."""
809 76aef8fc Michael Hanselmann
  OP_DSC_FIELD = "node_name"
810 65e183af Michael Hanselmann
  OP_PARAMS = [
811 65e183af Michael Hanselmann
    _PNodeName,
812 65e183af Michael Hanselmann
    _PStorageType,
813 45d4c81c Michael Hanselmann
    _PStorageName,
814 45d4c81c Michael Hanselmann
    _PIgnoreConsistency,
815 76aef8fc Michael Hanselmann
    ]
816 76aef8fc Michael Hanselmann
817 76aef8fc Michael Hanselmann
818 f13973c4 Iustin Pop
class OpNodeSetParams(OpCode):
819 b31c8676 Iustin Pop
  """Change the parameters of a node."""
820 b31c8676 Iustin Pop
  OP_DSC_FIELD = "node_name"
821 65e183af Michael Hanselmann
  OP_PARAMS = [
822 65e183af Michael Hanselmann
    _PNodeName,
823 65e183af Michael Hanselmann
    _PForce,
824 45d4c81c Michael Hanselmann
    ("master_candidate", None, ht.TMaybeBool,
825 45d4c81c Michael Hanselmann
     "Whether the node should become a master candidate"),
826 45d4c81c Michael Hanselmann
    ("offline", None, ht.TMaybeBool,
827 45d4c81c Michael Hanselmann
     "Whether the node should be marked as offline"),
828 45d4c81c Michael Hanselmann
    ("drained", None, ht.TMaybeBool,
829 45d4c81c Michael Hanselmann
     "Whether the node should be marked as drained"),
830 45d4c81c Michael Hanselmann
    ("auto_promote", False, ht.TBool,
831 45d4c81c Michael Hanselmann
     "Whether node(s) should be promoted to master candidate if necessary"),
832 45d4c81c Michael Hanselmann
    ("master_capable", None, ht.TMaybeBool,
833 45d4c81c Michael Hanselmann
     "Denote whether node can become master or master candidate"),
834 45d4c81c Michael Hanselmann
    ("vm_capable", None, ht.TMaybeBool,
835 45d4c81c Michael Hanselmann
     "Denote whether node can host instances"),
836 45d4c81c Michael Hanselmann
    ("secondary_ip", None, ht.TMaybeString,
837 45d4c81c Michael Hanselmann
     "Change node's secondary IP address"),
838 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
839 45d4c81c Michael Hanselmann
    ("powered", None, ht.TMaybeBool,
840 45d4c81c Michael Hanselmann
     "Whether the node should be marked as powered"),
841 b31c8676 Iustin Pop
    ]
842 b31c8676 Iustin Pop
843 f5118ade Iustin Pop
844 e0d4735f Iustin Pop
class OpNodePowercycle(OpCode):
845 f5118ade Iustin Pop
  """Tries to powercycle a node."""
846 f5118ade Iustin Pop
  OP_DSC_FIELD = "node_name"
847 65e183af Michael Hanselmann
  OP_PARAMS = [
848 65e183af Michael Hanselmann
    _PNodeName,
849 65e183af Michael Hanselmann
    _PForce,
850 f5118ade Iustin Pop
    ]
851 f5118ade Iustin Pop
852 7ffc5a86 Michael Hanselmann
853 5b14a488 Iustin Pop
class OpNodeMigrate(OpCode):
854 80cb875c Michael Hanselmann
  """Migrate all instances from a node."""
855 80cb875c Michael Hanselmann
  OP_DSC_FIELD = "node_name"
856 65e183af Michael Hanselmann
  OP_PARAMS = [
857 65e183af Michael Hanselmann
    _PNodeName,
858 65e183af Michael Hanselmann
    _PMigrationMode,
859 65e183af Michael Hanselmann
    _PMigrationLive,
860 8eb34306 Apollon Oikonomopoulos
    ("iallocator", None, ht.TMaybeString,
861 8eb34306 Apollon Oikonomopoulos
     "Iallocator for deciding the target node for shared-storage instances"),
862 80cb875c Michael Hanselmann
    ]
863 80cb875c Michael Hanselmann
864 80cb875c Michael Hanselmann
865 0ae89533 Iustin Pop
class OpNodeEvacStrategy(OpCode):
866 d6aaa598 Iustin Pop
  """Compute the evacuation strategy for a list of nodes."""
867 d6aaa598 Iustin Pop
  OP_DSC_FIELD = "nodes"
868 65e183af Michael Hanselmann
  OP_PARAMS = [
869 197b323b Michael Hanselmann
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString), None),
870 197b323b Michael Hanselmann
    ("remote_node", None, ht.TMaybeString, None),
871 197b323b Michael Hanselmann
    ("iallocator", None, ht.TMaybeString, None),
872 65e183af Michael Hanselmann
    ]
873 d6aaa598 Iustin Pop
874 d6aaa598 Iustin Pop
875 a8083063 Iustin Pop
# instance opcodes
876 a8083063 Iustin Pop
877 e1530b10 Iustin Pop
class OpInstanceCreate(OpCode):
878 9bf56d77 Michael Hanselmann
  """Create an instance.
879 9bf56d77 Michael Hanselmann

880 9bf56d77 Michael Hanselmann
  @ivar instance_name: Instance name
881 9bf56d77 Michael Hanselmann
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
882 9bf56d77 Michael Hanselmann
  @ivar source_handshake: Signed handshake from source (remote import only)
883 9bf56d77 Michael Hanselmann
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
884 9bf56d77 Michael Hanselmann
  @ivar source_instance_name: Previous name of instance (remote import only)
885 dae91d02 Michael Hanselmann
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
886 dae91d02 Michael Hanselmann
    (remote import only)
887 9bf56d77 Michael Hanselmann

888 9bf56d77 Michael Hanselmann
  """
889 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
890 65e183af Michael Hanselmann
  OP_PARAMS = [
891 65e183af Michael Hanselmann
    _PInstanceName,
892 45d4c81c Michael Hanselmann
    _PForceVariant,
893 45d4c81c Michael Hanselmann
    _PWaitForSync,
894 45d4c81c Michael Hanselmann
    _PNameCheck,
895 45d4c81c Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict, "Backend parameters for instance"),
896 526a662a Michael Hanselmann
    ("disks", ht.NoDefault,
897 526a662a Michael Hanselmann
     # TODO: Generate check from constants.IDISK_PARAMS_TYPES
898 526a662a Michael Hanselmann
     ht.TListOf(ht.TDictOf(ht.TElemOf(constants.IDISK_PARAMS),
899 526a662a Michael Hanselmann
                           ht.TOr(ht.TNonEmptyString, ht.TInt))),
900 526a662a Michael Hanselmann
     "Disk descriptions, for example ``[{\"%s\": 100}, {\"%s\": 5}]``;"
901 526a662a Michael Hanselmann
     " each disk definition must contain a ``%s`` value and"
902 526a662a Michael Hanselmann
     " can contain an optional ``%s`` value denoting the disk access mode"
903 526a662a Michael Hanselmann
     " (%s)" %
904 526a662a Michael Hanselmann
     (constants.IDISK_SIZE, constants.IDISK_SIZE, constants.IDISK_SIZE,
905 526a662a Michael Hanselmann
      constants.IDISK_MODE,
906 526a662a Michael Hanselmann
      " or ".join("``%s``" % i for i in sorted(constants.DISK_ACCESS_SET)))),
907 45d4c81c Michael Hanselmann
    ("disk_template", ht.NoDefault, _CheckDiskTemplate, "Disk template"),
908 45d4c81c Michael Hanselmann
    ("file_driver", None, ht.TOr(ht.TNone, ht.TElemOf(constants.FILE_DRIVER)),
909 45d4c81c Michael Hanselmann
     "Driver for file-backed disks"),
910 45d4c81c Michael Hanselmann
    ("file_storage_dir", None, ht.TMaybeString,
911 45d4c81c Michael Hanselmann
     "Directory for storing file-backed disks"),
912 45d4c81c Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict,
913 45d4c81c Michael Hanselmann
     "Hypervisor parameters for instance, hypervisor-dependent"),
914 45d4c81c Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString, "Hypervisor"),
915 45d4c81c Michael Hanselmann
    ("iallocator", None, ht.TMaybeString,
916 45d4c81c Michael Hanselmann
     "Iallocator for deciding which node(s) to use"),
917 45d4c81c Michael Hanselmann
    ("identify_defaults", False, ht.TBool,
918 45d4c81c Michael Hanselmann
     "Reset instance parameters to default if equal"),
919 45d4c81c Michael Hanselmann
    ("ip_check", True, ht.TBool, _PIpCheckDoc),
920 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.INSTANCE_CREATE_MODES),
921 45d4c81c Michael Hanselmann
     "Instance creation mode"),
922 526a662a Michael Hanselmann
    ("nics", ht.NoDefault, ht.TListOf(_TestNicDef),
923 526a662a Michael Hanselmann
     "List of NIC (network interface) definitions, for example"
924 526a662a Michael Hanselmann
     " ``[{}, {}, {\"%s\": \"198.51.100.4\"}]``; each NIC definition can"
925 526a662a Michael Hanselmann
     " contain the optional values %s" %
926 526a662a Michael Hanselmann
     (constants.INIC_IP,
927 526a662a Michael Hanselmann
      ", ".join("``%s``" % i for i in sorted(constants.INIC_PARAMS)))),
928 45d4c81c Michael Hanselmann
    ("no_install", None, ht.TMaybeBool,
929 45d4c81c Michael Hanselmann
     "Do not install the OS (will disable automatic start)"),
930 45d4c81c Michael Hanselmann
    ("osparams", ht.EmptyDict, ht.TDict, "OS parameters for instance"),
931 45d4c81c Michael Hanselmann
    ("os_type", None, ht.TMaybeString, "Operating system"),
932 45d4c81c Michael Hanselmann
    ("pnode", None, ht.TMaybeString, "Primary node"),
933 45d4c81c Michael Hanselmann
    ("snode", None, ht.TMaybeString, "Secondary node"),
934 45d4c81c Michael Hanselmann
    ("source_handshake", None, ht.TOr(ht.TList, ht.TNone),
935 45d4c81c Michael Hanselmann
     "Signed handshake from source (remote import only)"),
936 45d4c81c Michael Hanselmann
    ("source_instance_name", None, ht.TMaybeString,
937 45d4c81c Michael Hanselmann
     "Source instance name (remote import only)"),
938 65e183af Michael Hanselmann
    ("source_shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
939 526a662a Michael Hanselmann
     ht.TPositiveInt,
940 526a662a Michael Hanselmann
     "How long source instance was given to shut down (remote import only)"),
941 45d4c81c Michael Hanselmann
    ("source_x509_ca", None, ht.TMaybeString,
942 45d4c81c Michael Hanselmann
     "Source X509 CA in PEM format (remote import only)"),
943 45d4c81c Michael Hanselmann
    ("src_node", None, ht.TMaybeString, "Source node for import"),
944 45d4c81c Michael Hanselmann
    ("src_path", None, ht.TMaybeString, "Source directory for import"),
945 45d4c81c Michael Hanselmann
    ("start", True, ht.TBool, "Whether to start instance after creation"),
946 3b6d8c9b Iustin Pop
    ]
947 a8083063 Iustin Pop
948 a8083063 Iustin Pop
949 5073fd8f Iustin Pop
class OpInstanceReinstall(OpCode):
950 fdc267f4 Iustin Pop
  """Reinstall an instance's OS."""
951 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
952 65e183af Michael Hanselmann
  OP_PARAMS = [
953 65e183af Michael Hanselmann
    _PInstanceName,
954 45d4c81c Michael Hanselmann
    _PForceVariant,
955 45d4c81c Michael Hanselmann
    ("os_type", None, ht.TMaybeString, "Instance operating system"),
956 45d4c81c Michael Hanselmann
    ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
957 65e183af Michael Hanselmann
    ]
958 fe7b0351 Michael Hanselmann
959 fe7b0351 Michael Hanselmann
960 3cd2d4b1 Iustin Pop
class OpInstanceRemove(OpCode):
961 a8083063 Iustin Pop
  """Remove an instance."""
962 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
963 65e183af Michael Hanselmann
  OP_PARAMS = [
964 65e183af Michael Hanselmann
    _PInstanceName,
965 65e183af Michael Hanselmann
    _PShutdownTimeout,
966 45d4c81c Michael Hanselmann
    ("ignore_failures", False, ht.TBool,
967 45d4c81c Michael Hanselmann
     "Whether to ignore failures during removal"),
968 fc1baca9 Michael Hanselmann
    ]
969 a8083063 Iustin Pop
970 a8083063 Iustin Pop
971 5659e2e2 Iustin Pop
class OpInstanceRename(OpCode):
972 decd5f45 Iustin Pop
  """Rename an instance."""
973 65e183af Michael Hanselmann
  OP_PARAMS = [
974 65e183af Michael Hanselmann
    _PInstanceName,
975 45d4c81c Michael Hanselmann
    _PNameCheck,
976 45d4c81c Michael Hanselmann
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
977 45d4c81c Michael Hanselmann
    ("ip_check", False, ht.TBool, _PIpCheckDoc),
978 4f05fd3b Iustin Pop
    ]
979 decd5f45 Iustin Pop
980 decd5f45 Iustin Pop
981 c873d91c Iustin Pop
class OpInstanceStartup(OpCode):
982 fdc267f4 Iustin Pop
  """Startup an instance."""
983 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
984 65e183af Michael Hanselmann
  OP_PARAMS = [
985 65e183af Michael Hanselmann
    _PInstanceName,
986 65e183af Michael Hanselmann
    _PForce,
987 65e183af Michael Hanselmann
    _PIgnoreOfflineNodes,
988 45d4c81c Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict,
989 45d4c81c Michael Hanselmann
     "Temporary hypervisor parameters, hypervisor-dependent"),
990 45d4c81c Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
991 4f05fd3b Iustin Pop
    ]
992 a8083063 Iustin Pop
993 a8083063 Iustin Pop
994 ee3e37a7 Iustin Pop
class OpInstanceShutdown(OpCode):
995 fdc267f4 Iustin Pop
  """Shutdown an instance."""
996 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
997 65e183af Michael Hanselmann
  OP_PARAMS = [
998 65e183af Michael Hanselmann
    _PInstanceName,
999 65e183af Michael Hanselmann
    _PIgnoreOfflineNodes,
1000 45d4c81c Michael Hanselmann
    ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TPositiveInt,
1001 45d4c81c Michael Hanselmann
     "How long to wait for instance to shut down"),
1002 b44bd844 Michael Hanselmann
    ]
1003 a8083063 Iustin Pop
1004 a8083063 Iustin Pop
1005 90ab1a95 Iustin Pop
class OpInstanceReboot(OpCode):
1006 bf6929a2 Alexander Schreiber
  """Reboot an instance."""
1007 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1008 65e183af Michael Hanselmann
  OP_PARAMS = [
1009 65e183af Michael Hanselmann
    _PInstanceName,
1010 65e183af Michael Hanselmann
    _PShutdownTimeout,
1011 45d4c81c Michael Hanselmann
    ("ignore_secondaries", False, ht.TBool,
1012 45d4c81c Michael Hanselmann
     "Whether to start the instance even if secondary disks are failing"),
1013 45d4c81c Michael Hanselmann
    ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
1014 45d4c81c Michael Hanselmann
     "How to reboot instance"),
1015 4f05fd3b Iustin Pop
    ]
1016 bf6929a2 Alexander Schreiber
1017 bf6929a2 Alexander Schreiber
1018 668f755d Iustin Pop
class OpInstanceReplaceDisks(OpCode):
1019 fdc267f4 Iustin Pop
  """Replace the disks of an instance."""
1020 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1021 65e183af Michael Hanselmann
  OP_PARAMS = [
1022 65e183af Michael Hanselmann
    _PInstanceName,
1023 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1024 45d4c81c Michael Hanselmann
     "Replacement mode"),
1025 45d4c81c Michael Hanselmann
    ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt),
1026 45d4c81c Michael Hanselmann
     "Disk indexes"),
1027 45d4c81c Michael Hanselmann
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1028 45d4c81c Michael Hanselmann
    ("iallocator", None, ht.TMaybeString,
1029 45d4c81c Michael Hanselmann
     "Iallocator for deciding new secondary node"),
1030 45d4c81c Michael Hanselmann
    ("early_release", False, ht.TBool,
1031 45d4c81c Michael Hanselmann
     "Whether to release locks as soon as possible"),
1032 4f05fd3b Iustin Pop
    ]
1033 a8083063 Iustin Pop
1034 a8083063 Iustin Pop
1035 019dbee1 Iustin Pop
class OpInstanceFailover(OpCode):
1036 a8083063 Iustin Pop
  """Failover an instance."""
1037 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1038 65e183af Michael Hanselmann
  OP_PARAMS = [
1039 65e183af Michael Hanselmann
    _PInstanceName,
1040 65e183af Michael Hanselmann
    _PShutdownTimeout,
1041 45d4c81c Michael Hanselmann
    _PIgnoreConsistency,
1042 8eb34306 Apollon Oikonomopoulos
    ("iallocator", None, ht.TMaybeString,
1043 8eb34306 Apollon Oikonomopoulos
     "Iallocator for deciding the target node for shared-storage instances"),
1044 8eb34306 Apollon Oikonomopoulos
    ("target_node", None, ht.TMaybeString,
1045 8eb34306 Apollon Oikonomopoulos
     "Target node for shared-storage instances"),
1046 17c3f802 Guido Trotter
    ]
1047 a8083063 Iustin Pop
1048 a8083063 Iustin Pop
1049 75c866c2 Iustin Pop
class OpInstanceMigrate(OpCode):
1050 53c776b5 Iustin Pop
  """Migrate an instance.
1051 53c776b5 Iustin Pop

1052 53c776b5 Iustin Pop
  This migrates (without shutting down an instance) to its secondary
1053 53c776b5 Iustin Pop
  node.
1054 53c776b5 Iustin Pop

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

1058 53c776b5 Iustin Pop
  """
1059 ee69c97f Iustin Pop
  OP_DSC_FIELD = "instance_name"
1060 65e183af Michael Hanselmann
  OP_PARAMS = [
1061 65e183af Michael Hanselmann
    _PInstanceName,
1062 65e183af Michael Hanselmann
    _PMigrationMode,
1063 65e183af Michael Hanselmann
    _PMigrationLive,
1064 45d4c81c Michael Hanselmann
    ("cleanup", False, ht.TBool,
1065 45d4c81c Michael Hanselmann
     "Whether a previously failed migration should be cleaned up"),
1066 8eb34306 Apollon Oikonomopoulos
    ("iallocator", None, ht.TMaybeString,
1067 8eb34306 Apollon Oikonomopoulos
     "Iallocator for deciding the target node for shared-storage instances"),
1068 8eb34306 Apollon Oikonomopoulos
    ("target_node", None, ht.TMaybeString,
1069 8eb34306 Apollon Oikonomopoulos
     "Target node for shared-storage instances"),
1070 d5cafd31 Renรฉ Nussbaumer
    ("allow_failover", False, ht.TBool,
1071 d5cafd31 Renรฉ Nussbaumer
     "Whether we can fallback to failover if migration is not possible"),
1072 65e183af Michael Hanselmann
    ]
1073 53c776b5 Iustin Pop
1074 53c776b5 Iustin Pop
1075 0091b480 Iustin Pop
class OpInstanceMove(OpCode):
1076 313bcead Iustin Pop
  """Move an instance.
1077 313bcead Iustin Pop

1078 313bcead Iustin Pop
  This move (with shutting down an instance and data copying) to an
1079 313bcead Iustin Pop
  arbitrary node.
1080 313bcead Iustin Pop

1081 313bcead Iustin Pop
  @ivar instance_name: the name of the instance
1082 313bcead Iustin Pop
  @ivar target_node: the destination node
1083 313bcead Iustin Pop

1084 313bcead Iustin Pop
  """
1085 313bcead Iustin Pop
  OP_DSC_FIELD = "instance_name"
1086 65e183af Michael Hanselmann
  OP_PARAMS = [
1087 65e183af Michael Hanselmann
    _PInstanceName,
1088 65e183af Michael Hanselmann
    _PShutdownTimeout,
1089 45d4c81c Michael Hanselmann
    ("target_node", ht.NoDefault, ht.TNonEmptyString, "Target node"),
1090 154b9580 Balazs Lecz
    ]
1091 313bcead Iustin Pop
1092 313bcead Iustin Pop
1093 cc0dec7b Iustin Pop
class OpInstanceConsole(OpCode):
1094 fdc267f4 Iustin Pop
  """Connect to an instance's console."""
1095 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1096 65e183af Michael Hanselmann
  OP_PARAMS = [
1097 65e183af Michael Hanselmann
    _PInstanceName
1098 65e183af Michael Hanselmann
    ]
1099 a8083063 Iustin Pop
1100 a8083063 Iustin Pop
1101 83f5d475 Iustin Pop
class OpInstanceActivateDisks(OpCode):
1102 fdc267f4 Iustin Pop
  """Activate an instance's disks."""
1103 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1104 65e183af Michael Hanselmann
  OP_PARAMS = [
1105 65e183af Michael Hanselmann
    _PInstanceName,
1106 45d4c81c Michael Hanselmann
    ("ignore_size", False, ht.TBool, "Whether to ignore recorded size"),
1107 65e183af Michael Hanselmann
    ]
1108 a8083063 Iustin Pop
1109 a8083063 Iustin Pop
1110 e176281f Iustin Pop
class OpInstanceDeactivateDisks(OpCode):
1111 fdc267f4 Iustin Pop
  """Deactivate an instance's disks."""
1112 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1113 65e183af Michael Hanselmann
  OP_PARAMS = [
1114 c9c41373 Iustin Pop
    _PInstanceName,
1115 c9c41373 Iustin Pop
    _PForce,
1116 65e183af Michael Hanselmann
    ]
1117 a8083063 Iustin Pop
1118 a8083063 Iustin Pop
1119 6b273e78 Iustin Pop
class OpInstanceRecreateDisks(OpCode):
1120 bd315bfa Iustin Pop
  """Deactivate an instance's disks."""
1121 bd315bfa Iustin Pop
  OP_DSC_FIELD = "instance_name"
1122 65e183af Michael Hanselmann
  OP_PARAMS = [
1123 65e183af Michael Hanselmann
    _PInstanceName,
1124 45d4c81c Michael Hanselmann
    ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt),
1125 45d4c81c Michael Hanselmann
     "List of disk indexes"),
1126 65e183af Michael Hanselmann
    ]
1127 bd315bfa Iustin Pop
1128 bd315bfa Iustin Pop
1129 f2af0bec Iustin Pop
class OpInstanceQuery(OpCode):
1130 a8083063 Iustin Pop
  """Compute the list of instances."""
1131 65e183af Michael Hanselmann
  OP_PARAMS = [
1132 65e183af Michael Hanselmann
    _POutputFields,
1133 45d4c81c Michael Hanselmann
    _PUseLocking,
1134 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1135 45d4c81c Michael Hanselmann
     "Empty list to query all instances, instance names otherwise"),
1136 65e183af Michael Hanselmann
    ]
1137 a8083063 Iustin Pop
1138 a8083063 Iustin Pop
1139 dc28c4e4 Iustin Pop
class OpInstanceQueryData(OpCode):
1140 a8083063 Iustin Pop
  """Compute the run-time status of instances."""
1141 65e183af Michael Hanselmann
  OP_PARAMS = [
1142 af7b6689 Michael Hanselmann
    _PUseLocking,
1143 af7b6689 Michael Hanselmann
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1144 af7b6689 Michael Hanselmann
     "Instance names"),
1145 af7b6689 Michael Hanselmann
    ("static", False, ht.TBool,
1146 af7b6689 Michael Hanselmann
     "Whether to only return configuration data without querying"
1147 af7b6689 Michael Hanselmann
     " nodes"),
1148 65e183af Michael Hanselmann
    ]
1149 a8083063 Iustin Pop
1150 a8083063 Iustin Pop
1151 9a3cc7ae Iustin Pop
class OpInstanceSetParams(OpCode):
1152 a8083063 Iustin Pop
  """Change the parameters of an instance."""
1153 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1154 65e183af Michael Hanselmann
  OP_PARAMS = [
1155 65e183af Michael Hanselmann
    _PInstanceName,
1156 65e183af Michael Hanselmann
    _PForce,
1157 45d4c81c Michael Hanselmann
    _PForceVariant,
1158 526a662a Michael Hanselmann
    # TODO: Use _TestNicDef
1159 45d4c81c Michael Hanselmann
    ("nics", ht.EmptyList, ht.TList,
1160 45d4c81c Michael Hanselmann
     "List of NIC changes. Each item is of the form ``(op, settings)``."
1161 45d4c81c Michael Hanselmann
     " ``op`` can be ``%s`` to add a new NIC with the specified settings,"
1162 45d4c81c Michael Hanselmann
     " ``%s`` to remove the last NIC or a number to modify the settings"
1163 45d4c81c Michael Hanselmann
     " of the NIC with that index." %
1164 45d4c81c Michael Hanselmann
     (constants.DDM_ADD, constants.DDM_REMOVE)),
1165 45d4c81c Michael Hanselmann
    ("disks", ht.EmptyList, ht.TList, "List of disk changes. See ``nics``."),
1166 45d4c81c Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict, "Per-instance backend parameters"),
1167 45d4c81c Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict,
1168 45d4c81c Michael Hanselmann
     "Per-instance hypervisor parameters, hypervisor-dependent"),
1169 45d4c81c Michael Hanselmann
    ("disk_template", None, ht.TOr(ht.TNone, _CheckDiskTemplate),
1170 45d4c81c Michael Hanselmann
     "Disk template for instance"),
1171 45d4c81c Michael Hanselmann
    ("remote_node", None, ht.TMaybeString,
1172 45d4c81c Michael Hanselmann
     "Secondary node (used when changing disk template)"),
1173 45d4c81c Michael Hanselmann
    ("os_name", None, ht.TMaybeString,
1174 45d4c81c Michael Hanselmann
     "Change instance's OS name. Does not reinstall the instance."),
1175 45d4c81c Michael Hanselmann
    ("osparams", None, ht.TMaybeDict, "Per-instance OS parameters"),
1176 973d7867 Iustin Pop
    ]
1177 a8083063 Iustin Pop
1178 a8083063 Iustin Pop
1179 60472d29 Iustin Pop
class OpInstanceGrowDisk(OpCode):
1180 8729e0d7 Iustin Pop
  """Grow a disk of an instance."""
1181 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1182 65e183af Michael Hanselmann
  OP_PARAMS = [
1183 65e183af Michael Hanselmann
    _PInstanceName,
1184 45d4c81c Michael Hanselmann
    _PWaitForSync,
1185 45d4c81c Michael Hanselmann
    ("disk", ht.NoDefault, ht.TInt, "Disk index"),
1186 45d4c81c Michael Hanselmann
    ("amount", ht.NoDefault, ht.TInt,
1187 45d4c81c Michael Hanselmann
     "Amount of disk space to add (megabytes)"),
1188 4f05fd3b Iustin Pop
    ]
1189 8729e0d7 Iustin Pop
1190 8729e0d7 Iustin Pop
1191 70a6a926 Adeodato Simo
# Node group opcodes
1192 70a6a926 Adeodato Simo
1193 fabf1731 Iustin Pop
class OpGroupAdd(OpCode):
1194 b1ee5610 Adeodato Simo
  """Add a node group to the cluster."""
1195 b1ee5610 Adeodato Simo
  OP_DSC_FIELD = "group_name"
1196 65e183af Michael Hanselmann
  OP_PARAMS = [
1197 65e183af Michael Hanselmann
    _PGroupName,
1198 45d4c81c Michael Hanselmann
    _PNodeGroupAllocPolicy,
1199 45d4c81c Michael Hanselmann
    _PGroupNodeParams,
1200 483be60d Adeodato Simo
    ]
1201 b1ee5610 Adeodato Simo
1202 b1ee5610 Adeodato Simo
1203 934704ae Iustin Pop
class OpGroupAssignNodes(OpCode):
1204 96276ae7 Adeodato Simo
  """Assign nodes to a node group."""
1205 96276ae7 Adeodato Simo
  OP_DSC_FIELD = "group_name"
1206 96276ae7 Adeodato Simo
  OP_PARAMS = [
1207 96276ae7 Adeodato Simo
    _PGroupName,
1208 96276ae7 Adeodato Simo
    _PForce,
1209 45d4c81c Michael Hanselmann
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1210 45d4c81c Michael Hanselmann
     "List of nodes to assign"),
1211 96276ae7 Adeodato Simo
    ]
1212 96276ae7 Adeodato Simo
1213 96276ae7 Adeodato Simo
1214 d4d654bd Iustin Pop
class OpGroupQuery(OpCode):
1215 70a6a926 Adeodato Simo
  """Compute the list of node groups."""
1216 65e183af Michael Hanselmann
  OP_PARAMS = [
1217 65e183af Michael Hanselmann
    _POutputFields,
1218 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1219 45d4c81c Michael Hanselmann
     "Empty list to query all groups, group names otherwise"),
1220 65e183af Michael Hanselmann
    ]
1221 70a6a926 Adeodato Simo
1222 70a6a926 Adeodato Simo
1223 7cbf74f0 Iustin Pop
class OpGroupSetParams(OpCode):
1224 4da7909a Adeodato Simo
  """Change the parameters of a node group."""
1225 4da7909a Adeodato Simo
  OP_DSC_FIELD = "group_name"
1226 65e183af Michael Hanselmann
  OP_PARAMS = [
1227 65e183af Michael Hanselmann
    _PGroupName,
1228 45d4c81c Michael Hanselmann
    _PNodeGroupAllocPolicy,
1229 45d4c81c Michael Hanselmann
    _PGroupNodeParams,
1230 4da7909a Adeodato Simo
    ]
1231 4da7909a Adeodato Simo
1232 4da7909a Adeodato Simo
1233 4d1baa51 Iustin Pop
class OpGroupRemove(OpCode):
1234 94bd652a Adeodato Simo
  """Remove a node group from the cluster."""
1235 94bd652a Adeodato Simo
  OP_DSC_FIELD = "group_name"
1236 65e183af Michael Hanselmann
  OP_PARAMS = [
1237 65e183af Michael Hanselmann
    _PGroupName,
1238 65e183af Michael Hanselmann
    ]
1239 94bd652a Adeodato Simo
1240 94bd652a Adeodato Simo
1241 a8173e82 Iustin Pop
class OpGroupRename(OpCode):
1242 4fe5cf90 Adeodato Simo
  """Rename a node group in the cluster."""
1243 65e183af Michael Hanselmann
  OP_PARAMS = [
1244 12da663a Michael Hanselmann
    _PGroupName,
1245 12da663a Michael Hanselmann
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New group name"),
1246 65e183af Michael Hanselmann
    ]
1247 4fe5cf90 Adeodato Simo
1248 4fe5cf90 Adeodato Simo
1249 a8083063 Iustin Pop
# OS opcodes
1250 da2d02e7 Iustin Pop
class OpOsDiagnose(OpCode):
1251 a8083063 Iustin Pop
  """Compute the list of guest operating systems."""
1252 65e183af Michael Hanselmann
  OP_PARAMS = [
1253 65e183af Michael Hanselmann
    _POutputFields,
1254 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1255 45d4c81c Michael Hanselmann
     "Which operating systems to diagnose"),
1256 65e183af Michael Hanselmann
    ]
1257 a8083063 Iustin Pop
1258 7c0d6283 Michael Hanselmann
1259 a8083063 Iustin Pop
# Exports opcodes
1260 7ca2d4d8 Iustin Pop
class OpBackupQuery(OpCode):
1261 a8083063 Iustin Pop
  """Compute the list of exported images."""
1262 65e183af Michael Hanselmann
  OP_PARAMS = [
1263 45d4c81c Michael Hanselmann
    _PUseLocking,
1264 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1265 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
1266 65e183af Michael Hanselmann
    ]
1267 a8083063 Iustin Pop
1268 7c0d6283 Michael Hanselmann
1269 71910715 Iustin Pop
class OpBackupPrepare(OpCode):
1270 1410fa8d Michael Hanselmann
  """Prepares an instance export.
1271 1410fa8d Michael Hanselmann

1272 1410fa8d Michael Hanselmann
  @ivar instance_name: Instance name
1273 1410fa8d Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1274 1410fa8d Michael Hanselmann

1275 1410fa8d Michael Hanselmann
  """
1276 1410fa8d Michael Hanselmann
  OP_DSC_FIELD = "instance_name"
1277 65e183af Michael Hanselmann
  OP_PARAMS = [
1278 65e183af Michael Hanselmann
    _PInstanceName,
1279 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1280 45d4c81c Michael Hanselmann
     "Export mode"),
1281 1410fa8d Michael Hanselmann
    ]
1282 1410fa8d Michael Hanselmann
1283 1410fa8d Michael Hanselmann
1284 4ff922a2 Iustin Pop
class OpBackupExport(OpCode):
1285 4a96f1d1 Michael Hanselmann
  """Export an instance.
1286 4a96f1d1 Michael Hanselmann

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

1293 4a96f1d1 Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1294 4a96f1d1 Michael Hanselmann
  @ivar target_node: Export destination
1295 4a96f1d1 Michael Hanselmann
  @ivar x509_key_name: X509 key to use (remote export only)
1296 4a96f1d1 Michael Hanselmann
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1297 4a96f1d1 Michael Hanselmann
                             only)
1298 4a96f1d1 Michael Hanselmann

1299 4a96f1d1 Michael Hanselmann
  """
1300 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1301 65e183af Michael Hanselmann
  OP_PARAMS = [
1302 65e183af Michael Hanselmann
    _PInstanceName,
1303 65e183af Michael Hanselmann
    _PShutdownTimeout,
1304 4a96f1d1 Michael Hanselmann
    # TODO: Rename target_node as it changes meaning for different export modes
1305 4a96f1d1 Michael Hanselmann
    # (e.g. "destination")
1306 45d4c81c Michael Hanselmann
    ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1307 45d4c81c Michael Hanselmann
     "Destination information, depends on export mode"),
1308 45d4c81c Michael Hanselmann
    ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1309 45d4c81c Michael Hanselmann
    ("remove_instance", False, ht.TBool,
1310 45d4c81c Michael Hanselmann
     "Whether to remove instance after export"),
1311 45d4c81c Michael Hanselmann
    ("ignore_remove_failures", False, ht.TBool,
1312 45d4c81c Michael Hanselmann
     "Whether to ignore failures while removing instances"),
1313 45d4c81c Michael Hanselmann
    ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1314 45d4c81c Michael Hanselmann
     "Export mode"),
1315 45d4c81c Michael Hanselmann
    ("x509_key_name", None, ht.TOr(ht.TList, ht.TNone),
1316 45d4c81c Michael Hanselmann
     "Name of X509 key (remote export only)"),
1317 45d4c81c Michael Hanselmann
    ("destination_x509_ca", None, ht.TMaybeString,
1318 45d4c81c Michael Hanselmann
     "Destination X509 CA (remote export only)"),
1319 17c3f802 Guido Trotter
    ]
1320 5c947f38 Iustin Pop
1321 0a7bed64 Michael Hanselmann
1322 ca5890ad Iustin Pop
class OpBackupRemove(OpCode):
1323 9ac99fda Guido Trotter
  """Remove an instance's export."""
1324 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1325 65e183af Michael Hanselmann
  OP_PARAMS = [
1326 65e183af Michael Hanselmann
    _PInstanceName,
1327 65e183af Michael Hanselmann
    ]
1328 5c947f38 Iustin Pop
1329 0a7bed64 Michael Hanselmann
1330 5c947f38 Iustin Pop
# Tags opcodes
1331 c6afb1ca Iustin Pop
class OpTagsGet(OpCode):
1332 5c947f38 Iustin Pop
  """Returns the tags of the given object."""
1333 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
1334 65e183af Michael Hanselmann
  OP_PARAMS = [
1335 65e183af Michael Hanselmann
    _PTagKind,
1336 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1337 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString, None),
1338 65e183af Michael Hanselmann
    ]
1339 5c947f38 Iustin Pop
1340 5c947f38 Iustin Pop
1341 715462e7 Iustin Pop
class OpTagsSearch(OpCode):
1342 73415719 Iustin Pop
  """Searches the tags in the cluster for a given pattern."""
1343 60dd1473 Iustin Pop
  OP_DSC_FIELD = "pattern"
1344 65e183af Michael Hanselmann
  OP_PARAMS = [
1345 197b323b Michael Hanselmann
    ("pattern", ht.NoDefault, ht.TNonEmptyString, None),
1346 65e183af Michael Hanselmann
    ]
1347 73415719 Iustin Pop
1348 73415719 Iustin Pop
1349 d1602edc Iustin Pop
class OpTagsSet(OpCode):
1350 f27302fa Iustin Pop
  """Add a list of tags on a given object."""
1351 65e183af Michael Hanselmann
  OP_PARAMS = [
1352 65e183af Michael Hanselmann
    _PTagKind,
1353 65e183af Michael Hanselmann
    _PTags,
1354 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1355 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString, None),
1356 65e183af Michael Hanselmann
    ]
1357 5c947f38 Iustin Pop
1358 5c947f38 Iustin Pop
1359 3f0ab95f Iustin Pop
class OpTagsDel(OpCode):
1360 f27302fa Iustin Pop
  """Remove a list of tags from a given object."""
1361 65e183af Michael Hanselmann
  OP_PARAMS = [
1362 65e183af Michael Hanselmann
    _PTagKind,
1363 65e183af Michael Hanselmann
    _PTags,
1364 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1365 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString, None),
1366 65e183af Michael Hanselmann
    ]
1367 06009e27 Iustin Pop
1368 06009e27 Iustin Pop
# Test opcodes
1369 06009e27 Iustin Pop
class OpTestDelay(OpCode):
1370 06009e27 Iustin Pop
  """Sleeps for a configured amount of time.
1371 06009e27 Iustin Pop

1372 06009e27 Iustin Pop
  This is used just for debugging and testing.
1373 06009e27 Iustin Pop

1374 06009e27 Iustin Pop
  Parameters:
1375 06009e27 Iustin Pop
    - duration: the time to sleep
1376 06009e27 Iustin Pop
    - on_master: if true, sleep on the master
1377 06009e27 Iustin Pop
    - on_nodes: list of nodes in which to sleep
1378 06009e27 Iustin Pop

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

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

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

1389 06009e27 Iustin Pop
  """
1390 60dd1473 Iustin Pop
  OP_DSC_FIELD = "duration"
1391 65e183af Michael Hanselmann
  OP_PARAMS = [
1392 197b323b Michael Hanselmann
    ("duration", ht.NoDefault, ht.TFloat, None),
1393 197b323b Michael Hanselmann
    ("on_master", True, ht.TBool, None),
1394 197b323b Michael Hanselmann
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1395 197b323b Michael Hanselmann
    ("repeat", 0, ht.TPositiveInt, None),
1396 65e183af Michael Hanselmann
    ]
1397 d61df03e Iustin Pop
1398 d61df03e Iustin Pop
1399 d61df03e Iustin Pop
class OpTestAllocator(OpCode):
1400 d61df03e Iustin Pop
  """Allocator framework testing.
1401 d61df03e Iustin Pop

1402 d61df03e Iustin Pop
  This opcode has two modes:
1403 d61df03e Iustin Pop
    - gather and return allocator input for a given mode (allocate new
1404 d61df03e Iustin Pop
      or replace secondary) and a given instance definition (direction
1405 d61df03e Iustin Pop
      'in')
1406 d61df03e Iustin Pop
    - run a selected allocator for a given operation (as above) and
1407 d61df03e Iustin Pop
      return the allocator output (direction 'out')
1408 d61df03e Iustin Pop

1409 d61df03e Iustin Pop
  """
1410 60dd1473 Iustin Pop
  OP_DSC_FIELD = "allocator"
1411 65e183af Michael Hanselmann
  OP_PARAMS = [
1412 65e183af Michael Hanselmann
    ("direction", ht.NoDefault,
1413 197b323b Michael Hanselmann
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
1414 197b323b Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
1415 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
1416 65e183af Michael Hanselmann
    ("nics", ht.NoDefault, ht.TOr(ht.TNone, ht.TListOf(
1417 45d4c81c Michael Hanselmann
     ht.TDictOf(ht.TElemOf(["mac", "ip", "bridge"]),
1418 45d4c81c Michael Hanselmann
                ht.TOr(ht.TNone, ht.TNonEmptyString)))), None),
1419 197b323b Michael Hanselmann
    ("disks", ht.NoDefault, ht.TOr(ht.TNone, ht.TList), None),
1420 197b323b Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString, None),
1421 197b323b Michael Hanselmann
    ("allocator", None, ht.TMaybeString, None),
1422 197b323b Michael Hanselmann
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1423 197b323b Michael Hanselmann
    ("mem_size", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1424 197b323b Michael Hanselmann
    ("vcpus", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1425 197b323b Michael Hanselmann
    ("os", None, ht.TMaybeString, None),
1426 197b323b Michael Hanselmann
    ("disk_template", None, ht.TMaybeString, None),
1427 45d4c81c Michael Hanselmann
    ("evac_nodes", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1428 45d4c81c Michael Hanselmann
     None),
1429 d61df03e Iustin Pop
    ]
1430 363acb1e Iustin Pop
1431 76aef8fc Michael Hanselmann
1432 b469eb4d Iustin Pop
class OpTestJqueue(OpCode):
1433 e58f87a9 Michael Hanselmann
  """Utility opcode to test some aspects of the job queue.
1434 e58f87a9 Michael Hanselmann

1435 e58f87a9 Michael Hanselmann
  """
1436 65e183af Michael Hanselmann
  OP_PARAMS = [
1437 197b323b Michael Hanselmann
    ("notify_waitlock", False, ht.TBool, None),
1438 197b323b Michael Hanselmann
    ("notify_exec", False, ht.TBool, None),
1439 197b323b Michael Hanselmann
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
1440 197b323b Michael Hanselmann
    ("fail", False, ht.TBool, None),
1441 e58f87a9 Michael Hanselmann
    ]
1442 e58f87a9 Michael Hanselmann
1443 e58f87a9 Michael Hanselmann
1444 be760ba8 Michael Hanselmann
class OpTestDummy(OpCode):
1445 be760ba8 Michael Hanselmann
  """Utility opcode used by unittests.
1446 be760ba8 Michael Hanselmann

1447 be760ba8 Michael Hanselmann
  """
1448 65e183af Michael Hanselmann
  OP_PARAMS = [
1449 197b323b Michael Hanselmann
    ("result", ht.NoDefault, ht.NoType, None),
1450 197b323b Michael Hanselmann
    ("messages", ht.NoDefault, ht.NoType, None),
1451 197b323b Michael Hanselmann
    ("fail", ht.NoDefault, ht.NoType, None),
1452 6a373640 Michael Hanselmann
    ("submit_jobs", None, ht.NoType, None),
1453 be760ba8 Michael Hanselmann
    ]
1454 687c10d9 Iustin Pop
  WITH_LU = False
1455 be760ba8 Michael Hanselmann
1456 be760ba8 Michael Hanselmann
1457 dbc96028 Michael Hanselmann
def _GetOpList():
1458 dbc96028 Michael Hanselmann
  """Returns list of all defined opcodes.
1459 dbc96028 Michael Hanselmann

1460 dbc96028 Michael Hanselmann
  Does not eliminate duplicates by C{OP_ID}.
1461 dbc96028 Michael Hanselmann

1462 dbc96028 Michael Hanselmann
  """
1463 dbc96028 Michael Hanselmann
  return [v for v in globals().values()
1464 dbc96028 Michael Hanselmann
          if (isinstance(v, type) and issubclass(v, OpCode) and
1465 687c10d9 Iustin Pop
              hasattr(v, "OP_ID") and v is not OpCode)]
1466 dbc96028 Michael Hanselmann
1467 dbc96028 Michael Hanselmann
1468 dbc96028 Michael Hanselmann
OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())