Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 29a32ce5

History | View | Annotate | Download (64.9 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 fa47242b Iustin Pop
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 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 b459a848 Andrea Spadaccini
# pylint: disable=R0903
35 a8083063 Iustin Pop
36 1cbef6d8 Michael Hanselmann
import logging
37 ff0d18e6 Iustin Pop
import re
38 eaa4c57c Dimitris Aragiorgis
import ipaddr
39 1cbef6d8 Michael Hanselmann
40 65e183af Michael Hanselmann
from ganeti import constants
41 65e183af Michael Hanselmann
from ganeti import errors
42 65e183af Michael Hanselmann
from ganeti import ht
43 415feb2e René Nussbaumer
from ganeti import objects
44 32683096 René Nussbaumer
from ganeti import objectutils
45 65e183af Michael Hanselmann
46 65e183af Michael Hanselmann
47 65e183af Michael Hanselmann
# Common opcode attributes
48 65e183af Michael Hanselmann
49 65e183af Michael Hanselmann
#: output fields for a query operation
50 197b323b Michael Hanselmann
_POutputFields = ("output_fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
51 45d4c81c Michael Hanselmann
                  "Selected output fields")
52 65e183af Michael Hanselmann
53 65e183af Michael Hanselmann
#: the shutdown timeout
54 45d4c81c Michael Hanselmann
_PShutdownTimeout = \
55 2c9fa1ff Iustin Pop
  ("shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TNonNegativeInt,
56 45d4c81c Michael Hanselmann
   "How long to wait for instance to shut down")
57 65e183af Michael Hanselmann
58 65e183af Michael Hanselmann
#: the force parameter
59 45d4c81c Michael Hanselmann
_PForce = ("force", False, ht.TBool, "Whether to force the operation")
60 65e183af Michael Hanselmann
61 65e183af Michael Hanselmann
#: a required instance name (for single-instance LUs)
62 45d4c81c Michael Hanselmann
_PInstanceName = ("instance_name", ht.NoDefault, ht.TNonEmptyString,
63 45d4c81c Michael Hanselmann
                  "Instance name")
64 65e183af Michael Hanselmann
65 65e183af Michael Hanselmann
#: Whether to ignore offline nodes
66 45d4c81c Michael Hanselmann
_PIgnoreOfflineNodes = ("ignore_offline_nodes", False, ht.TBool,
67 45d4c81c Michael Hanselmann
                        "Whether to ignore offline nodes")
68 65e183af Michael Hanselmann
69 65e183af Michael Hanselmann
#: a required node name (for single-node LUs)
70 45d4c81c Michael Hanselmann
_PNodeName = ("node_name", ht.NoDefault, ht.TNonEmptyString, "Node name")
71 65e183af Michael Hanselmann
72 65e183af Michael Hanselmann
#: a required node group name (for single-group LUs)
73 45d4c81c Michael Hanselmann
_PGroupName = ("group_name", ht.NoDefault, ht.TNonEmptyString, "Group name")
74 65e183af Michael Hanselmann
75 65e183af Michael Hanselmann
#: Migration type (live/non-live)
76 65e183af Michael Hanselmann
_PMigrationMode = ("mode", None,
77 fd9f58fd Iustin Pop
                   ht.TMaybe(ht.TElemOf(constants.HT_MIGRATION_MODES)),
78 45d4c81c Michael Hanselmann
                   "Migration mode")
79 65e183af Michael Hanselmann
80 65e183af Michael Hanselmann
#: Obsolete 'live' migration mode (boolean)
81 45d4c81c Michael Hanselmann
_PMigrationLive = ("live", None, ht.TMaybeBool,
82 45d4c81c Michael Hanselmann
                   "Legacy setting for live migration, do not use")
83 65e183af Michael Hanselmann
84 65e183af Michael Hanselmann
#: Tag type
85 971b3a98 Michael Hanselmann
_PTagKind = ("kind", ht.NoDefault, ht.TElemOf(constants.VALID_TAG_TYPES),
86 971b3a98 Michael Hanselmann
             "Tag kind")
87 65e183af Michael Hanselmann
88 65e183af Michael Hanselmann
#: List of tag strings
89 971b3a98 Michael Hanselmann
_PTags = ("tags", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
90 971b3a98 Michael Hanselmann
          "List of tag names")
91 65e183af Michael Hanselmann
92 45d4c81c Michael Hanselmann
_PForceVariant = ("force_variant", False, ht.TBool,
93 45d4c81c Michael Hanselmann
                  "Whether to force an unknown OS variant")
94 45d4c81c Michael Hanselmann
95 45d4c81c Michael Hanselmann
_PWaitForSync = ("wait_for_sync", True, ht.TBool,
96 45d4c81c Michael Hanselmann
                 "Whether to wait for the disk to synchronize")
97 45d4c81c Michael Hanselmann
98 b69437c5 Iustin Pop
_PWaitForSyncFalse = ("wait_for_sync", False, ht.TBool,
99 b69437c5 Iustin Pop
                      "Whether to wait for the disk to synchronize"
100 b69437c5 Iustin Pop
                      " (defaults to false)")
101 b69437c5 Iustin Pop
102 45d4c81c Michael Hanselmann
_PIgnoreConsistency = ("ignore_consistency", False, ht.TBool,
103 45d4c81c Michael Hanselmann
                       "Whether to ignore disk consistency")
104 45d4c81c Michael Hanselmann
105 45d4c81c Michael Hanselmann
_PStorageName = ("name", ht.NoDefault, ht.TMaybeString, "Storage name")
106 45d4c81c Michael Hanselmann
107 45d4c81c Michael Hanselmann
_PUseLocking = ("use_locking", False, ht.TBool,
108 45d4c81c Michael Hanselmann
                "Whether to use synchronization")
109 45d4c81c Michael Hanselmann
110 45d4c81c Michael Hanselmann
_PNameCheck = ("name_check", True, ht.TBool, "Whether to check name")
111 45d4c81c Michael Hanselmann
112 45d4c81c Michael Hanselmann
_PNodeGroupAllocPolicy = \
113 45d4c81c Michael Hanselmann
  ("alloc_policy", None,
114 fd9f58fd Iustin Pop
   ht.TMaybe(ht.TElemOf(constants.VALID_ALLOC_POLICIES)),
115 45d4c81c Michael Hanselmann
   "Instance allocation policy")
116 45d4c81c Michael Hanselmann
117 45d4c81c Michael Hanselmann
_PGroupNodeParams = ("ndparams", None, ht.TMaybeDict,
118 45d4c81c Michael Hanselmann
                     "Default node parameters for group")
119 45d4c81c Michael Hanselmann
120 abd66bf8 Michael Hanselmann
_PQueryWhat = ("what", ht.NoDefault, ht.TElemOf(constants.QR_VIA_OP),
121 8e7078e0 Michael Hanselmann
               "Resource(s) to query for")
122 8e7078e0 Michael Hanselmann
123 e1f23243 Michael Hanselmann
_PEarlyRelease = ("early_release", False, ht.TBool,
124 e1f23243 Michael Hanselmann
                  "Whether to release locks as soon as possible")
125 e1f23243 Michael Hanselmann
126 45d4c81c Michael Hanselmann
_PIpCheckDoc = "Whether to ensure instance's IP address is inactive"
127 45d4c81c Michael Hanselmann
128 9b64e486 Iustin Pop
#: Do not remember instance state changes
129 6aac5aef Iustin Pop
_PNoRemember = ("no_remember", False, ht.TBool,
130 6aac5aef Iustin Pop
                "Do not remember the state change")
131 9b64e486 Iustin Pop
132 f8fa4175 Michael Hanselmann
#: Target node for instance migration/failover
133 f8fa4175 Michael Hanselmann
_PMigrationTargetNode = ("target_node", None, ht.TMaybeString,
134 f8fa4175 Michael Hanselmann
                         "Target node for shared-storage instances")
135 f8fa4175 Michael Hanselmann
136 323f9095 Stephen Shirley
_PStartupPaused = ("startup_paused", False, ht.TBool,
137 323f9095 Stephen Shirley
                   "Pause instance at startup")
138 323f9095 Stephen Shirley
139 57106b74 Michael Hanselmann
_PVerbose = ("verbose", False, ht.TBool, "Verbose mode")
140 57106b74 Michael Hanselmann
141 57106b74 Michael Hanselmann
# Parameters for cluster verification
142 57106b74 Michael Hanselmann
_PDebugSimulateErrors = ("debug_simulate_errors", False, ht.TBool,
143 57106b74 Michael Hanselmann
                         "Whether to simulate errors (useful for debugging)")
144 57106b74 Michael Hanselmann
_PErrorCodes = ("error_codes", False, ht.TBool, "Error codes")
145 57106b74 Michael Hanselmann
_PSkipChecks = ("skip_checks", ht.EmptyList,
146 57106b74 Michael Hanselmann
                ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)),
147 57106b74 Michael Hanselmann
                "Which checks to skip")
148 93f2399e Andrea Spadaccini
_PIgnoreErrors = ("ignore_errors", ht.EmptyList,
149 93f2399e Andrea Spadaccini
                  ht.TListOf(ht.TElemOf(constants.CV_ALL_ECODES_STRINGS)),
150 93f2399e Andrea Spadaccini
                  "List of error codes that should be treated as warnings")
151 323f9095 Stephen Shirley
152 bc5d0215 Andrea Spadaccini
# Disk parameters
153 a138ead7 Michael Hanselmann
_PDiskParams = \
154 a138ead7 Michael Hanselmann
  ("diskparams", None,
155 fd9f58fd Iustin Pop
   ht.TMaybe(ht.TDictOf(ht.TElemOf(constants.DISK_TEMPLATES), ht.TDict)),
156 a138ead7 Michael Hanselmann
   "Disk templates' parameter defaults")
157 bc5d0215 Andrea Spadaccini
158 0ec2ce46 René Nussbaumer
# Parameters for node resource model
159 0ec2ce46 René Nussbaumer
_PHvState = ("hv_state", None, ht.TMaybeDict, "Set hypervisor states")
160 0ec2ce46 René Nussbaumer
_PDiskState = ("disk_state", None, ht.TMaybeDict, "Set disk states")
161 0ec2ce46 René Nussbaumer
162 b6aaf437 René Nussbaumer
163 b6aaf437 René Nussbaumer
_PIgnoreIpolicy = ("ignore_ipolicy", False, ht.TBool,
164 b6aaf437 René Nussbaumer
                   "Whether to ignore ipolicy violations")
165 b6aaf437 René Nussbaumer
166 8c0b16f6 Guido Trotter
# Allow runtime changes while migrating
167 8c0b16f6 Guido Trotter
_PAllowRuntimeChgs = ("allow_runtime_changes", True, ht.TBool,
168 8c0b16f6 Guido Trotter
                      "Allow runtime changes (eg. memory ballooning)")
169 8c0b16f6 Guido Trotter
170 89514061 Iustin Pop
#: IAllocator field builder
171 89514061 Iustin Pop
_PIAllocFromDesc = lambda desc: ("iallocator", None, ht.TMaybeString, desc)
172 89514061 Iustin Pop
173 eaa4c57c Dimitris Aragiorgis
#: a required network name
174 eaa4c57c Dimitris Aragiorgis
_PNetworkName = ("network_name", ht.NoDefault, ht.TNonEmptyString,
175 eaa4c57c Dimitris Aragiorgis
                 "Set network name")
176 8c0b16f6 Guido Trotter
177 ff0d18e6 Iustin Pop
#: OP_ID conversion regular expression
178 ff0d18e6 Iustin Pop
_OPID_RE = re.compile("([a-z])([A-Z])")
179 ff0d18e6 Iustin Pop
180 8c9ee749 Michael Hanselmann
#: Utility function for L{OpClusterSetParams}
181 8f227489 Michael Hanselmann
_TestClusterOsListItem = \
182 8f227489 Michael Hanselmann
  ht.TAnd(ht.TIsLength(2), ht.TItems([
183 8f227489 Michael Hanselmann
    ht.TElemOf(constants.DDMS_VALUES),
184 8f227489 Michael Hanselmann
    ht.TNonEmptyString,
185 8f227489 Michael Hanselmann
    ]))
186 8c9ee749 Michael Hanselmann
187 ff8067cf Michael Hanselmann
_TestClusterOsList = ht.TMaybeListOf(_TestClusterOsListItem)
188 ff0d18e6 Iustin Pop
189 526a662a Michael Hanselmann
# TODO: Generate check from constants.INIC_PARAMS_TYPES
190 526a662a Michael Hanselmann
#: Utility function for testing NIC definitions
191 20a32e74 Michael Hanselmann
_TestNicDef = \
192 20a32e74 Michael Hanselmann
  ht.Comment("NIC parameters")(ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
193 fd9f58fd Iustin Pop
                                          ht.TMaybeString))
194 526a662a Michael Hanselmann
195 1456df62 Michael Hanselmann
_TSetParamsResultItemItems = [
196 1456df62 Michael Hanselmann
  ht.Comment("name of changed parameter")(ht.TNonEmptyString),
197 b3d2ee31 Michael Hanselmann
  ht.Comment("new value")(ht.TAny),
198 1456df62 Michael Hanselmann
  ]
199 1456df62 Michael Hanselmann
200 1456df62 Michael Hanselmann
_TSetParamsResult = \
201 1456df62 Michael Hanselmann
  ht.TListOf(ht.TAnd(ht.TIsLength(len(_TSetParamsResultItemItems)),
202 1456df62 Michael Hanselmann
                     ht.TItems(_TSetParamsResultItemItems)))
203 1456df62 Michael Hanselmann
204 735e1318 Michael Hanselmann
# TODO: Generate check from constants.IDISK_PARAMS_TYPES (however, not all users
205 735e1318 Michael Hanselmann
# of this check support all parameters)
206 20a32e74 Michael Hanselmann
_TDiskParams = \
207 20a32e74 Michael Hanselmann
  ht.Comment("Disk parameters")(ht.TDictOf(ht.TElemOf(constants.IDISK_PARAMS),
208 20a32e74 Michael Hanselmann
                                           ht.TOr(ht.TNonEmptyString, ht.TInt)))
209 735e1318 Michael Hanselmann
210 aa12a891 René Nussbaumer
_TQueryRow = \
211 aa12a891 René Nussbaumer
  ht.TListOf(ht.TAnd(ht.TIsLength(2),
212 aa12a891 René Nussbaumer
                     ht.TItems([ht.TElemOf(constants.RS_ALL),
213 aa12a891 René Nussbaumer
                                ht.TAny])))
214 aa12a891 René Nussbaumer
215 415feb2e René Nussbaumer
_TQueryResult = ht.TListOf(_TQueryRow)
216 415feb2e René Nussbaumer
217 415feb2e René Nussbaumer
_TOldQueryRow = ht.TListOf(ht.TAny)
218 415feb2e René Nussbaumer
219 415feb2e René Nussbaumer
_TOldQueryResult = ht.TListOf(_TOldQueryRow)
220 415feb2e René Nussbaumer
221 415feb2e René Nussbaumer
222 3ce9a5e7 Michael Hanselmann
_SUMMARY_PREFIX = {
223 3ce9a5e7 Michael Hanselmann
  "CLUSTER_": "C_",
224 3ce9a5e7 Michael Hanselmann
  "GROUP_": "G_",
225 3ce9a5e7 Michael Hanselmann
  "NODE_": "N_",
226 3ce9a5e7 Michael Hanselmann
  "INSTANCE_": "I_",
227 3ce9a5e7 Michael Hanselmann
  }
228 3ce9a5e7 Michael Hanselmann
229 b95479a5 Michael Hanselmann
#: Attribute name for dependencies
230 b95479a5 Michael Hanselmann
DEPEND_ATTR = "depends"
231 b95479a5 Michael Hanselmann
232 018ae30b Michael Hanselmann
#: Attribute name for comment
233 018ae30b Michael Hanselmann
COMMENT_ATTR = "comment"
234 018ae30b Michael Hanselmann
235 526a662a Michael Hanselmann
236 ff0d18e6 Iustin Pop
def _NameToId(name):
237 ff0d18e6 Iustin Pop
  """Convert an opcode class name to an OP_ID.
238 ff0d18e6 Iustin Pop

239 ff0d18e6 Iustin Pop
  @type name: string
240 ff0d18e6 Iustin Pop
  @param name: the class name, as OpXxxYyy
241 ff0d18e6 Iustin Pop
  @rtype: string
242 ff0d18e6 Iustin Pop
  @return: the name in the OP_XXXX_YYYY format
243 ff0d18e6 Iustin Pop

244 ff0d18e6 Iustin Pop
  """
245 ff0d18e6 Iustin Pop
  if not name.startswith("Op"):
246 ff0d18e6 Iustin Pop
    return None
247 ff0d18e6 Iustin Pop
  # Note: (?<=[a-z])(?=[A-Z]) would be ideal, since it wouldn't
248 ff0d18e6 Iustin Pop
  # consume any input, and hence we would just have all the elements
249 ff0d18e6 Iustin Pop
  # in the list, one by one; but it seems that split doesn't work on
250 ff0d18e6 Iustin Pop
  # non-consuming input, hence we have to process the input string a
251 ff0d18e6 Iustin Pop
  # bit
252 ff0d18e6 Iustin Pop
  name = _OPID_RE.sub(r"\1,\2", name)
253 ff0d18e6 Iustin Pop
  elems = name.split(",")
254 ff0d18e6 Iustin Pop
  return "_".join(n.upper() for n in elems)
255 ff0d18e6 Iustin Pop
256 65e183af Michael Hanselmann
257 415feb2e René Nussbaumer
def _GenerateObjectTypeCheck(obj, fields_types):
258 415feb2e René Nussbaumer
  """Helper to generate type checks for objects.
259 415feb2e René Nussbaumer

260 415feb2e René Nussbaumer
  @param obj: The object to generate type checks
261 415feb2e René Nussbaumer
  @param fields_types: The fields and their types as a dict
262 415feb2e René Nussbaumer
  @return: A ht type check function
263 415feb2e René Nussbaumer

264 415feb2e René Nussbaumer
  """
265 415feb2e René Nussbaumer
  assert set(obj.GetAllSlots()) == set(fields_types.keys()), \
