Statistics
| Branch: | Tag: | Revision:

root / lib / opcodes.py @ 66af5ec5

History | View | Annotate | Download (67 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 69c88fb1 Michele Tartara
# The reason for a state change of an instance
188 69c88fb1 Michele Tartara
_PReason = \
189 69c88fb1 Michele Tartara
  ("reason", (constants.INSTANCE_REASON_SOURCE_UNKNOWN, None),
190 69c88fb1 Michele Tartara
   ht.TAnd(ht.TIsLength(2),
191 69c88fb1 Michele Tartara
           ht.TItems([
192 69c88fb1 Michele Tartara
             ht.TElemOf(constants.INSTANCE_REASON_SOURCES),
193 69c88fb1 Michele Tartara
             ht.TMaybeString,
194 69c88fb1 Michele Tartara
           ])),
195 777e8b6f Michele Tartara
   "The reason why the state of the instance is changing")
196 69c88fb1 Michele Tartara
197 ff0d18e6 Iustin Pop
#: OP_ID conversion regular expression
198 ff0d18e6 Iustin Pop
_OPID_RE = re.compile("([a-z])([A-Z])")
199 ff0d18e6 Iustin Pop
200 8c9ee749 Michael Hanselmann
#: Utility function for L{OpClusterSetParams}
201 8f227489 Michael Hanselmann
_TestClusterOsListItem = \
202 8f227489 Michael Hanselmann
  ht.TAnd(ht.TIsLength(2), ht.TItems([
203 8f227489 Michael Hanselmann
    ht.TElemOf(constants.DDMS_VALUES),
204 8f227489 Michael Hanselmann
    ht.TNonEmptyString,
205 8f227489 Michael Hanselmann
    ]))
206 8c9ee749 Michael Hanselmann
207 ff8067cf Michael Hanselmann
_TestClusterOsList = ht.TMaybeListOf(_TestClusterOsListItem)
208 ff0d18e6 Iustin Pop
209 526a662a Michael Hanselmann
# TODO: Generate check from constants.INIC_PARAMS_TYPES
210 526a662a Michael Hanselmann
#: Utility function for testing NIC definitions
211 20a32e74 Michael Hanselmann
_TestNicDef = \
212 20a32e74 Michael Hanselmann
  ht.Comment("NIC parameters")(ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
213 fd9f58fd Iustin Pop
                                          ht.TMaybeString))
214 526a662a Michael Hanselmann
215 1456df62 Michael Hanselmann
_TSetParamsResultItemItems = [
216 1456df62 Michael Hanselmann
  ht.Comment("name of changed parameter")(ht.TNonEmptyString),
217 b3d2ee31 Michael Hanselmann
  ht.Comment("new value")(ht.TAny),
218 1456df62 Michael Hanselmann
  ]
219 1456df62 Michael Hanselmann
220 1456df62 Michael Hanselmann
_TSetParamsResult = \
221 1456df62 Michael Hanselmann
  ht.TListOf(ht.TAnd(ht.TIsLength(len(_TSetParamsResultItemItems)),
222 1456df62 Michael Hanselmann
                     ht.TItems(_TSetParamsResultItemItems)))
223 1456df62 Michael Hanselmann
224 3e3ddbf0 Constantinos Venetsanopoulos
# In the disks option we can provide arbitrary parameters too, which
225 3e3ddbf0 Constantinos Venetsanopoulos
# we may not be able to validate at this level, so we just check the
226 3e3ddbf0 Constantinos Venetsanopoulos
# format of the dict here and the checks concerning IDISK_PARAMS will
227 3e3ddbf0 Constantinos Venetsanopoulos
# happen at the LU level
228 20a32e74 Michael Hanselmann
_TDiskParams = \
229 3e3ddbf0 Constantinos Venetsanopoulos
  ht.Comment("Disk parameters")(ht.TDictOf(ht.TNonEmptyString,
230 20a32e74 Michael Hanselmann
                                           ht.TOr(ht.TNonEmptyString, ht.TInt)))
231 735e1318 Michael Hanselmann
232 aa12a891 René Nussbaumer
_TQueryRow = \
233 aa12a891 René Nussbaumer
  ht.TListOf(ht.TAnd(ht.TIsLength(2),
234 aa12a891 René Nussbaumer
                     ht.TItems([ht.TElemOf(constants.RS_ALL),
235 aa12a891 René Nussbaumer
                                ht.TAny])))
