Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 7af7da68

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

251 ff0d18e6 Iustin Pop
  @type name: string
252 ff0d18e6 Iustin Pop
  @param name: the class name, as OpXxxYyy
253 d634b4e0 Michele Tartara
  @rtype: array of strings
254 d634b4e0 Michele Tartara
  @return: the components of the name
255 ff0d18e6 Iustin Pop

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

271 d634b4e0 Michele Tartara
  @type name: string
272 d634b4e0 Michele Tartara
  @param name: the class name, as OpXxxYyy
273 d634b4e0 Michele Tartara
  @rtype: string
274 d634b4e0 Michele Tartara
  @return: the name in the OP_XXXX_YYYY format
275 d634b4e0 Michele Tartara

276 d634b4e0 Michele Tartara
  """
277 d634b4e0 Michele Tartara
  if not name.startswith("Op"):
278 d634b4e0 Michele Tartara
    return None
279 d634b4e0 Michele Tartara
  return "_".join(n.upper() for n in _NameComponents(name))
280 d634b4e0 Michele Tartara
281 d634b4e0 Michele Tartara
282 d634b4e0 Michele Tartara
def NameToReasonSrc(name):
283 d634b4e0 Michele Tartara
  """Convert an opcode class name to a source string for the reason trail
284 d634b4e0 Michele Tartara

285 d634b4e0 Michele Tartara
  @type name: string
286 d634b4e0 Michele Tartara
  @param name: the class name, as OpXxxYyy
287 d634b4e0 Michele Tartara
  @rtype: string
288 d634b4e0 Michele Tartara
  @return: the name in the OP_XXXX_YYYY format
289 d634b4e0 Michele Tartara

290 d634b4e0 Michele Tartara
  """
291 d634b4e0 Michele Tartara
  if not name.startswith("Op"):
292 d634b4e0 Michele Tartara
    return None
293 d634b4e0 Michele Tartara
  return "%s:%s" % (constants.OPCODE_REASON_SRC_OPCODE,
294 d634b4e0 Michele Tartara
                    "_".join(n.lower() for n in _NameComponents(name)))
295 ff0d18e6 Iustin Pop
296 65e183af Michael Hanselmann
297 415feb2e René Nussbaumer
def _GenerateObjectTypeCheck(obj, fields_types):
298 415feb2e René Nussbaumer
  """Helper to generate type checks for objects.
299 415feb2e René Nussbaumer

300 415feb2e René Nussbaumer
  @param obj: The object to generate type checks
301 415feb2e René Nussbaumer
  @param fields_types: The fields and their types as a dict
302 415feb2e René Nussbaumer
  @return: A ht type check function
303 415feb2e René Nussbaumer

304 415feb2e René Nussbaumer
  """
305 415feb2e René Nussbaumer
  assert set(obj.GetAllSlots()) == set(fields_types.keys()), \
306 415feb2e René Nussbaumer
    "%s != %s" % (set(obj.GetAllSlots()), set(fields_types.keys()))
307 415feb2e René Nussbaumer
  return ht.TStrictDict(True, True, fields_types)
308 415feb2e René Nussbaumer
309 415feb2e René Nussbaumer
310 b02c6bdf Michael Hanselmann
_TQueryFieldDef = \
311 b02c6bdf Michael Hanselmann
  _GenerateObjectTypeCheck(objects.QueryFieldDefinition, {
312 b02c6bdf Michael Hanselmann
    "name": ht.TNonEmptyString,
313 b02c6bdf Michael Hanselmann
    "title": ht.TNonEmptyString,
314 b02c6bdf Michael Hanselmann
    "kind": ht.TElemOf(constants.QFT_ALL),
315 b02c6bdf Michael Hanselmann
    "doc": ht.TNonEmptyString,
316 b02c6bdf Michael Hanselmann
    })
317 415feb2e René Nussbaumer
318 415feb2e René Nussbaumer
319 65e183af Michael Hanselmann
def RequireFileStorage():
320 65e183af Michael Hanselmann
  """Checks that file storage is enabled.
321 65e183af Michael Hanselmann

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

325 65e183af Michael Hanselmann
  @raise errors.OpPrereqError: when file storage is disabled
326 65e183af Michael Hanselmann

327 65e183af Michael Hanselmann
  """
328 65e183af Michael Hanselmann
  if not constants.ENABLE_FILE_STORAGE:
329 65e183af Michael Hanselmann
    raise errors.OpPrereqError("File storage disabled at configure time",
330 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
331 65e183af Michael Hanselmann
332 65e183af Michael Hanselmann
333 4b97f902 Apollon Oikonomopoulos
def RequireSharedFileStorage():
334 4b97f902 Apollon Oikonomopoulos
  """Checks that shared file storage is enabled.
335 4b97f902 Apollon Oikonomopoulos

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

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

341 4b97f902 Apollon Oikonomopoulos
  """
342 4b97f902 Apollon Oikonomopoulos
  if not constants.ENABLE_SHARED_FILE_STORAGE:
343 4b97f902 Apollon Oikonomopoulos
    raise errors.OpPrereqError("Shared file storage disabled at"
344 4b97f902 Apollon Oikonomopoulos
                               " configure time", errors.ECODE_INVAL)
345 4b97f902 Apollon Oikonomopoulos
346 4b97f902 Apollon Oikonomopoulos
347 8c9ee749 Michael Hanselmann
@ht.WithDesc("CheckFileStorage")
348 8c9ee749 Michael Hanselmann
def _CheckFileStorage(value):
349 8c9ee749 Michael Hanselmann
  """Ensures file storage is enabled if used.
350 65e183af Michael Hanselmann

351 65e183af Michael Hanselmann
  """
352 8c9ee749 Michael Hanselmann
  if value == constants.DT_FILE:
353 65e183af Michael Hanselmann
    RequireFileStorage()
354 4b97f902 Apollon Oikonomopoulos
  elif value == constants.DT_SHARED_FILE:
355 4b97f902 Apollon Oikonomopoulos
    RequireSharedFileStorage()
356 65e183af Michael Hanselmann
  return True
357 65e183af Michael Hanselmann
358 65e183af Michael Hanselmann
359 45fe090b Michael Hanselmann
def _BuildDiskTemplateCheck(accept_none):
360 45fe090b Michael Hanselmann
  """Builds check for disk template.
361 45fe090b Michael Hanselmann

362 45fe090b Michael Hanselmann
  @type accept_none: bool
363 45fe090b Michael Hanselmann
  @param accept_none: whether to accept None as a correct value
364 45fe090b Michael Hanselmann
  @rtype: callable
365 45fe090b Michael Hanselmann

366 45fe090b Michael Hanselmann
  """
367 45fe090b Michael Hanselmann
  template_check = ht.TElemOf(constants.DISK_TEMPLATES)
368 45fe090b Michael Hanselmann
369 45fe090b Michael Hanselmann
  if accept_none:
370 fd9f58fd Iustin Pop
    template_check = ht.TMaybe(template_check)
371 45fe090b Michael Hanselmann
372 45fe090b Michael Hanselmann
  return ht.TAnd(template_check, _CheckFileStorage)
373 8c9ee749 Michael Hanselmann
374 8c9ee749 Michael Hanselmann
375 65e183af Michael Hanselmann
def _CheckStorageType(storage_type):
376 65e183af Michael Hanselmann
  """Ensure a given storage type is valid.
377 65e183af Michael Hanselmann

378 65e183af Michael Hanselmann
  """
379 65e183af Michael Hanselmann
  if storage_type not in constants.VALID_STORAGE_TYPES:
380 65e183af Michael Hanselmann
    raise errors.OpPrereqError("Unknown storage type: %s" % storage_type,
381 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
382 65e183af Michael Hanselmann
  if storage_type == constants.ST_FILE:
383 63a3d8f7 Michael Hanselmann
    # TODO: What about shared file storage?
384 65e183af Michael Hanselmann
    RequireFileStorage()
385 65e183af Michael Hanselmann
  return True
386 65e183af Michael Hanselmann
387 65e183af Michael Hanselmann
388 65e183af Michael Hanselmann
#: Storage type parameter
389 45d4c81c Michael Hanselmann
_PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType,
390 45d4c81c Michael Hanselmann
                 "Storage type")
391 65e183af Michael Hanselmann
392 3c286190 Dimitris Aragiorgis
393 16091a6e Michael Hanselmann
@ht.WithDesc("IPv4 network")
394 eaa4c57c Dimitris Aragiorgis
def _CheckCIDRNetNotation(value):
395 16091a6e Michael Hanselmann
  """Ensure a given CIDR notation type is valid.
396 eaa4c57c Dimitris Aragiorgis

397 eaa4c57c Dimitris Aragiorgis
  """
398 eaa4c57c Dimitris Aragiorgis
  try:
399 eaa4c57c Dimitris Aragiorgis
    ipaddr.IPv4Network(value)
400 eaa4c57c Dimitris Aragiorgis
  except ipaddr.AddressValueError:
401 eaa4c57c Dimitris Aragiorgis
    return False
402 eaa4c57c Dimitris Aragiorgis
  return True
403 eaa4c57c Dimitris Aragiorgis
404 3c286190 Dimitris Aragiorgis
405 16091a6e Michael Hanselmann
@ht.WithDesc("IPv4 address")
406 eaa4c57c Dimitris Aragiorgis
def _CheckCIDRAddrNotation(value):
407 16091a6e Michael Hanselmann
  """Ensure a given CIDR notation type is valid.
408 eaa4c57c Dimitris Aragiorgis

