Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 274c7cab

History | View | Annotate | Download (68.9 kB)

1 2f31098c Iustin Pop
#
2 a8083063 Iustin Pop
#
3 a8083063 Iustin Pop
4 473d87a3 Iustin Pop
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 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 473d87a3 Iustin Pop
from ganeti import outils
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 da4a52a3 Thomas Thrainer
#: a instance UUID (for single-instance LUs)
66 da4a52a3 Thomas Thrainer
_PInstanceUuid = ("instance_uuid", None, ht.TMaybeString,
67 da4a52a3 Thomas Thrainer
                  "Instance UUID")
68 da4a52a3 Thomas Thrainer
69 65e183af Michael Hanselmann
#: Whether to ignore offline nodes
70 45d4c81c Michael Hanselmann
_PIgnoreOfflineNodes = ("ignore_offline_nodes", False, ht.TBool,
71 45d4c81c Michael Hanselmann
                        "Whether to ignore offline nodes")
72 65e183af Michael Hanselmann
73 65e183af Michael Hanselmann
#: a required node name (for single-node LUs)
74 45d4c81c Michael Hanselmann
_PNodeName = ("node_name", ht.NoDefault, ht.TNonEmptyString, "Node name")
75 65e183af Michael Hanselmann
76 1c3231aa Thomas Thrainer
#: a node UUID (for use with _PNodeName)
77 1c3231aa Thomas Thrainer
_PNodeUuid = ("node_uuid", None, ht.TMaybeString, "Node UUID")
78 1c3231aa Thomas Thrainer
79 65e183af Michael Hanselmann
#: a required node group name (for single-group LUs)
80 45d4c81c Michael Hanselmann
_PGroupName = ("group_name", ht.NoDefault, ht.TNonEmptyString, "Group name")
81 65e183af Michael Hanselmann
82 65e183af Michael Hanselmann
#: Migration type (live/non-live)
83 65e183af Michael Hanselmann
_PMigrationMode = ("mode", None,
84 fd9f58fd Iustin Pop
                   ht.TMaybe(ht.TElemOf(constants.HT_MIGRATION_MODES)),
85 45d4c81c Michael Hanselmann
                   "Migration mode")
86 65e183af Michael Hanselmann
87 65e183af Michael Hanselmann
#: Obsolete 'live' migration mode (boolean)
88 45d4c81c Michael Hanselmann
_PMigrationLive = ("live", None, ht.TMaybeBool,
89 45d4c81c Michael Hanselmann
                   "Legacy setting for live migration, do not use")
90 65e183af Michael Hanselmann
91 65e183af Michael Hanselmann
#: Tag type
92 971b3a98 Michael Hanselmann
_PTagKind = ("kind", ht.NoDefault, ht.TElemOf(constants.VALID_TAG_TYPES),
93 971b3a98 Michael Hanselmann
             "Tag kind")
94 65e183af Michael Hanselmann
95 65e183af Michael Hanselmann
#: List of tag strings
96 971b3a98 Michael Hanselmann
_PTags = ("tags", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
97 971b3a98 Michael Hanselmann
          "List of tag names")
98 65e183af Michael Hanselmann
99 45d4c81c Michael Hanselmann
_PForceVariant = ("force_variant", False, ht.TBool,
100 45d4c81c Michael Hanselmann
                  "Whether to force an unknown OS variant")
101 45d4c81c Michael Hanselmann
102 45d4c81c Michael Hanselmann
_PWaitForSync = ("wait_for_sync", True, ht.TBool,
103 45d4c81c Michael Hanselmann
                 "Whether to wait for the disk to synchronize")
104 45d4c81c Michael Hanselmann
105 b69437c5 Iustin Pop
_PWaitForSyncFalse = ("wait_for_sync", False, ht.TBool,
106 b69437c5 Iustin Pop
                      "Whether to wait for the disk to synchronize"
107 b69437c5 Iustin Pop
                      " (defaults to false)")
108 b69437c5 Iustin Pop
109 45d4c81c Michael Hanselmann
_PIgnoreConsistency = ("ignore_consistency", False, ht.TBool,
110 45d4c81c Michael Hanselmann
                       "Whether to ignore disk consistency")
111 45d4c81c Michael Hanselmann
112 45d4c81c Michael Hanselmann
_PStorageName = ("name", ht.NoDefault, ht.TMaybeString, "Storage name")
113 45d4c81c Michael Hanselmann
114 45d4c81c Michael Hanselmann
_PUseLocking = ("use_locking", False, ht.TBool,
115 45d4c81c Michael Hanselmann
                "Whether to use synchronization")
116 45d4c81c Michael Hanselmann
117 45d4c81c Michael Hanselmann
_PNameCheck = ("name_check", True, ht.TBool, "Whether to check name")
118 45d4c81c Michael Hanselmann
119 45d4c81c Michael Hanselmann
_PNodeGroupAllocPolicy = \
120 45d4c81c Michael Hanselmann
  ("alloc_policy", None,
121 fd9f58fd Iustin Pop
   ht.TMaybe(ht.TElemOf(constants.VALID_ALLOC_POLICIES)),
122 45d4c81c Michael Hanselmann
   "Instance allocation policy")
123 45d4c81c Michael Hanselmann
124 45d4c81c Michael Hanselmann
_PGroupNodeParams = ("ndparams", None, ht.TMaybeDict,
125 45d4c81c Michael Hanselmann
                     "Default node parameters for group")
126 45d4c81c Michael Hanselmann
127 abd66bf8 Michael Hanselmann
_PQueryWhat = ("what", ht.NoDefault, ht.TElemOf(constants.QR_VIA_OP),
128 8e7078e0 Michael Hanselmann
               "Resource(s) to query for")
129 8e7078e0 Michael Hanselmann
130 e1f23243 Michael Hanselmann
_PEarlyRelease = ("early_release", False, ht.TBool,
131 e1f23243 Michael Hanselmann
                  "Whether to release locks as soon as possible")
132 e1f23243 Michael Hanselmann
133 45d4c81c Michael Hanselmann
_PIpCheckDoc = "Whether to ensure instance's IP address is inactive"
134 45d4c81c Michael Hanselmann
135 9b64e486 Iustin Pop
#: Do not remember instance state changes
136 6aac5aef Iustin Pop
_PNoRemember = ("no_remember", False, ht.TBool,
137 6aac5aef Iustin Pop
                "Do not remember the state change")
138 9b64e486 Iustin Pop
139 f8fa4175 Michael Hanselmann
#: Target node for instance migration/failover
140 f8fa4175 Michael Hanselmann
_PMigrationTargetNode = ("target_node", None, ht.TMaybeString,
141 f8fa4175 Michael Hanselmann
                         "Target node for shared-storage instances")
142 f8fa4175 Michael Hanselmann
143 1c3231aa Thomas Thrainer
_PMigrationTargetNodeUuid = ("target_node_uuid", None, ht.TMaybeString,
144 1c3231aa Thomas Thrainer
                             "Target node UUID for shared-storage instances")
145 1c3231aa Thomas Thrainer
146 323f9095 Stephen Shirley
_PStartupPaused = ("startup_paused", False, ht.TBool,
147 323f9095 Stephen Shirley
                   "Pause instance at startup")
148 323f9095 Stephen Shirley
149 57106b74 Michael Hanselmann
_PVerbose = ("verbose", False, ht.TBool, "Verbose mode")
150 57106b74 Michael Hanselmann
151 57106b74 Michael Hanselmann
# Parameters for cluster verification
152 57106b74 Michael Hanselmann
_PDebugSimulateErrors = ("debug_simulate_errors", False, ht.TBool,
153 57106b74 Michael Hanselmann
                         "Whether to simulate errors (useful for debugging)")
154 57106b74 Michael Hanselmann
_PErrorCodes = ("error_codes", False, ht.TBool, "Error codes")
155 57106b74 Michael Hanselmann
_PSkipChecks = ("skip_checks", ht.EmptyList,
156 57106b74 Michael Hanselmann
                ht.TListOf(ht.TElemOf(constants.VERIFY_OPTIONAL_CHECKS)),
157 57106b74 Michael Hanselmann
                "Which checks to skip")
158 93f2399e Andrea Spadaccini
_PIgnoreErrors = ("ignore_errors", ht.EmptyList,
159 93f2399e Andrea Spadaccini
                  ht.TListOf(ht.TElemOf(constants.CV_ALL_ECODES_STRINGS)),
160 93f2399e Andrea Spadaccini
                  "List of error codes that should be treated as warnings")
161 323f9095 Stephen Shirley
162 bc5d0215 Andrea Spadaccini
# Disk parameters
163 a138ead7 Michael Hanselmann
_PDiskParams = \
164 a138ead7 Michael Hanselmann
  ("diskparams", None,
165 fd9f58fd Iustin Pop
   ht.TMaybe(ht.TDictOf(ht.TElemOf(constants.DISK_TEMPLATES), ht.TDict)),
166 a138ead7 Michael Hanselmann
   "Disk templates' parameter defaults")
167 bc5d0215 Andrea Spadaccini
168 0ec2ce46 René Nussbaumer
# Parameters for node resource model
169 0ec2ce46 René Nussbaumer
_PHvState = ("hv_state", None, ht.TMaybeDict, "Set hypervisor states")
170 0ec2ce46 René Nussbaumer
_PDiskState = ("disk_state", None, ht.TMaybeDict, "Set disk states")
171 0ec2ce46 René Nussbaumer
172 1f1188c3 Michael Hanselmann
#: Opportunistic locking
173 1f1188c3 Michael Hanselmann
_POpportunisticLocking = \
174 1f1188c3 Michael Hanselmann
  ("opportunistic_locking", False, ht.TBool,
175 1f1188c3 Michael Hanselmann
   ("Whether to employ opportunistic locking for nodes, meaning nodes"
176 1f1188c3 Michael Hanselmann
    " already locked by another opcode won't be considered for instance"
177 1f1188c3 Michael Hanselmann
    " allocation (only when an iallocator is used)"))
178 b6aaf437 René Nussbaumer
179 b6aaf437 René Nussbaumer
_PIgnoreIpolicy = ("ignore_ipolicy", False, ht.TBool,
180 b6aaf437 René Nussbaumer
                   "Whether to ignore ipolicy violations")
181 b6aaf437 René Nussbaumer
182 8c0b16f6 Guido Trotter
# Allow runtime changes while migrating
183 8c0b16f6 Guido Trotter
_PAllowRuntimeChgs = ("allow_runtime_changes", True, ht.TBool,
184 8c0b16f6 Guido Trotter
                      "Allow runtime changes (eg. memory ballooning)")
185 8c0b16f6 Guido Trotter
186 89514061 Iustin Pop
#: IAllocator field builder
187 89514061 Iustin Pop
_PIAllocFromDesc = lambda desc: ("iallocator", None, ht.TMaybeString, desc)
188 89514061 Iustin Pop
189 eaa4c57c Dimitris Aragiorgis
#: a required network name
190 eaa4c57c Dimitris Aragiorgis
_PNetworkName = ("network_name", ht.NoDefault, ht.TNonEmptyString,
191 eaa4c57c Dimitris Aragiorgis
                 "Set network name")
192 8c0b16f6 Guido Trotter
193 911ee606 Michael Hanselmann
_PTargetGroups = \
194 911ee606 Michael Hanselmann
  ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString),
195 911ee606 Michael Hanselmann
   "Destination group names or UUIDs (defaults to \"all but current group\")")
196 911ee606 Michael Hanselmann
197 ff0d18e6 Iustin Pop
#: OP_ID conversion regular expression
198 ff0d18e6 Iustin Pop
_OPID_RE = re.compile("([a-z])([A-Z])")
199 ff0d18e6 Iustin Pop
200 8c9ee749 Michael Hanselmann
#: Utility function for L{OpClusterSetParams}
201 8f227489 Michael Hanselmann
_TestClusterOsListItem = \
202 8f227489 Michael Hanselmann
  ht.TAnd(ht.TIsLength(2), ht.TItems([
203 8f227489 Michael Hanselmann
    ht.TElemOf(constants.DDMS_VALUES),
204 8f227489 Michael Hanselmann
    ht.TNonEmptyString,
205 8f227489 Michael Hanselmann
    ]))
206 8c9ee749 Michael Hanselmann
207 ff8067cf Michael Hanselmann
_TestClusterOsList = ht.TMaybeListOf(_TestClusterOsListItem)
208 ff0d18e6 Iustin Pop
209 526a662a Michael Hanselmann
# TODO: Generate check from constants.INIC_PARAMS_TYPES
210 526a662a Michael Hanselmann
#: Utility function for testing NIC definitions
211 20a32e74 Michael Hanselmann
_TestNicDef = \
212 20a32e74 Michael Hanselmann
  ht.Comment("NIC parameters")(ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
213 fd9f58fd Iustin Pop
                                          ht.TMaybeString))
214 526a662a Michael Hanselmann
215 1456df62 Michael Hanselmann
_TSetParamsResultItemItems = [
216 1456df62 Michael Hanselmann
  ht.Comment("name of changed parameter")(ht.TNonEmptyString),
217 b3d2ee31 Michael Hanselmann
  ht.Comment("new value")(ht.TAny),
218 1456df62 Michael Hanselmann
  ]
219 1456df62 Michael Hanselmann
220 1456df62 Michael Hanselmann
_TSetParamsResult = \
221 1456df62 Michael Hanselmann
  ht.TListOf(ht.TAnd(ht.TIsLength(len(_TSetParamsResultItemItems)),
222 1456df62 Michael Hanselmann
                     ht.TItems(_TSetParamsResultItemItems)))
223 1456df62 Michael Hanselmann
224 3e3ddbf0 Constantinos Venetsanopoulos
# In the disks option we can provide arbitrary parameters too, which
225 3e3ddbf0 Constantinos Venetsanopoulos
# we may not be able to validate at this level, so we just check the
226 3e3ddbf0 Constantinos Venetsanopoulos
# format of the dict here and the checks concerning IDISK_PARAMS will
227 3e3ddbf0 Constantinos Venetsanopoulos
# happen at the LU level
228 20a32e74 Michael Hanselmann
_TDiskParams = \
229 3e3ddbf0 Constantinos Venetsanopoulos
  ht.Comment("Disk parameters")(ht.TDictOf(ht.TNonEmptyString,
230 20a32e74 Michael Hanselmann
                                           ht.TOr(ht.TNonEmptyString, ht.TInt)))
231 735e1318 Michael Hanselmann
232 aa12a891 René Nussbaumer
_TQueryRow = \
233 aa12a891 René Nussbaumer
  ht.TListOf(ht.TAnd(ht.TIsLength(2),
234 aa12a891 René Nussbaumer
                     ht.TItems([ht.TElemOf(constants.RS_ALL),
235 aa12a891 René Nussbaumer
                                ht.TAny])))
236 aa12a891 René Nussbaumer
237 415feb2e René Nussbaumer
_TQueryResult = ht.TListOf(_TQueryRow)
238 415feb2e René Nussbaumer
239 415feb2e René Nussbaumer
_TOldQueryRow = ht.TListOf(ht.TAny)
240 415feb2e René Nussbaumer
241 415feb2e René Nussbaumer
_TOldQueryResult = ht.TListOf(_TOldQueryRow)
242 415feb2e René Nussbaumer
243 415feb2e René Nussbaumer
244 3ce9a5e7 Michael Hanselmann
_SUMMARY_PREFIX = {
245 3ce9a5e7 Michael Hanselmann
  "CLUSTER_": "C_",
246 3ce9a5e7 Michael Hanselmann
  "GROUP_": "G_",
247 3ce9a5e7 Michael Hanselmann
  "NODE_": "N_",
248 3ce9a5e7 Michael Hanselmann
  "INSTANCE_": "I_",
249 3ce9a5e7 Michael Hanselmann
  }
250 3ce9a5e7 Michael Hanselmann
251 b95479a5 Michael Hanselmann
#: Attribute name for dependencies
252 b95479a5 Michael Hanselmann
DEPEND_ATTR = "depends"
253 b95479a5 Michael Hanselmann
254 018ae30b Michael Hanselmann
#: Attribute name for comment
255 018ae30b Michael Hanselmann
COMMENT_ATTR = "comment"
256 018ae30b Michael Hanselmann
257 526a662a Michael Hanselmann
258 d634b4e0 Michele Tartara
def _NameComponents(name):
259 d634b4e0 Michele Tartara
  """Split an opcode class name into its components
260 ff0d18e6 Iustin Pop

261 ff0d18e6 Iustin Pop
  @type name: string
262 ff0d18e6 Iustin Pop
  @param name: the class name, as OpXxxYyy
263 d634b4e0 Michele Tartara
  @rtype: array of strings
264 d634b4e0 Michele Tartara
  @return: the components of the name
265 ff0d18e6 Iustin Pop

266 ff0d18e6 Iustin Pop
  """
267 d634b4e0 Michele Tartara
  assert name.startswith("Op")
268 ff0d18e6 Iustin Pop
  # Note: (?<=[a-z])(?=[A-Z]) would be ideal, since it wouldn't
269 ff0d18e6 Iustin Pop
  # consume any input, and hence we would just have all the elements
270 ff0d18e6 Iustin Pop
  # in the list, one by one; but it seems that split doesn't work on
271 ff0d18e6 Iustin Pop
  # non-consuming input, hence we have to process the input string a
272 ff0d18e6 Iustin Pop
  # bit
273 ff0d18e6 Iustin Pop
  name = _OPID_RE.sub(r"\1,\2", name)
274 ff0d18e6 Iustin Pop
  elems = name.split(",")
275 d634b4e0 Michele Tartara
  return elems
276 d634b4e0 Michele Tartara
277 d634b4e0 Michele Tartara
278 d634b4e0 Michele Tartara
def _NameToId(name):
279 d634b4e0 Michele Tartara
  """Convert an opcode class name to an OP_ID.
280 d634b4e0 Michele Tartara

281 d634b4e0 Michele Tartara
  @type name: string
282 d634b4e0 Michele Tartara
  @param name: the class name, as OpXxxYyy
283 d634b4e0 Michele Tartara
  @rtype: string
284 d634b4e0 Michele Tartara
  @return: the name in the OP_XXXX_YYYY format
285 d634b4e0 Michele Tartara

286 d634b4e0 Michele Tartara
  """
287 d634b4e0 Michele Tartara
  if not name.startswith("Op"):
288 d634b4e0 Michele Tartara
    return None
289 d634b4e0 Michele Tartara
  return "_".join(n.upper() for n in _NameComponents(name))
290 d634b4e0 Michele Tartara
291 d634b4e0 Michele Tartara
292 d634b4e0 Michele Tartara
def NameToReasonSrc(name):
293 d634b4e0 Michele Tartara
  """Convert an opcode class name to a source string for the reason trail
294 d634b4e0 Michele Tartara

295 d634b4e0 Michele Tartara
  @type name: string
296 d634b4e0 Michele Tartara
  @param name: the class name, as OpXxxYyy
297 d634b4e0 Michele Tartara
  @rtype: string
298 d634b4e0 Michele Tartara
  @return: the name in the OP_XXXX_YYYY format
299 d634b4e0 Michele Tartara

300 d634b4e0 Michele Tartara
  """