236 aa12a891 René Nussbaumer
237 415feb2e René Nussbaumer
_TQueryResult = ht.TListOf(_TQueryRow)
238 415feb2e René Nussbaumer
239 415feb2e René Nussbaumer
_TOldQueryRow = ht.TListOf(ht.TAny)
240 415feb2e René Nussbaumer
241 415feb2e René Nussbaumer
_TOldQueryResult = ht.TListOf(_TOldQueryRow)
242 415feb2e René Nussbaumer
243 415feb2e René Nussbaumer
244 3ce9a5e7 Michael Hanselmann
_SUMMARY_PREFIX = {
245 3ce9a5e7 Michael Hanselmann
  "CLUSTER_": "C_",
246 3ce9a5e7 Michael Hanselmann
  "GROUP_": "G_",
247 3ce9a5e7 Michael Hanselmann
  "NODE_": "N_",
248 3ce9a5e7 Michael Hanselmann
  "INSTANCE_": "I_",
249 3ce9a5e7 Michael Hanselmann
  }
250 3ce9a5e7 Michael Hanselmann
251 b95479a5 Michael Hanselmann
#: Attribute name for dependencies
252 b95479a5 Michael Hanselmann
DEPEND_ATTR = "depends"
253 b95479a5 Michael Hanselmann
254 018ae30b Michael Hanselmann
#: Attribute name for comment
255 018ae30b Michael Hanselmann
COMMENT_ATTR = "comment"
256 018ae30b Michael Hanselmann
257 526a662a Michael Hanselmann
258 ff0d18e6 Iustin Pop
def _NameToId(name):
259 ff0d18e6 Iustin Pop
  """Convert an opcode class name to an OP_ID.
260 ff0d18e6 Iustin Pop

261 ff0d18e6 Iustin Pop
  @type name: string
262 ff0d18e6 Iustin Pop
  @param name: the class name, as OpXxxYyy
263 ff0d18e6 Iustin Pop
  @rtype: string
264 ff0d18e6 Iustin Pop
  @return: the name in the OP_XXXX_YYYY format
265 ff0d18e6 Iustin Pop

266 ff0d18e6 Iustin Pop
  """
267 ff0d18e6 Iustin Pop
  if not name.startswith("Op"):
268 ff0d18e6 Iustin Pop
    return None
269 ff0d18e6 Iustin Pop
  # Note: (?<=[a-z])(?=[A-Z]) would be ideal, since it wouldn't
270 ff0d18e6 Iustin Pop
  # consume any input, and hence we would just have all the elements
271 ff0d18e6 Iustin Pop
  # in the list, one by one; but it seems that split doesn't work on
272 ff0d18e6 Iustin Pop
  # non-consuming input, hence we have to process the input string a
273 ff0d18e6 Iustin Pop
  # bit
274 ff0d18e6 Iustin Pop
  name = _OPID_RE.sub(r"\1,\2", name)
275 ff0d18e6 Iustin Pop
  elems = name.split(",")
276 ff0d18e6 Iustin Pop
  return "_".join(n.upper() for n in elems)
277 ff0d18e6 Iustin Pop
278 65e183af Michael Hanselmann
279 415feb2e René Nussbaumer
def _GenerateObjectTypeCheck(obj, fields_types):
280 415feb2e René Nussbaumer
  """Helper to generate type checks for objects.
281 415feb2e René Nussbaumer

282 415feb2e René Nussbaumer
  @param obj: The object to generate type checks
283 415feb2e René Nussbaumer
  @param fields_types: The fields and their types as a dict
284 415feb2e René Nussbaumer
  @return: A ht type check function
285 415feb2e René Nussbaumer

286 415feb2e René Nussbaumer
  """
287 415feb2e René Nussbaumer
  assert set(obj.GetAllSlots()) == set(fields_types.keys()), \
288 415feb2e René Nussbaumer
    "%s != %s" % (set(obj.GetAllSlots()), set(fields_types.keys()))
289 415feb2e René Nussbaumer
  return ht.TStrictDict(True, True, fields_types)
290 415feb2e René Nussbaumer
291 415feb2e René Nussbaumer
292 b02c6bdf Michael Hanselmann
_TQueryFieldDef = \
293 b02c6bdf Michael Hanselmann
  _GenerateObjectTypeCheck(objects.QueryFieldDefinition, {
294 b02c6bdf Michael Hanselmann
    "name": ht.TNonEmptyString,
295 b02c6bdf Michael Hanselmann
    "title": ht.TNonEmptyString,
296 b02c6bdf Michael Hanselmann
    "kind": ht.TElemOf(constants.QFT_ALL),
297 b02c6bdf Michael Hanselmann
    "doc": ht.TNonEmptyString,
298 b02c6bdf Michael Hanselmann
    })