409 eaa4c57c Dimitris Aragiorgis
  """
410 eaa4c57c Dimitris Aragiorgis
  try:
411 eaa4c57c Dimitris Aragiorgis
    ipaddr.IPv4Address(value)
412 eaa4c57c Dimitris Aragiorgis
  except ipaddr.AddressValueError:
413 eaa4c57c Dimitris Aragiorgis
    return False
414 eaa4c57c Dimitris Aragiorgis
  return True
415 eaa4c57c Dimitris Aragiorgis
416 3c286190 Dimitris Aragiorgis
417 16091a6e Michael Hanselmann
@ht.WithDesc("IPv6 address")
418 eaa4c57c Dimitris Aragiorgis
def _CheckCIDR6AddrNotation(value):
419 16091a6e Michael Hanselmann
  """Ensure a given CIDR notation type is valid.
420 eaa4c57c Dimitris Aragiorgis

421 eaa4c57c Dimitris Aragiorgis
  """
422 eaa4c57c Dimitris Aragiorgis
  try:
423 eaa4c57c Dimitris Aragiorgis
    ipaddr.IPv6Address(value)
424 eaa4c57c Dimitris Aragiorgis
  except ipaddr.AddressValueError:
425 eaa4c57c Dimitris Aragiorgis
    return False
426 eaa4c57c Dimitris Aragiorgis
  return True
427 eaa4c57c Dimitris Aragiorgis
428 3c286190 Dimitris Aragiorgis
429 16091a6e Michael Hanselmann
@ht.WithDesc("IPv6 network")
430 eaa4c57c Dimitris Aragiorgis
def _CheckCIDR6NetNotation(value):
431 16091a6e Michael Hanselmann
  """Ensure a given CIDR notation type is valid.
432 eaa4c57c Dimitris Aragiorgis

433 eaa4c57c Dimitris Aragiorgis
  """
434 eaa4c57c Dimitris Aragiorgis
  try:
435 eaa4c57c Dimitris Aragiorgis
    ipaddr.IPv6Network(value)
436 eaa4c57c Dimitris Aragiorgis
  except ipaddr.AddressValueError:
437 eaa4c57c Dimitris Aragiorgis
    return False
438 eaa4c57c Dimitris Aragiorgis
  return True
439 65e183af Michael Hanselmann
440 3c286190 Dimitris Aragiorgis
441 e1494c96 Iustin Pop
_TIpAddress4 = ht.TAnd(ht.TString, _CheckCIDRAddrNotation)
442 e1494c96 Iustin Pop
_TIpAddress6 = ht.TAnd(ht.TString, _CheckCIDR6AddrNotation)
443 e1494c96 Iustin Pop
_TIpNetwork4 = ht.TAnd(ht.TString, _CheckCIDRNetNotation)
444 e1494c96 Iustin Pop
_TIpNetwork6 = ht.TAnd(ht.TString, _CheckCIDR6NetNotation)
445 e1494c96 Iustin Pop
_TMaybeAddr4List = ht.TMaybe(ht.TListOf(_TIpAddress4))
446 32e3d8b1 Michael Hanselmann
447 32e3d8b1 Michael Hanselmann
448 473d87a3 Iustin Pop
class _AutoOpParamSlots(outils.AutoSlots):
449 65e183af Michael Hanselmann
  """Meta class for opcode definitions.
450 65e183af Michael Hanselmann

451 65e183af Michael Hanselmann
  """
452 65e183af Michael Hanselmann
  def __new__(mcs, name, bases, attrs):
453 65e183af Michael Hanselmann
    """Called when a class should be created.
454 65e183af Michael Hanselmann

455 65e183af Michael Hanselmann
    @param mcs: The meta class
456 65e183af Michael Hanselmann
    @param name: Name of created class
457 65e183af Michael Hanselmann
    @param bases: Base classes
458 65e183af Michael Hanselmann
    @type attrs: dict
459 65e183af Michael Hanselmann
    @param attrs: Class attributes
460 65e183af Michael Hanselmann

461 65e183af Michael Hanselmann
    """
462 e89a9021 Iustin Pop
    assert "OP_ID" not in attrs, "Class '%s' defining OP_ID" % name
463 ff0d18e6 Iustin Pop
464 32683096 René Nussbaumer
    slots = mcs._GetSlots(attrs)
465 32683096 René Nussbaumer
    assert "OP_DSC_FIELD" not in attrs or attrs["OP_DSC_FIELD"] in slots, \
466 32683096 René Nussbaumer
      "Class '%s' uses unknown field in OP_DSC_FIELD" % name
467 8bc17ebb Iustin Pop
    assert ("OP_DSC_FORMATTER" not in attrs or
468 8bc17ebb Iustin Pop
            callable(attrs["OP_DSC_FORMATTER"])), \
469 8bc17ebb Iustin Pop
      ("Class '%s' uses non-callable in OP_DSC_FORMATTER (%s)" %
470 8bc17ebb Iustin Pop
       (name, type(attrs["OP_DSC_FORMATTER"])))
471 32683096 René Nussbaumer
472 e89a9021 Iustin Pop
    attrs["OP_ID"] = _NameToId(name)
473 65e183af Michael Hanselmann
474 473d87a3 Iustin Pop
    return outils.AutoSlots.__new__(mcs, name, bases, attrs)
475 32683096 René Nussbaumer
476 32683096 René Nussbaumer
  @classmethod
477 32683096 René Nussbaumer
  def _GetSlots(mcs, attrs):
478 32683096 René Nussbaumer
    """Build the slots out of OP_PARAMS.
479 32683096 René Nussbaumer

480 32683096 René Nussbaumer
    """
481 65e183af Michael Hanselmann
    # Always set OP_PARAMS to avoid duplicates in BaseOpCode.GetAllParams
482 65e183af Michael Hanselmann
    params = attrs.setdefault("OP_PARAMS", [])
483 65e183af Michael Hanselmann
484 65e183af Michael Hanselmann
    # Use parameter names as slots
485 32683096 René Nussbaumer
    return [pname for (pname, _, _, _) in params]
486 65e183af Michael Hanselmann
487 df458e0b Iustin Pop
488 473d87a3 Iustin Pop
class BaseOpCode(outils.ValidatedSlots):
489 df458e0b Iustin Pop
  """A simple serializable object.
490 df458e0b Iustin Pop

491 0e46916d Iustin Pop
  This object serves as a parent class for OpCode without any custom
492 0e46916d Iustin Pop
  field handling.
493 0e46916d Iustin Pop

494 df458e0b Iustin Pop
  """
495 b459a848 Andrea Spadaccini
  # pylint: disable=E1101
496 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
497 65e183af Michael Hanselmann
  __metaclass__ = _AutoOpParamSlots
498 65e183af Michael Hanselmann
499 df458e0b Iustin Pop
  def __getstate__(self):
500 a7399f66 Iustin Pop
    """Generic serializer.
501 a7399f66 Iustin Pop

502 a7399f66 Iustin Pop
    This method just returns the contents of the instance as a
503 a7399f66 Iustin Pop
    dictionary.
504 a7399f66 Iustin Pop

505 a7399f66 Iustin Pop
    @rtype:  C{dict}
506 a7399f66 Iustin Pop
    @return: the instance attributes and their values
507 a7399f66 Iustin Pop

508 a7399f66 Iustin Pop
    """
509 df458e0b Iustin Pop
    state = {}
510 32683096 René Nussbaumer
    for name in self.GetAllSlots():
511 df458e0b Iustin Pop
      if hasattr(self, name):
512 df458e0b Iustin Pop
        state[name] = getattr(self, name)
513 df458e0b Iustin Pop
    return state
514 df458e0b Iustin Pop
515 df458e0b Iustin Pop
  def __setstate__(self, state):
516 a7399f66 Iustin Pop
    """Generic unserializer.
517 a7399f66 Iustin Pop

518 a7399f66 Iustin Pop
    This method just restores from the serialized state the attributes
519 a7399f66 Iustin Pop
    of the current instance.
520 a7399f66 Iustin Pop

521 a7399f66 Iustin Pop
    @param state: the serialized opcode data
522 a7399f66 Iustin Pop
    @type state:  C{dict}
523 a7399f66 Iustin Pop

524 a7399f66 Iustin Pop
    """
525 df458e0b Iustin Pop
    if not isinstance(state, dict):
526 df458e0b Iustin Pop
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
527 df458e0b Iustin Pop
                       type(state))
528 df458e0b Iustin Pop
529 32683096 René Nussbaumer
    for name in self.GetAllSlots():
530 44db3a6f Iustin Pop
      if name not in state and hasattr(self, name):
531 df458e0b Iustin Pop
        delattr(self, name)
532 df458e0b Iustin Pop
533 df458e0b Iustin Pop
    for name in state:
534 df458e0b Iustin Pop
      setattr(self, name, state[name])
535 df458e0b Iustin Pop
536 adf385c7 Iustin Pop
  @classmethod
537 65e183af Michael Hanselmann
  def GetAllParams(cls):
538 65e183af Michael Hanselmann
    """Compute list of all parameters for an opcode.
539 65e183af Michael Hanselmann

540 65e183af Michael Hanselmann
    """
541 65e183af Michael Hanselmann
    slots = []
542 65e183af Michael Hanselmann
    for parent in cls.__mro__:
543 65e183af Michael Hanselmann
      slots.extend(getattr(parent, "OP_PARAMS", []))
544 65e183af Michael Hanselmann
    return slots
545 65e183af Michael Hanselmann
546 32683096 René Nussbaumer
  def Validate(self, set_defaults): # pylint: disable=W0221
547 1cbef6d8 Michael Hanselmann
    """Validate opcode parameters, optionally setting default values.
548 1cbef6d8 Michael Hanselmann

549 1cbef6d8 Michael Hanselmann
    @type set_defaults: bool
550 1cbef6d8 Michael Hanselmann
    @param set_defaults: Whether to set default values