266 415feb2e René Nussbaumer
    "%s != %s" % (set(obj.GetAllSlots()), set(fields_types.keys()))
267 415feb2e René Nussbaumer
  return ht.TStrictDict(True, True, fields_types)
268 415feb2e René Nussbaumer
269 415feb2e René Nussbaumer
270 b02c6bdf Michael Hanselmann
_TQueryFieldDef = \
271 b02c6bdf Michael Hanselmann
  _GenerateObjectTypeCheck(objects.QueryFieldDefinition, {
272 b02c6bdf Michael Hanselmann
    "name": ht.TNonEmptyString,
273 b02c6bdf Michael Hanselmann
    "title": ht.TNonEmptyString,
274 b02c6bdf Michael Hanselmann
    "kind": ht.TElemOf(constants.QFT_ALL),
275 b02c6bdf Michael Hanselmann
    "doc": ht.TNonEmptyString,
276 b02c6bdf Michael Hanselmann
    })
277 415feb2e René Nussbaumer
278 415feb2e René Nussbaumer
279 65e183af Michael Hanselmann
def RequireFileStorage():
280 65e183af Michael Hanselmann
  """Checks that file storage is enabled.
281 65e183af Michael Hanselmann

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

285 65e183af Michael Hanselmann
  @raise errors.OpPrereqError: when file storage is disabled
286 65e183af Michael Hanselmann

287 65e183af Michael Hanselmann
  """
288 65e183af Michael Hanselmann
  if not constants.ENABLE_FILE_STORAGE:
289 65e183af Michael Hanselmann
    raise errors.OpPrereqError("File storage disabled at configure time",
290 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
291 65e183af Michael Hanselmann
292 65e183af Michael Hanselmann
293 4b97f902 Apollon Oikonomopoulos
def RequireSharedFileStorage():
294 4b97f902 Apollon Oikonomopoulos
  """Checks that shared file storage is enabled.
295 4b97f902 Apollon Oikonomopoulos

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

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

301 4b97f902 Apollon Oikonomopoulos
  """
302 4b97f902 Apollon Oikonomopoulos
  if not constants.ENABLE_SHARED_FILE_STORAGE:
303 4b97f902 Apollon Oikonomopoulos
    raise errors.OpPrereqError("Shared file storage disabled at"
304 4b97f902 Apollon Oikonomopoulos
                               " configure time", errors.ECODE_INVAL)
305 4b97f902 Apollon Oikonomopoulos
306 4b97f902 Apollon Oikonomopoulos
307 8c9ee749 Michael Hanselmann
@ht.WithDesc("CheckFileStorage")
308 8c9ee749 Michael Hanselmann
def _CheckFileStorage(value):
309 8c9ee749 Michael Hanselmann
  """Ensures file storage is enabled if used.
310 65e183af Michael Hanselmann

311 65e183af Michael Hanselmann
  """
312 8c9ee749 Michael Hanselmann
  if value == constants.DT_FILE:
313 65e183af Michael Hanselmann
    RequireFileStorage()
314 4b97f902 Apollon Oikonomopoulos
  elif value == constants.DT_SHARED_FILE:
315 4b97f902 Apollon Oikonomopoulos
    RequireSharedFileStorage()
316 65e183af Michael Hanselmann
  return True
317 65e183af Michael Hanselmann
318 65e183af Michael Hanselmann
319 45fe090b Michael Hanselmann
def _BuildDiskTemplateCheck(accept_none):
320 45fe090b Michael Hanselmann
  """Builds check for disk template.
321 45fe090b Michael Hanselmann

322 45fe090b Michael Hanselmann
  @type accept_none: bool
323 45fe090b Michael Hanselmann
  @param accept_none: whether to accept None as a correct value
324 45fe090b Michael Hanselmann
  @rtype: callable
325 45fe090b Michael Hanselmann

326 45fe090b Michael Hanselmann
  """
327 45fe090b Michael Hanselmann
  template_check = ht.TElemOf(constants.DISK_TEMPLATES)
328 45fe090b Michael Hanselmann
329 45fe090b Michael Hanselmann
  if accept_none:
330 fd9f58fd Iustin Pop
    template_check = ht.TMaybe(template_check)
331 45fe090b Michael Hanselmann
332 45fe090b Michael Hanselmann
  return ht.TAnd(template_check, _CheckFileStorage)
333 8c9ee749 Michael Hanselmann
334 8c9ee749 Michael Hanselmann
335 65e183af Michael Hanselmann
def _CheckStorageType(storage_type):
336 65e183af Michael Hanselmann
  """Ensure a given storage type is valid.
337 65e183af Michael Hanselmann

338 65e183af Michael Hanselmann
  """
339 65e183af Michael Hanselmann
  if storage_type not in constants.VALID_STORAGE_TYPES:
340 65e183af Michael Hanselmann
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
341 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
342 65e183af Michael Hanselmann
  if storage_type == constants.ST_FILE:
343 63a3d8f7 Michael Hanselmann
    # TODO: What about shared file storage?
344 65e183af Michael Hanselmann
    RequireFileStorage()
345 65e183af Michael Hanselmann
  return True
346 65e183af Michael Hanselmann
347 65e183af Michael Hanselmann
348 65e183af Michael Hanselmann
#: Storage type parameter
349 45d4c81c Michael Hanselmann
_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
350 45d4c81c Michael Hanselmann
                 "Storage type")
351 65e183af Michael Hanselmann
352 eaa4c57c Dimitris Aragiorgis
_CheckNetworkType = ht.TElemOf(constants.NETWORK_VALID_TYPES)
353 eaa4c57c Dimitris Aragiorgis
354 3c286190 Dimitris Aragiorgis
355 16091a6e Michael Hanselmann
@ht.WithDesc("IPv4 network")
356 eaa4c57c Dimitris Aragiorgis
def _CheckCIDRNetNotation(value):
357 16091a6e Michael Hanselmann
  """Ensure a given CIDR notation type is valid.
358 eaa4c57c Dimitris Aragiorgis

359 eaa4c57c Dimitris Aragiorgis
  """
360 eaa4c57c Dimitris Aragiorgis
  try:
361 eaa4c57c Dimitris Aragiorgis
    ipaddr.IPv4Network(value)
362 eaa4c57c Dimitris Aragiorgis
  except ipaddr.AddressValueError:
363 eaa4c57c Dimitris Aragiorgis
    return False
364 eaa4c57c Dimitris Aragiorgis
  return True
365 eaa4c57c Dimitris Aragiorgis
366 3c286190 Dimitris Aragiorgis
367 16091a6e Michael Hanselmann
@ht.WithDesc("IPv4 address")
368 eaa4c57c Dimitris Aragiorgis
def _CheckCIDRAddrNotation(value):
369 16091a6e Michael Hanselmann
  """Ensure a given CIDR notation type is valid.
370 eaa4c57c Dimitris Aragiorgis

371 eaa4c57c Dimitris Aragiorgis
  """
372 eaa4c57c Dimitris Aragiorgis
  try:
373 eaa4c57c Dimitris Aragiorgis
    ipaddr.IPv4Address(value)
374 eaa4c57c Dimitris Aragiorgis
  except ipaddr.AddressValueError:
375 eaa4c57c Dimitris Aragiorgis
    return False
376 eaa4c57c Dimitris Aragiorgis
  return True
377 eaa4c57c Dimitris Aragiorgis
378 3c286190 Dimitris Aragiorgis
379 16091a6e Michael Hanselmann
@ht.WithDesc("IPv6 address")
380 eaa4c57c Dimitris Aragiorgis
def _CheckCIDR6AddrNotation(value):
381 16091a6e Michael Hanselmann
  """Ensure a given CIDR notation type is valid.
382 eaa4c57c Dimitris Aragiorgis

383 eaa4c57c Dimitris Aragiorgis
  """
384 eaa4c57c Dimitris Aragiorgis
  try:
385 eaa4c57c Dimitris Aragiorgis
    ipaddr.IPv6Address(value)
386 eaa4c57c Dimitris Aragiorgis
  except ipaddr.AddressValueError:
387 eaa4c57c Dimitris Aragiorgis
    return False
388 eaa4c57c Dimitris Aragiorgis
  return True
389 eaa4c57c Dimitris Aragiorgis
390 3c286190 Dimitris Aragiorgis
391 16091a6e Michael Hanselmann
@ht.WithDesc("IPv6 network")
392 eaa4c57c Dimitris Aragiorgis
def _CheckCIDR6NetNotation(value):
393 16091a6e Michael Hanselmann
  """Ensure a given CIDR notation type is valid.
394 eaa4c57c Dimitris Aragiorgis

395 eaa4c57c Dimitris Aragiorgis
  """
396 eaa4c57c Dimitris Aragiorgis
  try:
397 eaa4c57c Dimitris Aragiorgis
    ipaddr.IPv6Network(value)
398 eaa4c57c Dimitris Aragiorgis
  except ipaddr.AddressValueError:
399 eaa4c57c Dimitris Aragiorgis
    return False
400 eaa4c57c Dimitris Aragiorgis
  return True
401 65e183af Michael Hanselmann
402 3c286190 Dimitris Aragiorgis
403 e1494c96 Iustin Pop
_TIpAddress4 = ht.TAnd(ht.TString, _CheckCIDRAddrNotation)
404 e1494c96 Iustin Pop
_TIpAddress6 = ht.TAnd(ht.TString, _CheckCIDR6AddrNotation)
405 e1494c96 Iustin Pop
_TIpNetwork4 = ht.TAnd(ht.TString, _CheckCIDRNetNotation)
406 e1494c96 Iustin Pop
_TIpNetwork6 = ht.TAnd(ht.TString, _CheckCIDR6NetNotation)
407 e1494c96 Iustin Pop
_TMaybeAddr4List = ht.TMaybe(ht.TListOf(_TIpAddress4))
408 32e3d8b1 Michael Hanselmann
409 32e3d8b1 Michael Hanselmann
410 32683096 René Nussbaumer
class _AutoOpParamSlots(objectutils.AutoSlots):
411 65e183af Michael Hanselmann
  """Meta class for opcode definitions.
412 65e183af Michael Hanselmann

413 65e183af Michael Hanselmann
  """
414 65e183af Michael Hanselmann
  def __new__(mcs, name, bases, attrs):
415 65e183af Michael Hanselmann
    """Called when a class should be created.
416 65e183af Michael Hanselmann

417 65e183af Michael Hanselmann
    @param mcs: The meta class
418 65e183af Michael Hanselmann
    @param name: Name of created class
419 65e183af Michael Hanselmann
    @param bases: Base classes
420 65e183af Michael Hanselmann
    @type attrs: dict
421 65e183af Michael Hanselmann
    @param attrs: Class attributes
422 65e183af Michael Hanselmann

423 65e183af Michael Hanselmann
    """
424 e89a9021 Iustin Pop
    assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
425 ff0d18e6 Iustin Pop
426 32683096 René Nussbaumer
    slots = mcs._GetSlots(attrs)
427 32683096 René Nussbaumer
    assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
428 32683096 René Nussbaumer
      "Class '%s' uses unknown field in OP_DSC_FIELD" % name
429 32683096 René Nussbaumer
430 e89a9021 Iustin Pop
    attrs["OP_ID"] = _NameToId(name)
431 65e183af Michael Hanselmann
432 32683096 René Nussbaumer
    return objectutils.AutoSlots.__new__(mcs, name, bases, attrs)
433 32683096 René Nussbaumer
434 32683096 René Nussbaumer
  @classmethod
435 32683096 René Nussbaumer
  def _GetSlots(mcs, attrs):
436 32683096 René Nussbaumer
    """Build the slots out of OP_PARAMS.
437 32683096 René Nussbaumer

438 32683096 René Nussbaumer
    """
439 65e183af Michael Hanselmann
    # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
440 65e183af Michael Hanselmann
    params = attrs.setdefault("OP_PARAMS", [])
441 65e183af Michael Hanselmann
442 65e183af Michael Hanselmann
    # Use parameter names as slots
443 32683096 René Nussbaumer
    return [pname for (pname, _, _, _) in params]
444 65e183af Michael Hanselmann
445 df458e0b Iustin Pop
446 32683096 René Nussbaumer
class BaseOpCode(objectutils.ValidatedSlots):
447 df458e0b Iustin Pop
  """A simple serializable object.
448 df458e0b Iustin Pop

449 0e46916d Iustin Pop
  This object serves as a parent class for OpCode without any custom
450 0e46916d Iustin Pop
  field handling.
451 0e46916d Iustin Pop

452 df458e0b Iustin Pop
  """
453 b459a848 Andrea Spadaccini
  # pylint: disable=E1101
454 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
455 65e183af Michael Hanselmann
  __metaclass__ = _AutoOpParamSlots
456 65e183af Michael Hanselmann
457 df458e0b Iustin Pop
  def __getstate__(self):
458 a7399f66 Iustin Pop
    """Generic serializer.
459 a7399f66 Iustin Pop

460 a7399f66 Iustin Pop
    This method just returns the contents of the instance as a
461 a7399f66 Iustin Pop
    dictionary.
462 a7399f66 Iustin Pop

463 a7399f66 Iustin Pop
    @rtype:  C{dict}
464 a7399f66 Iustin Pop
    @return: the instance attributes and their values
465 a7399f66 Iustin Pop

466 a7399f66 Iustin Pop
    """
467 df458e0b Iustin Pop
    state = {}
468 32683096 René Nussbaumer
    for name in self.GetAllSlots():
469 df458e0b Iustin Pop
      if hasattr(self, name):
470 df458e0b Iustin Pop
        state[name] = getattr(self, name)
471 df458e0b Iustin Pop
    return state
472 df458e0b Iustin Pop
473 df458e0b Iustin Pop
  def __setstate__(self, state):
474 a7399f66 Iustin Pop
    """Generic unserializer.
475 a7399f66 Iustin Pop

476 a7399f66 Iustin Pop
    This method just restores from the serialized state the attributes
477 a7399f66 Iustin Pop
    of the current instance.
478 a7399f66 Iustin Pop

479 a7399f66 Iustin Pop
    @param state: the serialized opcode data
480 a7399f66 Iustin Pop
    @type state:  C{dict}
481 a7399f66 Iustin Pop

482 a7399f66 Iustin Pop
    """
483 df458e0b Iustin Pop
    if not isinstance(state, dict):
484 df458e0b Iustin Pop
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
485 df458e0b Iustin Pop
                       type(state))
486 df458e0b Iustin Pop
487 32683096 René Nussbaumer
    for name in self.GetAllSlots():
488 44db3a6f Iustin Pop
      if name not in state and hasattr(self, name):
489 df458e0b Iustin Pop
        delattr(self, name)
490 df458e0b Iustin Pop
491 df458e0b Iustin Pop
    for name in state:
492 df458e0b Iustin Pop
      setattr(self, name, state[name])
493 df458e0b Iustin Pop
494 adf385c7 Iustin Pop
  @classmethod
495 65e183af Michael Hanselmann
  def GetAllParams(cls):
496 65e183af Michael Hanselmann
    """Compute list of all parameters for an opcode.
497 65e183af Michael Hanselmann

498 65e183af Michael Hanselmann
    """
499 65e183af Michael Hanselmann
    slots = []
500 65e183af Michael Hanselmann
    for parent in cls.__mro__:
501 65e183af Michael Hanselmann
      slots.extend(getattr(parent, "OP_PARAMS", []))
502 65e183af Michael Hanselmann
    return slots
503 65e183af Michael Hanselmann
504 32683096 René Nussbaumer
  def Validate(self, set_defaults): # pylint: disable=W0221
505 1cbef6d8 Michael Hanselmann
    """Validate opcode parameters, optionally setting default values.
506 1cbef6d8 Michael Hanselmann

507 1cbef6d8 Michael Hanselmann
    @type set_defaults: bool
508 1cbef6d8 Michael Hanselmann
    @param set_defaults: Whether to set default values
509 1cbef6d8 Michael Hanselmann
    @raise errors.OpPrereqError: When a parameter value doesn't match
510 1cbef6d8 Michael Hanselmann
                                 requirements
511 1cbef6d8 Michael Hanselmann

512 1cbef6d8 Michael Hanselmann
    """
513 197b323b Michael Hanselmann
    for (attr_name, default, test, _) in self.GetAllParams():
514 1cbef6d8 Michael Hanselmann
      assert test == ht.NoType or callable(test)
515 1cbef6d8 Michael Hanselmann
516 1cbef6d8 Michael Hanselmann
      if not hasattr(self, attr_name):
517 1cbef6d8 Michael Hanselmann
        if default == ht.NoDefault:
518 1cbef6d8 Michael Hanselmann
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
519 1cbef6d8 Michael Hanselmann
                                     (self.OP_ID, attr_name),
520 1cbef6d8 Michael Hanselmann
                                     errors.ECODE_INVAL)
521 1cbef6d8 Michael Hanselmann
        elif set_defaults:
522 1cbef6d8 Michael Hanselmann
          if callable(default):
523 1cbef6d8 Michael Hanselmann
            dval = default()
524 1cbef6d8 Michael Hanselmann
          else:
525 1cbef6d8 Michael Hanselmann
            dval = default
526 1cbef6d8 Michael Hanselmann
          setattr(self, attr_name, dval)
527 1cbef6d8 Michael Hanselmann
528 1cbef6d8 Michael Hanselmann
      if test == ht.NoType:
529 1cbef6d8 Michael Hanselmann
        # no tests here
530 1cbef6d8 Michael Hanselmann
        continue
531 1cbef6d8 Michael Hanselmann
532 1cbef6d8 Michael Hanselmann
      if set_defaults or hasattr(self, attr_name):
533 1cbef6d8 Michael Hanselmann
        attr_val = getattr(self, attr_name)
534 1cbef6d8 Michael Hanselmann
        if not test(attr_val):
535 e1ebbfcf Iustin Pop
          logging.error("OpCode %s, parameter %s, has invalid type %s/value"
536 e1ebbfcf Iustin Pop
                        " '%s' expecting type %s",
537 68b2e985 René Nussbaumer
                        self.OP_ID, attr_name, type(attr_val), attr_val, test)
538 1cbef6d8 Michael Hanselmann
          raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
539 1cbef6d8 Michael Hanselmann
                                     (self.OP_ID, attr_name),
