Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 383477e9

History | View | Annotate | Download (65.4 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 1f1188c3 Michael Hanselmann
#: Opportunistic locking
163 1f1188c3 Michael Hanselmann
_POpportunisticLocking = \
164 1f1188c3 Michael Hanselmann
  ("opportunistic_locking", False, ht.TBool,
165 1f1188c3 Michael Hanselmann
   ("Whether to employ opportunistic locking for nodes, meaning nodes"
166 1f1188c3 Michael Hanselmann
    " already locked by another opcode won't be considered for instance"
167 1f1188c3 Michael Hanselmann
    " allocation (only when an iallocator is used)"))
168 b6aaf437 René Nussbaumer
169 b6aaf437 René Nussbaumer
_PIgnoreIpolicy = ("ignore_ipolicy", False, ht.TBool,
170 b6aaf437 René Nussbaumer
                   "Whether to ignore ipolicy violations")
171 b6aaf437 René Nussbaumer
172 8c0b16f6 Guido Trotter
# Allow runtime changes while migrating
173 8c0b16f6 Guido Trotter
_PAllowRuntimeChgs = ("allow_runtime_changes", True, ht.TBool,
174 8c0b16f6 Guido Trotter
                      "Allow runtime changes (eg. memory ballooning)")
175 8c0b16f6 Guido Trotter
176 89514061 Iustin Pop
#: IAllocator field builder
177 89514061 Iustin Pop
_PIAllocFromDesc = lambda desc: ("iallocator", None, ht.TMaybeString, desc)
178 89514061 Iustin Pop
179 eaa4c57c Dimitris Aragiorgis
#: a required network name
180 eaa4c57c Dimitris Aragiorgis
_PNetworkName = ("network_name", ht.NoDefault, ht.TNonEmptyString,
181 eaa4c57c Dimitris Aragiorgis
                 "Set network name")
182 8c0b16f6 Guido Trotter
183 ff0d18e6 Iustin Pop
#: OP_ID conversion regular expression
184 ff0d18e6 Iustin Pop
_OPID_RE = re.compile("([a-z])([A-Z])")
185 ff0d18e6 Iustin Pop
186 8c9ee749 Michael Hanselmann
#: Utility function for L{OpClusterSetParams}
187 8f227489 Michael Hanselmann
_TestClusterOsListItem = \
188 8f227489 Michael Hanselmann
  ht.TAnd(ht.TIsLength(2), ht.TItems([
189 8f227489 Michael Hanselmann
    ht.TElemOf(constants.DDMS_VALUES),
190 8f227489 Michael Hanselmann
    ht.TNonEmptyString,
191 8f227489 Michael Hanselmann
    ]))
192 8c9ee749 Michael Hanselmann
193 ff8067cf Michael Hanselmann
_TestClusterOsList = ht.TMaybeListOf(_TestClusterOsListItem)
194 ff0d18e6 Iustin Pop
195 526a662a Michael Hanselmann
# TODO: Generate check from constants.INIC_PARAMS_TYPES
196 526a662a Michael Hanselmann
#: Utility function for testing NIC definitions
197 20a32e74 Michael Hanselmann
_TestNicDef = \
198 20a32e74 Michael Hanselmann
  ht.Comment("NIC parameters")(ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
199 fd9f58fd Iustin Pop
                                          ht.TMaybeString))
200 526a662a Michael Hanselmann
201 1456df62 Michael Hanselmann
_TSetParamsResultItemItems = [
202 1456df62 Michael Hanselmann
  ht.Comment("name of changed parameter")(ht.TNonEmptyString),
203 b3d2ee31 Michael Hanselmann
  ht.Comment("new value")(ht.TAny),
204 1456df62 Michael Hanselmann
  ]
205 1456df62 Michael Hanselmann
206 1456df62 Michael Hanselmann
_TSetParamsResult = \
207 1456df62 Michael Hanselmann
  ht.TListOf(ht.TAnd(ht.TIsLength(len(_TSetParamsResultItemItems)),
208 1456df62 Michael Hanselmann
                     ht.TItems(_TSetParamsResultItemItems)))
209 1456df62 Michael Hanselmann
210 3e3ddbf0 Constantinos Venetsanopoulos
# In the disks option we can provide arbitrary parameters too, which
211 3e3ddbf0 Constantinos Venetsanopoulos
# we may not be able to validate at this level, so we just check the
212 3e3ddbf0 Constantinos Venetsanopoulos
# format of the dict here and the checks concerning IDISK_PARAMS will
213 3e3ddbf0 Constantinos Venetsanopoulos
# happen at the LU level
214 20a32e74 Michael Hanselmann
_TDiskParams = \
215 3e3ddbf0 Constantinos Venetsanopoulos
  ht.Comment("Disk parameters")(ht.TDictOf(ht.TNonEmptyString,
216 20a32e74 Michael Hanselmann
                                           ht.TOr(ht.TNonEmptyString, ht.TInt)))
217 735e1318 Michael Hanselmann
218 aa12a891 René Nussbaumer
_TQueryRow = \
219 aa12a891 René Nussbaumer
  ht.TListOf(ht.TAnd(ht.TIsLength(2),
220 aa12a891 René Nussbaumer
                     ht.TItems([ht.TElemOf(constants.RS_ALL),
221 aa12a891 René Nussbaumer
                                ht.TAny])))
222 aa12a891 René Nussbaumer
223 415feb2e René Nussbaumer
_TQueryResult = ht.TListOf(_TQueryRow)
224 415feb2e René Nussbaumer
225 415feb2e René Nussbaumer
_TOldQueryRow = ht.TListOf(ht.TAny)
226 415feb2e René Nussbaumer
227 415feb2e René Nussbaumer
_TOldQueryResult = ht.TListOf(_TOldQueryRow)
228 415feb2e René Nussbaumer
229 415feb2e René Nussbaumer
230 3ce9a5e7 Michael Hanselmann
_SUMMARY_PREFIX = {
231 3ce9a5e7 Michael Hanselmann
  "CLUSTER_": "C_",
232 3ce9a5e7 Michael Hanselmann
  "GROUP_": "G_",
233 3ce9a5e7 Michael Hanselmann
  "NODE_": "N_",
234 3ce9a5e7 Michael Hanselmann
  "INSTANCE_": "I_",
235 3ce9a5e7 Michael Hanselmann
  }