551 1cbef6d8 Michael Hanselmann
    @raise errors.OpPrereqError: When a parameter value doesn't match
552 1cbef6d8 Michael Hanselmann
                                 requirements
553 1cbef6d8 Michael Hanselmann

554 1cbef6d8 Michael Hanselmann
    """
555 197b323b Michael Hanselmann
    for (attr_name, default, test, _) in self.GetAllParams():
556 1cbef6d8 Michael Hanselmann
      assert test == ht.NoType or callable(test)
557 1cbef6d8 Michael Hanselmann
558 1cbef6d8 Michael Hanselmann
      if not hasattr(self, attr_name):
559 1cbef6d8 Michael Hanselmann
        if default == ht.NoDefault:
560 1cbef6d8 Michael Hanselmann
          raise errors.OpPrereqError("Required parameter '%s.%s' missing" %
561 1cbef6d8 Michael Hanselmann
                                     (self.OP_ID, attr_name),
562 1cbef6d8 Michael Hanselmann
                                     errors.ECODE_INVAL)
563 1cbef6d8 Michael Hanselmann
        elif set_defaults:
564 1cbef6d8 Michael Hanselmann
          if callable(default):
565 1cbef6d8 Michael Hanselmann
            dval = default()
566 1cbef6d8 Michael Hanselmann
          else:
567 1cbef6d8 Michael Hanselmann
            dval = default
568 1cbef6d8 Michael Hanselmann
          setattr(self, attr_name, dval)
569 1cbef6d8 Michael Hanselmann
570 1cbef6d8 Michael Hanselmann
      if test == ht.NoType:
571 1cbef6d8 Michael Hanselmann
        # no tests here
572 1cbef6d8 Michael Hanselmann
        continue
573 1cbef6d8 Michael Hanselmann
574 1cbef6d8 Michael Hanselmann
      if set_defaults or hasattr(self, attr_name):
575 1cbef6d8 Michael Hanselmann
        attr_val = getattr(self, attr_name)
576 1cbef6d8 Michael Hanselmann
        if not test(attr_val):
577 e1ebbfcf Iustin Pop
          logging.error("OpCode %s, parameter %s, has invalid type %s/value"
578 e1ebbfcf Iustin Pop
                        " '%s' expecting type %s",
579 68b2e985 René Nussbaumer
                        self.OP_ID, attr_name, type(attr_val), attr_val, test)
580 1cbef6d8 Michael Hanselmann
          raise errors.OpPrereqError("Parameter '%s.%s' fails validation" %
581 1cbef6d8 Michael Hanselmann
                                     (self.OP_ID, attr_name),
582 1cbef6d8 Michael Hanselmann
                                     errors.ECODE_INVAL)
583 1cbef6d8 Michael Hanselmann
584 df458e0b Iustin Pop
585 b247c6fc Michael Hanselmann
def _BuildJobDepCheck(relative):
586 b247c6fc Michael Hanselmann
  """Builds check for job dependencies (L{DEPEND_ATTR}).
587 b247c6fc Michael Hanselmann

588 b247c6fc Michael Hanselmann
  @type relative: bool
589 b247c6fc Michael Hanselmann
  @param relative: Whether to accept relative job IDs (negative)
590 b247c6fc Michael Hanselmann
  @rtype: callable
591 b247c6fc Michael Hanselmann

592 b247c6fc Michael Hanselmann
  """
593 b247c6fc Michael Hanselmann
  if relative:
594 b247c6fc Michael Hanselmann
    job_id = ht.TOr(ht.TJobId, ht.TRelativeJobId)
595 b247c6fc Michael Hanselmann
  else:
596 b247c6fc Michael Hanselmann
    job_id = ht.TJobId
597 b247c6fc Michael Hanselmann
598 b247c6fc Michael Hanselmann
  job_dep = \
599 dd076c21 Iustin Pop
    ht.TAnd(ht.TOr(ht.TList, ht.TTuple),
600 dd076c21 Iustin Pop
            ht.TIsLength(2),
601 b247c6fc Michael Hanselmann
            ht.TItems([job_id,
602 b247c6fc Michael Hanselmann
                       ht.TListOf(ht.TElemOf(constants.JOBS_FINALIZED))]))
603 b247c6fc Michael Hanselmann
604 ff8067cf Michael Hanselmann
  return ht.TMaybeListOf(job_dep)
605 b247c6fc Michael Hanselmann
606 b247c6fc Michael Hanselmann
607 b247c6fc Michael Hanselmann
TNoRelativeJobDependencies = _BuildJobDepCheck(False)
608 b247c6fc Michael Hanselmann
609 1ce03fb1 Michael Hanselmann
#: List of submission status and job ID as returned by C{SubmitManyJobs}
610 1456df62 Michael Hanselmann
_TJobIdListItem = \
611 1456df62 Michael Hanselmann
  ht.TAnd(ht.TIsLength(2),
612 1456df62 Michael Hanselmann
          ht.TItems([ht.Comment("success")(ht.TBool),
613 1456df62 Michael Hanselmann
                     ht.Comment("Job ID if successful, error message"
614 1456df62 Michael Hanselmann
                                " otherwise")(ht.TOr(ht.TString,
615 1456df62 Michael Hanselmann
                                                     ht.TJobId))]))
616 1456df62 Michael Hanselmann
TJobIdList = ht.TListOf(_TJobIdListItem)
617 1ce03fb1 Michael Hanselmann
618 f7686867 Michael Hanselmann
#: Result containing only list of submitted jobs
619 f7686867 Michael Hanselmann
TJobIdListOnly = ht.TStrictDict(True, True, {
620 1456df62 Michael Hanselmann
  constants.JOB_IDS_KEY: ht.Comment("List of submitted jobs")(TJobIdList),
621 f7686867 Michael Hanselmann
  })
622 f7686867 Michael Hanselmann
623 b247c6fc Michael Hanselmann
624 0e46916d Iustin Pop
class OpCode(BaseOpCode):
625 a7399f66 Iustin Pop
  """Abstract OpCode.
626 a7399f66 Iustin Pop

627 a7399f66 Iustin Pop
  This is the root of the actual OpCode hierarchy. All clases derived
628 a7399f66 Iustin Pop
  from this class should override OP_ID.
629 a7399f66 Iustin Pop

630 a7399f66 Iustin Pop
  @cvar OP_ID: The ID of this opcode. This should be unique amongst all
631 20777413 Iustin Pop
               children of this class.
632 bde8f481 Adeodato Simo
  @cvar OP_DSC_FIELD: The name of a field whose value will be included in the
633 bde8f481 Adeodato Simo
                      string returned by Summary(); see the docstring of that
634 bde8f481 Adeodato Simo
                      method for details).
635 8bc17ebb Iustin Pop
  @cvar OP_DSC_FORMATTER: A callable that should format the OP_DSC_FIELD; if
636 8bc17ebb Iustin Pop
                          not present, then the field will be simply converted
637 8bc17ebb Iustin Pop
                          to string
638 65e183af Michael Hanselmann
  @cvar OP_PARAMS: List of opcode attributes, the default values they should
639 65e183af Michael Hanselmann
                   get if not already defined, and types they must match.
640 1ce03fb1 Michael Hanselmann
  @cvar OP_RESULT: Callable to verify opcode result
641 687c10d9 Iustin Pop
  @cvar WITH_LU: Boolean that specifies whether this should be included in
642 687c10d9 Iustin Pop
      mcpu's dispatch table
643 20777413 Iustin Pop
  @ivar dry_run: Whether the LU should be run in dry-run mode, i.e. just
644 20777413 Iustin Pop
                 the check steps
645 8f5c488d Michael Hanselmann
  @ivar priority: Opcode priority for queue
646 a7399f66 Iustin Pop

647 a7399f66 Iustin Pop
  """
648 b459a848 Andrea Spadaccini
  # pylint: disable=E1101
649 e89a9021 Iustin Pop
  # as OP_ID is dynamically defined
650 687c10d9 Iustin Pop
  WITH_LU = True
651 65e183af Michael Hanselmann
  OP_PARAMS = [
652 45d4c81c Michael Hanselmann
    ("dry_run", None, ht.TMaybeBool, "Run checks only, don't execute"),
653 fd9f58fd Iustin Pop
    ("debug_level", None, ht.TMaybe(ht.TNonNegativeInt), "Debug level"),
654 65e183af Michael Hanselmann
    ("priority", constants.OP_PRIO_DEFAULT,
655 45d4c81c Michael Hanselmann
     ht.TElemOf(constants.OP_PRIO_SUBMIT_VALID), "Opcode priority"),
656 b247c6fc Michael Hanselmann
    (DEPEND_ATTR, None, _BuildJobDepCheck(True),
657 b247c6fc Michael Hanselmann
     "Job dependencies; if used through ``SubmitManyJobs`` relative (negative)"
658 822a50c4 Michael Hanselmann
     " job IDs can be used; see :doc:`design document <design-chained-jobs>`"
659 822a50c4 Michael Hanselmann
     " for details"),
660 018ae30b Michael Hanselmann
    (COMMENT_ATTR, None, ht.TMaybeString,
661 018ae30b Michael Hanselmann
     "Comment describing the purpose of the opcode"),
662 dbae804a Michele Tartara
    (constants.OPCODE_REASON, None, ht.TMaybeList,
663 dbae804a Michele Tartara
     "The reason trail, describing why the OpCode is executed"),
664 65e183af Michael Hanselmann
    ]
665 1ce03fb1 Michael Hanselmann
  OP_RESULT = None
666 df458e0b Iustin Pop
667 df458e0b Iustin Pop
  def __getstate__(self):
668 df458e0b Iustin Pop
    """Specialized getstate for opcodes.