540 1cbef6d8 Michael Hanselmann
                                     errors.ECODE_INVAL)
541 1cbef6d8 Michael Hanselmann
542 df458e0b Iustin Pop
543 b247c6fc Michael Hanselmann
def _BuildJobDepCheck(relative):
544 b247c6fc Michael Hanselmann
  """Builds check for job dependencies (L{DEPEND_ATTR}).
545 b247c6fc Michael Hanselmann

546 b247c6fc Michael Hanselmann
  @type relative: bool
547 b247c6fc Michael Hanselmann
  @param relative: Whether to accept relative job IDs (negative)
548 b247c6fc Michael Hanselmann
  @rtype: callable
549 b247c6fc Michael Hanselmann

550 b247c6fc Michael Hanselmann
  """
551 b247c6fc Michael Hanselmann
  if relative:
552 b247c6fc Michael Hanselmann
    job_id = ht.TOr(ht.TJobId, ht.TRelativeJobId)
553 b247c6fc Michael Hanselmann
  else:
554 b247c6fc Michael Hanselmann
    job_id = ht.TJobId
555 b247c6fc Michael Hanselmann
556 b247c6fc Michael Hanselmann
  job_dep = \
557 b247c6fc Michael Hanselmann
    ht.TAnd(ht.TIsLength(2),
558 b247c6fc Michael Hanselmann
            ht.TItems([job_id,
559 b247c6fc Michael Hanselmann
                       ht.TListOf(ht.TElemOf(constants.JOBS_FINALIZED))]))
560 b247c6fc Michael Hanselmann
561 ff8067cf Michael Hanselmann
  return ht.TMaybeListOf(job_dep)
562 b247c6fc Michael Hanselmann
563 b247c6fc Michael Hanselmann
564 b247c6fc Michael Hanselmann
TNoRelativeJobDependencies = _BuildJobDepCheck(False)
565 b247c6fc Michael Hanselmann
566 1ce03fb1 Michael Hanselmann
#: List of submission status and job ID as returned by C{SubmitManyJobs}
567 1456df62 Michael Hanselmann
_TJobIdListItem = \
568 1456df62 Michael Hanselmann
  ht.TAnd(ht.TIsLength(2),
569 1456df62 Michael Hanselmann
          ht.TItems([ht.Comment("success")(ht.TBool),
570 1456df62 Michael Hanselmann
                     ht.Comment("Job ID if successful, error message"
571 1456df62 Michael Hanselmann
                                " otherwise")(ht.TOr(ht.TString,
572 1456df62 Michael Hanselmann
                                                     ht.TJobId))]))
573 1456df62 Michael Hanselmann
TJobIdList = ht.TListOf(_TJobIdListItem)
574 1ce03fb1 Michael Hanselmann
575 f7686867 Michael Hanselmann
#: Result containing only list of submitted jobs
576 f7686867 Michael Hanselmann
TJobIdListOnly = ht.TStrictDict(True, True, {
577 1456df62 Michael Hanselmann
  constants.JOB_IDS_KEY: ht.Comment("List of submitted jobs")(TJobIdList),
578 f7686867 Michael Hanselmann
  })
579 f7686867 Michael Hanselmann
580 b247c6fc Michael Hanselmann
581 0e46916d Iustin Pop
class OpCode(BaseOpCode):
582 a7399f66 Iustin Pop
  """Abstract OpCode.
583 a7399f66 Iustin Pop

584 a7399f66 Iustin Pop
  This is the root of the actual OpCode hierarchy. All clases derived
585 a7399f66 Iustin Pop
  from this class should override OP_ID.
586 a7399f66 Iustin Pop

587 a7399f66 Iustin Pop
  @cvar OP_ID: The ID of this opcode. This should be unique amongst all
588 20777413 Iustin Pop
               children of this class.
589 bde8f481 Adeodato Simo
  @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
590 bde8f481 Adeodato Simo
                      string returned by Summary(); see the docstring of that
591 bde8f481 Adeodato Simo
                      method for details).
592 65e183af Michael Hanselmann
  @cvar OP_PARAMS: List of opcode attributes, the default values they should
593 65e183af Michael Hanselmann
                   get if not already defined, and types they must match.
594 1ce03fb1 Michael Hanselmann
  @cvar OP_RESULT: Callable to verify opcode result
595 687c10d9 Iustin Pop
  @cvar WITH_LU: Boolean that specifies whether this should be included in
596 687c10d9 Iustin Pop
      mcpu's dispatch table
597 20777413 Iustin Pop
  @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
598 20777413 Iustin Pop
                 the check steps
599 8f5c488d Michael Hanselmann
  @ivar priority: Opcode priority for queue
600 a7399f66 Iustin Pop

601 a7399f66 Iustin Pop
  """
602 b459a848 Andrea Spadaccini
  # pylint: disable=E1101
603 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
604 687c10d9 Iustin Pop
  WITH_LU = True
605 65e183af Michael Hanselmann
  OP_PARAMS = [
606 45d4c81c Michael Hanselmann
    ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
607 fd9f58fd Iustin Pop
    ("debug_level", None, ht.TMaybe(ht.TNonNegativeInt), "Debug level"),
608 65e183af Michael Hanselmann
    ("priority", constants.OP_PRIO_DEFAULT,
609 45d4c81c Michael Hanselmann
     ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
610 b247c6fc Michael Hanselmann
    (DEPEND_ATTR, None, _BuildJobDepCheck(True),
611 b247c6fc Michael Hanselmann
     "Job dependencies; if used through ``SubmitManyJobs`` relative (negative)"
612 822a50c4 Michael Hanselmann
     " job IDs can be used; see :doc:`design document <design-chained-jobs>`"
613 822a50c4 Michael Hanselmann
     " for details"),
614 018ae30b Michael Hanselmann
    (COMMENT_ATTR, None, ht.TMaybeString,
615 018ae30b Michael Hanselmann
     "Comment describing the purpose of the opcode"),
616 65e183af Michael Hanselmann
    ]
617 1ce03fb1 Michael Hanselmann
  OP_RESULT = None
618 df458e0b Iustin Pop
619 df458e0b Iustin Pop
  def __getstate__(self):
620 df458e0b Iustin Pop
    """Specialized getstate for opcodes.
621 df458e0b Iustin Pop

622 a7399f66 Iustin Pop
    This method adds to the state dictionary the OP_ID of the class,
623 a7399f66 Iustin Pop
    so that on unload we can identify the correct class for
624 a7399f66 Iustin Pop
    instantiating the opcode.
625 a7399f66 Iustin Pop

626 a7399f66 Iustin Pop
    @rtype:   C{dict}
627 a7399f66 Iustin Pop
    @return:  the state as a dictionary
628 a7399f66 Iustin Pop

629 df458e0b Iustin Pop
    """
630 0e46916d Iustin Pop
    data = BaseOpCode.__getstate__(self)
631 df458e0b Iustin Pop
    data["OP_ID"] = self.OP_ID
632 df458e0b Iustin Pop
    return data
633 df458e0b Iustin Pop
634 df458e0b Iustin Pop
  @classmethod
635 00abdc96 Iustin Pop
  def LoadOpCode(cls, data):
636 df458e0b Iustin Pop
    """Generic load opcode method.
637 df458e0b Iustin Pop

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

642 a7399f66 Iustin Pop
    @type data:  C{dict}
643 a7399f66 Iustin Pop
    @param data: the serialized opcode
644 a7399f66 Iustin Pop

645 df458e0b Iustin Pop
    """
646 df458e0b Iustin Pop
    if not isinstance(data, dict):
647 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
648 df458e0b Iustin Pop
    if "OP_ID" not in data:
649 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
650 df458e0b Iustin Pop
    op_id = data["OP_ID"]
651 df458e0b Iustin Pop
    op_class = None
652 363acb1e Iustin Pop
    if op_id in OP_MAPPING:
653 363acb1e Iustin Pop
      op_class = OP_MAPPING[op_id]
654 363acb1e Iustin Pop
    else:
655 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
656 df458e0b Iustin Pop
                       op_id)
657 df458e0b Iustin Pop
    op = op_class()
658 df458e0b Iustin Pop
    new_data = data.copy()
659 df458e0b Iustin Pop
    del new_data["OP_ID"]
660 df458e0b Iustin Pop
    op.__setstate__(new_data)
661 df458e0b Iustin Pop
    return op
662 df458e0b Iustin Pop
663 60dd1473 Iustin Pop
  def Summary(self):
664 60dd1473 Iustin Pop
    """Generates a summary description of this opcode.
665 60dd1473 Iustin Pop

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

672 60dd1473 Iustin Pop
    """
673 ff0d18e6 Iustin Pop
    assert self.OP_ID is not None and len(self.OP_ID) > 3
674 60dd1473 Iustin Pop
    # all OP_ID start with OP_, we remove that
675 60dd1473 Iustin Pop
    txt = self.OP_ID[3:]
676 60dd1473 Iustin Pop
    field_name = getattr(self, "OP_DSC_FIELD", None)
677 60dd1473 Iustin Pop
    if field_name:
678 60dd1473 Iustin Pop
      field_value = getattr(self, field_name, None)
679 bc8bbda1 Iustin Pop
      if isinstance(field_value, (list, tuple)):
680 bc8bbda1 Iustin Pop
        field_value = ",".join(str(i) for i in field_value)
681 60dd1473 Iustin Pop
      txt = "%s(%s)" % (txt, field_value)
682 60dd1473 Iustin Pop
    return txt
683 60dd1473 Iustin Pop
684 3ce9a5e7 Michael Hanselmann
  def TinySummary(self):
685 3ce9a5e7 Michael Hanselmann
    """Generates a compact summary description of the opcode.
686 3ce9a5e7 Michael Hanselmann

687 3ce9a5e7 Michael Hanselmann
    """
688 3ce9a5e7 Michael Hanselmann
    assert self.OP_ID.startswith("OP_")
689 3ce9a5e7 Michael Hanselmann
690 3ce9a5e7 Michael Hanselmann
    text = self.OP_ID[3:]
691 3ce9a5e7 Michael Hanselmann
692 3ce9a5e7 Michael Hanselmann
    for (prefix, supplement) in _SUMMARY_PREFIX.items():
693 3ce9a5e7 Michael Hanselmann
      if text.startswith(prefix):
694 3ce9a5e7 Michael Hanselmann
        return supplement + text[len(prefix):]
695 3ce9a5e7 Michael Hanselmann
696 3ce9a5e7 Michael Hanselmann
    return text
697 3ce9a5e7 Michael Hanselmann
698 a8083063 Iustin Pop
699 afee0879 Iustin Pop
# cluster opcodes
700 afee0879 Iustin Pop
701 bc84ffa7 Iustin Pop
class OpClusterPostInit(OpCode):
702 b5f5fae9 Luca Bigliardi
  """Post cluster initialization.
703 b5f5fae9 Luca Bigliardi

704 b5f5fae9 Luca Bigliardi
  This opcode does not touch the cluster at all. Its purpose is to run hooks
705 b5f5fae9 Luca Bigliardi
  after the cluster has been initialized.
706 b5f5fae9 Luca Bigliardi

707 b5f5fae9 Luca Bigliardi
  """
708 c363310d René Nussbaumer
  OP_RESULT = ht.TBool
709 b5f5fae9 Luca Bigliardi
710 b5f5fae9 Luca Bigliardi
711 c6d43e9e Iustin Pop
class OpClusterDestroy(OpCode):
712 a7399f66 Iustin Pop
  """Destroy the cluster.
713 a7399f66 Iustin Pop

714 a7399f66 Iustin Pop
  This opcode has no other parameters. All the state is irreversibly
715 a7399f66 Iustin Pop
  lost after the execution of this opcode.
716 a7399f66 Iustin Pop

717 a7399f66 Iustin Pop
  """
718 c363310d René Nussbaumer
  OP_RESULT = ht.TNonEmptyString
719 a8083063 Iustin Pop
720 a8083063 Iustin Pop
721 a2f7ab92 Iustin Pop
class OpClusterQuery(OpCode):
722 fdc267f4 Iustin Pop
  """Query cluster information."""
723 415feb2e René Nussbaumer
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TAny)
724 a8083063 Iustin Pop
725 a8083063 Iustin Pop
726 fcad7225 Michael Hanselmann
class OpClusterVerify(OpCode):
727 fcad7225 Michael Hanselmann
  """Submits all jobs necessary to verify the cluster.
728 fcad7225 Michael Hanselmann

729 fcad7225 Michael Hanselmann
  """
730 fcad7225 Michael Hanselmann
  OP_PARAMS = [
731 fcad7225 Michael Hanselmann
    _PDebugSimulateErrors,
732 fcad7225 Michael Hanselmann
    _PErrorCodes,
733 fcad7225 Michael Hanselmann
    _PSkipChecks,
734 93f2399e Andrea Spadaccini
    _PIgnoreErrors,
735 fcad7225 Michael Hanselmann
    _PVerbose,
736 3c286190 Dimitris Aragiorgis
    ("group_name", None, ht.TMaybeString, "Group to verify"),
737 fcad7225 Michael Hanselmann
    ]
738 f7686867 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
739 fcad7225 Michael Hanselmann
740 fcad7225 Michael Hanselmann
741 bf93ae69 Adeodato Simo
class OpClusterVerifyConfig(OpCode):
742 bf93ae69 Adeodato Simo
  """Verify the cluster config.
743 bf93ae69 Adeodato Simo

744 bf93ae69 Adeodato Simo
  """
745 bf93ae69 Adeodato Simo
  OP_PARAMS = [
746 57106b74 Michael Hanselmann
    _PDebugSimulateErrors,
747 57106b74 Michael Hanselmann
    _PErrorCodes,
748 93f2399e Andrea Spadaccini
    _PIgnoreErrors,
749 57106b74 Michael Hanselmann
    _PVerbose,
750 bf93ae69 Adeodato Simo
    ]
751 f7686867 Michael Hanselmann
  OP_RESULT = ht.TBool
752 bf93ae69 Adeodato Simo
753 bf93ae69 Adeodato Simo
754 bf93ae69 Adeodato Simo
class OpClusterVerifyGroup(OpCode):
755 bf93ae69 Adeodato Simo
  """Run verify on a node group from the cluster.
756 a7399f66 Iustin Pop

757 a7399f66 Iustin Pop
  @type skip_checks: C{list}
758 a7399f66 Iustin Pop
  @ivar skip_checks: steps to be skipped from the verify process; this
759 a7399f66 Iustin Pop
                     needs to be a subset of
760 a7399f66 Iustin Pop
                     L{constants.VERIFY_OPTIONAL_CHECKS}; currently
761 a7399f66 Iustin Pop
                     only L{constants.VERIFY_NPLUSONE_MEM} can be passed
762 a7399f66 Iustin Pop

763 a7399f66 Iustin Pop
  """
764 bf93ae69 Adeodato Simo
  OP_DSC_FIELD = "group_name"
765 65e183af Michael Hanselmann
  OP_PARAMS = [
766 57106b74 Michael Hanselmann
    _PGroupName,
767 57106b74 Michael Hanselmann
    _PDebugSimulateErrors,
768 57106b74 Michael Hanselmann
    _PErrorCodes,
769 57106b74 Michael Hanselmann
    _PSkipChecks,
770 93f2399e Andrea Spadaccini
    _PIgnoreErrors,
771 57106b74 Michael Hanselmann
    _PVerbose,
772 65e183af Michael Hanselmann
    ]
773 f7686867 Michael Hanselmann
  OP_RESULT = ht.TBool
774 a8083063 Iustin Pop
775 a8083063 Iustin Pop
776 bd8210a7 Iustin Pop
class OpClusterVerifyDisks(OpCode):
777 150e978f Iustin Pop
  """Verify the cluster disks.
778 150e978f Iustin Pop

779 ae1a845c Michael Hanselmann
  """
780 f7686867 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
781 ae1a845c Michael Hanselmann
782 ae1a845c Michael Hanselmann
783 ae1a845c Michael Hanselmann
class OpGroupVerifyDisks(OpCode):
784 ae1a845c Michael Hanselmann
  """Verifies the status of all disks in a node group.
785 150e978f Iustin Pop

786 ae1a845c Michael Hanselmann
  Result: a tuple of three elements:
787 ae1a845c Michael Hanselmann
    - dict of node names with issues (values: error msg)
788 150e978f Iustin Pop
    - list of instances with degraded disks (that should be activated)
789 b63ed789 Iustin Pop
    - dict of instances with missing logical volumes (values: (node, vol)
790 b63ed789 Iustin Pop
      pairs with details about the missing volumes)
791 150e978f Iustin Pop

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

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

800 150e978f Iustin Pop
  """
801 ae1a845c Michael Hanselmann
  OP_DSC_FIELD = "group_name"
802 ae1a845c Michael Hanselmann
  OP_PARAMS = [
803 ae1a845c Michael Hanselmann
    _PGroupName,
804 ae1a845c Michael Hanselmann
    ]
805 1ce03fb1 Michael Hanselmann
  OP_RESULT = \
806 1ce03fb1 Michael Hanselmann
    ht.TAnd(ht.TIsLength(3),
807 1ce03fb1 Michael Hanselmann
            ht.TItems([ht.TDictOf(ht.TString, ht.TString),
808 1ce03fb1 Michael Hanselmann
                       ht.TListOf(ht.TString),
809 6973587f Michael Hanselmann
                       ht.TDictOf(ht.TString,
810 6973587f Michael Hanselmann
                                  ht.TListOf(ht.TListOf(ht.TString)))]))
811 150e978f Iustin Pop
812 150e978f Iustin Pop
813 5d01aca3 Iustin Pop
class OpClusterRepairDiskSizes(OpCode):
814 60975797 Iustin Pop
  """Verify the disk sizes of the instances and fixes configuration
815 60975797 Iustin Pop
  mimatches.
816 60975797 Iustin Pop

817 60975797 Iustin Pop
  Parameters: optional instances list, in case we want to restrict the
818 60975797 Iustin Pop
  checks to only a subset of the instances.
819 60975797 Iustin Pop

820 60975797 Iustin Pop
  Result: a list of tuples, (instance, disk, new-size) for changed
821 60975797 Iustin Pop
  configurations.
822 60975797 Iustin Pop

823 60975797 Iustin Pop
  In normal operation, the list should be empty.
824 60975797 Iustin Pop

825 60975797 Iustin Pop
  @type instances: list
826 60975797 Iustin Pop
  @ivar instances: the list of instances to check, or empty for all instances
827 60975797 Iustin Pop

828 60975797 Iustin Pop
  """
