Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 57106b74

History | View | Annotate | Download (49 kB)

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

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

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

30 a8083063 Iustin Pop
"""
31 a8083063 Iustin Pop
32 a8083063 Iustin Pop
# this are practically structures, so disable the message about too
33 a8083063 Iustin Pop
# few public methods:
34 a8083063 Iustin Pop
# pylint: disable-msg=R0903
35 a8083063 Iustin Pop
36 1cbef6d8 Michael Hanselmann
import logging
37 ff0d18e6 Iustin Pop
import re
38 1cbef6d8 Michael Hanselmann
39 eb62069e Iustin Pop
from ganeti import compat
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 57106b74 Michael Hanselmann
_PVerbose = ("verbose", False, ht.TBool, "Verbose mode")
132 57106b74 Michael Hanselmann
133 57106b74 Michael Hanselmann
# Parameters for cluster verification
134 57106b74 Michael Hanselmann
_PDebugSimulateErrors = ("debug_simulate_errors", False, ht.TBool,
135 57106b74 Michael Hanselmann
                         "Whether to simulate errors (useful for debugging)")
136 57106b74 Michael Hanselmann
_PErrorCodes = ("error_codes", False, ht.TBool, "Error codes")
137 57106b74 Michael Hanselmann
_PSkipChecks = ("skip_checks", ht.EmptyList,
138 57106b74 Michael Hanselmann
                ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)),
139 57106b74 Michael Hanselmann
                "Which checks to skip")
140 323f9095 Stephen Shirley
141 ff0d18e6 Iustin Pop
#: OP_ID conversion regular expression
142 ff0d18e6 Iustin Pop
_OPID_RE = re.compile("([a-z])([A-Z])")
143 ff0d18e6 Iustin Pop
144 8c9ee749 Michael Hanselmann
#: Utility function for L{OpClusterSetParams}
145 8c9ee749 Michael Hanselmann
_TestClusterOsList = ht.TOr(ht.TNone,
146 8c9ee749 Michael Hanselmann
  ht.TListOf(ht.TAnd(ht.TList, ht.TIsLength(2),
147 eb62069e Iustin Pop
    ht.TMap(ht.WithDesc("GetFirstItem")(compat.fst),
148 8c9ee749 Michael Hanselmann
            ht.TElemOf(constants.DDMS_VALUES)))))
149 8c9ee749 Michael Hanselmann
150 ff0d18e6 Iustin Pop
151 526a662a Michael Hanselmann
# TODO: Generate check from constants.INIC_PARAMS_TYPES
152 526a662a Michael Hanselmann
#: Utility function for testing NIC definitions
153 526a662a Michael Hanselmann
_TestNicDef = ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
154 526a662a Michael Hanselmann
                         ht.TOr(ht.TNone, ht.TNonEmptyString))
155 526a662a Michael Hanselmann
156 3ce9a5e7 Michael Hanselmann
_SUMMARY_PREFIX = {
157 3ce9a5e7 Michael Hanselmann
  "CLUSTER_": "C_",
158 3ce9a5e7 Michael Hanselmann
  "GROUP_": "G_",
159 3ce9a5e7 Michael Hanselmann
  "NODE_": "N_",
160 3ce9a5e7 Michael Hanselmann
  "INSTANCE_": "I_",
161 3ce9a5e7 Michael Hanselmann
  }
162 3ce9a5e7 Michael Hanselmann
163 b95479a5 Michael Hanselmann
#: Attribute name for dependencies
164 b95479a5 Michael Hanselmann
DEPEND_ATTR = "depends"
165 b95479a5 Michael Hanselmann
166 018ae30b Michael Hanselmann
#: Attribute name for comment
167 018ae30b Michael Hanselmann
COMMENT_ATTR = "comment"
168 018ae30b Michael Hanselmann
169 526a662a Michael Hanselmann
170 ff0d18e6 Iustin Pop
def _NameToId(name):
171 ff0d18e6 Iustin Pop
  """Convert an opcode class name to an OP_ID.
172 ff0d18e6 Iustin Pop

173 ff0d18e6 Iustin Pop
  @type name: string
174 ff0d18e6 Iustin Pop
  @param name: the class name, as OpXxxYyy
175 ff0d18e6 Iustin Pop
  @rtype: string
176 ff0d18e6 Iustin Pop
  @return: the name in the OP_XXXX_YYYY format
177 ff0d18e6 Iustin Pop

178 ff0d18e6 Iustin Pop
  """
179 ff0d18e6 Iustin Pop
  if not name.startswith("Op"):
180 ff0d18e6 Iustin Pop
    return None
181 ff0d18e6 Iustin Pop
  # Note: (?<=[a-z])(?=[A-Z]) would be ideal, since it wouldn't
182 ff0d18e6 Iustin Pop
  # consume any input, and hence we would just have all the elements
183 ff0d18e6 Iustin Pop
  # in the list, one by one; but it seems that split doesn't work on
184 ff0d18e6 Iustin Pop
  # non-consuming input, hence we have to process the input string a
185 ff0d18e6 Iustin Pop
  # bit
186 ff0d18e6 Iustin Pop
  name = _OPID_RE.sub(r"\1,\2", name)
187 ff0d18e6 Iustin Pop
  elems = name.split(",")
188 ff0d18e6 Iustin Pop
  return "_".join(n.upper() for n in elems)
189 ff0d18e6 Iustin Pop
190 65e183af Michael Hanselmann
191 65e183af Michael Hanselmann
def RequireFileStorage():
192 65e183af Michael Hanselmann
  """Checks that file storage is enabled.
193 65e183af Michael Hanselmann

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

197 65e183af Michael Hanselmann
  @raise errors.OpPrereqError: when file storage is disabled
198 65e183af Michael Hanselmann

199 65e183af Michael Hanselmann
  """
200 65e183af Michael Hanselmann
  if not constants.ENABLE_FILE_STORAGE:
201 65e183af Michael Hanselmann
    raise errors.OpPrereqError("File storage disabled at configure time",
202 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
203 65e183af Michael Hanselmann
204 65e183af Michael Hanselmann
205 4b97f902 Apollon Oikonomopoulos
def RequireSharedFileStorage():
206 4b97f902 Apollon Oikonomopoulos
  """Checks that shared file storage is enabled.
207 4b97f902 Apollon Oikonomopoulos

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

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

213 4b97f902 Apollon Oikonomopoulos
  """
214 4b97f902 Apollon Oikonomopoulos
  if not constants.ENABLE_SHARED_FILE_STORAGE:
215 4b97f902 Apollon Oikonomopoulos
    raise errors.OpPrereqError("Shared file storage disabled at"
216 4b97f902 Apollon Oikonomopoulos
                               " configure time", errors.ECODE_INVAL)
217 4b97f902 Apollon Oikonomopoulos
218 4b97f902 Apollon Oikonomopoulos
219 8c9ee749 Michael Hanselmann
@ht.WithDesc("CheckFileStorage")
220 8c9ee749 Michael Hanselmann
def _CheckFileStorage(value):
221 8c9ee749 Michael Hanselmann
  """Ensures file storage is enabled if used.
222 65e183af Michael Hanselmann

223 65e183af Michael Hanselmann
  """
224 8c9ee749 Michael Hanselmann
  if value == constants.DT_FILE:
225 65e183af Michael Hanselmann
    RequireFileStorage()
226 4b97f902 Apollon Oikonomopoulos
  elif value == constants.DT_SHARED_FILE:
227 4b97f902 Apollon Oikonomopoulos
    RequireSharedFileStorage()
228 65e183af Michael Hanselmann
  return True
229 65e183af Michael Hanselmann
230 65e183af Michael Hanselmann
231 8c9ee749 Michael Hanselmann
_CheckDiskTemplate = ht.TAnd(ht.TElemOf(constants.DISK_TEMPLATES),
232 8c9ee749 Michael Hanselmann
                             _CheckFileStorage)
233 8c9ee749 Michael Hanselmann
234 8c9ee749 Michael Hanselmann
235 65e183af Michael Hanselmann
def _CheckStorageType(storage_type):
236 65e183af Michael Hanselmann
  """Ensure a given storage type is valid.
237 65e183af Michael Hanselmann

238 65e183af Michael Hanselmann
  """
239 65e183af Michael Hanselmann
  if storage_type not in constants.VALID_STORAGE_TYPES:
240 65e183af Michael Hanselmann
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
241 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
242 65e183af Michael Hanselmann
  if storage_type == constants.ST_FILE:
243 65e183af Michael Hanselmann
    RequireFileStorage()
244 65e183af Michael Hanselmann
  return True
245 65e183af Michael Hanselmann
246 65e183af Michael Hanselmann
247 65e183af Michael Hanselmann
#: Storage type parameter
248 45d4c81c Michael Hanselmann
_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
249 45d4c81c Michael Hanselmann
                 "Storage type")
250 65e183af Michael Hanselmann
251 65e183af Michael Hanselmann
252 65e183af Michael Hanselmann
class _AutoOpParamSlots(type):
253 65e183af Michael Hanselmann
  """Meta class for opcode definitions.
254 65e183af Michael Hanselmann

255 65e183af Michael Hanselmann
  """
256 65e183af Michael Hanselmann
  def __new__(mcs, name, bases, attrs):
257 65e183af Michael Hanselmann
    """Called when a class should be created.
258 65e183af Michael Hanselmann

259 65e183af Michael Hanselmann
    @param mcs: The meta class
260 65e183af Michael Hanselmann
    @param name: Name of created class
261 65e183af Michael Hanselmann
    @param bases: Base classes
262 65e183af Michael Hanselmann
    @type attrs: dict
263 65e183af Michael Hanselmann
    @param attrs: Class attributes
264 65e183af Michael Hanselmann

265 65e183af Michael Hanselmann
    """
266 65e183af Michael Hanselmann
    assert "__slots__" not in attrs, \
267 65e183af Michael Hanselmann
      "Class '%s' defines __slots__ when it should use OP_PARAMS" % name
268 e89a9021 Iustin Pop
    assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
269 ff0d18e6 Iustin Pop
270 e89a9021 Iustin Pop
    attrs["OP_ID"] = _NameToId(name)
271 65e183af Michael Hanselmann
272 65e183af Michael Hanselmann
    # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
273 65e183af Michael Hanselmann
    params = attrs.setdefault("OP_PARAMS", [])
274 65e183af Michael Hanselmann
275 65e183af Michael Hanselmann
    # Use parameter names as slots
276 197b323b Michael Hanselmann
    slots = [pname for (pname, _, _, _) in params]
277 65e183af Michael Hanselmann
278 65e183af Michael Hanselmann
    assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
279 65e183af Michael Hanselmann
      "Class '%s' uses unknown field in OP_DSC_FIELD" % name
280 65e183af Michael Hanselmann
281 65e183af Michael Hanselmann
    attrs["__slots__"] = slots
282 65e183af Michael Hanselmann
283 65e183af Michael Hanselmann
    return type.__new__(mcs, name, bases, attrs)
284 65e183af Michael Hanselmann
285 df458e0b Iustin Pop
286 0e46916d Iustin Pop
class BaseOpCode(object):
287 df458e0b Iustin Pop
  """A simple serializable object.
288 df458e0b Iustin Pop

289 0e46916d Iustin Pop
  This object serves as a parent class for OpCode without any custom
290 0e46916d Iustin Pop
  field handling.
291 0e46916d Iustin Pop

292 df458e0b Iustin Pop
  """
293 e89a9021 Iustin Pop
  # pylint: disable-msg=E1101
294 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
295 65e183af Michael Hanselmann
  __metaclass__ = _AutoOpParamSlots
296 65e183af Michael Hanselmann
297 a8083063 Iustin Pop
  def __init__(self, **kwargs):
298 a7399f66 Iustin Pop
    """Constructor for BaseOpCode.
299 a7399f66 Iustin Pop

300 a7399f66 Iustin Pop
    The constructor takes only keyword arguments and will set
301 a7399f66 Iustin Pop
    attributes on this object based on the passed arguments. As such,
302 a7399f66 Iustin Pop
    it means that you should not pass arguments which are not in the
303 a7399f66 Iustin Pop
    __slots__ attribute for this class.
304 a7399f66 Iustin Pop