301 d634b4e0 Michele Tartara
  if not name.startswith("Op"):
302 d634b4e0 Michele Tartara
    return None
303 d634b4e0 Michele Tartara
  return "%s:%s" % (constants.OPCODE_REASON_SRC_OPCODE,
304 d634b4e0 Michele Tartara
                    "_".join(n.lower() for n in _NameComponents(name)))
305 ff0d18e6 Iustin Pop
306 65e183af Michael Hanselmann
307 415feb2e René Nussbaumer
def _GenerateObjectTypeCheck(obj, fields_types):
308 415feb2e René Nussbaumer
  """Helper to generate type checks for objects.
309 415feb2e René Nussbaumer

310 415feb2e René Nussbaumer
  @param obj: The object to generate type checks
311 415feb2e René Nussbaumer
  @param fields_types: The fields and their types as a dict
312 415feb2e René Nussbaumer
  @return: A ht type check function
313 415feb2e René Nussbaumer

314 415feb2e René Nussbaumer
  """
315 415feb2e René Nussbaumer
  assert set(obj.GetAllSlots()) == set(fields_types.keys()), \
316 415feb2e René Nussbaumer
    "%s != %s" % (set(obj.GetAllSlots()), set(fields_types.keys()))
317 415feb2e René Nussbaumer
  return ht.TStrictDict(True, True, fields_types)
318 415feb2e René Nussbaumer
319 415feb2e René Nussbaumer
320 b02c6bdf Michael Hanselmann
_TQueryFieldDef = \
321 b02c6bdf Michael Hanselmann
  _GenerateObjectTypeCheck(objects.QueryFieldDefinition, {
322 b02c6bdf Michael Hanselmann
    "name": ht.TNonEmptyString,
323 b02c6bdf Michael Hanselmann
    "title": ht.TNonEmptyString,
324 b02c6bdf Michael Hanselmann
    "kind": ht.TElemOf(constants.QFT_ALL),
325 b02c6bdf Michael Hanselmann
    "doc": ht.TNonEmptyString,
326 b02c6bdf Michael Hanselmann
    })
327 415feb2e René Nussbaumer
328 415feb2e René Nussbaumer
329 4b97f902 Apollon Oikonomopoulos
def RequireSharedFileStorage():
330 4b97f902 Apollon Oikonomopoulos
  """Checks that shared file storage is enabled.
331 4b97f902 Apollon Oikonomopoulos

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

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

337 4b97f902 Apollon Oikonomopoulos
  """
338 4b97f902 Apollon Oikonomopoulos
  if not constants.ENABLE_SHARED_FILE_STORAGE:
339 4b97f902 Apollon Oikonomopoulos
    raise errors.OpPrereqError("Shared file storage disabled at"
340 4b97f902 Apollon Oikonomopoulos
                               " configure time", errors.ECODE_INVAL)
341 4b97f902 Apollon Oikonomopoulos
342 4b97f902 Apollon Oikonomopoulos
343 45fe090b Michael Hanselmann
def _BuildDiskTemplateCheck(accept_none):
344 45fe090b Michael Hanselmann
  """Builds check for disk template.
345 45fe090b Michael Hanselmann

346 45fe090b Michael Hanselmann
  @type accept_none: bool
347 45fe090b Michael Hanselmann
  @param accept_none: whether to accept None as a correct value
348 45fe090b Michael Hanselmann
  @rtype: callable
349 45fe090b Michael Hanselmann

350 45fe090b Michael Hanselmann
  """
351 45fe090b Michael Hanselmann
  template_check = ht.TElemOf(constants.DISK_TEMPLATES)
352 45fe090b Michael Hanselmann
353 45fe090b Michael Hanselmann
  if accept_none:
354 fd9f58fd Iustin Pop
    template_check = ht.TMaybe(template_check)
355 45fe090b Michael Hanselmann
356 9d276e93 Helga Velroyen
  return template_check
357 8c9ee749 Michael Hanselmann
358 8c9ee749 Michael Hanselmann
359 65e183af Michael Hanselmann
def _CheckStorageType(storage_type):
360 65e183af Michael Hanselmann
  """Ensure a given storage type is valid.
361 65e183af Michael Hanselmann

362 65e183af Michael Hanselmann
  """
363 65e183af Michael Hanselmann
  if storage_type not in constants.VALID_STORAGE_TYPES:
364 65e183af Michael Hanselmann
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
365 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
366 65e183af Michael Hanselmann
  return True
367 65e183af Michael Hanselmann
368 65e183af Michael Hanselmann
369 65e183af Michael Hanselmann
#: Storage type parameter
370 45d4c81c Michael Hanselmann
_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
371 45d4c81c Michael Hanselmann
                 "Storage type")
372 65e183af Michael Hanselmann
373 3c286190 Dimitris Aragiorgis
374 16091a6e Michael Hanselmann
@ht.WithDesc("IPv4 network")
375 eaa4c57c Dimitris Aragiorgis
def _CheckCIDRNetNotation(value):
376 16091a6e Michael Hanselmann
  """Ensure a given CIDR notation type is valid.
377 eaa4c57c Dimitris Aragiorgis

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

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

402 eaa4c57c Dimitris Aragiorgis
  """
403 eaa4c57c Dimitris Aragiorgis
  try:
404 eaa4c57c Dimitris Aragiorgis
    ipaddr.IPv6Address(value)
405 eaa4c57c Dimitris Aragiorgis
  except ipaddr.AddressValueError:
406 eaa4c57c Dimitris Aragiorgis
    return False
407 eaa4c57c Dimitris Aragiorgis
  return True
408 eaa4c57c Dimitris Aragiorgis
409 3c286190 Dimitris Aragiorgis
410 16091a6e Michael Hanselmann
@ht.WithDesc("IPv6 network")
411 eaa4c57c Dimitris Aragiorgis
def _CheckCIDR6NetNotation(value):
412 16091a6e Michael Hanselmann
  """Ensure a given CIDR notation type is valid.
413 eaa4c57c Dimitris Aragiorgis

414 eaa4c57c Dimitris Aragiorgis
  """
415 eaa4c57c Dimitris Aragiorgis
  try:
416 eaa4c57c Dimitris Aragiorgis
    ipaddr.IPv6Network(value)
417 eaa4c57c Dimitris Aragiorgis
  except ipaddr.AddressValueError:
418 eaa4c57c Dimitris Aragiorgis
    return False
419 eaa4c57c Dimitris Aragiorgis
  return True
420 65e183af Michael Hanselmann
421 3c286190 Dimitris Aragiorgis
422 e1494c96 Iustin Pop
_TIpAddress4 = ht.TAnd(ht.TString, _CheckCIDRAddrNotation)
423 e1494c96 Iustin Pop
_TIpAddress6 = ht.TAnd(ht.TString, _CheckCIDR6AddrNotation)
424 e1494c96 Iustin Pop
_TIpNetwork4 = ht.TAnd(ht.TString, _CheckCIDRNetNotation)
425 e1494c96 Iustin Pop
_TIpNetwork6 = ht.TAnd(ht.TString, _CheckCIDR6NetNotation)
426 e1494c96 Iustin Pop
_TMaybeAddr4List = ht.TMaybe(ht.TListOf(_TIpAddress4))
427 32e3d8b1 Michael Hanselmann
428 32e3d8b1 Michael Hanselmann
429 473d87a3 Iustin Pop
class _AutoOpParamSlots(outils.AutoSlots):
430 65e183af Michael Hanselmann
  """Meta class for opcode definitions.
431 65e183af Michael Hanselmann

432 65e183af Michael Hanselmann
  """
433 65e183af Michael Hanselmann
  def __new__(mcs, name, bases, attrs):
434 65e183af Michael Hanselmann
    """Called when a class should be created.
435 65e183af Michael Hanselmann

436 65e183af Michael Hanselmann
    @param mcs: The meta class
437 65e183af Michael Hanselmann
    @param name: Name of created class
438 65e183af Michael Hanselmann
    @param bases: Base classes
439 65e183af Michael Hanselmann
    @type attrs: dict
440 65e183af Michael Hanselmann
    @param attrs: Class attributes
441 65e183af Michael Hanselmann

442 65e183af Michael Hanselmann
    """
443 e89a9021 Iustin Pop
    assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
444 ff0d18e6 Iustin Pop
445 32683096 René Nussbaumer
    slots = mcs._GetSlots(attrs)
446 32683096 René Nussbaumer
    assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
447 32683096 René Nussbaumer
      "Class '%s' uses unknown field in OP_DSC_FIELD" % name
448 8bc17ebb Iustin Pop
    assert ("OP_DSC_FORMATTER" not in attrs or
449 8bc17ebb Iustin Pop
            callable(attrs["OP_DSC_FORMATTER"])), \
450 8bc17ebb Iustin Pop
      ("Class '%s' uses non-callable in OP_DSC_FORMATTER (%s)" %
451 8bc17ebb Iustin Pop
       (name, type(attrs["OP_DSC_FORMATTER"])))
452 32683096 René Nussbaumer
453 e89a9021 Iustin Pop
    attrs["OP_ID"] = _NameToId(name)
454 65e183af Michael Hanselmann
455 473d87a3 Iustin Pop
    return outils.AutoSlots.__new__(mcs, name, bases, attrs)
456 32683096 René Nussbaumer
457 32683096 René Nussbaumer
  @classmethod
458 32683096 René Nussbaumer
  def _GetSlots(mcs, attrs):
459 32683096 René Nussbaumer
    """Build the slots out of OP_PARAMS.
460 32683096 René Nussbaumer

461 32683096 René Nussbaumer
    """
462 65e183af Michael Hanselmann
    # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
463 65e183af Michael Hanselmann
    params = attrs.setdefault("OP_PARAMS", [])
464 65e183af Michael Hanselmann
465 65e183af Michael Hanselmann
    # Use parameter names as slots
466 32683096 René Nussbaumer
    return [pname for (pname, _, _, _) in params]
467 65e183af Michael Hanselmann
468 df458e0b Iustin Pop
469 473d87a3 Iustin Pop
class BaseOpCode(outils.ValidatedSlots):
470 df458e0b Iustin Pop
  """A simple serializable object.
471 df458e0b Iustin Pop

472 0e46916d Iustin Pop
  This object serves as a parent class for OpCode without any custom
473 0e46916d Iustin Pop
  field handling.
474 0e46916d Iustin Pop

475 df458e0b Iustin Pop
  """
476 b459a848 Andrea Spadaccini
  # pylint: disable=E1101
477 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
478 65e183af Michael Hanselmann
  __metaclass__ = _AutoOpParamSlots
479 65e183af Michael Hanselmann
480 df458e0b Iustin Pop
  def __getstate__(self):
481 a7399f66 Iustin Pop
    """Generic serializer.
482 a7399f66 Iustin Pop

483 a7399f66 Iustin Pop
    This method just returns the contents of the instance as a
484 a7399f66 Iustin Pop
    dictionary.
485 a7399f66 Iustin Pop

486 a7399f66 Iustin Pop
    @rtype:  C{dict}
487 a7399f66 Iustin Pop
    @return: the instance attributes and their values
488 a7399f66 Iustin Pop

489 a7399f66 Iustin Pop
    """
490 df458e0b Iustin Pop
    state = {}
491 32683096 René Nussbaumer
    for name in self.GetAllSlots():
492 df458e0b Iustin Pop
      if hasattr(self, name):
493 df458e0b Iustin Pop
        state[name] = getattr(self, name)
494 df458e0b Iustin Pop
    return state
495 df458e0b Iustin Pop
496 df458e0b Iustin Pop
  def __setstate__(self, state):
497 a7399f66 Iustin Pop
    """Generic unserializer.
498 a7399f66 Iustin Pop

499 a7399f66 Iustin Pop
    This method just restores from the serialized state the attributes
500 a7399f66 Iustin Pop
    of the current instance.
501 a7399f66 Iustin Pop

502 a7399f66 Iustin Pop
    @param state: the serialized opcode data
503 a7399f66 Iustin Pop
    @type state:  C{dict}
504 a7399f66 Iustin Pop

505 a7399f66 Iustin Pop
    """
506 df458e0b Iustin Pop
    if not isinstance(state, dict):
507 df458e0b Iustin Pop
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
508 df458e0b Iustin Pop
                       type(state))
509 df458e0b Iustin Pop
510 32683096 René Nussbaumer
    for name in self.GetAllSlots():
511 44db3a6f Iustin Pop
      if name not in state and hasattr(self, name):
512 df458e0b Iustin Pop
        delattr(self, name)
513 df458e0b Iustin Pop
514 df458e0b Iustin Pop
    for name in state:
515 df458e0b Iustin Pop
      setattr(self, name, state[name])
516 df458e0b Iustin Pop
517 adf385c7 Iustin Pop
  @classmethod
518 65e183af Michael Hanselmann
  def GetAllParams(cls):
519 65e183af Michael Hanselmann
    """Compute list of all parameters for an opcode.
520 65e183af Michael Hanselmann

521 65e183af Michael Hanselmann
    """
522 65e183af Michael Hanselmann
    slots = []
523 65e183af Michael Hanselmann
    for parent in cls.__mro__:
524 65e183af Michael Hanselmann
      slots.extend(getattr(parent, "OP_PARAMS", []))
525 65e183af Michael Hanselmann
    return slots
526 65e183af Michael Hanselmann
527 32683096 René Nussbaumer
  def Validate(self, set_defaults): # pylint: disable=W0221
528 1cbef6d8 Michael Hanselmann
    """Validate opcode parameters, optionally setting default values.
529 1cbef6d8 Michael Hanselmann

530 1cbef6d8 Michael Hanselmann
    @type set_defaults: bool
531 1cbef6d8 Michael Hanselmann
    @param set_defaults: Whether to set default values
532 1cbef6d8 Michael Hanselmann
    @raise errors.OpPrereqError: When a parameter value doesn't match
533 1cbef6d8 Michael Hanselmann
                                 requirements
534 1cbef6d8 Michael Hanselmann

535 1cbef6d8 Michael Hanselmann
    """
536 197b323b Michael Hanselmann
    for (attr_name, default, test, _) in self.GetAllParams():
537 1cbef6d8 Michael Hanselmann
      assert test == ht.NoType or callable(test)
538 1cbef6d8 Michael Hanselmann
539 1cbef6d8 Michael Hanselmann
      if not hasattr(self, attr_name):
540 1cbef6d8 Michael Hanselmann
        if default == ht.NoDefault:
541 1cbef6d8 Michael Hanselmann
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
542 1cbef6d8 Michael Hanselmann
                                     (self.OP_ID, attr_name),
543 1cbef6d8 Michael Hanselmann
                                     errors.ECODE_INVAL)
544 1cbef6d8 Michael Hanselmann
        elif set_defaults:
545 1cbef6d8 Michael Hanselmann
          if callable(default):
546 1cbef6d8 Michael Hanselmann
            dval = default()
547 1cbef6d8 Michael Hanselmann
          else:
548 1cbef6d8 Michael Hanselmann
            dval = default
549 1cbef6d8 Michael Hanselmann
          setattr(self, attr_name, dval)
550 1cbef6d8 Michael Hanselmann
551 1cbef6d8 Michael Hanselmann
      if test == ht.NoType:
552 1cbef6d8 Michael Hanselmann
        # no tests here
553 1cbef6d8 Michael Hanselmann
        continue
554 1cbef6d8 Michael Hanselmann
555 1cbef6d8 Michael Hanselmann
      if set_defaults or hasattr(self, attr_name):
556 1cbef6d8 Michael Hanselmann
        attr_val = getattr(self, attr_name)
557 1cbef6d8 Michael Hanselmann
        if not test(attr_val):
558 e1ebbfcf Iustin Pop
          logging.error("OpCode %s, parameter %s, has invalid type %s/value"
559 e1ebbfcf Iustin Pop
                        " '%s' expecting type %s",
560 68b2e985 René Nussbaumer
                        self.OP_ID, attr_name, type(attr_val), attr_val, test)
561 1cbef6d8 Michael Hanselmann
          raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
562 1cbef6d8 Michael Hanselmann
                                     (self.OP_ID, attr_name),
563 1cbef6d8 Michael Hanselmann
                                     errors.ECODE_INVAL)
564 1cbef6d8 Michael Hanselmann
565 df458e0b Iustin Pop
566 b247c6fc Michael Hanselmann
def _BuildJobDepCheck(relative):
567 b247c6fc Michael Hanselmann
  """Builds check for job dependencies (L{DEPEND_ATTR}).
568 b247c6fc Michael Hanselmann

569 b247c6fc Michael Hanselmann
  @type relative: bool
570 b247c6fc Michael Hanselmann
  @param relative: Whether to accept relative job IDs (negative)
571 b247c6fc Michael Hanselmann
  @rtype: callable
572 b247c6fc Michael Hanselmann

573 b247c6fc Michael Hanselmann
  """
574 b247c6fc Michael Hanselmann
  if relative:
575 b247c6fc Michael Hanselmann
    job_id = ht.TOr(ht.TJobId, ht.TRelativeJobId)
576 b247c6fc Michael Hanselmann
  else:
577 b247c6fc Michael Hanselmann
    job_id = ht.TJobId
578 b247c6fc Michael Hanselmann
579 b247c6fc Michael Hanselmann
  job_dep = \
580 dd076c21 Iustin Pop
    ht.TAnd(ht.TOr(ht.TList, ht.TTuple),
581 dd076c21 Iustin Pop
            ht.TIsLength(2),
582 b247c6fc Michael Hanselmann
            ht.TItems([job_id,
583 b247c6fc Michael Hanselmann
                       ht.TListOf(ht.TElemOf(constants.JOBS_FINALIZED))]))
584 b247c6fc Michael Hanselmann
585 ff8067cf Michael Hanselmann
  return ht.TMaybeListOf(job_dep)
586 b247c6fc Michael Hanselmann
587 b247c6fc Michael Hanselmann
588 b247c6fc Michael Hanselmann
TNoRelativeJobDependencies = _BuildJobDepCheck(False)
589 b247c6fc Michael Hanselmann
590 1ce03fb1 Michael Hanselmann
#: List of submission status and job ID as returned by C{SubmitManyJobs}
591 1456df62 Michael Hanselmann
_TJobIdListItem = \
592 1456df62 Michael Hanselmann
  ht.TAnd(ht.TIsLength(2),
593 1456df62 Michael Hanselmann
          ht.TItems([ht.Comment("success")(ht.TBool),
594 1456df62 Michael Hanselmann
                     ht.Comment("Job ID if successful, error message"
595 1456df62 Michael Hanselmann
                                " otherwise")(ht.TOr(ht.TString,
596 1456df62 Michael Hanselmann
                                                     ht.TJobId))]))