829 65e183af Michael Hanselmann
  OP_PARAMS = [
830 197b323b Michael Hanselmann
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
831 65e183af Michael Hanselmann
    ]
832 c363310d René Nussbaumer
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
833 c363310d René Nussbaumer
                                 ht.TItems([ht.TNonEmptyString,
834 2c9fa1ff Iustin Pop
                                            ht.TNonNegativeInt,
835 2c9fa1ff Iustin Pop
                                            ht.TNonNegativeInt])))
836 60975797 Iustin Pop
837 60975797 Iustin Pop
838 2f093ea0 Iustin Pop
class OpClusterConfigQuery(OpCode):
839 ae5849b5 Michael Hanselmann
  """Query cluster configuration values."""
840 65e183af Michael Hanselmann
  OP_PARAMS = [
841 3c286190 Dimitris Aragiorgis
    _POutputFields,
842 65e183af Michael Hanselmann
    ]
843 c363310d René Nussbaumer
  OP_RESULT = ht.TListOf(ht.TAny)
844 a8083063 Iustin Pop
845 a8083063 Iustin Pop
846 e126df25 Iustin Pop
class OpClusterRename(OpCode):
847 a7399f66 Iustin Pop
  """Rename the cluster.
848 a7399f66 Iustin Pop

849 a7399f66 Iustin Pop
  @type name: C{str}
850 a7399f66 Iustin Pop
  @ivar name: The new name of the cluster. The name and/or the master IP
851 a7399f66 Iustin Pop
              address will be changed to match the new name and its IP
852 a7399f66 Iustin Pop
              address.
853 a7399f66 Iustin Pop

854 a7399f66 Iustin Pop
  """
855 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
856 65e183af Michael Hanselmann
  OP_PARAMS = [
857 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
858 65e183af Michael Hanselmann
    ]
859 c363310d René Nussbaumer
  OP_RESULT = ht.TNonEmptyString
860 07bd8a51 Iustin Pop
861 07bd8a51 Iustin Pop
862 a6682fdc Iustin Pop
class OpClusterSetParams(OpCode):
863 a7399f66 Iustin Pop
  """Change the parameters of the cluster.
864 a7399f66 Iustin Pop

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

868 a7399f66 Iustin Pop
  """
869 65e183af Michael Hanselmann
  OP_PARAMS = [
870 2da9f556 René Nussbaumer
    _PHvState,
871 2da9f556 René Nussbaumer
    _PDiskState,
872 fd9f58fd Iustin Pop
    ("vg_name", None, ht.TMaybe(ht.TString), "Volume group name"),
873 65e183af Michael Hanselmann
    ("enabled_hypervisors", None,
874 fd9f58fd Iustin Pop
     ht.TMaybe(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)),
875 fd9f58fd Iustin Pop
                       ht.TTrue)),
876 45d4c81c Michael Hanselmann
     "List of enabled hypervisors"),
877 a138ead7 Michael Hanselmann
    ("hvparams", None,
878 fd9f58fd Iustin Pop
     ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
879 45d4c81c Michael Hanselmann
     "Cluster-wide hypervisor parameter defaults, hypervisor-dependent"),
880 fd9f58fd Iustin Pop
    ("beparams", None, ht.TMaybeDict,
881 45d4c81c Michael Hanselmann
     "Cluster-wide backend parameter defaults"),
882 fd9f58fd Iustin Pop
    ("os_hvp", None, ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
883 45d4c81c Michael Hanselmann
     "Cluster-wide per-OS hypervisor parameter defaults"),
884 a138ead7 Michael Hanselmann
    ("osparams", None,
885 fd9f58fd Iustin Pop
     ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
886 45d4c81c Michael Hanselmann
     "Cluster-wide OS parameter defaults"),
887 bc5d0215 Andrea Spadaccini
    _PDiskParams,
888 fd9f58fd Iustin Pop
    ("candidate_pool_size", None, ht.TMaybe(ht.TPositiveInt),
889 45d4c81c Michael Hanselmann
     "Master candidate pool size"),
890 45d4c81c Michael Hanselmann
    ("uid_pool", None, ht.NoType,
891 45d4c81c Michael Hanselmann
     "Set UID pool, must be list of lists describing UID ranges (two items,"
892 45d4c81c Michael Hanselmann
     " start and end inclusive)"),
893 45d4c81c Michael Hanselmann
    ("add_uids", None, ht.NoType,
894 45d4c81c Michael Hanselmann
     "Extend UID pool, must be list of lists describing UID ranges (two"
895 45d4c81c Michael Hanselmann
     " items, start and end inclusive) to be added"),
896 45d4c81c Michael Hanselmann
    ("remove_uids", None, ht.NoType,
897 45d4c81c Michael Hanselmann
     "Shrink UID pool, must be list of lists describing UID ranges (two"
898 45d4c81c Michael Hanselmann
     " items, start and end inclusive) to be removed"),
899 45d4c81c Michael Hanselmann
    ("maintain_node_health", None, ht.TMaybeBool,
900 45d4c81c Michael Hanselmann
     "Whether to automatically maintain node health"),
901 45d4c81c Michael Hanselmann
    ("prealloc_wipe_disks", None, ht.TMaybeBool,
902 45d4c81c Michael Hanselmann
     "Whether to wipe disks before allocating them to instances"),
903 45d4c81c Michael Hanselmann
    ("nicparams", None, ht.TMaybeDict, "Cluster-wide NIC parameter defaults"),
904 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Cluster-wide node parameter defaults"),
905 1b01390b René Nussbaumer
    ("ipolicy", None, ht.TMaybeDict,
906 1b01390b René Nussbaumer
     "Cluster-wide :ref:`instance policy <rapi-ipolicy>` specs"),
907 fd9f58fd Iustin Pop
    ("drbd_helper", None, ht.TMaybe(ht.TString), "DRBD helper program"),
908 fd9f58fd Iustin Pop
    ("default_iallocator", None, ht.TMaybe(ht.TString),
909 45d4c81c Michael Hanselmann
     "Default iallocator for cluster"),
910 fd9f58fd Iustin Pop
    ("master_netdev", None, ht.TMaybe(ht.TString),
911 45d4c81c Michael Hanselmann
     "Master network device"),
912 fd9f58fd Iustin Pop
    ("master_netmask", None, ht.TMaybe(ht.TNonNegativeInt),
913 5a8648eb Andrea Spadaccini
     "Netmask of the master IP"),
914 ff8067cf Michael Hanselmann
    ("reserved_lvs", None, ht.TMaybeListOf(ht.TNonEmptyString),
915 45d4c81c Michael Hanselmann
     "List of reserved LVs"),
916 45d4c81c Michael Hanselmann
    ("hidden_os", None, _TestClusterOsList,
917 0fb66bb1 Iustin Pop
     "Modify list of hidden operating systems: each modification must have"
918 0fb66bb1 Iustin Pop
     " two items, the operation and the OS name; the operation can be"
919 0fb66bb1 Iustin Pop
     " ``%s`` or ``%s``" % (constants.DDM_ADD, constants.DDM_REMOVE)),
920 45d4c81c Michael Hanselmann
    ("blacklisted_os", None, _TestClusterOsList,
921 0fb66bb1 Iustin Pop
     "Modify list of blacklisted operating systems: each modification must"
922 0fb66bb1 Iustin Pop
     " have two items, the operation and the OS name; the operation can be"
923 0fb66bb1 Iustin Pop
     " ``%s`` or ``%s``" % (constants.DDM_ADD, constants.DDM_REMOVE)),
924 bf689b7a Andrea Spadaccini
    ("use_external_mip_script", None, ht.TMaybeBool,
925 bf689b7a Andrea Spadaccini
     "Whether to use an external master IP address setup script"),
926 4b7735f9 Iustin Pop
    ]
927 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
928 12515db7 Manuel Franceschini
929 12515db7 Manuel Franceschini
930 d1240007 Iustin Pop
class OpClusterRedistConf(OpCode):
931 afee0879 Iustin Pop
  """Force a full push of the cluster configuration.
932 afee0879 Iustin Pop

933 afee0879 Iustin Pop
  """
934 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
935 afee0879 Iustin Pop
936 83f72637 Michael Hanselmann
937 fb926117 Andrea Spadaccini
class OpClusterActivateMasterIp(OpCode):
938 fb926117 Andrea Spadaccini
  """Activate the master IP on the master node.
939 fb926117 Andrea Spadaccini

940 fb926117 Andrea Spadaccini
  """
941 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
942 fb926117 Andrea Spadaccini
943 fb926117 Andrea Spadaccini
944 fb926117 Andrea Spadaccini
class OpClusterDeactivateMasterIp(OpCode):
945 fb926117 Andrea Spadaccini
  """Deactivate the master IP on the master node.
946 fb926117 Andrea Spadaccini

947 fb926117 Andrea Spadaccini
  """
948 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
949 fb926117 Andrea Spadaccini
950 fb926117 Andrea Spadaccini
951 83f72637 Michael Hanselmann
class OpQuery(OpCode):
952 83f72637 Michael Hanselmann
  """Query for resources/items.
953 83f72637 Michael Hanselmann

954 abd66bf8 Michael Hanselmann
  @ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
955 83f72637 Michael Hanselmann
  @ivar fields: List of fields to retrieve
956 2e5c33db Iustin Pop
  @ivar qfilter: Query filter
957 83f72637 Michael Hanselmann

958 83f72637 Michael Hanselmann
  """
959 e50d8412 Michael Hanselmann
  OP_DSC_FIELD = "what"
960 65e183af Michael Hanselmann
  OP_PARAMS = [
961 8e7078e0 Michael Hanselmann
    _PQueryWhat,
962 ee13764f Michael Hanselmann
    _PUseLocking,
963 45d4c81c Michael Hanselmann
    ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
964 45d4c81c Michael Hanselmann
     "Requested fields"),
965 fd9f58fd Iustin Pop
    ("qfilter", None, ht.TMaybe(ht.TList),
966 45d4c81c Michael Hanselmann
     "Query filter"),
967 83f72637 Michael Hanselmann
    ]
968 415feb2e René Nussbaumer
  OP_RESULT = \
969 b02c6bdf Michael Hanselmann
    _GenerateObjectTypeCheck(objects.QueryResponse, {
970 b02c6bdf Michael Hanselmann
      "fields": ht.TListOf(_TQueryFieldDef),
971 b02c6bdf Michael Hanselmann
      "data": _TQueryResult,
972 b02c6bdf Michael Hanselmann
      })
973 83f72637 Michael Hanselmann
974 83f72637 Michael Hanselmann
975 83f72637 Michael Hanselmann
class OpQueryFields(OpCode):
976 83f72637 Michael Hanselmann
  """Query for available resource/item fields.
977 83f72637 Michael Hanselmann

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

981 83f72637 Michael Hanselmann
  """
982 e50d8412 Michael Hanselmann
  OP_DSC_FIELD = "what"
983 65e183af Michael Hanselmann
  OP_PARAMS = [
984 8e7078e0 Michael Hanselmann
    _PQueryWhat,
985 ff8067cf Michael Hanselmann
    ("fields", None, ht.TMaybeListOf(ht.TNonEmptyString),
986 8e7078e0 Michael Hanselmann
     "Requested fields; if not given, all are returned"),
987 83f72637 Michael Hanselmann
    ]
988 415feb2e René Nussbaumer
  OP_RESULT = \
989 b02c6bdf Michael Hanselmann
    _GenerateObjectTypeCheck(objects.QueryFieldsResponse, {
990 b02c6bdf Michael Hanselmann
      "fields": ht.TListOf(_TQueryFieldDef),
991 b02c6bdf Michael Hanselmann
      })
992 83f72637 Michael Hanselmann
993 83f72637 Michael Hanselmann
994 792af3ad René Nussbaumer
class OpOobCommand(OpCode):
995 eb64da59 René Nussbaumer
  """Interact with OOB."""
996 65e183af Michael Hanselmann
  OP_PARAMS = [
997 c4ec0755 René Nussbaumer
    ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
998 c4ec0755 René Nussbaumer
     "List of nodes to run the OOB command against"),
999 70296981 Iustin Pop
    ("command", ht.NoDefault, ht.TElemOf(constants.OOB_COMMANDS),
1000 c4ec0755 René Nussbaumer
     "OOB command to be run"),
1001 c4ec0755 René Nussbaumer
    ("timeout", constants.OOB_TIMEOUT, ht.TInt,
1002 c4ec0755 René Nussbaumer
     "Timeout before the OOB helper will be terminated"),
1003 c4ec0755 René Nussbaumer
    ("ignore_status", False, ht.TBool,
1004 c4ec0755 René Nussbaumer
     "Ignores the node offline status for power off"),
1005 2c9fa1ff Iustin Pop
    ("power_delay", constants.OOB_POWER_DELAY, ht.TNonNegativeFloat,
1006 beff3779 René Nussbaumer
     "Time in seconds to wait between powering on nodes"),
1007 eb64da59 René Nussbaumer
    ]
1008 c363310d René Nussbaumer
  # Fixme: Make it more specific with all the special cases in LUOobCommand
1009 415feb2e René Nussbaumer
  OP_RESULT = _TQueryResult
1010 eb64da59 René Nussbaumer
1011 eb64da59 René Nussbaumer
1012 e4d745a7 Michael Hanselmann
class OpRestrictedCommand(OpCode):
1013 e4d745a7 Michael Hanselmann
  """Runs a restricted command on node(s).
1014 e4d745a7 Michael Hanselmann

1015 e4d745a7 Michael Hanselmann
  """
1016 e4d745a7 Michael Hanselmann
  OP_PARAMS = [
1017 e4d745a7 Michael Hanselmann
    _PUseLocking,
1018 e4d745a7 Michael Hanselmann
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1019 e4d745a7 Michael Hanselmann
     "Nodes on which the command should be run (at least one)"),
1020 e4d745a7 Michael Hanselmann
    ("command", ht.NoDefault, ht.TNonEmptyString,
1021 e4d745a7 Michael Hanselmann
     "Command name (no parameters)"),
1022 e4d745a7 Michael Hanselmann
    ]
1023 e4d745a7 Michael Hanselmann
1024 e4d745a7 Michael Hanselmann
  _RESULT_ITEMS = [
1025 e4d745a7 Michael Hanselmann
    ht.Comment("success")(ht.TBool),
1026 e4d745a7 Michael Hanselmann
    ht.Comment("output or error message")(ht.TString),
1027 e4d745a7 Michael Hanselmann
    ]
1028 e4d745a7 Michael Hanselmann
1029 e4d745a7 Michael Hanselmann
  OP_RESULT = \
1030 e4d745a7 Michael Hanselmann
    ht.TListOf(ht.TAnd(ht.TIsLength(len(_RESULT_ITEMS)),
1031 e4d745a7 Michael Hanselmann
                       ht.TItems(_RESULT_ITEMS)))
1032 e4d745a7 Michael Hanselmann
1033 e4d745a7 Michael Hanselmann
1034 07bd8a51 Iustin Pop
# node opcodes
1035 07bd8a51 Iustin Pop
1036 73d565a3 Iustin Pop
class OpNodeRemove(OpCode):
1037 a7399f66 Iustin Pop
  """Remove a node.
1038 a7399f66 Iustin Pop

1039 a7399f66 Iustin Pop
  @type node_name: C{str}
1040 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to remove. If the node still has
1041 a7399f66 Iustin Pop
                   instances on it, the operation will fail.
1042 a7399f66 Iustin Pop

1043 a7399f66 Iustin Pop
  """
1044 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
1045 65e183af Michael Hanselmann
  OP_PARAMS = [
1046 65e183af Michael Hanselmann
    _PNodeName,
1047 65e183af Michael Hanselmann
    ]
1048 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1049 a8083063 Iustin Pop
1050 a8083063 Iustin Pop
1051 d817d49f Iustin Pop
class OpNodeAdd(OpCode):
1052 a7399f66 Iustin Pop
  """Add a node to the cluster.
1053 a7399f66 Iustin Pop

1054 a7399f66 Iustin Pop
  @type node_name: C{str}
1055 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to add. This can be a short name,
1056 a7399f66 Iustin Pop
                   but it will be expanded to the FQDN.
1057 a7399f66 Iustin Pop
  @type primary_ip: IP address
1058 a7399f66 Iustin Pop
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
1059 a7399f66 Iustin Pop
                    opcode is submitted, but will be filled during the node
1060 a7399f66 Iustin Pop
                    add (so it will be visible in the job query).
1061 a7399f66 Iustin Pop
  @type secondary_ip: IP address
1062 a7399f66 Iustin Pop
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
1063 a7399f66 Iustin Pop
                      if the cluster has been initialized in 'dual-network'
1064 a7399f66 Iustin Pop
                      mode, otherwise it must not be given.
1065 a7399f66 Iustin Pop
  @type readd: C{bool}
1066 a7399f66 Iustin Pop
  @ivar readd: Whether to re-add an existing node to the cluster. If
1067 a7399f66 Iustin Pop
               this is not passed, then the operation will abort if the node
1068 a7399f66 Iustin Pop
               name is already in the cluster; use this parameter to 'repair'
1069 a7399f66 Iustin Pop
               a node that had its configuration broken, or was reinstalled
1070 a7399f66 Iustin Pop
               without removal from the cluster.
1071 f936c153 Iustin Pop
  @type group: C{str}
1072 f936c153 Iustin Pop
  @ivar group: The node group to which this node will belong.
1073 fd3d37b6 Iustin Pop
  @type vm_capable: C{bool}
1074 fd3d37b6 Iustin Pop
  @ivar vm_capable: The vm_capable node attribute
1075 fd3d37b6 Iustin Pop
  @type master_capable: C{bool}
1076 fd3d37b6 Iustin Pop
  @ivar master_capable: The master_capable node attribute
1077 a7399f66 Iustin Pop

1078 a7399f66 Iustin Pop
  """