299 415feb2e René Nussbaumer
300 415feb2e René Nussbaumer
301 65e183af Michael Hanselmann
def RequireFileStorage():
302 65e183af Michael Hanselmann
  """Checks that file storage is enabled.
303 65e183af Michael Hanselmann

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

307 65e183af Michael Hanselmann
  @raise errors.OpPrereqError: when file storage is disabled
308 65e183af Michael Hanselmann

309 65e183af Michael Hanselmann
  """
310 65e183af Michael Hanselmann
  if not constants.ENABLE_FILE_STORAGE:
311 65e183af Michael Hanselmann
    raise errors.OpPrereqError("File storage disabled at configure time",
312 65e183af Michael Hanselmann
                               errors.ECODE_INVAL)
313 65e183af Michael Hanselmann
314 65e183af Michael Hanselmann
315 4b97f902 Apollon Oikonomopoulos
def RequireSharedFileStorage():
316 4b97f902 Apollon Oikonomopoulos
  """Checks that shared file storage is enabled.
317 4b97f902 Apollon Oikonomopoulos

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

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

323 4b97f902 Apollon Oikonomopoulos
  """
324 4b97f902 Apollon Oikonomopoulos
  if not constants.ENABLE_SHARED_FILE_STORAGE:
325 4b97f902 Apollon Oikonomopoulos
    raise errors.OpPrereqError("Shared file storage disabled at"
326 4b97f902 Apollon Oikonomopoulos
                               " configure time", errors.ECODE_INVAL)
327 4b97f902 Apollon Oikonomopoulos
328 4b97f902 Apollon Oikonomopoulos
329 8c9ee749 Michael Hanselmann
@ht.WithDesc("CheckFileStorage")
330 8c9ee749 Michael Hanselmann
def _CheckFileStorage(value):
331 8c9ee749 Michael Hanselmann
  """Ensures file storage is enabled if used.
332 65e183af Michael Hanselmann

333 65e183af Michael Hanselmann
  """
334 8c9ee749 Michael Hanselmann
  if value == constants.DT_FILE:
335 65e183af Michael Hanselmann
    RequireFileStorage()
336 4b97f902 Apollon Oikonomopoulos
  elif value == constants.DT_SHARED_FILE:
337 4b97f902 Apollon Oikonomopoulos
    RequireSharedFileStorage()
338 65e183af Michael Hanselmann
  return True
339 65e183af Michael Hanselmann
340 65e183af Michael Hanselmann
341 45fe090b Michael Hanselmann
def _BuildDiskTemplateCheck(accept_none):
342 45fe090b Michael Hanselmann
  """Builds check for disk template.
343 45fe090b Michael Hanselmann

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

851 60975797 Iustin Pop
  Result: a list of tuples, (instance, disk, new-size) for changed
852 60975797 Iustin Pop
  configurations.
853 60975797 Iustin Pop

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1079 a7399f66 Iustin Pop
  @type node_name: C{str}
1080 a7399f66 Iustin Pop
  @ivar node_name: The name of the node to remove. If the node still has
1081 a7399f66 Iustin Pop
                   instances on it, the operation will fail.
1082 a7399f66 Iustin Pop

1083 a7399f66 Iustin Pop
  """
1084 60dd1473 Iustin Pop
  OP_DSC_FIELD = "node_name"
1085 65e183af Michael Hanselmann
  OP_PARAMS = [
1086 65e183af Michael Hanselmann
    _PNodeName,
1087 65e183af Michael Hanselmann
    ]
