Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ c711d09e

History | View | Annotate | Download (45.8 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 9b64e486 Iustin Pop
#: Do not remember instance state changes
118 6aac5aef Iustin Pop
_PNoRemember = ("no_remember", False, ht.TBool,
119 6aac5aef Iustin Pop
                "Do not remember the state change")
120 9b64e486 Iustin Pop
121 ff0d18e6 Iustin Pop
#: OP_ID conversion regular expression
122 ff0d18e6 Iustin Pop
_OPID_RE = re.compile("([a-z])([A-Z])")
123 ff0d18e6 Iustin Pop
124 8c9ee749 Michael Hanselmann
#: Utility function for L{OpClusterSetParams}
125 8c9ee749 Michael Hanselmann
_TestClusterOsList = ht.TOr(ht.TNone,
126 8c9ee749 Michael Hanselmann
  ht.TListOf(ht.TAnd(ht.TList, ht.TIsLength(2),
127 8c9ee749 Michael Hanselmann
    ht.TMap(ht.WithDesc("GetFirstItem")(operator.itemgetter(0)),
128 8c9ee749 Michael Hanselmann
            ht.TElemOf(constants.DDMS_VALUES)))))
129 8c9ee749 Michael Hanselmann
130 ff0d18e6 Iustin Pop
131 526a662a Michael Hanselmann
# TODO: Generate check from constants.INIC_PARAMS_TYPES
132 526a662a Michael Hanselmann
#: Utility function for testing NIC definitions
133 526a662a Michael Hanselmann
_TestNicDef = ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
134 526a662a Michael Hanselmann
                         ht.TOr(ht.TNone, ht.TNonEmptyString))
135 526a662a Michael Hanselmann
136 3ce9a5e7 Michael Hanselmann
_SUMMARY_PREFIX = {
137 3ce9a5e7 Michael Hanselmann
  "CLUSTER_": "C_",
138 3ce9a5e7 Michael Hanselmann
  "GROUP_": "G_",
139 3ce9a5e7 Michael Hanselmann
  "NODE_": "N_",
140 3ce9a5e7 Michael Hanselmann
  "INSTANCE_": "I_",
141 3ce9a5e7 Michael Hanselmann
  }
142 3ce9a5e7 Michael Hanselmann
143 526a662a Michael Hanselmann
144 ff0d18e6 Iustin Pop
def _NameToId(name):
145 ff0d18e6 Iustin Pop
  """Convert an opcode class name to an OP_ID.
146 ff0d18e6 Iustin Pop

147 ff0d18e6 Iustin Pop
  @type name: string
148 ff0d18e6 Iustin Pop
  @param name: the class name, as OpXxxYyy
149 ff0d18e6 Iustin Pop
  @rtype: string
150 ff0d18e6 Iustin Pop
  @return: the name in the OP_XXXX_YYYY format
151 ff0d18e6 Iustin Pop

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

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

171 65e183af Michael Hanselmann
  @raise errors.OpPrereqError: when file storage is disabled
172 65e183af Michael Hanselmann

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

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

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

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

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

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

229 65e183af Michael Hanselmann
  """
230 65e183af Michael Hanselmann
  def __new__(mcs, name, bases, attrs):