597 1456df62 Michael Hanselmann
TJobIdList = ht.TListOf(_TJobIdListItem)
598 1ce03fb1 Michael Hanselmann
599 f7686867 Michael Hanselmann
#: Result containing only list of submitted jobs
600 f7686867 Michael Hanselmann
TJobIdListOnly = ht.TStrictDict(True, True, {
601 1456df62 Michael Hanselmann
  constants.JOB_IDS_KEY: ht.Comment("List of submitted jobs")(TJobIdList),
602 f7686867 Michael Hanselmann
  })
603 f7686867 Michael Hanselmann
604 b247c6fc Michael Hanselmann
605 0e46916d Iustin Pop
class OpCode(BaseOpCode):
606 a7399f66 Iustin Pop
  """Abstract OpCode.
607 a7399f66 Iustin Pop

608 a7399f66 Iustin Pop
  This is the root of the actual OpCode hierarchy. All clases derived
609 a7399f66 Iustin Pop
  from this class should override OP_ID.
610 a7399f66 Iustin Pop

611 a7399f66 Iustin Pop
  @cvar OP_ID: The ID of this opcode. This should be unique amongst all
612 20777413 Iustin Pop
               children of this class.
613 bde8f481 Adeodato Simo
  @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
614 bde8f481 Adeodato Simo
                      string returned by Summary(); see the docstring of that
615 bde8f481 Adeodato Simo
                      method for details).
616 8bc17ebb Iustin Pop
  @cvar OP_DSC_FORMATTER: A callable that should format the OP_DSC_FIELD; if
617 8bc17ebb Iustin Pop
                          not present, then the field will be simply converted
618 8bc17ebb Iustin Pop
                          to string
619 65e183af Michael Hanselmann
  @cvar OP_PARAMS: List of opcode attributes, the default values they should
620 65e183af Michael Hanselmann
                   get if not already defined, and types they must match.
621 1ce03fb1 Michael Hanselmann
  @cvar OP_RESULT: Callable to verify opcode result
622 687c10d9 Iustin Pop
  @cvar WITH_LU: Boolean that specifies whether this should be included in
623 687c10d9 Iustin Pop
      mcpu's dispatch table
624 20777413 Iustin Pop
  @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
625 20777413 Iustin Pop
                 the check steps
626 8f5c488d Michael Hanselmann
  @ivar priority: Opcode priority for queue
627 a7399f66 Iustin Pop

628 a7399f66 Iustin Pop
  """
629 b459a848 Andrea Spadaccini
  # pylint: disable=E1101
630 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
631 687c10d9 Iustin Pop
  WITH_LU = True
632 65e183af Michael Hanselmann
  OP_PARAMS = [
633 45d4c81c Michael Hanselmann
    ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
634 fd9f58fd Iustin Pop
    ("debug_level", None, ht.TMaybe(ht.TNonNegativeInt), "Debug level"),
635 65e183af Michael Hanselmann
    ("priority", constants.OP_PRIO_DEFAULT,
636 45d4c81c Michael Hanselmann
     ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
637 b247c6fc Michael Hanselmann
    (DEPEND_ATTR, None, _BuildJobDepCheck(True),
638 b247c6fc Michael Hanselmann
     "Job dependencies; if used through ``SubmitManyJobs`` relative (negative)"
639 822a50c4 Michael Hanselmann
     " job IDs can be used; see :doc:`design document <design-chained-jobs>`"
640 822a50c4 Michael Hanselmann
     " for details"),
641 018ae30b Michael Hanselmann
    (COMMENT_ATTR, None, ht.TMaybeString,
642 018ae30b Michael Hanselmann
     "Comment describing the purpose of the opcode"),
643 dbae804a Michele Tartara
    (constants.OPCODE_REASON, None, ht.TMaybeList,
644 dbae804a Michele Tartara
     "The reason trail, describing why the OpCode is executed"),
645 65e183af Michael Hanselmann
    ]
646 1ce03fb1 Michael Hanselmann
  OP_RESULT = None
647 df458e0b Iustin Pop
648 df458e0b Iustin Pop
  def __getstate__(self):
649 df458e0b Iustin Pop
    """Specialized getstate for opcodes.
650 df458e0b Iustin Pop

651 a7399f66 Iustin Pop
    This method adds to the state dictionary the OP_ID of the class,
652 a7399f66 Iustin Pop
    so that on unload we can identify the correct class for
653 a7399f66 Iustin Pop
    instantiating the opcode.
654 a7399f66 Iustin Pop

655 a7399f66 Iustin Pop
    @rtype:   C{dict}
656 a7399f66 Iustin Pop
    @return:  the state as a dictionary
657 a7399f66 Iustin Pop

658 df458e0b Iustin Pop
    """
659 0e46916d Iustin Pop
    data = BaseOpCode.__getstate__(self)
660 df458e0b Iustin Pop
    data["OP_ID"] = self.OP_ID
661 df458e0b Iustin Pop
    return data
662 df458e0b Iustin Pop
663 df458e0b Iustin Pop
  @classmethod
664 00abdc96 Iustin Pop
  def LoadOpCode(cls, data):
665 df458e0b Iustin Pop
    """Generic load opcode method.
666 df458e0b Iustin Pop

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

671 a7399f66 Iustin Pop
    @type data:  C{dict}
672 a7399f66 Iustin Pop
    @param data: the serialized opcode
673 a7399f66 Iustin Pop

674 df458e0b Iustin Pop
    """
675 df458e0b Iustin Pop
    if not isinstance(data, dict):
676 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
677 df458e0b Iustin Pop
    if "OP_ID" not in data:
678 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
679 df458e0b Iustin Pop
    op_id = data["OP_ID"]
680 df458e0b Iustin Pop
    op_class = None
681 363acb1e Iustin Pop
    if op_id in OP_MAPPING:
682 363acb1e Iustin Pop
      op_class = OP_MAPPING[op_id]
683 363acb1e Iustin Pop
    else:
684 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
685 df458e0b Iustin Pop
                       op_id)
686 df458e0b Iustin Pop
    op = op_class()
687 df458e0b Iustin Pop
    new_data = data.copy()
688 df458e0b Iustin Pop
    del new_data["OP_ID"]
689 df458e0b Iustin Pop
    op.__setstate__(new_data)
690 df458e0b Iustin Pop
    return op
691 df458e0b Iustin Pop
692 60dd1473 Iustin Pop
  def Summary(self):
693 60dd1473 Iustin Pop
    """Generates a summary description of this opcode.
694 60dd1473 Iustin Pop

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

701 60dd1473 Iustin Pop
    """
702 ff0d18e6 Iustin Pop
    assert self.OP_ID is not None and len(self.OP_ID) > 3
703 60dd1473 Iustin Pop
    # all OP_ID start with OP_, we remove that
704 60dd1473 Iustin Pop
    txt = self.OP_ID[3:]
705 60dd1473 Iustin Pop
    field_name = getattr(self, "OP_DSC_FIELD", None)
706 60dd1473 Iustin Pop
    if field_name:
707 60dd1473 Iustin Pop
      field_value = getattr(self, field_name, None)
708 8bc17ebb Iustin Pop
      field_formatter = getattr(self, "OP_DSC_FORMATTER", None)
709 8bc17ebb Iustin Pop
      if callable(field_formatter):
710 8bc17ebb Iustin Pop
        field_value = field_formatter(field_value)
711 8bc17ebb Iustin Pop
      elif isinstance(field_value, (list, tuple)):
712 bc8bbda1 Iustin Pop
        field_value = ",".join(str(i) for i in field_value)
713 60dd1473 Iustin Pop
      txt = "%s(%s)" % (txt, field_value)
714 60dd1473 Iustin Pop
    return txt
715 60dd1473 Iustin Pop
716 3ce9a5e7 Michael Hanselmann
  def TinySummary(self):
717 3ce9a5e7 Michael Hanselmann
    """Generates a compact summary description of the opcode.
718 3ce9a5e7 Michael Hanselmann

719 3ce9a5e7 Michael Hanselmann
    """
720 3ce9a5e7 Michael Hanselmann
    assert self.OP_ID.startswith("OP_")
721 3ce9a5e7 Michael Hanselmann
722 3ce9a5e7 Michael Hanselmann
    text = self.OP_ID[3:]
723 3ce9a5e7 Michael Hanselmann
724 3ce9a5e7 Michael Hanselmann
    for (prefix, supplement) in _SUMMARY_PREFIX.items():
725 3ce9a5e7 Michael Hanselmann
      if text.startswith(prefix):
726 3ce9a5e7 Michael Hanselmann
        return supplement + text[len(prefix):]
727 3ce9a5e7 Michael Hanselmann
728 3ce9a5e7 Michael Hanselmann
    return text
729 3ce9a5e7 Michael Hanselmann
730 a8083063 Iustin Pop
731 afee0879 Iustin Pop
# cluster opcodes
732 afee0879 Iustin Pop
733 bc84ffa7 Iustin Pop
class OpClusterPostInit(OpCode):
734 b5f5fae9 Luca Bigliardi
  """Post cluster initialization.
735 b5f5fae9 Luca Bigliardi

736 b5f5fae9 Luca Bigliardi
  This opcode does not touch the cluster at all. Its purpose is to run hooks
737 b5f5fae9 Luca Bigliardi
  after the cluster has been initialized.
738 b5f5fae9 Luca Bigliardi

739 b5f5fae9 Luca Bigliardi
  """
740 c363310d René Nussbaumer
  OP_RESULT = ht.TBool
741 b5f5fae9 Luca Bigliardi
742 b5f5fae9 Luca Bigliardi
743 c6d43e9e Iustin Pop
class OpClusterDestroy(OpCode):
744 a7399f66 Iustin Pop
  """Destroy the cluster.
745 a7399f66 Iustin Pop

746 a7399f66 Iustin Pop
  This opcode has no other parameters. All the state is irreversibly
747 a7399f66 Iustin Pop
  lost after the execution of this opcode.
748 a7399f66 Iustin Pop

749 a7399f66 Iustin Pop
  """
750 c363310d René Nussbaumer
  OP_RESULT = ht.TNonEmptyString
751 a8083063 Iustin Pop
752 a8083063 Iustin Pop
753 a2f7ab92 Iustin Pop
class OpClusterQuery(OpCode):
754 fdc267f4 Iustin Pop
  """Query cluster information."""
755 415feb2e René Nussbaumer
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TAny)
756 a8083063 Iustin Pop
757 a8083063 Iustin Pop
758 fcad7225 Michael Hanselmann
class OpClusterVerify(OpCode):
759 fcad7225 Michael Hanselmann
  """Submits all jobs necessary to verify the cluster.
760 fcad7225 Michael Hanselmann

761 fcad7225 Michael Hanselmann
  """
762 fcad7225 Michael Hanselmann
  OP_PARAMS = [
763 fcad7225 Michael Hanselmann
    _PDebugSimulateErrors,
764 fcad7225 Michael Hanselmann
    _PErrorCodes,
765 fcad7225 Michael Hanselmann
    _PSkipChecks,
766 93f2399e Andrea Spadaccini
    _PIgnoreErrors,
767 fcad7225 Michael Hanselmann
    _PVerbose,
768 3c286190 Dimitris Aragiorgis
    ("group_name", None, ht.TMaybeString, "Group to verify"),
769 fcad7225 Michael Hanselmann
    ]
770 f7686867 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
771 fcad7225 Michael Hanselmann
772 fcad7225 Michael Hanselmann
773 bf93ae69 Adeodato Simo
class OpClusterVerifyConfig(OpCode):
774 bf93ae69 Adeodato Simo
  """Verify the cluster config.
775 bf93ae69 Adeodato Simo

776 bf93ae69 Adeodato Simo
  """
777 bf93ae69 Adeodato Simo
  OP_PARAMS = [
778 57106b74 Michael Hanselmann
    _PDebugSimulateErrors,
779 57106b74 Michael Hanselmann
    _PErrorCodes,
780 93f2399e Andrea Spadaccini
    _PIgnoreErrors,
781 57106b74 Michael Hanselmann
    _PVerbose,
782 bf93ae69 Adeodato Simo
    ]
783 f7686867 Michael Hanselmann
  OP_RESULT = ht.TBool
784 bf93ae69 Adeodato Simo
785 bf93ae69 Adeodato Simo
786 bf93ae69 Adeodato Simo
class OpClusterVerifyGroup(OpCode):
787 bf93ae69 Adeodato Simo
  """Run verify on a node group from the cluster.
788 a7399f66 Iustin Pop

789 a7399f66 Iustin Pop
  @type skip_checks: C{list}
790 a7399f66 Iustin Pop
  @ivar skip_checks: steps to be skipped from the verify process; this
791 a7399f66 Iustin Pop
                     needs to be a subset of
792 a7399f66 Iustin Pop
                     L{constants.VERIFY_OPTIONAL_CHECKS}; currently
793 a7399f66 Iustin Pop
                     only L{constants.VERIFY_NPLUSONE_MEM} can be passed
794 a7399f66 Iustin Pop

795 a7399f66 Iustin Pop
  """
796 bf93ae69 Adeodato Simo
  OP_DSC_FIELD = "group_name"
797 65e183af Michael Hanselmann
  OP_PARAMS = [
798 57106b74 Michael Hanselmann
    _PGroupName,
799 57106b74 Michael Hanselmann
    _PDebugSimulateErrors,
800 57106b74 Michael Hanselmann
    _PErrorCodes,
801 57106b74 Michael Hanselmann
    _PSkipChecks,
802 93f2399e Andrea Spadaccini
    _PIgnoreErrors,
803 57106b74 Michael Hanselmann
    _PVerbose,
804 65e183af Michael Hanselmann
    ]
805 f7686867 Michael Hanselmann
  OP_RESULT = ht.TBool
806 a8083063 Iustin Pop
807 a8083063 Iustin Pop
808 bd8210a7 Iustin Pop
class OpClusterVerifyDisks(OpCode):
809 150e978f Iustin Pop
  """Verify the cluster disks.
810 150e978f Iustin Pop

811 ae1a845c Michael Hanselmann
  """
812 f7686867 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
813 ae1a845c Michael Hanselmann
814 ae1a845c Michael Hanselmann
815 ae1a845c Michael Hanselmann
class OpGroupVerifyDisks(OpCode):
816 ae1a845c Michael Hanselmann
  """Verifies the status of all disks in a node group.
817 150e978f Iustin Pop

818 ae1a845c Michael Hanselmann
  Result: a tuple of three elements:
819 ae1a845c Michael Hanselmann
    - dict of node names with issues (values: error msg)
820 150e978f Iustin Pop
    - list of instances with degraded disks (that should be activated)
821 b63ed789 Iustin Pop
    - dict of instances with missing logical volumes (values: (node, vol)
822 b63ed789 Iustin Pop
      pairs with details about the missing volumes)
823 150e978f Iustin Pop

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

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

832 150e978f Iustin Pop
  """
833 ae1a845c Michael Hanselmann
  OP_DSC_FIELD = "group_name"
834 ae1a845c Michael Hanselmann
  OP_PARAMS = [
835 ae1a845c Michael Hanselmann
    _PGroupName,
836 ae1a845c Michael Hanselmann
    ]
837 1ce03fb1 Michael Hanselmann
  OP_RESULT = \
838 1ce03fb1 Michael Hanselmann
    ht.TAnd(ht.TIsLength(3),
839 1ce03fb1 Michael Hanselmann
            ht.TItems([ht.TDictOf(ht.TString, ht.TString),
840 1ce03fb1 Michael Hanselmann
                       ht.TListOf(ht.TString),
841 6973587f Michael Hanselmann
                       ht.TDictOf(ht.TString,
842 6973587f Michael Hanselmann
                                  ht.TListOf(ht.TListOf(ht.TString)))]))
843 150e978f Iustin Pop
844 150e978f Iustin Pop
845 5d01aca3 Iustin Pop
class OpClusterRepairDiskSizes(OpCode):
846 60975797 Iustin Pop
  """Verify the disk sizes of the instances and fixes configuration
847 60975797 Iustin Pop
  mimatches.
848 60975797 Iustin Pop

849 60975797 Iustin Pop
  Parameters: optional instances list, in case we want to restrict the
850 60975797 Iustin Pop
  checks to only a subset of the instances.
851 60975797 Iustin Pop

852 40d93e3b Bernardo Dal Seno
  Result: a list of tuples, (instance, disk, parameter, new-size) for changed
853 60975797 Iustin Pop
  configurations.
854 60975797 Iustin Pop

855 60975797 Iustin Pop
  In normal operation, the list should be empty.
856 60975797 Iustin Pop

857 60975797 Iustin Pop
  @type instances: list
858 60975797 Iustin Pop
  @ivar instances: the list of instances to check, or empty for all instances
859 60975797 Iustin Pop

860 60975797 Iustin Pop
  """
861 65e183af Michael Hanselmann
  OP_PARAMS = [
862 197b323b Michael Hanselmann
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
863 65e183af Michael Hanselmann
    ]
864 40d93e3b Bernardo Dal Seno
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(4),
865 c363310d René Nussbaumer
                                 ht.TItems([ht.TNonEmptyString,
866 2c9fa1ff Iustin Pop
                                            ht.TNonNegativeInt,
867 40d93e3b Bernardo Dal Seno
                                            ht.TNonEmptyString,
868 2c9fa1ff Iustin Pop
                                            ht.TNonNegativeInt])))
869 60975797 Iustin Pop
870 60975797 Iustin Pop
871 2f093ea0 Iustin Pop
class OpClusterConfigQuery(OpCode):
872 ae5849b5 Michael Hanselmann
  """Query cluster configuration values."""
873 65e183af Michael Hanselmann
  OP_PARAMS = [
874 3c286190 Dimitris Aragiorgis
    _POutputFields,
875 65e183af Michael Hanselmann
    ]
876 c363310d René Nussbaumer
  OP_RESULT = ht.TListOf(ht.TAny)
877 a8083063 Iustin Pop
878 a8083063 Iustin Pop
879 e126df25 Iustin Pop
class OpClusterRename(OpCode):
880 a7399f66 Iustin Pop
  """Rename the cluster.
881 a7399f66 Iustin Pop

882 a7399f66 Iustin Pop
  @type name: C{str}
883 a7399f66 Iustin Pop
  @ivar name: The new name of the cluster. The name and/or the master IP
884 a7399f66 Iustin Pop
              address will be changed to match the new name and its IP
885 a7399f66 Iustin Pop
              address.
886 a7399f66 Iustin Pop

887 a7399f66 Iustin Pop
  """
888 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
889 65e183af Michael Hanselmann
  OP_PARAMS = [
890 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
891 65e183af Michael Hanselmann
    ]
892 c363310d René Nussbaumer
  OP_RESULT = ht.TNonEmptyString
893 07bd8a51 Iustin Pop
894 07bd8a51 Iustin Pop
895 a6682fdc Iustin Pop
class OpClusterSetParams(OpCode):
896 a7399f66 Iustin Pop
  """Change the parameters of the cluster.
897 a7399f66 Iustin Pop

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

901 a7399f66 Iustin Pop
  """
