Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 323f9095

History | View | Annotate | Download (46.6 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 e1f23243 Michael Hanselmann
_PEarlyRelease = ("early_release", False, ht.TBool,
116 e1f23243 Michael Hanselmann
                  "Whether to release locks as soon as possible")
117 e1f23243 Michael Hanselmann
118 45d4c81c Michael Hanselmann
_PIpCheckDoc = "Whether to ensure instance's IP address is inactive"
119 45d4c81c Michael Hanselmann
120 9b64e486 Iustin Pop
#: Do not remember instance state changes
121 6aac5aef Iustin Pop
_PNoRemember = ("no_remember", False, ht.TBool,
122 6aac5aef Iustin Pop
                "Do not remember the state change")
123 9b64e486 Iustin Pop
124 f8fa4175 Michael Hanselmann
#: Target node for instance migration/failover
125 f8fa4175 Michael Hanselmann
_PMigrationTargetNode = ("target_node", None, ht.TMaybeString,
126 f8fa4175 Michael Hanselmann
                         "Target node for shared-storage instances")
127 f8fa4175 Michael Hanselmann
128 323f9095 Stephen Shirley
_PStartupPaused = ("startup_paused", False, ht.TBool,
129 323f9095 Stephen Shirley
                   "Pause instance at startup")
130 323f9095 Stephen Shirley
131 323f9095 Stephen Shirley
132 ff0d18e6 Iustin Pop
#: OP_ID conversion regular expression
133 ff0d18e6 Iustin Pop
_OPID_RE = re.compile("([a-z])([A-Z])")
134 ff0d18e6 Iustin Pop
135 8c9ee749 Michael Hanselmann
#: Utility function for L{OpClusterSetParams}
136 8c9ee749 Michael Hanselmann
_TestClusterOsList = ht.TOr(ht.TNone,
137 8c9ee749 Michael Hanselmann
  ht.TListOf(ht.TAnd(ht.TList, ht.TIsLength(2),
138 8c9ee749 Michael Hanselmann
    ht.TMap(ht.WithDesc("GetFirstItem")(operator.itemgetter(0)),
139 8c9ee749 Michael Hanselmann
            ht.TElemOf(constants.DDMS_VALUES)))))
140 8c9ee749 Michael Hanselmann
141 ff0d18e6 Iustin Pop
142 526a662a Michael Hanselmann
# TODO: Generate check from constants.INIC_PARAMS_TYPES
143 526a662a Michael Hanselmann
#: Utility function for testing NIC definitions
144 526a662a Michael Hanselmann
_TestNicDef = ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
145 526a662a Michael Hanselmann
                         ht.TOr(ht.TNone, ht.TNonEmptyString))
146 526a662a Michael Hanselmann
147 3ce9a5e7 Michael Hanselmann
_SUMMARY_PREFIX = {
148 3ce9a5e7 Michael Hanselmann
  "CLUSTER_": "C_",
149 3ce9a5e7 Michael Hanselmann
  "GROUP_": "G_",
150 3ce9a5e7 Michael Hanselmann
  "NODE_": "N_",
151 3ce9a5e7 Michael Hanselmann
  "INSTANCE_": "I_",
152 3ce9a5e7 Michael Hanselmann
  }
153 3ce9a5e7 Michael Hanselmann
154 526a662a Michael Hanselmann
155 ff0d18e6 Iustin Pop
def _NameToId(name):
156 ff0d18e6 Iustin Pop
  """Convert an opcode class name to an OP_ID.
157 ff0d18e6 Iustin Pop

158 ff0d18e6 Iustin Pop
  @type name: string
159 ff0d18e6 Iustin Pop
  @param name: the class name, as OpXxxYyy
160 ff0d18e6 Iustin Pop
  @rtype: string
161 ff0d18e6 Iustin Pop
  @return: the name in the OP_XXXX_YYYY format
162 ff0d18e6 Iustin Pop

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

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

182 65e183af Michael Hanselmann
  @raise errors.OpPrereqError: when file storage is disabled
183 65e183af Michael Hanselmann

184 65e183af Michael Hanselmann
  """
185 65e183af Michael Hanselmann
  if not constants.ENABLE_FILE_STORAGE:
186 65e183af Michael Hanselmann
    raise errors.OpPrereqError("File storage disabled at configure time",
187 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
188 65e183af Michael Hanselmann
189 65e183af Michael Hanselmann
190 4b97f902 Apollon Oikonomopoulos
def RequireSharedFileStorage():
191 4b97f902 Apollon Oikonomopoulos
  """Checks that shared file storage is enabled.
192 4b97f902 Apollon Oikonomopoulos

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

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

198 4b97f902 Apollon Oikonomopoulos
  """
199 4b97f902 Apollon Oikonomopoulos
  if not constants.ENABLE_SHARED_FILE_STORAGE:
200 4b97f902 Apollon Oikonomopoulos
    raise errors.OpPrereqError("Shared file storage disabled at"
201 4b97f902 Apollon Oikonomopoulos
                               " configure time", errors.ECODE_INVAL)
202 4b97f902 Apollon Oikonomopoulos
203 4b97f902 Apollon Oikonomopoulos
204 8c9ee749 Michael Hanselmann
@ht.WithDesc("CheckFileStorage")
205 8c9ee749 Michael Hanselmann
def _CheckFileStorage(value):
206 8c9ee749 Michael Hanselmann
  """Ensures file storage is enabled if used.
207 65e183af Michael Hanselmann

208 65e183af Michael Hanselmann
  """
209 8c9ee749 Michael Hanselmann
  if value == constants.DT_FILE:
210 65e183af Michael Hanselmann
    RequireFileStorage()
211 4b97f902 Apollon Oikonomopoulos
  elif value == constants.DT_SHARED_FILE:
212 4b97f902 Apollon Oikonomopoulos
    RequireSharedFileStorage()
213 65e183af Michael Hanselmann
  return True
214 65e183af Michael Hanselmann
215 65e183af Michael Hanselmann
216 8c9ee749 Michael Hanselmann
_CheckDiskTemplate = ht.TAnd(ht.TElemOf(constants.DISK_TEMPLATES),
217 8c9ee749 Michael Hanselmann
                             _CheckFileStorage)
218 8c9ee749 Michael Hanselmann
219 8c9ee749 Michael Hanselmann
220 65e183af Michael Hanselmann
def _CheckStorageType(storage_type):
221 65e183af Michael Hanselmann
  """Ensure a given storage type is valid.
222 65e183af Michael Hanselmann

223 65e183af Michael Hanselmann
  """
224 65e183af Michael Hanselmann
  if storage_type not in constants.VALID_STORAGE_TYPES:
225 65e183af Michael Hanselmann
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
226 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
227 65e183af Michael Hanselmann
  if storage_type == constants.ST_FILE:
228 65e183af Michael Hanselmann
    RequireFileStorage()
229 65e183af Michael Hanselmann
  return True
230 65e183af Michael Hanselmann
231 65e183af Michael Hanselmann
232 65e183af Michael Hanselmann
#: Storage type parameter
233 45d4c81c Michael Hanselmann
_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
234 45d4c81c Michael Hanselmann
                 "Storage type")
235 65e183af Michael Hanselmann
236 65e183af Michael Hanselmann
237 65e183af Michael Hanselmann
class _AutoOpParamSlots(type):
238 65e183af Michael Hanselmann
  """Meta class for opcode definitions.
239 65e183af Michael Hanselmann

240 65e183af Michael Hanselmann
  """
241 65e183af Michael Hanselmann
  def __new__(mcs, name, bases, attrs):