236 3ce9a5e7 Michael Hanselmann
237 b95479a5 Michael Hanselmann
#: Attribute name for dependencies
238 b95479a5 Michael Hanselmann
DEPEND_ATTR = "depends"
239 b95479a5 Michael Hanselmann
240 018ae30b Michael Hanselmann
#: Attribute name for comment
241 018ae30b Michael Hanselmann
COMMENT_ATTR = "comment"
242 018ae30b Michael Hanselmann
243 526a662a Michael Hanselmann
244 ff0d18e6 Iustin Pop
def _NameToId(name):
245 ff0d18e6 Iustin Pop
  """Convert an opcode class name to an OP_ID.
246 ff0d18e6 Iustin Pop

247 ff0d18e6 Iustin Pop
  @type name: string
248 ff0d18e6 Iustin Pop
  @param name: the class name, as OpXxxYyy
249 ff0d18e6 Iustin Pop
  @rtype: string
250 ff0d18e6 Iustin Pop
  @return: the name in the OP_XXXX_YYYY format
251 ff0d18e6 Iustin Pop

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

268 415feb2e René Nussbaumer
  @param obj: The object to generate type checks
269 415feb2e René Nussbaumer
  @param fields_types: The fields and their types as a dict
270 415feb2e René Nussbaumer
  @return: A ht type check function
271 415feb2e René Nussbaumer

272 415feb2e René Nussbaumer
  """
273 415feb2e René Nussbaumer
  assert set(obj.GetAllSlots()) == set(fields_types.keys()), \
274 415feb2e René Nussbaumer
    "%s != %s" % (set(obj.GetAllSlots()), set(fields_types.keys()))
275 415feb2e René Nussbaumer
  return ht.TStrictDict(True, True, fields_types)
276 415feb2e René Nussbaumer
277 415feb2e René Nussbaumer
278 b02c6bdf Michael Hanselmann
_TQueryFieldDef = \
279 b02c6bdf Michael Hanselmann
  _GenerateObjectTypeCheck(objects.QueryFieldDefinition, {
280 b02c6bdf Michael Hanselmann
    "name": ht.TNonEmptyString,
281 b02c6bdf Michael Hanselmann
    "title": ht.TNonEmptyString,
282 b02c6bdf Michael Hanselmann
    "kind": ht.TElemOf(constants.QFT_ALL),
283 b02c6bdf Michael Hanselmann
    "doc": ht.TNonEmptyString,
284 b02c6bdf Michael Hanselmann
    })
285 415feb2e René Nussbaumer
286 415feb2e René Nussbaumer
287 65e183af Michael Hanselmann
def RequireFileStorage():
288 65e183af Michael Hanselmann
  """Checks that file storage is enabled.
289 65e183af Michael Hanselmann

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

293 65e183af Michael Hanselmann
  @raise errors.OpPrereqError: when file storage is disabled
294 65e183af Michael Hanselmann

295 65e183af Michael Hanselmann
  """
296 65e183af Michael Hanselmann
  if not constants.ENABLE_FILE_STORAGE:
297 65e183af Michael Hanselmann
    raise errors.OpPrereqError("File storage disabled at configure time",
298 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
299 65e183af Michael Hanselmann
300 65e183af Michael Hanselmann
301 4b97f902 Apollon Oikonomopoulos
def RequireSharedFileStorage():
302 4b97f902 Apollon Oikonomopoulos
  """Checks that shared file storage is enabled.
303 4b97f902 Apollon Oikonomopoulos

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

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

309 4b97f902 Apollon Oikonomopoulos
  """
310 4b97f902 Apollon Oikonomopoulos
  if not constants.ENABLE_SHARED_FILE_STORAGE:
311 4b97f902 Apollon Oikonomopoulos
    raise errors.OpPrereqError("Shared file storage disabled at"
312 4b97f902 Apollon Oikonomopoulos
                               " configure time", errors.ECODE_INVAL)
313 4b97f902 Apollon Oikonomopoulos
314 4b97f902 Apollon Oikonomopoulos
315 8c9ee749 Michael Hanselmann
@ht.WithDesc("CheckFileStorage")
316 8c9ee749 Michael Hanselmann
def _CheckFileStorage(value):
317 8c9ee749 Michael Hanselmann
  """Ensures file storage is enabled if used.
318 65e183af Michael Hanselmann

319 65e183af Michael Hanselmann
  """
320 8c9ee749 Michael Hanselmann
  if value == constants.DT_FILE:
321 65e183af Michael Hanselmann
    RequireFileStorage()
322 4b97f902 Apollon Oikonomopoulos
  elif value == constants.DT_SHARED_FILE:
323 4b97f902 Apollon Oikonomopoulos
    RequireSharedFileStorage()
324 65e183af Michael Hanselmann
  return True
325 65e183af Michael Hanselmann
326 65e183af Michael Hanselmann
327 45fe090b Michael Hanselmann
def _BuildDiskTemplateCheck(accept_none):
328 45fe090b Michael Hanselmann
  """Builds check for disk template.
329 45fe090b Michael Hanselmann

330 45fe090b Michael Hanselmann
  @type accept_none: bool
331 45fe090b Michael Hanselmann
  @param accept_none: whether to accept None as a correct value
332 45fe090b Michael Hanselmann
  @rtype: callable
333 45fe090b Michael Hanselmann

334 45fe090b Michael Hanselmann
  """
335 45fe090b Michael Hanselmann
  template_check = ht.TElemOf(constants.DISK_TEMPLATES)
336 45fe090b Michael Hanselmann
337 45fe090b Michael Hanselmann
  if accept_none:
338 fd9f58fd Iustin Pop
    template_check = ht.TMaybe(template_check)
339 45fe090b Michael Hanselmann
340 45fe090b Michael Hanselmann
  return ht.TAnd(template_check, _CheckFileStorage)
341 8c9ee749 Michael Hanselmann
342 8c9ee749 Michael Hanselmann
343 65e183af Michael Hanselmann
def _CheckStorageType(storage_type):
344 65e183af Michael Hanselmann
  """Ensure a given storage type is valid.
345 65e183af Michael Hanselmann

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

367 eaa4c57c Dimitris Aragiorgis
  """
368 eaa4c57c Dimitris Aragiorgis
  try:
369 eaa4c57c Dimitris Aragiorgis
    ipaddr.IPv4Network(value)
370 eaa4c57c Dimitris Aragiorgis
  except ipaddr.AddressValueError:
371 eaa4c57c Dimitris Aragiorgis
    return False
372 eaa4c57c Dimitris Aragiorgis
  return True
373 eaa4c57c Dimitris Aragiorgis
374 3c286190 Dimitris Aragiorgis
375 16091a6e Michael Hanselmann
@ht.WithDesc("IPv4 address")
376 eaa4c57c Dimitris Aragiorgis
def _CheckCIDRAddrNotation(value):
377 16091a6e Michael Hanselmann
  """Ensure a given CIDR notation type is valid.
378 eaa4c57c Dimitris Aragiorgis

379 eaa4c57c Dimitris Aragiorgis
  """
380 eaa4c57c Dimitris Aragiorgis
  try:
381 eaa4c57c Dimitris Aragiorgis
    ipaddr.IPv4Address(value)
382 eaa4c57c Dimitris Aragiorgis
  except ipaddr.AddressValueError:
383 eaa4c57c Dimitris Aragiorgis
    return False
384 eaa4c57c Dimitris Aragiorgis
  return True
385 eaa4c57c Dimitris Aragiorgis
386 3c286190 Dimitris Aragiorgis
387 16091a6e Michael Hanselmann
@ht.WithDesc("IPv6 address")
388 eaa4c57c Dimitris Aragiorgis
def _CheckCIDR6AddrNotation(value):
389 16091a6e Michael Hanselmann
  """Ensure a given CIDR notation type is valid.
390 eaa4c57c Dimitris Aragiorgis

391 eaa4c57c Dimitris Aragiorgis
  """
392 eaa4c57c Dimitris Aragiorgis
  try:
393 eaa4c57c Dimitris Aragiorgis
    ipaddr.IPv6Address(value)
394 eaa4c57c Dimitris Aragiorgis
  except ipaddr.AddressValueError:
395 eaa4c57c Dimitris Aragiorgis
    return False
396 eaa4c57c Dimitris Aragiorgis
  return True
397 eaa4c57c Dimitris Aragiorgis
398 3c286190 Dimitris Aragiorgis
399 16091a6e Michael Hanselmann
@ht.WithDesc("IPv6 network")
400 eaa4c57c Dimitris Aragiorgis
def _CheckCIDR6NetNotation(value):
401 16091a6e Michael Hanselmann
  """Ensure a given CIDR notation type is valid.
402 eaa4c57c Dimitris Aragiorgis

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

421 65e183af Michael Hanselmann
  """
422 65e183af Michael Hanselmann
  def __new__(mcs, name, bases, attrs):
423 65e183af Michael Hanselmann
    """Called when a class should be created.
424 65e183af Michael Hanselmann

425 65e183af Michael Hanselmann
    @param mcs: The meta class
426 65e183af Michael Hanselmann
    @param name: Name of created class
427 65e183af Michael Hanselmann
    @param bases: Base classes
428 65e183af Michael Hanselmann
    @type attrs: dict
429 65e183af Michael Hanselmann
    @param attrs: Class attributes
430 65e183af Michael Hanselmann

431 65e183af Michael Hanselmann
    """
432 e89a9021 Iustin Pop
    assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
433 ff0d18e6 Iustin Pop
434 32683096 René Nussbaumer
    slots = mcs._GetSlots(attrs)
435 32683096 René Nussbaumer
    assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
436 32683096 René Nussbaumer
      "Class '%s' uses unknown field in OP_DSC_FIELD" % name
437 32683096 René Nussbaumer
438 e89a9021 Iustin Pop
    attrs["OP_ID"] = _NameToId(name)
439 65e183af Michael Hanselmann
440 32683096 René Nussbaumer
    return objectutils.AutoSlots.__new__(mcs, name, bases, attrs)
441 32683096 René Nussbaumer
442 32683096 René Nussbaumer
  @classmethod
443 32683096 René Nussbaumer
  def _GetSlots(mcs, attrs):
444 32683096 René Nussbaumer
    """Build the slots out of OP_PARAMS.
445 32683096 René Nussbaumer

446 32683096 René Nussbaumer
    """
447 65e183af Michael Hanselmann
    # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
448 65e183af Michael Hanselmann
    params = attrs.setdefault("OP_PARAMS", [])
449 65e183af Michael Hanselmann
450 65e183af Michael Hanselmann
    # Use parameter names as slots
451 32683096 René Nussbaumer
    return [pname for (pname, _, _, _) in params]
452 65e183af Michael Hanselmann
453 df458e0b Iustin Pop
454 32683096 René Nussbaumer
class BaseOpCode(objectutils.ValidatedSlots):
455 df458e0b Iustin Pop
  """A simple serializable object.
456 df458e0b Iustin Pop

457 0e46916d Iustin Pop
  This object serves as a parent class for OpCode without any custom
458 0e46916d Iustin Pop
  field handling.
459 0e46916d Iustin Pop

460 df458e0b Iustin Pop
  """
461 b459a848 Andrea Spadaccini
  # pylint: disable=E1101
462 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
463 65e183af Michael Hanselmann
  __metaclass__ = _AutoOpParamSlots
464 65e183af Michael Hanselmann
465 df458e0b Iustin Pop
  def __getstate__(self):
466 a7399f66 Iustin Pop
    """Generic serializer.
467 a7399f66 Iustin Pop

468 a7399f66 Iustin Pop
    This method just returns the contents of the instance as a
469 a7399f66 Iustin Pop
    dictionary.
470 a7399f66 Iustin Pop

471 a7399f66 Iustin Pop
    @rtype:  C{dict}
472 a7399f66 Iustin Pop
    @return: the instance attributes and their values
473 a7399f66 Iustin Pop

474 a7399f66 Iustin Pop
    """
475 df458e0b Iustin Pop
    state = {}
476 32683096 René Nussbaumer
    for name in self.GetAllSlots():
477 df458e0b Iustin Pop
      if hasattr(self, name):
478 df458e0b Iustin Pop
        state[name] = getattr(self, name)
479 df458e0b Iustin Pop
    return state
480 df458e0b Iustin Pop
481 df458e0b Iustin Pop
  def __setstate__(self, state):
482 a7399f66 Iustin Pop
    """Generic unserializer.
483 a7399f66 Iustin Pop

484 a7399f66 Iustin Pop
    This method just restores from the serialized state the attributes
485 a7399f66 Iustin Pop
    of the current instance.
486 a7399f66 Iustin Pop

487 a7399f66 Iustin Pop
    @param state: the serialized opcode data
488 a7399f66 Iustin Pop
    @type state:  C{dict}
489 a7399f66 Iustin Pop

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

506 65e183af Michael Hanselmann
    """
507 65e183af Michael Hanselmann
    slots = []
508 65e183af Michael Hanselmann
    for parent in cls.__mro__:
509 65e183af Michael Hanselmann
      slots.extend(getattr(parent, "OP_PARAMS", []))
510 65e183af Michael Hanselmann
    return slots
511 65e183af Michael Hanselmann
512 32683096 René Nussbaumer
  def Validate(self, set_defaults): # pylint: disable=W0221
513 1cbef6d8 Michael Hanselmann
    """Validate opcode parameters, optionally setting default values.
514 1cbef6d8 Michael Hanselmann

515 1cbef6d8 Michael Hanselmann
    @type set_defaults: bool
516 1cbef6d8 Michael Hanselmann
    @param set_defaults: Whether to set default values
517 1cbef6d8 Michael Hanselmann
    @raise errors.OpPrereqError: When a parameter value doesn't match
518 1cbef6d8 Michael Hanselmann
                                 requirements
519 1cbef6d8 Michael Hanselmann

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

554 b247c6fc Michael Hanselmann
  @type relative: bool
555 b247c6fc Michael Hanselmann
  @param relative: Whether to accept relative job IDs (negative)
556 b247c6fc Michael Hanselmann
  @rtype: callable
557 b247c6fc Michael Hanselmann

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

593 a7399f66 Iustin Pop
  This is the root of the actual OpCode hierarchy. All clases derived
594 a7399f66 Iustin Pop
  from this class should override OP_ID.
595 a7399f66 Iustin Pop

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

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

631 a7399f66 Iustin Pop
    This method adds to the state dictionary the OP_ID of the class,
632 a7399f66 Iustin Pop
    so that on unload we can identify the correct class for
633 a7399f66 Iustin Pop
    instantiating the opcode.
634 a7399f66 Iustin Pop

635 a7399f66 Iustin Pop
    @rtype:   C{dict}
636 a7399f66 Iustin Pop
    @return:  the state as a dictionary
637 a7399f66 Iustin Pop

638 df458e0b Iustin Pop
    """
639 0e46916d Iustin Pop
    data = BaseOpCode.__getstate__(self)
640 df458e0b Iustin Pop
    data["OP_ID"] = self.OP_ID
641 df458e0b Iustin Pop
    return data
642 df458e0b Iustin Pop
643 df458e0b Iustin Pop
  @classmethod
644 00abdc96 Iustin Pop
  def LoadOpCode(cls, data):
645 df458e0b Iustin Pop
    """Generic load opcode method.
646 df458e0b Iustin Pop

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

651 a7399f66 Iustin Pop
    @type data:  C{dict}
652 a7399f66 Iustin Pop
    @param data: the serialized opcode
653 a7399f66 Iustin Pop

654 df458e0b Iustin Pop
    """
655 df458e0b Iustin Pop
    if not isinstance(data, dict):
656 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
657 df458e0b Iustin Pop
    if "OP_ID" not in data:
658 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
659 df458e0b Iustin Pop
    op_id = data["OP_ID"]
660 df458e0b Iustin Pop
    op_class = None
661 363acb1e Iustin Pop
    if op_id in OP_MAPPING:
662 363acb1e Iustin Pop
      op_class = OP_MAPPING[op_id]
663 363acb1e Iustin Pop
    else:
664 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
665 df458e0b Iustin Pop
                       op_id)