669 df458e0b Iustin Pop

670 a7399f66 Iustin Pop
    This method adds to the state dictionary the OP_ID of the class,
671 a7399f66 Iustin Pop
    so that on unload we can identify the correct class for
672 a7399f66 Iustin Pop
    instantiating the opcode.
673 a7399f66 Iustin Pop

674 a7399f66 Iustin Pop
    @rtype:   C{dict}
675 a7399f66 Iustin Pop
    @return:  the state as a dictionary
676 a7399f66 Iustin Pop

677 df458e0b Iustin Pop
    """
678 0e46916d Iustin Pop
    data = BaseOpCode.__getstate__(self)
679 df458e0b Iustin Pop
    data["OP_ID"] = self.OP_ID
680 df458e0b Iustin Pop
    return data
681 df458e0b Iustin Pop
682 df458e0b Iustin Pop
  @classmethod
683 00abdc96 Iustin Pop
  def LoadOpCode(cls, data):
684 df458e0b Iustin Pop
    """Generic load opcode method.
685 df458e0b Iustin Pop

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

690 a7399f66 Iustin Pop
    @type data:  C{dict}
691 a7399f66 Iustin Pop
    @param data: the serialized opcode
692 a7399f66 Iustin Pop

693 df458e0b Iustin Pop
    """
694 df458e0b Iustin Pop
    if not isinstance(data, dict):
695 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode (%s)" % type(data))
696 df458e0b Iustin Pop
    if "OP_ID" not in data:
697 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpcode, missing OP_ID")
698 df458e0b Iustin Pop
    op_id = data["OP_ID"]
699 df458e0b Iustin Pop
    op_class = None
700 363acb1e Iustin Pop
    if op_id in OP_MAPPING:
701 363acb1e Iustin Pop
      op_class = OP_MAPPING[op_id]
702 363acb1e Iustin Pop
    else:
703 df458e0b Iustin Pop
      raise ValueError("Invalid data to LoadOpCode: OP_ID %s unsupported" %
704 df458e0b Iustin Pop
                       op_id)
705 df458e0b Iustin Pop
    op = op_class()
706 df458e0b Iustin Pop
    new_data = data.copy()
707 df458e0b Iustin Pop
    del new_data["OP_ID"]
708 df458e0b Iustin Pop
    op.__setstate__(new_data)
709 df458e0b Iustin Pop
    return op
710 df458e0b Iustin Pop
711 60dd1473 Iustin Pop
  def Summary(self):
712 60dd1473 Iustin Pop
    """Generates a summary description of this opcode.
713 60dd1473 Iustin Pop

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

720 60dd1473 Iustin Pop
    """
721 ff0d18e6 Iustin Pop
    assert self.OP_ID is not None and len(self.OP_ID) > 3
722 60dd1473 Iustin Pop
    # all OP_ID start with OP_, we remove that
723 60dd1473 Iustin Pop
    txt = self.OP_ID[3:]
724 60dd1473 Iustin Pop
    field_name = getattr(self, "OP_DSC_FIELD", None)
725 60dd1473 Iustin Pop
    if field_name:
726 60dd1473 Iustin Pop
      field_value = getattr(self, field_name, None)
727 8bc17ebb Iustin Pop
      field_formatter = getattr(self, "OP_DSC_FORMATTER", None)
728 8bc17ebb Iustin Pop
      if callable(field_formatter):
729 8bc17ebb Iustin Pop
        field_value = field_formatter(field_value)
730 8bc17ebb Iustin Pop
      elif isinstance(field_value, (list, tuple)):
731 bc8bbda1 Iustin Pop
        field_value = ",".join(str(i) for i in field_value)
732 60dd1473 Iustin Pop
      txt = "%s(%s)" % (txt, field_value)
733 60dd1473 Iustin Pop
    return txt
734 60dd1473 Iustin Pop
735 3ce9a5e7 Michael Hanselmann
  def TinySummary(self):
736 3ce9a5e7 Michael Hanselmann
    """Generates a compact summary description of the opcode.
737 3ce9a5e7 Michael Hanselmann

738 3ce9a5e7 Michael Hanselmann
    """
739 3ce9a5e7 Michael Hanselmann
    assert self.OP_ID.startswith("OP_")
740 3ce9a5e7 Michael Hanselmann
741 3ce9a5e7 Michael Hanselmann
    text = self.OP_ID[3:]
742 3ce9a5e7 Michael Hanselmann
743 3ce9a5e7 Michael Hanselmann
    for (prefix, supplement) in _SUMMARY_PREFIX.items():
744 3ce9a5e7 Michael Hanselmann
      if text.startswith(prefix):
745 3ce9a5e7 Michael Hanselmann
        return supplement + text[len(prefix):]
746 3ce9a5e7 Michael Hanselmann
747 3ce9a5e7 Michael Hanselmann
    return text
748 3ce9a5e7 Michael Hanselmann
749 a8083063 Iustin Pop
750 afee0879 Iustin Pop
# cluster opcodes
751 afee0879 Iustin Pop
752 bc84ffa7 Iustin Pop
class OpClusterPostInit(OpCode):
753 b5f5fae9 Luca Bigliardi
  """Post cluster initialization.
754 b5f5fae9 Luca Bigliardi

755 b5f5fae9 Luca Bigliardi
  This opcode does not touch the cluster at all. Its purpose is to run hooks
756 b5f5fae9 Luca Bigliardi
  after the cluster has been initialized.
757 b5f5fae9 Luca Bigliardi

758 b5f5fae9 Luca Bigliardi
  """
759 c363310d René Nussbaumer
  OP_RESULT = ht.TBool
760 b5f5fae9 Luca Bigliardi
761 b5f5fae9 Luca Bigliardi
762 c6d43e9e Iustin Pop
class OpClusterDestroy(OpCode):
763 a7399f66 Iustin Pop
  """Destroy the cluster.
764 a7399f66 Iustin Pop

765 a7399f66 Iustin Pop
  This opcode has no other parameters. All the state is irreversibly
766 a7399f66 Iustin Pop
  lost after the execution of this opcode.
767 a7399f66 Iustin Pop

768 a7399f66 Iustin Pop
  """
769 c363310d René Nussbaumer
  OP_RESULT = ht.TNonEmptyString
770 a8083063 Iustin Pop
771 a8083063 Iustin Pop
772 a2f7ab92 Iustin Pop
class OpClusterQuery(OpCode):
773 fdc267f4 Iustin Pop
  """Query cluster information."""
774 415feb2e René Nussbaumer
  OP_RESULT = ht.TDictOf(ht.TNonEmptyString, ht.TAny)
775 a8083063 Iustin Pop
776 a8083063 Iustin Pop
777 fcad7225 Michael Hanselmann
class OpClusterVerify(OpCode):
778 fcad7225 Michael Hanselmann
  """Submits all jobs necessary to verify the cluster.
779 fcad7225 Michael Hanselmann

780 fcad7225 Michael Hanselmann
  """
781 fcad7225 Michael Hanselmann
  OP_PARAMS = [
782 fcad7225 Michael Hanselmann
    _PDebugSimulateErrors,
783 fcad7225 Michael Hanselmann
    _PErrorCodes,
784 fcad7225 Michael Hanselmann
    _PSkipChecks,
785 93f2399e Andrea Spadaccini
    _PIgnoreErrors,
786 fcad7225 Michael Hanselmann
    _PVerbose,
787 3c286190 Dimitris Aragiorgis
    ("group_name", None, ht.TMaybeString, "Group to verify"),
788 fcad7225 Michael Hanselmann
    ]
789 f7686867 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
790 fcad7225 Michael Hanselmann
791 fcad7225 Michael Hanselmann
792 bf93ae69 Adeodato Simo
class OpClusterVerifyConfig(OpCode):
793 bf93ae69 Adeodato Simo
  """Verify the cluster config.
794 bf93ae69 Adeodato Simo

795 bf93ae69 Adeodato Simo
  """
796 bf93ae69 Adeodato Simo
  OP_PARAMS = [
797 57106b74 Michael Hanselmann
    _PDebugSimulateErrors,
798 57106b74 Michael Hanselmann
    _PErrorCodes,
799 93f2399e Andrea Spadaccini
    _PIgnoreErrors,
800 57106b74 Michael Hanselmann
    _PVerbose,
801 bf93ae69 Adeodato Simo
    ]
802 f7686867 Michael Hanselmann
  OP_RESULT = ht.TBool
803 bf93ae69 Adeodato Simo
804 bf93ae69 Adeodato Simo
805 bf93ae69 Adeodato Simo
class OpClusterVerifyGroup(OpCode):
806 bf93ae69 Adeodato Simo
  """Run verify on a node group from the cluster.
807 a7399f66 Iustin Pop

808 a7399f66 Iustin Pop
  @type skip_checks: C{list}
809 a7399f66 Iustin Pop
  @ivar skip_checks: steps to be skipped from the verify process; this
810 a7399f66 Iustin Pop
                     needs to be a subset of
811 a7399f66 Iustin Pop
                     L{constants.VERIFY_OPTIONAL_CHECKS}; currently
812 a7399f66 Iustin Pop
                     only L{constants.VERIFY_NPLUSONE_MEM} can be passed
813 a7399f66 Iustin Pop