231 65e183af Michael Hanselmann
    """Called when a class should be created.
232 65e183af Michael Hanselmann

233 65e183af Michael Hanselmann
    @param mcs: The meta class
234 65e183af Michael Hanselmann
    @param name: Name of created class
235 65e183af Michael Hanselmann
    @param bases: Base classes
236 65e183af Michael Hanselmann
    @type attrs: dict
237 65e183af Michael Hanselmann
    @param attrs: Class attributes
238 65e183af Michael Hanselmann

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

263 0e46916d Iustin Pop
  This object serves as a parent class for OpCode without any custom
264 0e46916d Iustin Pop
  field handling.
265 0e46916d Iustin Pop

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

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

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

290 a7399f66 Iustin Pop
    This method just returns the contents of the instance as a
291 a7399f66 Iustin Pop
    dictionary.
292 a7399f66 Iustin Pop

293 a7399f66 Iustin Pop
    @rtype:  C{dict}
294 a7399f66 Iustin Pop
    @return: the instance attributes and their values
295 a7399f66 Iustin Pop

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

306 a7399f66 Iustin Pop
    This method just restores from the serialized state the attributes
307 a7399f66 Iustin Pop
    of the current instance.
308 a7399f66 Iustin Pop

309 a7399f66 Iustin Pop
    @param state: the serialized opcode data
310 a7399f66 Iustin Pop
    @type state:  C{dict}
311 a7399f66 Iustin Pop

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

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

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

347 1cbef6d8 Michael Hanselmann
    @type set_defaults: bool
348 1cbef6d8 Michael Hanselmann
    @param set_defaults: Whether to set default values
349 1cbef6d8 Michael Hanselmann
    @raise errors.OpPrereqError: When a parameter value doesn't match
350 1cbef6d8 Michael Hanselmann
                                 requirements
351 1cbef6d8 Michael Hanselmann

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

385 a7399f66 Iustin Pop
  This is the root of the actual OpCode hierarchy. All clases derived
386 a7399f66 Iustin Pop
  from this class should override OP_ID.
387 a7399f66 Iustin Pop

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

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

415 a7399f66 Iustin Pop
    This method adds to the state dictionary the OP_ID of the class,
416 a7399f66 Iustin Pop
    so that on unload we can identify the correct class for
417 a7399f66 Iustin Pop
    instantiating the opcode.
418 a7399f66 Iustin Pop

419 a7399f66 Iustin Pop
    @rtype:   C{dict}
420 a7399f66 Iustin Pop
    @return:  the state as a dictionary
421 a7399f66 Iustin Pop

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

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

435 a7399f66 Iustin Pop
    @type data:  C{dict}
436 a7399f66 Iustin Pop
    @param data: the serialized opcode
437 a7399f66 Iustin Pop

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

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

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

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

497 b5f5fae9 Luca Bigliardi
  This opcode does not touch the cluster at all. Its purpose is to run hooks
498 b5f5fae9 Luca Bigliardi
  after the cluster has been initialized.
499 b5f5fae9 Luca Bigliardi

500 b5f5fae9 Luca Bigliardi
  """
501 b5f5fae9 Luca Bigliardi
502 b5f5fae9 Luca Bigliardi
503 c6d43e9e Iustin Pop
class OpClusterDestroy(OpCode):
504 a7399f66 Iustin Pop
  """Destroy the cluster.
505 a7399f66 Iustin Pop

506 a7399f66 Iustin Pop
  This opcode has no other parameters. All the state is irreversibly
507 a7399f66 Iustin Pop
  lost after the execution of this opcode.
508 a7399f66 Iustin Pop

509 a7399f66 Iustin Pop
  """
510 a8083063 Iustin Pop
511 a8083063 Iustin Pop
512 a2f7ab92 Iustin Pop
class OpClusterQuery(OpCode):
513 fdc267f4 Iustin Pop
  """Query cluster information."""
514 a8083063 Iustin Pop
515 a8083063 Iustin Pop
516 a3d32770 Iustin Pop
class OpClusterVerify(OpCode):
517 a7399f66 Iustin Pop
  """Verify the cluster state.
518 a7399f66 Iustin Pop

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

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

538 150e978f Iustin Pop
  Parameters: none
539 150e978f Iustin Pop

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

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

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

555 150e978f Iustin Pop
  """
556 150e978f Iustin Pop
557 150e978f Iustin Pop
558 5d01aca3 Iustin Pop
class OpClusterRepairDiskSizes(OpCode):
559 60975797 Iustin Pop
  """Verify the disk sizes of the instances and fixes configuration
560 60975797 Iustin Pop
  mimatches.
561 60975797 Iustin Pop

562 60975797 Iustin Pop
  Parameters: optional instances list, in case we want to restrict the
563 60975797 Iustin Pop
  checks to only a subset of the instances.
564 60975797 Iustin Pop

565 60975797 Iustin Pop
  Result: a list of tuples, (instance, disk, new-size) for changed
566 60975797 Iustin Pop
  configurations.
567 60975797 Iustin Pop

568 60975797 Iustin Pop
  In normal operation, the list should be empty.
569 60975797 Iustin Pop

570 60975797 Iustin Pop
  @type instances: list
571 60975797 Iustin Pop
  @ivar instances: the list of instances to check, or empty for all instances
572 60975797 Iustin Pop

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

589 a7399f66 Iustin Pop
  @type name: C{str}
590 a7399f66 Iustin Pop
  @ivar name: The new name of the cluster. The name and/or the master IP
591 a7399f66 Iustin Pop
              address will be changed to match the new name and its IP
592 a7399f66 Iustin Pop
              address.
593 a7399f66 Iustin Pop

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

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

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

663 afee0879 Iustin Pop
  """