666 df458e0b Iustin Pop
    op = op_class()
667 df458e0b Iustin Pop
    new_data = data.copy()
668 df458e0b Iustin Pop
    del new_data["OP_ID"]
669 df458e0b Iustin Pop
    op.__setstate__(new_data)
670 df458e0b Iustin Pop
    return op
671 df458e0b Iustin Pop
672 60dd1473 Iustin Pop
  def Summary(self):
673 60dd1473 Iustin Pop
    """Generates a summary description of this opcode.
674 60dd1473 Iustin Pop

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

681 60dd1473 Iustin Pop
    """
682 ff0d18e6 Iustin Pop
    assert self.OP_ID is not None and len(self.OP_ID) > 3
683 60dd1473 Iustin Pop
    # all OP_ID start with OP_, we remove that
684 60dd1473 Iustin Pop
    txt = self.OP_ID[3:]
685 60dd1473 Iustin Pop
    field_name = getattr(self, "OP_DSC_FIELD", None)
686 60dd1473 Iustin Pop
    if field_name:
687 60dd1473 Iustin Pop
      field_value = getattr(self, field_name, None)
688 bc8bbda1 Iustin Pop
      if isinstance(field_value, (list, tuple)):
689 bc8bbda1 Iustin Pop
        field_value = ",".join(str(i) for i in field_value)
690 60dd1473 Iustin Pop
      txt = "%s(%s)" % (txt, field_value)
691 60dd1473 Iustin Pop
    return txt
692 60dd1473 Iustin Pop
693 3ce9a5e7 Michael Hanselmann
  def TinySummary(self):
694 3ce9a5e7 Michael Hanselmann
    """Generates a compact summary description of the opcode.
695 3ce9a5e7 Michael Hanselmann

696 3ce9a5e7 Michael Hanselmann
    """
697 3ce9a5e7 Michael Hanselmann
    assert self.OP_ID.startswith("OP_")
698 3ce9a5e7 Michael Hanselmann
699 3ce9a5e7 Michael Hanselmann
    text = self.OP_ID[3:]
700 3ce9a5e7 Michael Hanselmann
701 3ce9a5e7 Michael Hanselmann
    for (prefix, supplement) in _SUMMARY_PREFIX.items():
702 3ce9a5e7 Michael Hanselmann
      if text.startswith(prefix):
703 3ce9a5e7 Michael Hanselmann
        return supplement + text[len(prefix):]
704 3ce9a5e7 Michael Hanselmann
705 3ce9a5e7 Michael Hanselmann
    return text