1079 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
1080 65e183af Michael Hanselmann
  OP_PARAMS = [
1081 65e183af Michael Hanselmann
    _PNodeName,
1082 085e0d9f René Nussbaumer
    _PHvState,
1083 085e0d9f René Nussbaumer
    _PDiskState,
1084 45d4c81c Michael Hanselmann
    ("primary_ip", None, ht.NoType, "Primary IP address"),
1085 45d4c81c Michael Hanselmann
    ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
1086 45d4c81c Michael Hanselmann
    ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
1087 45d4c81c Michael Hanselmann
    ("group", None, ht.TMaybeString, "Initial node group"),
1088 45d4c81c Michael Hanselmann
    ("master_capable", None, ht.TMaybeBool,
1089 45d4c81c Michael Hanselmann
     "Whether node can become master or master candidate"),
1090 45d4c81c Michael Hanselmann
    ("vm_capable", None, ht.TMaybeBool,
1091 45d4c81c Michael Hanselmann
     "Whether node can host instances"),
1092 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Node parameters"),
1093 65e183af Michael Hanselmann
    ]
1094 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1095 a8083063 Iustin Pop
1096 a8083063 Iustin Pop
1097 2237687b Iustin Pop
class OpNodeQuery(OpCode):
1098 a8083063 Iustin Pop
  """Compute the list of nodes."""
1099 65e183af Michael Hanselmann
  OP_PARAMS = [
1100 65e183af Michael Hanselmann
    _POutputFields,
1101 45d4c81c Michael Hanselmann
    _PUseLocking,
1102 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1103 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
1104 65e183af Michael Hanselmann
    ]
1105 415feb2e René Nussbaumer
  OP_RESULT = _TOldQueryResult
1106 a8083063 Iustin Pop
1107 a8083063 Iustin Pop
1108 8ed55bfd Iustin Pop
class OpNodeQueryvols(OpCode):
1109 dcb93971 Michael Hanselmann
  """Get list of volumes on node."""
1110 65e183af Michael Hanselmann
  OP_PARAMS = [
1111 65e183af Michael Hanselmann
    _POutputFields,
1112 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1113 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
1114 65e183af Michael Hanselmann
    ]
1115 c363310d René Nussbaumer
  OP_RESULT = ht.TListOf(ht.TAny)
1116 dcb93971 Michael Hanselmann
1117 dcb93971 Michael Hanselmann
1118 ad8d0595 Iustin Pop
class OpNodeQueryStorage(OpCode):
1119 9e5442ce Michael Hanselmann
  """Get information on storage for node(s)."""
1120 65e183af Michael Hanselmann
  OP_PARAMS = [
1121 65e183af Michael Hanselmann
    _POutputFields,
1122 65e183af Michael Hanselmann
    _PStorageType,
1123 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
1124 45d4c81c Michael Hanselmann
    ("name", None, ht.TMaybeString, "Storage name"),
1125 9e5442ce Michael Hanselmann
    ]
1126 415feb2e René Nussbaumer
  OP_RESULT = _TOldQueryResult
1127 9e5442ce Michael Hanselmann
1128 9e5442ce Michael Hanselmann
1129 2cee4077 Iustin Pop
class OpNodeModifyStorage(OpCode):
1130 099c52ad Iustin Pop
  """Modifies the properies of a storage unit"""
1131 32708d0a Iustin Pop
  OP_DSC_FIELD = "node_name"
1132 65e183af Michael Hanselmann
  OP_PARAMS = [
1133 65e183af Michael Hanselmann
    _PNodeName,
1134 65e183af Michael Hanselmann
    _PStorageType,
1135 45d4c81c Michael Hanselmann
    _PStorageName,
1136 45d4c81c Michael Hanselmann
    ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
1137 efb8da02 Michael Hanselmann
    ]
1138 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1139 efb8da02 Michael Hanselmann
1140 efb8da02 Michael Hanselmann
1141 76aef8fc Michael Hanselmann
class OpRepairNodeStorage(OpCode):
1142 76aef8fc Michael Hanselmann
  """Repairs the volume group on a node."""
1143 76aef8fc Michael Hanselmann
  OP_DSC_FIELD = "node_name"
1144 65e183af Michael Hanselmann
  OP_PARAMS = [
1145 65e183af Michael Hanselmann
    _PNodeName,
1146 65e183af Michael Hanselmann
    _PStorageType,
1147 45d4c81c Michael Hanselmann
    _PStorageName,
1148 45d4c81c Michael Hanselmann
    _PIgnoreConsistency,
1149 76aef8fc Michael Hanselmann
    ]
1150 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1151 76aef8fc Michael Hanselmann
1152 76aef8fc Michael Hanselmann
1153 f13973c4 Iustin Pop
class OpNodeSetParams(OpCode):
1154 b31c8676 Iustin Pop
  """Change the parameters of a node."""
1155 b31c8676 Iustin Pop
  OP_DSC_FIELD = "node_name"
1156 65e183af Michael Hanselmann
  OP_PARAMS = [
1157 65e183af Michael Hanselmann
    _PNodeName,
1158 65e183af Michael Hanselmann
    _PForce,
1159 0ec2ce46 René Nussbaumer
    _PHvState,
1160 0ec2ce46 René Nussbaumer
    _PDiskState,
1161 45d4c81c Michael Hanselmann
    ("master_candidate", None, ht.TMaybeBool,
1162 45d4c81c Michael Hanselmann
     "Whether the node should become a master candidate"),
1163 45d4c81c Michael Hanselmann
    ("offline", None, ht.TMaybeBool,
1164 45d4c81c Michael Hanselmann
     "Whether the node should be marked as offline"),
1165 45d4c81c Michael Hanselmann
    ("drained", None, ht.TMaybeBool,
1166 45d4c81c Michael Hanselmann
     "Whether the node should be marked as drained"),
1167 45d4c81c Michael Hanselmann
    ("auto_promote", False, ht.TBool,
1168 45d4c81c Michael Hanselmann
     "Whether node(s) should be promoted to master candidate if necessary"),
1169 45d4c81c Michael Hanselmann
    ("master_capable", None, ht.TMaybeBool,
1170 45d4c81c Michael Hanselmann
     "Denote whether node can become master or master candidate"),
1171 45d4c81c Michael Hanselmann
    ("vm_capable", None, ht.TMaybeBool,
1172 45d4c81c Michael Hanselmann
     "Denote whether node can host instances"),
1173 45d4c81c Michael Hanselmann
    ("secondary_ip", None, ht.TMaybeString,
1174 45d4c81c Michael Hanselmann
     "Change node's secondary IP address"),
1175 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
1176 45d4c81c Michael Hanselmann
    ("powered", None, ht.TMaybeBool,
1177 45d4c81c Michael Hanselmann
     "Whether the node should be marked as powered"),
1178 b31c8676 Iustin Pop
    ]
1179 1456df62 Michael Hanselmann
  OP_RESULT = _TSetParamsResult
1180 b31c8676 Iustin Pop
1181 f5118ade Iustin Pop
1182 e0d4735f Iustin Pop
class OpNodePowercycle(OpCode):
1183 f5118ade Iustin Pop
  """Tries to powercycle a node."""
1184 f5118ade Iustin Pop
  OP_DSC_FIELD = "node_name"
1185 65e183af Michael Hanselmann
  OP_PARAMS = [
1186 65e183af Michael Hanselmann
    _PNodeName,
1187 65e183af Michael Hanselmann
    _PForce,
1188 f5118ade Iustin Pop
    ]
1189 c363310d René Nussbaumer
  OP_RESULT = ht.TMaybeString
1190 f5118ade Iustin Pop
1191 7ffc5a86 Michael Hanselmann
1192 5b14a488 Iustin Pop
class OpNodeMigrate(OpCode):
1193 80cb875c Michael Hanselmann
  """Migrate all instances from a node."""
1194 80cb875c Michael Hanselmann
  OP_DSC_FIELD = "node_name"
1195 65e183af Michael Hanselmann
  OP_PARAMS = [
1196 65e183af Michael Hanselmann
    _PNodeName,
1197 65e183af Michael Hanselmann
    _PMigrationMode,
1198 65e183af Michael Hanselmann
    _PMigrationLive,
1199 f8fa4175 Michael Hanselmann
    _PMigrationTargetNode,
1200 8c0b16f6 Guido Trotter
    _PAllowRuntimeChgs,
1201 9fa567b3 René Nussbaumer
    _PIgnoreIpolicy,
1202 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding the target node"
1203 89514061 Iustin Pop
                     " for shared-storage instances"),
1204 80cb875c Michael Hanselmann
    ]
1205 65c9591c Michael Hanselmann
  OP_RESULT = TJobIdListOnly
1206 80cb875c Michael Hanselmann
1207 80cb875c Michael Hanselmann
1208 e1f23243 Michael Hanselmann
class OpNodeEvacuate(OpCode):
1209 e1f23243 Michael Hanselmann
  """Evacuate instances off a number of nodes."""
1210 e1f23243 Michael Hanselmann
  OP_DSC_FIELD = "node_name"
1211 e1f23243 Michael Hanselmann
  OP_PARAMS = [
1212 e1f23243 Michael Hanselmann
    _PEarlyRelease,
1213 e1f23243 Michael Hanselmann
    _PNodeName,
1214 e1f23243 Michael Hanselmann
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1215 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for computing solution"),
1216 cb92e7a1 Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.NODE_EVAC_MODES),
1217 e1f23243 Michael Hanselmann
     "Node evacuation mode"),
1218 e1f23243 Michael Hanselmann
    ]
1219 1456df62 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
1220 e1f23243 Michael Hanselmann
1221 e1f23243 Michael Hanselmann
1222 a8083063 Iustin Pop
# instance opcodes
1223 a8083063 Iustin Pop
1224 e1530b10 Iustin Pop
class OpInstanceCreate(OpCode):
1225 9bf56d77 Michael Hanselmann
  """Create an instance.
1226 9bf56d77 Michael Hanselmann

1227 9bf56d77 Michael Hanselmann
  @ivar instance_name: Instance name
1228 9bf56d77 Michael Hanselmann
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
1229 9bf56d77 Michael Hanselmann
  @ivar source_handshake: Signed handshake from source (remote import only)
1230 9bf56d77 Michael Hanselmann
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
1231 9bf56d77 Michael Hanselmann
  @ivar source_instance_name: Previous name of instance (remote import only)
1232 dae91d02 Michael Hanselmann
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
1233 dae91d02 Michael Hanselmann
    (remote import only)
1234 9bf56d77 Michael Hanselmann

1235 9bf56d77 Michael Hanselmann
  """
1236 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1237 65e183af Michael Hanselmann
  OP_PARAMS = [
1238 65e183af Michael Hanselmann
    _PInstanceName,
1239 45d4c81c Michael Hanselmann
    _PForceVariant,
1240 45d4c81c Michael Hanselmann
    _PWaitForSync,
1241 45d4c81c Michael Hanselmann
    _PNameCheck,
1242 10889e0c René Nussbaumer
    _PIgnoreIpolicy,
1243 45d4c81c Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict, "Backend parameters for instance"),
1244 735e1318 Michael Hanselmann
    ("disks", ht.NoDefault, ht.TListOf(_TDiskParams),
1245 526a662a Michael Hanselmann
     "Disk descriptions, for example ``[{\"%s\": 100}, {\"%s\": 5}]``;"
1246 526a662a Michael Hanselmann
     " each disk definition must contain a ``%s`` value and"
1247 526a662a Michael Hanselmann
     " can contain an optional ``%s`` value denoting the disk access mode"
1248 526a662a Michael Hanselmann
     " (%s)" %
1249 526a662a Michael Hanselmann
     (constants.IDISK_SIZE, constants.IDISK_SIZE, constants.IDISK_SIZE,
1250 526a662a Michael Hanselmann
      constants.IDISK_MODE,
1251 526a662a Michael Hanselmann
      " or ".join("``%s``" % i for i in sorted(constants.DISK_ACCESS_SET)))),
1252 45fe090b Michael Hanselmann
    ("disk_template", ht.NoDefault, _BuildDiskTemplateCheck(True),
1253 45fe090b Michael Hanselmann
     "Disk template"),
1254 fd9f58fd Iustin Pop
    ("file_driver", None, ht.TMaybe(ht.TElemOf(constants.FILE_DRIVER)),
1255 45d4c81c Michael Hanselmann
     "Driver for file-backed disks"),
1256 45d4c81c Michael Hanselmann
    ("file_storage_dir", None, ht.TMaybeString,
1257 45d4c81c Michael Hanselmann
     "Directory for storing file-backed disks"),
1258 45d4c81c Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict,
1259 45d4c81c Michael Hanselmann
     "Hypervisor parameters for instance, hypervisor-dependent"),
1260 45d4c81c Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString, "Hypervisor"),
1261 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding which node(s) to use"),
1262 45d4c81c Michael Hanselmann
    ("identify_defaults", False, ht.TBool,
1263 45d4c81c Michael Hanselmann
     "Reset instance parameters to default if equal"),
1264 45d4c81c Michael Hanselmann
    ("ip_check", True, ht.TBool, _PIpCheckDoc),
1265 eaa4c57c Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool, "Check for conflicting IPs"),
1266 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.INSTANCE_CREATE_MODES),
1267 45d4c81c Michael Hanselmann
     "Instance creation mode"),
1268 526a662a Michael Hanselmann
    ("nics", ht.NoDefault, ht.TListOf(_TestNicDef),
1269 526a662a Michael Hanselmann
     "List of NIC (network interface) definitions, for example"
1270 526a662a Michael Hanselmann
     " ``[{}, {}, {\"%s\": \"198.51.100.4\"}]``; each NIC definition can"
1271 526a662a Michael Hanselmann
     " contain the optional values %s" %
1272 526a662a Michael Hanselmann
     (constants.INIC_IP,
1273 526a662a Michael Hanselmann
      ", ".join("``%s``" % i for i in sorted(constants.INIC_PARAMS)))),
1274 45d4c81c Michael Hanselmann
    ("no_install", None, ht.TMaybeBool,
1275 45d4c81c Michael Hanselmann
     "Do not install the OS (will disable automatic start)"),
1276 45d4c81c Michael Hanselmann
    ("osparams", ht.EmptyDict, ht.TDict, "OS parameters for instance"),
1277 45d4c81c Michael Hanselmann
    ("os_type", None, ht.TMaybeString, "Operating system"),
1278 45d4c81c Michael Hanselmann
    ("pnode", None, ht.TMaybeString, "Primary node"),
1279 45d4c81c Michael Hanselmann
    ("snode", None, ht.TMaybeString, "Secondary node"),
1280 fd9f58fd Iustin Pop
    ("source_handshake", None, ht.TMaybe(ht.TList),
1281 45d4c81c Michael Hanselmann
     "Signed handshake from source (remote import only)"),
1282 45d4c81c Michael Hanselmann
    ("source_instance_name", None, ht.TMaybeString,
1283 45d4c81c Michael Hanselmann
     "Source instance name (remote import only)"),
1284 65e183af Michael Hanselmann
    ("source_shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
1285 2c9fa1ff Iustin Pop
     ht.TNonNegativeInt,
1286 526a662a Michael Hanselmann
     "How long source instance was given to shut down (remote import only)"),
1287 45d4c81c Michael Hanselmann
    ("source_x509_ca", None, ht.TMaybeString,
1288 45d4c81c Michael Hanselmann
     "Source X509 CA in PEM format (remote import only)"),
1289 45d4c81c Michael Hanselmann
    ("src_node", None, ht.TMaybeString, "Source node for import"),
1290 45d4c81c Michael Hanselmann
    ("src_path", None, ht.TMaybeString, "Source directory for import"),
1291 45d4c81c Michael Hanselmann
    ("start", True, ht.TBool, "Whether to start instance after creation"),
1292 720f56c8 Apollon Oikonomopoulos
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Instance tags"),
1293 3b6d8c9b Iustin Pop
    ]
1294 1456df62 Michael Hanselmann
  OP_RESULT = ht.Comment("instance nodes")(ht.TListOf(ht.TNonEmptyString))
1295 a8083063 Iustin Pop
1296 a8083063 Iustin Pop
1297 12e62af5 René Nussbaumer
class OpInstanceMultiAlloc(OpCode):
1298 12e62af5 René Nussbaumer
  """Allocates multiple instances.
1299 12e62af5 René Nussbaumer

1300 12e62af5 René Nussbaumer
  """
1301 12e62af5 René Nussbaumer
  OP_PARAMS = [
1302 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator used to allocate all the instances"),
1303 4c405df7 Iustin Pop
    ("instances", ht.EmptyList, ht.TListOf(ht.TInstanceOf(OpInstanceCreate)),
1304 12e62af5 René Nussbaumer
     "List of instance create opcodes describing the instances to allocate"),
1305 12e62af5 René Nussbaumer
    ]
1306 12e62af5 René Nussbaumer
  _JOB_LIST = ht.Comment("List of submitted jobs")(TJobIdList)
1307 12e62af5 René Nussbaumer
  ALLOCATABLE_KEY = "allocatable"
1308 12e62af5 René Nussbaumer
  FAILED_KEY = "allocatable"
1309 12e62af5 René Nussbaumer
  OP_RESULT = ht.TStrictDict(True, True, {
1310 12e62af5 René Nussbaumer
    constants.JOB_IDS_KEY: _JOB_LIST,
1311 12e62af5 René Nussbaumer
    ALLOCATABLE_KEY: ht.TListOf(ht.TNonEmptyString),
1312 3c286190 Dimitris Aragiorgis
    FAILED_KEY: ht.TListOf(ht.TNonEmptyString),
1313 12e62af5 René Nussbaumer
    })
1314 12e62af5 René Nussbaumer
1315 12e62af5 René Nussbaumer
  def __getstate__(self):
1316 12e62af5 René Nussbaumer
    """Generic serializer.
1317 12e62af5 René Nussbaumer

1318 12e62af5 René Nussbaumer
    """
1319 12e62af5 René Nussbaumer
    state = OpCode.__getstate__(self)
1320 12e62af5 René Nussbaumer
    if hasattr(self, "instances"):
1321 12e62af5 René Nussbaumer
      # pylint: disable=E1101
1322 12e62af5 René Nussbaumer
      state["instances"] = [inst.__getstate__() for inst in self.instances]