242 65e183af Michael Hanselmann
    """Called when a class should be created.
243 65e183af Michael Hanselmann

244 65e183af Michael Hanselmann
    @param mcs: The meta class
245 65e183af Michael Hanselmann
    @param name: Name of created class
246 65e183af Michael Hanselmann
    @param bases: Base classes
247 65e183af Michael Hanselmann
    @type attrs: dict
248 65e183af Michael Hanselmann
    @param attrs: Class attributes
249 65e183af Michael Hanselmann

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

274 0e46916d Iustin Pop
  This object serves as a parent class for OpCode without any custom
275 0e46916d Iustin Pop
  field handling.
276 0e46916d Iustin Pop

277 df458e0b Iustin Pop
  """
278 e89a9021 Iustin Pop
  # pylint: disable-msg=E1101
279 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
280 65e183af Michael Hanselmann
  __metaclass__ = _AutoOpParamSlots
281 65e183af Michael Hanselmann
282 a8083063 Iustin Pop
  def __init__(self, **kwargs):
283 a7399f66 Iustin Pop
    """Constructor for BaseOpCode.
284 a7399f66 Iustin Pop

285 a7399f66 Iustin Pop
    The constructor takes only keyword arguments and will set
286 a7399f66 Iustin Pop
    attributes on this object based on the passed arguments. As such,
287 a7399f66 Iustin Pop
    it means that you should not pass arguments which are not in the
288 a7399f66 Iustin Pop
    __slots__ attribute for this class.
289 a7399f66 Iustin Pop

290 a7399f66 Iustin Pop
    """
291 adf385c7 Iustin Pop
    slots = self._all_slots()
292 a8083063 Iustin Pop
    for key in kwargs:
293 adf385c7 Iustin Pop
      if key not in slots:
294 df458e0b Iustin Pop
        raise TypeError("Object %s doesn't support the parameter '%s'" %
295 3ecf6786 Iustin Pop
                        (self.__class__.__name__, key))
296 a8083063 Iustin Pop
      setattr(self, key, kwargs[key])
297 a8083063 Iustin Pop
298 df458e0b Iustin Pop
  def __getstate__(self):
299 a7399f66 Iustin Pop
    """Generic serializer.
300 a7399f66 Iustin Pop

301 a7399f66 Iustin Pop
    This method just returns the contents of the instance as a
302 a7399f66 Iustin Pop
    dictionary.
303 a7399f66 Iustin Pop

304 a7399f66 Iustin Pop
    @rtype:  C{dict}
305 a7399f66 Iustin Pop
    @return: the instance attributes and their values
306 a7399f66 Iustin Pop

307 a7399f66 Iustin Pop
    """
308 df458e0b Iustin Pop
    state = {}
309 adf385c7 Iustin Pop
    for name in self._all_slots():
310 df458e0b Iustin Pop
      if hasattr(self, name):
311 df458e0b Iustin Pop
        state[name] = getattr(self, name)
312 df458e0b Iustin Pop
    return state
313 df458e0b Iustin Pop
314 df458e0b Iustin Pop
  def __setstate__(self, state):
315 a7399f66 Iustin Pop
    """Generic unserializer.
316 a7399f66 Iustin Pop

317 a7399f66 Iustin Pop
    This method just restores from the serialized state the attributes
318 a7399f66 Iustin Pop
    of the current instance.
319 a7399f66 Iustin Pop

320 a7399f66 Iustin Pop
    @param state: the serialized opcode data
321 a7399f66 Iustin Pop
    @type state:  C{dict}
322 a7399f66 Iustin Pop

323 a7399f66 Iustin Pop
    """
324 df458e0b Iustin Pop
    if not isinstance(state, dict):
325 df458e0b Iustin Pop
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
326 df458e0b Iustin Pop
                       type(state))
327 df458e0b Iustin Pop
328 adf385c7 Iustin Pop
    for name in self._all_slots():
329 44db3a6f Iustin Pop
      if name not in state and hasattr(self, name):
330 df458e0b Iustin Pop
        delattr(self, name)
331 df458e0b Iustin Pop
332 df458e0b Iustin Pop
    for name in state:
333 df458e0b Iustin Pop
      setattr(self, name, state[name])
334 df458e0b Iustin Pop
335 adf385c7 Iustin Pop
  @classmethod
336 adf385c7 Iustin Pop
  def _all_slots(cls):
337 adf385c7 Iustin Pop
    """Compute the list of all declared slots for a class.
338 adf385c7 Iustin Pop

339 adf385c7 Iustin Pop
    """
340 adf385c7 Iustin Pop
    slots = []
341 adf385c7 Iustin Pop
    for parent in cls.__mro__:
342 adf385c7 Iustin Pop
      slots.extend(getattr(parent, "__slots__", []))
343 adf385c7 Iustin Pop
    return slots
344 adf385c7 Iustin Pop
345 65e183af Michael Hanselmann
  @classmethod
346 65e183af Michael Hanselmann
  def GetAllParams(cls):
347 65e183af Michael Hanselmann
    """Compute list of all parameters for an opcode.
348 65e183af Michael Hanselmann

349 65e183af Michael Hanselmann
    """
350 65e183af Michael Hanselmann
    slots = []
351 65e183af Michael Hanselmann
    for parent in cls.__mro__:
352 65e183af Michael Hanselmann
      slots.extend(getattr(parent, "OP_PARAMS", []))
353 65e183af Michael Hanselmann
    return slots
354 65e183af Michael Hanselmann
355 1cbef6d8 Michael Hanselmann
  def Validate(self, set_defaults):
356 1cbef6d8 Michael Hanselmann
    """Validate opcode parameters, optionally setting default values.
357 1cbef6d8 Michael Hanselmann

358 1cbef6d8 Michael Hanselmann
    @type set_defaults: bool
359 1cbef6d8 Michael Hanselmann
    @param set_defaults: Whether to set default values
360 1cbef6d8 Michael Hanselmann
    @raise errors.OpPrereqError: When a parameter value doesn't match
361 1cbef6d8 Michael Hanselmann
                                 requirements
362 1cbef6d8 Michael Hanselmann

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

396 a7399f66 Iustin Pop
  This is the root of the actual OpCode hierarchy. All clases derived
397 a7399f66 Iustin Pop
  from this class should override OP_ID.
398 a7399f66 Iustin Pop

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

412 a7399f66 Iustin Pop
  """
413 e89a9021 Iustin Pop
  # pylint: disable-msg=E1101
414 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
415 687c10d9 Iustin Pop
  WITH_LU = True
416 65e183af Michael Hanselmann
  OP_PARAMS = [
417 45d4c81c Michael Hanselmann
    ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
418 45d4c81c Michael Hanselmann
    ("debug_level", None, ht.TOr(ht.TNone, ht.TPositiveInt), "Debug level"),
419 65e183af Michael Hanselmann
    ("priority", constants.OP_PRIO_DEFAULT,
420 45d4c81c Michael Hanselmann
     ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
421 65e183af Michael Hanselmann
    ]
422 df458e0b Iustin Pop
423 df458e0b Iustin Pop
  def __getstate__(self):
424 df458e0b Iustin Pop
    """Specialized getstate for opcodes.
425 df458e0b Iustin Pop

426 a7399f66 Iustin Pop
    This method adds to the state dictionary the OP_ID of the class,
427 a7399f66 Iustin Pop
    so that on unload we can identify the correct class for
428 a7399f66 Iustin Pop
    instantiating the opcode.
429 a7399f66 Iustin Pop

430 a7399f66 Iustin Pop
    @rtype:   C{dict}
431 a7399f66 Iustin Pop
    @return:  the state as a dictionary
432 a7399f66 Iustin Pop

433 df458e0b Iustin Pop
    """
434 0e46916d Iustin Pop
    data = BaseOpCode.__getstate__(self)
435 df458e0b Iustin Pop
    data["OP_ID"] = self.OP_ID
436 df458e0b Iustin Pop
    return data
437 df458e0b Iustin Pop
438 df458e0b Iustin Pop
  @classmethod
439 00abdc96 Iustin Pop
  def LoadOpCode(cls, data):
440 df458e0b Iustin Pop
    """Generic load opcode method.
441 df458e0b Iustin Pop

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

446 a7399f66 Iustin Pop
    @type data:  C{dict}
447 a7399f66 Iustin Pop
    @param data: the serialized opcode
448 a7399f66 Iustin Pop

449 df458e0b Iustin Pop
    """
450 df458e0b Iustin Pop
    if not isinstance(data, dict):
451 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
452 df458e0b Iustin Pop
    if "OP_ID" not in data:
453 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
454 df458e0b Iustin Pop
    op_id = data["OP_ID"]
455 df458e0b Iustin Pop
    op_class = None
456 363acb1e Iustin Pop
    if op_id in OP_MAPPING:
457 363acb1e Iustin Pop
      op_class = OP_MAPPING[op_id]
458 363acb1e Iustin Pop
    else:
459 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
460 df458e0b Iustin Pop
                       op_id)