305 a7399f66 Iustin Pop
    """
306 adf385c7 Iustin Pop
    slots = self._all_slots()
307 a8083063 Iustin Pop
    for key in kwargs:
308 adf385c7 Iustin Pop
      if key not in slots:
309 df458e0b Iustin Pop
        raise TypeError("Object %s doesn't support the parameter '%s'" %
310 3ecf6786 Iustin Pop
                        (self.__class__.__name__, key))
311 a8083063 Iustin Pop
      setattr(self, key, kwargs[key])
312 a8083063 Iustin Pop
313 df458e0b Iustin Pop
  def __getstate__(self):
314 a7399f66 Iustin Pop
    """Generic serializer.
315 a7399f66 Iustin Pop

316 a7399f66 Iustin Pop
    This method just returns the contents of the instance as a
317 a7399f66 Iustin Pop
    dictionary.
318 a7399f66 Iustin Pop

319 a7399f66 Iustin Pop
    @rtype:  C{dict}
320 a7399f66 Iustin Pop
    @return: the instance attributes and their values
321 a7399f66 Iustin Pop

322 a7399f66 Iustin Pop
    """
323 df458e0b Iustin Pop
    state = {}
324 adf385c7 Iustin Pop
    for name in self._all_slots():
325 df458e0b Iustin Pop
      if hasattr(self, name):
326 df458e0b Iustin Pop
        state[name] = getattr(self, name)
327 df458e0b Iustin Pop
    return state
328 df458e0b Iustin Pop
329 df458e0b Iustin Pop
  def __setstate__(self, state):
330 a7399f66 Iustin Pop
    """Generic unserializer.
331 a7399f66 Iustin Pop

332 a7399f66 Iustin Pop
    This method just restores from the serialized state the attributes
333 a7399f66 Iustin Pop
    of the current instance.
334 a7399f66 Iustin Pop

335 a7399f66 Iustin Pop
    @param state: the serialized opcode data
336 a7399f66 Iustin Pop
    @type state:  C{dict}
337 a7399f66 Iustin Pop

338 a7399f66 Iustin Pop
    """
339 df458e0b Iustin Pop
    if not isinstance(state, dict):
340 df458e0b Iustin Pop
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
341 df458e0b Iustin Pop
                       type(state))
342 df458e0b Iustin Pop
343 adf385c7 Iustin Pop
    for name in self._all_slots():
344 44db3a6f Iustin Pop
      if name not in state and hasattr(self, name):
345 df458e0b Iustin Pop
        delattr(self, name)
346 df458e0b Iustin Pop
347 df458e0b Iustin Pop
    for name in state:
348 df458e0b Iustin Pop
      setattr(self, name, state[name])
349 df458e0b Iustin Pop
350 adf385c7 Iustin Pop
  @classmethod
351 adf385c7 Iustin Pop
  def _all_slots(cls):
352 adf385c7 Iustin Pop
    """Compute the list of all declared slots for a class.
353 adf385c7 Iustin Pop

354 adf385c7 Iustin Pop
    """
355 adf385c7 Iustin Pop
    slots = []
356 adf385c7 Iustin Pop
    for parent in cls.__mro__:
357 adf385c7 Iustin Pop
      slots.extend(getattr(parent, "__slots__", []))
358 adf385c7 Iustin Pop
    return slots
359 adf385c7 Iustin Pop
360 65e183af Michael Hanselmann
  @classmethod
361 65e183af Michael Hanselmann
  def GetAllParams(cls):
362 65e183af Michael Hanselmann
    """Compute list of all parameters for an opcode.
363 65e183af Michael Hanselmann

364 65e183af Michael Hanselmann
    """
365 65e183af Michael Hanselmann
    slots = []
366 65e183af Michael Hanselmann
    for parent in cls.__mro__:
367 65e183af Michael Hanselmann
      slots.extend(getattr(parent, "OP_PARAMS", []))
368 65e183af Michael Hanselmann
    return slots
369 65e183af Michael Hanselmann
370 1cbef6d8 Michael Hanselmann
  def Validate(self, set_defaults):
371 1cbef6d8 Michael Hanselmann
    """Validate opcode parameters, optionally setting default values.
372 1cbef6d8 Michael Hanselmann

373 1cbef6d8 Michael Hanselmann
    @type set_defaults: bool
374 1cbef6d8 Michael Hanselmann
    @param set_defaults: Whether to set default values
375 1cbef6d8 Michael Hanselmann
    @raise errors.OpPrereqError: When a parameter value doesn't match
376 1cbef6d8 Michael Hanselmann
                                 requirements
377 1cbef6d8 Michael Hanselmann

378 1cbef6d8 Michael Hanselmann
    """
379 197b323b Michael Hanselmann
    for (attr_name, default, test, _) in self.GetAllParams():
380 1cbef6d8 Michael Hanselmann
      assert test == ht.NoType or callable(test)
381 1cbef6d8 Michael Hanselmann
382 1cbef6d8 Michael Hanselmann
      if not hasattr(self, attr_name):
383 1cbef6d8 Michael Hanselmann
        if default == ht.NoDefault:
384 1cbef6d8 Michael Hanselmann
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
385 1cbef6d8 Michael Hanselmann
                                     (self.OP_ID, attr_name),
386 1cbef6d8 Michael Hanselmann
                                     errors.ECODE_INVAL)
387 1cbef6d8 Michael Hanselmann
        elif set_defaults:
388 1cbef6d8 Michael Hanselmann
          if callable(default):
389 1cbef6d8 Michael Hanselmann
            dval = default()
390 1cbef6d8 Michael Hanselmann
          else:
391 1cbef6d8 Michael Hanselmann
            dval = default
392 1cbef6d8 Michael Hanselmann
          setattr(self, attr_name, dval)
393 1cbef6d8 Michael Hanselmann
394 1cbef6d8 Michael Hanselmann
      if test == ht.NoType:
395 1cbef6d8 Michael Hanselmann
        # no tests here
396 1cbef6d8 Michael Hanselmann
        continue
397 1cbef6d8 Michael Hanselmann
398 1cbef6d8 Michael Hanselmann
      if set_defaults or hasattr(self, attr_name):
399 1cbef6d8 Michael Hanselmann
        attr_val = getattr(self, attr_name)
400 1cbef6d8 Michael Hanselmann
        if not test(attr_val):
401 1cbef6d8 Michael Hanselmann
          logging.error("OpCode %s, parameter %s, has invalid type %s/value %s",
402 1cbef6d8 Michael Hanselmann
                        self.OP_ID, attr_name, type(attr_val), attr_val)
403 1cbef6d8 Michael Hanselmann
          raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
404 1cbef6d8 Michael Hanselmann
                                     (self.OP_ID, attr_name),
405 1cbef6d8 Michael Hanselmann
                                     errors.ECODE_INVAL)
406 1cbef6d8 Michael Hanselmann
407 df458e0b Iustin Pop
408 b247c6fc Michael Hanselmann
def _BuildJobDepCheck(relative):
409 b247c6fc Michael Hanselmann
  """Builds check for job dependencies (L{DEPEND_ATTR}).
410 b247c6fc Michael Hanselmann

411 b247c6fc Michael Hanselmann
  @type relative: bool
412 b247c6fc Michael Hanselmann
  @param relative: Whether to accept relative job IDs (negative)
413 b247c6fc Michael Hanselmann
  @rtype: callable
414 b247c6fc Michael Hanselmann

415 b247c6fc Michael Hanselmann
  """
416 b247c6fc Michael Hanselmann
  if relative:
417 b247c6fc Michael Hanselmann
    job_id = ht.TOr(ht.TJobId, ht.TRelativeJobId)
418 b247c6fc Michael Hanselmann
  else:
419 b247c6fc Michael Hanselmann
    job_id = ht.TJobId
420 b247c6fc Michael Hanselmann
421 b247c6fc Michael Hanselmann
  job_dep = \
422 b247c6fc Michael Hanselmann
    ht.TAnd(ht.TIsLength(2),
423 b247c6fc Michael Hanselmann
            ht.TItems([job_id,
424 b247c6fc Michael Hanselmann
                       ht.TListOf(ht.TElemOf(constants.JOBS_FINALIZED))]))
425 b247c6fc Michael Hanselmann
426 b247c6fc Michael Hanselmann
  return ht.TOr(ht.TNone, ht.TListOf(job_dep))
427 b247c6fc Michael Hanselmann
428 b247c6fc Michael Hanselmann
429 b247c6fc Michael Hanselmann
TNoRelativeJobDependencies = _BuildJobDepCheck(False)
430 b247c6fc Michael Hanselmann
431 1ce03fb1 Michael Hanselmann
#: List of submission status and job ID as returned by C{SubmitManyJobs}
432 1ce03fb1 Michael Hanselmann
TJobIdList = ht.TListOf(ht.TItems([ht.TBool, ht.TOr(ht.TString, ht.TJobId)]))
433 1ce03fb1 Michael Hanselmann
434 b247c6fc Michael Hanselmann
435 0e46916d Iustin Pop
class OpCode(BaseOpCode):
436 a7399f66 Iustin Pop
  """Abstract OpCode.
437 a7399f66 Iustin Pop

438 a7399f66 Iustin Pop
  This is the root of the actual OpCode hierarchy. All clases derived
439 a7399f66 Iustin Pop
  from this class should override OP_ID.
440 a7399f66 Iustin Pop

441 a7399f66 Iustin Pop
  @cvar OP_ID: The ID of this opcode. This should be unique amongst all
442 20777413 Iustin Pop
               children of this class.
443 bde8f481 Adeodato Simo
  @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
444 bde8f481 Adeodato Simo
                      string returned by Summary(); see the docstring of that
445 bde8f481 Adeodato Simo
                      method for details).
446 65e183af Michael Hanselmann
  @cvar OP_PARAMS: List of opcode attributes, the default values they should
447 65e183af Michael Hanselmann
                   get if not already defined, and types they must match.
448 1ce03fb1 Michael Hanselmann
  @cvar OP_RESULT: Callable to verify opcode result
449 687c10d9 Iustin Pop
  @cvar WITH_LU: Boolean that specifies whether this should be included in
450 687c10d9 Iustin Pop
      mcpu's dispatch table
451 20777413 Iustin Pop
  @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
452 20777413 Iustin Pop
                 the check steps
453 8f5c488d Michael Hanselmann
  @ivar priority: Opcode priority for queue
454 a7399f66 Iustin Pop

455 a7399f66 Iustin Pop
  """
456 e89a9021 Iustin Pop
  # pylint: disable-msg=E1101
457 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
458 687c10d9 Iustin Pop
  WITH_LU = True
459 65e183af Michael Hanselmann
  OP_PARAMS = [
460 45d4c81c Michael Hanselmann
    ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
461 45d4c81c Michael Hanselmann
    ("debug_level", None, ht.TOr(ht.TNone, ht.TPositiveInt), "Debug level"),
462 65e183af Michael Hanselmann
    ("priority", constants.OP_PRIO_DEFAULT,
463 45d4c81c Michael Hanselmann
     ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
464 b247c6fc Michael Hanselmann
    (DEPEND_ATTR, None, _BuildJobDepCheck(True),
465 b247c6fc Michael Hanselmann
     "Job dependencies; if used through ``SubmitManyJobs`` relative (negative)"
466 b247c6fc Michael Hanselmann
     " job IDs can be used"),
467 018ae30b Michael Hanselmann
    (COMMENT_ATTR, None, ht.TMaybeString,
468 018ae30b Michael Hanselmann
     "Comment describing the purpose of the opcode"),
469 65e183af Michael Hanselmann
    ]
470 1ce03fb1 Michael Hanselmann
  OP_RESULT = None
471 df458e0b Iustin Pop
472 df458e0b Iustin Pop
  def __getstate__(self):
473 df458e0b Iustin Pop
    """Specialized getstate for opcodes.
474 df458e0b Iustin Pop

475 a7399f66 Iustin Pop
    This method adds to the state dictionary the OP_ID of the class,
476 a7399f66 Iustin Pop
    so that on unload we can identify the correct class for
477 a7399f66 Iustin Pop
    instantiating the opcode.
478 a7399f66 Iustin Pop

479 a7399f66 Iustin Pop
    @rtype:   C{dict}
480 a7399f66 Iustin Pop
    @return:  the state as a dictionary
481 a7399f66 Iustin Pop

482 df458e0b Iustin Pop
    """
483 0e46916d Iustin Pop
    data = BaseOpCode.__getstate__(self)
484 df458e0b Iustin Pop
    data["OP_ID"] = self.OP_ID
485 df458e0b Iustin Pop
    return data
486 df458e0b Iustin Pop
487 df458e0b Iustin Pop
  @classmethod
488 00abdc96 Iustin Pop
  def LoadOpCode(cls, data):
489 df458e0b Iustin Pop
    """Generic load opcode method.