1088 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1089 a8083063 Iustin Pop
1090 a8083063 Iustin Pop
1091 d817d49f Iustin Pop
class OpNodeAdd(OpCode):
1092 a7399f66 Iustin Pop
  """Add a node to the cluster.
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 add. This can be a short name,
1096 a7399f66 Iustin Pop
                   but it will be expanded to the FQDN.
1097 a7399f66 Iustin Pop
  @type primary_ip: IP address
1098 a7399f66 Iustin Pop
  @ivar primary_ip: The primary IP of the node. This will be ignored when the
1099 a7399f66 Iustin Pop
                    opcode is submitted, but will be filled during the node
1100 a7399f66 Iustin Pop
                    add (so it will be visible in the job query).
1101 a7399f66 Iustin Pop
  @type secondary_ip: IP address
1102 a7399f66 Iustin Pop
  @ivar secondary_ip: The secondary IP of the node. This needs to be passed
1103 a7399f66 Iustin Pop
                      if the cluster has been initialized in 'dual-network'
1104 a7399f66 Iustin Pop
                      mode, otherwise it must not be given.
1105 a7399f66 Iustin Pop
  @type readd: C{bool}
1106 a7399f66 Iustin Pop
  @ivar readd: Whether to re-add an existing node to the cluster. If
1107 a7399f66 Iustin Pop
               this is not passed, then the operation will abort if the node
1108 a7399f66 Iustin Pop
               name is already in the cluster; use this parameter to 'repair'
1109 a7399f66 Iustin Pop
               a node that had its configuration broken, or was reinstalled
1110 a7399f66 Iustin Pop
               without removal from the cluster.
1111 f936c153 Iustin Pop
  @type group: C{str}
1112 f936c153 Iustin Pop
  @ivar group: The node group to which this node will belong.
1113 fd3d37b6 Iustin Pop
  @type vm_capable: C{bool}
1114 fd3d37b6 Iustin Pop
  @ivar vm_capable: The vm_capable node attribute
1115 fd3d37b6 Iustin Pop
  @type master_capable: C{bool}
1116 fd3d37b6 Iustin Pop
  @ivar master_capable: The master_capable node attribute
1117 a7399f66 Iustin Pop

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

1267 9bf56d77 Michael Hanselmann
  @ivar instance_name: Instance name
1268 9bf56d77 Michael Hanselmann
  @ivar mode: Instance creation mode (one of L{constants.INSTANCE_CREATE_MODES})
1269 9bf56d77 Michael Hanselmann
  @ivar source_handshake: Signed handshake from source (remote import only)
1270 9bf56d77 Michael Hanselmann
  @ivar source_x509_ca: Source X509 CA in PEM format (remote import only)
1271 9bf56d77 Michael Hanselmann
  @ivar source_instance_name: Previous name of instance (remote import only)
1272 dae91d02 Michael Hanselmann
  @ivar source_shutdown_timeout: Shutdown timeout used for source instance
1273 dae91d02 Michael Hanselmann
    (remote import only)
1274 9bf56d77 Michael Hanselmann

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

1341 12e62af5 René Nussbaumer
  """
1342 12e62af5 René Nussbaumer
  OP_PARAMS = [
1343 1f1188c3 Michael Hanselmann
    _POpportunisticLocking,
1344 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator used to allocate all the instances"),
1345 4c405df7 Iustin Pop
    ("instances", ht.EmptyList, ht.TListOf(ht.TInstanceOf(OpInstanceCreate)),
1346 12e62af5 René Nussbaumer
     "List of instance create opcodes describing the instances to allocate"),
1347 12e62af5 René Nussbaumer
    ]
1348 12e62af5 René Nussbaumer
  _JOB_LIST = ht.Comment("List of submitted jobs")(TJobIdList)
1349 12e62af5 René Nussbaumer
  ALLOCATABLE_KEY = "allocatable"
1350 12e62af5 René Nussbaumer
  FAILED_KEY = "allocatable"
1351 12e62af5 René Nussbaumer
  OP_RESULT = ht.TStrictDict(True, True, {
1352 12e62af5 René Nussbaumer
    constants.JOB_IDS_KEY: _JOB_LIST,
1353 12e62af5 René Nussbaumer
    ALLOCATABLE_KEY: ht.TListOf(ht.TNonEmptyString),
1354 3c286190 Dimitris Aragiorgis
    FAILED_KEY: ht.TListOf(ht.TNonEmptyString),
1355 12e62af5 René Nussbaumer
    })
1356 12e62af5 René Nussbaumer
1357 12e62af5 René Nussbaumer
  def __getstate__(self):
1358 12e62af5 René Nussbaumer
    """Generic serializer.
1359 12e62af5 René Nussbaumer

1360 12e62af5 René Nussbaumer
    """
1361 12e62af5 René Nussbaumer
    state = OpCode.__getstate__(self)
1362 12e62af5 René Nussbaumer
    if hasattr(self, "instances"):
1363 12e62af5 René Nussbaumer
      # pylint: disable=E1101
1364 12e62af5 René Nussbaumer
      state["instances"] = [inst.__getstate__() for inst in self.instances]
1365 12e62af5 René Nussbaumer
    return state
1366 12e62af5 René Nussbaumer
1367 12e62af5 René Nussbaumer
  def __setstate__(self, state):
1368 12e62af5 René Nussbaumer
    """Generic unserializer.
1369 12e62af5 René Nussbaumer

1370 12e62af5 René Nussbaumer
    This method just restores from the serialized state the attributes
1371 12e62af5 René Nussbaumer
    of the current instance.
1372 12e62af5 René Nussbaumer

1373 12e62af5 René Nussbaumer
    @param state: the serialized opcode data
1374 12e62af5 René Nussbaumer
    @type state: C{dict}
1375 12e62af5 René Nussbaumer

1376 12e62af5 René Nussbaumer
    """