461 df458e0b Iustin Pop
    op = op_class()
462 df458e0b Iustin Pop
    new_data = data.copy()
463 df458e0b Iustin Pop
    del new_data["OP_ID"]
464 df458e0b Iustin Pop
    op.__setstate__(new_data)
465 df458e0b Iustin Pop
    return op
466 df458e0b Iustin Pop
467 60dd1473 Iustin Pop
  def Summary(self):
468 60dd1473 Iustin Pop
    """Generates a summary description of this opcode.
469 60dd1473 Iustin Pop

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

476 60dd1473 Iustin Pop
    """
477 ff0d18e6 Iustin Pop
    assert self.OP_ID is not None and len(self.OP_ID) > 3
478 60dd1473 Iustin Pop
    # all OP_ID start with OP_, we remove that
479 60dd1473 Iustin Pop
    txt = self.OP_ID[3:]
480 60dd1473 Iustin Pop
    field_name = getattr(self, "OP_DSC_FIELD", None)
481 60dd1473 Iustin Pop
    if field_name:
482 60dd1473 Iustin Pop
      field_value = getattr(self, field_name, None)
483 bc8bbda1 Iustin Pop
      if isinstance(field_value, (list, tuple)):
484 bc8bbda1 Iustin Pop
        field_value = ",".join(str(i) for i in field_value)
485 60dd1473 Iustin Pop
      txt = "%s(%s)" % (txt, field_value)
486 60dd1473 Iustin Pop
    return txt
487 60dd1473 Iustin Pop
488 3ce9a5e7 Michael Hanselmann
  def TinySummary(self):
489 3ce9a5e7 Michael Hanselmann
    """Generates a compact summary description of the opcode.
490 3ce9a5e7 Michael Hanselmann

491 3ce9a5e7 Michael Hanselmann
    """
492 3ce9a5e7 Michael Hanselmann
    assert self.OP_ID.startswith("OP_")
493 3ce9a5e7 Michael Hanselmann
494 3ce9a5e7 Michael Hanselmann
    text = self.OP_ID[3:]
495 3ce9a5e7 Michael Hanselmann
496 3ce9a5e7 Michael Hanselmann
    for (prefix, supplement) in _SUMMARY_PREFIX.items():
497 3ce9a5e7 Michael Hanselmann
      if text.startswith(prefix):
498 3ce9a5e7 Michael Hanselmann
        return supplement + text[len(prefix):]
499 3ce9a5e7 Michael Hanselmann
500 3ce9a5e7 Michael Hanselmann
    return text
501 3ce9a5e7 Michael Hanselmann
502 a8083063 Iustin Pop
503 afee0879 Iustin Pop
# cluster opcodes
504 afee0879 Iustin Pop
505 bc84ffa7 Iustin Pop
class OpClusterPostInit(OpCode):
506 b5f5fae9 Luca Bigliardi
  """Post cluster initialization.
507 b5f5fae9 Luca Bigliardi

508 b5f5fae9 Luca Bigliardi
  This opcode does not touch the cluster at all. Its purpose is to run hooks
509 b5f5fae9 Luca Bigliardi
  after the cluster has been initialized.
510 b5f5fae9 Luca Bigliardi

511 b5f5fae9 Luca Bigliardi
  """
512 b5f5fae9 Luca Bigliardi
513 b5f5fae9 Luca Bigliardi
514 c6d43e9e Iustin Pop
class OpClusterDestroy(OpCode):
515 a7399f66 Iustin Pop
  """Destroy the cluster.
516 a7399f66 Iustin Pop

517 a7399f66 Iustin Pop
  This opcode has no other parameters. All the state is irreversibly
518 a7399f66 Iustin Pop
  lost after the execution of this opcode.
519 a7399f66 Iustin Pop

520 a7399f66 Iustin Pop
  """
521 a8083063 Iustin Pop
522 a8083063 Iustin Pop
523 a2f7ab92 Iustin Pop
class OpClusterQuery(OpCode):
524 fdc267f4 Iustin Pop
  """Query cluster information."""
525 a8083063 Iustin Pop
526 a8083063 Iustin Pop
527 bf93ae69 Adeodato Simo
class OpClusterVerifyConfig(OpCode):
528 bf93ae69 Adeodato Simo
  """Verify the cluster config.
529 bf93ae69 Adeodato Simo

530 bf93ae69 Adeodato Simo
  """
531 bf93ae69 Adeodato Simo
  OP_PARAMS = [
532 bf93ae69 Adeodato Simo
    ("verbose", False, ht.TBool, None),
533 bf93ae69 Adeodato Simo
    ("error_codes", False, ht.TBool, None),
534 bf93ae69 Adeodato Simo
    ("debug_simulate_errors", False, ht.TBool, None),
535 bf93ae69 Adeodato Simo
    ]
536 bf93ae69 Adeodato Simo
537 bf93ae69 Adeodato Simo
538 bf93ae69 Adeodato Simo
class OpClusterVerifyGroup(OpCode):
539 bf93ae69 Adeodato Simo
  """Run verify on a node group from the cluster.
540 a7399f66 Iustin Pop

541 a7399f66 Iustin Pop
  @type skip_checks: C{list}
542 a7399f66 Iustin Pop
  @ivar skip_checks: steps to be skipped from the verify process; this
543 a7399f66 Iustin Pop
                     needs to be a subset of
544 a7399f66 Iustin Pop
                     L{constants.VERIFY_OPTIONAL_CHECKS}; currently
545 a7399f66 Iustin Pop
                     only L{constants.VERIFY_NPLUSONE_MEM} can be passed
546 a7399f66 Iustin Pop

547 a7399f66 Iustin Pop
  """
548 bf93ae69 Adeodato Simo
  OP_DSC_FIELD = "group_name"
549 65e183af Michael Hanselmann
  OP_PARAMS = [
550 bf93ae69 Adeodato Simo
    ("group_name", ht.NoDefault, ht.TNonEmptyString, None),
551 65e183af Michael Hanselmann
    ("skip_checks", ht.EmptyList,
552 197b323b Michael Hanselmann
     ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)), None),
553 197b323b Michael Hanselmann
    ("verbose", False, ht.TBool, None),
554 197b323b Michael Hanselmann
    ("error_codes", False, ht.TBool, None),
555 197b323b Michael Hanselmann
    ("debug_simulate_errors", False, ht.TBool, None),
556 65e183af Michael Hanselmann
    ]