902 65e183af Michael Hanselmann
  OP_PARAMS = [
903 e5c92cfb Klaus Aehlig
    _PForce,
904 2da9f556 René Nussbaumer
    _PHvState,
905 2da9f556 René Nussbaumer
    _PDiskState,
906 fd9f58fd Iustin Pop
    ("vg_name", None, ht.TMaybe(ht.TString), "Volume group name"),
907 65e183af Michael Hanselmann
    ("enabled_hypervisors", None,
908 fd9f58fd Iustin Pop
     ht.TMaybe(ht.TAnd(ht.TListOf(ht.TElemOf(constants.HYPER_TYPES)),
909 fd9f58fd Iustin Pop
                       ht.TTrue)),
910 45d4c81c Michael Hanselmann
     "List of enabled hypervisors"),
911 a138ead7 Michael Hanselmann
    ("hvparams", None,
912 fd9f58fd Iustin Pop
     ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
913 45d4c81c Michael Hanselmann
     "Cluster-wide hypervisor parameter defaults, hypervisor-dependent"),
914 fd9f58fd Iustin Pop
    ("beparams", None, ht.TMaybeDict,
915 45d4c81c Michael Hanselmann
     "Cluster-wide backend parameter defaults"),
916 fd9f58fd Iustin Pop
    ("os_hvp", None, ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
917 45d4c81c Michael Hanselmann
     "Cluster-wide per-OS hypervisor parameter defaults"),
918 a138ead7 Michael Hanselmann
    ("osparams", None,
919 fd9f58fd Iustin Pop
     ht.TMaybe(ht.TDictOf(ht.TNonEmptyString, ht.TDict)),
920 45d4c81c Michael Hanselmann
     "Cluster-wide OS parameter defaults"),
921 bc5d0215 Andrea Spadaccini
    _PDiskParams,
922 fd9f58fd Iustin Pop
    ("candidate_pool_size", None, ht.TMaybe(ht.TPositiveInt),
923 45d4c81c Michael Hanselmann
     "Master candidate pool size"),
924 45d4c81c Michael Hanselmann
    ("uid_pool", None, ht.NoType,
925 45d4c81c Michael Hanselmann
     "Set UID pool, must be list of lists describing UID ranges (two items,"
926 45d4c81c Michael Hanselmann
     " start and end inclusive)"),
927 45d4c81c Michael Hanselmann
    ("add_uids", None, ht.NoType,
928 45d4c81c Michael Hanselmann
     "Extend UID pool, must be list of lists describing UID ranges (two"
929 45d4c81c Michael Hanselmann
     " items, start and end inclusive) to be added"),
930 45d4c81c Michael Hanselmann
    ("remove_uids", None, ht.NoType,
931 45d4c81c Michael Hanselmann
     "Shrink UID pool, must be list of lists describing UID ranges (two"
932 45d4c81c Michael Hanselmann
     " items, start and end inclusive) to be removed"),
933 45d4c81c Michael Hanselmann
    ("maintain_node_health", None, ht.TMaybeBool,
934 45d4c81c Michael Hanselmann
     "Whether to automatically maintain node health"),
935 45d4c81c Michael Hanselmann
    ("prealloc_wipe_disks", None, ht.TMaybeBool,
936 45d4c81c Michael Hanselmann
     "Whether to wipe disks before allocating them to instances"),
937 45d4c81c Michael Hanselmann
    ("nicparams", None, ht.TMaybeDict, "Cluster-wide NIC parameter defaults"),
938 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Cluster-wide node parameter defaults"),
939 1b01390b René Nussbaumer
    ("ipolicy", None, ht.TMaybeDict,
940 1b01390b René Nussbaumer
     "Cluster-wide :ref:`instance policy <rapi-ipolicy>` specs"),
941 fd9f58fd Iustin Pop
    ("drbd_helper", None, ht.TMaybe(ht.TString), "DRBD helper program"),
942 fd9f58fd Iustin Pop
    ("default_iallocator", None, ht.TMaybe(ht.TString),
943 45d4c81c Michael Hanselmann
     "Default iallocator for cluster"),
944 fd9f58fd Iustin Pop
    ("master_netdev", None, ht.TMaybe(ht.TString),
945 45d4c81c Michael Hanselmann
     "Master network device"),
946 fd9f58fd Iustin Pop
    ("master_netmask", None, ht.TMaybe(ht.TNonNegativeInt),
947 5a8648eb Andrea Spadaccini
     "Netmask of the master IP"),
948 ff8067cf Michael Hanselmann
    ("reserved_lvs", None, ht.TMaybeListOf(ht.TNonEmptyString),
949 45d4c81c Michael Hanselmann
     "List of reserved LVs"),
950 45d4c81c Michael Hanselmann
    ("hidden_os", None, _TestClusterOsList,
951 0fb66bb1 Iustin Pop
     "Modify list of hidden operating systems: each modification must have"
952 0fb66bb1 Iustin Pop
     " two items, the operation and the OS name; the operation can be"
953 0fb66bb1 Iustin Pop
     " ``%s`` or ``%s``" % (constants.DDM_ADD, constants.DDM_REMOVE)),
954 45d4c81c Michael Hanselmann
    ("blacklisted_os", None, _TestClusterOsList,
955 0fb66bb1 Iustin Pop
     "Modify list of blacklisted operating systems: each modification must"
956 0fb66bb1 Iustin Pop
     " have two items, the operation and the OS name; the operation can be"
957 0fb66bb1 Iustin Pop
     " ``%s`` or ``%s``" % (constants.DDM_ADD, constants.DDM_REMOVE)),
958 bf689b7a Andrea Spadaccini
    ("use_external_mip_script", None, ht.TMaybeBool,
959 bf689b7a Andrea Spadaccini
     "Whether to use an external master IP address setup script"),
960 66af5ec5 Helga Velroyen
    ("enabled_disk_templates", None,
961 66af5ec5 Helga Velroyen
     ht.TMaybe(ht.TAnd(ht.TListOf(ht.TElemOf(constants.DISK_TEMPLATES)),
962 66af5ec5 Helga Velroyen
                       ht.TTrue)),
963 66af5ec5 Helga Velroyen
     "List of enabled disk templates"),
964 75f2ff7d Michele Tartara
    ("modify_etc_hosts", None, ht.TMaybeBool,
965 75f2ff7d Michele Tartara
     "Whether the cluster can modify and keep in sync the /etc/hosts files"),
966 4b7735f9 Iustin Pop
    ]
967 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
968 12515db7 Manuel Franceschini
969 12515db7 Manuel Franceschini
970 d1240007 Iustin Pop
class OpClusterRedistConf(OpCode):
971 afee0879 Iustin Pop
  """Force a full push of the cluster configuration.
972 afee0879 Iustin Pop

973 afee0879 Iustin Pop
  """
974 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
975 afee0879 Iustin Pop
976 83f72637 Michael Hanselmann
977 fb926117 Andrea Spadaccini
class OpClusterActivateMasterIp(OpCode):
978 fb926117 Andrea Spadaccini
  """Activate the master IP on the master node.
979 fb926117 Andrea Spadaccini

980 fb926117 Andrea Spadaccini
  """
981 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
982 fb926117 Andrea Spadaccini
983 fb926117 Andrea Spadaccini
984 fb926117 Andrea Spadaccini
class OpClusterDeactivateMasterIp(OpCode):
985 fb926117 Andrea Spadaccini
  """Deactivate the master IP on the master node.
986 fb926117 Andrea Spadaccini

987 fb926117 Andrea Spadaccini
  """
988 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
989 fb926117 Andrea Spadaccini
990 fb926117 Andrea Spadaccini
991 83f72637 Michael Hanselmann
class OpQuery(OpCode):
992 83f72637 Michael Hanselmann
  """Query for resources/items.
993 83f72637 Michael Hanselmann

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

998 83f72637 Michael Hanselmann
  """
999 e50d8412 Michael Hanselmann
  OP_DSC_FIELD = "what"
1000 65e183af Michael Hanselmann
  OP_PARAMS = [
1001 8e7078e0 Michael Hanselmann
    _PQueryWhat,
1002 ee13764f Michael Hanselmann
    _PUseLocking,
1003 45d4c81c Michael Hanselmann
    ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1004 45d4c81c Michael Hanselmann
     "Requested fields"),
1005 fd9f58fd Iustin Pop
    ("qfilter", None, ht.TMaybe(ht.TList),
1006 45d4c81c Michael Hanselmann
     "Query filter"),
1007 83f72637 Michael Hanselmann
    ]
1008 415feb2e René Nussbaumer
  OP_RESULT = \
1009 b02c6bdf Michael Hanselmann
    _GenerateObjectTypeCheck(objects.QueryResponse, {
1010 b02c6bdf Michael Hanselmann
      "fields": ht.TListOf(_TQueryFieldDef),
1011 b02c6bdf Michael Hanselmann
      "data": _TQueryResult,
1012 b02c6bdf Michael Hanselmann
      })
1013 83f72637 Michael Hanselmann
1014 83f72637 Michael Hanselmann
1015 83f72637 Michael Hanselmann
class OpQueryFields(OpCode):
1016 83f72637 Michael Hanselmann
  """Query for available resource/item fields.
1017 83f72637 Michael Hanselmann

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

1021 83f72637 Michael Hanselmann
  """
1022 e50d8412 Michael Hanselmann
  OP_DSC_FIELD = "what"
1023 65e183af Michael Hanselmann
  OP_PARAMS = [
1024 8e7078e0 Michael Hanselmann
    _PQueryWhat,
1025 ff8067cf Michael Hanselmann
    ("fields", None, ht.TMaybeListOf(ht.TNonEmptyString),
1026 8e7078e0 Michael Hanselmann
     "Requested fields; if not given, all are returned"),
1027 83f72637 Michael Hanselmann
    ]
1028 415feb2e René Nussbaumer
  OP_RESULT = \
1029 b02c6bdf Michael Hanselmann
    _GenerateObjectTypeCheck(objects.QueryFieldsResponse, {
1030 b02c6bdf Michael Hanselmann
      "fields": ht.TListOf(_TQueryFieldDef),
1031 b02c6bdf Michael Hanselmann
      })
1032 83f72637 Michael Hanselmann
1033 83f72637 Michael Hanselmann
1034 792af3ad René Nussbaumer
class OpOobCommand(OpCode):
1035 eb64da59 René Nussbaumer
  """Interact with OOB."""
1036 65e183af Michael Hanselmann
  OP_PARAMS = [
1037 c4ec0755 René Nussbaumer
    ("node_names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1038 1c3231aa Thomas Thrainer
     "List of node names to run the OOB command against"),
1039 1c3231aa Thomas Thrainer
    ("node_uuids", None, ht.TMaybeListOf(ht.TNonEmptyString),
1040 1c3231aa Thomas Thrainer
     "List of node UUIDs to run the OOB command against"),
1041 70296981 Iustin Pop
    ("command", ht.NoDefault, ht.TElemOf(constants.OOB_COMMANDS),
1042 c4ec0755 René Nussbaumer
     "OOB command to be run"),
1043 c4ec0755 René Nussbaumer
    ("timeout", constants.OOB_TIMEOUT, ht.TInt,
1044 c4ec0755 René Nussbaumer
     "Timeout before the OOB helper will be terminated"),
1045 c4ec0755 René Nussbaumer
    ("ignore_status", False, ht.TBool,
1046 c4ec0755 René Nussbaumer
     "Ignores the node offline status for power off"),
1047 2c9fa1ff Iustin Pop
    ("power_delay", constants.OOB_POWER_DELAY, ht.TNonNegativeFloat,
1048 beff3779 René Nussbaumer
     "Time in seconds to wait between powering on nodes"),
1049 eb64da59 René Nussbaumer
    ]
1050 c363310d René Nussbaumer
  # Fixme: Make it more specific with all the special cases in LUOobCommand
1051 415feb2e René Nussbaumer
  OP_RESULT = _TQueryResult
1052 eb64da59 René Nussbaumer
1053 eb64da59 René Nussbaumer
1054 e4d745a7 Michael Hanselmann
class OpRestrictedCommand(OpCode):
1055 e4d745a7 Michael Hanselmann
  """Runs a restricted command on node(s).
1056 e4d745a7 Michael Hanselmann

1057 e4d745a7 Michael Hanselmann
  """
1058 e4d745a7 Michael Hanselmann
  OP_PARAMS = [
1059 e4d745a7 Michael Hanselmann
    _PUseLocking,
1060 e4d745a7 Michael Hanselmann
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1061 e4d745a7 Michael Hanselmann
     "Nodes on which the command should be run (at least one)"),
1062 1c3231aa Thomas Thrainer
    ("node_uuids", None, ht.TMaybeListOf(ht.TNonEmptyString),
1063 1c3231aa Thomas Thrainer
     "Node UUIDs on which the command should be run (at least one)"),
1064 e4d745a7 Michael Hanselmann
    ("command", ht.NoDefault, ht.TNonEmptyString,
1065 e4d745a7 Michael Hanselmann
     "Command name (no parameters)"),
1066 e4d745a7 Michael Hanselmann
    ]
1067 e4d745a7 Michael Hanselmann
1068 e4d745a7 Michael Hanselmann
  _RESULT_ITEMS = [
1069 e4d745a7 Michael Hanselmann
    ht.Comment("success")(ht.TBool),
1070 e4d745a7 Michael Hanselmann
    ht.Comment("output or error message")(ht.TString),
1071 e4d745a7 Michael Hanselmann
    ]
1072 e4d745a7 Michael Hanselmann
1073 e4d745a7 Michael Hanselmann
  OP_RESULT = \
1074 e4d745a7 Michael Hanselmann
    ht.TListOf(ht.TAnd(ht.TIsLength(len(_RESULT_ITEMS)),
1075 e4d745a7 Michael Hanselmann
                       ht.TItems(_RESULT_ITEMS)))
1076 e4d745a7 Michael Hanselmann
1077 e4d745a7 Michael Hanselmann
1078 07bd8a51 Iustin Pop
# node opcodes
1079 07bd8a51 Iustin Pop
1080 73d565a3 Iustin Pop
class OpNodeRemove(OpCode):
1081 a7399f66 Iustin Pop
  """Remove a node.
1082 a7399f66 Iustin Pop

1083 a7399f66 Iustin Pop
  @type node_name: C{str}
1084 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to remove. If the node still has
1085 a7399f66 Iustin Pop
                   instances on it, the operation will fail.
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 1c3231aa Thomas Thrainer
    _PNodeUuid
1092 65e183af Michael Hanselmann
    ]
1093 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1094 a8083063 Iustin Pop
1095 a8083063 Iustin Pop
1096 d817d49f Iustin Pop
class OpNodeAdd(OpCode):
1097 a7399f66 Iustin Pop
  """Add a node to the cluster.
1098 a7399f66 Iustin Pop

1099 a7399f66 Iustin Pop
  @type node_name: C{str}
1100 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to add. This can be a short name,
1101 a7399f66 Iustin Pop
                   but it will be expanded to the FQDN.
1102 a7399f66 Iustin Pop
  @type primary_ip: IP address
1103 a7399f66 Iustin Pop
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
1104 a7399f66 Iustin Pop
                    opcode is submitted, but will be filled during the node
1105 a7399f66 Iustin Pop
                    add (so it will be visible in the job query).
1106 a7399f66 Iustin Pop
  @type secondary_ip: IP address
1107 a7399f66 Iustin Pop
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
1108 a7399f66 Iustin Pop
                      if the cluster has been initialized in 'dual-network'
1109 a7399f66 Iustin Pop
                      mode, otherwise it must not be given.
1110 a7399f66 Iustin Pop
  @type readd: C{bool}
1111 a7399f66 Iustin Pop
  @ivar readd: Whether to re-add an existing node to the cluster. If
1112 a7399f66 Iustin Pop
               this is not passed, then the operation will abort if the node
1113 a7399f66 Iustin Pop
               name is already in the cluster; use this parameter to 'repair'
1114 a7399f66 Iustin Pop
               a node that had its configuration broken, or was reinstalled
1115 a7399f66 Iustin Pop
               without removal from the cluster.
1116 f936c153 Iustin Pop
  @type group: C{str}
1117 f936c153 Iustin Pop
  @ivar group: The node group to which this node will belong.
1118 fd3d37b6 Iustin Pop
  @type vm_capable: C{bool}
1119 fd3d37b6 Iustin Pop
  @ivar vm_capable: The vm_capable node attribute
1120 fd3d37b6 Iustin Pop
  @type master_capable: C{bool}
1121 fd3d37b6 Iustin Pop
  @ivar master_capable: The master_capable node attribute
1122 a7399f66 Iustin Pop