1377 12e62af5 René Nussbaumer
    if not isinstance(state, dict):
1378 12e62af5 René Nussbaumer
      raise ValueError("Invalid data to __setstate__: expected dict, got %s" %
1379 12e62af5 René Nussbaumer
                       type(state))
1380 12e62af5 René Nussbaumer
1381 12e62af5 René Nussbaumer
    if "instances" in state:
1382 5dff65da Michael Hanselmann
      state["instances"] = map(OpCode.LoadOpCode, state["instances"])
1383 5dff65da Michael Hanselmann
1384 12e62af5 René Nussbaumer
    return OpCode.__setstate__(self, state)
1385 12e62af5 René Nussbaumer
1386 9bc5ac44 René Nussbaumer
  def Validate(self, set_defaults):
1387 9bc5ac44 René Nussbaumer
    """Validates this opcode.
1388 9bc5ac44 René Nussbaumer

1389 9bc5ac44 René Nussbaumer
    We do this recursively.
1390 9bc5ac44 René Nussbaumer

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

1513 53c776b5 Iustin Pop
  This migrates (without shutting down an instance) to its secondary
1514 53c776b5 Iustin Pop
  node.
1515 53c776b5 Iustin Pop

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

1519 53c776b5 Iustin Pop
  """
1520 ee69c97f Iustin Pop
  OP_DSC_FIELD = "instance_name"
1521 65e183af Michael Hanselmann
  OP_PARAMS = [
1522 65e183af Michael Hanselmann
    _PInstanceName,
1523 65e183af Michael Hanselmann
    _PMigrationMode,
1524 65e183af Michael Hanselmann
    _PMigrationLive,
1525 f8fa4175 Michael Hanselmann
    _PMigrationTargetNode,
1526 8c0b16f6 Guido Trotter
    _PAllowRuntimeChgs,
1527 3ed23330 René Nussbaumer
    _PIgnoreIpolicy,
1528 45d4c81c Michael Hanselmann
    ("cleanup", False, ht.TBool,
1529 45d4c81c Michael Hanselmann
     "Whether a previously failed migration should be cleaned up"),
1530 89514061 Iustin Pop
    _PIAllocFromDesc("Iallocator for deciding the target node for"
1531 89514061 Iustin Pop
                     " shared-storage instances"),
1532 d5cafd31 René Nussbaumer
    ("allow_failover", False, ht.TBool,
1533 d5cafd31 René Nussbaumer
     "Whether we can fallback to failover if migration is not possible"),
1534 65e183af Michael Hanselmann
    ]
1535 c363310d René Nussbaumer
  OP_RESULT = ht.TNone
1536 53c776b5 Iustin Pop
1537 53c776b5 Iustin Pop
1538 0091b480 Iustin Pop
class OpInstanceMove(OpCode):
1539 313bcead Iustin Pop
  """Move an instance.
1540 313bcead Iustin Pop

1541 313bcead Iustin Pop
  This move (with shutting down an instance and data copying) to an
1542 313bcead Iustin Pop
  arbitrary node.
1543 313bcead Iustin Pop

1544 313bcead Iustin Pop
  @ivar instance_name: the name of the instance
1545 313bcead Iustin Pop
  @ivar target_node: the destination node
1546 313bcead Iustin Pop

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

1640 ddc1de7c Michael Hanselmann
  """
1641 e9c3d864 Michael Hanselmann
  # Old format
1642 e9c3d864 Michael Hanselmann
  # TODO: Remove in version 2.8 including support in LUInstanceSetParams
1643 e9c3d864 Michael Hanselmann
  old_mod_item_fn = \
1644 ddc1de7c Michael Hanselmann
    ht.TAnd(ht.TIsLength(2), ht.TItems([
1645 2c9fa1ff Iustin Pop
      ht.TOr(ht.TElemOf(constants.DDMS_VALUES), ht.TNonNegativeInt),
1646 ddc1de7c Michael Hanselmann
      fn,
1647 ddc1de7c Michael Hanselmann
      ]))
1648 ddc1de7c Michael Hanselmann
1649 e9c3d864 Michael Hanselmann
  # New format, supporting adding/removing disks/NICs at arbitrary indices
1650 e9c3d864 Michael Hanselmann
  mod_item_fn = \