557 a8083063 Iustin Pop
558 a8083063 Iustin Pop
559 bd8210a7 Iustin Pop
class OpClusterVerifyDisks(OpCode):
560 150e978f Iustin Pop
  """Verify the cluster disks.
561 150e978f Iustin Pop

562 150e978f Iustin Pop
  Parameters: none
563 150e978f Iustin Pop

564 5188ab37 Iustin Pop
  Result: a tuple of four elements:
565 150e978f Iustin Pop
    - list of node names with bad data returned (unreachable, etc.)
566 a7399f66 Iustin Pop
    - dict of node names with broken volume groups (values: error msg)
567 150e978f Iustin Pop
    - list of instances with degraded disks (that should be activated)
568 b63ed789 Iustin Pop
    - dict of instances with missing logical volumes (values: (node, vol)
569 b63ed789 Iustin Pop
      pairs with details about the missing volumes)
570 150e978f Iustin Pop

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

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

579 150e978f Iustin Pop
  """
580 150e978f Iustin Pop
581 150e978f Iustin Pop
582 5d01aca3 Iustin Pop
class OpClusterRepairDiskSizes(OpCode):
583 60975797 Iustin Pop
  """Verify the disk sizes of the instances and fixes configuration
584 60975797 Iustin Pop
  mimatches.
585 60975797 Iustin Pop

586 60975797 Iustin Pop
  Parameters: optional instances list, in case we want to restrict the
587 60975797 Iustin Pop
  checks to only a subset of the instances.
588 60975797 Iustin Pop

589 60975797 Iustin Pop
  Result: a list of tuples, (instance, disk, new-size) for changed
590 60975797 Iustin Pop
  configurations.
591 60975797 Iustin Pop

592 60975797 Iustin Pop
  In normal operation, the list should be empty.
593 60975797 Iustin Pop

594 60975797 Iustin Pop
  @type instances: list
595 60975797 Iustin Pop
  @ivar instances: the list of instances to check, or empty for all instances
596 60975797 Iustin Pop

597 60975797 Iustin Pop
  """
598 65e183af Michael Hanselmann
  OP_PARAMS = [
599 197b323b Michael Hanselmann
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
600 65e183af Michael Hanselmann
    ]
601 60975797 Iustin Pop
602 60975797 Iustin Pop
603 2f093ea0 Iustin Pop
class OpClusterConfigQuery(OpCode):
604 ae5849b5 Michael Hanselmann
  """Query cluster configuration values."""
605 65e183af Michael Hanselmann
  OP_PARAMS = [
606 65e183af Michael Hanselmann
    _POutputFields
607 65e183af Michael Hanselmann
    ]
608 a8083063 Iustin Pop
609 a8083063 Iustin Pop
610 e126df25 Iustin Pop
class OpClusterRename(OpCode):
611 a7399f66 Iustin Pop
  """Rename the cluster.
612 a7399f66 Iustin Pop

613 a7399f66 Iustin Pop
  @type name: C{str}
614 a7399f66 Iustin Pop
  @ivar name: The new name of the cluster. The name and/or the master IP
615 a7399f66 Iustin Pop
              address will be changed to match the new name and its IP
616 a7399f66 Iustin Pop
              address.
617 a7399f66 Iustin Pop

618 a7399f66 Iustin Pop
  """
619 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
620 65e183af Michael Hanselmann
  OP_PARAMS = [
621 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
622 65e183af Michael Hanselmann
    ]
623 07bd8a51 Iustin Pop
624 07bd8a51 Iustin Pop
625 a6682fdc Iustin Pop
class OpClusterSetParams(OpCode):
626 a7399f66 Iustin Pop
  """Change the parameters of the cluster.
627 a7399f66 Iustin Pop

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

631 a7399f66 Iustin Pop
  """
632 65e183af Michael Hanselmann
  OP_PARAMS = [
633 45d4c81c Michael Hanselmann
    ("vg_name", None, ht.TMaybeString, "Volume group name"),
634 65e183af Michael Hanselmann
    ("enabled_hypervisors", None,
635 65e183af Michael Hanselmann
     ht.TOr(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)), ht.TTrue),
636 45d4c81c Michael Hanselmann
            ht.TNone),
637 45d4c81c Michael Hanselmann
     "List of enabled hypervisors"),
638 65e183af Michael Hanselmann
    ("hvparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
639 45d4c81c Michael Hanselmann
                              ht.TNone),
640 45d4c81c Michael Hanselmann
     "Cluster-wide hypervisor parameter defaults, hypervisor-dependent"),
641 45d4c81c Michael Hanselmann
    ("beparams", None, ht.TOr(ht.TDict, ht.TNone),
642 45d4c81c Michael Hanselmann
     "Cluster-wide backend parameter defaults"),
643 65e183af Michael Hanselmann
    ("os_hvp", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
644 45d4c81c Michael Hanselmann
                            ht.TNone),
645 45d4c81c Michael Hanselmann
     "Cluster-wide per-OS hypervisor parameter defaults"),
646 65e183af Michael Hanselmann
    ("osparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
647 45d4c81c Michael Hanselmann
                              ht.TNone),
648 45d4c81c Michael Hanselmann
     "Cluster-wide OS parameter defaults"),
649 197b323b Michael Hanselmann
    ("candidate_pool_size", None, ht.TOr(ht.TStrictPositiveInt, ht.TNone),
650 45d4c81c Michael Hanselmann
     "Master candidate pool size"),
651 45d4c81c Michael Hanselmann
    ("uid_pool", None, ht.NoType,
652 45d4c81c Michael Hanselmann
     "Set UID pool, must be list of lists describing UID ranges (two items,"
653 45d4c81c Michael Hanselmann
     " start and end inclusive)"),
654 45d4c81c Michael Hanselmann
    ("add_uids", None, ht.NoType,
655 45d4c81c Michael Hanselmann
     "Extend UID pool, must be list of lists describing UID ranges (two"
656 45d4c81c Michael Hanselmann
     " items, start and end inclusive) to be added"),
657 45d4c81c Michael Hanselmann
    ("remove_uids", None, ht.NoType,
658 45d4c81c Michael Hanselmann
     "Shrink UID pool, must be list of lists describing UID ranges (two"
659 45d4c81c Michael Hanselmann
     " items, start and end inclusive) to be removed"),
660 45d4c81c Michael Hanselmann
    ("maintain_node_health", None, ht.TMaybeBool,
661 45d4c81c Michael Hanselmann
     "Whether to automatically maintain node health"),
662 45d4c81c Michael Hanselmann
    ("prealloc_wipe_disks", None, ht.TMaybeBool,
663 45d4c81c Michael Hanselmann
     "Whether to wipe disks before allocating them to instances"),
664 45d4c81c Michael Hanselmann
    ("nicparams", None, ht.TMaybeDict, "Cluster-wide NIC parameter defaults"),
665 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Cluster-wide node parameter defaults"),
666 45d4c81c Michael Hanselmann
    ("drbd_helper", None, ht.TOr(ht.TString, ht.TNone), "DRBD helper program"),
667 45d4c81c Michael Hanselmann
    ("default_iallocator", None, ht.TOr(ht.TString, ht.TNone),
668 45d4c81c Michael Hanselmann
     "Default iallocator for cluster"),
669 45d4c81c Michael Hanselmann
    ("master_netdev", None, ht.TOr(ht.TString, ht.TNone),
670 45d4c81c Michael Hanselmann
     "Master network device"),
671 45d4c81c Michael Hanselmann
    ("reserved_lvs", None, ht.TOr(ht.TListOf(ht.TNonEmptyString), ht.TNone),
672 45d4c81c Michael Hanselmann
     "List of reserved LVs"),