814 a7399f66 Iustin Pop
  """
815 bf93ae69 Adeodato Simo
  OP_DSC_FIELD = "group_name"
816 65e183af Michael Hanselmann
  OP_PARAMS = [
817 57106b74 Michael Hanselmann
    _PGroupName,
818 57106b74 Michael Hanselmann
    _PDebugSimulateErrors,
819 57106b74 Michael Hanselmann
    _PErrorCodes,
820 57106b74 Michael Hanselmann
    _PSkipChecks,
821 93f2399e Andrea Spadaccini
    _PIgnoreErrors,
822 57106b74 Michael Hanselmann
    _PVerbose,
823 65e183af Michael Hanselmann
    ]
824 f7686867 Michael Hanselmann
  OP_RESULT = ht.TBool
825 a8083063 Iustin Pop
826 a8083063 Iustin Pop
827 bd8210a7 Iustin Pop
class OpClusterVerifyDisks(OpCode):
828 150e978f Iustin Pop
  """Verify the cluster disks.
829 150e978f Iustin Pop

830 ae1a845c Michael Hanselmann
  """
831 f7686867 Michael Hanselmann
  OP_RESULT = TJobIdListOnly
832 ae1a845c Michael Hanselmann
833 ae1a845c Michael Hanselmann
834 ae1a845c Michael Hanselmann
class OpGroupVerifyDisks(OpCode):
835 ae1a845c Michael Hanselmann
  """Verifies the status of all disks in a node group.
836 150e978f Iustin Pop

837 ae1a845c Michael Hanselmann
  Result: a tuple of three elements:
838 ae1a845c Michael Hanselmann
    - dict of node names with issues (values: error msg)
839 150e978f Iustin Pop
    - list of instances with degraded disks (that should be activated)
840 b63ed789 Iustin Pop
    - dict of instances with missing logical volumes (values: (node, vol)
841 b63ed789 Iustin Pop
      pairs with details about the missing volumes)
842 150e978f Iustin Pop

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

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

851 150e978f Iustin Pop
  """
852 ae1a845c Michael Hanselmann
  OP_DSC_FIELD = "group_name"
853 ae1a845c Michael Hanselmann
  OP_PARAMS = [
854 ae1a845c Michael Hanselmann
    _PGroupName,
855 ae1a845c Michael Hanselmann
    ]
856 1ce03fb1 Michael Hanselmann
  OP_RESULT = \
857 1ce03fb1 Michael Hanselmann
    ht.TAnd(ht.TIsLength(3),
858 1ce03fb1 Michael Hanselmann
            ht.TItems([ht.TDictOf(ht.TString, ht.TString),
859 1ce03fb1 Michael Hanselmann
                       ht.TListOf(ht.TString),
860 6973587f Michael Hanselmann
                       ht.TDictOf(ht.TString,
861 6973587f Michael Hanselmann
                                  ht.TListOf(ht.TListOf(ht.TString)))]))
862 150e978f Iustin Pop
863 150e978f Iustin Pop
864 5d01aca3 Iustin Pop
class OpClusterRepairDiskSizes(OpCode):
865 60975797 Iustin Pop
  """Verify the disk sizes of the instances and fixes configuration
866 60975797 Iustin Pop
  mimatches.
867 60975797 Iustin Pop

868 60975797 Iustin Pop
  Parameters: optional instances list, in case we want to restrict the
869 60975797 Iustin Pop
  checks to only a subset of the instances.
870 60975797 Iustin Pop

871 60975797 Iustin Pop
  Result: a list of tuples, (instance, disk, new-size) for changed
872 60975797 Iustin Pop
  configurations.
873 60975797 Iustin Pop

874 60975797 Iustin Pop
  In normal operation, the list should be empty.
875 60975797 Iustin Pop

876 60975797 Iustin Pop
  @type instances: list
877 60975797 Iustin Pop
  @ivar instances: the list of instances to check, or empty for all instances
878 60975797 Iustin Pop

879 60975797 Iustin Pop
  """
880 65e183af Michael Hanselmann
  OP_PARAMS = [
881 197b323b Michael Hanselmann
    ("instances", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
882 65e183af Michael Hanselmann
    ]
883 c363310d René Nussbaumer
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(3),
884 c363310d René Nussbaumer
                                 ht.TItems([ht.TNonEmptyString,
885 2c9fa1ff Iustin Pop
                                            ht.TNonNegativeInt,
886 2c9fa1ff Iustin Pop
                                            ht.TNonNegativeInt])))
887 60975797 Iustin Pop
888 60975797 Iustin Pop
889 2f093ea0 Iustin Pop
class OpClusterConfigQuery(OpCode):
890 ae5849b5 Michael Hanselmann
  """Query cluster configuration values."""
891 65e183af Michael Hanselmann
  OP_PARAMS = [
892 3c286190 Dimitris Aragiorgis
    _POutputFields,
893 65e183af Michael Hanselmann
    ]
894 c363310d René Nussbaumer
  OP_RESULT = ht.TListOf(ht.TAny)
895 a8083063 Iustin Pop
896 a8083063 Iustin Pop
897 e126df25 Iustin Pop
class OpClusterRename(OpCode):
898 a7399f66 Iustin Pop
  """Rename the cluster.
899 a7399f66 Iustin Pop

900 a7399f66 Iustin Pop
  @type name: C{str}
901 a7399f66 Iustin Pop
  @ivar name: The new name of the cluster. The name and/or the master IP
902 a7399f66 Iustin Pop
              address will be changed to match the new name and its IP
903 a7399f66 Iustin Pop
              address.
904 a7399f66 Iustin Pop

905 a7399f66 Iustin Pop
  """
906 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
907 65e183af Michael Hanselmann
  OP_PARAMS = [
908 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
909 65e183af Michael Hanselmann
    ]
910 c363310d René Nussbaumer
  OP_RESULT = ht.TNonEmptyString
911 07bd8a51 Iustin Pop
912 07bd8a51 Iustin Pop
913 a6682fdc Iustin Pop
class OpClusterSetParams(OpCode):
914 a7399f66 Iustin Pop
  """Change the parameters of the cluster.
915 a7399f66 Iustin Pop

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

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

988 afee0879 Iustin Pop
  """
989 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
990 afee0879 Iustin Pop
991 83f72637 Michael Hanselmann
992 fb926117 Andrea Spadaccini
class OpClusterActivateMasterIp(OpCode):
993 fb926117 Andrea Spadaccini
  """Activate the master IP on the master node.
994 fb926117 Andrea Spadaccini

995 fb926117 Andrea Spadaccini
  """
996 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
997 fb926117 Andrea Spadaccini
998 fb926117 Andrea Spadaccini
999 fb926117 Andrea Spadaccini
class OpClusterDeactivateMasterIp(OpCode):
1000 fb926117 Andrea Spadaccini
  """Deactivate the master IP on the master node.
1001 fb926117 Andrea Spadaccini

1002 fb926117 Andrea Spadaccini
  """
1003 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1004 fb926117 Andrea Spadaccini
1005 fb926117 Andrea Spadaccini
1006 83f72637 Michael Hanselmann
class OpQuery(OpCode):
1007 83f72637 Michael Hanselmann
  """Query for resources/items.
1008 83f72637 Michael Hanselmann

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

1013 83f72637 Michael Hanselmann
  """
1014 e50d8412 Michael Hanselmann
  OP_DSC_FIELD = "what"
1015 65e183af Michael Hanselmann
  OP_PARAMS = [
1016 8e7078e0 Michael Hanselmann
    _PQueryWhat,
1017 ee13764f Michael Hanselmann
    _PUseLocking,
1018 45d4c81c Michael Hanselmann
    ("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1019 45d4c81c Michael Hanselmann
     "Requested fields"),
1020 fd9f58fd Iustin Pop
    ("qfilter", None, ht.TMaybe(ht.TList),
1021 45d4c81c Michael Hanselmann
     "Query filter"),
1022 83f72637 Michael Hanselmann
    ]
1023 415feb2e René Nussbaumer
  OP_RESULT = \
1024 b02c6bdf Michael Hanselmann
    _GenerateObjectTypeCheck(objects.QueryResponse, {
1025 b02c6bdf Michael Hanselmann
      "fields": ht.TListOf(_TQueryFieldDef),
1026 b02c6bdf Michael Hanselmann
      "data": _TQueryResult,
1027 b02c6bdf Michael Hanselmann
      })
1028 83f72637 Michael Hanselmann
1029 83f72637 Michael Hanselmann
1030 83f72637 Michael Hanselmann
class OpQueryFields(OpCode):
1031 83f72637 Michael Hanselmann
  """Query for available resource/item fields.
1032 83f72637 Michael Hanselmann

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

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

1070 e4d745a7 Michael Hanselmann
  """
1071 e4d745a7 Michael Hanselmann
  OP_PARAMS = [
1072 e4d745a7 Michael Hanselmann
    _PUseLocking,
1073 e4d745a7 Michael Hanselmann
    ("nodes", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
1074 e4d745a7 Michael Hanselmann
     "Nodes on which the command should be run (at least one)"),
1075 e4d745a7 Michael Hanselmann
    ("command", ht.NoDefault, ht.TNonEmptyString,
1076 e4d745a7 Michael Hanselmann
     "Command name (no parameters)"),
1077 e4d745a7 Michael Hanselmann
    ]
1078 e4d745a7 Michael Hanselmann
1079 e4d745a7 Michael Hanselmann
  _RESULT_ITEMS = [
1080 e4d745a7 Michael Hanselmann
    ht.Comment("success")(ht.TBool),
1081 e4d745a7 Michael Hanselmann
    ht.Comment("output or error message")(ht.TString),
1082 e4d745a7 Michael Hanselmann
    ]
1083 e4d745a7 Michael Hanselmann
1084 e4d745a7 Michael Hanselmann
  OP_RESULT = \
1085 e4d745a7 Michael Hanselmann
    ht.TListOf(ht.TAnd(ht.TIsLength(len(_RESULT_ITEMS)),
1086 e4d745a7 Michael Hanselmann
                       ht.TItems(_RESULT_ITEMS)))
1087 e4d745a7 Michael Hanselmann
1088 e4d745a7 Michael Hanselmann
1089 07bd8a51 Iustin Pop
# node opcodes
1090 07bd8a51 Iustin Pop
1091 73d565a3 Iustin Pop
class OpNodeRemove(OpCode):
1092 a7399f66 Iustin Pop
  """Remove a node.