1323 12e62af5 René Nussbaumer
    return state
1324 12e62af5 René Nussbaumer
1325 12e62af5 René Nussbaumer
  def __setstate__(self, state):
1326 12e62af5 René Nussbaumer
    """Generic unserializer.
1327 12e62af5 René Nussbaumer

1328 12e62af5 René Nussbaumer
    This method just restores from the serialized state the attributes
1329 12e62af5 René Nussbaumer
    of the current instance.
1330 12e62af5 René Nussbaumer

1331 12e62af5 René Nussbaumer
    @param state: the serialized opcode data
1332 12e62af5 René Nussbaumer
    @type state: C{dict}
1333 12e62af5 René Nussbaumer

1334 12e62af5 René Nussbaumer
    """
1335 12e62af5 René Nussbaumer
    if not isinstance(state, dict):
1336 12e62af5 René Nussbaumer
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
1337 12e62af5 René Nussbaumer
                       type(state))
1338 12e62af5 René Nussbaumer
1339 12e62af5 René Nussbaumer
    if "instances" in state:
1340 5dff65da Michael Hanselmann
      state["instances"] = map(OpCode.LoadOpCode, state["instances"])
1341 5dff65da Michael Hanselmann
1342 12e62af5 René Nussbaumer
    return OpCode.__setstate__(self, state)
1343 12e62af5 René Nussbaumer
1344 9bc5ac44 René Nussbaumer
  def Validate(self, set_defaults):
1345 9bc5ac44 René Nussbaumer
    """Validates this opcode.
1346 9bc5ac44 René Nussbaumer

1347 9bc5ac44 René Nussbaumer
    We do this recursively.
1348 9bc5ac44 René Nussbaumer

1349 9bc5ac44 René Nussbaumer
    """
1350 9bc5ac44 René Nussbaumer
    OpCode.Validate(self, set_defaults)
1351 9bc5ac44 René Nussbaumer
1352 3779121c René Nussbaumer
    for inst in self.instances: # pylint: disable=E1101
1353 9bc5ac44 René Nussbaumer
      inst.Validate(set_defaults)
1354 9bc5ac44 René Nussbaumer
1355 12e62af5 René Nussbaumer
1356 5073fd8f Iustin Pop
class OpInstanceReinstall(OpCode):
1357 fdc267f4 Iustin Pop
  """Reinstall an instance's OS."""
1358 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1359 65e183af Michael Hanselmann
  OP_PARAMS = [
1360 65e183af Michael Hanselmann
    _PInstanceName,
1361 45d4c81c Michael Hanselmann
    _PForceVariant,
1362 45d4c81c Michael Hanselmann
    ("os_type", None, ht.TMaybeString, "Instance operating system"),
1363 45d4c81c Michael Hanselmann
    ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
1364 65e183af Michael Hanselmann
    ]
1365 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1366 fe7b0351 Michael Hanselmann
1367 fe7b0351 Michael Hanselmann
1368 3cd2d4b1 Iustin Pop
class OpInstanceRemove(OpCode):
1369 a8083063 Iustin Pop
  """Remove an instance."""
1370 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1371 65e183af Michael Hanselmann
  OP_PARAMS = [
1372 65e183af Michael Hanselmann
    _PInstanceName,
1373 65e183af Michael Hanselmann
    _PShutdownTimeout,
1374 45d4c81c Michael Hanselmann
    ("ignore_failures", False, ht.TBool,
1375 45d4c81c Michael Hanselmann
     "Whether to ignore failures during removal"),
1376 fc1baca9 Michael Hanselmann
    ]
1377 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1378 a8083063 Iustin Pop
1379 a8083063 Iustin Pop
1380 5659e2e2 Iustin Pop
class OpInstanceRename(OpCode):
1381 decd5f45 Iustin Pop
  """Rename an instance."""
1382 65e183af Michael Hanselmann
  OP_PARAMS = [
1383 65e183af Michael Hanselmann
    _PInstanceName,
1384 45d4c81c Michael Hanselmann
    _PNameCheck,
1385 45d4c81c Michael Hanselmann
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
1386 45d4c81c Michael Hanselmann
    ("ip_check", False, ht.TBool, _PIpCheckDoc),
1387 4f05fd3b Iustin Pop
    ]
1388 1456df62 Michael Hanselmann
  OP_RESULT = ht.Comment("New instance name")(ht.TNonEmptyString)
1389 decd5f45 Iustin Pop
1390 decd5f45 Iustin Pop
1391 c873d91c Iustin Pop
class OpInstanceStartup(OpCode):
1392 fdc267f4 Iustin Pop
  """Startup an instance."""
1393 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1394 65e183af Michael Hanselmann
  OP_PARAMS = [
1395 65e183af Michael Hanselmann
    _PInstanceName,
1396 65e183af Michael Hanselmann
    _PForce,
1397 65e183af Michael Hanselmann
    _PIgnoreOfflineNodes,
1398 45d4c81c Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict,
1399 45d4c81c Michael Hanselmann
     "Temporary hypervisor parameters, hypervisor-dependent"),
1400 45d4c81c Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
1401 9b64e486 Iustin Pop
    _PNoRemember,
1402 323f9095 Stephen Shirley
    _PStartupPaused,
1403 4f05fd3b Iustin Pop
    ]
1404 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1405 a8083063 Iustin Pop
1406 a8083063 Iustin Pop
1407 ee3e37a7 Iustin Pop
class OpInstanceShutdown(OpCode):
1408 fdc267f4 Iustin Pop
  """Shutdown an instance."""
1409 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1410 65e183af Michael Hanselmann
  OP_PARAMS = [
1411 65e183af Michael Hanselmann
    _PInstanceName,
1412 65e183af Michael Hanselmann
    _PIgnoreOfflineNodes,
1413 2c9fa1ff Iustin Pop
    ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TNonNegativeInt,
1414 45d4c81c Michael Hanselmann
     "How long to wait for instance to shut down"),
1415 9b64e486 Iustin Pop
    _PNoRemember,
1416 b44bd844 Michael Hanselmann
    ]
1417 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1418 a8083063 Iustin Pop
1419 a8083063 Iustin Pop
1420 90ab1a95 Iustin Pop
class OpInstanceReboot(OpCode):
1421 bf6929a2 Alexander Schreiber
  """Reboot an instance."""
1422 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1423 65e183af Michael Hanselmann
  OP_PARAMS = [
1424 65e183af Michael Hanselmann
    _PInstanceName,
1425 65e183af Michael Hanselmann
    _PShutdownTimeout,
1426 45d4c81c Michael Hanselmann
    ("ignore_secondaries", False, ht.TBool,
1427 45d4c81c Michael Hanselmann
     "Whether to start the instance even if secondary disks are failing"),
1428 45d4c81c Michael Hanselmann
    ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
1429 45d4c81c Michael Hanselmann
     "How to reboot instance"),
1430 4f05fd3b Iustin Pop
    ]
1431 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1432 bf6929a2 Alexander Schreiber
1433 bf6929a2 Alexander Schreiber
1434 668f755d Iustin Pop
class OpInstanceReplaceDisks(OpCode):
1435 fdc267f4 Iustin Pop
  """Replace the disks of an instance."""
1436 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1437 65e183af Michael Hanselmann
  OP_PARAMS = [
1438 65e183af Michael Hanselmann
    _PInstanceName,
1439 e1f23243 Michael Hanselmann
    _PEarlyRelease,
1440 d2fe2bfb René Nussbaumer
    _PIgnoreIpolicy,
1441 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1442 45d4c81c Michael Hanselmann
     "Replacement mode"),
1443 2c9fa1ff Iustin Pop
    ("disks", ht.EmptyList, ht.TListOf(ht.TNonNegativeInt),
1444 45d4c81c Michael Hanselmann
     "Disk indexes"),
1445 45d4c81c Michael Hanselmann
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1446 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding new secondary node"),
1447 4f05fd3b Iustin Pop
    ]
1448 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1449 a8083063 Iustin Pop
1450 a8083063 Iustin Pop
1451 019dbee1 Iustin Pop
class OpInstanceFailover(OpCode):
1452 a8083063 Iustin Pop
  """Failover an instance."""
1453 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1454 65e183af Michael Hanselmann
  OP_PARAMS = [
1455 65e183af Michael Hanselmann
    _PInstanceName,
1456 65e183af Michael Hanselmann
    _PShutdownTimeout,
1457 45d4c81c Michael Hanselmann
    _PIgnoreConsistency,
1458 f8fa4175 Michael Hanselmann
    _PMigrationTargetNode,
1459 b6aaf437 René Nussbaumer
    _PIgnoreIpolicy,
1460 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1461 89514061 Iustin Pop
                     " shared-storage instances"),
1462 17c3f802 Guido Trotter
    ]
1463 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1464 a8083063 Iustin Pop
1465 a8083063 Iustin Pop
1466 75c866c2 Iustin Pop
class OpInstanceMigrate(OpCode):
1467 53c776b5 Iustin Pop
  """Migrate an instance.
1468 53c776b5 Iustin Pop

1469 53c776b5 Iustin Pop
  This migrates (without shutting down an instance) to its secondary
1470 53c776b5 Iustin Pop
  node.
1471 53c776b5 Iustin Pop

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

1475 53c776b5 Iustin Pop
  """
1476 ee69c97f Iustin Pop
  OP_DSC_FIELD = "instance_name"
1477 65e183af Michael Hanselmann
  OP_PARAMS = [
1478 65e183af Michael Hanselmann
    _PInstanceName,
1479 65e183af Michael Hanselmann
    _PMigrationMode,
1480 65e183af Michael Hanselmann
    _PMigrationLive,
1481 f8fa4175 Michael Hanselmann
    _PMigrationTargetNode,
1482 8c0b16f6 Guido Trotter
    _PAllowRuntimeChgs,
1483 3ed23330 René Nussbaumer
    _PIgnoreIpolicy,
1484 45d4c81c Michael Hanselmann
    ("cleanup", False, ht.TBool,
1485 45d4c81c Michael Hanselmann
     "Whether a previously failed migration should be cleaned up"),
1486 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1487 89514061 Iustin Pop
                     " shared-storage instances"),
1488 d5cafd31 René Nussbaumer
    ("allow_failover", False, ht.TBool,
1489 d5cafd31 René Nussbaumer
     "Whether we can fallback to failover if migration is not possible"),
1490 65e183af Michael Hanselmann
    ]
1491 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1492 53c776b5 Iustin Pop
1493 53c776b5 Iustin Pop
1494 0091b480 Iustin Pop
class OpInstanceMove(OpCode):
1495 313bcead Iustin Pop
  """Move an instance.
1496 313bcead Iustin Pop

1497 313bcead Iustin Pop
  This move (with shutting down an instance and data copying) to an
1498 313bcead Iustin Pop
  arbitrary node.
1499 313bcead Iustin Pop

1500 313bcead Iustin Pop
  @ivar instance_name: the name of the instance
1501 313bcead Iustin Pop
  @ivar target_node: the destination node
1502 313bcead Iustin Pop

1503 313bcead Iustin Pop
  """
1504 313bcead Iustin Pop
  OP_DSC_FIELD = "instance_name"
1505 65e183af Michael Hanselmann
  OP_PARAMS = [
1506 65e183af Michael Hanselmann
    _PInstanceName,
1507 65e183af Michael Hanselmann
    _PShutdownTimeout,
1508 92cf62e3 René Nussbaumer
    _PIgnoreIpolicy,
1509 45d4c81c Michael Hanselmann
    ("target_node", ht.NoDefault, ht.TNonEmptyString, "Target node"),
1510 bb851c63 Iustin Pop
    _PIgnoreConsistency,
1511 154b9580 Balazs Lecz
    ]
1512 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1513 313bcead Iustin Pop
1514 313bcead Iustin Pop
1515 cc0dec7b Iustin Pop
class OpInstanceConsole(OpCode):
1516 fdc267f4 Iustin Pop
  """Connect to an instance's console."""
1517 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1518 65e183af Michael Hanselmann
  OP_PARAMS = [
1519 3c286190 Dimitris Aragiorgis
    _PInstanceName,
1520 65e183af Michael Hanselmann
    ]
1521 c363310d René Nussbaumer
  OP_RESULT = ht.TDict
1522 a8083063 Iustin Pop
1523 a8083063 Iustin Pop
1524 83f5d475 Iustin Pop
class OpInstanceActivateDisks(OpCode):
1525 fdc267f4 Iustin Pop
  """Activate an instance's disks."""
1526 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1527 65e183af Michael Hanselmann
  OP_PARAMS = [
1528 65e183af Michael Hanselmann
    _PInstanceName,
1529 45d4c81c Michael Hanselmann
    ("ignore_size", False, ht.TBool, "Whether to ignore recorded size"),
1530 b69437c5 Iustin Pop
    _PWaitForSyncFalse,
1531 65e183af Michael Hanselmann
    ]
1532 c363310d René Nussbaumer
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
1533 c363310d René Nussbaumer
                                 ht.TItems([ht.TNonEmptyString,
1534 c363310d René Nussbaumer
                                            ht.TNonEmptyString,
1535 c363310d René Nussbaumer
                                            ht.TNonEmptyString])))
1536 a8083063 Iustin Pop
1537 a8083063 Iustin Pop
1538 e176281f Iustin Pop
class OpInstanceDeactivateDisks(OpCode):
1539 fdc267f4 Iustin Pop
  """Deactivate an instance's disks."""
1540 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1541 65e183af Michael Hanselmann
  OP_PARAMS = [
1542 c9c41373 Iustin Pop
    _PInstanceName,
1543 c9c41373 Iustin Pop
    _PForce,
1544 65e183af Michael Hanselmann
    ]
1545 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1546 a8083063 Iustin Pop
1547 a8083063 Iustin Pop
1548 6b273e78 Iustin Pop
class OpInstanceRecreateDisks(OpCode):
1549 a52978c7 Michael Hanselmann
  """Recreate an instance's disks."""
1550 735e1318 Michael Hanselmann
  _TDiskChanges = \
1551 735e1318 Michael Hanselmann
    ht.TAnd(ht.TIsLength(2),
1552 2c9fa1ff Iustin Pop
            ht.TItems([ht.Comment("Disk index")(ht.TNonNegativeInt),
1553 735e1318 Michael Hanselmann
                       ht.Comment("Parameters")(_TDiskParams)]))
1554 735e1318 Michael Hanselmann
1555 bd315bfa Iustin Pop
  OP_DSC_FIELD = "instance_name"
1556 65e183af Michael Hanselmann
  OP_PARAMS = [
1557 65e183af Michael Hanselmann
    _PInstanceName,
1558 735e1318 Michael Hanselmann
    ("disks", ht.EmptyList,
1559 2c9fa1ff Iustin Pop
     ht.TOr(ht.TListOf(ht.TNonNegativeInt), ht.TListOf(_TDiskChanges)),
1560 735e1318 Michael Hanselmann
     "List of disk indexes (deprecated) or a list of tuples containing a disk"
1561 735e1318 Michael Hanselmann
     " index and a possibly empty dictionary with disk parameter changes"),
1562 93384b8c Guido Trotter
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1563 93384b8c Guido Trotter
     "New instance nodes, if relocation is desired"),
1564 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding new nodes"),
1565 65e183af Michael Hanselmann
    ]
1566 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1567 bd315bfa Iustin Pop
1568 bd315bfa Iustin Pop
1569 f2af0bec Iustin Pop
class OpInstanceQuery(OpCode):
1570 a8083063 Iustin Pop
  """Compute the list of instances."""
1571 65e183af Michael Hanselmann
  OP_PARAMS = [
1572 65e183af Michael Hanselmann
    _POutputFields,
1573 45d4c81c Michael Hanselmann
    _PUseLocking,
1574 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1575 45d4c81c Michael Hanselmann
     "Empty list to query all instances, instance names otherwise"),
1576 65e183af Michael Hanselmann
    ]
1577 415feb2e René Nussbaumer
  OP_RESULT = _TOldQueryResult
1578 a8083063 Iustin Pop
1579 a8083063 Iustin Pop
1580 dc28c4e4 Iustin Pop
class OpInstanceQueryData(OpCode):
1581 a8083063 Iustin Pop
  """Compute the run-time status of instances."""
1582 65e183af Michael Hanselmann
  OP_PARAMS = [
1583 af7b6689 Michael Hanselmann
    _PUseLocking,
1584 af7b6689 Michael Hanselmann
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1585 af7b6689 Michael Hanselmann
     "Instance names"),
1586 af7b6689 Michael Hanselmann
    ("static", False, ht.TBool,
1587 af7b6689 Michael Hanselmann
     "Whether to only return configuration data without querying"
1588 af7b6689 Michael Hanselmann
     " nodes"),
1589 65e183af Michael Hanselmann
    ]
1590 415feb2e René Nussbaumer
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TDict)
1591 a8083063 Iustin Pop
1592 a8083063 Iustin Pop
1593 ddc1de7c Michael Hanselmann
def _TestInstSetParamsModList(fn):
1594 ddc1de7c Michael Hanselmann
  """Generates a check for modification lists.
1595 ddc1de7c Michael Hanselmann

1596 ddc1de7c Michael Hanselmann
  """
1597 e9c3d864 Michael Hanselmann
  # Old format
1598 e9c3d864 Michael Hanselmann
  # TODO: Remove in version 2.8 including support in LUInstanceSetParams
1599 e9c3d864 Michael Hanselmann
  old_mod_item_fn = \
1600 ddc1de7c Michael Hanselmann
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1601 2c9fa1ff Iustin Pop
      ht.TOr(ht.TElemOf(constants.DDMS_VALUES), ht.TNonNegativeInt),
1602 ddc1de7c Michael Hanselmann
      fn,
1603 ddc1de7c Michael Hanselmann
      ]))
1604 ddc1de7c Michael Hanselmann
1605 e9c3d864 Michael Hanselmann
  # New format, supporting adding/removing disks/NICs at arbitrary indices
1606 e9c3d864 Michael Hanselmann
  mod_item_fn = \