673 45d4c81c Michael Hanselmann
    ("hidden_os", None, _TestClusterOsList,
674 45d4c81c Michael Hanselmann
     "Modify list of hidden operating systems. Each modification must have"
675 45d4c81c Michael Hanselmann
     " two items, the operation and the OS name. The operation can be"
676 45d4c81c Michael Hanselmann
     " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
677 45d4c81c Michael Hanselmann
    ("blacklisted_os", None, _TestClusterOsList,
678 45d4c81c Michael Hanselmann
     "Modify list of blacklisted operating systems. Each modification must have"
679 45d4c81c Michael Hanselmann
     " two items, the operation and the OS name. The operation can be"
680 45d4c81c Michael Hanselmann
     " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
681 4b7735f9 Iustin Pop
    ]
682 12515db7 Manuel Franceschini
683 12515db7 Manuel Franceschini
684 d1240007 Iustin Pop
class OpClusterRedistConf(OpCode):
685 afee0879 Iustin Pop
  """Force a full push of the cluster configuration.
686 afee0879 Iustin Pop

687 afee0879 Iustin Pop
  """
688 afee0879 Iustin Pop
689 83f72637 Michael Hanselmann
690 83f72637 Michael Hanselmann
class OpQuery(OpCode):
691 83f72637 Michael Hanselmann
  """Query for resources/items.
692 83f72637 Michael Hanselmann

693 abd66bf8 Michael Hanselmann
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
694 83f72637 Michael Hanselmann
  @ivar fields: List of fields to retrieve
695 83f72637 Michael Hanselmann
  @ivar filter: Query filter
696 83f72637 Michael Hanselmann

697 83f72637 Michael Hanselmann
  """
698 65e183af Michael Hanselmann
  OP_PARAMS = [
699 8e7078e0 Michael Hanselmann
    _PQueryWhat,
700 45d4c81c Michael Hanselmann
    ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
701 45d4c81c Michael Hanselmann
     "Requested fields"),
702 b107fe05 Michael Hanselmann
    ("filter", None, ht.TOr(ht.TNone, ht.TListOf),
703 45d4c81c Michael Hanselmann
     "Query filter"),
704 83f72637 Michael Hanselmann
    ]
705 83f72637 Michael Hanselmann
706 83f72637 Michael Hanselmann
707 83f72637 Michael Hanselmann
class OpQueryFields(OpCode):
708 83f72637 Michael Hanselmann
  """Query for available resource/item fields.
709 83f72637 Michael Hanselmann

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

713 83f72637 Michael Hanselmann
  """
714 65e183af Michael Hanselmann
  OP_PARAMS = [
715 8e7078e0 Michael Hanselmann
    _PQueryWhat,
716 8e7078e0 Michael Hanselmann
    ("fields", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
717 8e7078e0 Michael Hanselmann
     "Requested fields; if not given, all are returned"),
718 83f72637 Michael Hanselmann
    ]
719 83f72637 Michael Hanselmann
720 83f72637 Michael Hanselmann
721 792af3ad Renรฉ Nussbaumer
class OpOobCommand(OpCode):
722 eb64da59 Renรฉ Nussbaumer
  """Interact with OOB."""
723 65e183af Michael Hanselmann
  OP_PARAMS = [
724 c4ec0755 Renรฉ Nussbaumer
    ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
725 c4ec0755 Renรฉ Nussbaumer
     "List of nodes to run the OOB command against"),
726 c4ec0755 Renรฉ Nussbaumer
    ("command", None, ht.TElemOf(constants.OOB_COMMANDS),
727 c4ec0755 Renรฉ Nussbaumer
     "OOB command to be run"),
728 c4ec0755 Renรฉ Nussbaumer
    ("timeout", constants.OOB_TIMEOUT, ht.TInt,
729 c4ec0755 Renรฉ Nussbaumer
     "Timeout before the OOB helper will be terminated"),
730 c4ec0755 Renรฉ Nussbaumer
    ("ignore_status", False, ht.TBool,
731 c4ec0755 Renรฉ Nussbaumer
     "Ignores the node offline status for power off"),
732 beff3779 Renรฉ Nussbaumer
    ("power_delay", constants.OOB_POWER_DELAY, ht.TPositiveFloat,
733 beff3779 Renรฉ Nussbaumer
     "Time in seconds to wait between powering on nodes"),
734 eb64da59 Renรฉ Nussbaumer
    ]
735 eb64da59 Renรฉ Nussbaumer
736 eb64da59 Renรฉ Nussbaumer
737 07bd8a51 Iustin Pop
# node opcodes
738 07bd8a51 Iustin Pop
739 73d565a3 Iustin Pop
class OpNodeRemove(OpCode):
740 a7399f66 Iustin Pop
  """Remove a node.
741 a7399f66 Iustin Pop

742 a7399f66 Iustin Pop
  @type node_name: C{str}
743 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to remove. If the node still has
744 a7399f66 Iustin Pop
                   instances on it, the operation will fail.
745 a7399f66 Iustin Pop

746 a7399f66 Iustin Pop
  """
747 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
748 65e183af Michael Hanselmann
  OP_PARAMS = [
749 65e183af Michael Hanselmann
    _PNodeName,
750 65e183af Michael Hanselmann
    ]
751 a8083063 Iustin Pop
752 a8083063 Iustin Pop
753 d817d49f Iustin Pop
class OpNodeAdd(OpCode):
754 a7399f66 Iustin Pop
  """Add a node to the cluster.
755 a7399f66 Iustin Pop

756 a7399f66 Iustin Pop
  @type node_name: C{str}
757 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to add. This can be a short name,
758 a7399f66 Iustin Pop
                   but it will be expanded to the FQDN.
759 a7399f66 Iustin Pop
  @type primary_ip: IP address
760 a7399f66 Iustin Pop
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
761 a7399f66 Iustin Pop
                    opcode is submitted, but will be filled during the node
762 a7399f66 Iustin Pop
                    add (so it will be visible in the job query).
763 a7399f66 Iustin Pop
  @type secondary_ip: IP address
764 a7399f66 Iustin Pop
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
765 a7399f66 Iustin Pop
                      if the cluster has been initialized in 'dual-network'
766 a7399f66 Iustin Pop
                      mode, otherwise it must not be given.
767 a7399f66 Iustin Pop
  @type readd: C{bool}
768 a7399f66 Iustin Pop
  @ivar readd: Whether to re-add an existing node to the cluster. If
769 a7399f66 Iustin Pop
               this is not passed, then the operation will abort if the node
770 a7399f66 Iustin Pop
               name is already in the cluster; use this parameter to 'repair'
771 a7399f66 Iustin Pop
               a node that had its configuration broken, or was reinstalled
772 a7399f66 Iustin Pop
               without removal from the cluster.
773 f936c153 Iustin Pop
  @type group: C{str}
774 f936c153 Iustin Pop
  @ivar group: The node group to which this node will belong.
775 fd3d37b6 Iustin Pop
  @type vm_capable: C{bool}
776 fd3d37b6 Iustin Pop
  @ivar vm_capable: The vm_capable node attribute
777 fd3d37b6 Iustin Pop
  @type master_capable: C{bool}
778 fd3d37b6 Iustin Pop
  @ivar master_capable: The master_capable node attribute
779 a7399f66 Iustin Pop

780 a7399f66 Iustin Pop
  """
781 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
782 65e183af Michael Hanselmann
  OP_PARAMS = [
783 65e183af Michael Hanselmann
    _PNodeName,
784 45d4c81c Michael Hanselmann
    ("primary_ip", None, ht.NoType, "Primary IP address"),
785 45d4c81c Michael Hanselmann
    ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
786 45d4c81c Michael Hanselmann
    ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
787 45d4c81c Michael Hanselmann
    ("group", None, ht.TMaybeString, "Initial node group"),
788 45d4c81c Michael Hanselmann
    ("master_capable", None, ht.TMaybeBool,
789 45d4c81c Michael Hanselmann
     "Whether node can become master or master candidate"),
790 45d4c81c Michael Hanselmann
    ("vm_capable", None, ht.TMaybeBool,
791 45d4c81c Michael Hanselmann
     "Whether node can host instances"),
792 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Node parameters"),
793 65e183af Michael Hanselmann
    ]