1093 a7399f66 Iustin Pop

1094 a7399f66 Iustin Pop
  @type node_name: C{str}
1095 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to remove. If the node still has
1096 a7399f66 Iustin Pop
                   instances on it, the operation will fail.
1097 a7399f66 Iustin Pop

1098 a7399f66 Iustin Pop
  """
1099 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
1100 65e183af Michael Hanselmann
  OP_PARAMS = [
1101 65e183af Michael Hanselmann
    _PNodeName,
1102 65e183af Michael Hanselmann
    ]
1103 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1104 a8083063 Iustin Pop
1105 a8083063 Iustin Pop
1106 d817d49f Iustin Pop
class OpNodeAdd(OpCode):
1107 a7399f66 Iustin Pop
  """Add a node to the cluster.
1108 a7399f66 Iustin Pop

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

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

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

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

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

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

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

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

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

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

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

1527 53c776b5 Iustin Pop
  This migrates (without shutting down an instance) to its secondary
1528 53c776b5 Iustin Pop
  node.
1529 53c776b5 Iustin Pop

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

1533 53c776b5 Iustin Pop
  """
1534 ee69c97f Iustin Pop
  OP_DSC_FIELD = "instance_name"
1535 65e183af Michael Hanselmann
  OP_PARAMS = [
1536 65e183af Michael Hanselmann
    _PInstanceName,
1537 65e183af Michael Hanselmann
    _PMigrationMode,
1538 65e183af Michael Hanselmann
    _PMigrationLive,
1539 f8fa4175 Michael Hanselmann
    _PMigrationTargetNode,
1540 8c0b16f6 Guido Trotter
    _PAllowRuntimeChgs,
1541 3ed23330 René Nussbaumer
    _PIgnoreIpolicy,
1542 45d4c81c Michael Hanselmann
    ("cleanup", False, ht.TBool,
1543 45d4c81c Michael Hanselmann
     "Whether a previously failed migration should be cleaned up"),
1544 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1545 89514061 Iustin Pop
                     " shared-storage instances"),
1546 d5cafd31 René Nussbaumer
    ("allow_failover", False, ht.TBool,
1547 d5cafd31 René Nussbaumer
     "Whether we can fallback to failover if migration is not possible"),
1548 65e183af Michael Hanselmann
    ]
1549 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1550 53c776b5 Iustin Pop
1551 53c776b5 Iustin Pop
1552 0091b480 Iustin Pop
class OpInstanceMove(OpCode):
1553 313bcead Iustin Pop
  """Move an instance.
1554 313bcead Iustin Pop

1555 313bcead Iustin Pop
  This move (with shutting down an instance and data copying) to an
1556 313bcead Iustin Pop
  arbitrary node.
1557 313bcead Iustin Pop

1558 313bcead Iustin Pop
  @ivar instance_name: the name of the instance
1559 313bcead Iustin Pop
  @ivar target_node: the destination node
1560 313bcead Iustin Pop

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

1654 ddc1de7c Michael Hanselmann
  """
1655 e9c3d864 Michael Hanselmann
  # Old format
1656 e9c3d864 Michael Hanselmann
  # TODO: Remove in version 2.8 including support in LUInstanceSetParams
1657 e9c3d864 Michael Hanselmann
  old_mod_item_fn = \
1658 ddc1de7c Michael Hanselmann
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1659 2c9fa1ff Iustin Pop
      ht.TOr(ht.TElemOf(constants.DDMS_VALUES), ht.TNonNegativeInt),
1660 ddc1de7c Michael Hanselmann
      fn,
1661 ddc1de7c Michael Hanselmann
      ]))
1662 ddc1de7c Michael Hanselmann
1663 e9c3d864 Michael Hanselmann
  # New format, supporting adding/removing disks/NICs at arbitrary indices
1664 e9c3d864 Michael Hanselmann
  mod_item_fn = \
1665 e9c3d864 Michael Hanselmann
    ht.TAnd(ht.TIsLength(3), ht.TItems([
1666 e9c3d864 Michael Hanselmann
      ht.TElemOf(constants.DDMS_VALUES_WITH_MODIFY),
1667 b21d488b Christos Stavrakakis
      ht.Comment("Device index, can be negative, e.g. -1 for last disk")
1668 b21d488b Christos Stavrakakis
                 (ht.TOr(ht.TInt, ht.TString)),
1669 e9c3d864 Michael Hanselmann
      fn,
1670 e9c3d864 Michael Hanselmann
      ]))
1671 e9c3d864 Michael Hanselmann
1672 e9c3d864 Michael Hanselmann
  return ht.TOr(ht.Comment("Recommended")(ht.TListOf(mod_item_fn)),
1673 e9c3d864 Michael Hanselmann
                ht.Comment("Deprecated")(ht.TListOf(old_mod_item_fn)))
1674 ddc1de7c Michael Hanselmann
1675 ddc1de7c Michael Hanselmann
1676 9a3cc7ae Iustin Pop
class OpInstanceSetParams(OpCode):
1677 ddc1de7c Michael Hanselmann
  """Change the parameters of an instance.
1678 ddc1de7c Michael Hanselmann

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

1872 1410fa8d Michael Hanselmann
  @ivar instance_name: Instance name
1873 1410fa8d Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1874 1410fa8d Michael Hanselmann

1875 1410fa8d Michael Hanselmann
  """
1876 1410fa8d Michael Hanselmann
  OP_DSC_FIELD = "instance_name"
1877 65e183af Michael Hanselmann
  OP_PARAMS = [
1878 65e183af Michael Hanselmann
    _PInstanceName,
1879 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1880 45d4c81c Michael Hanselmann
     "Export mode"),
1881 1410fa8d Michael Hanselmann
    ]
1882 fd9f58fd Iustin Pop
  OP_RESULT = ht.TMaybeDict
1883 1410fa8d Michael Hanselmann
1884 1410fa8d Michael Hanselmann
1885 4ff922a2 Iustin Pop
class OpBackupExport(OpCode):
1886 4a96f1d1 Michael Hanselmann
  """Export an instance.
1887 4a96f1d1 Michael Hanselmann

1888 398e9066 Iustin Pop
  For local exports, the export destination is the node name. For
1889 398e9066 Iustin Pop
  remote exports, the export destination is a list of tuples, each
1890 398e9066 Iustin Pop
  consisting of hostname/IP address, port, magic, HMAC and HMAC
1891 398e9066 Iustin Pop
  salt. The HMAC is calculated using the cluster domain secret over
1892 398e9066 Iustin Pop
  the value "${index}:${hostname}:${port}". The destination X509 CA
1893 398e9066 Iustin Pop
  must be a signed certificate.
1894 4a96f1d1 Michael Hanselmann

1895 4a96f1d1 Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1896 4a96f1d1 Michael Hanselmann
  @ivar target_node: Export destination
1897 4a96f1d1 Michael Hanselmann
  @ivar x509_key_name: X509 key to use (remote export only)
1898 4a96f1d1 Michael Hanselmann
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1899 4a96f1d1 Michael Hanselmann
                             only)
1900 4a96f1d1 Michael Hanselmann

1901 4a96f1d1 Michael Hanselmann
  """
1902 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1903 65e183af Michael Hanselmann
  OP_PARAMS = [
1904 65e183af Michael Hanselmann
    _PInstanceName,
1905 65e183af Michael Hanselmann
    _PShutdownTimeout,
1906 4a96f1d1 Michael Hanselmann
    # TODO: Rename target_node as it changes meaning for different export modes
1907 4a96f1d1 Michael Hanselmann
    # (e.g. "destination")
1908 45d4c81c Michael Hanselmann
    ("target_node", ht.NoDefault, ht.TOr(ht.TNonEmptyString, ht.TList),
1909 45d4c81c Michael Hanselmann
     "Destination information, depends on export mode"),
1910 45d4c81c Michael Hanselmann
    ("shutdown", True, ht.TBool, "Whether to shutdown instance before export"),
1911 45d4c81c Michael Hanselmann
    ("remove_instance", False, ht.TBool,
1912 45d4c81c Michael Hanselmann
     "Whether to remove instance after export"),
1913 45d4c81c Michael Hanselmann
    ("ignore_remove_failures", False, ht.TBool,
1914 45d4c81c Michael Hanselmann
     "Whether to ignore failures while removing instances"),
1915 45d4c81c Michael Hanselmann
    ("mode", constants.EXPORT_MODE_LOCAL, ht.TElemOf(constants.EXPORT_MODES),
1916 45d4c81c Michael Hanselmann
     "Export mode"),
1917 fd9f58fd Iustin Pop
    ("x509_key_name", None, ht.TMaybe(ht.TList),
1918 45d4c81c Michael Hanselmann
     "Name of X509 key (remote export only)"),
1919 45d4c81c Michael Hanselmann
    ("destination_x509_ca", None, ht.TMaybeString,
1920 45d4c81c Michael Hanselmann
     "Destination X509 CA (remote export only)"),
1921 17c3f802 Guido Trotter
    ]
1922 202fb4e0 Michael Hanselmann
  OP_RESULT = \
1923 202fb4e0 Michael Hanselmann
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1924 202fb4e0 Michael Hanselmann
      ht.Comment("Finalizing status")(ht.TBool),
1925 202fb4e0 Michael Hanselmann
      ht.Comment("Status for every exported disk")(ht.TListOf(ht.TBool)),
1926 202fb4e0 Michael Hanselmann
      ]))
1927 5c947f38 Iustin Pop
1928 0a7bed64 Michael Hanselmann
1929 ca5890ad Iustin Pop
class OpBackupRemove(OpCode):
1930 9ac99fda Guido Trotter
  """Remove an instance's export."""