706 3ce9a5e7 Michael Hanselmann
707 a8083063 Iustin Pop
708 afee0879 Iustin Pop
# cluster opcodes
709 afee0879 Iustin Pop
710 bc84ffa7 Iustin Pop
class OpClusterPostInit(OpCode):
711 b5f5fae9 Luca Bigliardi
  """Post cluster initialization.
712 b5f5fae9 Luca Bigliardi

713 b5f5fae9 Luca Bigliardi
  This opcode does not touch the cluster at all. Its purpose is to run hooks
714 b5f5fae9 Luca Bigliardi
  after the cluster has been initialized.
715 b5f5fae9 Luca Bigliardi

716 b5f5fae9 Luca Bigliardi
  """
717 c363310d René Nussbaumer
  OP_RESULT = ht.TBool
718 b5f5fae9 Luca Bigliardi
719 b5f5fae9 Luca Bigliardi
720 c6d43e9e Iustin Pop
class OpClusterDestroy(OpCode):
721 a7399f66 Iustin Pop
  """Destroy the cluster.
722 a7399f66 Iustin Pop

723 a7399f66 Iustin Pop
  This opcode has no other parameters. All the state is irreversibly
724 a7399f66 Iustin Pop
  lost after the execution of this opcode.
725 a7399f66 Iustin Pop

726 a7399f66 Iustin Pop
  """
727 c363310d René Nussbaumer
  OP_RESULT = ht.TNonEmptyString
728 a8083063 Iustin Pop
729 a8083063 Iustin Pop
730 a2f7ab92 Iustin Pop
class OpClusterQuery(OpCode):
731 fdc267f4 Iustin Pop
  """Query cluster information."""
732 415feb2e René Nussbaumer
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TAny)
733 a8083063 Iustin Pop
734 a8083063 Iustin Pop
735 fcad7225 Michael Hanselmann
class OpClusterVerify(OpCode):
736 fcad7225 Michael Hanselmann
  """Submits all jobs necessary to verify the cluster.
737 fcad7225 Michael Hanselmann

738 fcad7225 Michael Hanselmann
  """
739 fcad7225 Michael Hanselmann
  OP_PARAMS = [
740 fcad7225 Michael Hanselmann
    _PDebugSimulateErrors,
741 fcad7225 Michael Hanselmann
    _PErrorCodes,
742 fcad7225 Michael Hanselmann
    _PSkipChecks,
743 93f2399e Andrea Spadaccini
    _PIgnoreErrors,
744 fcad7225 Michael Hanselmann
    _PVerbose,
745 3c286190 Dimitris Aragiorgis
    ("group_name", None, ht.TMaybeString, "Group to verify"),
746 fcad7225 Michael Hanselmann
    ]
747 f7686867 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
748 fcad7225 Michael Hanselmann
749 fcad7225 Michael Hanselmann
750 bf93ae69 Adeodato Simo
class OpClusterVerifyConfig(OpCode):
751 bf93ae69 Adeodato Simo
  """Verify the cluster config.
752 bf93ae69 Adeodato Simo

753 bf93ae69 Adeodato Simo
  """
754 bf93ae69 Adeodato Simo
  OP_PARAMS = [
755 57106b74 Michael Hanselmann
    _PDebugSimulateErrors,
756 57106b74 Michael Hanselmann
    _PErrorCodes,
757 93f2399e Andrea Spadaccini
    _PIgnoreErrors,
758 57106b74 Michael Hanselmann
    _PVerbose,
759 bf93ae69 Adeodato Simo
    ]
760 f7686867 Michael Hanselmann
  OP_RESULT = ht.TBool
761 bf93ae69 Adeodato Simo
762 bf93ae69 Adeodato Simo
763 bf93ae69 Adeodato Simo
class OpClusterVerifyGroup(OpCode):
764 bf93ae69 Adeodato Simo
  """Run verify on a node group from the cluster.
765 a7399f66 Iustin Pop

766 a7399f66 Iustin Pop
  @type skip_checks: C{list}
767 a7399f66 Iustin Pop
  @ivar skip_checks: steps to be skipped from the verify process; this
768 a7399f66 Iustin Pop
                     needs to be a subset of
769 a7399f66 Iustin Pop
                     L{constants.VERIFY_OPTIONAL_CHECKS}; currently
770 a7399f66 Iustin Pop
                     only L{constants.VERIFY_NPLUSONE_MEM} can be passed
771 a7399f66 Iustin Pop

772 a7399f66 Iustin Pop
  """
773 bf93ae69 Adeodato Simo
  OP_DSC_FIELD = "group_name"
774 65e183af Michael Hanselmann
  OP_PARAMS = [
775 57106b74 Michael Hanselmann
    _PGroupName,
776 57106b74 Michael Hanselmann
    _PDebugSimulateErrors,
777 57106b74 Michael Hanselmann
    _PErrorCodes,
778 57106b74 Michael Hanselmann
    _PSkipChecks,
779 93f2399e Andrea Spadaccini
    _PIgnoreErrors,
780 57106b74 Michael Hanselmann
    _PVerbose,
781 65e183af Michael Hanselmann
    ]
782 f7686867 Michael Hanselmann
  OP_RESULT = ht.TBool
783 a8083063 Iustin Pop
784 a8083063 Iustin Pop
785 bd8210a7 Iustin Pop
class OpClusterVerifyDisks(OpCode):
786 150e978f Iustin Pop
  """Verify the cluster disks.
787 150e978f Iustin Pop

788 ae1a845c Michael Hanselmann
  """
789 f7686867 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
790 ae1a845c Michael Hanselmann
791 ae1a845c Michael Hanselmann
792 ae1a845c Michael Hanselmann
class OpGroupVerifyDisks(OpCode):
793 ae1a845c Michael Hanselmann
  """Verifies the status of all disks in a node group.
794 150e978f Iustin Pop

795 ae1a845c Michael Hanselmann
  Result: a tuple of three elements:
796 ae1a845c Michael Hanselmann
    - dict of node names with issues (values: error msg)
797 150e978f Iustin Pop
    - list of instances with degraded disks (that should be activated)
798 b63ed789 Iustin Pop
    - dict of instances with missing logical volumes (values: (node, vol)
799 b63ed789 Iustin Pop
      pairs with details about the missing volumes)
800 150e978f Iustin Pop

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

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

809 150e978f Iustin Pop
  """