490 df458e0b Iustin Pop

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

495 a7399f66 Iustin Pop
    @type data:  C{dict}
496 a7399f66 Iustin Pop
    @param data: the serialized opcode
497 a7399f66 Iustin Pop

498 df458e0b Iustin Pop
    """
499 df458e0b Iustin Pop
    if not isinstance(data, dict):
500 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
501 df458e0b Iustin Pop
    if "OP_ID" not in data:
502 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
503 df458e0b Iustin Pop
    op_id = data["OP_ID"]
504 df458e0b Iustin Pop
    op_class = None
505 363acb1e Iustin Pop
    if op_id in OP_MAPPING:
506 363acb1e Iustin Pop
      op_class = OP_MAPPING[op_id]
507 363acb1e Iustin Pop
    else:
508 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
509 df458e0b Iustin Pop
                       op_id)
510 df458e0b Iustin Pop
    op = op_class()
511 df458e0b Iustin Pop
    new_data = data.copy()
512 df458e0b Iustin Pop
    del new_data["OP_ID"]
513 df458e0b Iustin Pop
    op.__setstate__(new_data)
514 df458e0b Iustin Pop
    return op
515 df458e0b Iustin Pop
516 60dd1473 Iustin Pop
  def Summary(self):
517 60dd1473 Iustin Pop
    """Generates a summary description of this opcode.
518 60dd1473 Iustin Pop

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

525 60dd1473 Iustin Pop
    """
526 ff0d18e6 Iustin Pop
    assert self.OP_ID is not None and len(self.OP_ID) > 3
527 60dd1473 Iustin Pop
    # all OP_ID start with OP_, we remove that
528 60dd1473 Iustin Pop
    txt = self.OP_ID[3:]
529 60dd1473 Iustin Pop
    field_name = getattr(self, "OP_DSC_FIELD", None)
530 60dd1473 Iustin Pop
    if field_name:
531 60dd1473 Iustin Pop
      field_value = getattr(self, field_name, None)
532 bc8bbda1 Iustin Pop
      if isinstance(field_value, (list, tuple)):
533 bc8bbda1 Iustin Pop
        field_value = ",".join(str(i) for i in field_value)
534 60dd1473 Iustin Pop
      txt = "%s(%s)" % (txt, field_value)
535 60dd1473 Iustin Pop
    return txt
536 60dd1473 Iustin Pop
537 3ce9a5e7 Michael Hanselmann
  def TinySummary(self):
538 3ce9a5e7 Michael Hanselmann
    """Generates a compact summary description of the opcode.
539 3ce9a5e7 Michael Hanselmann

540 3ce9a5e7 Michael Hanselmann
    """
541 3ce9a5e7 Michael Hanselmann
    assert self.OP_ID.startswith("OP_")
542 3ce9a5e7 Michael Hanselmann
543 3ce9a5e7 Michael Hanselmann
    text = self.OP_ID[3:]
544 3ce9a5e7 Michael Hanselmann
545 3ce9a5e7 Michael Hanselmann
    for (prefix, supplement) in _SUMMARY_PREFIX.items():
546 3ce9a5e7 Michael Hanselmann
      if text.startswith(prefix):
547 3ce9a5e7 Michael Hanselmann
        return supplement + text[len(prefix):]
548 3ce9a5e7 Michael Hanselmann
549 3ce9a5e7 Michael Hanselmann
    return text
550 3ce9a5e7 Michael Hanselmann
551 a8083063 Iustin Pop
552 afee0879 Iustin Pop
# cluster opcodes
553 afee0879 Iustin Pop
554 bc84ffa7 Iustin Pop
class OpClusterPostInit(OpCode):
555 b5f5fae9 Luca Bigliardi
  """Post cluster initialization.
556 b5f5fae9 Luca Bigliardi

557 b5f5fae9 Luca Bigliardi
  This opcode does not touch the cluster at all. Its purpose is to run hooks
558 b5f5fae9 Luca Bigliardi
  after the cluster has been initialized.
559 b5f5fae9 Luca Bigliardi

560 b5f5fae9 Luca Bigliardi
  """
561 b5f5fae9 Luca Bigliardi
562 b5f5fae9 Luca Bigliardi
563 c6d43e9e Iustin Pop
class OpClusterDestroy(OpCode):
564 a7399f66 Iustin Pop
  """Destroy the cluster.
565 a7399f66 Iustin Pop

566 a7399f66 Iustin Pop
  This opcode has no other parameters. All the state is irreversibly
567 a7399f66 Iustin Pop
  lost after the execution of this opcode.
568 a7399f66 Iustin Pop

569 a7399f66 Iustin Pop
  """
570 a8083063 Iustin Pop
571 a8083063 Iustin Pop
572 a2f7ab92 Iustin Pop
class OpClusterQuery(OpCode):
573 fdc267f4 Iustin Pop
  """Query cluster information."""
574 a8083063 Iustin Pop
575 a8083063 Iustin Pop
576 bf93ae69 Adeodato Simo
class OpClusterVerifyConfig(OpCode):
577 bf93ae69 Adeodato Simo
  """Verify the cluster config.
578 bf93ae69 Adeodato Simo

579 bf93ae69 Adeodato Simo
  """
580 bf93ae69 Adeodato Simo
  OP_PARAMS = [
581 57106b74 Michael Hanselmann
    _PDebugSimulateErrors,
582 57106b74 Michael Hanselmann
    _PErrorCodes,
583 57106b74 Michael Hanselmann
    _PVerbose,
584 bf93ae69 Adeodato Simo
    ]
585 bf93ae69 Adeodato Simo
586 bf93ae69 Adeodato Simo
587 bf93ae69 Adeodato Simo
class OpClusterVerifyGroup(OpCode):
588 bf93ae69 Adeodato Simo
  """Run verify on a node group from the cluster.
589 a7399f66 Iustin Pop

590 a7399f66 Iustin Pop
  @type skip_checks: C{list}
591 a7399f66 Iustin Pop
  @ivar skip_checks: steps to be skipped from the verify process; this
592 a7399f66 Iustin Pop
                     needs to be a subset of
593 a7399f66 Iustin Pop
                     L{constants.VERIFY_OPTIONAL_CHECKS}; currently
594 a7399f66 Iustin Pop
                     only L{constants.VERIFY_NPLUSONE_MEM} can be passed
595 a7399f66 Iustin Pop

596 a7399f66 Iustin Pop
  """
597 bf93ae69 Adeodato Simo
  OP_DSC_FIELD = "group_name"
598 65e183af Michael Hanselmann
  OP_PARAMS = [
599 57106b74 Michael Hanselmann
    _PGroupName,
600 57106b74 Michael Hanselmann
    _PDebugSimulateErrors,
601 57106b74 Michael Hanselmann
    _PErrorCodes,
602 57106b74 Michael Hanselmann
    _PSkipChecks,
603 57106b74 Michael Hanselmann
    _PVerbose,
604 65e183af Michael Hanselmann
    ]
605 a8083063 Iustin Pop
606 a8083063 Iustin Pop
607 bd8210a7 Iustin Pop
class OpClusterVerifyDisks(OpCode):
608 150e978f Iustin Pop
  """Verify the cluster disks.
609 150e978f Iustin Pop

610 ae1a845c Michael Hanselmann
  """
611 1ce03fb1 Michael Hanselmann
  OP_RESULT = ht.TStrictDict(True, True, {
612 1ce03fb1 Michael Hanselmann
    constants.JOB_IDS_KEY: TJobIdList,
613 1ce03fb1 Michael Hanselmann
    })
614 ae1a845c Michael Hanselmann
615 ae1a845c Michael Hanselmann
616 ae1a845c Michael Hanselmann
class OpGroupVerifyDisks(OpCode):
617 ae1a845c Michael Hanselmann
  """Verifies the status of all disks in a node group.
618 150e978f Iustin Pop

619 ae1a845c Michael Hanselmann
  Result: a tuple of three elements:
620 ae1a845c Michael Hanselmann
    - dict of node names with issues (values: error msg)
621 150e978f Iustin Pop
    - list of instances with degraded disks (that should be activated)
622 b63ed789 Iustin Pop
    - dict of instances with missing logical volumes (values: (node, vol)
623 b63ed789 Iustin Pop
      pairs with details about the missing volumes)
624 150e978f Iustin Pop

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

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

633 150e978f Iustin Pop
  """
634 ae1a845c Michael Hanselmann
  OP_DSC_FIELD = "group_name"
635 ae1a845c Michael Hanselmann
  OP_PARAMS = [
636 ae1a845c Michael Hanselmann
    _PGroupName,
637 ae1a845c Michael Hanselmann
    ]
638 1ce03fb1 Michael Hanselmann
  OP_RESULT = \
639 1ce03fb1 Michael Hanselmann
    ht.TAnd(ht.TIsLength(3),
640 1ce03fb1 Michael Hanselmann
            ht.TItems([ht.TDictOf(ht.TString, ht.TString),
641 1ce03fb1 Michael Hanselmann
                       ht.TListOf(ht.TString),
642 1ce03fb1 Michael Hanselmann
                       ht.TDictOf(ht.TString, ht.TListOf(ht.TString))]))
643 150e978f Iustin Pop
644 150e978f Iustin Pop
645 5d01aca3 Iustin Pop
class OpClusterRepairDiskSizes(OpCode):
646 60975797 Iustin Pop
  """Verify the disk sizes of the instances and fixes configuration
647 60975797 Iustin Pop
  mimatches.
648 60975797 Iustin Pop

649 60975797 Iustin Pop
  Parameters: optional instances list, in case we want to restrict the
650 60975797 Iustin Pop
  checks to only a subset of the instances.
651 60975797 Iustin Pop

652 60975797 Iustin Pop
  Result: a list of tuples, (instance, disk, new-size) for changed
653 60975797 Iustin Pop
  configurations.
654 60975797 Iustin Pop

655 60975797 Iustin Pop
  In normal operation, the list should be empty.
656 60975797 Iustin Pop

657 60975797 Iustin Pop
  @type instances: list
658 60975797 Iustin Pop
  @ivar instances: the list of instances to check, or empty for all instances
659 60975797 Iustin Pop

660 60975797 Iustin Pop
  """
661 65e183af Michael Hanselmann
  OP_PARAMS = [
662 197b323b Michael Hanselmann
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
663 65e183af Michael Hanselmann
    ]
664 60975797 Iustin Pop
665 60975797 Iustin Pop
666 2f093ea0 Iustin Pop
class OpClusterConfigQuery(OpCode):
667 ae5849b5 Michael Hanselmann
  """Query cluster configuration values."""
668 65e183af Michael Hanselmann
  OP_PARAMS = [
669 65e183af Michael Hanselmann
    _POutputFields
670 65e183af Michael Hanselmann
    ]
671 a8083063 Iustin Pop
672 a8083063 Iustin Pop
673 e126df25 Iustin Pop
class OpClusterRename(OpCode):
674 a7399f66 Iustin Pop
  """Rename the cluster.
675 a7399f66 Iustin Pop

676 a7399f66 Iustin Pop
  @type name: C{str}
677 a7399f66 Iustin Pop
  @ivar name: The new name of the cluster. The name and/or the master IP
678 a7399f66 Iustin Pop
              address will be changed to match the new name and its IP
679 a7399f66 Iustin Pop
              address.
680 a7399f66 Iustin Pop

681 a7399f66 Iustin Pop
  """
682 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
683 65e183af Michael Hanselmann
  OP_PARAMS = [
684 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
685 65e183af Michael Hanselmann
    ]
686 07bd8a51 Iustin Pop
687 07bd8a51 Iustin Pop
688 a6682fdc Iustin Pop
class OpClusterSetParams(OpCode):
689 a7399f66 Iustin Pop
  """Change the parameters of the cluster.
690 a7399f66 Iustin Pop

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