794 a8083063 Iustin Pop
795 a8083063 Iustin Pop
796 2237687b Iustin Pop
class OpNodeQuery(OpCode):
797 a8083063 Iustin Pop
  """Compute the list of nodes."""
798 65e183af Michael Hanselmann
  OP_PARAMS = [
799 65e183af Michael Hanselmann
    _POutputFields,
800 45d4c81c Michael Hanselmann
    _PUseLocking,
801 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
802 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
803 65e183af Michael Hanselmann
    ]
804 a8083063 Iustin Pop
805 a8083063 Iustin Pop
806 8ed55bfd Iustin Pop
class OpNodeQueryvols(OpCode):
807 dcb93971 Michael Hanselmann
  """Get list of volumes on node."""
808 65e183af Michael Hanselmann
  OP_PARAMS = [
809 65e183af Michael Hanselmann
    _POutputFields,
810 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
811 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
812 65e183af Michael Hanselmann
    ]
813 dcb93971 Michael Hanselmann
814 dcb93971 Michael Hanselmann
815 ad8d0595 Iustin Pop
class OpNodeQueryStorage(OpCode):
816 9e5442ce Michael Hanselmann
  """Get information on storage for node(s)."""
817 65e183af Michael Hanselmann
  OP_PARAMS = [
818 65e183af Michael Hanselmann
    _POutputFields,
819 65e183af Michael Hanselmann
    _PStorageType,
820 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
821 45d4c81c Michael Hanselmann
    ("name", None, ht.TMaybeString, "Storage name"),
822 9e5442ce Michael Hanselmann
    ]
823 9e5442ce Michael Hanselmann
824 9e5442ce Michael Hanselmann
825 2cee4077 Iustin Pop
class OpNodeModifyStorage(OpCode):
826 099c52ad Iustin Pop
  """Modifies the properies of a storage unit"""
827 65e183af Michael Hanselmann
  OP_PARAMS = [
828 65e183af Michael Hanselmann
    _PNodeName,
829 65e183af Michael Hanselmann
    _PStorageType,
830 45d4c81c Michael Hanselmann
    _PStorageName,
831 45d4c81c Michael Hanselmann
    ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
832 efb8da02 Michael Hanselmann
    ]
833 efb8da02 Michael Hanselmann
834 efb8da02 Michael Hanselmann
835 76aef8fc Michael Hanselmann
class OpRepairNodeStorage(OpCode):
836 76aef8fc Michael Hanselmann
  """Repairs the volume group on a node."""
837 76aef8fc Michael Hanselmann
  OP_DSC_FIELD = "node_name"
838 65e183af Michael Hanselmann
  OP_PARAMS = [
839 65e183af Michael Hanselmann
    _PNodeName,
840 65e183af Michael Hanselmann
    _PStorageType,
841 45d4c81c Michael Hanselmann
    _PStorageName,
842 45d4c81c Michael Hanselmann
    _PIgnoreConsistency,
843 76aef8fc Michael Hanselmann
    ]
844 76aef8fc Michael Hanselmann
845 76aef8fc Michael Hanselmann
846 f13973c4 Iustin Pop
class OpNodeSetParams(OpCode):
847 b31c8676 Iustin Pop
  """Change the parameters of a node."""
848 b31c8676 Iustin Pop
  OP_DSC_FIELD = "node_name"
849 65e183af Michael Hanselmann
  OP_PARAMS = [
850 65e183af Michael Hanselmann
    _PNodeName,
851 65e183af Michael Hanselmann
    _PForce,
852 45d4c81c Michael Hanselmann
    ("master_candidate", None, ht.TMaybeBool,
853 45d4c81c Michael Hanselmann
     "Whether the node should become a master candidate"),
854 45d4c81c Michael Hanselmann
    ("offline", None, ht.TMaybeBool,
855 45d4c81c Michael Hanselmann
     "Whether the node should be marked as offline"),
856 45d4c81c Michael Hanselmann
    ("drained", None, ht.TMaybeBool,
857 45d4c81c Michael Hanselmann
     "Whether the node should be marked as drained"),
858 45d4c81c Michael Hanselmann
    ("auto_promote", False, ht.TBool,
859 45d4c81c Michael Hanselmann
     "Whether node(s) should be promoted to master candidate if necessary"),
860 45d4c81c Michael Hanselmann
    ("master_capable", None, ht.TMaybeBool,
861 45d4c81c Michael Hanselmann
     "Denote whether node can become master or master candidate"),
862 45d4c81c Michael Hanselmann
    ("vm_capable", None, ht.TMaybeBool,
863 45d4c81c Michael Hanselmann
     "Denote whether node can host instances"),
864 45d4c81c Michael Hanselmann
    ("secondary_ip", None, ht.TMaybeString,
865 45d4c81c Michael Hanselmann
     "Change node's secondary IP address"),
866 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
867 45d4c81c Michael Hanselmann
    ("powered", None, ht.TMaybeBool,
868 45d4c81c Michael Hanselmann
     "Whether the node should be marked as powered"),
869 b31c8676 Iustin Pop
    ]
870 b31c8676 Iustin Pop
871 f5118ade Iustin Pop
872 e0d4735f Iustin Pop
class OpNodePowercycle(OpCode):
873 f5118ade Iustin Pop
  """Tries to powercycle a node."""
874 f5118ade Iustin Pop
  OP_DSC_FIELD = "node_name"
875 65e183af Michael Hanselmann
  OP_PARAMS = [
876 65e183af Michael Hanselmann
    _PNodeName,
877 65e183af Michael Hanselmann
    _PForce,
878 f5118ade Iustin Pop
    ]
879 f5118ade Iustin Pop
880 7ffc5a86 Michael Hanselmann
881 5b14a488 Iustin Pop
class OpNodeMigrate(OpCode):
882 80cb875c Michael Hanselmann
  """Migrate all instances from a node."""
883 80cb875c Michael Hanselmann
  OP_DSC_FIELD = "node_name"
884 65e183af Michael Hanselmann
  OP_PARAMS = [
885 65e183af Michael Hanselmann
    _PNodeName,
886 65e183af Michael Hanselmann
    _PMigrationMode,
887 65e183af Michael Hanselmann
    _PMigrationLive,
888 f8fa4175 Michael Hanselmann
    _PMigrationTargetNode,
889 8eb34306 Apollon Oikonomopoulos
    ("iallocator", None, ht.TMaybeString,
890 8eb34306 Apollon Oikonomopoulos
     "Iallocator for deciding the target node for shared-storage instances"),
891 80cb875c Michael Hanselmann
    ]
892 80cb875c Michael Hanselmann
893 80cb875c Michael Hanselmann
894 e1f23243 Michael Hanselmann
class OpNodeEvacuate(OpCode):
895 e1f23243 Michael Hanselmann
  """Evacuate instances off a number of nodes."""
896 e1f23243 Michael Hanselmann
  OP_DSC_FIELD = "node_name"
897 e1f23243 Michael Hanselmann
  OP_PARAMS = [
898 e1f23243 Michael Hanselmann
    _PEarlyRelease,
899 e1f23243 Michael Hanselmann
    _PNodeName,
900 e1f23243 Michael Hanselmann
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
901 e1f23243 Michael Hanselmann
    ("iallocator", None, ht.TMaybeString, "Iallocator for computing solution"),
902 e1f23243 Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES),
903 e1f23243 Michael Hanselmann
     "Node evacuation mode"),
904 e1f23243 Michael Hanselmann
    ]