1931 60dd1473 Iustin Pop
  OP_DSC_FIELD = "instance_name"
1932 65e183af Michael Hanselmann
  OP_PARAMS = [
1933 65e183af Michael Hanselmann
    _PInstanceName,
1934 65e183af Michael Hanselmann
    ]
1935 202fb4e0 Michael Hanselmann
  OP_RESULT = ht.TNone
1936 5c947f38 Iustin Pop
1937 0a7bed64 Michael Hanselmann
1938 5c947f38 Iustin Pop
# Tags opcodes
1939 c6afb1ca Iustin Pop
class OpTagsGet(OpCode):
1940 5c947f38 Iustin Pop
  """Returns the tags of the given object."""
1941 60dd1473 Iustin Pop
  OP_DSC_FIELD = "name"
1942 65e183af Michael Hanselmann
  OP_PARAMS = [
1943 65e183af Michael Hanselmann
    _PTagKind,
1944 cfdf561d Michael Hanselmann
    # Not using _PUseLocking as the default is different for historical reasons
1945 cfdf561d Michael Hanselmann
    ("use_locking", True, ht.TBool, "Whether to use synchronization"),
1946 65e183af Michael Hanselmann
    # Name is only meaningful for nodes and instances
1947 971b3a98 Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString,
1948 971b3a98 Michael Hanselmann
     "Name of object to retrieve tags from"),
1949 65e183af Michael Hanselmann
    ]
1950 48796673 Michael Hanselmann
  OP_RESULT = ht.TListOf(ht.TNonEmptyString)
1951 5c947f38 Iustin Pop
1952 5c947f38 Iustin Pop
1953 715462e7 Iustin Pop
class OpTagsSearch(OpCode):
1954 73415719 Iustin Pop
  """Searches the tags in the cluster for a given pattern."""
1955 60dd1473 Iustin Pop
  OP_DSC_FIELD = "pattern"
1956 65e183af Michael Hanselmann
  OP_PARAMS = [
1957 971b3a98 Michael Hanselmann
    ("pattern", ht.NoDefault, ht.TNonEmptyString,
1958 971b3a98 Michael Hanselmann
     "Search pattern (regular expression)"),
1959 65e183af Michael Hanselmann
    ]
1960 48796673 Michael Hanselmann
  OP_RESULT = ht.TListOf(ht.TAnd(ht.TIsLength(2), ht.TItems([
1961 48796673 Michael Hanselmann
    ht.TNonEmptyString,
1962 48796673 Michael Hanselmann
    ht.TNonEmptyString,
1963 48796673 Michael Hanselmann
    ])))
1964 73415719 Iustin Pop
1965 73415719 Iustin Pop
1966 d1602edc Iustin Pop
class OpTagsSet(OpCode):
1967 f27302fa Iustin Pop
  """Add a list of tags on a given object."""
1968 65e183af Michael Hanselmann
  OP_PARAMS = [
1969 65e183af Michael Hanselmann
    _PTagKind,
1970 65e183af Michael Hanselmann
    _PTags,
1971 bcd35e09 Dato Simó
    # Name is only meaningful for groups, nodes and instances
1972 971b3a98 Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString,
1973 971b3a98 Michael Hanselmann
     "Name of object where tag(s) should be added"),
1974 65e183af Michael Hanselmann
    ]
1975 48796673 Michael Hanselmann
  OP_RESULT = ht.TNone
1976 5c947f38 Iustin Pop
1977 5c947f38 Iustin Pop
1978 3f0ab95f Iustin Pop
class OpTagsDel(OpCode):
1979 f27302fa Iustin Pop
  """Remove a list of tags from a given object."""
1980 65e183af Michael Hanselmann
  OP_PARAMS = [
1981 65e183af Michael Hanselmann
    _PTagKind,
1982 65e183af Michael Hanselmann
    _PTags,
1983 bcd35e09 Dato Simó
    # Name is only meaningful for groups, nodes and instances
1984 971b3a98 Michael Hanselmann
    ("name", ht.NoDefault, ht.TMaybeString,
1985 971b3a98 Michael Hanselmann
     "Name of object where tag(s) should be deleted"),
1986 65e183af Michael Hanselmann
    ]
1987 48796673 Michael Hanselmann
  OP_RESULT = ht.TNone
1988 06009e27 Iustin Pop
1989 e687ec01 Michael Hanselmann
1990 06009e27 Iustin Pop
# Test opcodes
1991 06009e27 Iustin Pop
class OpTestDelay(OpCode):
1992 06009e27 Iustin Pop
  """Sleeps for a configured amount of time.
1993 06009e27 Iustin Pop

1994 06009e27 Iustin Pop
  This is used just for debugging and testing.
1995 06009e27 Iustin Pop

1996 06009e27 Iustin Pop
  Parameters:
1997 aeb6ba44 Dato Simó
    - duration: the time to sleep, in seconds
1998 06009e27 Iustin Pop
    - on_master: if true, sleep on the master
1999 06009e27 Iustin Pop
    - on_nodes: list of nodes in which to sleep
2000 06009e27 Iustin Pop

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

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

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

2011 06009e27 Iustin Pop
  """
2012 60dd1473 Iustin Pop
  OP_DSC_FIELD = "duration"
2013 65e183af Michael Hanselmann
  OP_PARAMS = [
2014 b795a775 Michael Hanselmann
    ("duration", ht.NoDefault, ht.TNumber, None),
2015 197b323b Michael Hanselmann
    ("on_master", True, ht.TBool, None),
2016 197b323b Michael Hanselmann
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
2017 2c9fa1ff Iustin Pop
    ("repeat", 0, ht.TNonNegativeInt, None),
2018 65e183af Michael Hanselmann
    ]
2019 d61df03e Iustin Pop
2020 8bc17ebb Iustin Pop
  def OP_DSC_FORMATTER(self, value): # pylint: disable=C0103,R0201
2021 8bc17ebb Iustin Pop
    """Custom formatter for duration.
2022 8bc17ebb Iustin Pop

2023 8bc17ebb Iustin Pop
    """
2024 8bc17ebb Iustin Pop
    try:
2025 8bc17ebb Iustin Pop
      v = float(value)
2026 8bc17ebb Iustin Pop
    except TypeError:
2027 8bc17ebb Iustin Pop
      v = value
2028 8bc17ebb Iustin Pop
    return str(v)
2029 8bc17ebb Iustin Pop
2030 d61df03e Iustin Pop
2031 d61df03e Iustin Pop
class OpTestAllocator(OpCode):
2032 d61df03e Iustin Pop
  """Allocator framework testing.
2033 d61df03e Iustin Pop

2034 d61df03e Iustin Pop
  This opcode has two modes:
2035 d61df03e Iustin Pop
    - gather and return allocator input for a given mode (allocate new
2036 d61df03e Iustin Pop
      or replace secondary) and a given instance definition (direction
2037 d61df03e Iustin Pop
      'in')
2038 d61df03e Iustin Pop
    - run a selected allocator for a given operation (as above) and
2039 d61df03e Iustin Pop
      return the allocator output (direction 'out')
2040 d61df03e Iustin Pop

2041 d61df03e Iustin Pop
  """
2042 55b7e783 Iustin Pop
  OP_DSC_FIELD = "iallocator"