694 a7399f66 Iustin Pop
  """
695 65e183af Michael Hanselmann
  OP_PARAMS = [
696 45d4c81c Michael Hanselmann
    ("vg_name", None, ht.TMaybeString, "Volume group name"),
697 65e183af Michael Hanselmann
    ("enabled_hypervisors", None,
698 65e183af Michael Hanselmann
     ht.TOr(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)), ht.TTrue),
699 45d4c81c Michael Hanselmann
            ht.TNone),
700 45d4c81c Michael Hanselmann
     "List of enabled hypervisors"),
701 65e183af Michael Hanselmann
    ("hvparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
702 45d4c81c Michael Hanselmann
                              ht.TNone),
703 45d4c81c Michael Hanselmann
     "Cluster-wide hypervisor parameter defaults, hypervisor-dependent"),
704 45d4c81c Michael Hanselmann
    ("beparams", None, ht.TOr(ht.TDict, ht.TNone),
705 45d4c81c Michael Hanselmann
     "Cluster-wide backend parameter defaults"),
706 65e183af Michael Hanselmann
    ("os_hvp", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
707 45d4c81c Michael Hanselmann
                            ht.TNone),
708 45d4c81c Michael Hanselmann
     "Cluster-wide per-OS hypervisor parameter defaults"),
709 65e183af Michael Hanselmann
    ("osparams", None, ht.TOr(ht.TDictOf(ht.TNonEmptyString, ht.TDict),
710 45d4c81c Michael Hanselmann
                              ht.TNone),
711 45d4c81c Michael Hanselmann
     "Cluster-wide OS parameter defaults"),
712 197b323b Michael Hanselmann
    ("candidate_pool_size", None, ht.TOr(ht.TStrictPositiveInt, ht.TNone),
713 45d4c81c Michael Hanselmann
     "Master candidate pool size"),
714 45d4c81c Michael Hanselmann
    ("uid_pool", None, ht.NoType,
715 45d4c81c Michael Hanselmann
     "Set UID pool, must be list of lists describing UID ranges (two items,"
716 45d4c81c Michael Hanselmann
     " start and end inclusive)"),
717 45d4c81c Michael Hanselmann
    ("add_uids", None, ht.NoType,
718 45d4c81c Michael Hanselmann
     "Extend UID pool, must be list of lists describing UID ranges (two"
719 45d4c81c Michael Hanselmann
     " items, start and end inclusive) to be added"),
720 45d4c81c Michael Hanselmann
    ("remove_uids", None, ht.NoType,
721 45d4c81c Michael Hanselmann
     "Shrink UID pool, must be list of lists describing UID ranges (two"
722 45d4c81c Michael Hanselmann
     " items, start and end inclusive) to be removed"),
723 45d4c81c Michael Hanselmann
    ("maintain_node_health", None, ht.TMaybeBool,
724 45d4c81c Michael Hanselmann
     "Whether to automatically maintain node health"),
725 45d4c81c Michael Hanselmann
    ("prealloc_wipe_disks", None, ht.TMaybeBool,
726 45d4c81c Michael Hanselmann
     "Whether to wipe disks before allocating them to instances"),
727 45d4c81c Michael Hanselmann
    ("nicparams", None, ht.TMaybeDict, "Cluster-wide NIC parameter defaults"),
728 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Cluster-wide node parameter defaults"),
729 45d4c81c Michael Hanselmann
    ("drbd_helper", None, ht.TOr(ht.TString, ht.TNone), "DRBD helper program"),
730 45d4c81c Michael Hanselmann
    ("default_iallocator", None, ht.TOr(ht.TString, ht.TNone),
731 45d4c81c Michael Hanselmann
     "Default iallocator for cluster"),
732 45d4c81c Michael Hanselmann
    ("master_netdev", None, ht.TOr(ht.TString, ht.TNone),
733 45d4c81c Michael Hanselmann
     "Master network device"),
734 45d4c81c Michael Hanselmann
    ("reserved_lvs", None, ht.TOr(ht.TListOf(ht.TNonEmptyString), ht.TNone),
735 45d4c81c Michael Hanselmann
     "List of reserved LVs"),
736 45d4c81c Michael Hanselmann
    ("hidden_os", None, _TestClusterOsList,
737 45d4c81c Michael Hanselmann
     "Modify list of hidden operating systems. Each modification must have"
738 45d4c81c Michael Hanselmann
     " two items, the operation and the OS name. The operation can be"
739 45d4c81c Michael Hanselmann
     " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
740 45d4c81c Michael Hanselmann
    ("blacklisted_os", None, _TestClusterOsList,
741 45d4c81c Michael Hanselmann
     "Modify list of blacklisted operating systems. Each modification must have"
742 45d4c81c Michael Hanselmann
     " two items, the operation and the OS name. The operation can be"
743 45d4c81c Michael Hanselmann
     " ``%s`` or ``%s``." % (constants.DDM_ADD, constants.DDM_REMOVE)),
744 4b7735f9 Iustin Pop
    ]
745 12515db7 Manuel Franceschini
746 12515db7 Manuel Franceschini
747 d1240007 Iustin Pop
class OpClusterRedistConf(OpCode):
748 afee0879 Iustin Pop
  """Force a full push of the cluster configuration.
749 afee0879 Iustin Pop

750 afee0879 Iustin Pop
  """
751 afee0879 Iustin Pop
752 83f72637 Michael Hanselmann
753 83f72637 Michael Hanselmann
class OpQuery(OpCode):
754 83f72637 Michael Hanselmann
  """Query for resources/items.
755 83f72637 Michael Hanselmann

756 abd66bf8 Michael Hanselmann
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
757 83f72637 Michael Hanselmann
  @ivar fields: List of fields to retrieve
758 83f72637 Michael Hanselmann
  @ivar filter: Query filter
759 83f72637 Michael Hanselmann

760 83f72637 Michael Hanselmann
  """
761 e50d8412 Michael Hanselmann
  OP_DSC_FIELD = "what"
762 65e183af Michael Hanselmann
  OP_PARAMS = [
763 8e7078e0 Michael Hanselmann
    _PQueryWhat,
764 45d4c81c Michael Hanselmann
    ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
765 45d4c81c Michael Hanselmann
     "Requested fields"),
766 b107fe05 Michael Hanselmann
    ("filter", None, ht.TOr(ht.TNone, ht.TListOf),
767 45d4c81c Michael Hanselmann
     "Query filter"),
768 83f72637 Michael Hanselmann
    ]
769 83f72637 Michael Hanselmann
770 83f72637 Michael Hanselmann
771 83f72637 Michael Hanselmann
class OpQueryFields(OpCode):
772 83f72637 Michael Hanselmann
  """Query for available resource/item fields.
773 83f72637 Michael Hanselmann

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

777 83f72637 Michael Hanselmann
  """
778 e50d8412 Michael Hanselmann
  OP_DSC_FIELD = "what"
779 65e183af Michael Hanselmann
  OP_PARAMS = [
780 8e7078e0 Michael Hanselmann
    _PQueryWhat,
781 8e7078e0 Michael Hanselmann
    ("fields", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
782 8e7078e0 Michael Hanselmann
     "Requested fields; if not given, all are returned"),
783 83f72637 Michael Hanselmann
    ]
784 83f72637 Michael Hanselmann
785 83f72637 Michael Hanselmann
786 792af3ad Renรฉ Nussbaumer
class OpOobCommand(OpCode):
787 eb64da59 Renรฉ Nussbaumer
  """Interact with OOB."""
788 65e183af Michael Hanselmann
  OP_PARAMS = [
789 c4ec0755 Renรฉ Nussbaumer
    ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
790 c4ec0755 Renรฉ Nussbaumer
     "List of nodes to run the OOB command against"),
791 c4ec0755 Renรฉ Nussbaumer
    ("command", None, ht.TElemOf(constants.OOB_COMMANDS),
792 c4ec0755 Renรฉ Nussbaumer
     "OOB command to be run"),
793 c4ec0755 Renรฉ Nussbaumer
    ("timeout", constants.OOB_TIMEOUT, ht.TInt,
794 c4ec0755 Renรฉ Nussbaumer
     "Timeout before the OOB helper will be terminated"),
795 c4ec0755 Renรฉ Nussbaumer
    ("ignore_status", False, ht.TBool,
796 c4ec0755 Renรฉ Nussbaumer
     "Ignores the node offline status for power off"),
797 beff3779 Renรฉ Nussbaumer
    ("power_delay", constants.OOB_POWER_DELAY, ht.TPositiveFloat,
798 beff3779 Renรฉ Nussbaumer
     "Time in seconds to wait between powering on nodes"),
799 eb64da59 Renรฉ Nussbaumer
    ]
800 eb64da59 Renรฉ Nussbaumer
801 eb64da59 Renรฉ Nussbaumer
802 07bd8a51 Iustin Pop
# node opcodes
803 07bd8a51 Iustin Pop
804 73d565a3 Iustin Pop
class OpNodeRemove(OpCode):
805 a7399f66 Iustin Pop
  """Remove a node.
806 a7399f66 Iustin Pop

807 a7399f66 Iustin Pop
  @type node_name: C{str}
808 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to remove. If the node still has
809 a7399f66 Iustin Pop
                   instances on it, the operation will fail.
810 a7399f66 Iustin Pop

811 a7399f66 Iustin Pop
  """
812 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
813 65e183af Michael Hanselmann
  OP_PARAMS = [
814 65e183af Michael Hanselmann
    _PNodeName,
815 65e183af Michael Hanselmann
    ]
816 a8083063 Iustin Pop
817 a8083063 Iustin Pop
818 d817d49f Iustin Pop
class OpNodeAdd(OpCode):
819 a7399f66 Iustin Pop
  """Add a node to the cluster.
820 a7399f66 Iustin Pop

821 a7399f66 Iustin Pop
  @type node_name: C{str}
822 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to add. This can be a short name,
823 a7399f66 Iustin Pop
                   but it will be expanded to the FQDN.
824 a7399f66 Iustin Pop
  @type primary_ip: IP address
825 a7399f66 Iustin Pop
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
826 a7399f66 Iustin Pop
                    opcode is submitted, but will be filled during the node
827 a7399f66 Iustin Pop
                    add (so it will be visible in the job query).
828 a7399f66 Iustin Pop
  @type secondary_ip: IP address
829 a7399f66 Iustin Pop
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
830 a7399f66 Iustin Pop
                      if the cluster has been initialized in 'dual-network'
831 a7399f66 Iustin Pop
                      mode, otherwise it must not be given.
832 a7399f66 Iustin Pop
  @type readd: C{bool}
833 a7399f66 Iustin Pop
  @ivar readd: Whether to re-add an existing node to the cluster. If
834 a7399f66 Iustin Pop
               this is not passed, then the operation will abort if the node
835 a7399f66 Iustin Pop
               name is already in the cluster; use this parameter to 'repair'
836 a7399f66 Iustin Pop
               a node that had its configuration broken, or was reinstalled
837 a7399f66 Iustin Pop
               without removal from the cluster.
838 f936c153 Iustin Pop
  @type group: C{str}
839 f936c153 Iustin Pop
  @ivar group: The node group to which this node will belong.
840 fd3d37b6 Iustin Pop
  @type vm_capable: C{bool}
841 fd3d37b6 Iustin Pop
  @ivar vm_capable: The vm_capable node attribute
842 fd3d37b6 Iustin Pop
  @type master_capable: C{bool}
843 fd3d37b6 Iustin Pop
  @ivar master_capable: The master_capable node attribute
844 a7399f66 Iustin Pop