1123 a7399f66 Iustin Pop
  """
1124 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
1125 65e183af Michael Hanselmann
  OP_PARAMS = [
1126 65e183af Michael Hanselmann
    _PNodeName,
1127 085e0d9f René Nussbaumer
    _PHvState,
1128 085e0d9f René Nussbaumer
    _PDiskState,
1129 45d4c81c Michael Hanselmann
    ("primary_ip", None, ht.NoType, "Primary IP address"),
1130 45d4c81c Michael Hanselmann
    ("secondary_ip", None, ht.TMaybeString, "Secondary IP address"),
1131 45d4c81c Michael Hanselmann
    ("readd", False, ht.TBool, "Whether node is re-added to cluster"),
1132 45d4c81c Michael Hanselmann
    ("group", None, ht.TMaybeString, "Initial node group"),
1133 45d4c81c Michael Hanselmann
    ("master_capable", None, ht.TMaybeBool,
1134 45d4c81c Michael Hanselmann
     "Whether node can become master or master candidate"),
1135 45d4c81c Michael Hanselmann
    ("vm_capable", None, ht.TMaybeBool,
1136 45d4c81c Michael Hanselmann
     "Whether node can host instances"),
1137 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Node parameters"),
1138 65e183af Michael Hanselmann
    ]
1139 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1140 a8083063 Iustin Pop
1141 a8083063 Iustin Pop
1142 2237687b Iustin Pop
class OpNodeQuery(OpCode):
1143 a8083063 Iustin Pop
  """Compute the list of nodes."""
1144 65e183af Michael Hanselmann
  OP_PARAMS = [
1145 65e183af Michael Hanselmann
    _POutputFields,
1146 45d4c81c Michael Hanselmann
    _PUseLocking,
1147 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1148 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
1149 65e183af Michael Hanselmann
    ]
1150 415feb2e René Nussbaumer
  OP_RESULT = _TOldQueryResult
1151 a8083063 Iustin Pop
1152 a8083063 Iustin Pop
1153 8ed55bfd Iustin Pop
class OpNodeQueryvols(OpCode):
1154 dcb93971 Michael Hanselmann
  """Get list of volumes on node."""
1155 65e183af Michael Hanselmann
  OP_PARAMS = [
1156 65e183af Michael Hanselmann
    _POutputFields,
1157 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1158 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
1159 65e183af Michael Hanselmann
    ]
1160 c363310d René Nussbaumer
  OP_RESULT = ht.TListOf(ht.TAny)
1161 dcb93971 Michael Hanselmann
1162 dcb93971 Michael Hanselmann
1163 ad8d0595 Iustin Pop
class OpNodeQueryStorage(OpCode):
1164 9e5442ce Michael Hanselmann
  """Get information on storage for node(s)."""
1165 65e183af Michael Hanselmann
  OP_PARAMS = [
1166 65e183af Michael Hanselmann
    _POutputFields,
1167 65e183af Michael Hanselmann
    _PStorageType,
1168 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "List of nodes"),
1169 45d4c81c Michael Hanselmann
    ("name", None, ht.TMaybeString, "Storage name"),
1170 9e5442ce Michael Hanselmann
    ]
1171 415feb2e René Nussbaumer
  OP_RESULT = _TOldQueryResult
1172 9e5442ce Michael Hanselmann
1173 9e5442ce Michael Hanselmann
1174 2cee4077 Iustin Pop
class OpNodeModifyStorage(OpCode):
1175 099c52ad Iustin Pop
  """Modifies the properies of a storage unit"""
1176 32708d0a Iustin Pop
  OP_DSC_FIELD = "node_name"
1177 65e183af Michael Hanselmann
  OP_PARAMS = [
1178 65e183af Michael Hanselmann
    _PNodeName,
1179 1c3231aa Thomas Thrainer
    _PNodeUuid,
1180 65e183af Michael Hanselmann
    _PStorageType,
1181 45d4c81c Michael Hanselmann
    _PStorageName,
1182 45d4c81c Michael Hanselmann
    ("changes", ht.NoDefault, ht.TDict, "Requested changes"),
1183 efb8da02 Michael Hanselmann
    ]
1184 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1185 efb8da02 Michael Hanselmann
1186 efb8da02 Michael Hanselmann
1187 76aef8fc Michael Hanselmann
class OpRepairNodeStorage(OpCode):
1188 76aef8fc Michael Hanselmann
  """Repairs the volume group on a node."""
1189 76aef8fc Michael Hanselmann
  OP_DSC_FIELD = "node_name"
1190 65e183af Michael Hanselmann
  OP_PARAMS = [
1191 65e183af Michael Hanselmann
    _PNodeName,
1192 1c3231aa Thomas Thrainer
    _PNodeUuid,
1193 65e183af Michael Hanselmann
    _PStorageType,
1194 45d4c81c Michael Hanselmann
    _PStorageName,
1195 45d4c81c Michael Hanselmann
    _PIgnoreConsistency,
1196 76aef8fc Michael Hanselmann
    ]
1197 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1198 76aef8fc Michael Hanselmann
1199 76aef8fc Michael Hanselmann
1200 f13973c4 Iustin Pop
class OpNodeSetParams(OpCode):
1201 b31c8676 Iustin Pop
  """Change the parameters of a node."""
1202 b31c8676 Iustin Pop
  OP_DSC_FIELD = "node_name"
1203 65e183af Michael Hanselmann
  OP_PARAMS = [
1204 65e183af Michael Hanselmann
    _PNodeName,
1205 1c3231aa Thomas Thrainer
    _PNodeUuid,
1206 65e183af Michael Hanselmann
    _PForce,
1207 0ec2ce46 René Nussbaumer
    _PHvState,
1208 0ec2ce46 René Nussbaumer
    _PDiskState,
1209 45d4c81c Michael Hanselmann
    ("master_candidate", None, ht.TMaybeBool,
1210 45d4c81c Michael Hanselmann
     "Whether the node should become a master candidate"),
1211 45d4c81c Michael Hanselmann
    ("offline", None, ht.TMaybeBool,
1212 45d4c81c Michael Hanselmann
     "Whether the node should be marked as offline"),
1213 45d4c81c Michael Hanselmann
    ("drained", None, ht.TMaybeBool,
1214 45d4c81c Michael Hanselmann
     "Whether the node should be marked as drained"),
1215 45d4c81c Michael Hanselmann
    ("auto_promote", False, ht.TBool,
1216 45d4c81c Michael Hanselmann
     "Whether node(s) should be promoted to master candidate if necessary"),
1217 45d4c81c Michael Hanselmann
    ("master_capable", None, ht.TMaybeBool,
1218 45d4c81c Michael Hanselmann
     "Denote whether node can become master or master candidate"),
1219 45d4c81c Michael Hanselmann
    ("vm_capable", None, ht.TMaybeBool,
1220 45d4c81c Michael Hanselmann
     "Denote whether node can host instances"),
1221 45d4c81c Michael Hanselmann
    ("secondary_ip", None, ht.TMaybeString,
1222 45d4c81c Michael Hanselmann
     "Change node's secondary IP address"),
1223 45d4c81c Michael Hanselmann
    ("ndparams", None, ht.TMaybeDict, "Set node parameters"),
1224 45d4c81c Michael Hanselmann
    ("powered", None, ht.TMaybeBool,
1225 45d4c81c Michael Hanselmann
     "Whether the node should be marked as powered"),
1226 b31c8676 Iustin Pop
    ]
1227 1456df62 Michael Hanselmann
  OP_RESULT = _TSetParamsResult
1228 b31c8676 Iustin Pop
1229 f5118ade Iustin Pop
1230 e0d4735f Iustin Pop
class OpNodePowercycle(OpCode):
1231 f5118ade Iustin Pop
  """Tries to powercycle a node."""
1232 f5118ade Iustin Pop
  OP_DSC_FIELD = "node_name"
1233 65e183af Michael Hanselmann
  OP_PARAMS = [
1234 65e183af Michael Hanselmann
    _PNodeName,
1235 1c3231aa Thomas Thrainer
    _PNodeUuid,
1236 65e183af Michael Hanselmann
    _PForce,
1237 f5118ade Iustin Pop
    ]
1238 c363310d René Nussbaumer
  OP_RESULT = ht.TMaybeString
1239 f5118ade Iustin Pop
1240 7ffc5a86 Michael Hanselmann
1241 5b14a488 Iustin Pop
class OpNodeMigrate(OpCode):
1242 80cb875c Michael Hanselmann
  """Migrate all instances from a node."""
1243 80cb875c Michael Hanselmann
  OP_DSC_FIELD = "node_name"
1244 65e183af Michael Hanselmann
  OP_PARAMS = [
1245 65e183af Michael Hanselmann
    _PNodeName,
1246 1c3231aa Thomas Thrainer
    _PNodeUuid,
1247 65e183af Michael Hanselmann
    _PMigrationMode,
1248 65e183af Michael Hanselmann
    _PMigrationLive,
1249 f8fa4175 Michael Hanselmann
    _PMigrationTargetNode,
1250 1c3231aa Thomas Thrainer
    _PMigrationTargetNodeUuid,
1251 8c0b16f6 Guido Trotter
    _PAllowRuntimeChgs,
1252 9fa567b3 René Nussbaumer
    _PIgnoreIpolicy,
1253 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding the target node"
1254 89514061 Iustin Pop
                     " for shared-storage instances"),
1255 80cb875c Michael Hanselmann
    ]
1256 65c9591c Michael Hanselmann
  OP_RESULT = TJobIdListOnly
1257 80cb875c Michael Hanselmann
1258 80cb875c Michael Hanselmann
1259 e1f23243 Michael Hanselmann
class OpNodeEvacuate(OpCode):
1260 e1f23243 Michael Hanselmann
  """Evacuate instances off a number of nodes."""
1261 e1f23243 Michael Hanselmann
  OP_DSC_FIELD = "node_name"
1262 e1f23243 Michael Hanselmann
  OP_PARAMS = [
1263 e1f23243 Michael Hanselmann
    _PEarlyRelease,
1264 e1f23243 Michael Hanselmann
    _PNodeName,
1265 1c3231aa Thomas Thrainer
    _PNodeUuid,
1266 e1f23243 Michael Hanselmann
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1267 1c3231aa Thomas Thrainer
    ("remote_node_uuid", None, ht.TMaybeString, "New secondary node UUID"),
1268 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for computing solution"),
1269 cb92e7a1 Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.NODE_EVAC_MODES),
1270 e1f23243 Michael Hanselmann
     "Node evacuation mode"),
1271 e1f23243 Michael Hanselmann
    ]
1272 1456df62 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
1273 e1f23243 Michael Hanselmann
1274 e1f23243 Michael Hanselmann
1275 a8083063 Iustin Pop
# instance opcodes
1276 a8083063 Iustin Pop
1277 e1530b10 Iustin Pop
class OpInstanceCreate(OpCode):
1278 9bf56d77 Michael Hanselmann
  """Create an instance.
1279 9bf56d77 Michael Hanselmann

1280 9bf56d77 Michael Hanselmann
  @ivar instance_name: Instance name
1281 9bf56d77 Michael Hanselmann
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
1282 9bf56d77 Michael Hanselmann
  @ivar source_handshake: Signed handshake from source (remote import only)
1283 9bf56d77 Michael Hanselmann
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
1284 9bf56d77 Michael Hanselmann
  @ivar source_instance_name: Previous name of instance (remote import only)
1285 dae91d02 Michael Hanselmann
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
1286 dae91d02 Michael Hanselmann
    (remote import only)
1287 9bf56d77 Michael Hanselmann

1288 9bf56d77 Michael Hanselmann
  """
1289 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1290 65e183af Michael Hanselmann
  OP_PARAMS = [
1291 65e183af Michael Hanselmann
    _PInstanceName,
1292 45d4c81c Michael Hanselmann
    _PForceVariant,
1293 45d4c81c Michael Hanselmann
    _PWaitForSync,
1294 45d4c81c Michael Hanselmann
    _PNameCheck,
1295 10889e0c René Nussbaumer
    _PIgnoreIpolicy,
1296 1f1188c3 Michael Hanselmann
    _POpportunisticLocking,
1297 45d4c81c Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict, "Backend parameters for instance"),
1298 735e1318 Michael Hanselmann
    ("disks", ht.NoDefault, ht.TListOf(_TDiskParams),
1299 526a662a Michael Hanselmann
     "Disk descriptions, for example ``[{\"%s\": 100}, {\"%s\": 5}]``;"
1300 526a662a Michael Hanselmann
     " each disk definition must contain a ``%s`` value and"
1301 526a662a Michael Hanselmann
     " can contain an optional ``%s`` value denoting the disk access mode"
1302 526a662a Michael Hanselmann
     " (%s)" %
1303 526a662a Michael Hanselmann
     (constants.IDISK_SIZE, constants.IDISK_SIZE, constants.IDISK_SIZE,
1304 526a662a Michael Hanselmann
      constants.IDISK_MODE,
1305 526a662a Michael Hanselmann
      " or ".join("``%s``" % i for i in sorted(constants.DISK_ACCESS_SET)))),
1306 45fe090b Michael Hanselmann
    ("disk_template", ht.NoDefault, _BuildDiskTemplateCheck(True),
1307 45fe090b Michael Hanselmann
     "Disk template"),
1308 fd9f58fd Iustin Pop
    ("file_driver", None, ht.TMaybe(ht.TElemOf(constants.FILE_DRIVER)),
1309 45d4c81c Michael Hanselmann
     "Driver for file-backed disks"),
1310 45d4c81c Michael Hanselmann
    ("file_storage_dir", None, ht.TMaybeString,
1311 45d4c81c Michael Hanselmann
     "Directory for storing file-backed disks"),
1312 45d4c81c Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict,
1313 45d4c81c Michael Hanselmann
     "Hypervisor parameters for instance, hypervisor-dependent"),
1314 45d4c81c Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString, "Hypervisor"),
1315 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding which node(s) to use"),
1316 45d4c81c Michael Hanselmann
    ("identify_defaults", False, ht.TBool,
1317 45d4c81c Michael Hanselmann
     "Reset instance parameters to default if equal"),
1318 45d4c81c Michael Hanselmann
    ("ip_check", True, ht.TBool, _PIpCheckDoc),
1319 eaa4c57c Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool, "Check for conflicting IPs"),
1320 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.INSTANCE_CREATE_MODES),
1321 45d4c81c Michael Hanselmann
     "Instance creation mode"),
1322 526a662a Michael Hanselmann
    ("nics", ht.NoDefault, ht.TListOf(_TestNicDef),
1323 526a662a Michael Hanselmann
     "List of NIC (network interface) definitions, for example"
1324 526a662a Michael Hanselmann
     " ``[{}, {}, {\"%s\": \"198.51.100.4\"}]``; each NIC definition can"
1325 526a662a Michael Hanselmann
     " contain the optional values %s" %
1326 526a662a Michael Hanselmann
     (constants.INIC_IP,
1327 526a662a Michael Hanselmann
      ", ".join("``%s``" % i for i in sorted(constants.INIC_PARAMS)))),
1328 45d4c81c Michael Hanselmann
    ("no_install", None, ht.TMaybeBool,
1329 45d4c81c Michael Hanselmann
     "Do not install the OS (will disable automatic start)"),
1330 45d4c81c Michael Hanselmann
    ("osparams", ht.EmptyDict, ht.TDict, "OS parameters for instance"),
1331 45d4c81c Michael Hanselmann
    ("os_type", None, ht.TMaybeString, "Operating system"),
1332 45d4c81c Michael Hanselmann
    ("pnode", None, ht.TMaybeString, "Primary node"),
1333 1c3231aa Thomas Thrainer
    ("pnode_uuid", None, ht.TMaybeString, "Primary node UUID"),
1334 45d4c81c Michael Hanselmann
    ("snode", None, ht.TMaybeString, "Secondary node"),
1335 1c3231aa Thomas Thrainer
    ("snode_uuid", None, ht.TMaybeString, "Secondary node UUID"),
1336 fd9f58fd Iustin Pop
    ("source_handshake", None, ht.TMaybe(ht.TList),
1337 45d4c81c Michael Hanselmann
     "Signed handshake from source (remote import only)"),
1338 45d4c81c Michael Hanselmann
    ("source_instance_name", None, ht.TMaybeString,
1339 45d4c81c Michael Hanselmann
     "Source instance name (remote import only)"),
1340 65e183af Michael Hanselmann
    ("source_shutdown_timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT,
1341 2c9fa1ff Iustin Pop
     ht.TNonNegativeInt,
1342 526a662a Michael Hanselmann
     "How long source instance was given to shut down (remote import only)"),
1343 45d4c81c Michael Hanselmann
    ("source_x509_ca", None, ht.TMaybeString,
1344 45d4c81c Michael Hanselmann
     "Source X509 CA in PEM format (remote import only)"),
1345 45d4c81c Michael Hanselmann
    ("src_node", None, ht.TMaybeString, "Source node for import"),
1346 1c3231aa Thomas Thrainer
    ("src_node_uuid", None, ht.TMaybeString, "Source node UUID for import"),
1347 45d4c81c Michael Hanselmann
    ("src_path", None, ht.TMaybeString, "Source directory for import"),
1348 45d4c81c Michael Hanselmann
    ("start", True, ht.TBool, "Whether to start instance after creation"),
1349 720f56c8 Apollon Oikonomopoulos
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Instance tags"),
1350 3b6d8c9b Iustin Pop
    ]
1351 1456df62 Michael Hanselmann
  OP_RESULT = ht.Comment("instance nodes")(ht.TListOf(ht.TNonEmptyString))
1352 a8083063 Iustin Pop
1353 a8083063 Iustin Pop
1354 12e62af5 René Nussbaumer
class OpInstanceMultiAlloc(OpCode):
1355 12e62af5 René Nussbaumer
  """Allocates multiple instances.
1356 12e62af5 René Nussbaumer

1357 12e62af5 René Nussbaumer
  """
1358 12e62af5 René Nussbaumer
  OP_PARAMS = [
1359 1f1188c3 Michael Hanselmann
    _POpportunisticLocking,
1360 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator used to allocate all the instances"),
1361 4c405df7 Iustin Pop
    ("instances", ht.EmptyList, ht.TListOf(ht.TInstanceOf(OpInstanceCreate)),
1362 12e62af5 René Nussbaumer
     "List of instance create opcodes describing the instances to allocate"),
1363 12e62af5 René Nussbaumer
    ]
1364 12e62af5 René Nussbaumer
  _JOB_LIST = ht.Comment("List of submitted jobs")(TJobIdList)
1365 12e62af5 René Nussbaumer
  ALLOCATABLE_KEY = "allocatable"
1366 12e62af5 René Nussbaumer
  FAILED_KEY = "allocatable"
1367 12e62af5 René Nussbaumer
  OP_RESULT = ht.TStrictDict(True, True, {
1368 12e62af5 René Nussbaumer
    constants.JOB_IDS_KEY: _JOB_LIST,
1369 12e62af5 René Nussbaumer
    ALLOCATABLE_KEY: ht.TListOf(ht.TNonEmptyString),
1370 3c286190 Dimitris Aragiorgis
    FAILED_KEY: ht.TListOf(ht.TNonEmptyString),
1371 12e62af5 René Nussbaumer
    })
1372 12e62af5 René Nussbaumer
1373 12e62af5 René Nussbaumer
  def __getstate__(self):
1374 12e62af5 René Nussbaumer
    """Generic serializer.
1375 12e62af5 René Nussbaumer

1376 12e62af5 René Nussbaumer
    """
1377 12e62af5 René Nussbaumer
    state = OpCode.__getstate__(self)
1378 12e62af5 René Nussbaumer
    if hasattr(self, "instances"):
1379 12e62af5 René Nussbaumer
      # pylint: disable=E1101
1380 12e62af5 René Nussbaumer
      state["instances"] = [inst.__getstate__() for inst in self.instances]
1381 12e62af5 René Nussbaumer
    return state
1382 12e62af5 René Nussbaumer
1383 12e62af5 René Nussbaumer
  def __setstate__(self, state):
1384 12e62af5 René Nussbaumer
    """Generic unserializer.
1385 12e62af5 René Nussbaumer

1386 12e62af5 René Nussbaumer
    This method just restores from the serialized state the attributes
1387 12e62af5 René Nussbaumer
    of the current instance.
1388 12e62af5 René Nussbaumer

1389 12e62af5 René Nussbaumer
    @param state: the serialized opcode data
1390 12e62af5 René Nussbaumer
    @type state: C{dict}
1391 12e62af5 René Nussbaumer

1392 12e62af5 René Nussbaumer
    """
1393 12e62af5 René Nussbaumer
    if not isinstance(state, dict):
1394 12e62af5 René Nussbaumer
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
1395 12e62af5 René Nussbaumer
                       type(state))
1396 12e62af5 René Nussbaumer
1397 12e62af5 René Nussbaumer
    if "instances" in state:
1398 5dff65da Michael Hanselmann
      state["instances"] = map(OpCode.LoadOpCode, state["instances"])
1399 5dff65da Michael Hanselmann
1400 12e62af5 René Nussbaumer
    return OpCode.__setstate__(self, state)
1401 12e62af5 René Nussbaumer
1402 9bc5ac44 René Nussbaumer
  def Validate(self, set_defaults):
1403 9bc5ac44 René Nussbaumer
    """Validates this opcode.
1404 9bc5ac44 René Nussbaumer

1405 9bc5ac44 René Nussbaumer
    We do this recursively.
1406 9bc5ac44 René Nussbaumer