664 afee0879 Iustin Pop
665 83f72637 Michael Hanselmann
666 83f72637 Michael Hanselmann
class OpQuery(OpCode):
667 83f72637 Michael Hanselmann
  """Query for resources/items.
668 83f72637 Michael Hanselmann

669 abd66bf8 Michael Hanselmann
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
670 83f72637 Michael Hanselmann
  @ivar fields: List of fields to retrieve
671 83f72637 Michael Hanselmann
  @ivar filter: Query filter
672 83f72637 Michael Hanselmann

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

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

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

718 a7399f66 Iustin Pop
  @type node_name: C{str}
719 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to remove. If the node still has
720 a7399f66 Iustin Pop
                   instances on it, the operation will fail.
721 a7399f66 Iustin Pop

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

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

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

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

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

1058 53c776b5 Iustin Pop
  This migrates (without shutting down an instance) to its secondary
1059 53c776b5 Iustin Pop
  node.
1060 53c776b5 Iustin Pop

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

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

1084 313bcead Iustin Pop
  This move (with shutting down an instance and data copying) to an
1085 313bcead Iustin Pop
  arbitrary node.
1086 313bcead Iustin Pop

1087 313bcead Iustin Pop
  @ivar instance_name: the name of the instance
1088 313bcead Iustin Pop
  @ivar target_node: the destination node
1089 313bcead Iustin Pop

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

1283 1410fa8d Michael Hanselmann
  @ivar instance_name: Instance name
1284 1410fa8d Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1285 1410fa8d Michael Hanselmann

1286 1410fa8d Michael Hanselmann
  """
1287 1410fa8d Michael Hanselmann
  OP_DSC_FIELD = "instance_name"
1288 65e183af Michael Hanselmann
  OP_PARAMS = [
1289 65e183af Michael Hanselmann
    _PInstanceName,
1290 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1291 45d4c81c Michael Hanselmann
     "Export mode"),
1292 1410fa8d Michael Hanselmann
    ]
1293 1410fa8d Michael Hanselmann
1294 1410fa8d Michael Hanselmann
1295 4ff922a2 Iustin Pop
class OpBackupExport(OpCode):
1296 4a96f1d1 Michael Hanselmann
  """Export an instance.
1297 4a96f1d1 Michael Hanselmann

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

1304 4a96f1d1 Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1305 4a96f1d1 Michael Hanselmann
  @ivar target_node: Export destination
1306 4a96f1d1 Michael Hanselmann
  @ivar x509_key_name: X509 key to use (remote export only)
1307 4a96f1d1 Michael Hanselmann
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1308 4a96f1d1 Michael Hanselmann
                             only)
1309 4a96f1d1 Michael Hanselmann

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

1383 06009e27 Iustin Pop
  This is used just for debugging and testing.
1384 06009e27 Iustin Pop

1385 06009e27 Iustin Pop
  Parameters:
1386 06009e27 Iustin Pop
    - duration: the time to sleep
1387 06009e27 Iustin Pop
    - on_master: if true, sleep on the master
1388 06009e27 Iustin Pop
    - on_nodes: list of nodes in which to sleep
1389 06009e27 Iustin Pop

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

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

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

1400 06009e27 Iustin Pop
  """
1401 60dd1473 Iustin Pop
  OP_DSC_FIELD = "duration"
1402 65e183af Michael Hanselmann
  OP_PARAMS = [
1403 197b323b Michael Hanselmann
    ("duration", ht.NoDefault, ht.TFloat, None),
1404 197b323b Michael Hanselmann
    ("on_master", True, ht.TBool, None),
1405 197b323b Michael Hanselmann
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1406 197b323b Michael Hanselmann
    ("repeat", 0, ht.TPositiveInt, None),
1407 65e183af Michael Hanselmann
    ]
1408 d61df03e Iustin Pop
1409 d61df03e Iustin Pop
1410 d61df03e Iustin Pop
class OpTestAllocator(OpCode):
1411 d61df03e Iustin Pop
  """Allocator framework testing.
1412 d61df03e Iustin Pop

1413 d61df03e Iustin Pop
  This opcode has two modes:
1414 d61df03e Iustin Pop
    - gather and return allocator input for a given mode (allocate new
1415 d61df03e Iustin Pop
      or replace secondary) and a given instance definition (direction
1416 d61df03e Iustin Pop
      'in')
1417 d61df03e Iustin Pop
    - run a selected allocator for a given operation (as above) and
1418 d61df03e Iustin Pop
      return the allocator output (direction 'out')
1419 d61df03e Iustin Pop

1420 d61df03e Iustin Pop
  """
1421 60dd1473 Iustin Pop
  OP_DSC_FIELD = "allocator"