845 a7399f66 Iustin Pop
  """
846 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
847 65e183af Michael Hanselmann
  OP_PARAMS = [
848 65e183af Michael Hanselmann
    _PNodeName,
849 45d4c81c Michael Hanselmann
    ("primary_ip", None, ht.NoType, "Primary IP address"),
850 45d4c81c Michael Hanselmann
    ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
851 45d4c81c Michael Hanselmann
    ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
852 45d4c81c Michael Hanselmann
    ("group", None, ht.TMaybeString, "Initial node group"),
853 45d4c81c Michael Hanselmann
    ("master_capable", None, ht.TMaybeBool,
854 45d4c81c Michael Hanselmann
     "Whether node can become master or master candidate"),
855 45d4c81c Michael Hanselmann
    ("vm_capable", None, ht.TMaybeBool,
856 45d4c81c Michael Hanselmann
     "Whether node can host instances"),
857 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Node parameters"),
858 65e183af Michael Hanselmann
    ]
859 a8083063 Iustin Pop
860 a8083063 Iustin Pop
861 2237687b Iustin Pop
class OpNodeQuery(OpCode):
862 a8083063 Iustin Pop
  """Compute the list of nodes."""
863 65e183af Michael Hanselmann
  OP_PARAMS = [
864 65e183af Michael Hanselmann
    _POutputFields,
865 45d4c81c Michael Hanselmann
    _PUseLocking,
866 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
867 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
868 65e183af Michael Hanselmann
    ]
869 a8083063 Iustin Pop
870 a8083063 Iustin Pop
871 8ed55bfd Iustin Pop
class OpNodeQueryvols(OpCode):
872 dcb93971 Michael Hanselmann
  """Get list of volumes on node."""
873 65e183af Michael Hanselmann
  OP_PARAMS = [
874 65e183af Michael Hanselmann
    _POutputFields,
875 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
876 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
877 65e183af Michael Hanselmann
    ]
878 dcb93971 Michael Hanselmann
879 dcb93971 Michael Hanselmann
880 ad8d0595 Iustin Pop
class OpNodeQueryStorage(OpCode):
881 9e5442ce Michael Hanselmann
  """Get information on storage for node(s)."""
882 65e183af Michael Hanselmann
  OP_PARAMS = [
883 65e183af Michael Hanselmann
    _POutputFields,
884 65e183af Michael Hanselmann
    _PStorageType,
885 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
886 45d4c81c Michael Hanselmann
    ("name", None, ht.TMaybeString, "Storage name"),
887 9e5442ce Michael Hanselmann
    ]
888 9e5442ce Michael Hanselmann
889 9e5442ce Michael Hanselmann
890 2cee4077 Iustin Pop
class OpNodeModifyStorage(OpCode):
891 099c52ad Iustin Pop
  """Modifies the properies of a storage unit"""
892 65e183af Michael Hanselmann
  OP_PARAMS = [
893 65e183af Michael Hanselmann
    _PNodeName,
894 65e183af Michael Hanselmann
    _PStorageType,
895 45d4c81c Michael Hanselmann
    _PStorageName,
896 45d4c81c Michael Hanselmann
    ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
897 efb8da02 Michael Hanselmann
    ]
898 efb8da02 Michael Hanselmann
899 efb8da02 Michael Hanselmann
900 76aef8fc Michael Hanselmann
class OpRepairNodeStorage(OpCode):
901 76aef8fc Michael Hanselmann
  """Repairs the volume group on a node."""
902 76aef8fc Michael Hanselmann
  OP_DSC_FIELD = "node_name"
903 65e183af Michael Hanselmann
  OP_PARAMS = [
904 65e183af Michael Hanselmann
    _PNodeName,
905 65e183af Michael Hanselmann
    _PStorageType,
906 45d4c81c Michael Hanselmann
    _PStorageName,
907 45d4c81c Michael Hanselmann
    _PIgnoreConsistency,
908 76aef8fc Michael Hanselmann
    ]
909 76aef8fc Michael Hanselmann
910 76aef8fc Michael Hanselmann
911 f13973c4 Iustin Pop
class OpNodeSetParams(OpCode):
912 b31c8676 Iustin Pop
  """Change the parameters of a node."""
913 b31c8676 Iustin Pop
  OP_DSC_FIELD = "node_name"
914 65e183af Michael Hanselmann
  OP_PARAMS = [
915 65e183af Michael Hanselmann
    _PNodeName,
916 65e183af Michael Hanselmann
    _PForce,
917 45d4c81c Michael Hanselmann
    ("master_candidate", None, ht.TMaybeBool,
918 45d4c81c Michael Hanselmann
     "Whether the node should become a master candidate"),
919 45d4c81c Michael Hanselmann
    ("offline", None, ht.TMaybeBool,
920 45d4c81c Michael Hanselmann
     "Whether the node should be marked as offline"),
921 45d4c81c Michael Hanselmann
    ("drained", None, ht.TMaybeBool,
922 45d4c81c Michael Hanselmann
     "Whether the node should be marked as drained"),
923 45d4c81c Michael Hanselmann
    ("auto_promote", False, ht.TBool,
924 45d4c81c Michael Hanselmann
     "Whether node(s) should be promoted to master candidate if necessary"),
925 45d4c81c Michael Hanselmann
    ("master_capable", None, ht.TMaybeBool,
926 45d4c81c Michael Hanselmann
     "Denote whether node can become master or master candidate"),
927 45d4c81c Michael Hanselmann
    ("vm_capable", None, ht.TMaybeBool,
928 45d4c81c Michael Hanselmann
     "Denote whether node can host instances"),
929 45d4c81c Michael Hanselmann
    ("secondary_ip", None, ht.TMaybeString,
930 45d4c81c Michael Hanselmann
     "Change node's secondary IP address"),
931 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
932 45d4c81c Michael Hanselmann
    ("powered", None, ht.TMaybeBool,
933 45d4c81c Michael Hanselmann
     "Whether the node should be marked as powered"),
934 b31c8676 Iustin Pop
    ]
935 b31c8676 Iustin Pop
936 f5118ade Iustin Pop
937 e0d4735f Iustin Pop
class OpNodePowercycle(OpCode):
938 f5118ade Iustin Pop
  """Tries to powercycle a node."""
939 f5118ade Iustin Pop
  OP_DSC_FIELD = "node_name"
940 65e183af Michael Hanselmann
  OP_PARAMS = [
941 65e183af Michael Hanselmann
    _PNodeName,
942 65e183af Michael Hanselmann
    _PForce,
943 f5118ade Iustin Pop
    ]
944 f5118ade Iustin Pop
945 7ffc5a86 Michael Hanselmann
946 5b14a488 Iustin Pop
class OpNodeMigrate(OpCode):
947 80cb875c Michael Hanselmann
  """Migrate all instances from a node."""
948 80cb875c Michael Hanselmann
  OP_DSC_FIELD = "node_name"
949 65e183af Michael Hanselmann
  OP_PARAMS = [
950 65e183af Michael Hanselmann
    _PNodeName,
951 65e183af Michael Hanselmann
    _PMigrationMode,
952 65e183af Michael Hanselmann
    _PMigrationLive,
953 f8fa4175 Michael Hanselmann
    _PMigrationTargetNode,
954 8eb34306 Apollon Oikonomopoulos
    ("iallocator", None, ht.TMaybeString,
955 8eb34306 Apollon Oikonomopoulos
     "Iallocator for deciding the target node for shared-storage instances"),
956 80cb875c Michael Hanselmann
    ]
957 80cb875c Michael Hanselmann
958 80cb875c Michael Hanselmann
959 e1f23243 Michael Hanselmann
class OpNodeEvacuate(OpCode):
960 e1f23243 Michael Hanselmann
  """Evacuate instances off a number of nodes."""
961 e1f23243 Michael Hanselmann
  OP_DSC_FIELD = "node_name"
962 e1f23243 Michael Hanselmann
  OP_PARAMS = [
963 e1f23243 Michael Hanselmann
    _PEarlyRelease,
964 e1f23243 Michael Hanselmann
    _PNodeName,
965 e1f23243 Michael Hanselmann
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
966 e1f23243 Michael Hanselmann
    ("iallocator", None, ht.TMaybeString, "Iallocator for computing solution"),
967 e1f23243 Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES),
968 e1f23243 Michael Hanselmann
     "Node evacuation mode"),
969 e1f23243 Michael Hanselmann
    ]
970 e1f23243 Michael Hanselmann
971 e1f23243 Michael Hanselmann
972 a8083063 Iustin Pop
# instance opcodes
973 a8083063 Iustin Pop
974 e1530b10 Iustin Pop
class OpInstanceCreate(OpCode):
975 9bf56d77 Michael Hanselmann
  """Create an instance.
976 9bf56d77 Michael Hanselmann

977 9bf56d77 Michael Hanselmann
  @ivar instance_name: Instance name
978 9bf56d77 Michael Hanselmann
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
979 9bf56d77 Michael Hanselmann
  @ivar source_handshake: Signed handshake from source (remote import only)
980 9bf56d77 Michael Hanselmann
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
981 9bf56d77 Michael Hanselmann
  @ivar source_instance_name: Previous name of instance (remote import only)
982 dae91d02 Michael Hanselmann
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
983 dae91d02 Michael Hanselmann
    (remote import only)
984 9bf56d77 Michael Hanselmann

985 9bf56d77 Michael Hanselmann
  """
986 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
987 65e183af Michael Hanselmann
  OP_PARAMS = [
988 65e183af Michael Hanselmann
    _PInstanceName,
989 45d4c81c Michael Hanselmann
    _PForceVariant,
990 45d4c81c Michael Hanselmann
    _PWaitForSync,
991 45d4c81c Michael Hanselmann
    _PNameCheck,
992 45d4c81c Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict, "Backend parameters for instance"),
993 526a662a Michael Hanselmann
    ("disks", ht.NoDefault,
994 526a662a Michael Hanselmann
     # TODO: Generate check from constants.IDISK_PARAMS_TYPES
995 526a662a Michael Hanselmann
     ht.TListOf(ht.TDictOf(ht.TElemOf(constants.IDISK_PARAMS),
996 526a662a Michael Hanselmann
                           ht.TOr(ht.TNonEmptyString, ht.TInt))),
997 526a662a Michael Hanselmann
     "Disk descriptions, for example ``[{\"%s\": 100}, {\"%s\": 5}]``;"
998 526a662a Michael Hanselmann
     " each disk definition must contain a ``%s`` value and"
999 526a662a Michael Hanselmann
     " can contain an optional ``%s`` value denoting the disk access mode"
1000 526a662a Michael Hanselmann
     " (%s)" %
1001 526a662a Michael Hanselmann
     (constants.IDISK_SIZE, constants.IDISK_SIZE, constants.IDISK_SIZE,
1002 526a662a Michael Hanselmann
      constants.IDISK_MODE,
1003 526a662a Michael Hanselmann
      " or ".join("``%s``" % i for i in sorted(constants.DISK_ACCESS_SET)))),
1004 45d4c81c Michael Hanselmann
    ("disk_template", ht.NoDefault, _CheckDiskTemplate, "Disk template"),
1005 45d4c81c Michael Hanselmann
    ("file_driver", None, ht.TOr(ht.TNone, ht.TElemOf(constants.FILE_DRIVER)),
1006 45d4c81c Michael Hanselmann
     "Driver for file-backed disks"),
1007 45d4c81c Michael Hanselmann
    ("file_storage_dir", None, ht.TMaybeString,
1008 45d4c81c Michael Hanselmann
     "Directory for storing file-backed disks"),
1009 45d4c81c Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict,
1010 45d4c81c Michael Hanselmann
     "Hypervisor parameters for instance, hypervisor-dependent"),
1011 45d4c81c Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString, "Hypervisor"),
1012 45d4c81c Michael Hanselmann
    ("iallocator", None, ht.TMaybeString,
1013 45d4c81c Michael Hanselmann
     "Iallocator for deciding which node(s) to use"),
1014 45d4c81c Michael Hanselmann
    ("identify_defaults", False, ht.TBool,
1015 45d4c81c Michael Hanselmann
     "Reset instance parameters to default if equal"),
1016 45d4c81c Michael Hanselmann
    ("ip_check", True, ht.TBool, _PIpCheckDoc),
1017 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.INSTANCE_CREATE_MODES),
1018 45d4c81c Michael Hanselmann
     "Instance creation mode"),
1019 526a662a Michael Hanselmann
    ("nics", ht.NoDefault, ht.TListOf(_TestNicDef),
1020 526a662a Michael Hanselmann
     "List of NIC (network interface) definitions, for example"
1021 526a662a Michael Hanselmann
     " ``[{}, {}, {\"%s\": \"198.51.100.4\"}]``; each NIC definition can"
1022 526a662a Michael Hanselmann
     " contain the optional values %s" %
1023 526a662a Michael Hanselmann
     (constants.INIC_IP,
1024 526a662a Michael Hanselmann
      ", ".join("``%s``" % i for i in sorted(constants.INIC_PARAMS)))),
1025 45d4c81c Michael Hanselmann
    ("no_install", None, ht.TMaybeBool,
1026 45d4c81c Michael Hanselmann
     "Do not install the OS (will disable automatic start)"),
1027 45d4c81c Michael Hanselmann
    ("osparams", ht.EmptyDict, ht.TDict, "OS parameters for instance"),
1028 45d4c81c Michael Hanselmann
    ("os_type", None, ht.TMaybeString, "Operating system"),
1029 45d4c81c Michael Hanselmann
    ("pnode", None, ht.TMaybeString, "Primary node"),
1030 45d4c81c Michael Hanselmann
    ("snode", None, ht.TMaybeString, "Secondary node"),
1031 45d4c81c Michael Hanselmann
    ("source_handshake", None, ht.TOr(ht.TList, ht.TNone),
1032 45d4c81c Michael Hanselmann
     "Signed handshake from source (remote import only)"),
1033 45d4c81c Michael Hanselmann
    ("source_instance_name", None, ht.TMaybeString,
1034 45d4c81c Michael Hanselmann
     "Source instance name (remote import only)"),
1035 65e183af Michael Hanselmann
    ("source_shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
1036 526a662a Michael Hanselmann
     ht.TPositiveInt,
1037 526a662a Michael Hanselmann
     "How long source instance was given to shut down (remote import only)"),
1038 45d4c81c Michael Hanselmann
    ("source_x509_ca", None, ht.TMaybeString,
1039 45d4c81c Michael Hanselmann
     "Source X509 CA in PEM format (remote import only)"),
1040 45d4c81c Michael Hanselmann
    ("src_node", None, ht.TMaybeString, "Source node for import"),
1041 45d4c81c Michael Hanselmann
    ("src_path", None, ht.TMaybeString, "Source directory for import"),
1042 45d4c81c Michael Hanselmann
    ("start", True, ht.TBool, "Whether to start instance after creation"),
1043 720f56c8 Apollon Oikonomopoulos
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Instance tags"),
1044 3b6d8c9b Iustin Pop
    ]
1045 a8083063 Iustin Pop
1046 a8083063 Iustin Pop
1047 5073fd8f Iustin Pop
class OpInstanceReinstall(OpCode):
1048 fdc267f4 Iustin Pop
  """Reinstall an instance's OS."""