1407 9bc5ac44 René Nussbaumer
    """
1408 9bc5ac44 René Nussbaumer
    OpCode.Validate(self, set_defaults)
1409 9bc5ac44 René Nussbaumer
1410 3779121c René Nussbaumer
    for inst in self.instances: # pylint: disable=E1101
1411 9bc5ac44 René Nussbaumer
      inst.Validate(set_defaults)
1412 9bc5ac44 René Nussbaumer
1413 12e62af5 René Nussbaumer
1414 5073fd8f Iustin Pop
class OpInstanceReinstall(OpCode):
1415 fdc267f4 Iustin Pop
  """Reinstall an instance's OS."""
1416 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1417 65e183af Michael Hanselmann
  OP_PARAMS = [
1418 65e183af Michael Hanselmann
    _PInstanceName,
1419 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1420 45d4c81c Michael Hanselmann
    _PForceVariant,
1421 45d4c81c Michael Hanselmann
    ("os_type", None, ht.TMaybeString, "Instance operating system"),
1422 45d4c81c Michael Hanselmann
    ("osparams", None, ht.TMaybeDict, "Temporary OS parameters"),
1423 65e183af Michael Hanselmann
    ]
1424 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1425 fe7b0351 Michael Hanselmann
1426 fe7b0351 Michael Hanselmann
1427 3cd2d4b1 Iustin Pop
class OpInstanceRemove(OpCode):
1428 a8083063 Iustin Pop
  """Remove an instance."""
1429 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1430 65e183af Michael Hanselmann
  OP_PARAMS = [
1431 65e183af Michael Hanselmann
    _PInstanceName,
1432 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1433 65e183af Michael Hanselmann
    _PShutdownTimeout,
1434 45d4c81c Michael Hanselmann
    ("ignore_failures", False, ht.TBool,
1435 45d4c81c Michael Hanselmann
     "Whether to ignore failures during removal"),
1436 fc1baca9 Michael Hanselmann
    ]
1437 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1438 a8083063 Iustin Pop
1439 a8083063 Iustin Pop
1440 5659e2e2 Iustin Pop
class OpInstanceRename(OpCode):
1441 decd5f45 Iustin Pop
  """Rename an instance."""
1442 65e183af Michael Hanselmann
  OP_PARAMS = [
1443 65e183af Michael Hanselmann
    _PInstanceName,
1444 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1445 45d4c81c Michael Hanselmann
    _PNameCheck,
1446 45d4c81c Michael Hanselmann
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New instance name"),
1447 45d4c81c Michael Hanselmann
    ("ip_check", False, ht.TBool, _PIpCheckDoc),
1448 4f05fd3b Iustin Pop
    ]
1449 1456df62 Michael Hanselmann
  OP_RESULT = ht.Comment("New instance name")(ht.TNonEmptyString)
1450 decd5f45 Iustin Pop
1451 decd5f45 Iustin Pop
1452 c873d91c Iustin Pop
class OpInstanceStartup(OpCode):
1453 fdc267f4 Iustin Pop
  """Startup an instance."""
1454 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1455 65e183af Michael Hanselmann
  OP_PARAMS = [
1456 65e183af Michael Hanselmann
    _PInstanceName,
1457 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1458 65e183af Michael Hanselmann
    _PForce,
1459 65e183af Michael Hanselmann
    _PIgnoreOfflineNodes,
1460 45d4c81c Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict,
1461 45d4c81c Michael Hanselmann
     "Temporary hypervisor parameters, hypervisor-dependent"),
1462 45d4c81c Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict, "Temporary backend parameters"),
1463 9b64e486 Iustin Pop
    _PNoRemember,
1464 323f9095 Stephen Shirley
    _PStartupPaused,
1465 4f05fd3b Iustin Pop
    ]
1466 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1467 a8083063 Iustin Pop
1468 a8083063 Iustin Pop
1469 ee3e37a7 Iustin Pop
class OpInstanceShutdown(OpCode):
1470 fdc267f4 Iustin Pop
  """Shutdown an instance."""
1471 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1472 65e183af Michael Hanselmann
  OP_PARAMS = [
1473 65e183af Michael Hanselmann
    _PInstanceName,
1474 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1475 0d57ce24 Guido Trotter
    _PForce,
1476 65e183af Michael Hanselmann
    _PIgnoreOfflineNodes,
1477 2c9fa1ff Iustin Pop
    ("timeout", constants.DEFAULT_SHUTDOWN_TIMEOUT, ht.TNonNegativeInt,
1478 45d4c81c Michael Hanselmann
     "How long to wait for instance to shut down"),
1479 9b64e486 Iustin Pop
    _PNoRemember,
1480 b44bd844 Michael Hanselmann
    ]
1481 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1482 a8083063 Iustin Pop
1483 a8083063 Iustin Pop
1484 90ab1a95 Iustin Pop
class OpInstanceReboot(OpCode):
1485 bf6929a2 Alexander Schreiber
  """Reboot an instance."""
1486 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1487 65e183af Michael Hanselmann
  OP_PARAMS = [
1488 65e183af Michael Hanselmann
    _PInstanceName,
1489 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1490 65e183af Michael Hanselmann
    _PShutdownTimeout,
1491 45d4c81c Michael Hanselmann
    ("ignore_secondaries", False, ht.TBool,
1492 45d4c81c Michael Hanselmann
     "Whether to start the instance even if secondary disks are failing"),
1493 45d4c81c Michael Hanselmann
    ("reboot_type", ht.NoDefault, ht.TElemOf(constants.REBOOT_TYPES),
1494 45d4c81c Michael Hanselmann
     "How to reboot instance"),
1495 4f05fd3b Iustin Pop
    ]
1496 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1497 bf6929a2 Alexander Schreiber
1498 bf6929a2 Alexander Schreiber
1499 668f755d Iustin Pop
class OpInstanceReplaceDisks(OpCode):
1500 fdc267f4 Iustin Pop
  """Replace the disks of an instance."""
1501 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1502 65e183af Michael Hanselmann
  OP_PARAMS = [
1503 65e183af Michael Hanselmann
    _PInstanceName,
1504 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1505 e1f23243 Michael Hanselmann
    _PEarlyRelease,
1506 d2fe2bfb René Nussbaumer
    _PIgnoreIpolicy,
1507 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.REPLACE_MODES),
1508 45d4c81c Michael Hanselmann
     "Replacement mode"),
1509 2c9fa1ff Iustin Pop
    ("disks", ht.EmptyList, ht.TListOf(ht.TNonNegativeInt),
1510 45d4c81c Michael Hanselmann
     "Disk indexes"),
1511 45d4c81c Michael Hanselmann
    ("remote_node", None, ht.TMaybeString, "New secondary node"),
1512 1c3231aa Thomas Thrainer
    ("remote_node_uuid", None, ht.TMaybeString, "New secondary node UUID"),
1513 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding new secondary node"),
1514 4f05fd3b Iustin Pop
    ]
1515 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1516 a8083063 Iustin Pop
1517 a8083063 Iustin Pop
1518 019dbee1 Iustin Pop
class OpInstanceFailover(OpCode):
1519 a8083063 Iustin Pop
  """Failover an instance."""
1520 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1521 65e183af Michael Hanselmann
  OP_PARAMS = [
1522 65e183af Michael Hanselmann
    _PInstanceName,
1523 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1524 65e183af Michael Hanselmann
    _PShutdownTimeout,
1525 45d4c81c Michael Hanselmann
    _PIgnoreConsistency,
1526 f8fa4175 Michael Hanselmann
    _PMigrationTargetNode,
1527 1c3231aa Thomas Thrainer
    _PMigrationTargetNodeUuid,
1528 b6aaf437 René Nussbaumer
    _PIgnoreIpolicy,
1529 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1530 89514061 Iustin Pop
                     " shared-storage instances"),
1531 17c3f802 Guido Trotter
    ]
1532 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1533 a8083063 Iustin Pop
1534 a8083063 Iustin Pop
1535 75c866c2 Iustin Pop
class OpInstanceMigrate(OpCode):
1536 53c776b5 Iustin Pop
  """Migrate an instance.
1537 53c776b5 Iustin Pop

1538 53c776b5 Iustin Pop
  This migrates (without shutting down an instance) to its secondary
1539 53c776b5 Iustin Pop
  node.
1540 53c776b5 Iustin Pop

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

1544 53c776b5 Iustin Pop
  """
1545 ee69c97f Iustin Pop
  OP_DSC_FIELD = "instance_name"
1546 65e183af Michael Hanselmann
  OP_PARAMS = [
1547 65e183af Michael Hanselmann
    _PInstanceName,
1548 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1549 65e183af Michael Hanselmann
    _PMigrationMode,
1550 65e183af Michael Hanselmann
    _PMigrationLive,
1551 f8fa4175 Michael Hanselmann
    _PMigrationTargetNode,
1552 1c3231aa Thomas Thrainer
    _PMigrationTargetNodeUuid,
1553 8c0b16f6 Guido Trotter
    _PAllowRuntimeChgs,
1554 3ed23330 René Nussbaumer
    _PIgnoreIpolicy,
1555 45d4c81c Michael Hanselmann
    ("cleanup", False, ht.TBool,
1556 45d4c81c Michael Hanselmann
     "Whether a previously failed migration should be cleaned up"),
1557 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1558 89514061 Iustin Pop
                     " shared-storage instances"),
1559 d5cafd31 René Nussbaumer
    ("allow_failover", False, ht.TBool,
1560 d5cafd31 René Nussbaumer
     "Whether we can fallback to failover if migration is not possible"),
1561 65e183af Michael Hanselmann
    ]
1562 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1563 53c776b5 Iustin Pop
1564 53c776b5 Iustin Pop
1565 0091b480 Iustin Pop
class OpInstanceMove(OpCode):
1566 313bcead Iustin Pop
  """Move an instance.
1567 313bcead Iustin Pop

1568 313bcead Iustin Pop
  This move (with shutting down an instance and data copying) to an
1569 313bcead Iustin Pop
  arbitrary node.
1570 313bcead Iustin Pop

1571 313bcead Iustin Pop
  @ivar instance_name: the name of the instance
1572 313bcead Iustin Pop
  @ivar target_node: the destination node
1573 313bcead Iustin Pop

1574 313bcead Iustin Pop
  """
1575 313bcead Iustin Pop
  OP_DSC_FIELD = "instance_name"
1576 65e183af Michael Hanselmann
  OP_PARAMS = [
1577 65e183af Michael Hanselmann
    _PInstanceName,
1578 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1579 65e183af Michael Hanselmann
    _PShutdownTimeout,
1580 92cf62e3 René Nussbaumer
    _PIgnoreIpolicy,
1581 45d4c81c Michael Hanselmann
    ("target_node", ht.NoDefault, ht.TNonEmptyString, "Target node"),
1582 1c3231aa Thomas Thrainer
    ("target_node_uuid", None, ht.TMaybeString, "Target node UUID"),
1583 bb851c63 Iustin Pop
    _PIgnoreConsistency,
1584 154b9580 Balazs Lecz
    ]
1585 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1586 313bcead Iustin Pop
1587 313bcead Iustin Pop
1588 cc0dec7b Iustin Pop
class OpInstanceConsole(OpCode):
1589 fdc267f4 Iustin Pop
  """Connect to an instance's console."""
1590 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1591 65e183af Michael Hanselmann
  OP_PARAMS = [
1592 3c286190 Dimitris Aragiorgis
    _PInstanceName,
1593 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1594 65e183af Michael Hanselmann
    ]
1595 c363310d René Nussbaumer
  OP_RESULT = ht.TDict
1596 a8083063 Iustin Pop
1597 a8083063 Iustin Pop
1598 83f5d475 Iustin Pop
class OpInstanceActivateDisks(OpCode):
1599 fdc267f4 Iustin Pop
  """Activate an instance's disks."""
1600 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1601 65e183af Michael Hanselmann
  OP_PARAMS = [
1602 65e183af Michael Hanselmann
    _PInstanceName,
1603 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1604 45d4c81c Michael Hanselmann
    ("ignore_size", False, ht.TBool, "Whether to ignore recorded size"),
1605 b69437c5 Iustin Pop
    _PWaitForSyncFalse,
1606 65e183af Michael Hanselmann
    ]
1607 c363310d René Nussbaumer
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
1608 c363310d René Nussbaumer
                                 ht.TItems([ht.TNonEmptyString,
1609 c363310d René Nussbaumer
                                            ht.TNonEmptyString,
1610 c363310d René Nussbaumer
                                            ht.TNonEmptyString])))
1611 a8083063 Iustin Pop
1612 a8083063 Iustin Pop
1613 e176281f Iustin Pop
class OpInstanceDeactivateDisks(OpCode):
1614 fdc267f4 Iustin Pop
  """Deactivate an instance's disks."""
1615 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1616 65e183af Michael Hanselmann
  OP_PARAMS = [
1617 c9c41373 Iustin Pop
    _PInstanceName,
1618 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1619 c9c41373 Iustin Pop
    _PForce,
1620 65e183af Michael Hanselmann
    ]
1621 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1622 a8083063 Iustin Pop
1623 a8083063 Iustin Pop
1624 6b273e78 Iustin Pop
class OpInstanceRecreateDisks(OpCode):
1625 a52978c7 Michael Hanselmann
  """Recreate an instance's disks."""
1626 735e1318 Michael Hanselmann
  _TDiskChanges = \
1627 735e1318 Michael Hanselmann
    ht.TAnd(ht.TIsLength(2),
1628 2c9fa1ff Iustin Pop
            ht.TItems([ht.Comment("Disk index")(ht.TNonNegativeInt),
1629 735e1318 Michael Hanselmann
                       ht.Comment("Parameters")(_TDiskParams)]))
1630 735e1318 Michael Hanselmann
1631 bd315bfa Iustin Pop
  OP_DSC_FIELD = "instance_name"
1632 65e183af Michael Hanselmann
  OP_PARAMS = [
1633 65e183af Michael Hanselmann
    _PInstanceName,
1634 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1635 735e1318 Michael Hanselmann
    ("disks", ht.EmptyList,
1636 2c9fa1ff Iustin Pop
     ht.TOr(ht.TListOf(ht.TNonNegativeInt), ht.TListOf(_TDiskChanges)),
1637 735e1318 Michael Hanselmann
     "List of disk indexes (deprecated) or a list of tuples containing a disk"
1638 735e1318 Michael Hanselmann
     " index and a possibly empty dictionary with disk parameter changes"),
1639 93384b8c Guido Trotter
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1640 93384b8c Guido Trotter
     "New instance nodes, if relocation is desired"),
1641 1c3231aa Thomas Thrainer
    ("node_uuids", None, ht.TMaybeListOf(ht.TNonEmptyString),
1642 1c3231aa Thomas Thrainer
     "New instance node UUIDs, if relocation is desired"),
1643 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding new nodes"),
1644 65e183af Michael Hanselmann
    ]
1645 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1646 bd315bfa Iustin Pop
1647 bd315bfa Iustin Pop
1648 f2af0bec Iustin Pop
class OpInstanceQuery(OpCode):
1649 a8083063 Iustin Pop
  """Compute the list of instances."""
1650 65e183af Michael Hanselmann
  OP_PARAMS = [
1651 65e183af Michael Hanselmann
    _POutputFields,
1652 45d4c81c Michael Hanselmann
    _PUseLocking,
1653 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1654 45d4c81c Michael Hanselmann
     "Empty list to query all instances, instance names otherwise"),
1655 65e183af Michael Hanselmann
    ]
1656 415feb2e René Nussbaumer
  OP_RESULT = _TOldQueryResult
1657 a8083063 Iustin Pop
1658 a8083063 Iustin Pop
1659 dc28c4e4 Iustin Pop
class OpInstanceQueryData(OpCode):
1660 a8083063 Iustin Pop
  """Compute the run-time status of instances."""
1661 65e183af Michael Hanselmann
  OP_PARAMS = [
1662 af7b6689 Michael Hanselmann
    _PUseLocking,
1663 af7b6689 Michael Hanselmann
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1664 af7b6689 Michael Hanselmann
     "Instance names"),
1665 af7b6689 Michael Hanselmann
    ("static", False, ht.TBool,
1666 af7b6689 Michael Hanselmann
     "Whether to only return configuration data without querying"
1667 af7b6689 Michael Hanselmann
     " nodes"),
1668 65e183af Michael Hanselmann
    ]
1669 415feb2e René Nussbaumer
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TDict)
1670 a8083063 Iustin Pop
1671 a8083063 Iustin Pop
1672 ddc1de7c Michael Hanselmann
def _TestInstSetParamsModList(fn):
1673 ddc1de7c Michael Hanselmann
  """Generates a check for modification lists.
1674 ddc1de7c Michael Hanselmann

1675 ddc1de7c Michael Hanselmann
  """
1676 e9c3d864 Michael Hanselmann
  # Old format
1677 e9c3d864 Michael Hanselmann
  # TODO: Remove in version 2.8 including support in LUInstanceSetParams
1678 e9c3d864 Michael Hanselmann
  old_mod_item_fn = \
1679 ddc1de7c Michael Hanselmann
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1680 2c9fa1ff Iustin Pop
      ht.TOr(ht.TElemOf(constants.DDMS_VALUES), ht.TNonNegativeInt),
1681 ddc1de7c Michael Hanselmann
      fn,
1682 ddc1de7c Michael Hanselmann
      ]))
1683 ddc1de7c Michael Hanselmann
1684 e9c3d864 Michael Hanselmann
  # New format, supporting adding/removing disks/NICs at arbitrary indices
1685 e9c3d864 Michael Hanselmann
  mod_item_fn = \
1686 e9c3d864 Michael Hanselmann
    ht.TAnd(ht.TIsLength(3), ht.TItems([
1687 e9c3d864 Michael Hanselmann
      ht.TElemOf(constants.DDMS_VALUES_WITH_MODIFY),
1688 b21d488b Christos Stavrakakis
      ht.Comment("Device index, can be negative, e.g. -1 for last disk")
1689 b21d488b Christos Stavrakakis
                 (ht.TOr(ht.TInt, ht.TString)),
1690 e9c3d864 Michael Hanselmann
      fn,
1691 e9c3d864 Michael Hanselmann
      ]))
1692 e9c3d864 Michael Hanselmann
1693 e9c3d864 Michael Hanselmann
  return ht.TOr(ht.Comment("Recommended")(ht.TListOf(mod_item_fn)),
1694 e9c3d864 Michael Hanselmann
                ht.Comment("Deprecated")(ht.TListOf(old_mod_item_fn)))
1695 ddc1de7c Michael Hanselmann
1696 ddc1de7c Michael Hanselmann
1697 9a3cc7ae Iustin Pop
class OpInstanceSetParams(OpCode):
1698 ddc1de7c Michael Hanselmann
  """Change the parameters of an instance.
1699 ddc1de7c Michael Hanselmann

1700 ddc1de7c Michael Hanselmann
  """
1701 a2aadb34 Michael Hanselmann
  TestNicModifications = _TestInstSetParamsModList(_TestNicDef)