1422 65e183af Michael Hanselmann
  OP_PARAMS = [
1423 65e183af Michael Hanselmann
    ("direction", ht.NoDefault,
1424 197b323b Michael Hanselmann
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
1425 197b323b Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
1426 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
1427 65e183af Michael Hanselmann
    ("nics", ht.NoDefault, ht.TOr(ht.TNone, ht.TListOf(
1428 fdbe29ee Michael Hanselmann
     ht.TDictOf(ht.TElemOf([constants.INIC_MAC, constants.INIC_IP, "bridge"]),
1429 45d4c81c Michael Hanselmann
                ht.TOr(ht.TNone, ht.TNonEmptyString)))), None),
1430 197b323b Michael Hanselmann
    ("disks", ht.NoDefault, ht.TOr(ht.TNone, ht.TList), None),
1431 197b323b Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString, None),
1432 197b323b Michael Hanselmann
    ("allocator", None, ht.TMaybeString, None),
1433 197b323b Michael Hanselmann
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1434 197b323b Michael Hanselmann
    ("mem_size", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1435 197b323b Michael Hanselmann
    ("vcpus", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1436 197b323b Michael Hanselmann
    ("os", None, ht.TMaybeString, None),
1437 197b323b Michael Hanselmann
    ("disk_template", None, ht.TMaybeString, None),
1438 45d4c81c Michael Hanselmann
    ("evac_nodes", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1439 45d4c81c Michael Hanselmann
     None),
1440 bee581e2 Michael Hanselmann
    ("instances", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1441 bee581e2 Michael Hanselmann
     None),
1442 bee581e2 Michael Hanselmann
    ("reloc_mode", None,
1443 bee581e2 Michael Hanselmann
     ht.TOr(ht.TNone, ht.TElemOf(constants.IALLOCATOR_MRELOC_MODES)), None),
1444 bee581e2 Michael Hanselmann
    ("target_groups", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1445 bee581e2 Michael Hanselmann
     None),
1446 d61df03e Iustin Pop
    ]
1447 363acb1e Iustin Pop
1448 76aef8fc Michael Hanselmann
1449 b469eb4d Iustin Pop
class OpTestJqueue(OpCode):
1450 e58f87a9 Michael Hanselmann
  """Utility opcode to test some aspects of the job queue.
1451 e58f87a9 Michael Hanselmann

1452 e58f87a9 Michael Hanselmann
  """
1453 65e183af Michael Hanselmann
  OP_PARAMS = [
1454 197b323b Michael Hanselmann
    ("notify_waitlock", False, ht.TBool, None),
1455 197b323b Michael Hanselmann
    ("notify_exec", False, ht.TBool, None),
1456 197b323b Michael Hanselmann
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
1457 197b323b Michael Hanselmann
    ("fail", False, ht.TBool, None),
1458 e58f87a9 Michael Hanselmann
    ]
1459 e58f87a9 Michael Hanselmann
1460 e58f87a9 Michael Hanselmann
1461 be760ba8 Michael Hanselmann
class OpTestDummy(OpCode):
1462 be760ba8 Michael Hanselmann
  """Utility opcode used by unittests.
1463 be760ba8 Michael Hanselmann

1464 be760ba8 Michael Hanselmann
  """
1465 65e183af Michael Hanselmann
  OP_PARAMS = [
1466 197b323b Michael Hanselmann
    ("result", ht.NoDefault, ht.NoType, None),
1467 197b323b Michael Hanselmann
    ("messages", ht.NoDefault, ht.NoType, None),
1468 197b323b Michael Hanselmann
    ("fail", ht.NoDefault, ht.NoType, None),
1469 6a373640 Michael Hanselmann
    ("submit_jobs", None, ht.NoType, None),
1470 be760ba8 Michael Hanselmann
    ]
1471 687c10d9 Iustin Pop
  WITH_LU = False
1472 be760ba8 Michael Hanselmann
1473 be760ba8 Michael Hanselmann
1474 dbc96028 Michael Hanselmann
def _GetOpList():
1475 dbc96028 Michael Hanselmann
  """Returns list of all defined opcodes.
1476 dbc96028 Michael Hanselmann

1477 dbc96028 Michael Hanselmann
  Does not eliminate duplicates by C{OP_ID}.
1478 dbc96028 Michael Hanselmann

1479 dbc96028 Michael Hanselmann
  """
1480 dbc96028 Michael Hanselmann
  return [v for v in globals().values()
1481 dbc96028 Michael Hanselmann
          if (isinstance(v, type) and issubclass(v, OpCode) and
1482 687c10d9 Iustin Pop
              hasattr(v, "OP_ID") and v is not OpCode)]
1483 dbc96028 Michael Hanselmann
1484 dbc96028 Michael Hanselmann
1485 dbc96028 Michael Hanselmann
OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())