1049 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1050 65e183af Michael Hanselmann
  OP_PARAMS = [
1051 65e183af Michael Hanselmann
    _PInstanceName,
1052 45d4c81c Michael Hanselmann
    _PForceVariant,
1053 45d4c81c Michael Hanselmann
    ("os_type", None, ht.TMaybeString, "Instance operating system"),
1054 45d4c81c Michael Hanselmann
    ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
1055 65e183af Michael Hanselmann
    ]
1056 fe7b0351 Michael Hanselmann
1057 fe7b0351 Michael Hanselmann
1058 3cd2d4b1 Iustin Pop
class OpInstanceRemove(OpCode):
1059 a8083063 Iustin Pop
  """Remove an instance."""
1060 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1061 65e183af Michael Hanselmann
  OP_PARAMS = [
1062 65e183af Michael Hanselmann
    _PInstanceName,
1063 65e183af Michael Hanselmann
    _PShutdownTimeout,
1064 45d4c81c Michael Hanselmann
    ("ignore_failures", False, ht.TBool,
1065 45d4c81c Michael Hanselmann
     "Whether to ignore failures during removal"),
1066 fc1baca9 Michael Hanselmann
    ]
1067 a8083063 Iustin Pop
1068 a8083063 Iustin Pop
1069 5659e2e2 Iustin Pop
class OpInstanceRename(OpCode):
1070 decd5f45 Iustin Pop
  """Rename an instance."""
1071 65e183af Michael Hanselmann
  OP_PARAMS = [
1072 65e183af Michael Hanselmann
    _PInstanceName,
1073 45d4c81c Michael Hanselmann
    _PNameCheck,
1074 45d4c81c Michael Hanselmann
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
1075 45d4c81c Michael Hanselmann
    ("ip_check", False, ht.TBool, _PIpCheckDoc),
1076 4f05fd3b Iustin Pop
    ]
1077 decd5f45 Iustin Pop
1078 decd5f45 Iustin Pop
1079 c873d91c Iustin Pop
class OpInstanceStartup(OpCode):
1080 fdc267f4 Iustin Pop
  """Startup an instance."""
1081 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1082 65e183af Michael Hanselmann
  OP_PARAMS = [
1083 65e183af Michael Hanselmann
    _PInstanceName,
1084 65e183af Michael Hanselmann
    _PForce,
1085 65e183af Michael Hanselmann
    _PIgnoreOfflineNodes,
1086 45d4c81c Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict,
1087 45d4c81c Michael Hanselmann
     "Temporary hypervisor parameters, hypervisor-dependent"),
1088 45d4c81c Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
1089 9b64e486 Iustin Pop
    _PNoRemember,
1090 323f9095 Stephen Shirley
    _PStartupPaused,
1091 4f05fd3b Iustin Pop
    ]
1092 a8083063 Iustin Pop
1093 a8083063 Iustin Pop
1094 ee3e37a7 Iustin Pop
class OpInstanceShutdown(OpCode):
1095 fdc267f4 Iustin Pop
  """Shutdown an instance."""
1096 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1097 65e183af Michael Hanselmann
  OP_PARAMS = [
1098 65e183af Michael Hanselmann
    _PInstanceName,
1099 65e183af Michael Hanselmann
    _PIgnoreOfflineNodes,
1100 45d4c81c Michael Hanselmann
    ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TPositiveInt,
1101 45d4c81c Michael Hanselmann
     "How long to wait for instance to shut down"),
1102 9b64e486 Iustin Pop
    _PNoRemember,
1103 b44bd844 Michael Hanselmann
    ]
1104 a8083063 Iustin Pop
1105 a8083063 Iustin Pop
1106 90ab1a95 Iustin Pop
class OpInstanceReboot(OpCode):
1107 bf6929a2 Alexander Schreiber
  """Reboot an instance."""
1108 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1109 65e183af Michael Hanselmann
  OP_PARAMS = [
1110 65e183af Michael Hanselmann
    _PInstanceName,
1111 65e183af Michael Hanselmann
    _PShutdownTimeout,
1112 45d4c81c Michael Hanselmann
    ("ignore_secondaries", False, ht.TBool,
1113 45d4c81c Michael Hanselmann
     "Whether to start the instance even if secondary disks are failing"),
1114 45d4c81c Michael Hanselmann
    ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
1115 45d4c81c Michael Hanselmann
     "How to reboot instance"),
1116 4f05fd3b Iustin Pop
    ]
1117 bf6929a2 Alexander Schreiber
1118 bf6929a2 Alexander Schreiber
1119 668f755d Iustin Pop
class OpInstanceReplaceDisks(OpCode):
1120 fdc267f4 Iustin Pop
  """Replace the disks of an instance."""
1121 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1122 65e183af Michael Hanselmann
  OP_PARAMS = [
1123 65e183af Michael Hanselmann
    _PInstanceName,
1124 e1f23243 Michael Hanselmann
    _PEarlyRelease,
1125 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1126 45d4c81c Michael Hanselmann
     "Replacement mode"),
1127 45d4c81c Michael Hanselmann
    ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt),
1128 45d4c81c Michael Hanselmann
     "Disk indexes"),
1129 45d4c81c Michael Hanselmann
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1130 45d4c81c Michael Hanselmann
    ("iallocator", None, ht.TMaybeString,
1131 45d4c81c Michael Hanselmann
     "Iallocator for deciding new secondary node"),
1132 4f05fd3b Iustin Pop
    ]
1133 a8083063 Iustin Pop
1134 a8083063 Iustin Pop
1135 019dbee1 Iustin Pop
class OpInstanceFailover(OpCode):
1136 a8083063 Iustin Pop
  """Failover an instance."""
1137 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1138 65e183af Michael Hanselmann
  OP_PARAMS = [
1139 65e183af Michael Hanselmann
    _PInstanceName,
1140 65e183af Michael Hanselmann
    _PShutdownTimeout,
1141 45d4c81c Michael Hanselmann
    _PIgnoreConsistency,
1142 f8fa4175 Michael Hanselmann
    _PMigrationTargetNode,
1143 8eb34306 Apollon Oikonomopoulos
    ("iallocator", None, ht.TMaybeString,
1144 8eb34306 Apollon Oikonomopoulos
     "Iallocator for deciding the target node for shared-storage instances"),
1145 17c3f802 Guido Trotter
    ]
1146 a8083063 Iustin Pop
1147 a8083063 Iustin Pop
1148 75c866c2 Iustin Pop
class OpInstanceMigrate(OpCode):
1149 53c776b5 Iustin Pop
  """Migrate an instance.
1150 53c776b5 Iustin Pop

1151 53c776b5 Iustin Pop
  This migrates (without shutting down an instance) to its secondary
1152 53c776b5 Iustin Pop
  node.
1153 53c776b5 Iustin Pop

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

1157 53c776b5 Iustin Pop
  """
1158 ee69c97f Iustin Pop
  OP_DSC_FIELD = "instance_name"
1159 65e183af Michael Hanselmann
  OP_PARAMS = [
1160 65e183af Michael Hanselmann
    _PInstanceName,
1161 65e183af Michael Hanselmann
    _PMigrationMode,
1162 65e183af Michael Hanselmann
    _PMigrationLive,
1163 f8fa4175 Michael Hanselmann
    _PMigrationTargetNode,
1164 45d4c81c Michael Hanselmann
    ("cleanup", False, ht.TBool,
1165 45d4c81c Michael Hanselmann
     "Whether a previously failed migration should be cleaned up"),
1166 8eb34306 Apollon Oikonomopoulos
    ("iallocator", None, ht.TMaybeString,
1167 8eb34306 Apollon Oikonomopoulos
     "Iallocator for deciding the target node for shared-storage instances"),
1168 d5cafd31 Renรฉ Nussbaumer
    ("allow_failover", False, ht.TBool,
1169 d5cafd31 Renรฉ Nussbaumer
     "Whether we can fallback to failover if migration is not possible"),
1170 65e183af Michael Hanselmann
    ]
1171 53c776b5 Iustin Pop
1172 53c776b5 Iustin Pop
1173 0091b480 Iustin Pop
class OpInstanceMove(OpCode):
1174 313bcead Iustin Pop
  """Move an instance.
1175 313bcead Iustin Pop

1176 313bcead Iustin Pop
  This move (with shutting down an instance and data copying) to an
1177 313bcead Iustin Pop
  arbitrary node.
1178 313bcead Iustin Pop

1179 313bcead Iustin Pop
  @ivar instance_name: the name of the instance
1180 313bcead Iustin Pop
  @ivar target_node: the destination node
1181 313bcead Iustin Pop

1182 313bcead Iustin Pop
  """
1183 313bcead Iustin Pop
  OP_DSC_FIELD = "instance_name"
1184 65e183af Michael Hanselmann
  OP_PARAMS = [
1185 65e183af Michael Hanselmann
    _PInstanceName,
1186 65e183af Michael Hanselmann
    _PShutdownTimeout,
1187 45d4c81c Michael Hanselmann
    ("target_node", ht.NoDefault, ht.TNonEmptyString, "Target node"),
1188 bb851c63 Iustin Pop
    _PIgnoreConsistency,
1189 154b9580 Balazs Lecz
    ]
1190 313bcead Iustin Pop
1191 313bcead Iustin Pop
1192 cc0dec7b Iustin Pop
class OpInstanceConsole(OpCode):
1193 fdc267f4 Iustin Pop
  """Connect to an instance's console."""
1194 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1195 65e183af Michael Hanselmann
  OP_PARAMS = [
1196 65e183af Michael Hanselmann
    _PInstanceName
1197 65e183af Michael Hanselmann
    ]
1198 a8083063 Iustin Pop
1199 a8083063 Iustin Pop
1200 83f5d475 Iustin Pop
class OpInstanceActivateDisks(OpCode):
1201 fdc267f4 Iustin Pop
  """Activate an instance's disks."""
1202 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1203 65e183af Michael Hanselmann
  OP_PARAMS = [
1204 65e183af Michael Hanselmann
    _PInstanceName,
1205 45d4c81c Michael Hanselmann
    ("ignore_size", False, ht.TBool, "Whether to ignore recorded size"),
1206 65e183af Michael Hanselmann
    ]
1207 a8083063 Iustin Pop
1208 a8083063 Iustin Pop
1209 e176281f Iustin Pop
class OpInstanceDeactivateDisks(OpCode):
1210 fdc267f4 Iustin Pop
  """Deactivate an instance's disks."""
1211 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1212 65e183af Michael Hanselmann
  OP_PARAMS = [
1213 c9c41373 Iustin Pop
    _PInstanceName,
1214 c9c41373 Iustin Pop
    _PForce,
1215 65e183af Michael Hanselmann
    ]
1216 a8083063 Iustin Pop
1217 a8083063 Iustin Pop
1218 6b273e78 Iustin Pop
class OpInstanceRecreateDisks(OpCode):
1219 bd315bfa Iustin Pop
  """Deactivate an instance's disks."""
1220 bd315bfa Iustin Pop
  OP_DSC_FIELD = "instance_name"