1702 a2aadb34 Michael Hanselmann
  TestDiskModifications = _TestInstSetParamsModList(_TDiskParams)
1703 ddc1de7c Michael Hanselmann
1704 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1705 65e183af Michael Hanselmann
  OP_PARAMS = [
1706 65e183af Michael Hanselmann
    _PInstanceName,
1707 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1708 65e183af Michael Hanselmann
    _PForce,
1709 45d4c81c Michael Hanselmann
    _PForceVariant,
1710 1559e1e7 René Nussbaumer
    _PIgnoreIpolicy,
1711 a2aadb34 Michael Hanselmann
    ("nics", ht.EmptyList, TestNicModifications,
1712 b21d488b Christos Stavrakakis
     "List of NIC changes: each item is of the form"
1713 b21d488b Christos Stavrakakis
     " ``(op, identifier, settings)``, ``op`` is one of ``%s``, ``%s`` or"
1714 b21d488b Christos Stavrakakis
     " ``%s``, ``identifier`` can be a zero-based index number (or -1 to refer"
1715 b21d488b Christos Stavrakakis
     " to the last position), the NIC's UUID of the NIC's name; a"
1716 0fb66bb1 Iustin Pop
     " deprecated version of this parameter used the form ``(op, settings)``,"
1717 0fb66bb1 Iustin Pop
     " where ``op`` can be ``%s`` to add a new NIC with the specified"
1718 0fb66bb1 Iustin Pop
     " settings, ``%s`` to remove the last NIC or a number to modify the"
1719 0fb66bb1 Iustin Pop
     " settings of the NIC with that index" %
1720 e9c3d864 Michael Hanselmann
     (constants.DDM_ADD, constants.DDM_MODIFY, constants.DDM_REMOVE,
1721 e9c3d864 Michael Hanselmann
      constants.DDM_ADD, constants.DDM_REMOVE)),
1722 a2aadb34 Michael Hanselmann
    ("disks", ht.EmptyList, TestDiskModifications,
1723 0fb66bb1 Iustin Pop
     "List of disk changes; see ``nics``"),
1724 45d4c81c Michael Hanselmann
    ("beparams", ht.EmptyDict, ht.TDict, "Per-instance backend parameters"),
1725 2c9fa1ff Iustin Pop
    ("runtime_mem", None, ht.TMaybePositiveInt, "New runtime memory"),
1726 45d4c81c Michael Hanselmann
    ("hvparams", ht.EmptyDict, ht.TDict,
1727 45d4c81c Michael Hanselmann
     "Per-instance hypervisor parameters, hypervisor-dependent"),
1728 fd9f58fd Iustin Pop
    ("disk_template", None, ht.TMaybe(_BuildDiskTemplateCheck(False)),
1729 45d4c81c Michael Hanselmann
     "Disk template for instance"),
1730 d2204b1a Klaus Aehlig
    ("pnode", None, ht.TMaybeString, "New primary node"),
1731 1c3231aa Thomas Thrainer
    ("pnode_uuid", None, ht.TMaybeString, "New primary node UUID"),
1732 45d4c81c Michael Hanselmann
    ("remote_node", None, ht.TMaybeString,
1733 45d4c81c Michael Hanselmann
     "Secondary node (used when changing disk template)"),
1734 1c3231aa Thomas Thrainer
    ("remote_node_uuid", None, ht.TMaybeString,
1735 1c3231aa Thomas Thrainer
     "Secondary node UUID (used when changing disk template)"),
1736 45d4c81c Michael Hanselmann
    ("os_name", None, ht.TMaybeString,
1737 0fb66bb1 Iustin Pop
     "Change the instance's OS without reinstalling the instance"),
1738 45d4c81c Michael Hanselmann
    ("osparams", None, ht.TMaybeDict, "Per-instance OS parameters"),
1739 93384b8c Guido Trotter
    ("wait_for_sync", True, ht.TBool,
1740 93384b8c Guido Trotter
     "Whether to wait for the disk to synchronize, when changing template"),
1741 3016bc1f Michael Hanselmann
    ("offline", None, ht.TMaybeBool, "Whether to mark instance as offline"),
1742 eaa4c57c Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool, "Check for conflicting IPs"),
1743 274c7cab Dimitris Aragiorgis
    ("hotplug", None, ht.TMaybeBool, "Whether to hotplug devices"),
1744 973d7867 Iustin Pop
    ]
1745 1456df62 Michael Hanselmann
  OP_RESULT = _TSetParamsResult
1746 a8083063 Iustin Pop
1747 a8083063 Iustin Pop
1748 60472d29 Iustin Pop
class OpInstanceGrowDisk(OpCode):
1749 8729e0d7 Iustin Pop
  """Grow a disk of an instance."""
1750 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1751 65e183af Michael Hanselmann
  OP_PARAMS = [
1752 65e183af Michael Hanselmann
    _PInstanceName,
1753 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1754 45d4c81c Michael Hanselmann
    _PWaitForSync,
1755 45d4c81c Michael Hanselmann
    ("disk", ht.NoDefault, ht.TInt, "Disk index"),
1756 2c9fa1ff Iustin Pop
    ("amount", ht.NoDefault, ht.TNonNegativeInt,
1757 45d4c81c Michael Hanselmann
     "Amount of disk space to add (megabytes)"),
1758 e7f99087 Iustin Pop
    ("absolute", False, ht.TBool,
1759 e7f99087 Iustin Pop
     "Whether the amount parameter is an absolute target or a relative one"),
1760 4f05fd3b Iustin Pop
    ]
1761 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1762 8729e0d7 Iustin Pop
1763 8729e0d7 Iustin Pop
1764 1aef3df8 Michael Hanselmann
class OpInstanceChangeGroup(OpCode):
1765 1aef3df8 Michael Hanselmann
  """Moves an instance to another node group."""
1766 1aef3df8 Michael Hanselmann
  OP_DSC_FIELD = "instance_name"
1767 1aef3df8 Michael Hanselmann
  OP_PARAMS = [
1768 1aef3df8 Michael Hanselmann
    _PInstanceName,
1769 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1770 1aef3df8 Michael Hanselmann
    _PEarlyRelease,
1771 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for computing solution"),
1772 911ee606 Michael Hanselmann
    _PTargetGroups,
1773 1aef3df8 Michael Hanselmann
    ]
1774 1456df62 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
1775 1aef3df8 Michael Hanselmann
1776 1aef3df8 Michael Hanselmann
1777 70a6a926 Adeodato Simo
# Node group opcodes
1778 70a6a926 Adeodato Simo
1779 fabf1731 Iustin Pop
class OpGroupAdd(OpCode):
1780 b1ee5610 Adeodato Simo
  """Add a node group to the cluster."""
1781 b1ee5610 Adeodato Simo
  OP_DSC_FIELD = "group_name"
1782 65e183af Michael Hanselmann
  OP_PARAMS = [
1783 65e183af Michael Hanselmann
    _PGroupName,
1784 45d4c81c Michael Hanselmann
    _PNodeGroupAllocPolicy,
1785 45d4c81c Michael Hanselmann
    _PGroupNodeParams,
1786 bc5d0215 Andrea Spadaccini
    _PDiskParams,
1787 e4c03256 René Nussbaumer
    _PHvState,
1788 e4c03256 René Nussbaumer
    _PDiskState,
1789 1b01390b René Nussbaumer
    ("ipolicy", None, ht.TMaybeDict,
1790 1b01390b René Nussbaumer
     "Group-wide :ref:`instance policy <rapi-ipolicy>` specs"),
1791 483be60d Adeodato Simo
    ]
1792 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1793 b1ee5610 Adeodato Simo
1794 b1ee5610 Adeodato Simo
1795 934704ae Iustin Pop
class OpGroupAssignNodes(OpCode):
1796 96276ae7 Adeodato Simo
  """Assign nodes to a node group."""
1797 96276ae7 Adeodato Simo
  OP_DSC_FIELD = "group_name"
1798 96276ae7 Adeodato Simo
  OP_PARAMS = [
1799 96276ae7 Adeodato Simo
    _PGroupName,
1800 96276ae7 Adeodato Simo
    _PForce,
1801 45d4c81c Michael Hanselmann
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1802 45d4c81c Michael Hanselmann
     "List of nodes to assign"),
1803 1c3231aa Thomas Thrainer
    ("node_uuids", None, ht.TMaybeListOf(ht.TNonEmptyString),
1804 1c3231aa Thomas Thrainer
     "List of node UUIDs to assign"),
1805 96276ae7 Adeodato Simo
    ]
1806 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1807 96276ae7 Adeodato Simo
1808 96276ae7 Adeodato Simo
1809 d4d654bd Iustin Pop
class OpGroupQuery(OpCode):
1810 70a6a926 Adeodato Simo
  """Compute the list of node groups."""
1811 65e183af Michael Hanselmann
  OP_PARAMS = [
1812 65e183af Michael Hanselmann
    _POutputFields,
1813 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1814 45d4c81c Michael Hanselmann
     "Empty list to query all groups, group names otherwise"),
1815 65e183af Michael Hanselmann
    ]
1816 415feb2e René Nussbaumer
  OP_RESULT = _TOldQueryResult
1817 70a6a926 Adeodato Simo
1818 70a6a926 Adeodato Simo
1819 7cbf74f0 Iustin Pop
class OpGroupSetParams(OpCode):
1820 4da7909a Adeodato Simo
  """Change the parameters of a node group."""
1821 4da7909a Adeodato Simo
  OP_DSC_FIELD = "group_name"
1822 65e183af Michael Hanselmann
  OP_PARAMS = [
1823 65e183af Michael Hanselmann
    _PGroupName,
1824 45d4c81c Michael Hanselmann
    _PNodeGroupAllocPolicy,
1825 45d4c81c Michael Hanselmann
    _PGroupNodeParams,
1826 bc5d0215 Andrea Spadaccini
    _PDiskParams,
1827 a8282327 René Nussbaumer
    _PHvState,
1828 a8282327 René Nussbaumer
    _PDiskState,
1829 fb644e77 Agata Murawska
    ("ipolicy", None, ht.TMaybeDict, "Group-wide instance policy specs"),
1830 4da7909a Adeodato Simo
    ]
1831 1456df62 Michael Hanselmann
  OP_RESULT = _TSetParamsResult
1832 4da7909a Adeodato Simo
1833 4da7909a Adeodato Simo
1834 4d1baa51 Iustin Pop
class OpGroupRemove(OpCode):
1835 94bd652a Adeodato Simo
  """Remove a node group from the cluster."""
1836 94bd652a Adeodato Simo
  OP_DSC_FIELD = "group_name"
1837 65e183af Michael Hanselmann
  OP_PARAMS = [
1838 65e183af Michael Hanselmann
    _PGroupName,
1839 65e183af Michael Hanselmann
    ]
1840 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1841 94bd652a Adeodato Simo
1842 94bd652a Adeodato Simo
1843 a8173e82 Iustin Pop
class OpGroupRename(OpCode):
1844 4fe5cf90 Adeodato Simo
  """Rename a node group in the cluster."""
1845 65e183af Michael Hanselmann
  OP_PARAMS = [
1846 12da663a Michael Hanselmann
    _PGroupName,
1847 12da663a Michael Hanselmann
    ("new_name", ht.NoDefault, ht.TNonEmptyString, "New group name"),
1848 65e183af Michael Hanselmann
    ]
1849 1456df62 Michael Hanselmann
  OP_RESULT = ht.Comment("New group name")(ht.TNonEmptyString)
1850 4fe5cf90 Adeodato Simo
1851 4fe5cf90 Adeodato Simo
1852 08f8c82c Michael Hanselmann
class OpGroupEvacuate(OpCode):
1853 08f8c82c Michael Hanselmann
  """Evacuate a node group in the cluster."""
1854 08f8c82c Michael Hanselmann
  OP_DSC_FIELD = "group_name"
1855 08f8c82c Michael Hanselmann
  OP_PARAMS = [
1856 08f8c82c Michael Hanselmann
    _PGroupName,
1857 08f8c82c Michael Hanselmann
    _PEarlyRelease,
1858 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for computing solution"),
1859 911ee606 Michael Hanselmann
    _PTargetGroups,
1860 08f8c82c Michael Hanselmann
    ]
1861 1456df62 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
1862 08f8c82c Michael Hanselmann
1863 08f8c82c Michael Hanselmann
1864 a8083063 Iustin Pop
# OS opcodes
1865 da2d02e7 Iustin Pop
class OpOsDiagnose(OpCode):
1866 a8083063 Iustin Pop
  """Compute the list of guest operating systems."""
1867 65e183af Michael Hanselmann
  OP_PARAMS = [
1868 65e183af Michael Hanselmann
    _POutputFields,
1869 45d4c81c Michael Hanselmann
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1870 45d4c81c Michael Hanselmann
     "Which operating systems to diagnose"),
1871 65e183af Michael Hanselmann
    ]
1872 415feb2e René Nussbaumer
  OP_RESULT = _TOldQueryResult
1873 a8083063 Iustin Pop
1874 7c0d6283 Michael Hanselmann
1875 b954f097 Constantinos Venetsanopoulos
# ExtStorage opcodes
1876 b954f097 Constantinos Venetsanopoulos
class OpExtStorageDiagnose(OpCode):
1877 b954f097 Constantinos Venetsanopoulos
  """Compute the list of external storage providers."""
1878 b954f097 Constantinos Venetsanopoulos
  OP_PARAMS = [
1879 b954f097 Constantinos Venetsanopoulos
    _POutputFields,
1880 b954f097 Constantinos Venetsanopoulos
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1881 b954f097 Constantinos Venetsanopoulos
     "Which ExtStorage Provider to diagnose"),
1882 b954f097 Constantinos Venetsanopoulos
    ]
1883 b954f097 Constantinos Venetsanopoulos
  OP_RESULT = _TOldQueryResult
1884 b954f097 Constantinos Venetsanopoulos
1885 b954f097 Constantinos Venetsanopoulos
1886 a8083063 Iustin Pop
# Exports opcodes
1887 7ca2d4d8 Iustin Pop
class OpBackupQuery(OpCode):
1888 a8083063 Iustin Pop
  """Compute the list of exported images."""
1889 65e183af Michael Hanselmann
  OP_PARAMS = [
1890 45d4c81c Michael Hanselmann
    _PUseLocking,
1891 45d4c81c Michael Hanselmann
    ("nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
1892 45d4c81c Michael Hanselmann
     "Empty list to query all nodes, node names otherwise"),
1893 65e183af Michael Hanselmann
    ]
1894 202fb4e0 Michael Hanselmann
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString,
1895 202fb4e0 Michael Hanselmann
                         ht.TOr(ht.Comment("False on error")(ht.TBool),
1896 202fb4e0 Michael Hanselmann
                                ht.TListOf(ht.TNonEmptyString)))
1897 a8083063 Iustin Pop
1898 7c0d6283 Michael Hanselmann
1899 71910715 Iustin Pop
class OpBackupPrepare(OpCode):
1900 1410fa8d Michael Hanselmann
  """Prepares an instance export.
1901 1410fa8d Michael Hanselmann

1902 1410fa8d Michael Hanselmann
  @ivar instance_name: Instance name
1903 1410fa8d Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1904 1410fa8d Michael Hanselmann

1905 1410fa8d Michael Hanselmann
  """
1906 1410fa8d Michael Hanselmann
  OP_DSC_FIELD = "instance_name"
1907 65e183af Michael Hanselmann
  OP_PARAMS = [
1908 65e183af Michael Hanselmann
    _PInstanceName,
1909 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1910 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1911 45d4c81c Michael Hanselmann
     "Export mode"),
1912 1410fa8d Michael Hanselmann
    ]
1913 fd9f58fd Iustin Pop
  OP_RESULT = ht.TMaybeDict
1914 1410fa8d Michael Hanselmann
1915 1410fa8d Michael Hanselmann
1916 4ff922a2 Iustin Pop
class OpBackupExport(OpCode):
1917 4a96f1d1 Michael Hanselmann
  """Export an instance.
1918 4a96f1d1 Michael Hanselmann

1919 398e9066 Iustin Pop
  For local exports, the export destination is the node name. For
1920 398e9066 Iustin Pop
  remote exports, the export destination is a list of tuples, each
1921 398e9066 Iustin Pop
  consisting of hostname/IP address, port, magic, HMAC and HMAC
1922 398e9066 Iustin Pop
  salt. The HMAC is calculated using the cluster domain secret over
1923 398e9066 Iustin Pop
  the value "${index}:${hostname}:${port}". The destination X509 CA
1924 398e9066 Iustin Pop
  must be a signed certificate.
1925 4a96f1d1 Michael Hanselmann

1926 4a96f1d1 Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1927 4a96f1d1 Michael Hanselmann
  @ivar target_node: Export destination
1928 4a96f1d1 Michael Hanselmann
  @ivar x509_key_name: X509 key to use (remote export only)
1929 4a96f1d1 Michael Hanselmann
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1930 4a96f1d1 Michael Hanselmann
                             only)
1931 4a96f1d1 Michael Hanselmann

1932 4a96f1d1 Michael Hanselmann
  """
1933 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1934 65e183af Michael Hanselmann
  OP_PARAMS = [
1935 65e183af Michael Hanselmann
    _PInstanceName,
1936 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1937 65e183af Michael Hanselmann
    _PShutdownTimeout,
1938 4a96f1d1 Michael Hanselmann
    # TODO: Rename target_node as it changes meaning for different export modes
1939 4a96f1d1 Michael Hanselmann
    # (e.g. "destination")
1940 45d4c81c Michael Hanselmann
    ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1941 45d4c81c Michael Hanselmann
     "Destination information, depends on export mode"),
1942 1c3231aa Thomas Thrainer
    ("target_node_uuid", None, ht.TMaybeString,
1943 1c3231aa Thomas Thrainer
     "Target node UUID (if local export)"),
1944 45d4c81c Michael Hanselmann
    ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1945 45d4c81c Michael Hanselmann
    ("remove_instance", False, ht.TBool,
1946 45d4c81c Michael Hanselmann
     "Whether to remove instance after export"),
1947 45d4c81c Michael Hanselmann
    ("ignore_remove_failures", False, ht.TBool,
1948 45d4c81c Michael Hanselmann
     "Whether to ignore failures while removing instances"),
1949 45d4c81c Michael Hanselmann
    ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1950 45d4c81c Michael Hanselmann
     "Export mode"),
1951 fd9f58fd Iustin Pop
    ("x509_key_name", None, ht.TMaybe(ht.TList),
1952 45d4c81c Michael Hanselmann
     "Name of X509 key (remote export only)"),
1953 45d4c81c Michael Hanselmann
    ("destination_x509_ca", None, ht.TMaybeString,
1954 45d4c81c Michael Hanselmann
     "Destination X509 CA (remote export only)"),
1955 17c3f802 Guido Trotter
    ]
1956 202fb4e0 Michael Hanselmann
  OP_RESULT = \
1957 202fb4e0 Michael Hanselmann
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1958 202fb4e0 Michael Hanselmann
      ht.Comment("Finalizing status")(ht.TBool),