1651 e9c3d864 Michael Hanselmann
    ht.TAnd(ht.TIsLength(3), ht.TItems([
1652 e9c3d864 Michael Hanselmann
      ht.TElemOf(constants.DDMS_VALUES_WITH_MODIFY),
1653 e9c3d864 Michael Hanselmann
      ht.Comment("Disk index, can be negative, e.g. -1 for last disk")(ht.TInt),
1654 e9c3d864 Michael Hanselmann
      fn,
1655 e9c3d864 Michael Hanselmann
      ]))
1656 e9c3d864 Michael Hanselmann
1657 e9c3d864 Michael Hanselmann
  return ht.TOr(ht.Comment("Recommended")(ht.TListOf(mod_item_fn)),
1658 e9c3d864 Michael Hanselmann
                ht.Comment("Deprecated")(ht.TListOf(old_mod_item_fn)))
1659 ddc1de7c Michael Hanselmann
1660 ddc1de7c Michael Hanselmann
1661 9a3cc7ae Iustin Pop
class OpInstanceSetParams(OpCode):
1662 ddc1de7c Michael Hanselmann
  """Change the parameters of an instance.
1663 ddc1de7c Michael Hanselmann

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

1856 1410fa8d Michael Hanselmann
  @ivar instance_name: Instance name
1857 1410fa8d Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1858 1410fa8d Michael Hanselmann

1859 1410fa8d Michael Hanselmann
  """
1860 1410fa8d Michael Hanselmann
  OP_DSC_FIELD = "instance_name"
1861 65e183af Michael Hanselmann
  OP_PARAMS = [
1862 65e183af Michael Hanselmann
    _PInstanceName,
1863 45d4c81c Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.EXPORT_MODES),
1864 45d4c81c Michael Hanselmann
     "Export mode"),
1865 1410fa8d Michael Hanselmann
    ]
1866 fd9f58fd Iustin Pop
  OP_RESULT = ht.TMaybeDict
1867 1410fa8d Michael Hanselmann
1868 1410fa8d Michael Hanselmann
1869 4ff922a2 Iustin Pop
class OpBackupExport(OpCode):
1870 4a96f1d1 Michael Hanselmann
  """Export an instance.
1871 4a96f1d1 Michael Hanselmann

1872 398e9066 Iustin Pop
  For local exports, the export destination is the node name. For
1873 398e9066 Iustin Pop
  remote exports, the export destination is a list of tuples, each
1874 398e9066 Iustin Pop
  consisting of hostname/IP address, port, magic, HMAC and HMAC
1875 398e9066 Iustin Pop
  salt. The HMAC is calculated using the cluster domain secret over
1876 398e9066 Iustin Pop
  the value "${index}:${hostname}:${port}". The destination X509 CA
1877 398e9066 Iustin Pop
  must be a signed certificate.
1878 4a96f1d1 Michael Hanselmann

1879 4a96f1d1 Michael Hanselmann
  @ivar mode: Export mode (one of L{constants.EXPORT_MODES})
1880 4a96f1d1 Michael Hanselmann
  @ivar target_node: Export destination
1881 4a96f1d1 Michael Hanselmann
  @ivar x509_key_name: X509 key to use (remote export only)
1882 4a96f1d1 Michael Hanselmann
  @ivar destination_x509_ca: Destination X509 CA in PEM format (remote export
1883 4a96f1d1 Michael Hanselmann
                             only)
1884 4a96f1d1 Michael Hanselmann

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

1978 06009e27 Iustin Pop
  This is used just for debugging and testing.
1979 06009e27 Iustin Pop

1980 06009e27 Iustin Pop
  Parameters:
1981 aeb6ba44 Dato Simó
    - duration: the time to sleep, in seconds
1982 06009e27 Iustin Pop
    - on_master: if true, sleep on the master
1983 06009e27 Iustin Pop
    - on_nodes: list of nodes in which to sleep
1984 06009e27 Iustin Pop

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

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

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

1995 06009e27 Iustin Pop
  """
1996 60dd1473 Iustin Pop
  OP_DSC_FIELD = "duration"