1607 e9c3d864 Michael Hanselmann
    ht.TAnd(ht.TIsLength(3), ht.TItems([
1608 e9c3d864 Michael Hanselmann
      ht.TElemOf(constants.DDMS_VALUES_WITH_MODIFY),
1609 e9c3d864 Michael Hanselmann
      ht.Comment("Disk index, can be negative, e.g. -1 for last disk")(ht.TInt),
1610 e9c3d864 Michael Hanselmann
      fn,
1611 e9c3d864 Michael Hanselmann
      ]))
1612 e9c3d864 Michael Hanselmann
1613 e9c3d864 Michael Hanselmann
  return ht.TOr(ht.Comment("Recommended")(ht.TListOf(mod_item_fn)),
1614 e9c3d864 Michael Hanselmann
                ht.Comment("Deprecated")(ht.TListOf(old_mod_item_fn)))
1615 ddc1de7c Michael Hanselmann
1616 ddc1de7c Michael Hanselmann
1617 9a3cc7ae Iustin Pop
class OpInstanceSetParams(OpCode):
1618 ddc1de7c Michael Hanselmann
  """Change the parameters of an instance.
1619 ddc1de7c Michael Hanselmann

1620 ddc1de7c Michael Hanselmann
  """
1621 a2aadb34 Michael Hanselmann
  TestNicModifications = _TestInstSetParamsModList(_TestNicDef)
1622 a2aadb34 Michael Hanselmann
  TestDiskModifications = _TestInstSetParamsModList(_TDiskParams)
1623 ddc1de7c Michael Hanselmann
1624 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1625 65e183af Michael Hanselmann
  OP_PARAMS = [
1626 65e183af Michael Hanselmann
    _PInstanceName,
1627 65e183af Michael Hanselmann
    _PForce,
1628 45d4c81c Michael Hanselmann
    _PForceVariant,
1629 1559e1e7 René Nussbaumer
    _PIgnoreIpolicy,
1630 a2aadb34 Michael Hanselmann
    ("nics", ht.EmptyList, TestNicModifications,
1631 0fb66bb1 Iustin Pop
     "List of NIC changes: each item is of the form ``(op, index, settings)``,"
1632 0fb66bb1 Iustin Pop
     " ``op`` is one of ``%s``, ``%s`` or ``%s``, ``index`` can be either -1"
1633 0fb66bb1 Iustin Pop
     " to refer to the last position, or a zero-based index number; a"
1634 0fb66bb1 Iustin Pop
     " deprecated version of this parameter used the form ``(op, settings)``,"
1635 0fb66bb1 Iustin Pop
     " where ``op`` can be ``%s`` to add a new NIC with the specified"
1636 0fb66bb1 Iustin Pop
     " settings, ``%s`` to remove the last NIC or a number to modify the"
1637 0fb66bb1 Iustin Pop
     " settings of the NIC with that index" %
1638 e9c3d864 Michael Hanselmann
     (constants.DDM_ADD, constants.DDM_MODIFY, constants.DDM_REMOVE,
1639 e9c3d864 Michael Hanselmann
      constants.DDM_ADD, constants.DDM_REMOVE)),
1640 a2aadb34 Michael Hanselmann
    ("disks", ht.EmptyList, TestDiskModifications,
1641 0fb66bb1 Iustin Pop
     "List of disk changes; see ``nics``"),
1642 45d4c81c Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict, "Per-instance backend parameters"),
1643 2c9fa1ff Iustin Pop
    ("runtime_mem", None, ht.TMaybePositiveInt, "New runtime memory"),
1644 45d4c81c Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict,
1645 45d4c81c Michael Hanselmann
     "Per-instance hypervisor parameters, hypervisor-dependent"),
1646 fd9f58fd Iustin Pop
    ("disk_template", None, ht.TMaybe(_BuildDiskTemplateCheck(False)),
1647 45d4c81c Michael Hanselmann
     "Disk template for instance"),
1648 45d4c81c Michael Hanselmann
    ("remote_node", None, ht.TMaybeString,
1649 45d4c81c Michael Hanselmann
     "Secondary node (used when changing disk template)"),
1650 45d4c81c Michael Hanselmann
    ("os_name", None, ht.TMaybeString,
1651 0fb66bb1 Iustin Pop
     "Change the instance's OS without reinstalling the instance"),
1652 45d4c81c Michael Hanselmann
    ("osparams", None, ht.TMaybeDict, "Per-instance OS parameters"),
1653 93384b8c Guido Trotter
    ("wait_for_sync", True, ht.TBool,
1654 93384b8c Guido Trotter
     "Whether to wait for the disk to synchronize, when changing template"),
1655 3016bc1f Michael Hanselmann
    ("offline", None, ht.TMaybeBool, "Whether to mark instance as offline"),
1656 eaa4c57c Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool, "Check for conflicting IPs"),
1657 973d7867 Iustin Pop
    ]
1658 1456df62 Michael Hanselmann
  OP_RESULT = _TSetParamsResult
1659 a8083063 Iustin Pop
1660 a8083063 Iustin Pop
1661 60472d29 Iustin Pop
class OpInstanceGrowDisk(OpCode):
1662 8729e0d7 Iustin Pop
  """Grow a disk of an instance."""
1663 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1664 65e183af Michael Hanselmann
  OP_PARAMS = [
1665 65e183af Michael Hanselmann
    _PInstanceName,
1666 45d4c81c Michael Hanselmann
    _PWaitForSync,
1667 45d4c81c Michael Hanselmann
    ("disk", ht.NoDefault, ht.TInt, "Disk index"),
1668 2c9fa1ff Iustin Pop
    ("amount", ht.NoDefault, ht.TNonNegativeInt,
1669 45d4c81c Michael Hanselmann
     "Amount of disk space to add (megabytes)"),
1670 e7f99087 Iustin Pop
    ("absolute", False, ht.TBool,
1671 e7f99087 Iustin Pop
     "Whether the amount parameter is an absolute target or a relative one"),
1672 4f05fd3b Iustin Pop
    ]
1673 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1674 8729e0d7 Iustin Pop
1675 8729e0d7 Iustin Pop
1676 1aef3df8 Michael Hanselmann
class OpInstanceChangeGroup(OpCode):
1677 1aef3df8 Michael Hanselmann
  """Moves an instance to another node group."""
1678 1aef3df8 Michael Hanselmann
  OP_DSC_FIELD = "instance_name"
1679 1aef3df8 Michael Hanselmann
  OP_PARAMS = [
1680 1aef3df8 Michael Hanselmann
    _PInstanceName,
1681 1aef3df8 Michael Hanselmann
    _PEarlyRelease,
1682 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for computing solution"),
1683 ff8067cf Michael Hanselmann
    ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString),
1684 1aef3df8 Michael Hanselmann
     "Destination group names or UUIDs (defaults to \"all but current group\""),
1685 1aef3df8 Michael Hanselmann
    ]
1686 1456df62 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
1687 1aef3df8 Michael Hanselmann
1688 1aef3df8 Michael Hanselmann
1689 70a6a926 Adeodato Simo
# Node group opcodes
1690 70a6a926 Adeodato Simo
1691 fabf1731 Iustin Pop
class OpGroupAdd(OpCode):
1692 b1ee5610 Adeodato Simo
  """Add a node group to the cluster."""
1693 b1ee5610 Adeodato Simo
  OP_DSC_FIELD = "group_name"
1694 65e183af Michael Hanselmann
  OP_PARAMS = [
1695 65e183af Michael Hanselmann
    _PGroupName,
1696 45d4c81c Michael Hanselmann
    _PNodeGroupAllocPolicy,
1697 45d4c81c Michael Hanselmann
    _PGroupNodeParams,
1698 bc5d0215 Andrea Spadaccini
    _PDiskParams,
1699 e4c03256 René Nussbaumer
    _PHvState,
1700 e4c03256 René Nussbaumer
    _PDiskState,
1701 1b01390b René Nussbaumer
    ("ipolicy", None, ht.TMaybeDict,
1702 1b01390b René Nussbaumer
     "Group-wide :ref:`instance policy <rapi-ipolicy>` specs"),
1703 483be60d Adeodato Simo
    ]
1704 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1705 b1ee5610 Adeodato Simo
1706 b1ee5610 Adeodato Simo
1707 934704ae Iustin Pop
class OpGroupAssignNodes(OpCode):
1708 96276ae7 Adeodato Simo
  """Assign nodes to a node group."""
1709 96276ae7 Adeodato Simo
  OP_DSC_FIELD = "group_name"
1710 96276ae7 Adeodato Simo
  OP_PARAMS = [
1711 96276ae7 Adeodato Simo
    _PGroupName,
1712 96276ae7 Adeodato Simo
    _PForce,
1713 45d4c81c Michael Hanselmann
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1714 45d4c81c Michael Hanselmann
     "List of nodes to assign"),
1715 96276ae7 Adeodato Simo
    ]
1716 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1717 96276ae7 Adeodato Simo
1718 96276ae7 Adeodato Simo
1719 d4d654bd Iustin Pop
class OpGroupQuery(OpCode):
1720 70a6a926 Adeodato Simo
  """Compute the list of node groups."""
1721 65e183af Michael Hanselmann
  OP_PARAMS = [
1722 65e183af Michael Hanselmann
    _POutputFields,
1723 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1724 45d4c81c Michael Hanselmann
     "Empty list to query all groups, group names otherwise"),
1725 65e183af Michael Hanselmann
    ]
1726 415feb2e René Nussbaumer
  OP_RESULT = _TOldQueryResult
1727 70a6a926 Adeodato Simo
1728 70a6a926 Adeodato Simo
1729 7cbf74f0 Iustin Pop
class OpGroupSetParams(OpCode):
1730 4da7909a Adeodato Simo
  """Change the parameters of a node group."""
1731 4da7909a Adeodato Simo
  OP_DSC_FIELD = "group_name"
1732 65e183af Michael Hanselmann
  OP_PARAMS = [
1733 65e183af Michael Hanselmann
    _PGroupName,
1734 45d4c81c Michael Hanselmann
    _PNodeGroupAllocPolicy,
1735 45d4c81c Michael Hanselmann
    _PGroupNodeParams,
1736 bc5d0215 Andrea Spadaccini
    _PDiskParams,
1737 a8282327 René Nussbaumer
    _PHvState,
1738 a8282327 René Nussbaumer
    _PDiskState,
1739 fb644e77 Agata Murawska
    ("ipolicy", None, ht.TMaybeDict, "Group-wide instance policy specs"),
1740 4da7909a Adeodato Simo
    ]
1741 1456df62 Michael Hanselmann
  OP_RESULT = _TSetParamsResult
1742 4da7909a Adeodato Simo
1743 4da7909a Adeodato Simo
1744 4d1baa51 Iustin Pop
class OpGroupRemove(OpCode):
1745 94bd652a Adeodato Simo
  """Remove a node group from the cluster."""
1746 94bd652a Adeodato Simo
  OP_DSC_FIELD = "group_name"
1747 65e183af Michael Hanselmann
  OP_PARAMS = [
1748 65e183af Michael Hanselmann
    _PGroupName,
1749 65e183af Michael Hanselmann
    ]
1750 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1751 94bd652a Adeodato Simo
1752 94bd652a Adeodato Simo
1753 a8173e82 Iustin Pop
class OpGroupRename(OpCode):
1754 4fe5cf90 Adeodato Simo
  """Rename a node group in the cluster."""
1755 65e183af Michael Hanselmann
  OP_PARAMS = [
1756 12da663a Michael Hanselmann
    _PGroupName,
1757 12da663a Michael Hanselmann
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New group name"),
1758 65e183af Michael Hanselmann
    ]
1759 1456df62 Michael Hanselmann
  OP_RESULT = ht.Comment("New group name")(ht.TNonEmptyString)
1760 4fe5cf90 Adeodato Simo
1761 4fe5cf90 Adeodato Simo
1762 08f8c82c Michael Hanselmann
class OpGroupEvacuate(OpCode):
1763 08f8c82c Michael Hanselmann
  """Evacuate a node group in the cluster."""
1764 08f8c82c Michael Hanselmann
  OP_DSC_FIELD = "group_name"
1765 08f8c82c Michael Hanselmann
  OP_PARAMS = [
1766 08f8c82c Michael Hanselmann
    _PGroupName,
1767 08f8c82c Michael Hanselmann
    _PEarlyRelease,
1768 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for computing solution"),
1769 ff8067cf Michael Hanselmann
    ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString),
1770 08f8c82c Michael Hanselmann
     "Destination group names or UUIDs"),
1771 08f8c82c Michael Hanselmann
    ]
1772 1456df62 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
1773 08f8c82c Michael Hanselmann
1774 08f8c82c Michael Hanselmann
1775 a8083063 Iustin Pop
# OS opcodes
1776 da2d02e7 Iustin Pop
class OpOsDiagnose(OpCode):
1777 a8083063 Iustin Pop
  """Compute the list of guest operating systems."""
1778 65e183af Michael Hanselmann
  OP_PARAMS = [
1779 65e183af Michael Hanselmann
    _POutputFields,
1780 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1781 45d4c81c Michael Hanselmann
     "Which operating systems to diagnose"),
1782 65e183af Michael Hanselmann
    ]
1783 415feb2e René Nussbaumer
  OP_RESULT = _TOldQueryResult
1784 a8083063 Iustin Pop
1785 7c0d6283 Michael Hanselmann
1786 a8083063 Iustin Pop
# Exports opcodes
1787 7ca2d4d8 Iustin Pop
class OpBackupQuery(OpCode):
1788 a8083063 Iustin Pop
  """Compute the list of exported images."""
1789 65e183af Michael Hanselmann
  OP_PARAMS = [
1790 45d4c81c Michael Hanselmann
    _PUseLocking,
1791 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1792 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
1793 65e183af Michael Hanselmann
    ]
1794 202fb4e0 Michael Hanselmann
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString,
1795 202fb4e0 Michael Hanselmann
                         ht.TOr(ht.Comment("False on error")(ht.TBool),
1796 202fb4e0 Michael Hanselmann
                                ht.TListOf(ht.TNonEmptyString)))
1797 a8083063 Iustin Pop
1798 7c0d6283 Michael Hanselmann
1799 71910715 Iustin Pop
class OpBackupPrepare(OpCode):
1800 1410fa8d Michael Hanselmann
  """Prepares an instance export.
1801 1410fa8d Michael Hanselmann

1802 1410fa8d Michael Hanselmann
  @ivar instance_name: Instance name
1803 1410fa8d Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1804 1410fa8d Michael Hanselmann

1805 1410fa8d Michael Hanselmann
  """
1806 1410fa8d Michael Hanselmann
  OP_DSC_FIELD = "instance_name"
1807 65e183af Michael Hanselmann
  OP_PARAMS = [
1808 65e183af Michael Hanselmann
    _PInstanceName,
1809 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1810 45d4c81c Michael Hanselmann
     "Export mode"),
1811 1410fa8d Michael Hanselmann
    ]
1812 fd9f58fd Iustin Pop
  OP_RESULT = ht.TMaybeDict
1813 1410fa8d Michael Hanselmann
1814 1410fa8d Michael Hanselmann
1815 4ff922a2 Iustin Pop
class OpBackupExport(OpCode):
1816 4a96f1d1 Michael Hanselmann
  """Export an instance.
1817 4a96f1d1 Michael Hanselmann

1818 398e9066 Iustin Pop
  For local exports, the export destination is the node name. For
1819 398e9066 Iustin Pop
  remote exports, the export destination is a list of tuples, each
1820 398e9066 Iustin Pop
  consisting of hostname/IP address, port, magic, HMAC and HMAC
1821 398e9066 Iustin Pop
  salt. The HMAC is calculated using the cluster domain secret over
1822 398e9066 Iustin Pop
  the value "${index}:${hostname}:${port}". The destination X509 CA
1823 398e9066 Iustin Pop
  must be a signed certificate.
1824 4a96f1d1 Michael Hanselmann

1825 4a96f1d1 Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1826 4a96f1d1 Michael Hanselmann
  @ivar target_node: Export destination
1827 4a96f1d1 Michael Hanselmann
  @ivar x509_key_name: X509 key to use (remote export only)
1828 4a96f1d1 Michael Hanselmann
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1829 4a96f1d1 Michael Hanselmann
                             only)
1830 4a96f1d1 Michael Hanselmann

1831 4a96f1d1 Michael Hanselmann
  """
1832 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1833 65e183af Michael Hanselmann
  OP_PARAMS = [
1834 65e183af Michael Hanselmann
    _PInstanceName,
1835 65e183af Michael Hanselmann
    _PShutdownTimeout,
1836 4a96f1d1 Michael Hanselmann
    # TODO: Rename target_node as it changes meaning for different export modes
1837 4a96f1d1 Michael Hanselmann
    # (e.g. "destination")
1838 45d4c81c Michael Hanselmann
    ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1839 45d4c81c Michael Hanselmann
     "Destination information, depends on export mode"),
1840 45d4c81c Michael Hanselmann
    ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1841 45d4c81c Michael Hanselmann
    ("remove_instance", False, ht.TBool,
1842 45d4c81c Michael Hanselmann
     "Whether to remove instance after export"),
1843 45d4c81c Michael Hanselmann
    ("ignore_remove_failures", False, ht.TBool,
1844 45d4c81c Michael Hanselmann
     "Whether to ignore failures while removing instances"),
1845 45d4c81c Michael Hanselmann
    ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1846 45d4c81c Michael Hanselmann
     "Export mode"),
1847 fd9f58fd Iustin Pop
    ("x509_key_name", None, ht.TMaybe(ht.TList),
1848 45d4c81c Michael Hanselmann
     "Name of X509 key (remote export only)"),
1849 45d4c81c Michael Hanselmann
    ("destination_x509_ca", None, ht.TMaybeString,
1850 45d4c81c Michael Hanselmann
     "Destination X509 CA (remote export only)"),
1851 17c3f802 Guido Trotter
    ]
1852 202fb4e0 Michael Hanselmann
  OP_RESULT = \
1853 202fb4e0 Michael Hanselmann
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1854 202fb4e0 Michael Hanselmann
      ht.Comment("Finalizing status")(ht.TBool),
1855 202fb4e0 Michael Hanselmann
      ht.Comment("Status for every exported disk")(ht.TListOf(ht.TBool)),
1856 202fb4e0 Michael Hanselmann
      ]))