1221 65e183af Michael Hanselmann
  OP_PARAMS = [
1222 65e183af Michael Hanselmann
    _PInstanceName,
1223 45d4c81c Michael Hanselmann
    ("disks", ht.EmptyList, ht.TListOf(ht.TPositiveInt),
1224 45d4c81c Michael Hanselmann
     "List of disk indexes"),
1225 93384b8c Guido Trotter
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1226 93384b8c Guido Trotter
     "New instance nodes, if relocation is desired"),
1227 65e183af Michael Hanselmann
    ]
1228 bd315bfa Iustin Pop
1229 bd315bfa Iustin Pop
1230 f2af0bec Iustin Pop
class OpInstanceQuery(OpCode):
1231 a8083063 Iustin Pop
  """Compute the list of instances."""
1232 65e183af Michael Hanselmann
  OP_PARAMS = [
1233 65e183af Michael Hanselmann
    _POutputFields,
1234 45d4c81c Michael Hanselmann
    _PUseLocking,
1235 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1236 45d4c81c Michael Hanselmann
     "Empty list to query all instances, instance names otherwise"),
1237 65e183af Michael Hanselmann
    ]
1238 a8083063 Iustin Pop
1239 a8083063 Iustin Pop
1240 dc28c4e4 Iustin Pop
class OpInstanceQueryData(OpCode):
1241 a8083063 Iustin Pop
  """Compute the run-time status of instances."""
1242 65e183af Michael Hanselmann
  OP_PARAMS = [
1243 af7b6689 Michael Hanselmann
    _PUseLocking,
1244 af7b6689 Michael Hanselmann
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1245 af7b6689 Michael Hanselmann
     "Instance names"),
1246 af7b6689 Michael Hanselmann
    ("static", False, ht.TBool,
1247 af7b6689 Michael Hanselmann
     "Whether to only return configuration data without querying"
1248 af7b6689 Michael Hanselmann
     " nodes"),
1249 65e183af Michael Hanselmann
    ]
1250 a8083063 Iustin Pop
1251 a8083063 Iustin Pop
1252 9a3cc7ae Iustin Pop
class OpInstanceSetParams(OpCode):
1253 a8083063 Iustin Pop
  """Change the parameters of an instance."""
1254 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1255 65e183af Michael Hanselmann
  OP_PARAMS = [
1256 65e183af Michael Hanselmann
    _PInstanceName,
1257 65e183af Michael Hanselmann
    _PForce,
1258 45d4c81c Michael Hanselmann
    _PForceVariant,
1259 526a662a Michael Hanselmann
    # TODO: Use _TestNicDef
1260 45d4c81c Michael Hanselmann
    ("nics", ht.EmptyList, ht.TList,
1261 45d4c81c Michael Hanselmann
     "List of NIC changes. Each item is of the form ``(op, settings)``."
1262 45d4c81c Michael Hanselmann
     " ``op`` can be ``%s`` to add a new NIC with the specified settings,"
1263 45d4c81c Michael Hanselmann
     " ``%s`` to remove the last NIC or a number to modify the settings"
1264 45d4c81c Michael Hanselmann
     " of the NIC with that index." %
1265 45d4c81c Michael Hanselmann
     (constants.DDM_ADD, constants.DDM_REMOVE)),
1266 45d4c81c Michael Hanselmann
    ("disks", ht.EmptyList, ht.TList, "List of disk changes. See ``nics``."),
1267 45d4c81c Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict, "Per-instance backend parameters"),
1268 45d4c81c Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict,
1269 45d4c81c Michael Hanselmann
     "Per-instance hypervisor parameters, hypervisor-dependent"),
1270 45d4c81c Michael Hanselmann
    ("disk_template", None, ht.TOr(ht.TNone, _CheckDiskTemplate),
1271 45d4c81c Michael Hanselmann
     "Disk template for instance"),
1272 45d4c81c Michael Hanselmann
    ("remote_node", None, ht.TMaybeString,
1273 45d4c81c Michael Hanselmann
     "Secondary node (used when changing disk template)"),
1274 45d4c81c Michael Hanselmann
    ("os_name", None, ht.TMaybeString,
1275 45d4c81c Michael Hanselmann
     "Change instance's OS name. Does not reinstall the instance."),
1276 45d4c81c Michael Hanselmann
    ("osparams", None, ht.TMaybeDict, "Per-instance OS parameters"),
1277 93384b8c Guido Trotter
    ("wait_for_sync", True, ht.TBool,
1278 93384b8c Guido Trotter
     "Whether to wait for the disk to synchronize, when changing template"),
1279 973d7867 Iustin Pop
    ]
1280 a8083063 Iustin Pop
1281 a8083063 Iustin Pop
1282 60472d29 Iustin Pop
class OpInstanceGrowDisk(OpCode):
1283 8729e0d7 Iustin Pop
  """Grow a disk of an instance."""
1284 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1285 65e183af Michael Hanselmann
  OP_PARAMS = [
1286 65e183af Michael Hanselmann
    _PInstanceName,
1287 45d4c81c Michael Hanselmann
    _PWaitForSync,
1288 45d4c81c Michael Hanselmann
    ("disk", ht.NoDefault, ht.TInt, "Disk index"),
1289 45d4c81c Michael Hanselmann
    ("amount", ht.NoDefault, ht.TInt,
1290 45d4c81c Michael Hanselmann
     "Amount of disk space to add (megabytes)"),
1291 4f05fd3b Iustin Pop
    ]
1292 8729e0d7 Iustin Pop
1293 8729e0d7 Iustin Pop
1294 1aef3df8 Michael Hanselmann
class OpInstanceChangeGroup(OpCode):
1295 1aef3df8 Michael Hanselmann
  """Moves an instance to another node group."""
1296 1aef3df8 Michael Hanselmann
  OP_DSC_FIELD = "instance_name"
1297 1aef3df8 Michael Hanselmann
  OP_PARAMS = [
1298 1aef3df8 Michael Hanselmann
    _PInstanceName,
1299 1aef3df8 Michael Hanselmann
    _PEarlyRelease,
1300 1aef3df8 Michael Hanselmann
    ("iallocator", None, ht.TMaybeString, "Iallocator for computing solution"),
1301 1aef3df8 Michael Hanselmann
    ("target_groups", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1302 1aef3df8 Michael Hanselmann
     "Destination group names or UUIDs (defaults to \"all but current group\""),
1303 1aef3df8 Michael Hanselmann
    ]
1304 1aef3df8 Michael Hanselmann
1305 1aef3df8 Michael Hanselmann
1306 70a6a926 Adeodato Simo
# Node group opcodes
1307 70a6a926 Adeodato Simo
1308 fabf1731 Iustin Pop
class OpGroupAdd(OpCode):
1309 b1ee5610 Adeodato Simo
  """Add a node group to the cluster."""
1310 b1ee5610 Adeodato Simo
  OP_DSC_FIELD = "group_name"
1311 65e183af Michael Hanselmann
  OP_PARAMS = [
1312 65e183af Michael Hanselmann
    _PGroupName,
1313 45d4c81c Michael Hanselmann
    _PNodeGroupAllocPolicy,
1314 45d4c81c Michael Hanselmann
    _PGroupNodeParams,
1315 483be60d Adeodato Simo
    ]
1316 b1ee5610 Adeodato Simo
1317 b1ee5610 Adeodato Simo
1318 934704ae Iustin Pop
class OpGroupAssignNodes(OpCode):
1319 96276ae7 Adeodato Simo
  """Assign nodes to a node group."""
1320 96276ae7 Adeodato Simo
  OP_DSC_FIELD = "group_name"
1321 96276ae7 Adeodato Simo
  OP_PARAMS = [
1322 96276ae7 Adeodato Simo
    _PGroupName,
1323 96276ae7 Adeodato Simo
    _PForce,
1324 45d4c81c Michael Hanselmann
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1325 45d4c81c Michael Hanselmann
     "List of nodes to assign"),
1326 96276ae7 Adeodato Simo
    ]
1327 96276ae7 Adeodato Simo
1328 96276ae7 Adeodato Simo
1329 d4d654bd Iustin Pop
class OpGroupQuery(OpCode):
1330 70a6a926 Adeodato Simo
  """Compute the list of node groups."""
1331 65e183af Michael Hanselmann
  OP_PARAMS = [
1332 65e183af Michael Hanselmann
    _POutputFields,
1333 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1334 45d4c81c Michael Hanselmann
     "Empty list to query all groups, group names otherwise"),
1335 65e183af Michael Hanselmann
    ]
1336 70a6a926 Adeodato Simo
1337 70a6a926 Adeodato Simo
1338 7cbf74f0 Iustin Pop
class OpGroupSetParams(OpCode):
1339 4da7909a Adeodato Simo
  """Change the parameters of a node group."""
1340 4da7909a Adeodato Simo
  OP_DSC_FIELD = "group_name"
1341 65e183af Michael Hanselmann
  OP_PARAMS = [
1342 65e183af Michael Hanselmann
    _PGroupName,
1343 45d4c81c Michael Hanselmann
    _PNodeGroupAllocPolicy,
1344 45d4c81c Michael Hanselmann
    _PGroupNodeParams,
1345 4da7909a Adeodato Simo
    ]
1346 4da7909a Adeodato Simo
1347 4da7909a Adeodato Simo
1348 4d1baa51 Iustin Pop
class OpGroupRemove(OpCode):
1349 94bd652a Adeodato Simo
  """Remove a node group from the cluster."""
1350 94bd652a Adeodato Simo
  OP_DSC_FIELD = "group_name"
1351 65e183af Michael Hanselmann
  OP_PARAMS = [
1352 65e183af Michael Hanselmann
    _PGroupName,
1353 65e183af Michael Hanselmann
    ]
1354 94bd652a Adeodato Simo
1355 94bd652a Adeodato Simo
1356 a8173e82 Iustin Pop
class OpGroupRename(OpCode):
1357 4fe5cf90 Adeodato Simo
  """Rename a node group in the cluster."""
1358 65e183af Michael Hanselmann
  OP_PARAMS = [
1359 12da663a Michael Hanselmann
    _PGroupName,
1360 12da663a Michael Hanselmann
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New group name"),
1361 65e183af Michael Hanselmann
    ]
1362 4fe5cf90 Adeodato Simo
1363 4fe5cf90 Adeodato Simo
1364 08f8c82c Michael Hanselmann
class OpGroupEvacuate(OpCode):
1365 08f8c82c Michael Hanselmann
  """Evacuate a node group in the cluster."""
1366 08f8c82c Michael Hanselmann
  OP_DSC_FIELD = "group_name"
1367 08f8c82c Michael Hanselmann
  OP_PARAMS = [
1368 08f8c82c Michael Hanselmann
    _PGroupName,
1369 08f8c82c Michael Hanselmann
    _PEarlyRelease,
1370 08f8c82c Michael Hanselmann
    ("iallocator", None, ht.TMaybeString, "Iallocator for computing solution"),
1371 08f8c82c Michael Hanselmann
    ("target_groups", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1372 08f8c82c Michael Hanselmann
     "Destination group names or UUIDs"),
1373 08f8c82c Michael Hanselmann
    ]
1374 08f8c82c Michael Hanselmann
1375 08f8c82c Michael Hanselmann
1376 a8083063 Iustin Pop
# OS opcodes
1377 da2d02e7 Iustin Pop
class OpOsDiagnose(OpCode):
1378 a8083063 Iustin Pop
  """Compute the list of guest operating systems."""
1379 65e183af Michael Hanselmann
  OP_PARAMS = [
1380 65e183af Michael Hanselmann
    _POutputFields,
1381 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1382 45d4c81c Michael Hanselmann
     "Which operating systems to diagnose"),
1383 65e183af Michael Hanselmann
    ]
1384 a8083063 Iustin Pop
1385 7c0d6283 Michael Hanselmann
1386 a8083063 Iustin Pop
# Exports opcodes
1387 7ca2d4d8 Iustin Pop
class OpBackupQuery(OpCode):
1388 a8083063 Iustin Pop
  """Compute the list of exported images."""
1389 65e183af Michael Hanselmann
  OP_PARAMS = [
1390 45d4c81c Michael Hanselmann
    _PUseLocking,
1391 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1392 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
1393 65e183af Michael Hanselmann
    ]
1394 a8083063 Iustin Pop
1395 7c0d6283 Michael Hanselmann
1396 71910715 Iustin Pop
class OpBackupPrepare(OpCode):
1397 1410fa8d Michael Hanselmann
  """Prepares an instance export.
1398 1410fa8d Michael Hanselmann

1399 1410fa8d Michael Hanselmann
  @ivar instance_name: Instance name
1400 1410fa8d Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1401 1410fa8d Michael Hanselmann

1402 1410fa8d Michael Hanselmann
  """