1997 65e183af Michael Hanselmann
  OP_PARAMS = [
1998 b795a775 Michael Hanselmann
    ("duration", ht.NoDefault, ht.TNumber, None),
1999 197b323b Michael Hanselmann
    ("on_master", True, ht.TBool, None),
2000 197b323b Michael Hanselmann
    ("on_nodes", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
2001 2c9fa1ff Iustin Pop
    ("repeat", 0, ht.TNonNegativeInt, None),
2002 65e183af Michael Hanselmann
    ]
2003 d61df03e Iustin Pop
2004 8bc17ebb Iustin Pop
  def OP_DSC_FORMATTER(self, value): # pylint: disable=C0103,R0201
2005 8bc17ebb Iustin Pop
    """Custom formatter for duration.
2006 8bc17ebb Iustin Pop

2007 8bc17ebb Iustin Pop
    """
2008 8bc17ebb Iustin Pop
    try:
2009 8bc17ebb Iustin Pop
      v = float(value)
2010 8bc17ebb Iustin Pop
    except TypeError:
2011 8bc17ebb Iustin Pop
      v = value
2012 8bc17ebb Iustin Pop
    return str(v)
2013 8bc17ebb Iustin Pop
2014 d61df03e Iustin Pop
2015 d61df03e Iustin Pop
class OpTestAllocator(OpCode):
2016 d61df03e Iustin Pop
  """Allocator framework testing.
2017 d61df03e Iustin Pop

2018 d61df03e Iustin Pop
  This opcode has two modes:
2019 d61df03e Iustin Pop
    - gather and return allocator input for a given mode (allocate new
2020 d61df03e Iustin Pop
      or replace secondary) and a given instance definition (direction
2021 d61df03e Iustin Pop
      'in')
2022 d61df03e Iustin Pop
    - run a selected allocator for a given operation (as above) and
2023 d61df03e Iustin Pop
      return the allocator output (direction 'out')
2024 d61df03e Iustin Pop

2025 d61df03e Iustin Pop
  """
2026 55b7e783 Iustin Pop
  OP_DSC_FIELD = "iallocator"
2027 65e183af Michael Hanselmann
  OP_PARAMS = [
2028 65e183af Michael Hanselmann
    ("direction", ht.NoDefault,
2029 197b323b Michael Hanselmann
     ht.TElemOf(constants.VALID_IALLOCATOR_DIRECTIONS), None),
2030 197b323b Michael Hanselmann
    ("mode", ht.NoDefault, ht.TElemOf(constants.VALID_IALLOCATOR_MODES), None),
2031 197b323b Michael Hanselmann
    ("name", ht.NoDefault, ht.TNonEmptyString, None),
2032 ff8067cf Michael Hanselmann
    ("nics", ht.NoDefault,
2033 ff8067cf Michael Hanselmann
     ht.TMaybeListOf(ht.TDictOf(ht.TElemOf([constants.INIC_MAC,
2034 ff8067cf Michael Hanselmann
                                            constants.INIC_IP,
2035 ff8067cf Michael Hanselmann
                                            "bridge"]),
2036 fd9f58fd Iustin Pop
                                ht.TMaybeString)),
2037 ff8067cf Michael Hanselmann
     None),
2038 fd9f58fd Iustin Pop
    ("disks", ht.NoDefault, ht.TMaybe(ht.TList), None),
2039 197b323b Michael Hanselmann
    ("hypervisor", None, ht.TMaybeString, None),
2040 55b7e783 Iustin Pop
    _PIAllocFromDesc(None),
2041 197b323b Michael Hanselmann
    ("tags", ht.EmptyList, ht.TListOf(ht.TNonEmptyString), None),
2042 fd9f58fd Iustin Pop
    ("memory", None, ht.TMaybe(ht.TNonNegativeInt), None),
2043 fd9f58fd Iustin Pop
    ("vcpus", None, ht.TMaybe(ht.TNonNegativeInt), None),
2044 197b323b Michael Hanselmann
    ("os", None, ht.TMaybeString, None),
2045 197b323b Michael Hanselmann
    ("disk_template", None, ht.TMaybeString, None),
2046 ff8067cf Michael Hanselmann
    ("instances", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2047 60152bbe Michael Hanselmann
    ("evac_mode", None,
2048 fd9f58fd Iustin Pop
     ht.TMaybe(ht.TElemOf(constants.IALLOCATOR_NEVAC_MODES)), None),
2049 ff8067cf Michael Hanselmann
    ("target_groups", None, ht.TMaybeListOf(ht.TNonEmptyString), None),
2050 2c9fa1ff Iustin Pop
    ("spindle_use", 1, ht.TNonNegativeInt, None),
2051 2c9fa1ff Iustin Pop
    ("count", 1, ht.TNonNegativeInt, None),
2052 d61df03e Iustin Pop
    ]
2053 363acb1e Iustin Pop
2054 76aef8fc Michael Hanselmann
2055 b469eb4d Iustin Pop
class OpTestJqueue(OpCode):
2056 e58f87a9 Michael Hanselmann
  """Utility opcode to test some aspects of the job queue.
2057 e58f87a9 Michael Hanselmann

2058 e58f87a9 Michael Hanselmann
  """
2059 65e183af Michael Hanselmann
  OP_PARAMS = [
2060 197b323b Michael Hanselmann
    ("notify_waitlock", False, ht.TBool, None),
2061 197b323b Michael Hanselmann
    ("notify_exec", False, ht.TBool, None),
2062 197b323b Michael Hanselmann
    ("log_messages", ht.EmptyList, ht.TListOf(ht.TString), None),
2063 197b323b Michael Hanselmann
    ("fail", False, ht.TBool, None),
2064 e58f87a9 Michael Hanselmann
    ]
2065 e58f87a9 Michael Hanselmann
2066 e58f87a9 Michael Hanselmann
2067 be760ba8 Michael Hanselmann
class OpTestDummy(OpCode):
2068 be760ba8 Michael Hanselmann
  """Utility opcode used by unittests.
2069 be760ba8 Michael Hanselmann

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

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

2140 eaa4c57c Dimitris Aragiorgis
  """
2141 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2142 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2143 eaa4c57c Dimitris Aragiorgis
    _PGroupName,
2144 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2145 6ef37395 Michael Hanselmann
    ("network_mode", ht.NoDefault, ht.TElemOf(constants.NIC_VALID_MODES),
2146 6ef37395 Michael Hanselmann
     "Connectivity mode"),
2147 e1494c96 Iustin Pop
    ("network_link", ht.NoDefault, ht.TString, "Connectivity link"),
2148 6e8091f9 Dimitris Aragiorgis
    ("conflicts_check", True, ht.TBool, "Whether to check for conflicting IPs"),
2149 eaa4c57c Dimitris Aragiorgis
    ]
2150 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2151 eaa4c57c Dimitris Aragiorgis
2152 3c286190 Dimitris Aragiorgis
2153 eaa4c57c Dimitris Aragiorgis
class OpNetworkDisconnect(OpCode):
2154 eaa4c57c Dimitris Aragiorgis
  """Disconnect a Network from a Nodegroup. Produce errors if NICs are
2155 eaa4c57c Dimitris Aragiorgis
     present in the Network unless --no-conficts-check option is passed.
2156 eaa4c57c Dimitris Aragiorgis

2157 eaa4c57c Dimitris Aragiorgis
  """
2158 eaa4c57c Dimitris Aragiorgis
  OP_DSC_FIELD = "network_name"
2159 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2160 eaa4c57c Dimitris Aragiorgis
    _PGroupName,
2161 eaa4c57c Dimitris Aragiorgis
    _PNetworkName,
2162 eaa4c57c Dimitris Aragiorgis
    ]