905 e1f23243 Michael Hanselmann
906 e1f23243 Michael Hanselmann
907 a8083063 Iustin Pop
# instance opcodes
908 a8083063 Iustin Pop
909 e1530b10 Iustin Pop
class OpInstanceCreate(OpCode):
910 9bf56d77 Michael Hanselmann
  """Create an instance.
911 9bf56d77 Michael Hanselmann

912 9bf56d77 Michael Hanselmann
  @ivar instance_name: Instance name
913 9bf56d77 Michael Hanselmann
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
914 9bf56d77 Michael Hanselmann
  @ivar source_handshake: Signed handshake from source (remote import only)
915 9bf56d77 Michael Hanselmann
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
916 9bf56d77 Michael Hanselmann
  @ivar source_instance_name: Previous name of instance (remote import only)
917 dae91d02 Michael Hanselmann
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
918 dae91d02 Michael Hanselmann
    (remote import only)
919 9bf56d77 Michael Hanselmann

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

1086 53c776b5 Iustin Pop
  This migrates (without shutting down an instance) to its secondary
1087 53c776b5 Iustin Pop
  node.
1088 53c776b5 Iustin Pop

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

1092 53c776b5 Iustin Pop
  """
1093 ee69c97f Iustin Pop
  OP_DSC_FIELD = "instance_name"
1094 65e183af Michael Hanselmann
  OP_PARAMS = [
1095 65e183af Michael Hanselmann
    _PInstanceName,
1096 65e183af Michael Hanselmann
    _PMigrationMode,
1097 65e183af Michael Hanselmann
    _PMigrationLive,
1098 f8fa4175 Michael Hanselmann
    _PMigrationTargetNode,
1099 45d4c81c Michael Hanselmann
    ("cleanup", False, ht.TBool,
1100 45d4c81c Michael Hanselmann
     "Whether a previously failed migration should be cleaned up"),
1101 8eb34306 Apollon Oikonomopoulos
    ("iallocator", None, ht.TMaybeString,
1102 8eb34306 Apollon Oikonomopoulos
     "Iallocator for deciding the target node for shared-storage instances"),
1103 d5cafd31 Renรฉ Nussbaumer
    ("allow_failover", False, ht.TBool,
1104 d5cafd31 Renรฉ Nussbaumer
     "Whether we can fallback to failover if migration is not possible"),
1105 65e183af Michael Hanselmann
    ]
1106 53c776b5 Iustin Pop
1107 53c776b5 Iustin Pop
1108 0091b480 Iustin Pop
class OpInstanceMove(OpCode):
1109 313bcead Iustin Pop
  """Move an instance.
1110 313bcead Iustin Pop

1111 313bcead Iustin Pop
  This move (with shutting down an instance and data copying) to an
1112 313bcead Iustin Pop
  arbitrary node.
1113 313bcead Iustin Pop

1114 313bcead Iustin Pop
  @ivar instance_name: the name of the instance
1115 313bcead Iustin Pop
  @ivar target_node: the destination node
1116 313bcead Iustin Pop

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

1310 1410fa8d Michael Hanselmann
  @ivar instance_name: Instance name
1311 1410fa8d Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1312 1410fa8d Michael Hanselmann

1313 1410fa8d Michael Hanselmann
  """
1314 1410fa8d Michael Hanselmann
  OP_DSC_FIELD = "instance_name"
1315 65e183af Michael Hanselmann
  OP_PARAMS = [
1316 65e183af Michael Hanselmann
    _PInstanceName,
1317 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1318 45d4c81c Michael Hanselmann
     "Export mode"),
1319 1410fa8d Michael Hanselmann
    ]
1320 1410fa8d Michael Hanselmann
1321 1410fa8d Michael Hanselmann
1322 4ff922a2 Iustin Pop
class OpBackupExport(OpCode):
1323 4a96f1d1 Michael Hanselmann
  """Export an instance.
1324 4a96f1d1 Michael Hanselmann

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

1331 4a96f1d1 Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1332 4a96f1d1 Michael Hanselmann
  @ivar target_node: Export destination
1333 4a96f1d1 Michael Hanselmann
  @ivar x509_key_name: X509 key to use (remote export only)
1334 4a96f1d1 Michael Hanselmann
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1335 4a96f1d1 Michael Hanselmann
                             only)
1336 4a96f1d1 Michael Hanselmann

1337 4a96f1d1 Michael Hanselmann
  """
1338 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1339 65e183af Michael Hanselmann
  OP_PARAMS = [
1340 65e183af Michael Hanselmann
    _PInstanceName,
1341 65e183af Michael Hanselmann
    _PShutdownTimeout,
1342 4a96f1d1 Michael Hanselmann
    # TODO: Rename target_node as it changes meaning for different export modes
1343 4a96f1d1 Michael Hanselmann
    # (e.g. "destination")
1344 45d4c81c Michael Hanselmann
    ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1345 45d4c81c Michael Hanselmann
     "Destination information, depends on export mode"),
1346 45d4c81c Michael Hanselmann
    ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1347 45d4c81c Michael Hanselmann
    ("remove_instance", False, ht.TBool,
1348 45d4c81c Michael Hanselmann
     "Whether to remove instance after export"),
1349 45d4c81c Michael Hanselmann
    ("ignore_remove_failures", False, ht.TBool,
1350 45d4c81c Michael Hanselmann
     "Whether to ignore failures while removing instances"),
1351 45d4c81c Michael Hanselmann
    ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1352 45d4c81c Michael Hanselmann
     "Export mode"),
1353 45d4c81c Michael Hanselmann
    ("x509_key_name", None, ht.TOr(ht.TList, ht.TNone),
1354 45d4c81c Michael Hanselmann
     "Name of X509 key (remote export only)"),
1355 45d4c81c Michael Hanselmann
    ("destination_x509_ca", None, ht.TMaybeString,
1356 45d4c81c Michael Hanselmann
     "Destination X509 CA (remote export only)"),
1357 17c3f802 Guido Trotter
    ]
1358 5c947f38 Iustin Pop
1359 0a7bed64 Michael Hanselmann
1360 ca5890ad Iustin Pop
class OpBackupRemove(OpCode):
1361 9ac99fda Guido Trotter
  """Remove an instance's export."""
1362 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1363 65e183af Michael Hanselmann
  OP_PARAMS = [
1364 65e183af Michael Hanselmann
    _PInstanceName,
1365 65e183af Michael Hanselmann
    ]
1366 5c947f38 Iustin Pop
1367 0a7bed64 Michael Hanselmann
1368 5c947f38 Iustin Pop
# Tags opcodes
1369 c6afb1ca Iustin Pop
class OpTagsGet(OpCode):
1370 5c947f38 Iustin Pop
  """Returns the tags of the given object."""
1371 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
1372 65e183af Michael Hanselmann
  OP_PARAMS = [
1373 65e183af Michael Hanselmann
    _PTagKind,
1374 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1375 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString, None),
1376 65e183af Michael Hanselmann
    ]
1377 5c947f38 Iustin Pop
1378 5c947f38 Iustin Pop
1379 715462e7 Iustin Pop
class OpTagsSearch(OpCode):
1380 73415719 Iustin Pop
  """Searches the tags in the cluster for a given pattern."""
1381 60dd1473 Iustin Pop
  OP_DSC_FIELD = "pattern"
1382 65e183af Michael Hanselmann
  OP_PARAMS = [
1383 197b323b Michael Hanselmann
    ("pattern", ht.NoDefault, ht.TNonEmptyString, None),
1384 65e183af Michael Hanselmann
    ]
1385 73415719 Iustin Pop
1386 73415719 Iustin Pop
1387 d1602edc Iustin Pop
class OpTagsSet(OpCode):
1388 f27302fa Iustin Pop
  """Add a list of tags on a given object."""