810 ae1a845c Michael Hanselmann
  OP_DSC_FIELD = "group_name"
811 ae1a845c Michael Hanselmann
  OP_PARAMS = [
812 ae1a845c Michael Hanselmann
    _PGroupName,
813 ae1a845c Michael Hanselmann
    ]
814 1ce03fb1 Michael Hanselmann
  OP_RESULT = \
815 1ce03fb1 Michael Hanselmann
    ht.TAnd(ht.TIsLength(3),
816 1ce03fb1 Michael Hanselmann
            ht.TItems([ht.TDictOf(ht.TString, ht.TString),
817 1ce03fb1 Michael Hanselmann
                       ht.TListOf(ht.TString),
818 6973587f Michael Hanselmann
                       ht.TDictOf(ht.TString,
819 6973587f Michael Hanselmann
                                  ht.TListOf(ht.TListOf(ht.TString)))]))
820 150e978f Iustin Pop
821 150e978f Iustin Pop
822 5d01aca3 Iustin Pop
class OpClusterRepairDiskSizes(OpCode):
823 60975797 Iustin Pop
  """Verify the disk sizes of the instances and fixes configuration
824 60975797 Iustin Pop
  mimatches.
825 60975797 Iustin Pop

826 60975797 Iustin Pop
  Parameters: optional instances list, in case we want to restrict the
827 60975797 Iustin Pop
  checks to only a subset of the instances.
828 60975797 Iustin Pop

829 60975797 Iustin Pop
  Result: a list of tuples, (instance, disk, new-size) for changed
830 60975797 Iustin Pop
  configurations.
831 60975797 Iustin Pop

832 60975797 Iustin Pop
  In normal operation, the list should be empty.
833 60975797 Iustin Pop

834 60975797 Iustin Pop
  @type instances: list
835 60975797 Iustin Pop
  @ivar instances: the list of instances to check, or empty for all instances
836 60975797 Iustin Pop

837 60975797 Iustin Pop
  """
838 65e183af Michael Hanselmann
  OP_PARAMS = [
839 197b323b Michael Hanselmann
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
840 65e183af Michael Hanselmann
    ]
841 c363310d René Nussbaumer
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
842 c363310d René Nussbaumer
                                 ht.TItems([ht.TNonEmptyString,
843 2c9fa1ff Iustin Pop
                                            ht.TNonNegativeInt,
844 2c9fa1ff Iustin Pop
                                            ht.TNonNegativeInt])))
845 60975797 Iustin Pop
846 60975797 Iustin Pop
847 2f093ea0 Iustin Pop
class OpClusterConfigQuery(OpCode):
848 ae5849b5 Michael Hanselmann
  """Query cluster configuration values."""
849 65e183af Michael Hanselmann
  OP_PARAMS = [
850 3c286190 Dimitris Aragiorgis
    _POutputFields,
851 65e183af Michael Hanselmann
    ]
852 c363310d René Nussbaumer
  OP_RESULT = ht.TListOf(ht.TAny)
853 a8083063 Iustin Pop
854 a8083063 Iustin Pop
855 e126df25 Iustin Pop
class OpClusterRename(OpCode):
856 a7399f66 Iustin Pop
  """Rename the cluster.
857 a7399f66 Iustin Pop

858 a7399f66 Iustin Pop
  @type name: C{str}
859 a7399f66 Iustin Pop
  @ivar name: The new name of the cluster. The name and/or the master IP
860 a7399f66 Iustin Pop
              address will be changed to match the new name and its IP
861 a7399f66 Iustin Pop
              address.
862 a7399f66 Iustin Pop

863 a7399f66 Iustin Pop
  """
864 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
865 65e183af Michael Hanselmann
  OP_PARAMS = [
866 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
867 65e183af Michael Hanselmann
    ]
868 c363310d René Nussbaumer
  OP_RESULT = ht.TNonEmptyString
869 07bd8a51 Iustin Pop
870 07bd8a51 Iustin Pop
871 a6682fdc Iustin Pop
class OpClusterSetParams(OpCode):
872 a7399f66 Iustin Pop
  """Change the parameters of the cluster.
873 a7399f66 Iustin Pop

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

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

942 afee0879 Iustin Pop
  """
943 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
944 afee0879 Iustin Pop
945 83f72637 Michael Hanselmann
946 fb926117 Andrea Spadaccini
class OpClusterActivateMasterIp(OpCode):
947 fb926117 Andrea Spadaccini
  """Activate the master IP on the master node.
948 fb926117 Andrea Spadaccini

949 fb926117 Andrea Spadaccini
  """
950 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
951 fb926117 Andrea Spadaccini
952 fb926117 Andrea Spadaccini
953 fb926117 Andrea Spadaccini
class OpClusterDeactivateMasterIp(OpCode):
954 fb926117 Andrea Spadaccini
  """Deactivate the master IP on the master node.
955 fb926117 Andrea Spadaccini

956 fb926117 Andrea Spadaccini
  """
957 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
958 fb926117 Andrea Spadaccini
959 fb926117 Andrea Spadaccini
960 83f72637 Michael Hanselmann
class OpQuery(OpCode):
961 83f72637 Michael Hanselmann
  """Query for resources/items.
962 83f72637 Michael Hanselmann

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

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

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

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

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

1048 a7399f66 Iustin Pop
  @type node_name: C{str}
1049 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to remove. If the node still has
1050 a7399f66 Iustin Pop
                   instances on it, the operation will fail.
1051 a7399f66 Iustin Pop

1052 a7399f66 Iustin Pop
  """
1053 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
1054 65e183af Michael Hanselmann
  OP_PARAMS = [
1055 65e183af Michael Hanselmann
    _PNodeName,
1056 65e183af Michael Hanselmann
    ]
1057 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1058 a8083063 Iustin Pop
1059 a8083063 Iustin Pop
1060 d817d49f Iustin Pop
class OpNodeAdd(OpCode):
1061 a7399f66 Iustin Pop
  """Add a node to the cluster.
1062 a7399f66 Iustin Pop

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

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

1236 9bf56d77 Michael Hanselmann
  @ivar instance_name: Instance name
1237 9bf56d77 Michael Hanselmann
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
1238 9bf56d77 Michael Hanselmann
  @ivar source_handshake: Signed handshake from source (remote import only)
1239 9bf56d77 Michael Hanselmann
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
1240 9bf56d77 Michael Hanselmann
  @ivar source_instance_name: Previous name of instance (remote import only)
1241 dae91d02 Michael Hanselmann
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
1242 dae91d02 Michael Hanselmann
    (remote import only)
1243 9bf56d77 Michael Hanselmann

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

1310 12e62af5 René Nussbaumer
  """