1857 5c947f38 Iustin Pop
1858 0a7bed64 Michael Hanselmann
1859 ca5890ad Iustin Pop
class OpBackupRemove(OpCode):
1860 9ac99fda Guido Trotter
  """Remove an instance's export."""
1861 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1862 65e183af Michael Hanselmann
  OP_PARAMS = [
1863 65e183af Michael Hanselmann
    _PInstanceName,
1864 65e183af Michael Hanselmann
    ]
1865 202fb4e0 Michael Hanselmann
  OP_RESULT = ht.TNone
1866 5c947f38 Iustin Pop
1867 0a7bed64 Michael Hanselmann
1868 5c947f38 Iustin Pop
# Tags opcodes
1869 c6afb1ca Iustin Pop
class OpTagsGet(OpCode):
1870 5c947f38 Iustin Pop
  """Returns the tags of the given object."""
1871 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
1872 65e183af Michael Hanselmann
  OP_PARAMS = [
1873 65e183af Michael Hanselmann
    _PTagKind,
1874 cfdf561d Michael Hanselmann
    # Not using _PUseLocking as the default is different for historical reasons
1875 cfdf561d Michael Hanselmann
    ("use_locking", True, ht.TBool, "Whether to use synchronization"),
1876 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1877 971b3a98 Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString,
1878 971b3a98 Michael Hanselmann
     "Name of object to retrieve tags from"),
1879 65e183af Michael Hanselmann
    ]
1880 48796673 Michael Hanselmann
  OP_RESULT = ht.TListOf(ht.TNonEmptyString)
1881 5c947f38 Iustin Pop
1882 5c947f38 Iustin Pop
1883 715462e7 Iustin Pop
class OpTagsSearch(OpCode):
1884 73415719 Iustin Pop
  """Searches the tags in the cluster for a given pattern."""
1885 60dd1473 Iustin Pop
  OP_DSC_FIELD = "pattern"
1886 65e183af Michael Hanselmann
  OP_PARAMS = [
1887 971b3a98 Michael Hanselmann
    ("pattern", ht.NoDefault, ht.TNonEmptyString,
1888 971b3a98 Michael Hanselmann
     "Search pattern (regular expression)"),
1889 65e183af Michael Hanselmann
    ]
1890 48796673 Michael Hanselmann
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(2), ht.TItems([
1891 48796673 Michael Hanselmann
    ht.TNonEmptyString,
1892 48796673 Michael Hanselmann
    ht.TNonEmptyString,
1893 48796673 Michael Hanselmann
    ])))
1894 73415719 Iustin Pop
1895 73415719 Iustin Pop
1896 d1602edc Iustin Pop
class OpTagsSet(OpCode):
1897 f27302fa Iustin Pop
  """Add a list of tags on a given object."""
1898 65e183af Michael Hanselmann
  OP_PARAMS = [
1899 65e183af Michael Hanselmann
    _PTagKind,
1900 65e183af Michael Hanselmann
    _PTags,
1901 bcd35e09 Dato Simó
    # Name is only meaningful for groups, nodes and instances
1902 971b3a98 Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString,
1903 971b3a98 Michael Hanselmann
     "Name of object where tag(s) should be added"),
1904 65e183af Michael Hanselmann
    ]
1905 48796673 Michael Hanselmann
  OP_RESULT = ht.TNone
1906 5c947f38 Iustin Pop
1907 5c947f38 Iustin Pop
1908 3f0ab95f Iustin Pop
class OpTagsDel(OpCode):
1909 f27302fa Iustin Pop
  """Remove a list of tags from a given object."""
1910 65e183af Michael Hanselmann
  OP_PARAMS = [
1911 65e183af Michael Hanselmann
    _PTagKind,
1912 65e183af Michael Hanselmann
    _PTags,
1913 bcd35e09 Dato Simó
    # Name is only meaningful for groups, nodes and instances
1914 971b3a98 Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString,
1915 971b3a98 Michael Hanselmann
     "Name of object where tag(s) should be deleted"),
1916 65e183af Michael Hanselmann
    ]
1917 48796673 Michael Hanselmann
  OP_RESULT = ht.TNone
1918 06009e27 Iustin Pop
1919 e687ec01 Michael Hanselmann
1920 06009e27 Iustin Pop
# Test opcodes
1921 06009e27 Iustin Pop
class OpTestDelay(OpCode):
1922 06009e27 Iustin Pop
  """Sleeps for a configured amount of time.
1923 06009e27 Iustin Pop

1924 06009e27 Iustin Pop
  This is used just for debugging and testing.
1925 06009e27 Iustin Pop

1926 06009e27 Iustin Pop
  Parameters:
1927 06009e27 Iustin Pop
    - duration: the time to sleep
1928 06009e27 Iustin Pop
    - on_master: if true, sleep on the master
1929 06009e27 Iustin Pop
    - on_nodes: list of nodes in which to sleep
1930 06009e27 Iustin Pop

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

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

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

1941 06009e27 Iustin Pop
  """
1942 60dd1473 Iustin Pop
  OP_DSC_FIELD = "duration"
1943 65e183af Michael Hanselmann
  OP_PARAMS = [
1944 b795a775 Michael Hanselmann
    ("duration", ht.NoDefault, ht.TNumber, None),
1945 197b323b Michael Hanselmann
    ("on_master", True, ht.TBool, None),
1946 197b323b Michael Hanselmann
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1947 2c9fa1ff Iustin Pop
    ("repeat", 0, ht.TNonNegativeInt, None),
1948 65e183af Michael Hanselmann
    ]
1949 d61df03e Iustin Pop
1950 d61df03e Iustin Pop
1951 d61df03e Iustin Pop
class OpTestAllocator(OpCode):
1952 d61df03e Iustin Pop
  """Allocator framework testing.
1953 d61df03e Iustin Pop

1954 d61df03e Iustin Pop
  This opcode has two modes:
1955 d61df03e Iustin Pop
    - gather and return allocator input for a given mode (allocate new
1956 d61df03e Iustin Pop
      or replace secondary) and a given instance definition (direction
1957 d61df03e Iustin Pop
      'in')
1958 d61df03e Iustin Pop
    - run a selected allocator for a given operation (as above) and
1959 d61df03e Iustin Pop
      return the allocator output (direction 'out')
1960 d61df03e Iustin Pop

1961 d61df03e Iustin Pop
  """
1962 55b7e783 Iustin Pop
  OP_DSC_FIELD = "iallocator"
1963 65e183af Michael Hanselmann
  OP_PARAMS = [
1964 65e183af Michael Hanselmann
    ("direction", ht.NoDefault,
1965 197b323b Michael Hanselmann
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
1966 197b323b Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
1967 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
1968 ff8067cf Michael Hanselmann
    ("nics", ht.NoDefault,
1969 ff8067cf Michael Hanselmann
     ht.TMaybeListOf(ht.TDictOf(ht.TElemOf([constants.INIC_MAC,
1970 ff8067cf Michael Hanselmann
                                            constants.INIC_IP,
1971 ff8067cf Michael Hanselmann
                                            "bridge"]),
1972 fd9f58fd Iustin Pop
                                ht.TMaybeString)),
1973 ff8067cf Michael Hanselmann
     None),
1974 fd9f58fd Iustin Pop
    ("disks", ht.NoDefault, ht.TMaybe(ht.TList), None),
1975 197b323b Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString, None),
1976 55b7e783 Iustin Pop
    _PIAllocFromDesc(None),
1977 197b323b Michael Hanselmann
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1978 fd9f58fd Iustin Pop
    ("memory", None, ht.TMaybe(ht.TNonNegativeInt), None),
1979 fd9f58fd Iustin Pop
    ("vcpus", None, ht.TMaybe(ht.TNonNegativeInt), None),
1980 197b323b Michael Hanselmann
    ("os", None, ht.TMaybeString, None),
1981 197b323b Michael Hanselmann
    ("disk_template", None, ht.TMaybeString, None),
1982 ff8067cf Michael Hanselmann
    ("instances", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
1983 60152bbe Michael Hanselmann
    ("evac_mode", None,
1984 fd9f58fd Iustin Pop
     ht.TMaybe(ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
1985 ff8067cf Michael Hanselmann
    ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
1986 2c9fa1ff Iustin Pop
    ("spindle_use", 1, ht.TNonNegativeInt, None),
1987 2c9fa1ff Iustin Pop
    ("count", 1, ht.TNonNegativeInt, None),
1988 d61df03e Iustin Pop
    ]
1989 363acb1e Iustin Pop
1990 76aef8fc Michael Hanselmann
1991 b469eb4d Iustin Pop
class OpTestJqueue(OpCode):
1992 e58f87a9 Michael Hanselmann
  """Utility opcode to test some aspects of the job queue.
1993 e58f87a9 Michael Hanselmann

1994 e58f87a9 Michael Hanselmann
  """
1995 65e183af Michael Hanselmann
  OP_PARAMS = [
1996 197b323b Michael Hanselmann
    ("notify_waitlock", False, ht.TBool, None),
1997 197b323b Michael Hanselmann
    ("notify_exec", False, ht.TBool, None),
1998 197b323b Michael Hanselmann
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
1999 197b323b Michael Hanselmann
    ("fail", False, ht.TBool, None),
2000 e58f87a9 Michael Hanselmann
    ]
2001 e58f87a9 Michael Hanselmann
2002 e58f87a9 Michael Hanselmann
2003 be760ba8 Michael Hanselmann
class OpTestDummy(OpCode):
2004 be760ba8 Michael Hanselmann
  """Utility opcode used by unittests.
2005 be760ba8 Michael Hanselmann

2006 be760ba8 Michael Hanselmann
  """
2007 65e183af Michael Hanselmann
  OP_PARAMS = [
2008 197b323b Michael Hanselmann
    ("result", ht.NoDefault, ht.NoType, None),
2009 197b323b Michael Hanselmann
    ("messages", ht.NoDefault, ht.NoType, None),
2010 197b323b Michael Hanselmann
    ("fail", ht.NoDefault, ht.NoType, None),
2011 6a373640 Michael Hanselmann
    ("submit_jobs", None, ht.NoType, None),
2012 be760ba8 Michael Hanselmann
    ]
2013 687c10d9 Iustin Pop
  WITH_LU = False
2014 be760ba8 Michael Hanselmann
2015 be760ba8 Michael Hanselmann
2016 eaa4c57c Dimitris Aragiorgis
# Network opcodes
2017 eaa4c57c Dimitris Aragiorgis
# Add a new network in the cluster
2018 eaa4c57c Dimitris Aragiorgis
class OpNetworkAdd(OpCode):
2019 eaa4c57c Dimitris Aragiorgis
  """Add an IP network to the cluster."""
2020 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2021 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2022 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2023 e055a2ab Dimitris Aragiorgis
    ("network_type", None, ht.TMaybe(_CheckNetworkType), "Network type"),
2024 e1494c96 Iustin Pop
    ("network", ht.NoDefault, _TIpNetwork4, "IPv4 subnet"),
2025 e1494c96 Iustin Pop
    ("gateway", None, ht.TMaybe(_TIpAddress4), "IPv4 gateway"),
2026 e1494c96 Iustin Pop
    ("network6", None, ht.TMaybe(_TIpNetwork6), "IPv6 subnet"),
2027 e1494c96 Iustin Pop
    ("gateway6", None, ht.TMaybe(_TIpAddress6), "IPv6 gateway"),
2028 6e8091f9 Dimitris Aragiorgis
    ("mac_prefix", None, ht.TMaybeString,
2029 32e3d8b1 Michael Hanselmann
     "MAC address prefix that overrides cluster one"),
2030 e1494c96 Iustin Pop
    ("add_reserved_ips", None, _TMaybeAddr4List,
2031 32e3d8b1 Michael Hanselmann
     "Which IP addresses to reserve"),
2032 213076fe Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool,
2033 213076fe Dimitris Aragiorgis
     "Whether to check for conflicting IP addresses"),
2034 8140e24f Dimitris Aragiorgis
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Network tags"),
2035 eaa4c57c Dimitris Aragiorgis
    ]
2036 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2037 eaa4c57c Dimitris Aragiorgis
2038 3c286190 Dimitris Aragiorgis
2039 eaa4c57c Dimitris Aragiorgis
class OpNetworkRemove(OpCode):
2040 eaa4c57c Dimitris Aragiorgis
  """Remove an existing network from the cluster.
2041 eaa4c57c Dimitris Aragiorgis
     Must not be connected to any nodegroup.
2042 eaa4c57c Dimitris Aragiorgis

2043 eaa4c57c Dimitris Aragiorgis
  """
2044 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2045 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2046 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2047 eaa4c57c Dimitris Aragiorgis
    _PForce,
2048 eaa4c57c Dimitris Aragiorgis
    ]
2049 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2050 eaa4c57c Dimitris Aragiorgis
2051 3c286190 Dimitris Aragiorgis
2052 eaa4c57c Dimitris Aragiorgis
class OpNetworkSetParams(OpCode):
2053 eaa4c57c Dimitris Aragiorgis
  """Modify Network's parameters except for IPv4 subnet"""
2054 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2055 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2056 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2057 e055a2ab Dimitris Aragiorgis
    ("network_type", None, ht.TMaybeValueNone(_CheckNetworkType),
2058 e055a2ab Dimitris Aragiorgis
     "Network type"),
2059 e055a2ab Dimitris Aragiorgis
    ("gateway", None, ht.TMaybeValueNone(_TIpAddress4), "IPv4 gateway"),
2060 e055a2ab Dimitris Aragiorgis
    ("network6", None, ht.TMaybeValueNone(_TIpNetwork6), "IPv6 subnet"),
2061 e055a2ab Dimitris Aragiorgis
    ("gateway6", None, ht.TMaybeValueNone(_TIpAddress6), "IPv6 gateway"),
2062 e055a2ab Dimitris Aragiorgis
    ("mac_prefix", None, ht.TMaybeValueNone(ht.TString),
2063 32e3d8b1 Michael Hanselmann
     "MAC address prefix that overrides cluster one"),
2064 e1494c96 Iustin Pop
    ("add_reserved_ips", None, _TMaybeAddr4List,
2065 32e3d8b1 Michael Hanselmann
     "Which external IP addresses to reserve"),
2066 e1494c96 Iustin Pop
    ("remove_reserved_ips", None, _TMaybeAddr4List,
2067 32e3d8b1 Michael Hanselmann
     "Which external IP addresses to release"),
2068 eaa4c57c Dimitris Aragiorgis
    ]
2069 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2070 eaa4c57c Dimitris Aragiorgis
2071 3c286190 Dimitris Aragiorgis
2072 eaa4c57c Dimitris Aragiorgis
class OpNetworkConnect(OpCode):
2073 eaa4c57c Dimitris Aragiorgis
  """Connect a Network to a specific Nodegroup with the defined netparams
2074 eaa4c57c Dimitris Aragiorgis
     (mode, link). Nics in this Network will inherit those params.
2075 eaa4c57c Dimitris Aragiorgis
     Produce errors if a NIC (that its not already assigned to a network)
2076 eaa4c57c Dimitris Aragiorgis
     has an IP that is contained in the Network this will produce error unless
2077 eaa4c57c Dimitris Aragiorgis
     --no-conflicts-check is passed.
2078 eaa4c57c Dimitris Aragiorgis

2079 eaa4c57c Dimitris Aragiorgis
  """
2080 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2081 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2082 eaa4c57c Dimitris Aragiorgis
    _PGroupName,
2083 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2084 e1494c96 Iustin Pop
    ("network_mode", ht.NoDefault, ht.TString, "Connectivity mode"),
2085 e1494c96 Iustin Pop
    ("network_link", ht.NoDefault, ht.TString, "Connectivity link"),
2086 6e8091f9 Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool, "Whether to check for conflicting IPs"),
2087 eaa4c57c Dimitris Aragiorgis
    ]
2088 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2089 eaa4c57c Dimitris Aragiorgis
2090 3c286190 Dimitris Aragiorgis
2091 eaa4c57c Dimitris Aragiorgis
class OpNetworkDisconnect(OpCode):
2092 eaa4c57c Dimitris Aragiorgis
  """Disconnect a Network from a Nodegroup. Produce errors if NICs are
2093 eaa4c57c Dimitris Aragiorgis
     present in the Network unless --no-conficts-check option is passed.
2094 eaa4c57c Dimitris Aragiorgis

2095 eaa4c57c Dimitris Aragiorgis
  """
2096 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2097 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2098 eaa4c57c Dimitris Aragiorgis
    _PGroupName,
2099 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2100 6e8091f9 Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool, "Whether to check for conflicting IPs"),
2101 eaa4c57c Dimitris Aragiorgis
    ]
2102 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2103 eaa4c57c Dimitris Aragiorgis
2104 3c286190 Dimitris Aragiorgis
2105 eaa4c57c Dimitris Aragiorgis
class OpNetworkQuery(OpCode):
2106 eaa4c57c Dimitris Aragiorgis
  """Compute the list of networks."""
2107 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2108 eaa4c57c Dimitris Aragiorgis
    _POutputFields,
2109 eaa4c57c Dimitris Aragiorgis
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
2110 eaa4c57c Dimitris Aragiorgis
     "Empty list to query all groups, group names otherwise"),
2111 eaa4c57c Dimitris Aragiorgis
    ]
2112 829cfbc5 Dimitris Aragiorgis
  OP_RESULT = _TOldQueryResult
2113 eaa4c57c Dimitris Aragiorgis
2114 eaa4c57c Dimitris Aragiorgis
2115 dbc96028 Michael Hanselmann
def _GetOpList():
2116 dbc96028 Michael Hanselmann
  """Returns list of all defined opcodes.
2117 dbc96028 Michael Hanselmann

2118 dbc96028 Michael Hanselmann
  Does not eliminate duplicates by C{OP_ID}.
2119 dbc96028 Michael Hanselmann

2120 dbc96028 Michael Hanselmann
  """
2121 dbc96028 Michael Hanselmann
  return [v for v in globals().values()
2122 dbc96028 Michael Hanselmann
          if (isinstance(v, type) and issubclass(v, OpCode) and
2123 687c10d9 Iustin Pop
              hasattr(v, "OP_ID") and v is not OpCode)]
2124 dbc96028 Michael Hanselmann
2125 dbc96028 Michael Hanselmann
2126 dbc96028 Michael Hanselmann
OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())