1389 65e183af Michael Hanselmann
  OP_PARAMS = [
1390 65e183af Michael Hanselmann
    _PTagKind,
1391 65e183af Michael Hanselmann
    _PTags,
1392 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1393 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString, None),
1394 65e183af Michael Hanselmann
    ]
1395 5c947f38 Iustin Pop
1396 5c947f38 Iustin Pop
1397 3f0ab95f Iustin Pop
class OpTagsDel(OpCode):
1398 f27302fa Iustin Pop
  """Remove a list of tags from a given object."""
1399 65e183af Michael Hanselmann
  OP_PARAMS = [
1400 65e183af Michael Hanselmann
    _PTagKind,
1401 65e183af Michael Hanselmann
    _PTags,
1402 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1403 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString, None),
1404 65e183af Michael Hanselmann
    ]
1405 06009e27 Iustin Pop
1406 06009e27 Iustin Pop
# Test opcodes
1407 06009e27 Iustin Pop
class OpTestDelay(OpCode):
1408 06009e27 Iustin Pop
  """Sleeps for a configured amount of time.
1409 06009e27 Iustin Pop

1410 06009e27 Iustin Pop
  This is used just for debugging and testing.
1411 06009e27 Iustin Pop

1412 06009e27 Iustin Pop
  Parameters:
1413 06009e27 Iustin Pop
    - duration: the time to sleep
1414 06009e27 Iustin Pop
    - on_master: if true, sleep on the master
1415 06009e27 Iustin Pop
    - on_nodes: list of nodes in which to sleep
1416 06009e27 Iustin Pop

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

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

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

1427 06009e27 Iustin Pop
  """
1428 60dd1473 Iustin Pop
  OP_DSC_FIELD = "duration"
1429 65e183af Michael Hanselmann
  OP_PARAMS = [
1430 197b323b Michael Hanselmann
    ("duration", ht.NoDefault, ht.TFloat, None),
1431 197b323b Michael Hanselmann
    ("on_master", True, ht.TBool, None),
1432 197b323b Michael Hanselmann
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1433 197b323b Michael Hanselmann
    ("repeat", 0, ht.TPositiveInt, None),
1434 65e183af Michael Hanselmann
    ]
1435 d61df03e Iustin Pop
1436 d61df03e Iustin Pop
1437 d61df03e Iustin Pop
class OpTestAllocator(OpCode):
1438 d61df03e Iustin Pop
  """Allocator framework testing.
1439 d61df03e Iustin Pop

1440 d61df03e Iustin Pop
  This opcode has two modes:
1441 d61df03e Iustin Pop
    - gather and return allocator input for a given mode (allocate new
1442 d61df03e Iustin Pop
      or replace secondary) and a given instance definition (direction
1443 d61df03e Iustin Pop
      'in')
1444 d61df03e Iustin Pop
    - run a selected allocator for a given operation (as above) and
1445 d61df03e Iustin Pop
      return the allocator output (direction 'out')
1446 d61df03e Iustin Pop

1447 d61df03e Iustin Pop
  """
1448 60dd1473 Iustin Pop
  OP_DSC_FIELD = "allocator"
1449 65e183af Michael Hanselmann
  OP_PARAMS = [
1450 65e183af Michael Hanselmann
    ("direction", ht.NoDefault,
1451 197b323b Michael Hanselmann
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
1452 197b323b Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
1453 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
1454 65e183af Michael Hanselmann
    ("nics", ht.NoDefault, ht.TOr(ht.TNone, ht.TListOf(
1455 fdbe29ee Michael Hanselmann
     ht.TDictOf(ht.TElemOf([constants.INIC_MAC, constants.INIC_IP, "bridge"]),
1456 45d4c81c Michael Hanselmann
                ht.TOr(ht.TNone, ht.TNonEmptyString)))), None),
1457 197b323b Michael Hanselmann
    ("disks", ht.NoDefault, ht.TOr(ht.TNone, ht.TList), None),
1458 197b323b Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString, None),
1459 197b323b Michael Hanselmann
    ("allocator", None, ht.TMaybeString, None),
1460 197b323b Michael Hanselmann
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1461 dd47a0f0 Iustin Pop
    ("memory", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1462 197b323b Michael Hanselmann
    ("vcpus", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1463 197b323b Michael Hanselmann
    ("os", None, ht.TMaybeString, None),
1464 197b323b Michael Hanselmann
    ("disk_template", None, ht.TMaybeString, None),
1465 45d4c81c Michael Hanselmann
    ("evac_nodes", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1466 45d4c81c Michael Hanselmann
     None),
1467 bee581e2 Michael Hanselmann
    ("instances", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1468 bee581e2 Michael Hanselmann
     None),
1469 60152bbe Michael Hanselmann
    ("evac_mode", None,
1470 60152bbe Michael Hanselmann
     ht.TOr(ht.TNone, ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
1471 bee581e2 Michael Hanselmann
    ("target_groups", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1472 bee581e2 Michael Hanselmann
     None),
1473 d61df03e Iustin Pop
    ]
1474 363acb1e Iustin Pop
1475 76aef8fc Michael Hanselmann
1476 b469eb4d Iustin Pop
class OpTestJqueue(OpCode):
1477 e58f87a9 Michael Hanselmann
  """Utility opcode to test some aspects of the job queue.
1478 e58f87a9 Michael Hanselmann

1479 e58f87a9 Michael Hanselmann
  """
1480 65e183af Michael Hanselmann
  OP_PARAMS = [
1481 197b323b Michael Hanselmann
    ("notify_waitlock", False, ht.TBool, None),
1482 197b323b Michael Hanselmann
    ("notify_exec", False, ht.TBool, None),
1483 197b323b Michael Hanselmann
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
1484 197b323b Michael Hanselmann
    ("fail", False, ht.TBool, None),
1485 e58f87a9 Michael Hanselmann
    ]
1486 e58f87a9 Michael Hanselmann
1487 e58f87a9 Michael Hanselmann
1488 be760ba8 Michael Hanselmann
class OpTestDummy(OpCode):
1489 be760ba8 Michael Hanselmann
  """Utility opcode used by unittests.
1490 be760ba8 Michael Hanselmann

1491 be760ba8 Michael Hanselmann
  """
1492 65e183af Michael Hanselmann
  OP_PARAMS = [
1493 197b323b Michael Hanselmann
    ("result", ht.NoDefault, ht.NoType, None),
1494 197b323b Michael Hanselmann
    ("messages", ht.NoDefault, ht.NoType, None),
1495 197b323b Michael Hanselmann
    ("fail", ht.NoDefault, ht.NoType, None),
1496 6a373640 Michael Hanselmann
    ("submit_jobs", None, ht.NoType, None),
1497 be760ba8 Michael Hanselmann
    ]
1498 687c10d9 Iustin Pop
  WITH_LU = False
1499 be760ba8 Michael Hanselmann
1500 be760ba8 Michael Hanselmann
1501 dbc96028 Michael Hanselmann
def _GetOpList():
1502 dbc96028 Michael Hanselmann
  """Returns list of all defined opcodes.
1503 dbc96028 Michael Hanselmann

1504 dbc96028 Michael Hanselmann
  Does not eliminate duplicates by C{OP_ID}.
1505 dbc96028 Michael Hanselmann

1506 dbc96028 Michael Hanselmann
  """
1507 dbc96028 Michael Hanselmann
  return [v for v in globals().values()
1508 dbc96028 Michael Hanselmann
          if (isinstance(v, type) and issubclass(v, OpCode) and
1509 687c10d9 Iustin Pop
              hasattr(v, "OP_ID") and v is not OpCode)]
1510 dbc96028 Michael Hanselmann
1511 dbc96028 Michael Hanselmann
1512 dbc96028 Michael Hanselmann
OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())