1311 12e62af5 René Nussbaumer
  OP_PARAMS = [
1312 1f1188c3 Michael Hanselmann
    _POpportunisticLocking,
1313 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator used to allocate all the instances"),
1314 4c405df7 Iustin Pop
    ("instances", ht.EmptyList, ht.TListOf(ht.TInstanceOf(OpInstanceCreate)),
1315 12e62af5 René Nussbaumer
     "List of instance create opcodes describing the instances to allocate"),
1316 12e62af5 René Nussbaumer
    ]
1317 12e62af5 René Nussbaumer
  _JOB_LIST = ht.Comment("List of submitted jobs")(TJobIdList)
1318 12e62af5 René Nussbaumer
  ALLOCATABLE_KEY = "allocatable"
1319 12e62af5 René Nussbaumer
  FAILED_KEY = "allocatable"
1320 12e62af5 René Nussbaumer
  OP_RESULT = ht.TStrictDict(True, True, {
1321 12e62af5 René Nussbaumer
    constants.JOB_IDS_KEY: _JOB_LIST,
1322 12e62af5 René Nussbaumer
    ALLOCATABLE_KEY: ht.TListOf(ht.TNonEmptyString),
1323 3c286190 Dimitris Aragiorgis
    FAILED_KEY: ht.TListOf(ht.TNonEmptyString),
1324 12e62af5 René Nussbaumer
    })
1325 12e62af5 René Nussbaumer
1326 12e62af5 René Nussbaumer
  def __getstate__(self):
1327 12e62af5 René Nussbaumer
    """Generic serializer.
1328 12e62af5 René Nussbaumer

1329 12e62af5 René Nussbaumer
    """
1330 12e62af5 René Nussbaumer
    state = OpCode.__getstate__(self)
1331 12e62af5 René Nussbaumer
    if hasattr(self, "instances"):
1332 12e62af5 René Nussbaumer
      # pylint: disable=E1101
1333 12e62af5 René Nussbaumer
      state["instances"] = [inst.__getstate__() for inst in self.instances]
1334 12e62af5 René Nussbaumer
    return state
1335 12e62af5 René Nussbaumer
1336 12e62af5 René Nussbaumer
  def __setstate__(self, state):
1337 12e62af5 René Nussbaumer
    """Generic unserializer.
1338 12e62af5 René Nussbaumer

1339 12e62af5 René Nussbaumer
    This method just restores from the serialized state the attributes
1340 12e62af5 René Nussbaumer
    of the current instance.
1341 12e62af5 René Nussbaumer

1342 12e62af5 René Nussbaumer
    @param state: the serialized opcode data
1343 12e62af5 René Nussbaumer
    @type state: C{dict}
1344 12e62af5 René Nussbaumer

1345 12e62af5 René Nussbaumer
    """
1346 12e62af5 René Nussbaumer
    if not isinstance(state, dict):
1347 12e62af5 René Nussbaumer
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
1348 12e62af5 René Nussbaumer
                       type(state))
1349 12e62af5 René Nussbaumer
1350 12e62af5 René Nussbaumer
    if "instances" in state:
1351 5dff65da Michael Hanselmann
      state["instances"] = map(OpCode.LoadOpCode, state["instances"])
1352 5dff65da Michael Hanselmann
1353 12e62af5 René Nussbaumer
    return OpCode.__setstate__(self, state)
1354 12e62af5 René Nussbaumer
1355 9bc5ac44 René Nussbaumer
  def Validate(self, set_defaults):
1356 9bc5ac44 René Nussbaumer
    """Validates this opcode.
1357 9bc5ac44 René Nussbaumer

1358 9bc5ac44 René Nussbaumer
    We do this recursively.
1359 9bc5ac44 René Nussbaumer

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

1480 53c776b5 Iustin Pop
  This migrates (without shutting down an instance) to its secondary
1481 53c776b5 Iustin Pop
  node.
1482 53c776b5 Iustin Pop

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

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

1508 313bcead Iustin Pop
  This move (with shutting down an instance and data copying) to an
1509 313bcead Iustin Pop
  arbitrary node.
1510 313bcead Iustin Pop

1511 313bcead Iustin Pop
  @ivar instance_name: the name of the instance
1512 313bcead Iustin Pop
  @ivar target_node: the destination node
1513 313bcead Iustin Pop

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

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

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

1813 1410fa8d Michael Hanselmann
  @ivar instance_name: Instance name
1814 1410fa8d Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1815 1410fa8d Michael Hanselmann

1816 1410fa8d Michael Hanselmann
  """
1817 1410fa8d Michael Hanselmann
  OP_DSC_FIELD = "instance_name"
1818 65e183af Michael Hanselmann
  OP_PARAMS = [
1819 65e183af Michael Hanselmann
    _PInstanceName,
1820 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1821 45d4c81c Michael Hanselmann
     "Export mode"),
1822 1410fa8d Michael Hanselmann
    ]
1823 fd9f58fd Iustin Pop
  OP_RESULT = ht.TMaybeDict