1403 1410fa8d Michael Hanselmann
  OP_DSC_FIELD = "instance_name"
1404 65e183af Michael Hanselmann
  OP_PARAMS = [
1405 65e183af Michael Hanselmann
    _PInstanceName,
1406 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1407 45d4c81c Michael Hanselmann
     "Export mode"),
1408 1410fa8d Michael Hanselmann
    ]
1409 1410fa8d Michael Hanselmann
1410 1410fa8d Michael Hanselmann
1411 4ff922a2 Iustin Pop
class OpBackupExport(OpCode):
1412 4a96f1d1 Michael Hanselmann
  """Export an instance.
1413 4a96f1d1 Michael Hanselmann

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

1420 4a96f1d1 Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1421 4a96f1d1 Michael Hanselmann
  @ivar target_node: Export destination
1422 4a96f1d1 Michael Hanselmann
  @ivar x509_key_name: X509 key to use (remote export only)
1423 4a96f1d1 Michael Hanselmann
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1424 4a96f1d1 Michael Hanselmann
                             only)
1425 4a96f1d1 Michael Hanselmann

1426 4a96f1d1 Michael Hanselmann
  """
1427 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1428 65e183af Michael Hanselmann
  OP_PARAMS = [
1429 65e183af Michael Hanselmann
    _PInstanceName,
1430 65e183af Michael Hanselmann
    _PShutdownTimeout,
1431 4a96f1d1 Michael Hanselmann
    # TODO: Rename target_node as it changes meaning for different export modes
1432 4a96f1d1 Michael Hanselmann
    # (e.g. "destination")
1433 45d4c81c Michael Hanselmann
    ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1434 45d4c81c Michael Hanselmann
     "Destination information, depends on export mode"),
1435 45d4c81c Michael Hanselmann
    ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1436 45d4c81c Michael Hanselmann
    ("remove_instance", False, ht.TBool,
1437 45d4c81c Michael Hanselmann
     "Whether to remove instance after export"),
1438 45d4c81c Michael Hanselmann
    ("ignore_remove_failures", False, ht.TBool,
1439 45d4c81c Michael Hanselmann
     "Whether to ignore failures while removing instances"),
1440 45d4c81c Michael Hanselmann
    ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1441 45d4c81c Michael Hanselmann
     "Export mode"),
1442 45d4c81c Michael Hanselmann
    ("x509_key_name", None, ht.TOr(ht.TList, ht.TNone),
1443 45d4c81c Michael Hanselmann
     "Name of X509 key (remote export only)"),
1444 45d4c81c Michael Hanselmann
    ("destination_x509_ca", None, ht.TMaybeString,
1445 45d4c81c Michael Hanselmann
     "Destination X509 CA (remote export only)"),
1446 17c3f802 Guido Trotter
    ]
1447 5c947f38 Iustin Pop
1448 0a7bed64 Michael Hanselmann
1449 ca5890ad Iustin Pop
class OpBackupRemove(OpCode):
1450 9ac99fda Guido Trotter
  """Remove an instance's export."""
1451 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1452 65e183af Michael Hanselmann
  OP_PARAMS = [
1453 65e183af Michael Hanselmann
    _PInstanceName,
1454 65e183af Michael Hanselmann
    ]
1455 5c947f38 Iustin Pop
1456 0a7bed64 Michael Hanselmann
1457 5c947f38 Iustin Pop
# Tags opcodes
1458 c6afb1ca Iustin Pop
class OpTagsGet(OpCode):
1459 5c947f38 Iustin Pop
  """Returns the tags of the given object."""
1460 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
1461 65e183af Michael Hanselmann
  OP_PARAMS = [
1462 65e183af Michael Hanselmann
    _PTagKind,
1463 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1464 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString, None),
1465 65e183af Michael Hanselmann
    ]
1466 5c947f38 Iustin Pop
1467 5c947f38 Iustin Pop
1468 715462e7 Iustin Pop
class OpTagsSearch(OpCode):
1469 73415719 Iustin Pop
  """Searches the tags in the cluster for a given pattern."""
1470 60dd1473 Iustin Pop
  OP_DSC_FIELD = "pattern"
1471 65e183af Michael Hanselmann
  OP_PARAMS = [
1472 197b323b Michael Hanselmann
    ("pattern", ht.NoDefault, ht.TNonEmptyString, None),
1473 65e183af Michael Hanselmann
    ]
1474 73415719 Iustin Pop
1475 73415719 Iustin Pop
1476 d1602edc Iustin Pop
class OpTagsSet(OpCode):
1477 f27302fa Iustin Pop
  """Add a list of tags on a given object."""
1478 65e183af Michael Hanselmann
  OP_PARAMS = [
1479 65e183af Michael Hanselmann
    _PTagKind,
1480 65e183af Michael Hanselmann
    _PTags,
1481 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1482 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString, None),
1483 65e183af Michael Hanselmann
    ]
1484 5c947f38 Iustin Pop
1485 5c947f38 Iustin Pop
1486 3f0ab95f Iustin Pop
class OpTagsDel(OpCode):
1487 f27302fa Iustin Pop
  """Remove a list of tags from a given object."""
1488 65e183af Michael Hanselmann
  OP_PARAMS = [
1489 65e183af Michael Hanselmann
    _PTagKind,
1490 65e183af Michael Hanselmann
    _PTags,
1491 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1492 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString, None),
1493 65e183af Michael Hanselmann
    ]
1494 06009e27 Iustin Pop
1495 06009e27 Iustin Pop
# Test opcodes
1496 06009e27 Iustin Pop
class OpTestDelay(OpCode):
1497 06009e27 Iustin Pop
  """Sleeps for a configured amount of time.
1498 06009e27 Iustin Pop

1499 06009e27 Iustin Pop
  This is used just for debugging and testing.
1500 06009e27 Iustin Pop

1501 06009e27 Iustin Pop
  Parameters:
1502 06009e27 Iustin Pop
    - duration: the time to sleep
1503 06009e27 Iustin Pop
    - on_master: if true, sleep on the master
1504 06009e27 Iustin Pop
    - on_nodes: list of nodes in which to sleep
1505 06009e27 Iustin Pop

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

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

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

1516 06009e27 Iustin Pop
  """
1517 60dd1473 Iustin Pop
  OP_DSC_FIELD = "duration"
1518 65e183af Michael Hanselmann
  OP_PARAMS = [
1519 b795a775 Michael Hanselmann
    ("duration", ht.NoDefault, ht.TNumber, None),
1520 197b323b Michael Hanselmann
    ("on_master", True, ht.TBool, None),
1521 197b323b Michael Hanselmann
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1522 197b323b Michael Hanselmann
    ("repeat", 0, ht.TPositiveInt, None),
1523 65e183af Michael Hanselmann
    ]
1524 d61df03e Iustin Pop
1525 d61df03e Iustin Pop
1526 d61df03e Iustin Pop
class OpTestAllocator(OpCode):
1527 d61df03e Iustin Pop
  """Allocator framework testing.
1528 d61df03e Iustin Pop

1529 d61df03e Iustin Pop
  This opcode has two modes:
1530 d61df03e Iustin Pop
    - gather and return allocator input for a given mode (allocate new
1531 d61df03e Iustin Pop
      or replace secondary) and a given instance definition (direction
1532 d61df03e Iustin Pop
      'in')
1533 d61df03e Iustin Pop
    - run a selected allocator for a given operation (as above) and
1534 d61df03e Iustin Pop
      return the allocator output (direction 'out')
1535 d61df03e Iustin Pop

1536 d61df03e Iustin Pop
  """
1537 60dd1473 Iustin Pop
  OP_DSC_FIELD = "allocator"
1538 65e183af Michael Hanselmann
  OP_PARAMS = [
1539 65e183af Michael Hanselmann
    ("direction", ht.NoDefault,
1540 197b323b Michael Hanselmann
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
1541 197b323b Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
1542 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
1543 65e183af Michael Hanselmann
    ("nics", ht.NoDefault, ht.TOr(ht.TNone, ht.TListOf(
1544 fdbe29ee Michael Hanselmann
     ht.TDictOf(ht.TElemOf([constants.INIC_MAC, constants.INIC_IP, "bridge"]),
1545 45d4c81c Michael Hanselmann
                ht.TOr(ht.TNone, ht.TNonEmptyString)))), None),
1546 197b323b Michael Hanselmann
    ("disks", ht.NoDefault, ht.TOr(ht.TNone, ht.TList), None),
1547 197b323b Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString, None),
1548 197b323b Michael Hanselmann
    ("allocator", None, ht.TMaybeString, None),
1549 197b323b Michael Hanselmann
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1550 dd47a0f0 Iustin Pop
    ("memory", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1551 197b323b Michael Hanselmann
    ("vcpus", None, ht.TOr(ht.TNone, ht.TPositiveInt), None),
1552 197b323b Michael Hanselmann
    ("os", None, ht.TMaybeString, None),
1553 197b323b Michael Hanselmann
    ("disk_template", None, ht.TMaybeString, None),
1554 bee581e2 Michael Hanselmann
    ("instances", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1555 bee581e2 Michael Hanselmann
     None),
1556 60152bbe Michael Hanselmann
    ("evac_mode", None,
1557 60152bbe Michael Hanselmann
     ht.TOr(ht.TNone, ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
1558 bee581e2 Michael Hanselmann
    ("target_groups", None, ht.TOr(ht.TNone, ht.TListOf(ht.TNonEmptyString)),
1559 bee581e2 Michael Hanselmann
     None),
1560 d61df03e Iustin Pop
    ]
1561 363acb1e Iustin Pop
1562 76aef8fc Michael Hanselmann
1563 b469eb4d Iustin Pop
class OpTestJqueue(OpCode):
1564 e58f87a9 Michael Hanselmann
  """Utility opcode to test some aspects of the job queue.
1565 e58f87a9 Michael Hanselmann

1566 e58f87a9 Michael Hanselmann
  """
1567 65e183af Michael Hanselmann
  OP_PARAMS = [
1568 197b323b Michael Hanselmann
    ("notify_waitlock", False, ht.TBool, None),
1569 197b323b Michael Hanselmann
    ("notify_exec", False, ht.TBool, None),
1570 197b323b Michael Hanselmann
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
1571 197b323b Michael Hanselmann
    ("fail", False, ht.TBool, None),
1572 e58f87a9 Michael Hanselmann
    ]
1573 e58f87a9 Michael Hanselmann
1574 e58f87a9 Michael Hanselmann
1575 be760ba8 Michael Hanselmann
class OpTestDummy(OpCode):
1576 be760ba8 Michael Hanselmann
  """Utility opcode used by unittests.
1577 be760ba8 Michael Hanselmann

1578 be760ba8 Michael Hanselmann
  """
1579 65e183af Michael Hanselmann
  OP_PARAMS = [
1580 197b323b Michael Hanselmann
    ("result", ht.NoDefault, ht.NoType, None),
1581 197b323b Michael Hanselmann
    ("messages", ht.NoDefault, ht.NoType, None),
1582 197b323b Michael Hanselmann
    ("fail", ht.NoDefault, ht.NoType, None),
1583 6a373640 Michael Hanselmann
    ("submit_jobs", None, ht.NoType, None),
1584 be760ba8 Michael Hanselmann
    ]
1585 687c10d9 Iustin Pop
  WITH_LU = False
1586 be760ba8 Michael Hanselmann
1587 be760ba8 Michael Hanselmann
1588 dbc96028 Michael Hanselmann
def _GetOpList():
1589 dbc96028 Michael Hanselmann
  """Returns list of all defined opcodes.
1590 dbc96028 Michael Hanselmann

1591 dbc96028 Michael Hanselmann
  Does not eliminate duplicates by C{OP_ID}.
1592 dbc96028 Michael Hanselmann

1593 dbc96028 Michael Hanselmann
  """
1594 dbc96028 Michael Hanselmann
  return [v for v in globals().values()
1595 dbc96028 Michael Hanselmann
          if (isinstance(v, type) and issubclass(v, OpCode) and
1596 687c10d9 Iustin Pop
              hasattr(v, "OP_ID") and v is not OpCode)]
1597 dbc96028 Michael Hanselmann
1598 dbc96028 Michael Hanselmann
1599 dbc96028 Michael Hanselmann
OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())