2043 65e183af Michael Hanselmann
  OP_PARAMS = [
2044 65e183af Michael Hanselmann
    ("direction", ht.NoDefault,
2045 197b323b Michael Hanselmann
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
2046 197b323b Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
2047 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
2048 ff8067cf Michael Hanselmann
    ("nics", ht.NoDefault,
2049 ff8067cf Michael Hanselmann
     ht.TMaybeListOf(ht.TDictOf(ht.TElemOf([constants.INIC_MAC,
2050 ff8067cf Michael Hanselmann
                                            constants.INIC_IP,
2051 ff8067cf Michael Hanselmann
                                            "bridge"]),
2052 fd9f58fd Iustin Pop
                                ht.TMaybeString)),
2053 ff8067cf Michael Hanselmann
     None),
2054 fd9f58fd Iustin Pop
    ("disks", ht.NoDefault, ht.TMaybe(ht.TList), None),
2055 197b323b Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString, None),
2056 55b7e783 Iustin Pop
    _PIAllocFromDesc(None),
2057 197b323b Michael Hanselmann
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
2058 fd9f58fd Iustin Pop
    ("memory", None, ht.TMaybe(ht.TNonNegativeInt), None),
2059 fd9f58fd Iustin Pop
    ("vcpus", None, ht.TMaybe(ht.TNonNegativeInt), None),
2060 197b323b Michael Hanselmann
    ("os", None, ht.TMaybeString, None),
2061 197b323b Michael Hanselmann
    ("disk_template", None, ht.TMaybeString, None),
2062 ff8067cf Michael Hanselmann
    ("instances", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2063 60152bbe Michael Hanselmann
    ("evac_mode", None,
2064 fd9f58fd Iustin Pop
     ht.TMaybe(ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
2065 ff8067cf Michael Hanselmann
    ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2066 2c9fa1ff Iustin Pop
    ("spindle_use", 1, ht.TNonNegativeInt, None),
2067 2c9fa1ff Iustin Pop
    ("count", 1, ht.TNonNegativeInt, None),
2068 d61df03e Iustin Pop
    ]
2069 363acb1e Iustin Pop
2070 76aef8fc Michael Hanselmann
2071 b469eb4d Iustin Pop
class OpTestJqueue(OpCode):
2072 e58f87a9 Michael Hanselmann
  """Utility opcode to test some aspects of the job queue.
2073 e58f87a9 Michael Hanselmann

2074 e58f87a9 Michael Hanselmann
  """
2075 65e183af Michael Hanselmann
  OP_PARAMS = [
2076 197b323b Michael Hanselmann
    ("notify_waitlock", False, ht.TBool, None),
2077 197b323b Michael Hanselmann
    ("notify_exec", False, ht.TBool, None),
2078 197b323b Michael Hanselmann
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
2079 197b323b Michael Hanselmann
    ("fail", False, ht.TBool, None),
2080 e58f87a9 Michael Hanselmann
    ]
2081 e58f87a9 Michael Hanselmann
2082 e58f87a9 Michael Hanselmann
2083 be760ba8 Michael Hanselmann
class OpTestDummy(OpCode):
2084 be760ba8 Michael Hanselmann
  """Utility opcode used by unittests.
2085 be760ba8 Michael Hanselmann

2086 be760ba8 Michael Hanselmann
  """
2087 65e183af Michael Hanselmann
  OP_PARAMS = [
2088 197b323b Michael Hanselmann
    ("result", ht.NoDefault, ht.NoType, None),
2089 197b323b Michael Hanselmann
    ("messages", ht.NoDefault, ht.NoType, None),
2090 197b323b Michael Hanselmann
    ("fail", ht.NoDefault, ht.NoType, None),
2091 6a373640 Michael Hanselmann
    ("submit_jobs", None, ht.NoType, None),
2092 be760ba8 Michael Hanselmann
    ]
2093 687c10d9 Iustin Pop
  WITH_LU = False
2094 be760ba8 Michael Hanselmann
2095 be760ba8 Michael Hanselmann
2096 eaa4c57c Dimitris Aragiorgis
# Network opcodes
2097 eaa4c57c Dimitris Aragiorgis
# Add a new network in the cluster
2098 eaa4c57c Dimitris Aragiorgis
class OpNetworkAdd(OpCode):
2099 eaa4c57c Dimitris Aragiorgis
  """Add an IP network to the cluster."""
2100 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2101 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2102 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2103 e1494c96 Iustin Pop
    ("network", ht.NoDefault, _TIpNetwork4, "IPv4 subnet"),
2104 e1494c96 Iustin Pop
    ("gateway", None, ht.TMaybe(_TIpAddress4), "IPv4 gateway"),
2105 e1494c96 Iustin Pop
    ("network6", None, ht.TMaybe(_TIpNetwork6), "IPv6 subnet"),
2106 e1494c96 Iustin Pop
    ("gateway6", None, ht.TMaybe(_TIpAddress6), "IPv6 gateway"),
2107 6e8091f9 Dimitris Aragiorgis
    ("mac_prefix", None, ht.TMaybeString,
2108 32e3d8b1 Michael Hanselmann
     "MAC address prefix that overrides cluster one"),
2109 e1494c96 Iustin Pop
    ("add_reserved_ips", None, _TMaybeAddr4List,
2110 32e3d8b1 Michael Hanselmann
     "Which IP addresses to reserve"),
2111 213076fe Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool,
2112 213076fe Dimitris Aragiorgis
     "Whether to check for conflicting IP addresses"),
2113 8140e24f Dimitris Aragiorgis
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), "Network tags"),
2114 eaa4c57c Dimitris Aragiorgis
    ]
2115 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2116 eaa4c57c Dimitris Aragiorgis
2117 3c286190 Dimitris Aragiorgis
2118 eaa4c57c Dimitris Aragiorgis
class OpNetworkRemove(OpCode):
2119 eaa4c57c Dimitris Aragiorgis
  """Remove an existing network from the cluster.
2120 eaa4c57c Dimitris Aragiorgis
     Must not be connected to any nodegroup.
2121 eaa4c57c Dimitris Aragiorgis

2122 eaa4c57c Dimitris Aragiorgis
  """
2123 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2124 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2125 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2126 eaa4c57c Dimitris Aragiorgis
    _PForce,
2127 eaa4c57c Dimitris Aragiorgis
    ]
2128 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2129 eaa4c57c Dimitris Aragiorgis
2130 3c286190 Dimitris Aragiorgis
2131 eaa4c57c Dimitris Aragiorgis
class OpNetworkSetParams(OpCode):
2132 eaa4c57c Dimitris Aragiorgis
  """Modify Network's parameters except for IPv4 subnet"""
2133 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2134 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2135 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2136 e055a2ab Dimitris Aragiorgis
    ("gateway", None, ht.TMaybeValueNone(_TIpAddress4), "IPv4 gateway"),
2137 e055a2ab Dimitris Aragiorgis
    ("network6", None, ht.TMaybeValueNone(_TIpNetwork6), "IPv6 subnet"),
2138 e055a2ab Dimitris Aragiorgis
    ("gateway6", None, ht.TMaybeValueNone(_TIpAddress6), "IPv6 gateway"),
2139 e055a2ab Dimitris Aragiorgis
    ("mac_prefix", None, ht.TMaybeValueNone(ht.TString),
2140 32e3d8b1 Michael Hanselmann
     "MAC address prefix that overrides cluster one"),
2141 e1494c96 Iustin Pop
    ("add_reserved_ips", None, _TMaybeAddr4List,
2142 32e3d8b1 Michael Hanselmann
     "Which external IP addresses to reserve"),
2143 e1494c96 Iustin Pop
    ("remove_reserved_ips", None, _TMaybeAddr4List,
2144 32e3d8b1 Michael Hanselmann
     "Which external IP addresses to release"),
2145 eaa4c57c Dimitris Aragiorgis
    ]
2146 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2147 eaa4c57c Dimitris Aragiorgis
2148 3c286190 Dimitris Aragiorgis
2149 eaa4c57c Dimitris Aragiorgis
class OpNetworkConnect(OpCode):
2150 eaa4c57c Dimitris Aragiorgis
  """Connect a Network to a specific Nodegroup with the defined netparams
2151 eaa4c57c Dimitris Aragiorgis
     (mode, link). Nics in this Network will inherit those params.
2152 eaa4c57c Dimitris Aragiorgis
     Produce errors if a NIC (that its not already assigned to a network)
2153 eaa4c57c Dimitris Aragiorgis
     has an IP that is contained in the Network this will produce error unless
2154 eaa4c57c Dimitris Aragiorgis
     --no-conflicts-check is passed.
2155 eaa4c57c Dimitris Aragiorgis

2156 eaa4c57c Dimitris Aragiorgis
  """
2157 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2158 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2159 eaa4c57c Dimitris Aragiorgis
    _PGroupName,
2160 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2161 6ef37395 Michael Hanselmann
    ("network_mode", ht.NoDefault, ht.TElemOf(constants.NIC_VALID_MODES),
2162 6ef37395 Michael Hanselmann
     "Connectivity mode"),
2163 e1494c96 Iustin Pop
    ("network_link", ht.NoDefault, ht.TString, "Connectivity link"),
2164 6e8091f9 Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool, "Whether to check for conflicting IPs"),
2165 eaa4c57c Dimitris Aragiorgis
    ]
2166 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2167 eaa4c57c Dimitris Aragiorgis
2168 3c286190 Dimitris Aragiorgis
2169 eaa4c57c Dimitris Aragiorgis
class OpNetworkDisconnect(OpCode):
2170 eaa4c57c Dimitris Aragiorgis
  """Disconnect a Network from a Nodegroup. Produce errors if NICs are
2171 eaa4c57c Dimitris Aragiorgis
     present in the Network unless --no-conficts-check option is passed.
2172 eaa4c57c Dimitris Aragiorgis

2173 eaa4c57c Dimitris Aragiorgis
  """
2174 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2175 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2176 eaa4c57c Dimitris Aragiorgis
    _PGroupName,
2177 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2178 eaa4c57c Dimitris Aragiorgis
    ]
2179 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2180 eaa4c57c Dimitris Aragiorgis
2181 3c286190 Dimitris Aragiorgis
2182 eaa4c57c Dimitris Aragiorgis
class OpNetworkQuery(OpCode):
2183 eaa4c57c Dimitris Aragiorgis
  """Compute the list of networks."""
2184 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2185 eaa4c57c Dimitris Aragiorgis
    _POutputFields,
2186 8d459129 Michael Hanselmann
    _PUseLocking,
2187 eaa4c57c Dimitris Aragiorgis
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
2188 eaa4c57c Dimitris Aragiorgis
     "Empty list to query all groups, group names otherwise"),
2189 eaa4c57c Dimitris Aragiorgis
    ]
2190 829cfbc5 Dimitris Aragiorgis
  OP_RESULT = _TOldQueryResult
2191 eaa4c57c Dimitris Aragiorgis
2192 eaa4c57c Dimitris Aragiorgis
2193 dbc96028 Michael Hanselmann
def _GetOpList():
2194 dbc96028 Michael Hanselmann
  """Returns list of all defined opcodes.
2195 dbc96028 Michael Hanselmann

2196 dbc96028 Michael Hanselmann
  Does not eliminate duplicates by C{OP_ID}.
2197 dbc96028 Michael Hanselmann

2198 dbc96028 Michael Hanselmann
  """
2199 dbc96028 Michael Hanselmann
  return [v for v in globals().values()
2200 dbc96028 Michael Hanselmann
          if (isinstance(v, type) and issubclass(v, OpCode) and
2201 687c10d9 Iustin Pop
              hasattr(v, "OP_ID") and v is not OpCode)]
2202 dbc96028 Michael Hanselmann
2203 dbc96028 Michael Hanselmann
2204 dbc96028 Michael Hanselmann
OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())