1824 1410fa8d Michael Hanselmann
1825 1410fa8d Michael Hanselmann
1826 4ff922a2 Iustin Pop
class OpBackupExport(OpCode):
1827 4a96f1d1 Michael Hanselmann
  """Export an instance.
1828 4a96f1d1 Michael Hanselmann

1829 398e9066 Iustin Pop
  For local exports, the export destination is the node name. For
1830 398e9066 Iustin Pop
  remote exports, the export destination is a list of tuples, each
1831 398e9066 Iustin Pop
  consisting of hostname/IP address, port, magic, HMAC and HMAC
1832 398e9066 Iustin Pop
  salt. The HMAC is calculated using the cluster domain secret over
1833 398e9066 Iustin Pop
  the value "${index}:${hostname}:${port}". The destination X509 CA
1834 398e9066 Iustin Pop
  must be a signed certificate.
1835 4a96f1d1 Michael Hanselmann

1836 4a96f1d1 Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1837 4a96f1d1 Michael Hanselmann
  @ivar target_node: Export destination
1838 4a96f1d1 Michael Hanselmann
  @ivar x509_key_name: X509 key to use (remote export only)
1839 4a96f1d1 Michael Hanselmann
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1840 4a96f1d1 Michael Hanselmann
                             only)
1841 4a96f1d1 Michael Hanselmann

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

1935 06009e27 Iustin Pop
  This is used just for debugging and testing.
1936 06009e27 Iustin Pop

1937 06009e27 Iustin Pop
  Parameters:
1938 06009e27 Iustin Pop
    - duration: the time to sleep
1939 06009e27 Iustin Pop
    - on_master: if true, sleep on the master
1940 06009e27 Iustin Pop
    - on_nodes: list of nodes in which to sleep
1941 06009e27 Iustin Pop

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

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

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

1952 06009e27 Iustin Pop
  """
1953 60dd1473 Iustin Pop
  OP_DSC_FIELD = "duration"
1954 65e183af Michael Hanselmann
  OP_PARAMS = [
1955 b795a775 Michael Hanselmann
    ("duration", ht.NoDefault, ht.TNumber, None),
1956 197b323b Michael Hanselmann
    ("on_master", True, ht.TBool, None),
1957 197b323b Michael Hanselmann
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
1958 2c9fa1ff Iustin Pop
    ("repeat", 0, ht.TNonNegativeInt, None),
1959 65e183af Michael Hanselmann
    ]
1960 d61df03e Iustin Pop
1961 d61df03e Iustin Pop
1962 d61df03e Iustin Pop
class OpTestAllocator(OpCode):
1963 d61df03e Iustin Pop
  """Allocator framework testing.
1964 d61df03e Iustin Pop

1965 d61df03e Iustin Pop
  This opcode has two modes:
1966 d61df03e Iustin Pop
    - gather and return allocator input for a given mode (allocate new
1967 d61df03e Iustin Pop
      or replace secondary) and a given instance definition (direction
1968 d61df03e Iustin Pop
      'in')
1969 d61df03e Iustin Pop
    - run a selected allocator for a given operation (as above) and
1970 d61df03e Iustin Pop
      return the allocator output (direction 'out')
1971 d61df03e Iustin Pop

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

2005 e58f87a9 Michael Hanselmann
  """
2006 65e183af Michael Hanselmann
  OP_PARAMS = [
2007 197b323b Michael Hanselmann
    ("notify_waitlock", False, ht.TBool, None),
2008 197b323b Michael Hanselmann
    ("notify_exec", False, ht.TBool, None),
2009 197b323b Michael Hanselmann
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
2010 197b323b Michael Hanselmann
    ("fail", False, ht.TBool, None),
2011 e58f87a9 Michael Hanselmann
    ]
2012 e58f87a9 Michael Hanselmann
2013 e58f87a9 Michael Hanselmann
2014 be760ba8 Michael Hanselmann
class OpTestDummy(OpCode):
2015 be760ba8 Michael Hanselmann
  """Utility opcode used by unittests.
2016 be760ba8 Michael Hanselmann

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

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

2090 eaa4c57c Dimitris Aragiorgis
  """
2091 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2092 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2093 eaa4c57c Dimitris Aragiorgis
    _PGroupName,
2094 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2095 e1494c96 Iustin Pop
    ("network_mode", ht.NoDefault, ht.TString, "Connectivity mode"),
2096 e1494c96 Iustin Pop
    ("network_link", ht.NoDefault, ht.TString, "Connectivity link"),
2097 6e8091f9 Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool, "Whether to check for conflicting IPs"),
2098 eaa4c57c Dimitris Aragiorgis
    ]
2099 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2100 eaa4c57c Dimitris Aragiorgis
2101 3c286190 Dimitris Aragiorgis
2102 eaa4c57c Dimitris Aragiorgis
class OpNetworkDisconnect(OpCode):
2103 eaa4c57c Dimitris Aragiorgis
  """Disconnect a Network from a Nodegroup. Produce errors if NICs are
2104 eaa4c57c Dimitris Aragiorgis
     present in the Network unless --no-conficts-check option is passed.
2105 eaa4c57c Dimitris Aragiorgis

2106 eaa4c57c Dimitris Aragiorgis
  """
2107 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2108 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2109 eaa4c57c Dimitris Aragiorgis
    _PGroupName,
2110 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2111 6e8091f9 Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool, "Whether to check for conflicting IPs"),
2112 eaa4c57c Dimitris Aragiorgis
    ]
2113 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2114 eaa4c57c Dimitris Aragiorgis
2115 3c286190 Dimitris Aragiorgis
2116 eaa4c57c Dimitris Aragiorgis
class OpNetworkQuery(OpCode):
2117 eaa4c57c Dimitris Aragiorgis
  """Compute the list of networks."""
2118 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2119 eaa4c57c Dimitris Aragiorgis
    _POutputFields,
2120 eaa4c57c Dimitris Aragiorgis
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
2121 eaa4c57c Dimitris Aragiorgis
     "Empty list to query all groups, group names otherwise"),
2122 eaa4c57c Dimitris Aragiorgis
    ]
2123 829cfbc5 Dimitris Aragiorgis
  OP_RESULT = _TOldQueryResult
2124 eaa4c57c Dimitris Aragiorgis
2125 eaa4c57c Dimitris Aragiorgis
2126 dbc96028 Michael Hanselmann
def _GetOpList():
2127 dbc96028 Michael Hanselmann
  """Returns list of all defined opcodes.
2128 dbc96028 Michael Hanselmann

2129 dbc96028 Michael Hanselmann
  Does not eliminate duplicates by C{OP_ID}.
2130 dbc96028 Michael Hanselmann

2131 dbc96028 Michael Hanselmann
  """
2132 dbc96028 Michael Hanselmann
  return [v for v in globals().values()
2133 dbc96028 Michael Hanselmann
          if (isinstance(v, type) and issubclass(v, OpCode) and
2134 687c10d9 Iustin Pop
              hasattr(v, "OP_ID") and v is not OpCode)]
2135 dbc96028 Michael Hanselmann
2136 dbc96028 Michael Hanselmann
2137 dbc96028 Michael Hanselmann
OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())