2163 6e8091f9 Dimitris Aragiorgis
  OP_RESULT = ht.TNone
2164 eaa4c57c Dimitris Aragiorgis
2165 3c286190 Dimitris Aragiorgis
2166 eaa4c57c Dimitris Aragiorgis
class OpNetworkQuery(OpCode):
2167 eaa4c57c Dimitris Aragiorgis
  """Compute the list of networks."""
2168 eaa4c57c Dimitris Aragiorgis
  OP_PARAMS = [
2169 eaa4c57c Dimitris Aragiorgis
    _POutputFields,
2170 8d459129 Michael Hanselmann
    _PUseLocking,
2171 eaa4c57c Dimitris Aragiorgis
    ("names", ht.EmptyList, ht.TListOf(ht.TNonEmptyString),
2172 eaa4c57c Dimitris Aragiorgis
     "Empty list to query all groups, group names otherwise"),
2173 eaa4c57c Dimitris Aragiorgis
    ]
2174 829cfbc5 Dimitris Aragiorgis
  OP_RESULT = _TOldQueryResult
2175 eaa4c57c Dimitris Aragiorgis
2176 eaa4c57c Dimitris Aragiorgis
2177 dbc96028 Michael Hanselmann
def _GetOpList():
2178 dbc96028 Michael Hanselmann
  """Returns list of all defined opcodes.
2179 dbc96028 Michael Hanselmann

2180 dbc96028 Michael Hanselmann
  Does not eliminate duplicates by C{OP_ID}.
2181 dbc96028 Michael Hanselmann

2182 dbc96028 Michael Hanselmann
  """
2183 dbc96028 Michael Hanselmann
  return [v for v in globals().values()
2184 dbc96028 Michael Hanselmann
          if (isinstance(v, type) and issubclass(v, OpCode) and
2185 687c10d9 Iustin Pop
              hasattr(v, "OP_ID") and v is not OpCode)]
2186 dbc96028 Michael Hanselmann
2187 dbc96028 Michael Hanselmann
2188 dbc96028 Michael Hanselmann
OP_MAPPING = dict((v.OP_ID, v) for v in _GetOpList())