1959 202fb4e0 Michael Hanselmann
      ht.Comment("Status for every exported disk")(ht.TListOf(ht.TBool)),
1960 202fb4e0 Michael Hanselmann
      ]))
1961 5c947f38 Iustin Pop
1962 0a7bed64 Michael Hanselmann
1963 ca5890ad Iustin Pop
class OpBackupRemove(OpCode):
1964 9ac99fda Guido Trotter
  """Remove an instance's export."""
1965 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1966 65e183af Michael Hanselmann
  OP_PARAMS = [
1967 65e183af Michael Hanselmann
    _PInstanceName,
1968 da4a52a3 Thomas Thrainer
    _PInstanceUuid,
1969 65e183af Michael Hanselmann
    ]
1970 202fb4e0 Michael Hanselmann
  OP_RESULT = ht.TNone
1971 5c947f38 Iustin Pop
1972 0a7bed64 Michael Hanselmann
1973 5c947f38 Iustin Pop
# Tags opcodes
1974 c6afb1ca Iustin Pop
class OpTagsGet(OpCode):
1975 5c947f38 Iustin Pop
  """Returns the tags of the given object."""
1976 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
1977 65e183af Michael Hanselmann
  OP_PARAMS = [
1978 65e183af Michael Hanselmann
    _PTagKind,
1979 cfdf561d Michael Hanselmann
    # Not using _PUseLocking as the default is different for historical reasons
1980 cfdf561d Michael Hanselmann
    ("use_locking", True, ht.TBool, "Whether to use synchronization"),
1981 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1982 971b3a98 Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString,
1983 971b3a98 Michael Hanselmann
     "Name of object to retrieve tags from"),
1984 65e183af Michael Hanselmann
    ]
1985 48796673 Michael Hanselmann
  OP_RESULT = ht.TListOf(ht.TNonEmptyString)
1986 5c947f38 Iustin Pop
1987 5c947f38 Iustin Pop
1988 715462e7 Iustin Pop
class OpTagsSearch(OpCode):
1989 73415719 Iustin Pop
  """Searches the tags in the cluster for a given pattern."""
1990 60dd1473 Iustin Pop
  OP_DSC_FIELD = "pattern"
1991 65e183af Michael Hanselmann
  OP_PARAMS = [
1992 971b3a98 Michael Hanselmann
    ("pattern", ht.NoDefault, ht.TNonEmptyString,
1993 971b3a98 Michael Hanselmann
     "Search pattern (regular expression)"),
1994 65e183af Michael Hanselmann
    ]
1995 48796673 Michael Hanselmann
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(2), ht.TItems([
1996 48796673 Michael Hanselmann
    ht.TNonEmptyString,
1997 48796673 Michael Hanselmann
    ht.TNonEmptyString,
1998 48796673 Michael Hanselmann
    ])))
1999 73415719 Iustin Pop
2000 73415719 Iustin Pop
2001 d1602edc Iustin Pop
class OpTagsSet(OpCode):
2002 f27302fa Iustin Pop
  """Add a list of tags on a given object."""
2003 65e183af Michael Hanselmann
  OP_PARAMS = [
2004 65e183af Michael Hanselmann
    _PTagKind,
2005 65e183af Michael Hanselmann
    _PTags,
2006 bcd35e09 Dato Simó
    # Name is only meaningful for groups, nodes and instances
2007 971b3a98 Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString,
2008 971b3a98 Michael Hanselmann
     "Name of object where tag(s) should be added"),
2009 65e183af Michael Hanselmann
    ]
2010 48796673 Michael Hanselmann
  OP_RESULT = ht.TNone
2011 5c947f38 Iustin Pop
2012 5c947f38 Iustin Pop
2013 3f0ab95f Iustin Pop
class OpTagsDel(OpCode):
2014 f27302fa Iustin Pop
  """Remove a list of tags from a given object."""
2015 65e183af Michael Hanselmann
  OP_PARAMS = [
2016 65e183af Michael Hanselmann
    _PTagKind,
2017 65e183af Michael Hanselmann
    _PTags,
2018 bcd35e09 Dato Simó
    # Name is only meaningful for groups, nodes and instances
2019 971b3a98 Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString,
2020 971b3a98 Michael Hanselmann
     "Name of object where tag(s) should be deleted"),
2021 65e183af Michael Hanselmann
    ]
2022 48796673 Michael Hanselmann
  OP_RESULT = ht.TNone
2023 06009e27 Iustin Pop
2024 e687ec01 Michael Hanselmann
2025 06009e27 Iustin Pop
# Test opcodes
2026 06009e27 Iustin Pop
class OpTestDelay(OpCode):
2027 06009e27 Iustin Pop
  """Sleeps for a configured amount of time.
2028 06009e27 Iustin Pop

2029 06009e27 Iustin Pop
  This is used just for debugging and testing.
2030 06009e27 Iustin Pop

2031 06009e27 Iustin Pop
  Parameters:
2032 aeb6ba44 Dato Simó
    - duration: the time to sleep, in seconds
2033 06009e27 Iustin Pop
    - on_master: if true, sleep on the master
2034 06009e27 Iustin Pop
    - on_nodes: list of nodes in which to sleep
2035 06009e27 Iustin Pop

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

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

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

2046 06009e27 Iustin Pop
  """
2047 60dd1473 Iustin Pop
  OP_DSC_FIELD = "duration"
2048 65e183af Michael Hanselmann
  OP_PARAMS = [
2049 b795a775 Michael Hanselmann
    ("duration", ht.NoDefault, ht.TNumber, None),
2050 197b323b Michael Hanselmann
    ("on_master", True, ht.TBool, None),
2051 197b323b Michael Hanselmann
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
2052 1c3231aa Thomas Thrainer
    ("on_node_uuids", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2053 2c9fa1ff Iustin Pop
    ("repeat", 0, ht.TNonNegativeInt, None),
2054 65e183af Michael Hanselmann
    ]
2055 d61df03e Iustin Pop
2056 8bc17ebb Iustin Pop
  def OP_DSC_FORMATTER(self, value): # pylint: disable=C0103,R0201
2057 8bc17ebb Iustin Pop
    """Custom formatter for duration.
2058 8bc17ebb Iustin Pop

2059 8bc17ebb Iustin Pop
    """
2060 8bc17ebb Iustin Pop
    try:
2061 8bc17ebb Iustin Pop
      v = float(value)
2062 8bc17ebb Iustin Pop
    except TypeError:
2063 8bc17ebb Iustin Pop
      v = value
2064 8bc17ebb Iustin Pop
    return str(v)
2065 8bc17ebb Iustin Pop
2066 d61df03e Iustin Pop
2067 d61df03e Iustin Pop
class OpTestAllocator(OpCode):
2068 d61df03e Iustin Pop
  """Allocator framework testing.
2069 d61df03e Iustin Pop

2070 d61df03e Iustin Pop
  This opcode has two modes:
2071 d61df03e Iustin Pop
    - gather and return allocator input for a given mode (allocate new
2072 d61df03e Iustin Pop
      or replace secondary) and a given instance definition (direction
2073 d61df03e Iustin Pop
      'in')
2074 d61df03e Iustin Pop
    - run a selected allocator for a given operation (as above) and
2075 d61df03e Iustin Pop
      return the allocator output (direction 'out')
2076 d61df03e Iustin Pop

2077 d61df03e Iustin Pop
  """
2078 55b7e783 Iustin Pop
  OP_DSC_FIELD = "iallocator"
2079 65e183af Michael Hanselmann
  OP_PARAMS = [
2080 65e183af Michael Hanselmann
    ("direction", ht.NoDefault,
2081 197b323b Michael Hanselmann
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
2082 197b323b Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
2083 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
2084 ff8067cf Michael Hanselmann
    ("nics", ht.NoDefault,
2085 ff8067cf Michael Hanselmann
     ht.TMaybeListOf(ht.TDictOf(ht.TElemOf([constants.INIC_MAC,
2086 ff8067cf Michael Hanselmann
                                            constants.INIC_IP,
2087 ff8067cf Michael Hanselmann
                                            "bridge"]),
2088 fd9f58fd Iustin Pop
                                ht.TMaybeString)),
2089 ff8067cf Michael Hanselmann
     None),
2090 fd9f58fd Iustin Pop
    ("disks", ht.NoDefault, ht.TMaybe(ht.TList), None),
2091 197b323b Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString, None),
2092 55b7e783 Iustin Pop
    _PIAllocFromDesc(None),
2093 197b323b Michael Hanselmann
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
2094 fd9f58fd Iustin Pop
    ("memory", None, ht.TMaybe(ht.TNonNegativeInt), None),
2095 fd9f58fd Iustin Pop
    ("vcpus", None, ht.TMaybe(ht.TNonNegativeInt), None),
2096 197b323b Michael Hanselmann
    ("os", None, ht.TMaybeString, None),
2097 197b323b Michael Hanselmann
    ("disk_template", None, ht.TMaybeString, None),
2098 ff8067cf Michael Hanselmann
    ("instances", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2099 60152bbe Michael Hanselmann
    ("evac_mode", None,
2100 fd9f58fd Iustin Pop
     ht.TMaybe(ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
2101 ff8067cf Michael Hanselmann
    ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2102 2c9fa1ff Iustin Pop
    ("spindle_use", 1, ht.TNonNegativeInt, None),
2103 2c9fa1ff Iustin Pop
    ("count", 1, ht.TNonNegativeInt, None),
2104 d61df03e Iustin Pop
    ]
2105 363acb1e Iustin Pop
2106 76aef8fc Michael Hanselmann
2107 b469eb4d Iustin Pop
class OpTestJqueue(OpCode):
2108 e58f87a9 Michael Hanselmann
  """Utility opcode to test some aspects of the job queue.
2109 e58f87a9 Michael Hanselmann

2110 e58f87a9 Michael Hanselmann
  """
2111 65e183af Michael Hanselmann
  OP_PARAMS = [
2112 197b323b Michael Hanselmann
    ("notify_waitlock", False, ht.TBool, None),
2113 197b323b Michael Hanselmann
    ("notify_exec", False, ht.TBool, None),
2114 197b323b Michael Hanselmann
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
2115 197b323b Michael Hanselmann
    ("fail", False, ht.TBool, None),
2116 e58f87a9 Michael Hanselmann
    ]
2117 e58f87a9 Michael Hanselmann
2118 e58f87a9 Michael Hanselmann
2119 be760ba8 Michael Hanselmann
class OpTestDummy(OpCode):
2120 be760ba8 Michael Hanselmann
  """Utility opcode used by unittests.
2121 be760ba8 Michael Hanselmann

2122 be760ba8 Michael Hanselmann
  """
2123 65e183af Michael Hanselmann
  OP_PARAMS = [
2124 197b323b Michael Hanselmann
    ("result", ht.NoDefault, ht.NoType, None),
2125 197b323b Michael Hanselmann
    ("messages", ht.NoDefault, ht.NoType, None),
2126 197b323b Michael Hanselmann
    ("fail", ht.NoDefault, ht.NoType, None),
2127 6a373640 Michael Hanselmann
    ("submit_jobs", None, ht.NoType, None),
2128 be760ba8 Michael Hanselmann
    ]
2129 687c10d9 Iustin Pop
  WITH_LU = False
2130 be760ba8 Michael Hanselmann
2131 be760ba8 Michael Hanselmann
2132 eaa4c57c Dimitris Aragiorgis
# Network opcodes
2133 eaa4c57c Dimitris Aragiorgis
# Add a new network in the cluster
2134 eaa4c57c Dimitris Aragiorgis
class OpNetworkAdd(OpCode):
2135 eaa4c57c Dimitris Aragiorgis
  """Add an IP network to the cluster."""
2136 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2137 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2138 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2139 e1494c96 Iustin Pop
    ("network", ht.NoDefault, _TIpNetwork4, "IPv4 subnet"),
2140 e1494c96 Iustin Pop
    ("gateway", None, ht.TMaybe(_TIpAddress4), "IPv4 gateway"),
2141 e1494c96 Iustin Pop
    ("network6", None, ht.TMaybe(_TIpNetwork6), "IPv6 subnet"),
2142 e1494c96 Iustin Pop
    ("gateway6", None, ht.TMaybe(_TIpAddress6), "IPv6 gateway"),
2143 6e8091f9 Dimitris Aragiorgis
    ("mac_prefix", None, ht.TMaybeString,
2144 32e3d8b1 Michael Hanselmann
     "MAC address prefix that overrides cluster one"),
2145 e1494c96 Iustin Pop
    ("add_reserved_ips", None, _TMaybeAddr4List,
2146 32e3d8b1 Michael Hanselmann
     "Which IP addresses to reserve"),
2147 213076fe Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool,
2148 213076fe Dimitris Aragiorgis
     "Whether to check for conflicting IP addresses"),
2149 8140e24f Dimitris Aragiorgis
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Network tags"),
2150 eaa4c57c Dimitris Aragiorgis
    ]
2151 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2152 eaa4c57c Dimitris Aragiorgis
2153 3c286190 Dimitris Aragiorgis
2154 eaa4c57c Dimitris Aragiorgis
class OpNetworkRemove(OpCode):
2155 eaa4c57c Dimitris Aragiorgis
  """Remove an existing network from the cluster.
2156 eaa4c57c Dimitris Aragiorgis
     Must not be connected to any nodegroup.
2157 eaa4c57c Dimitris Aragiorgis

2158 eaa4c57c Dimitris Aragiorgis
  """
2159 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2160 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2161 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2162 eaa4c57c Dimitris Aragiorgis
    _PForce,
2163 eaa4c57c Dimitris Aragiorgis
    ]
2164 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2165 eaa4c57c Dimitris Aragiorgis
2166 3c286190 Dimitris Aragiorgis
2167 eaa4c57c Dimitris Aragiorgis
class OpNetworkSetParams(OpCode):
2168 eaa4c57c Dimitris Aragiorgis
  """Modify Network's parameters except for IPv4 subnet"""
2169 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2170 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2171 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2172 e055a2ab Dimitris Aragiorgis
    ("gateway", None, ht.TMaybeValueNone(_TIpAddress4), "IPv4 gateway"),
2173 e055a2ab Dimitris Aragiorgis
    ("network6", None, ht.TMaybeValueNone(_TIpNetwork6), "IPv6 subnet"),
2174 e055a2ab Dimitris Aragiorgis
    ("gateway6", None, ht.TMaybeValueNone(_TIpAddress6), "IPv6 gateway"),
2175 e055a2ab Dimitris Aragiorgis
    ("mac_prefix", None, ht.TMaybeValueNone(ht.TString),
2176 32e3d8b1 Michael Hanselmann
     "MAC address prefix that overrides cluster one"),
2177 e1494c96 Iustin Pop
    ("add_reserved_ips", None, _TMaybeAddr4List,
2178 32e3d8b1 Michael Hanselmann
     "Which external IP addresses to reserve"),
2179 e1494c96 Iustin Pop
    ("remove_reserved_ips", None, _TMaybeAddr4List,
2180 32e3d8b1 Michael Hanselmann
     "Which external IP addresses to release"),
2181 eaa4c57c Dimitris Aragiorgis
    ]
2182 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2183 eaa4c57c Dimitris Aragiorgis
2184 3c286190 Dimitris Aragiorgis
2185 eaa4c57c Dimitris Aragiorgis
class OpNetworkConnect(OpCode):
2186 eaa4c57c Dimitris Aragiorgis
  """Connect a Network to a specific Nodegroup with the defined netparams
2187 eaa4c57c Dimitris Aragiorgis
     (mode, link). Nics in this Network will inherit those params.
2188 eaa4c57c Dimitris Aragiorgis
     Produce errors if a NIC (that its not already assigned to a network)
2189 eaa4c57c Dimitris Aragiorgis
     has an IP that is contained in the Network this will produce error unless
2190 eaa4c57c Dimitris Aragiorgis
     --no-conflicts-check is passed.
2191 eaa4c57c Dimitris Aragiorgis

2192 eaa4c57c Dimitris Aragiorgis
  """
2193 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2194 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2195 eaa4c57c Dimitris Aragiorgis
    _PGroupName,
2196 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2197 6ef37395 Michael Hanselmann
    ("network_mode", ht.NoDefault, ht.TElemOf(constants.NIC_VALID_MODES),
2198 6ef37395 Michael Hanselmann
     "Connectivity mode"),
2199 e1494c96 Iustin Pop
    ("network_link", ht.NoDefault, ht.TString, "Connectivity link"),
2200 6e8091f9 Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool, "Whether to check for conflicting IPs"),
2201 eaa4c57c Dimitris Aragiorgis
    ]
2202 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2203 eaa4c57c Dimitris Aragiorgis
2204 3c286190 Dimitris Aragiorgis
2205 eaa4c57c Dimitris Aragiorgis
class OpNetworkDisconnect(OpCode):
2206 eaa4c57c Dimitris Aragiorgis
  """Disconnect a Network from a Nodegroup. Produce errors if NICs are
2207 eaa4c57c Dimitris Aragiorgis
     present in the Network unless --no-conficts-check option is passed.
2208 eaa4c57c Dimitris Aragiorgis

2209 eaa4c57c Dimitris Aragiorgis
  """
2210 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2211 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2212 eaa4c57c Dimitris Aragiorgis
    _PGroupName,
2213 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2214 eaa4c57c Dimitris Aragiorgis
    ]
2215 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2216 eaa4c57c Dimitris Aragiorgis
2217 3c286190 Dimitris Aragiorgis
2218 eaa4c57c Dimitris Aragiorgis
class OpNetworkQuery(OpCode):
2219 eaa4c57c Dimitris Aragiorgis
  """Compute the list of networks."""
2220 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2221 eaa4c57c Dimitris Aragiorgis
    _POutputFields,
2222 8d459129 Michael Hanselmann
    _PUseLocking,
2223 eaa4c57c Dimitris Aragiorgis
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
2224 eaa4c57c Dimitris Aragiorgis
     "Empty list to query all groups, group names otherwise"),
2225 eaa4c57c Dimitris Aragiorgis
    ]
2226 829cfbc5 Dimitris Aragiorgis
  OP_RESULT = _TOldQueryResult
2227 eaa4c57c Dimitris Aragiorgis
2228 eaa4c57c Dimitris Aragiorgis
2229 dbc96028 Michael Hanselmann
def _GetOpList():
2230 dbc96028 Michael Hanselmann
  """Returns list of all defined opcodes.
2231 dbc96028 Michael Hanselmann

2232 dbc96028 Michael Hanselmann
  Does not eliminate duplicates by C{OP_ID}.
2233 dbc96028 Michael Hanselmann

2234 dbc96028 Michael Hanselmann
  """
2235 dbc96028 Michael Hanselmann
  return [v for v in globals().values()
2236 dbc96028 Michael Hanselmann
          if (isinstance(v, type) and issubclass(v, OpCode) and
2237 687c10d9 Iustin Pop
              hasattr(v, "OP_ID") and v is not OpCode)]
2238 dbc96028 Michael Hanselmann
2239 dbc96028 Michael Hanselmann
2240 dbc96028 Michael Hanselmann
OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())