Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib / instance.py @ 0973f9ed

History | View | Annotate | Download (138.6 kB)

1 22b7f6f8 Thomas Thrainer
#
2 22b7f6f8 Thomas Thrainer
#
3 22b7f6f8 Thomas Thrainer
4 22b7f6f8 Thomas Thrainer
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Google Inc.
5 22b7f6f8 Thomas Thrainer
#
6 22b7f6f8 Thomas Thrainer
# This program is free software; you can redistribute it and/or modify
7 22b7f6f8 Thomas Thrainer
# it under the terms of the GNU General Public License as published by
8 22b7f6f8 Thomas Thrainer
# the Free Software Foundation; either version 2 of the License, or
9 22b7f6f8 Thomas Thrainer
# (at your option) any later version.
10 22b7f6f8 Thomas Thrainer
#
11 22b7f6f8 Thomas Thrainer
# This program is distributed in the hope that it will be useful, but
12 22b7f6f8 Thomas Thrainer
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 22b7f6f8 Thomas Thrainer
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 22b7f6f8 Thomas Thrainer
# General Public License for more details.
15 22b7f6f8 Thomas Thrainer
#
16 22b7f6f8 Thomas Thrainer
# You should have received a copy of the GNU General Public License
17 22b7f6f8 Thomas Thrainer
# along with this program; if not, write to the Free Software
18 22b7f6f8 Thomas Thrainer
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 22b7f6f8 Thomas Thrainer
# 02110-1301, USA.
20 22b7f6f8 Thomas Thrainer
21 22b7f6f8 Thomas Thrainer
22 22b7f6f8 Thomas Thrainer
"""Logical units dealing with instances."""
23 22b7f6f8 Thomas Thrainer
24 22b7f6f8 Thomas Thrainer
import OpenSSL
25 22b7f6f8 Thomas Thrainer
import copy
26 22b7f6f8 Thomas Thrainer
import logging
27 22b7f6f8 Thomas Thrainer
import os
28 22b7f6f8 Thomas Thrainer
29 22b7f6f8 Thomas Thrainer
from ganeti import compat
30 22b7f6f8 Thomas Thrainer
from ganeti import constants
31 22b7f6f8 Thomas Thrainer
from ganeti import errors
32 22b7f6f8 Thomas Thrainer
from ganeti import ht
33 22b7f6f8 Thomas Thrainer
from ganeti import hypervisor
34 22b7f6f8 Thomas Thrainer
from ganeti import locking
35 22b7f6f8 Thomas Thrainer
from ganeti.masterd import iallocator
36 22b7f6f8 Thomas Thrainer
from ganeti import masterd
37 22b7f6f8 Thomas Thrainer
from ganeti import netutils
38 22b7f6f8 Thomas Thrainer
from ganeti import objects
39 22b7f6f8 Thomas Thrainer
from ganeti import opcodes
40 22b7f6f8 Thomas Thrainer
from ganeti import pathutils
41 22b7f6f8 Thomas Thrainer
from ganeti import rpc
42 22b7f6f8 Thomas Thrainer
from ganeti import utils
43 22b7f6f8 Thomas Thrainer
44 8aa8f6b1 Thomas Thrainer
from ganeti.cmdlib.base import NoHooksLU, LogicalUnit, ResultWithJobs
45 22b7f6f8 Thomas Thrainer
46 13f6af81 Thomas Thrainer
from ganeti.cmdlib.common import INSTANCE_DOWN, \
47 5eacbcae Thomas Thrainer
  INSTANCE_NOT_RUNNING, CAN_CHANGE_INSTANCE_OFFLINE, CheckNodeOnline, \
48 5eacbcae Thomas Thrainer
  ShareAll, GetDefaultIAllocator, CheckInstanceNodeGroups, \
49 5eacbcae Thomas Thrainer
  LoadNodeEvacResult, CheckIAllocatorOrNode, CheckParamsNotGlobal, \
50 5eacbcae Thomas Thrainer
  IsExclusiveStorageEnabledNode, CheckHVParams, CheckOSParams, \
51 5eacbcae Thomas Thrainer
  AnnotateDiskParams, GetUpdatedParams, ExpandInstanceName, \
52 5eacbcae Thomas Thrainer
  ComputeIPolicySpecViolation, CheckInstanceState, ExpandNodeName
53 5eacbcae Thomas Thrainer
from ganeti.cmdlib.instance_storage import CreateDisks, \
54 a365b47f Bernardo Dal Seno
  CheckNodesFreeDiskPerVG, WipeDisks, WipeOrCleanupDisks, WaitForSync, \
55 5eacbcae Thomas Thrainer
  IsExclusiveStorageEnabledNodeName, CreateSingleBlockDev, ComputeDisks, \
56 5eacbcae Thomas Thrainer
  CheckRADOSFreeSpace, ComputeDiskSizePerVG, GenerateDiskTemplate, \
57 a365b47f Bernardo Dal Seno
  StartInstanceDisks, ShutdownInstanceDisks, AssembleInstanceDisks
58 5eacbcae Thomas Thrainer
from ganeti.cmdlib.instance_utils import BuildInstanceHookEnvByObject, \
59 5eacbcae Thomas Thrainer
  GetClusterDomainSecret, BuildInstanceHookEnv, NICListToTuple, \
60 5eacbcae Thomas Thrainer
  NICToTuple, CheckNodeNotDrained, RemoveInstance, CopyLockList, \
61 5eacbcae Thomas Thrainer
  ReleaseLocks, CheckNodeVmCapable, CheckTargetNodeIPolicy, \
62 5eacbcae Thomas Thrainer
  GetInstanceInfoText, RemoveDisks, CheckNodeFreeMemory, \
63 5eacbcae Thomas Thrainer
  CheckInstanceBridgesExist, CheckNicsBridgesExist, CheckNodeHasOS
64 22b7f6f8 Thomas Thrainer
65 22b7f6f8 Thomas Thrainer
import ganeti.masterd.instance
66 22b7f6f8 Thomas Thrainer
67 22b7f6f8 Thomas Thrainer
68 5eacbcae Thomas Thrainer
#: Type description for changes as returned by L{_ApplyContainerMods}'s
69 22b7f6f8 Thomas Thrainer
#: callbacks
70 22b7f6f8 Thomas Thrainer
_TApplyContModsCbChanges = \
71 22b7f6f8 Thomas Thrainer
  ht.TMaybeListOf(ht.TAnd(ht.TIsLength(2), ht.TItems([
72 22b7f6f8 Thomas Thrainer
    ht.TNonEmptyString,
73 22b7f6f8 Thomas Thrainer
    ht.TAny,
74 22b7f6f8 Thomas Thrainer
    ])))
75 22b7f6f8 Thomas Thrainer
76 22b7f6f8 Thomas Thrainer
77 22b7f6f8 Thomas Thrainer
def _CheckHostnameSane(lu, name):
78 22b7f6f8 Thomas Thrainer
  """Ensures that a given hostname resolves to a 'sane' name.
79 22b7f6f8 Thomas Thrainer

80 22b7f6f8 Thomas Thrainer
  The given name is required to be a prefix of the resolved hostname,
81 22b7f6f8 Thomas Thrainer
  to prevent accidental mismatches.
82 22b7f6f8 Thomas Thrainer

83 22b7f6f8 Thomas Thrainer
  @param lu: the logical unit on behalf of which we're checking
84 22b7f6f8 Thomas Thrainer
  @param name: the name we should resolve and check
85 22b7f6f8 Thomas Thrainer
  @return: the resolved hostname object
86 22b7f6f8 Thomas Thrainer

87 22b7f6f8 Thomas Thrainer
  """
88 22b7f6f8 Thomas Thrainer
  hostname = netutils.GetHostname(name=name)
89 22b7f6f8 Thomas Thrainer
  if hostname.name != name:
90 22b7f6f8 Thomas Thrainer
    lu.LogInfo("Resolved given name '%s' to '%s'", name, hostname.name)
91 22b7f6f8 Thomas Thrainer
  if not utils.MatchNameComponent(name, [hostname.name]):
92 22b7f6f8 Thomas Thrainer
    raise errors.OpPrereqError(("Resolved hostname '%s' does not look the"
93 22b7f6f8 Thomas Thrainer
                                " same as given hostname '%s'") %
94 22b7f6f8 Thomas Thrainer
                               (hostname.name, name), errors.ECODE_INVAL)
95 22b7f6f8 Thomas Thrainer
  return hostname
96 22b7f6f8 Thomas Thrainer
97 22b7f6f8 Thomas Thrainer
98 22b7f6f8 Thomas Thrainer
def _CheckOpportunisticLocking(op):
99 22b7f6f8 Thomas Thrainer
  """Generate error if opportunistic locking is not possible.
100 22b7f6f8 Thomas Thrainer

101 22b7f6f8 Thomas Thrainer
  """
102 22b7f6f8 Thomas Thrainer
  if op.opportunistic_locking and not op.iallocator:
103 22b7f6f8 Thomas Thrainer
    raise errors.OpPrereqError("Opportunistic locking is only available in"
104 22b7f6f8 Thomas Thrainer
                               " combination with an instance allocator",
105 22b7f6f8 Thomas Thrainer
                               errors.ECODE_INVAL)
106 22b7f6f8 Thomas Thrainer
107 22b7f6f8 Thomas Thrainer
108 22b7f6f8 Thomas Thrainer
def _CreateInstanceAllocRequest(op, disks, nics, beparams, node_whitelist):
109 22b7f6f8 Thomas Thrainer
  """Wrapper around IAReqInstanceAlloc.
110 22b7f6f8 Thomas Thrainer

111 22b7f6f8 Thomas Thrainer
  @param op: The instance opcode
112 22b7f6f8 Thomas Thrainer
  @param disks: The computed disks
113 22b7f6f8 Thomas Thrainer
  @param nics: The computed nics
114 22b7f6f8 Thomas Thrainer
  @param beparams: The full filled beparams
115 22b7f6f8 Thomas Thrainer
  @param node_whitelist: List of nodes which should appear as online to the
116 22b7f6f8 Thomas Thrainer
    allocator (unless the node is already marked offline)
117 22b7f6f8 Thomas Thrainer

118 22b7f6f8 Thomas Thrainer
  @returns: A filled L{iallocator.IAReqInstanceAlloc}
119 22b7f6f8 Thomas Thrainer

120 22b7f6f8 Thomas Thrainer
  """
121 22b7f6f8 Thomas Thrainer
  spindle_use = beparams[constants.BE_SPINDLE_USE]
122 22b7f6f8 Thomas Thrainer
  return iallocator.IAReqInstanceAlloc(name=op.instance_name,
123 22b7f6f8 Thomas Thrainer
                                       disk_template=op.disk_template,
124 22b7f6f8 Thomas Thrainer
                                       tags=op.tags,
125 22b7f6f8 Thomas Thrainer
                                       os=op.os_type,
126 22b7f6f8 Thomas Thrainer
                                       vcpus=beparams[constants.BE_VCPUS],
127 22b7f6f8 Thomas Thrainer
                                       memory=beparams[constants.BE_MAXMEM],
128 22b7f6f8 Thomas Thrainer
                                       spindle_use=spindle_use,
129 22b7f6f8 Thomas Thrainer
                                       disks=disks,
130 22b7f6f8 Thomas Thrainer
                                       nics=[n.ToDict() for n in nics],
131 22b7f6f8 Thomas Thrainer
                                       hypervisor=op.hypervisor,
132 22b7f6f8 Thomas Thrainer
                                       node_whitelist=node_whitelist)
133 22b7f6f8 Thomas Thrainer
134 22b7f6f8 Thomas Thrainer
135 22b7f6f8 Thomas Thrainer
def _ComputeFullBeParams(op, cluster):
136 22b7f6f8 Thomas Thrainer
  """Computes the full beparams.
137 22b7f6f8 Thomas Thrainer

138 22b7f6f8 Thomas Thrainer
  @param op: The instance opcode
139 22b7f6f8 Thomas Thrainer
  @param cluster: The cluster config object
140 22b7f6f8 Thomas Thrainer

141 22b7f6f8 Thomas Thrainer
  @return: The fully filled beparams
142 22b7f6f8 Thomas Thrainer

143 22b7f6f8 Thomas Thrainer
  """
144 22b7f6f8 Thomas Thrainer
  default_beparams = cluster.beparams[constants.PP_DEFAULT]
145 22b7f6f8 Thomas Thrainer
  for param, value in op.beparams.iteritems():
146 22b7f6f8 Thomas Thrainer
    if value == constants.VALUE_AUTO:
147 22b7f6f8 Thomas Thrainer
      op.beparams[param] = default_beparams[param]
148 22b7f6f8 Thomas Thrainer
  objects.UpgradeBeParams(op.beparams)
149 22b7f6f8 Thomas Thrainer
  utils.ForceDictType(op.beparams, constants.BES_PARAMETER_TYPES)
150 22b7f6f8 Thomas Thrainer
  return cluster.SimpleFillBE(op.beparams)
151 22b7f6f8 Thomas Thrainer
152 22b7f6f8 Thomas Thrainer
153 22b7f6f8 Thomas Thrainer
def _ComputeNics(op, cluster, default_ip, cfg, ec_id):
154 22b7f6f8 Thomas Thrainer
  """Computes the nics.
155 22b7f6f8 Thomas Thrainer

156 22b7f6f8 Thomas Thrainer
  @param op: The instance opcode
157 22b7f6f8 Thomas Thrainer
  @param cluster: Cluster configuration object
158 22b7f6f8 Thomas Thrainer
  @param default_ip: The default ip to assign
159 22b7f6f8 Thomas Thrainer
  @param cfg: An instance of the configuration object
160 22b7f6f8 Thomas Thrainer
  @param ec_id: Execution context ID
161 22b7f6f8 Thomas Thrainer

162 22b7f6f8 Thomas Thrainer
  @returns: The build up nics
163 22b7f6f8 Thomas Thrainer

164 22b7f6f8 Thomas Thrainer
  """
165 22b7f6f8 Thomas Thrainer
  nics = []
166 22b7f6f8 Thomas Thrainer
  for nic in op.nics:
167 22b7f6f8 Thomas Thrainer
    nic_mode_req = nic.get(constants.INIC_MODE, None)
168 22b7f6f8 Thomas Thrainer
    nic_mode = nic_mode_req
169 22b7f6f8 Thomas Thrainer
    if nic_mode is None or nic_mode == constants.VALUE_AUTO:
170 22b7f6f8 Thomas Thrainer
      nic_mode = cluster.nicparams[constants.PP_DEFAULT][constants.NIC_MODE]
171 22b7f6f8 Thomas Thrainer
172 22b7f6f8 Thomas Thrainer
    net = nic.get(constants.INIC_NETWORK, None)
173 22b7f6f8 Thomas Thrainer
    link = nic.get(constants.NIC_LINK, None)
174 22b7f6f8 Thomas Thrainer
    ip = nic.get(constants.INIC_IP, None)
175 22b7f6f8 Thomas Thrainer
176 22b7f6f8 Thomas Thrainer
    if net is None or net.lower() == constants.VALUE_NONE:
177 22b7f6f8 Thomas Thrainer
      net = None
178 22b7f6f8 Thomas Thrainer
    else:
179 22b7f6f8 Thomas Thrainer
      if nic_mode_req is not None or link is not None:
180 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("If network is given, no mode or link"
181 22b7f6f8 Thomas Thrainer
                                   " is allowed to be passed",
182 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
183 22b7f6f8 Thomas Thrainer
184 22b7f6f8 Thomas Thrainer
    # ip validity checks
185 22b7f6f8 Thomas Thrainer
    if ip is None or ip.lower() == constants.VALUE_NONE:
186 22b7f6f8 Thomas Thrainer
      nic_ip = None
187 22b7f6f8 Thomas Thrainer
    elif ip.lower() == constants.VALUE_AUTO:
188 22b7f6f8 Thomas Thrainer
      if not op.name_check:
189 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("IP address set to auto but name checks"
190 22b7f6f8 Thomas Thrainer
                                   " have been skipped",
191 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
192 22b7f6f8 Thomas Thrainer
      nic_ip = default_ip
193 22b7f6f8 Thomas Thrainer
    else:
194 22b7f6f8 Thomas Thrainer
      # We defer pool operations until later, so that the iallocator has
195 22b7f6f8 Thomas Thrainer
      # filled in the instance's node(s) dimara
196 22b7f6f8 Thomas Thrainer
      if ip.lower() == constants.NIC_IP_POOL:
197 22b7f6f8 Thomas Thrainer
        if net is None:
198 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("if ip=pool, parameter network"
199 22b7f6f8 Thomas Thrainer
                                     " must be passed too",
200 22b7f6f8 Thomas Thrainer
                                     errors.ECODE_INVAL)
201 22b7f6f8 Thomas Thrainer
202 22b7f6f8 Thomas Thrainer
      elif not netutils.IPAddress.IsValid(ip):
203 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Invalid IP address '%s'" % ip,
204 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
205 22b7f6f8 Thomas Thrainer
206 22b7f6f8 Thomas Thrainer
      nic_ip = ip
207 22b7f6f8 Thomas Thrainer
208 22b7f6f8 Thomas Thrainer
    # TODO: check the ip address for uniqueness
209 22b7f6f8 Thomas Thrainer
    if nic_mode == constants.NIC_MODE_ROUTED and not nic_ip:
210 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Routed nic mode requires an ip address",
211 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
212 22b7f6f8 Thomas Thrainer
213 22b7f6f8 Thomas Thrainer
    # MAC address verification
214 22b7f6f8 Thomas Thrainer
    mac = nic.get(constants.INIC_MAC, constants.VALUE_AUTO)
215 22b7f6f8 Thomas Thrainer
    if mac not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
216 22b7f6f8 Thomas Thrainer
      mac = utils.NormalizeAndValidateMac(mac)
217 22b7f6f8 Thomas Thrainer
218 22b7f6f8 Thomas Thrainer
      try:
219 22b7f6f8 Thomas Thrainer
        # TODO: We need to factor this out
220 22b7f6f8 Thomas Thrainer
        cfg.ReserveMAC(mac, ec_id)
221 22b7f6f8 Thomas Thrainer
      except errors.ReservationError:
222 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("MAC address %s already in use"
223 22b7f6f8 Thomas Thrainer
                                   " in cluster" % mac,
224 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_NOTUNIQUE)
225 22b7f6f8 Thomas Thrainer
226 22b7f6f8 Thomas Thrainer
    #  Build nic parameters
227 22b7f6f8 Thomas Thrainer
    nicparams = {}
228 22b7f6f8 Thomas Thrainer
    if nic_mode_req:
229 22b7f6f8 Thomas Thrainer
      nicparams[constants.NIC_MODE] = nic_mode
230 22b7f6f8 Thomas Thrainer
    if link:
231 22b7f6f8 Thomas Thrainer
      nicparams[constants.NIC_LINK] = link
232 22b7f6f8 Thomas Thrainer
233 22b7f6f8 Thomas Thrainer
    check_params = cluster.SimpleFillNIC(nicparams)
234 22b7f6f8 Thomas Thrainer
    objects.NIC.CheckParameterSyntax(check_params)
235 22b7f6f8 Thomas Thrainer
    net_uuid = cfg.LookupNetwork(net)
236 22b7f6f8 Thomas Thrainer
    name = nic.get(constants.INIC_NAME, None)
237 22b7f6f8 Thomas Thrainer
    if name is not None and name.lower() == constants.VALUE_NONE:
238 22b7f6f8 Thomas Thrainer
      name = None
239 22b7f6f8 Thomas Thrainer
    nic_obj = objects.NIC(mac=mac, ip=nic_ip, name=name,
240 22b7f6f8 Thomas Thrainer
                          network=net_uuid, nicparams=nicparams)
241 22b7f6f8 Thomas Thrainer
    nic_obj.uuid = cfg.GenerateUniqueID(ec_id)
242 22b7f6f8 Thomas Thrainer
    nics.append(nic_obj)
243 22b7f6f8 Thomas Thrainer
244 22b7f6f8 Thomas Thrainer
  return nics
245 22b7f6f8 Thomas Thrainer
246 22b7f6f8 Thomas Thrainer
247 22b7f6f8 Thomas Thrainer
def _CheckForConflictingIp(lu, ip, node):
248 22b7f6f8 Thomas Thrainer
  """In case of conflicting IP address raise error.
249 22b7f6f8 Thomas Thrainer

250 22b7f6f8 Thomas Thrainer
  @type ip: string
251 22b7f6f8 Thomas Thrainer
  @param ip: IP address
252 22b7f6f8 Thomas Thrainer
  @type node: string
253 22b7f6f8 Thomas Thrainer
  @param node: node name
254 22b7f6f8 Thomas Thrainer

255 22b7f6f8 Thomas Thrainer
  """
256 22b7f6f8 Thomas Thrainer
  (conf_net, _) = lu.cfg.CheckIPInNodeGroup(ip, node)
257 22b7f6f8 Thomas Thrainer
  if conf_net is not None:
258 22b7f6f8 Thomas Thrainer
    raise errors.OpPrereqError(("The requested IP address (%s) belongs to"
259 22b7f6f8 Thomas Thrainer
                                " network %s, but the target NIC does not." %
260 22b7f6f8 Thomas Thrainer
                                (ip, conf_net)),
261 22b7f6f8 Thomas Thrainer
                               errors.ECODE_STATE)
262 22b7f6f8 Thomas Thrainer
263 22b7f6f8 Thomas Thrainer
  return (None, None)
264 22b7f6f8 Thomas Thrainer
265 22b7f6f8 Thomas Thrainer
266 22b7f6f8 Thomas Thrainer
def _ComputeIPolicyInstanceSpecViolation(
267 22b7f6f8 Thomas Thrainer
  ipolicy, instance_spec, disk_template,
268 5eacbcae Thomas Thrainer
  _compute_fn=ComputeIPolicySpecViolation):
269 22b7f6f8 Thomas Thrainer
  """Compute if instance specs meets the specs of ipolicy.
270 22b7f6f8 Thomas Thrainer

271 22b7f6f8 Thomas Thrainer
  @type ipolicy: dict
272 22b7f6f8 Thomas Thrainer
  @param ipolicy: The ipolicy to verify against
273 22b7f6f8 Thomas Thrainer
  @param instance_spec: dict
274 22b7f6f8 Thomas Thrainer
  @param instance_spec: The instance spec to verify
275 22b7f6f8 Thomas Thrainer
  @type disk_template: string
276 22b7f6f8 Thomas Thrainer
  @param disk_template: the disk template of the instance
277 22b7f6f8 Thomas Thrainer
  @param _compute_fn: The function to verify ipolicy (unittest only)
278 5eacbcae Thomas Thrainer
  @see: L{ComputeIPolicySpecViolation}
279 22b7f6f8 Thomas Thrainer

280 22b7f6f8 Thomas Thrainer
  """
281 22b7f6f8 Thomas Thrainer
  mem_size = instance_spec.get(constants.ISPEC_MEM_SIZE, None)
282 22b7f6f8 Thomas Thrainer
  cpu_count = instance_spec.get(constants.ISPEC_CPU_COUNT, None)
283 22b7f6f8 Thomas Thrainer
  disk_count = instance_spec.get(constants.ISPEC_DISK_COUNT, 0)
284 22b7f6f8 Thomas Thrainer
  disk_sizes = instance_spec.get(constants.ISPEC_DISK_SIZE, [])
285 22b7f6f8 Thomas Thrainer
  nic_count = instance_spec.get(constants.ISPEC_NIC_COUNT, 0)
286 22b7f6f8 Thomas Thrainer
  spindle_use = instance_spec.get(constants.ISPEC_SPINDLE_USE, None)
287 22b7f6f8 Thomas Thrainer
288 22b7f6f8 Thomas Thrainer
  return _compute_fn(ipolicy, mem_size, cpu_count, disk_count, nic_count,
289 22b7f6f8 Thomas Thrainer
                     disk_sizes, spindle_use, disk_template)
290 22b7f6f8 Thomas Thrainer
291 22b7f6f8 Thomas Thrainer
292 22b7f6f8 Thomas Thrainer
def _CheckOSVariant(os_obj, name):
293 22b7f6f8 Thomas Thrainer
  """Check whether an OS name conforms to the os variants specification.
294 22b7f6f8 Thomas Thrainer

295 22b7f6f8 Thomas Thrainer
  @type os_obj: L{objects.OS}
296 22b7f6f8 Thomas Thrainer
  @param os_obj: OS object to check
297 22b7f6f8 Thomas Thrainer
  @type name: string
298 22b7f6f8 Thomas Thrainer
  @param name: OS name passed by the user, to check for validity
299 22b7f6f8 Thomas Thrainer

300 22b7f6f8 Thomas Thrainer
  """
301 22b7f6f8 Thomas Thrainer
  variant = objects.OS.GetVariant(name)
302 22b7f6f8 Thomas Thrainer
  if not os_obj.supported_variants:
303 22b7f6f8 Thomas Thrainer
    if variant:
304 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("OS '%s' doesn't support variants ('%s'"
305 22b7f6f8 Thomas Thrainer
                                 " passed)" % (os_obj.name, variant),
306 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
307 22b7f6f8 Thomas Thrainer
    return
308 22b7f6f8 Thomas Thrainer
  if not variant:
309 22b7f6f8 Thomas Thrainer
    raise errors.OpPrereqError("OS name must include a variant",
310 22b7f6f8 Thomas Thrainer
                               errors.ECODE_INVAL)
311 22b7f6f8 Thomas Thrainer
312 22b7f6f8 Thomas Thrainer
  if variant not in os_obj.supported_variants:
313 22b7f6f8 Thomas Thrainer
    raise errors.OpPrereqError("Unsupported OS variant", errors.ECODE_INVAL)
314 22b7f6f8 Thomas Thrainer
315 22b7f6f8 Thomas Thrainer
316 22b7f6f8 Thomas Thrainer
class LUInstanceCreate(LogicalUnit):
317 22b7f6f8 Thomas Thrainer
  """Create an instance.
318 22b7f6f8 Thomas Thrainer

319 22b7f6f8 Thomas Thrainer
  """
320 22b7f6f8 Thomas Thrainer
  HPATH = "instance-add"
321 22b7f6f8 Thomas Thrainer
  HTYPE = constants.HTYPE_INSTANCE
322 22b7f6f8 Thomas Thrainer
  REQ_BGL = False
323 22b7f6f8 Thomas Thrainer
324 22b7f6f8 Thomas Thrainer
  def CheckArguments(self):
325 22b7f6f8 Thomas Thrainer
    """Check arguments.
326 22b7f6f8 Thomas Thrainer

327 22b7f6f8 Thomas Thrainer
    """
328 22b7f6f8 Thomas Thrainer
    # do not require name_check to ease forward/backward compatibility
329 22b7f6f8 Thomas Thrainer
    # for tools
330 22b7f6f8 Thomas Thrainer
    if self.op.no_install and self.op.start:
331 22b7f6f8 Thomas Thrainer
      self.LogInfo("No-installation mode selected, disabling startup")
332 22b7f6f8 Thomas Thrainer
      self.op.start = False
333 22b7f6f8 Thomas Thrainer
    # validate/normalize the instance name
334 22b7f6f8 Thomas Thrainer
    self.op.instance_name = \
335 22b7f6f8 Thomas Thrainer
      netutils.Hostname.GetNormalizedName(self.op.instance_name)
336 22b7f6f8 Thomas Thrainer
337 22b7f6f8 Thomas Thrainer
    if self.op.ip_check and not self.op.name_check:
338 22b7f6f8 Thomas Thrainer
      # TODO: make the ip check more flexible and not depend on the name check
339 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Cannot do IP address check without a name"
340 22b7f6f8 Thomas Thrainer
                                 " check", errors.ECODE_INVAL)
341 22b7f6f8 Thomas Thrainer
342 22b7f6f8 Thomas Thrainer
    # check nics' parameter names
343 22b7f6f8 Thomas Thrainer
    for nic in self.op.nics:
344 22b7f6f8 Thomas Thrainer
      utils.ForceDictType(nic, constants.INIC_PARAMS_TYPES)
345 22b7f6f8 Thomas Thrainer
    # check that NIC's parameters names are unique and valid
346 22b7f6f8 Thomas Thrainer
    utils.ValidateDeviceNames("NIC", self.op.nics)
347 22b7f6f8 Thomas Thrainer
348 22b7f6f8 Thomas Thrainer
    # check that disk's names are unique and valid
349 22b7f6f8 Thomas Thrainer
    utils.ValidateDeviceNames("disk", self.op.disks)
350 22b7f6f8 Thomas Thrainer
351 22b7f6f8 Thomas Thrainer
    cluster = self.cfg.GetClusterInfo()
352 22b7f6f8 Thomas Thrainer
    if not self.op.disk_template in cluster.enabled_disk_templates:
353 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Cannot create an instance with disk template"
354 22b7f6f8 Thomas Thrainer
                                 " '%s', because it is not enabled in the"
355 22b7f6f8 Thomas Thrainer
                                 " cluster. Enabled disk templates are: %s." %
356 22b7f6f8 Thomas Thrainer
                                 (self.op.disk_template,
357 22b7f6f8 Thomas Thrainer
                                  ",".join(cluster.enabled_disk_templates)))
358 22b7f6f8 Thomas Thrainer
359 22b7f6f8 Thomas Thrainer
    # check disks. parameter names and consistent adopt/no-adopt strategy
360 22b7f6f8 Thomas Thrainer
    has_adopt = has_no_adopt = False
361 22b7f6f8 Thomas Thrainer
    for disk in self.op.disks:
362 22b7f6f8 Thomas Thrainer
      if self.op.disk_template != constants.DT_EXT:
363 22b7f6f8 Thomas Thrainer
        utils.ForceDictType(disk, constants.IDISK_PARAMS_TYPES)
364 22b7f6f8 Thomas Thrainer
      if constants.IDISK_ADOPT in disk:
365 22b7f6f8 Thomas Thrainer
        has_adopt = True
366 22b7f6f8 Thomas Thrainer
      else:
367 22b7f6f8 Thomas Thrainer
        has_no_adopt = True
368 22b7f6f8 Thomas Thrainer
    if has_adopt and has_no_adopt:
369 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Either all disks are adopted or none is",
370 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
371 22b7f6f8 Thomas Thrainer
    if has_adopt:
372 22b7f6f8 Thomas Thrainer
      if self.op.disk_template not in constants.DTS_MAY_ADOPT:
373 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Disk adoption is not supported for the"
374 22b7f6f8 Thomas Thrainer
                                   " '%s' disk template" %
375 22b7f6f8 Thomas Thrainer
                                   self.op.disk_template,
376 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
377 22b7f6f8 Thomas Thrainer
      if self.op.iallocator is not None:
378 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Disk adoption not allowed with an"
379 22b7f6f8 Thomas Thrainer
                                   " iallocator script", errors.ECODE_INVAL)
380 22b7f6f8 Thomas Thrainer
      if self.op.mode == constants.INSTANCE_IMPORT:
381 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Disk adoption not allowed for"
382 22b7f6f8 Thomas Thrainer
                                   " instance import", errors.ECODE_INVAL)
383 22b7f6f8 Thomas Thrainer
    else:
384 22b7f6f8 Thomas Thrainer
      if self.op.disk_template in constants.DTS_MUST_ADOPT:
385 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Disk template %s requires disk adoption,"
386 22b7f6f8 Thomas Thrainer
                                   " but no 'adopt' parameter given" %
387 22b7f6f8 Thomas Thrainer
                                   self.op.disk_template,
388 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
389 22b7f6f8 Thomas Thrainer
390 22b7f6f8 Thomas Thrainer
    self.adopt_disks = has_adopt
391 22b7f6f8 Thomas Thrainer
392 22b7f6f8 Thomas Thrainer
    # instance name verification
393 22b7f6f8 Thomas Thrainer
    if self.op.name_check:
394 22b7f6f8 Thomas Thrainer
      self.hostname1 = _CheckHostnameSane(self, self.op.instance_name)
395 22b7f6f8 Thomas Thrainer
      self.op.instance_name = self.hostname1.name
396 22b7f6f8 Thomas Thrainer
      # used in CheckPrereq for ip ping check
397 22b7f6f8 Thomas Thrainer
      self.check_ip = self.hostname1.ip
398 22b7f6f8 Thomas Thrainer
    else:
399 22b7f6f8 Thomas Thrainer
      self.check_ip = None
400 22b7f6f8 Thomas Thrainer
401 22b7f6f8 Thomas Thrainer
    # file storage checks
402 22b7f6f8 Thomas Thrainer
    if (self.op.file_driver and
403 22b7f6f8 Thomas Thrainer
        not self.op.file_driver in constants.FILE_DRIVER):
404 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Invalid file driver name '%s'" %
405 22b7f6f8 Thomas Thrainer
                                 self.op.file_driver, errors.ECODE_INVAL)
406 22b7f6f8 Thomas Thrainer
407 ff34fb97 Michele Tartara
    # set default file_driver if unset and required
408 ff34fb97 Michele Tartara
    if (not self.op.file_driver and
409 ff34fb97 Michele Tartara
        self.op.disk_template in [constants.DT_FILE,
410 ff34fb97 Michele Tartara
                                  constants.DT_SHARED_FILE]):
411 77b0d264 Michele Tartara
      self.op.file_driver = constants.FD_DEFAULT
412 ff34fb97 Michele Tartara
413 22b7f6f8 Thomas Thrainer
    if self.op.disk_template == constants.DT_FILE:
414 22b7f6f8 Thomas Thrainer
      opcodes.RequireFileStorage()
415 22b7f6f8 Thomas Thrainer
    elif self.op.disk_template == constants.DT_SHARED_FILE:
416 22b7f6f8 Thomas Thrainer
      opcodes.RequireSharedFileStorage()
417 22b7f6f8 Thomas Thrainer
418 22b7f6f8 Thomas Thrainer
    ### Node/iallocator related checks
419 5eacbcae Thomas Thrainer
    CheckIAllocatorOrNode(self, "iallocator", "pnode")
420 22b7f6f8 Thomas Thrainer
421 22b7f6f8 Thomas Thrainer
    if self.op.pnode is not None:
422 22b7f6f8 Thomas Thrainer
      if self.op.disk_template in constants.DTS_INT_MIRROR:
423 22b7f6f8 Thomas Thrainer
        if self.op.snode is None:
424 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("The networked disk templates need"
425 22b7f6f8 Thomas Thrainer
                                     " a mirror node", errors.ECODE_INVAL)
426 22b7f6f8 Thomas Thrainer
      elif self.op.snode:
427 22b7f6f8 Thomas Thrainer
        self.LogWarning("Secondary node will be ignored on non-mirrored disk"
428 22b7f6f8 Thomas Thrainer
                        " template")
429 22b7f6f8 Thomas Thrainer
        self.op.snode = None
430 22b7f6f8 Thomas Thrainer
431 22b7f6f8 Thomas Thrainer
    _CheckOpportunisticLocking(self.op)
432 22b7f6f8 Thomas Thrainer
433 5eacbcae Thomas Thrainer
    self._cds = GetClusterDomainSecret()
434 22b7f6f8 Thomas Thrainer
435 22b7f6f8 Thomas Thrainer
    if self.op.mode == constants.INSTANCE_IMPORT:
436 22b7f6f8 Thomas Thrainer
      # On import force_variant must be True, because if we forced it at
437 22b7f6f8 Thomas Thrainer
      # initial install, our only chance when importing it back is that it
438 22b7f6f8 Thomas Thrainer
      # works again!
439 22b7f6f8 Thomas Thrainer
      self.op.force_variant = True
440 22b7f6f8 Thomas Thrainer
441 22b7f6f8 Thomas Thrainer
      if self.op.no_install:
442 22b7f6f8 Thomas Thrainer
        self.LogInfo("No-installation mode has no effect during import")
443 22b7f6f8 Thomas Thrainer
444 22b7f6f8 Thomas Thrainer
    elif self.op.mode == constants.INSTANCE_CREATE:
445 22b7f6f8 Thomas Thrainer
      if self.op.os_type is None:
446 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("No guest OS specified",
447 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
448 22b7f6f8 Thomas Thrainer
      if self.op.os_type in self.cfg.GetClusterInfo().blacklisted_os:
449 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Guest OS '%s' is not allowed for"
450 22b7f6f8 Thomas Thrainer
                                   " installation" % self.op.os_type,
451 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_STATE)
452 22b7f6f8 Thomas Thrainer
      if self.op.disk_template is None:
453 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("No disk template specified",
454 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
455 22b7f6f8 Thomas Thrainer
456 22b7f6f8 Thomas Thrainer
    elif self.op.mode == constants.INSTANCE_REMOTE_IMPORT:
457 22b7f6f8 Thomas Thrainer
      # Check handshake to ensure both clusters have the same domain secret
458 22b7f6f8 Thomas Thrainer
      src_handshake = self.op.source_handshake
459 22b7f6f8 Thomas Thrainer
      if not src_handshake:
460 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Missing source handshake",
461 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
462 22b7f6f8 Thomas Thrainer
463 22b7f6f8 Thomas Thrainer
      errmsg = masterd.instance.CheckRemoteExportHandshake(self._cds,
464 22b7f6f8 Thomas Thrainer
                                                           src_handshake)
465 22b7f6f8 Thomas Thrainer
      if errmsg:
466 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Invalid handshake: %s" % errmsg,
467 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
468 22b7f6f8 Thomas Thrainer
469 22b7f6f8 Thomas Thrainer
      # Load and check source CA
470 22b7f6f8 Thomas Thrainer
      self.source_x509_ca_pem = self.op.source_x509_ca
471 22b7f6f8 Thomas Thrainer
      if not self.source_x509_ca_pem:
472 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Missing source X509 CA",
473 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
474 22b7f6f8 Thomas Thrainer
475 22b7f6f8 Thomas Thrainer
      try:
476 22b7f6f8 Thomas Thrainer
        (cert, _) = utils.LoadSignedX509Certificate(self.source_x509_ca_pem,
477 22b7f6f8 Thomas Thrainer
                                                    self._cds)
478 22b7f6f8 Thomas Thrainer
      except OpenSSL.crypto.Error, err:
479 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Unable to load source X509 CA (%s)" %
480 22b7f6f8 Thomas Thrainer
                                   (err, ), errors.ECODE_INVAL)
481 22b7f6f8 Thomas Thrainer
482 22b7f6f8 Thomas Thrainer
      (errcode, msg) = utils.VerifyX509Certificate(cert, None, None)
483 22b7f6f8 Thomas Thrainer
      if errcode is not None:
484 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Invalid source X509 CA (%s)" % (msg, ),
485 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
486 22b7f6f8 Thomas Thrainer
487 22b7f6f8 Thomas Thrainer
      self.source_x509_ca = cert
488 22b7f6f8 Thomas Thrainer
489 22b7f6f8 Thomas Thrainer
      src_instance_name = self.op.source_instance_name
490 22b7f6f8 Thomas Thrainer
      if not src_instance_name:
491 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Missing source instance name",
492 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
493 22b7f6f8 Thomas Thrainer
494 22b7f6f8 Thomas Thrainer
      self.source_instance_name = \
495 22b7f6f8 Thomas Thrainer
        netutils.GetHostname(name=src_instance_name).name
496 22b7f6f8 Thomas Thrainer
497 22b7f6f8 Thomas Thrainer
    else:
498 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Invalid instance creation mode %r" %
499 22b7f6f8 Thomas Thrainer
                                 self.op.mode, errors.ECODE_INVAL)
500 22b7f6f8 Thomas Thrainer
501 22b7f6f8 Thomas Thrainer
  def ExpandNames(self):
502 22b7f6f8 Thomas Thrainer
    """ExpandNames for CreateInstance.
503 22b7f6f8 Thomas Thrainer

504 22b7f6f8 Thomas Thrainer
    Figure out the right locks for instance creation.
505 22b7f6f8 Thomas Thrainer

506 22b7f6f8 Thomas Thrainer
    """
507 22b7f6f8 Thomas Thrainer
    self.needed_locks = {}
508 22b7f6f8 Thomas Thrainer
509 22b7f6f8 Thomas Thrainer
    instance_name = self.op.instance_name
510 22b7f6f8 Thomas Thrainer
    # this is just a preventive check, but someone might still add this
511 22b7f6f8 Thomas Thrainer
    # instance in the meantime, and creation will fail at lock-add time
512 22b7f6f8 Thomas Thrainer
    if instance_name in self.cfg.GetInstanceList():
513 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
514 22b7f6f8 Thomas Thrainer
                                 instance_name, errors.ECODE_EXISTS)
515 22b7f6f8 Thomas Thrainer
516 22b7f6f8 Thomas Thrainer
    self.add_locks[locking.LEVEL_INSTANCE] = instance_name
517 22b7f6f8 Thomas Thrainer
518 22b7f6f8 Thomas Thrainer
    if self.op.iallocator:
519 22b7f6f8 Thomas Thrainer
      # TODO: Find a solution to not lock all nodes in the cluster, e.g. by
520 22b7f6f8 Thomas Thrainer
      # specifying a group on instance creation and then selecting nodes from
521 22b7f6f8 Thomas Thrainer
      # that group
522 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
523 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_ALLOC] = locking.ALL_SET
524 22b7f6f8 Thomas Thrainer
525 22b7f6f8 Thomas Thrainer
      if self.op.opportunistic_locking:
526 22b7f6f8 Thomas Thrainer
        self.opportunistic_locks[locking.LEVEL_NODE] = True
527 22b7f6f8 Thomas Thrainer
    else:
528 5eacbcae Thomas Thrainer
      self.op.pnode = ExpandNodeName(self.cfg, self.op.pnode)
529 22b7f6f8 Thomas Thrainer
      nodelist = [self.op.pnode]
530 22b7f6f8 Thomas Thrainer
      if self.op.snode is not None:
531 5eacbcae Thomas Thrainer
        self.op.snode = ExpandNodeName(self.cfg, self.op.snode)
532 22b7f6f8 Thomas Thrainer
        nodelist.append(self.op.snode)
533 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE] = nodelist
534 22b7f6f8 Thomas Thrainer
535 22b7f6f8 Thomas Thrainer
    # in case of import lock the source node too
536 22b7f6f8 Thomas Thrainer
    if self.op.mode == constants.INSTANCE_IMPORT:
537 22b7f6f8 Thomas Thrainer
      src_node = self.op.src_node
538 22b7f6f8 Thomas Thrainer
      src_path = self.op.src_path
539 22b7f6f8 Thomas Thrainer
540 22b7f6f8 Thomas Thrainer
      if src_path is None:
541 22b7f6f8 Thomas Thrainer
        self.op.src_path = src_path = self.op.instance_name
542 22b7f6f8 Thomas Thrainer
543 22b7f6f8 Thomas Thrainer
      if src_node is None:
544 22b7f6f8 Thomas Thrainer
        self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
545 22b7f6f8 Thomas Thrainer
        self.needed_locks[locking.LEVEL_NODE_ALLOC] = locking.ALL_SET
546 22b7f6f8 Thomas Thrainer
        self.op.src_node = None
547 22b7f6f8 Thomas Thrainer
        if os.path.isabs(src_path):
548 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("Importing an instance from a path"
549 22b7f6f8 Thomas Thrainer
                                     " requires a source node option",
550 22b7f6f8 Thomas Thrainer
                                     errors.ECODE_INVAL)
551 22b7f6f8 Thomas Thrainer
      else:
552 5eacbcae Thomas Thrainer
        self.op.src_node = src_node = ExpandNodeName(self.cfg, src_node)
553 22b7f6f8 Thomas Thrainer
        if self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET:
554 22b7f6f8 Thomas Thrainer
          self.needed_locks[locking.LEVEL_NODE].append(src_node)
555 22b7f6f8 Thomas Thrainer
        if not os.path.isabs(src_path):
556 22b7f6f8 Thomas Thrainer
          self.op.src_path = src_path = \
557 22b7f6f8 Thomas Thrainer
            utils.PathJoin(pathutils.EXPORT_DIR, src_path)
558 22b7f6f8 Thomas Thrainer
559 22b7f6f8 Thomas Thrainer
    self.needed_locks[locking.LEVEL_NODE_RES] = \
560 5eacbcae Thomas Thrainer
      CopyLockList(self.needed_locks[locking.LEVEL_NODE])
561 22b7f6f8 Thomas Thrainer
562 8b9887c5 Petr Pudlak
    # Optimistically acquire shared group locks (we're reading the
563 8b9887c5 Petr Pudlak
    # configuration).  We can't just call GetInstanceNodeGroups, because the
564 8b9887c5 Petr Pudlak
    # instance doesn't exist yet. Therefore we lock all node groups of all
565 8b9887c5 Petr Pudlak
    # nodes we have.
566 8b9887c5 Petr Pudlak
    if self.needed_locks[locking.LEVEL_NODE] == locking.ALL_SET:
567 8b9887c5 Petr Pudlak
      # In the case we lock all nodes for opportunistic allocation, we have no
568 8b9887c5 Petr Pudlak
      # choice than to lock all groups, because they're allocated before nodes.
569 8b9887c5 Petr Pudlak
      # This is sad, but true. At least we release all those we don't need in
570 8b9887c5 Petr Pudlak
      # CheckPrereq later.
571 8b9887c5 Petr Pudlak
      self.needed_locks[locking.LEVEL_NODEGROUP] = locking.ALL_SET
572 8b9887c5 Petr Pudlak
    else:
573 8b9887c5 Petr Pudlak
      self.needed_locks[locking.LEVEL_NODEGROUP] = \
574 8b9887c5 Petr Pudlak
        list(self.cfg.GetNodeGroupsFromNodes(
575 8b9887c5 Petr Pudlak
          self.needed_locks[locking.LEVEL_NODE]))
576 8b9887c5 Petr Pudlak
    self.share_locks[locking.LEVEL_NODEGROUP] = 1
577 8b9887c5 Petr Pudlak
578 4289f617 Thomas Thrainer
  def DeclareLocks(self, level):
579 4289f617 Thomas Thrainer
    if level == locking.LEVEL_NODE_RES and \
580 4289f617 Thomas Thrainer
      self.opportunistic_locks[locking.LEVEL_NODE]:
581 4289f617 Thomas Thrainer
      # Even when using opportunistic locking, we require the same set of
582 4289f617 Thomas Thrainer
      # NODE_RES locks as we got NODE locks
583 4289f617 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_RES] = \
584 4289f617 Thomas Thrainer
        self.owned_locks(locking.LEVEL_NODE)
585 4289f617 Thomas Thrainer
586 22b7f6f8 Thomas Thrainer
  def _RunAllocator(self):
587 22b7f6f8 Thomas Thrainer
    """Run the allocator based on input opcode.
588 22b7f6f8 Thomas Thrainer

589 22b7f6f8 Thomas Thrainer
    """
590 22b7f6f8 Thomas Thrainer
    if self.op.opportunistic_locking:
591 22b7f6f8 Thomas Thrainer
      # Only consider nodes for which a lock is held
592 22b7f6f8 Thomas Thrainer
      node_whitelist = list(self.owned_locks(locking.LEVEL_NODE))
593 22b7f6f8 Thomas Thrainer
    else:
594 22b7f6f8 Thomas Thrainer
      node_whitelist = None
595 22b7f6f8 Thomas Thrainer
596 22b7f6f8 Thomas Thrainer
    #TODO Export network to iallocator so that it chooses a pnode
597 22b7f6f8 Thomas Thrainer
    #     in a nodegroup that has the desired network connected to
598 22b7f6f8 Thomas Thrainer
    req = _CreateInstanceAllocRequest(self.op, self.disks,
599 22b7f6f8 Thomas Thrainer
                                      self.nics, self.be_full,
600 22b7f6f8 Thomas Thrainer
                                      node_whitelist)
601 22b7f6f8 Thomas Thrainer
    ial = iallocator.IAllocator(self.cfg, self.rpc, req)
602 22b7f6f8 Thomas Thrainer
603 22b7f6f8 Thomas Thrainer
    ial.Run(self.op.iallocator)
604 22b7f6f8 Thomas Thrainer
605 22b7f6f8 Thomas Thrainer
    if not ial.success:
606 22b7f6f8 Thomas Thrainer
      # When opportunistic locks are used only a temporary failure is generated
607 22b7f6f8 Thomas Thrainer
      if self.op.opportunistic_locking:
608 22b7f6f8 Thomas Thrainer
        ecode = errors.ECODE_TEMP_NORES
609 22b7f6f8 Thomas Thrainer
      else:
610 22b7f6f8 Thomas Thrainer
        ecode = errors.ECODE_NORES
611 22b7f6f8 Thomas Thrainer
612 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Can't compute nodes using"
613 22b7f6f8 Thomas Thrainer
                                 " iallocator '%s': %s" %
614 22b7f6f8 Thomas Thrainer
                                 (self.op.iallocator, ial.info),
615 22b7f6f8 Thomas Thrainer
                                 ecode)
616 22b7f6f8 Thomas Thrainer
617 22b7f6f8 Thomas Thrainer
    self.op.pnode = ial.result[0]
618 22b7f6f8 Thomas Thrainer
    self.LogInfo("Selected nodes for instance %s via iallocator %s: %s",
619 22b7f6f8 Thomas Thrainer
                 self.op.instance_name, self.op.iallocator,
620 22b7f6f8 Thomas Thrainer
                 utils.CommaJoin(ial.result))
621 22b7f6f8 Thomas Thrainer
622 22b7f6f8 Thomas Thrainer
    assert req.RequiredNodes() in (1, 2), "Wrong node count from iallocator"
623 22b7f6f8 Thomas Thrainer
624 22b7f6f8 Thomas Thrainer
    if req.RequiredNodes() == 2:
625 22b7f6f8 Thomas Thrainer
      self.op.snode = ial.result[1]
626 22b7f6f8 Thomas Thrainer
627 22b7f6f8 Thomas Thrainer
  def BuildHooksEnv(self):
628 22b7f6f8 Thomas Thrainer
    """Build hooks env.
629 22b7f6f8 Thomas Thrainer

630 22b7f6f8 Thomas Thrainer
    This runs on master, primary and secondary nodes of the instance.
631 22b7f6f8 Thomas Thrainer

632 22b7f6f8 Thomas Thrainer
    """
633 22b7f6f8 Thomas Thrainer
    env = {
634 22b7f6f8 Thomas Thrainer
      "ADD_MODE": self.op.mode,
635 22b7f6f8 Thomas Thrainer
      }
636 22b7f6f8 Thomas Thrainer
    if self.op.mode == constants.INSTANCE_IMPORT:
637 22b7f6f8 Thomas Thrainer
      env["SRC_NODE"] = self.op.src_node
638 22b7f6f8 Thomas Thrainer
      env["SRC_PATH"] = self.op.src_path
639 22b7f6f8 Thomas Thrainer
      env["SRC_IMAGES"] = self.src_images
640 22b7f6f8 Thomas Thrainer
641 5eacbcae Thomas Thrainer
    env.update(BuildInstanceHookEnv(
642 22b7f6f8 Thomas Thrainer
      name=self.op.instance_name,
643 22b7f6f8 Thomas Thrainer
      primary_node=self.op.pnode,
644 22b7f6f8 Thomas Thrainer
      secondary_nodes=self.secondaries,
645 22b7f6f8 Thomas Thrainer
      status=self.op.start,
646 22b7f6f8 Thomas Thrainer
      os_type=self.op.os_type,
647 22b7f6f8 Thomas Thrainer
      minmem=self.be_full[constants.BE_MINMEM],
648 22b7f6f8 Thomas Thrainer
      maxmem=self.be_full[constants.BE_MAXMEM],
649 22b7f6f8 Thomas Thrainer
      vcpus=self.be_full[constants.BE_VCPUS],
650 5eacbcae Thomas Thrainer
      nics=NICListToTuple(self, self.nics),
651 22b7f6f8 Thomas Thrainer
      disk_template=self.op.disk_template,
652 8a348b15 Christos Stavrakakis
      disks=[(d[constants.IDISK_NAME], d.get("uuid", ""),
653 8a348b15 Christos Stavrakakis
              d[constants.IDISK_SIZE], d[constants.IDISK_MODE])
654 8a348b15 Christos Stavrakakis
             for d in self.disks],
655 22b7f6f8 Thomas Thrainer
      bep=self.be_full,
656 22b7f6f8 Thomas Thrainer
      hvp=self.hv_full,
657 22b7f6f8 Thomas Thrainer
      hypervisor_name=self.op.hypervisor,
658 22b7f6f8 Thomas Thrainer
      tags=self.op.tags,
659 22b7f6f8 Thomas Thrainer
      ))
660 22b7f6f8 Thomas Thrainer
661 22b7f6f8 Thomas Thrainer
    return env
662 22b7f6f8 Thomas Thrainer
663 22b7f6f8 Thomas Thrainer
  def BuildHooksNodes(self):
664 22b7f6f8 Thomas Thrainer
    """Build hooks nodes.
665 22b7f6f8 Thomas Thrainer

666 22b7f6f8 Thomas Thrainer
    """
667 22b7f6f8 Thomas Thrainer
    nl = [self.cfg.GetMasterNode(), self.op.pnode] + self.secondaries
668 22b7f6f8 Thomas Thrainer
    return nl, nl
669 22b7f6f8 Thomas Thrainer
670 22b7f6f8 Thomas Thrainer
  def _ReadExportInfo(self):
671 22b7f6f8 Thomas Thrainer
    """Reads the export information from disk.
672 22b7f6f8 Thomas Thrainer

673 22b7f6f8 Thomas Thrainer
    It will override the opcode source node and path with the actual
674 22b7f6f8 Thomas Thrainer
    information, if these two were not specified before.
675 22b7f6f8 Thomas Thrainer

676 22b7f6f8 Thomas Thrainer
    @return: the export information
677 22b7f6f8 Thomas Thrainer

678 22b7f6f8 Thomas Thrainer
    """
679 22b7f6f8 Thomas Thrainer
    assert self.op.mode == constants.INSTANCE_IMPORT
680 22b7f6f8 Thomas Thrainer
681 22b7f6f8 Thomas Thrainer
    src_node = self.op.src_node
682 22b7f6f8 Thomas Thrainer
    src_path = self.op.src_path
683 22b7f6f8 Thomas Thrainer
684 22b7f6f8 Thomas Thrainer
    if src_node is None:
685 22b7f6f8 Thomas Thrainer
      locked_nodes = self.owned_locks(locking.LEVEL_NODE)
686 22b7f6f8 Thomas Thrainer
      exp_list = self.rpc.call_export_list(locked_nodes)
687 22b7f6f8 Thomas Thrainer
      found = False
688 22b7f6f8 Thomas Thrainer
      for node in exp_list:
689 22b7f6f8 Thomas Thrainer
        if exp_list[node].fail_msg:
690 22b7f6f8 Thomas Thrainer
          continue
691 22b7f6f8 Thomas Thrainer
        if src_path in exp_list[node].payload:
692 22b7f6f8 Thomas Thrainer
          found = True
693 22b7f6f8 Thomas Thrainer
          self.op.src_node = src_node = node
694 22b7f6f8 Thomas Thrainer
          self.op.src_path = src_path = utils.PathJoin(pathutils.EXPORT_DIR,
695 22b7f6f8 Thomas Thrainer
                                                       src_path)
696 22b7f6f8 Thomas Thrainer
          break
697 22b7f6f8 Thomas Thrainer
      if not found:
698 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("No export found for relative path %s" %
699 22b7f6f8 Thomas Thrainer
                                   src_path, errors.ECODE_INVAL)
700 22b7f6f8 Thomas Thrainer
701 5eacbcae Thomas Thrainer
    CheckNodeOnline(self, src_node)
702 22b7f6f8 Thomas Thrainer
    result = self.rpc.call_export_info(src_node, src_path)
703 22b7f6f8 Thomas Thrainer
    result.Raise("No export or invalid export found in dir %s" % src_path)
704 22b7f6f8 Thomas Thrainer
705 22b7f6f8 Thomas Thrainer
    export_info = objects.SerializableConfigParser.Loads(str(result.payload))
706 22b7f6f8 Thomas Thrainer
    if not export_info.has_section(constants.INISECT_EXP):
707 22b7f6f8 Thomas Thrainer
      raise errors.ProgrammerError("Corrupted export config",
708 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_ENVIRON)
709 22b7f6f8 Thomas Thrainer
710 22b7f6f8 Thomas Thrainer
    ei_version = export_info.get(constants.INISECT_EXP, "version")
711 22b7f6f8 Thomas Thrainer
    if (int(ei_version) != constants.EXPORT_VERSION):
712 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Wrong export version %s (wanted %d)" %
713 22b7f6f8 Thomas Thrainer
                                 (ei_version, constants.EXPORT_VERSION),
714 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_ENVIRON)
715 22b7f6f8 Thomas Thrainer
    return export_info
716 22b7f6f8 Thomas Thrainer
717 22b7f6f8 Thomas Thrainer
  def _ReadExportParams(self, einfo):
718 22b7f6f8 Thomas Thrainer
    """Use export parameters as defaults.
719 22b7f6f8 Thomas Thrainer

720 22b7f6f8 Thomas Thrainer
    In case the opcode doesn't specify (as in override) some instance
721 22b7f6f8 Thomas Thrainer
    parameters, then try to use them from the export information, if
722 22b7f6f8 Thomas Thrainer
    that declares them.
723 22b7f6f8 Thomas Thrainer

724 22b7f6f8 Thomas Thrainer
    """
725 22b7f6f8 Thomas Thrainer
    self.op.os_type = einfo.get(constants.INISECT_EXP, "os")
726 22b7f6f8 Thomas Thrainer
727 22b7f6f8 Thomas Thrainer
    if self.op.disk_template is None:
728 22b7f6f8 Thomas Thrainer
      if einfo.has_option(constants.INISECT_INS, "disk_template"):
729 22b7f6f8 Thomas Thrainer
        self.op.disk_template = einfo.get(constants.INISECT_INS,
730 22b7f6f8 Thomas Thrainer
                                          "disk_template")
731 22b7f6f8 Thomas Thrainer
        if self.op.disk_template not in constants.DISK_TEMPLATES:
732 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("Disk template specified in configuration"
733 22b7f6f8 Thomas Thrainer
                                     " file is not one of the allowed values:"
734 22b7f6f8 Thomas Thrainer
                                     " %s" %
735 22b7f6f8 Thomas Thrainer
                                     " ".join(constants.DISK_TEMPLATES),
736 22b7f6f8 Thomas Thrainer
                                     errors.ECODE_INVAL)
737 22b7f6f8 Thomas Thrainer
      else:
738 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("No disk template specified and the export"
739 22b7f6f8 Thomas Thrainer
                                   " is missing the disk_template information",
740 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
741 22b7f6f8 Thomas Thrainer
742 22b7f6f8 Thomas Thrainer
    if not self.op.disks:
743 22b7f6f8 Thomas Thrainer
      disks = []
744 22b7f6f8 Thomas Thrainer
      # TODO: import the disk iv_name too
745 22b7f6f8 Thomas Thrainer
      for idx in range(constants.MAX_DISKS):
746 22b7f6f8 Thomas Thrainer
        if einfo.has_option(constants.INISECT_INS, "disk%d_size" % idx):
747 22b7f6f8 Thomas Thrainer
          disk_sz = einfo.getint(constants.INISECT_INS, "disk%d_size" % idx)
748 22b7f6f8 Thomas Thrainer
          disks.append({constants.IDISK_SIZE: disk_sz})
749 22b7f6f8 Thomas Thrainer
      self.op.disks = disks
750 22b7f6f8 Thomas Thrainer
      if not disks and self.op.disk_template != constants.DT_DISKLESS:
751 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("No disk info specified and the export"
752 22b7f6f8 Thomas Thrainer
                                   " is missing the disk information",
753 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
754 22b7f6f8 Thomas Thrainer
755 22b7f6f8 Thomas Thrainer
    if not self.op.nics:
756 22b7f6f8 Thomas Thrainer
      nics = []
757 22b7f6f8 Thomas Thrainer
      for idx in range(constants.MAX_NICS):
758 22b7f6f8 Thomas Thrainer
        if einfo.has_option(constants.INISECT_INS, "nic%d_mac" % idx):
759 22b7f6f8 Thomas Thrainer
          ndict = {}
760 0973f9ed Dimitris Aragiorgis
          for name in [constants.INIC_IP, constants.INIC_MAC]:
761 22b7f6f8 Thomas Thrainer
            v = einfo.get(constants.INISECT_INS, "nic%d_%s" % (idx, name))
762 22b7f6f8 Thomas Thrainer
            ndict[name] = v
763 0973f9ed Dimitris Aragiorgis
          network = einfo.get(constants.INISECT_INS,
764 0973f9ed Dimitris Aragiorgis
                              "nic%d_%s" % (idx, constants.INIC_NETWORK))
765 0973f9ed Dimitris Aragiorgis
          # in case network is given link and mode are inherited
766 0973f9ed Dimitris Aragiorgis
          # from nodegroup's netparams and thus should not be passed here
767 0973f9ed Dimitris Aragiorgis
          if network:
768 0973f9ed Dimitris Aragiorgis
            ndict[constants.INIC_NETWORK] = network
769 0973f9ed Dimitris Aragiorgis
          else:
770 0973f9ed Dimitris Aragiorgis
            for name in list(constants.NICS_PARAMETERS):
771 0973f9ed Dimitris Aragiorgis
              v = einfo.get(constants.INISECT_INS, "nic%d_%s" % (idx, name))
772 0973f9ed Dimitris Aragiorgis
              ndict[name] = v
773 22b7f6f8 Thomas Thrainer
          nics.append(ndict)
774 22b7f6f8 Thomas Thrainer
        else:
775 22b7f6f8 Thomas Thrainer
          break
776 22b7f6f8 Thomas Thrainer
      self.op.nics = nics
777 22b7f6f8 Thomas Thrainer
778 22b7f6f8 Thomas Thrainer
    if not self.op.tags and einfo.has_option(constants.INISECT_INS, "tags"):
779 22b7f6f8 Thomas Thrainer
      self.op.tags = einfo.get(constants.INISECT_INS, "tags").split()
780 22b7f6f8 Thomas Thrainer
781 22b7f6f8 Thomas Thrainer
    if (self.op.hypervisor is None and
782 22b7f6f8 Thomas Thrainer
        einfo.has_option(constants.INISECT_INS, "hypervisor")):
783 22b7f6f8 Thomas Thrainer
      self.op.hypervisor = einfo.get(constants.INISECT_INS, "hypervisor")
784 22b7f6f8 Thomas Thrainer
785 22b7f6f8 Thomas Thrainer
    if einfo.has_section(constants.INISECT_HYP):
786 22b7f6f8 Thomas Thrainer
      # use the export parameters but do not override the ones
787 22b7f6f8 Thomas Thrainer
      # specified by the user
788 22b7f6f8 Thomas Thrainer
      for name, value in einfo.items(constants.INISECT_HYP):
789 22b7f6f8 Thomas Thrainer
        if name not in self.op.hvparams:
790 22b7f6f8 Thomas Thrainer
          self.op.hvparams[name] = value
791 22b7f6f8 Thomas Thrainer
792 22b7f6f8 Thomas Thrainer
    if einfo.has_section(constants.INISECT_BEP):
793 22b7f6f8 Thomas Thrainer
      # use the parameters, without overriding
794 22b7f6f8 Thomas Thrainer
      for name, value in einfo.items(constants.INISECT_BEP):
795 22b7f6f8 Thomas Thrainer
        if name not in self.op.beparams:
796 22b7f6f8 Thomas Thrainer
          self.op.beparams[name] = value
797 22b7f6f8 Thomas Thrainer
        # Compatibility for the old "memory" be param
798 22b7f6f8 Thomas Thrainer
        if name == constants.BE_MEMORY:
799 22b7f6f8 Thomas Thrainer
          if constants.BE_MAXMEM not in self.op.beparams:
800 22b7f6f8 Thomas Thrainer
            self.op.beparams[constants.BE_MAXMEM] = value
801 22b7f6f8 Thomas Thrainer
          if constants.BE_MINMEM not in self.op.beparams:
802 22b7f6f8 Thomas Thrainer
            self.op.beparams[constants.BE_MINMEM] = value
803 22b7f6f8 Thomas Thrainer
    else:
804 22b7f6f8 Thomas Thrainer
      # try to read the parameters old style, from the main section
805 22b7f6f8 Thomas Thrainer
      for name in constants.BES_PARAMETERS:
806 22b7f6f8 Thomas Thrainer
        if (name not in self.op.beparams and
807 22b7f6f8 Thomas Thrainer
            einfo.has_option(constants.INISECT_INS, name)):
808 22b7f6f8 Thomas Thrainer
          self.op.beparams[name] = einfo.get(constants.INISECT_INS, name)
809 22b7f6f8 Thomas Thrainer
810 22b7f6f8 Thomas Thrainer
    if einfo.has_section(constants.INISECT_OSP):
811 22b7f6f8 Thomas Thrainer
      # use the parameters, without overriding
812 22b7f6f8 Thomas Thrainer
      for name, value in einfo.items(constants.INISECT_OSP):
813 22b7f6f8 Thomas Thrainer
        if name not in self.op.osparams:
814 22b7f6f8 Thomas Thrainer
          self.op.osparams[name] = value
815 22b7f6f8 Thomas Thrainer
816 22b7f6f8 Thomas Thrainer
  def _RevertToDefaults(self, cluster):
817 22b7f6f8 Thomas Thrainer
    """Revert the instance parameters to the default values.
818 22b7f6f8 Thomas Thrainer

819 22b7f6f8 Thomas Thrainer
    """
820 22b7f6f8 Thomas Thrainer
    # hvparams
821 22b7f6f8 Thomas Thrainer
    hv_defs = cluster.SimpleFillHV(self.op.hypervisor, self.op.os_type, {})
822 22b7f6f8 Thomas Thrainer
    for name in self.op.hvparams.keys():
823 22b7f6f8 Thomas Thrainer
      if name in hv_defs and hv_defs[name] == self.op.hvparams[name]:
824 22b7f6f8 Thomas Thrainer
        del self.op.hvparams[name]
825 22b7f6f8 Thomas Thrainer
    # beparams
826 22b7f6f8 Thomas Thrainer
    be_defs = cluster.SimpleFillBE({})
827 22b7f6f8 Thomas Thrainer
    for name in self.op.beparams.keys():
828 22b7f6f8 Thomas Thrainer
      if name in be_defs and be_defs[name] == self.op.beparams[name]:
829 22b7f6f8 Thomas Thrainer
        del self.op.beparams[name]
830 22b7f6f8 Thomas Thrainer
    # nic params
831 22b7f6f8 Thomas Thrainer
    nic_defs = cluster.SimpleFillNIC({})
832 22b7f6f8 Thomas Thrainer
    for nic in self.op.nics:
833 22b7f6f8 Thomas Thrainer
      for name in constants.NICS_PARAMETERS:
834 22b7f6f8 Thomas Thrainer
        if name in nic and name in nic_defs and nic[name] == nic_defs[name]:
835 22b7f6f8 Thomas Thrainer
          del nic[name]
836 22b7f6f8 Thomas Thrainer
    # osparams
837 22b7f6f8 Thomas Thrainer
    os_defs = cluster.SimpleFillOS(self.op.os_type, {})
838 22b7f6f8 Thomas Thrainer
    for name in self.op.osparams.keys():
839 22b7f6f8 Thomas Thrainer
      if name in os_defs and os_defs[name] == self.op.osparams[name]:
840 22b7f6f8 Thomas Thrainer
        del self.op.osparams[name]
841 22b7f6f8 Thomas Thrainer
842 22b7f6f8 Thomas Thrainer
  def _CalculateFileStorageDir(self):
843 22b7f6f8 Thomas Thrainer
    """Calculate final instance file storage dir.
844 22b7f6f8 Thomas Thrainer

845 22b7f6f8 Thomas Thrainer
    """
846 22b7f6f8 Thomas Thrainer
    # file storage dir calculation/check
847 22b7f6f8 Thomas Thrainer
    self.instance_file_storage_dir = None
848 22b7f6f8 Thomas Thrainer
    if self.op.disk_template in constants.DTS_FILEBASED:
849 22b7f6f8 Thomas Thrainer
      # build the full file storage dir path
850 22b7f6f8 Thomas Thrainer
      joinargs = []
851 22b7f6f8 Thomas Thrainer
852 22b7f6f8 Thomas Thrainer
      if self.op.disk_template == constants.DT_SHARED_FILE:
853 22b7f6f8 Thomas Thrainer
        get_fsd_fn = self.cfg.GetSharedFileStorageDir
854 22b7f6f8 Thomas Thrainer
      else:
855 22b7f6f8 Thomas Thrainer
        get_fsd_fn = self.cfg.GetFileStorageDir
856 22b7f6f8 Thomas Thrainer
857 22b7f6f8 Thomas Thrainer
      cfg_storagedir = get_fsd_fn()
858 22b7f6f8 Thomas Thrainer
      if not cfg_storagedir:
859 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Cluster file storage dir not defined",
860 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_STATE)
861 22b7f6f8 Thomas Thrainer
      joinargs.append(cfg_storagedir)
862 22b7f6f8 Thomas Thrainer
863 22b7f6f8 Thomas Thrainer
      if self.op.file_storage_dir is not None:
864 22b7f6f8 Thomas Thrainer
        joinargs.append(self.op.file_storage_dir)
865 22b7f6f8 Thomas Thrainer
866 22b7f6f8 Thomas Thrainer
      joinargs.append(self.op.instance_name)
867 22b7f6f8 Thomas Thrainer
868 22b7f6f8 Thomas Thrainer
      # pylint: disable=W0142
869 22b7f6f8 Thomas Thrainer
      self.instance_file_storage_dir = utils.PathJoin(*joinargs)
870 22b7f6f8 Thomas Thrainer
871 22b7f6f8 Thomas Thrainer
  def CheckPrereq(self): # pylint: disable=R0914
872 22b7f6f8 Thomas Thrainer
    """Check prerequisites.
873 22b7f6f8 Thomas Thrainer

874 22b7f6f8 Thomas Thrainer
    """
875 8b9887c5 Petr Pudlak
    # Check that the optimistically acquired groups are correct wrt the
876 8b9887c5 Petr Pudlak
    # acquired nodes
877 8b9887c5 Petr Pudlak
    owned_groups = frozenset(self.owned_locks(locking.LEVEL_NODEGROUP))
878 8b9887c5 Petr Pudlak
    owned_nodes = frozenset(self.owned_locks(locking.LEVEL_NODE))
879 8b9887c5 Petr Pudlak
    cur_groups = list(self.cfg.GetNodeGroupsFromNodes(owned_nodes))
880 8b9887c5 Petr Pudlak
    if not owned_groups.issuperset(cur_groups):
881 8b9887c5 Petr Pudlak
      raise errors.OpPrereqError("New instance %s's node groups changed since"
882 8b9887c5 Petr Pudlak
                                 " locks were acquired, current groups are"
883 8b9887c5 Petr Pudlak
                                 " are '%s', owning groups '%s'; retry the"
884 8b9887c5 Petr Pudlak
                                 " operation" %
885 8b9887c5 Petr Pudlak
                                 (self.op.instance_name,
886 8b9887c5 Petr Pudlak
                                  utils.CommaJoin(cur_groups),
887 8b9887c5 Petr Pudlak
                                  utils.CommaJoin(owned_groups)),
888 8b9887c5 Petr Pudlak
                                 errors.ECODE_STATE)
889 8b9887c5 Petr Pudlak
890 22b7f6f8 Thomas Thrainer
    self._CalculateFileStorageDir()
891 22b7f6f8 Thomas Thrainer
892 22b7f6f8 Thomas Thrainer
    if self.op.mode == constants.INSTANCE_IMPORT:
893 22b7f6f8 Thomas Thrainer
      export_info = self._ReadExportInfo()
894 22b7f6f8 Thomas Thrainer
      self._ReadExportParams(export_info)
895 22b7f6f8 Thomas Thrainer
      self._old_instance_name = export_info.get(constants.INISECT_INS, "name")
896 22b7f6f8 Thomas Thrainer
    else:
897 22b7f6f8 Thomas Thrainer
      self._old_instance_name = None
898 22b7f6f8 Thomas Thrainer
899 22b7f6f8 Thomas Thrainer
    if (not self.cfg.GetVGName() and
900 22b7f6f8 Thomas Thrainer
        self.op.disk_template not in constants.DTS_NOT_LVM):
901 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Cluster does not support lvm-based"
902 22b7f6f8 Thomas Thrainer
                                 " instances", errors.ECODE_STATE)
903 22b7f6f8 Thomas Thrainer
904 22b7f6f8 Thomas Thrainer
    if (self.op.hypervisor is None or
905 22b7f6f8 Thomas Thrainer
        self.op.hypervisor == constants.VALUE_AUTO):
906 22b7f6f8 Thomas Thrainer
      self.op.hypervisor = self.cfg.GetHypervisorType()
907 22b7f6f8 Thomas Thrainer
908 22b7f6f8 Thomas Thrainer
    cluster = self.cfg.GetClusterInfo()
909 22b7f6f8 Thomas Thrainer
    enabled_hvs = cluster.enabled_hypervisors
910 22b7f6f8 Thomas Thrainer
    if self.op.hypervisor not in enabled_hvs:
911 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Selected hypervisor (%s) not enabled in the"
912 22b7f6f8 Thomas Thrainer
                                 " cluster (%s)" %
913 22b7f6f8 Thomas Thrainer
                                 (self.op.hypervisor, ",".join(enabled_hvs)),
914 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_STATE)
915 22b7f6f8 Thomas Thrainer
916 22b7f6f8 Thomas Thrainer
    # Check tag validity
917 22b7f6f8 Thomas Thrainer
    for tag in self.op.tags:
918 22b7f6f8 Thomas Thrainer
      objects.TaggableObject.ValidateTag(tag)
919 22b7f6f8 Thomas Thrainer
920 22b7f6f8 Thomas Thrainer
    # check hypervisor parameter syntax (locally)
921 22b7f6f8 Thomas Thrainer
    utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES)
922 22b7f6f8 Thomas Thrainer
    filled_hvp = cluster.SimpleFillHV(self.op.hypervisor, self.op.os_type,
923 22b7f6f8 Thomas Thrainer
                                      self.op.hvparams)
924 22b7f6f8 Thomas Thrainer
    hv_type = hypervisor.GetHypervisorClass(self.op.hypervisor)
925 22b7f6f8 Thomas Thrainer
    hv_type.CheckParameterSyntax(filled_hvp)
926 22b7f6f8 Thomas Thrainer
    self.hv_full = filled_hvp
927 22b7f6f8 Thomas Thrainer
    # check that we don't specify global parameters on an instance
928 5eacbcae Thomas Thrainer
    CheckParamsNotGlobal(self.op.hvparams, constants.HVC_GLOBALS, "hypervisor",
929 5eacbcae Thomas Thrainer
                         "instance", "cluster")
930 22b7f6f8 Thomas Thrainer
931 22b7f6f8 Thomas Thrainer
    # fill and remember the beparams dict
932 22b7f6f8 Thomas Thrainer
    self.be_full = _ComputeFullBeParams(self.op, cluster)
933 22b7f6f8 Thomas Thrainer
934 22b7f6f8 Thomas Thrainer
    # build os parameters
935 22b7f6f8 Thomas Thrainer
    self.os_full = cluster.SimpleFillOS(self.op.os_type, self.op.osparams)
936 22b7f6f8 Thomas Thrainer
937 22b7f6f8 Thomas Thrainer
    # now that hvp/bep are in final format, let's reset to defaults,
938 22b7f6f8 Thomas Thrainer
    # if told to do so
939 22b7f6f8 Thomas Thrainer
    if self.op.identify_defaults:
940 22b7f6f8 Thomas Thrainer
      self._RevertToDefaults(cluster)
941 22b7f6f8 Thomas Thrainer
942 22b7f6f8 Thomas Thrainer
    # NIC buildup
943 22b7f6f8 Thomas Thrainer
    self.nics = _ComputeNics(self.op, cluster, self.check_ip, self.cfg,
944 22b7f6f8 Thomas Thrainer
                             self.proc.GetECId())
945 22b7f6f8 Thomas Thrainer
946 22b7f6f8 Thomas Thrainer
    # disk checks/pre-build
947 22b7f6f8 Thomas Thrainer
    default_vg = self.cfg.GetVGName()
948 5eacbcae Thomas Thrainer
    self.disks = ComputeDisks(self.op, default_vg)
949 22b7f6f8 Thomas Thrainer
950 22b7f6f8 Thomas Thrainer
    if self.op.mode == constants.INSTANCE_IMPORT:
951 22b7f6f8 Thomas Thrainer
      disk_images = []
952 22b7f6f8 Thomas Thrainer
      for idx in range(len(self.disks)):
953 22b7f6f8 Thomas Thrainer
        option = "disk%d_dump" % idx
954 22b7f6f8 Thomas Thrainer
        if export_info.has_option(constants.INISECT_INS, option):
955 22b7f6f8 Thomas Thrainer
          # FIXME: are the old os-es, disk sizes, etc. useful?
956 22b7f6f8 Thomas Thrainer
          export_name = export_info.get(constants.INISECT_INS, option)
957 22b7f6f8 Thomas Thrainer
          image = utils.PathJoin(self.op.src_path, export_name)
958 22b7f6f8 Thomas Thrainer
          disk_images.append(image)
959 22b7f6f8 Thomas Thrainer
        else:
960 22b7f6f8 Thomas Thrainer
          disk_images.append(False)
961 22b7f6f8 Thomas Thrainer
962 22b7f6f8 Thomas Thrainer
      self.src_images = disk_images
963 22b7f6f8 Thomas Thrainer
964 22b7f6f8 Thomas Thrainer
      if self.op.instance_name == self._old_instance_name:
965 22b7f6f8 Thomas Thrainer
        for idx, nic in enumerate(self.nics):
966 22b7f6f8 Thomas Thrainer
          if nic.mac == constants.VALUE_AUTO:
967 22b7f6f8 Thomas Thrainer
            nic_mac_ini = "nic%d_mac" % idx
968 22b7f6f8 Thomas Thrainer
            nic.mac = export_info.get(constants.INISECT_INS, nic_mac_ini)
969 22b7f6f8 Thomas Thrainer
970 22b7f6f8 Thomas Thrainer
    # ENDIF: self.op.mode == constants.INSTANCE_IMPORT
971 22b7f6f8 Thomas Thrainer
972 22b7f6f8 Thomas Thrainer
    # ip ping checks (we use the same ip that was resolved in ExpandNames)
973 22b7f6f8 Thomas Thrainer
    if self.op.ip_check:
974 22b7f6f8 Thomas Thrainer
      if netutils.TcpPing(self.check_ip, constants.DEFAULT_NODED_PORT):
975 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("IP %s of instance %s already in use" %
976 22b7f6f8 Thomas Thrainer
                                   (self.check_ip, self.op.instance_name),
977 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_NOTUNIQUE)
978 22b7f6f8 Thomas Thrainer
979 22b7f6f8 Thomas Thrainer
    #### mac address generation
980 22b7f6f8 Thomas Thrainer
    # By generating here the mac address both the allocator and the hooks get
981 22b7f6f8 Thomas Thrainer
    # the real final mac address rather than the 'auto' or 'generate' value.
982 22b7f6f8 Thomas Thrainer
    # There is a race condition between the generation and the instance object
983 22b7f6f8 Thomas Thrainer
    # creation, which means that we know the mac is valid now, but we're not
984 22b7f6f8 Thomas Thrainer
    # sure it will be when we actually add the instance. If things go bad
985 22b7f6f8 Thomas Thrainer
    # adding the instance will abort because of a duplicate mac, and the
986 22b7f6f8 Thomas Thrainer
    # creation job will fail.
987 22b7f6f8 Thomas Thrainer
    for nic in self.nics:
988 22b7f6f8 Thomas Thrainer
      if nic.mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
989 22b7f6f8 Thomas Thrainer
        nic.mac = self.cfg.GenerateMAC(nic.network, self.proc.GetECId())
990 22b7f6f8 Thomas Thrainer
991 22b7f6f8 Thomas Thrainer
    #### allocator run
992 22b7f6f8 Thomas Thrainer
993 22b7f6f8 Thomas Thrainer
    if self.op.iallocator is not None:
994 22b7f6f8 Thomas Thrainer
      self._RunAllocator()
995 22b7f6f8 Thomas Thrainer
996 22b7f6f8 Thomas Thrainer
    # Release all unneeded node locks
997 22b7f6f8 Thomas Thrainer
    keep_locks = filter(None, [self.op.pnode, self.op.snode, self.op.src_node])
998 5eacbcae Thomas Thrainer
    ReleaseLocks(self, locking.LEVEL_NODE, keep=keep_locks)
999 5eacbcae Thomas Thrainer
    ReleaseLocks(self, locking.LEVEL_NODE_RES, keep=keep_locks)
1000 5eacbcae Thomas Thrainer
    ReleaseLocks(self, locking.LEVEL_NODE_ALLOC)
1001 8b9887c5 Petr Pudlak
    # Release all unneeded group locks
1002 8b9887c5 Petr Pudlak
    ReleaseLocks(self, locking.LEVEL_NODEGROUP,
1003 8b9887c5 Petr Pudlak
                 keep=self.cfg.GetNodeGroupsFromNodes(keep_locks))
1004 22b7f6f8 Thomas Thrainer
1005 22b7f6f8 Thomas Thrainer
    assert (self.owned_locks(locking.LEVEL_NODE) ==
1006 22b7f6f8 Thomas Thrainer
            self.owned_locks(locking.LEVEL_NODE_RES)), \
1007 22b7f6f8 Thomas Thrainer
      "Node locks differ from node resource locks"
1008 22b7f6f8 Thomas Thrainer
1009 22b7f6f8 Thomas Thrainer
    #### node related checks
1010 22b7f6f8 Thomas Thrainer
1011 22b7f6f8 Thomas Thrainer
    # check primary node
1012 22b7f6f8 Thomas Thrainer
    self.pnode = pnode = self.cfg.GetNodeInfo(self.op.pnode)
1013 22b7f6f8 Thomas Thrainer
    assert self.pnode is not None, \
1014 22b7f6f8 Thomas Thrainer
      "Cannot retrieve locked node %s" % self.op.pnode
1015 22b7f6f8 Thomas Thrainer
    if pnode.offline:
1016 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Cannot use offline primary node '%s'" %
1017 22b7f6f8 Thomas Thrainer
                                 pnode.name, errors.ECODE_STATE)
1018 22b7f6f8 Thomas Thrainer
    if pnode.drained:
1019 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Cannot use drained primary node '%s'" %
1020 22b7f6f8 Thomas Thrainer
                                 pnode.name, errors.ECODE_STATE)
1021 22b7f6f8 Thomas Thrainer
    if not pnode.vm_capable:
1022 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Cannot use non-vm_capable primary node"
1023 22b7f6f8 Thomas Thrainer
                                 " '%s'" % pnode.name, errors.ECODE_STATE)
1024 22b7f6f8 Thomas Thrainer
1025 22b7f6f8 Thomas Thrainer
    self.secondaries = []
1026 22b7f6f8 Thomas Thrainer
1027 22b7f6f8 Thomas Thrainer
    # Fill in any IPs from IP pools. This must happen here, because we need to
1028 22b7f6f8 Thomas Thrainer
    # know the nic's primary node, as specified by the iallocator
1029 22b7f6f8 Thomas Thrainer
    for idx, nic in enumerate(self.nics):
1030 22b7f6f8 Thomas Thrainer
      net_uuid = nic.network
1031 22b7f6f8 Thomas Thrainer
      if net_uuid is not None:
1032 22b7f6f8 Thomas Thrainer
        nobj = self.cfg.GetNetwork(net_uuid)
1033 22b7f6f8 Thomas Thrainer
        netparams = self.cfg.GetGroupNetParams(net_uuid, self.pnode.name)
1034 22b7f6f8 Thomas Thrainer
        if netparams is None:
1035 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("No netparams found for network"
1036 22b7f6f8 Thomas Thrainer
                                     " %s. Propably not connected to"
1037 22b7f6f8 Thomas Thrainer
                                     " node's %s nodegroup" %
1038 22b7f6f8 Thomas Thrainer
                                     (nobj.name, self.pnode.name),
1039 22b7f6f8 Thomas Thrainer
                                     errors.ECODE_INVAL)
1040 22b7f6f8 Thomas Thrainer
        self.LogInfo("NIC/%d inherits netparams %s" %
1041 22b7f6f8 Thomas Thrainer
                     (idx, netparams.values()))
1042 22b7f6f8 Thomas Thrainer
        nic.nicparams = dict(netparams)
1043 22b7f6f8 Thomas Thrainer
        if nic.ip is not None:
1044 22b7f6f8 Thomas Thrainer
          if nic.ip.lower() == constants.NIC_IP_POOL:
1045 22b7f6f8 Thomas Thrainer
            try:
1046 22b7f6f8 Thomas Thrainer
              nic.ip = self.cfg.GenerateIp(net_uuid, self.proc.GetECId())
1047 22b7f6f8 Thomas Thrainer
            except errors.ReservationError:
1048 22b7f6f8 Thomas Thrainer
              raise errors.OpPrereqError("Unable to get a free IP for NIC %d"
1049 22b7f6f8 Thomas Thrainer
                                         " from the address pool" % idx,
1050 22b7f6f8 Thomas Thrainer
                                         errors.ECODE_STATE)
1051 22b7f6f8 Thomas Thrainer
            self.LogInfo("Chose IP %s from network %s", nic.ip, nobj.name)
1052 22b7f6f8 Thomas Thrainer
          else:
1053 22b7f6f8 Thomas Thrainer
            try:
1054 22b7f6f8 Thomas Thrainer
              self.cfg.ReserveIp(net_uuid, nic.ip, self.proc.GetECId())
1055 22b7f6f8 Thomas Thrainer
            except errors.ReservationError:
1056 22b7f6f8 Thomas Thrainer
              raise errors.OpPrereqError("IP address %s already in use"
1057 22b7f6f8 Thomas Thrainer
                                         " or does not belong to network %s" %
1058 22b7f6f8 Thomas Thrainer
                                         (nic.ip, nobj.name),
1059 22b7f6f8 Thomas Thrainer
                                         errors.ECODE_NOTUNIQUE)
1060 22b7f6f8 Thomas Thrainer
1061 22b7f6f8 Thomas Thrainer
      # net is None, ip None or given
1062 22b7f6f8 Thomas Thrainer
      elif self.op.conflicts_check:
1063 22b7f6f8 Thomas Thrainer
        _CheckForConflictingIp(self, nic.ip, self.pnode.name)
1064 22b7f6f8 Thomas Thrainer
1065 22b7f6f8 Thomas Thrainer
    # mirror node verification
1066 22b7f6f8 Thomas Thrainer
    if self.op.disk_template in constants.DTS_INT_MIRROR:
1067 22b7f6f8 Thomas Thrainer
      if self.op.snode == pnode.name:
1068 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("The secondary node cannot be the"
1069 22b7f6f8 Thomas Thrainer
                                   " primary node", errors.ECODE_INVAL)
1070 5eacbcae Thomas Thrainer
      CheckNodeOnline(self, self.op.snode)
1071 5eacbcae Thomas Thrainer
      CheckNodeNotDrained(self, self.op.snode)
1072 5eacbcae Thomas Thrainer
      CheckNodeVmCapable(self, self.op.snode)
1073 22b7f6f8 Thomas Thrainer
      self.secondaries.append(self.op.snode)
1074 22b7f6f8 Thomas Thrainer
1075 22b7f6f8 Thomas Thrainer
      snode = self.cfg.GetNodeInfo(self.op.snode)
1076 22b7f6f8 Thomas Thrainer
      if pnode.group != snode.group:
1077 22b7f6f8 Thomas Thrainer
        self.LogWarning("The primary and secondary nodes are in two"
1078 22b7f6f8 Thomas Thrainer
                        " different node groups; the disk parameters"
1079 22b7f6f8 Thomas Thrainer
                        " from the first disk's node group will be"
1080 22b7f6f8 Thomas Thrainer
                        " used")
1081 22b7f6f8 Thomas Thrainer
1082 22b7f6f8 Thomas Thrainer
    if not self.op.disk_template in constants.DTS_EXCL_STORAGE:
1083 22b7f6f8 Thomas Thrainer
      nodes = [pnode]
1084 22b7f6f8 Thomas Thrainer
      if self.op.disk_template in constants.DTS_INT_MIRROR:
1085 22b7f6f8 Thomas Thrainer
        nodes.append(snode)
1086 5eacbcae Thomas Thrainer
      has_es = lambda n: IsExclusiveStorageEnabledNode(self.cfg, n)
1087 22b7f6f8 Thomas Thrainer
      if compat.any(map(has_es, nodes)):
1088 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Disk template %s not supported with"
1089 22b7f6f8 Thomas Thrainer
                                   " exclusive storage" % self.op.disk_template,
1090 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_STATE)
1091 22b7f6f8 Thomas Thrainer
1092 22b7f6f8 Thomas Thrainer
    nodenames = [pnode.name] + self.secondaries
1093 22b7f6f8 Thomas Thrainer
1094 22b7f6f8 Thomas Thrainer
    if not self.adopt_disks:
1095 22b7f6f8 Thomas Thrainer
      if self.op.disk_template == constants.DT_RBD:
1096 22b7f6f8 Thomas Thrainer
        # _CheckRADOSFreeSpace() is just a placeholder.
1097 22b7f6f8 Thomas Thrainer
        # Any function that checks prerequisites can be placed here.
1098 22b7f6f8 Thomas Thrainer
        # Check if there is enough space on the RADOS cluster.
1099 5eacbcae Thomas Thrainer
        CheckRADOSFreeSpace()
1100 22b7f6f8 Thomas Thrainer
      elif self.op.disk_template == constants.DT_EXT:
1101 22b7f6f8 Thomas Thrainer
        # FIXME: Function that checks prereqs if needed
1102 22b7f6f8 Thomas Thrainer
        pass
1103 22b7f6f8 Thomas Thrainer
      else:
1104 22b7f6f8 Thomas Thrainer
        # Check lv size requirements, if not adopting
1105 5eacbcae Thomas Thrainer
        req_sizes = ComputeDiskSizePerVG(self.op.disk_template, self.disks)
1106 5eacbcae Thomas Thrainer
        CheckNodesFreeDiskPerVG(self, nodenames, req_sizes)
1107 22b7f6f8 Thomas Thrainer
1108 22b7f6f8 Thomas Thrainer
    elif self.op.disk_template == constants.DT_PLAIN: # Check the adoption data
1109 22b7f6f8 Thomas Thrainer
      all_lvs = set(["%s/%s" % (disk[constants.IDISK_VG],
1110 22b7f6f8 Thomas Thrainer
                                disk[constants.IDISK_ADOPT])
1111 22b7f6f8 Thomas Thrainer
                     for disk in self.disks])
1112 22b7f6f8 Thomas Thrainer
      if len(all_lvs) != len(self.disks):
1113 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Duplicate volume names given for adoption",
1114 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
1115 22b7f6f8 Thomas Thrainer
      for lv_name in all_lvs:
1116 22b7f6f8 Thomas Thrainer
        try:
1117 22b7f6f8 Thomas Thrainer
          # FIXME: lv_name here is "vg/lv" need to ensure that other calls
1118 22b7f6f8 Thomas Thrainer
          # to ReserveLV uses the same syntax
1119 22b7f6f8 Thomas Thrainer
          self.cfg.ReserveLV(lv_name, self.proc.GetECId())
1120 22b7f6f8 Thomas Thrainer
        except errors.ReservationError:
1121 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("LV named %s used by another instance" %
1122 22b7f6f8 Thomas Thrainer
                                     lv_name, errors.ECODE_NOTUNIQUE)
1123 22b7f6f8 Thomas Thrainer
1124 22b7f6f8 Thomas Thrainer
      vg_names = self.rpc.call_vg_list([pnode.name])[pnode.name]
1125 22b7f6f8 Thomas Thrainer
      vg_names.Raise("Cannot get VG information from node %s" % pnode.name)
1126 22b7f6f8 Thomas Thrainer
1127 22b7f6f8 Thomas Thrainer
      node_lvs = self.rpc.call_lv_list([pnode.name],
1128 22b7f6f8 Thomas Thrainer
                                       vg_names.payload.keys())[pnode.name]
1129 22b7f6f8 Thomas Thrainer
      node_lvs.Raise("Cannot get LV information from node %s" % pnode.name)
1130 22b7f6f8 Thomas Thrainer
      node_lvs = node_lvs.payload
1131 22b7f6f8 Thomas Thrainer
1132 22b7f6f8 Thomas Thrainer
      delta = all_lvs.difference(node_lvs.keys())
1133 22b7f6f8 Thomas Thrainer
      if delta:
1134 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Missing logical volume(s): %s" %
1135 22b7f6f8 Thomas Thrainer
                                   utils.CommaJoin(delta),
1136 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
1137 22b7f6f8 Thomas Thrainer
      online_lvs = [lv for lv in all_lvs if node_lvs[lv][2]]
1138 22b7f6f8 Thomas Thrainer
      if online_lvs:
1139 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Online logical volumes found, cannot"
1140 22b7f6f8 Thomas Thrainer
                                   " adopt: %s" % utils.CommaJoin(online_lvs),
1141 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_STATE)
1142 22b7f6f8 Thomas Thrainer
      # update the size of disk based on what is found
1143 22b7f6f8 Thomas Thrainer
      for dsk in self.disks:
1144 22b7f6f8 Thomas Thrainer
        dsk[constants.IDISK_SIZE] = \
1145 22b7f6f8 Thomas Thrainer
          int(float(node_lvs["%s/%s" % (dsk[constants.IDISK_VG],
1146 22b7f6f8 Thomas Thrainer
                                        dsk[constants.IDISK_ADOPT])][0]))
1147 22b7f6f8 Thomas Thrainer
1148 22b7f6f8 Thomas Thrainer
    elif self.op.disk_template == constants.DT_BLOCK:
1149 22b7f6f8 Thomas Thrainer
      # Normalize and de-duplicate device paths
1150 22b7f6f8 Thomas Thrainer
      all_disks = set([os.path.abspath(disk[constants.IDISK_ADOPT])
1151 22b7f6f8 Thomas Thrainer
                       for disk in self.disks])
1152 22b7f6f8 Thomas Thrainer
      if len(all_disks) != len(self.disks):
1153 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Duplicate disk names given for adoption",
1154 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
1155 22b7f6f8 Thomas Thrainer
      baddisks = [d for d in all_disks
1156 22b7f6f8 Thomas Thrainer
                  if not d.startswith(constants.ADOPTABLE_BLOCKDEV_ROOT)]
1157 22b7f6f8 Thomas Thrainer
      if baddisks:
1158 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Device node(s) %s lie outside %s and"
1159 22b7f6f8 Thomas Thrainer
                                   " cannot be adopted" %
1160 22b7f6f8 Thomas Thrainer
                                   (utils.CommaJoin(baddisks),
1161 22b7f6f8 Thomas Thrainer
                                    constants.ADOPTABLE_BLOCKDEV_ROOT),
1162 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
1163 22b7f6f8 Thomas Thrainer
1164 22b7f6f8 Thomas Thrainer
      node_disks = self.rpc.call_bdev_sizes([pnode.name],
1165 22b7f6f8 Thomas Thrainer
                                            list(all_disks))[pnode.name]
1166 22b7f6f8 Thomas Thrainer
      node_disks.Raise("Cannot get block device information from node %s" %
1167 22b7f6f8 Thomas Thrainer
                       pnode.name)
1168 22b7f6f8 Thomas Thrainer
      node_disks = node_disks.payload
1169 22b7f6f8 Thomas Thrainer
      delta = all_disks.difference(node_disks.keys())
1170 22b7f6f8 Thomas Thrainer
      if delta:
1171 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Missing block device(s): %s" %
1172 22b7f6f8 Thomas Thrainer
                                   utils.CommaJoin(delta),
1173 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
1174 22b7f6f8 Thomas Thrainer
      for dsk in self.disks:
1175 22b7f6f8 Thomas Thrainer
        dsk[constants.IDISK_SIZE] = \
1176 22b7f6f8 Thomas Thrainer
          int(float(node_disks[dsk[constants.IDISK_ADOPT]]))
1177 22b7f6f8 Thomas Thrainer
1178 22b7f6f8 Thomas Thrainer
    # Verify instance specs
1179 22b7f6f8 Thomas Thrainer
    spindle_use = self.be_full.get(constants.BE_SPINDLE_USE, None)
1180 22b7f6f8 Thomas Thrainer
    ispec = {
1181 22b7f6f8 Thomas Thrainer
      constants.ISPEC_MEM_SIZE: self.be_full.get(constants.BE_MAXMEM, None),
1182 22b7f6f8 Thomas Thrainer
      constants.ISPEC_CPU_COUNT: self.be_full.get(constants.BE_VCPUS, None),
1183 22b7f6f8 Thomas Thrainer
      constants.ISPEC_DISK_COUNT: len(self.disks),
1184 22b7f6f8 Thomas Thrainer
      constants.ISPEC_DISK_SIZE: [disk[constants.IDISK_SIZE]
1185 22b7f6f8 Thomas Thrainer
                                  for disk in self.disks],
1186 22b7f6f8 Thomas Thrainer
      constants.ISPEC_NIC_COUNT: len(self.nics),
1187 22b7f6f8 Thomas Thrainer
      constants.ISPEC_SPINDLE_USE: spindle_use,
1188 22b7f6f8 Thomas Thrainer
      }
1189 22b7f6f8 Thomas Thrainer
1190 22b7f6f8 Thomas Thrainer
    group_info = self.cfg.GetNodeGroup(pnode.group)
1191 22b7f6f8 Thomas Thrainer
    ipolicy = ganeti.masterd.instance.CalculateGroupIPolicy(cluster, group_info)
1192 22b7f6f8 Thomas Thrainer
    res = _ComputeIPolicyInstanceSpecViolation(ipolicy, ispec,
1193 22b7f6f8 Thomas Thrainer
                                               self.op.disk_template)
1194 22b7f6f8 Thomas Thrainer
    if not self.op.ignore_ipolicy and res:
1195 22b7f6f8 Thomas Thrainer
      msg = ("Instance allocation to group %s (%s) violates policy: %s" %
1196 22b7f6f8 Thomas Thrainer
             (pnode.group, group_info.name, utils.CommaJoin(res)))
1197 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
1198 22b7f6f8 Thomas Thrainer
1199 5eacbcae Thomas Thrainer
    CheckHVParams(self, nodenames, self.op.hypervisor, self.op.hvparams)
1200 22b7f6f8 Thomas Thrainer
1201 5eacbcae Thomas Thrainer
    CheckNodeHasOS(self, pnode.name, self.op.os_type, self.op.force_variant)
1202 22b7f6f8 Thomas Thrainer
    # check OS parameters (remotely)
1203 5eacbcae Thomas Thrainer
    CheckOSParams(self, True, nodenames, self.op.os_type, self.os_full)
1204 22b7f6f8 Thomas Thrainer
1205 5eacbcae Thomas Thrainer
    CheckNicsBridgesExist(self, self.nics, self.pnode.name)
1206 22b7f6f8 Thomas Thrainer
1207 22b7f6f8 Thomas Thrainer
    #TODO: _CheckExtParams (remotely)
1208 22b7f6f8 Thomas Thrainer
    # Check parameters for extstorage
1209 22b7f6f8 Thomas Thrainer
1210 22b7f6f8 Thomas Thrainer
    # memory check on primary node
1211 22b7f6f8 Thomas Thrainer
    #TODO(dynmem): use MINMEM for checking
1212 22b7f6f8 Thomas Thrainer
    if self.op.start:
1213 5eacbcae Thomas Thrainer
      CheckNodeFreeMemory(self, self.pnode.name,
1214 5eacbcae Thomas Thrainer
                          "creating instance %s" % self.op.instance_name,
1215 5eacbcae Thomas Thrainer
                          self.be_full[constants.BE_MAXMEM],
1216 5eacbcae Thomas Thrainer
                          self.op.hypervisor)
1217 22b7f6f8 Thomas Thrainer
1218 22b7f6f8 Thomas Thrainer
    self.dry_run_result = list(nodenames)
1219 22b7f6f8 Thomas Thrainer
1220 22b7f6f8 Thomas Thrainer
  def Exec(self, feedback_fn):
1221 22b7f6f8 Thomas Thrainer
    """Create and add the instance to the cluster.
1222 22b7f6f8 Thomas Thrainer

1223 22b7f6f8 Thomas Thrainer
    """
1224 22b7f6f8 Thomas Thrainer
    instance = self.op.instance_name
1225 22b7f6f8 Thomas Thrainer
    pnode_name = self.pnode.name
1226 22b7f6f8 Thomas Thrainer
1227 22b7f6f8 Thomas Thrainer
    assert not (self.owned_locks(locking.LEVEL_NODE_RES) -
1228 22b7f6f8 Thomas Thrainer
                self.owned_locks(locking.LEVEL_NODE)), \
1229 22b7f6f8 Thomas Thrainer
      "Node locks differ from node resource locks"
1230 22b7f6f8 Thomas Thrainer
    assert not self.glm.is_owned(locking.LEVEL_NODE_ALLOC)
1231 22b7f6f8 Thomas Thrainer
1232 22b7f6f8 Thomas Thrainer
    ht_kind = self.op.hypervisor
1233 22b7f6f8 Thomas Thrainer
    if ht_kind in constants.HTS_REQ_PORT:
1234 22b7f6f8 Thomas Thrainer
      network_port = self.cfg.AllocatePort()
1235 22b7f6f8 Thomas Thrainer
    else:
1236 22b7f6f8 Thomas Thrainer
      network_port = None
1237 22b7f6f8 Thomas Thrainer
1238 22b7f6f8 Thomas Thrainer
    # This is ugly but we got a chicken-egg problem here
1239 22b7f6f8 Thomas Thrainer
    # We can only take the group disk parameters, as the instance
1240 22b7f6f8 Thomas Thrainer
    # has no disks yet (we are generating them right here).
1241 22b7f6f8 Thomas Thrainer
    node = self.cfg.GetNodeInfo(pnode_name)
1242 22b7f6f8 Thomas Thrainer
    nodegroup = self.cfg.GetNodeGroup(node.group)
1243 5eacbcae Thomas Thrainer
    disks = GenerateDiskTemplate(self,
1244 5eacbcae Thomas Thrainer
                                 self.op.disk_template,
1245 5eacbcae Thomas Thrainer
                                 instance, pnode_name,
1246 5eacbcae Thomas Thrainer
                                 self.secondaries,
1247 5eacbcae Thomas Thrainer
                                 self.disks,
1248 5eacbcae Thomas Thrainer
                                 self.instance_file_storage_dir,
1249 5eacbcae Thomas Thrainer
                                 self.op.file_driver,
1250 5eacbcae Thomas Thrainer
                                 0,
1251 5eacbcae Thomas Thrainer
                                 feedback_fn,
1252 5eacbcae Thomas Thrainer
                                 self.cfg.GetGroupDiskParams(nodegroup))
1253 22b7f6f8 Thomas Thrainer
1254 22b7f6f8 Thomas Thrainer
    iobj = objects.Instance(name=instance, os=self.op.os_type,
1255 22b7f6f8 Thomas Thrainer
                            primary_node=pnode_name,
1256 22b7f6f8 Thomas Thrainer
                            nics=self.nics, disks=disks,
1257 22b7f6f8 Thomas Thrainer
                            disk_template=self.op.disk_template,
1258 1d4a4b26 Thomas Thrainer
                            disks_active=False,
1259 22b7f6f8 Thomas Thrainer
                            admin_state=constants.ADMINST_DOWN,
1260 22b7f6f8 Thomas Thrainer
                            network_port=network_port,
1261 22b7f6f8 Thomas Thrainer
                            beparams=self.op.beparams,
1262 22b7f6f8 Thomas Thrainer
                            hvparams=self.op.hvparams,
1263 22b7f6f8 Thomas Thrainer
                            hypervisor=self.op.hypervisor,
1264 22b7f6f8 Thomas Thrainer
                            osparams=self.op.osparams,
1265 22b7f6f8 Thomas Thrainer
                            )
1266 22b7f6f8 Thomas Thrainer
1267 22b7f6f8 Thomas Thrainer
    if self.op.tags:
1268 22b7f6f8 Thomas Thrainer
      for tag in self.op.tags:
1269 22b7f6f8 Thomas Thrainer
        iobj.AddTag(tag)
1270 22b7f6f8 Thomas Thrainer
1271 22b7f6f8 Thomas Thrainer
    if self.adopt_disks:
1272 22b7f6f8 Thomas Thrainer
      if self.op.disk_template == constants.DT_PLAIN:
1273 22b7f6f8 Thomas Thrainer
        # rename LVs to the newly-generated names; we need to construct
1274 22b7f6f8 Thomas Thrainer
        # 'fake' LV disks with the old data, plus the new unique_id
1275 22b7f6f8 Thomas Thrainer
        tmp_disks = [objects.Disk.FromDict(v.ToDict()) for v in disks]
1276 22b7f6f8 Thomas Thrainer
        rename_to = []
1277 22b7f6f8 Thomas Thrainer
        for t_dsk, a_dsk in zip(tmp_disks, self.disks):
1278 22b7f6f8 Thomas Thrainer
          rename_to.append(t_dsk.logical_id)
1279 22b7f6f8 Thomas Thrainer
          t_dsk.logical_id = (t_dsk.logical_id[0], a_dsk[constants.IDISK_ADOPT])
1280 22b7f6f8 Thomas Thrainer
          self.cfg.SetDiskID(t_dsk, pnode_name)
1281 22b7f6f8 Thomas Thrainer
        result = self.rpc.call_blockdev_rename(pnode_name,
1282 22b7f6f8 Thomas Thrainer
                                               zip(tmp_disks, rename_to))
1283 22b7f6f8 Thomas Thrainer
        result.Raise("Failed to rename adoped LVs")
1284 22b7f6f8 Thomas Thrainer
    else:
1285 22b7f6f8 Thomas Thrainer
      feedback_fn("* creating instance disks...")
1286 22b7f6f8 Thomas Thrainer
      try:
1287 5eacbcae Thomas Thrainer
        CreateDisks(self, iobj)
1288 22b7f6f8 Thomas Thrainer
      except errors.OpExecError:
1289 22b7f6f8 Thomas Thrainer
        self.LogWarning("Device creation failed")
1290 22b7f6f8 Thomas Thrainer
        self.cfg.ReleaseDRBDMinors(instance)
1291 22b7f6f8 Thomas Thrainer
        raise
1292 22b7f6f8 Thomas Thrainer
1293 22b7f6f8 Thomas Thrainer
    feedback_fn("adding instance %s to cluster config" % instance)
1294 22b7f6f8 Thomas Thrainer
1295 22b7f6f8 Thomas Thrainer
    self.cfg.AddInstance(iobj, self.proc.GetECId())
1296 22b7f6f8 Thomas Thrainer
1297 22b7f6f8 Thomas Thrainer
    # Declare that we don't want to remove the instance lock anymore, as we've
1298 22b7f6f8 Thomas Thrainer
    # added the instance to the config
1299 22b7f6f8 Thomas Thrainer
    del self.remove_locks[locking.LEVEL_INSTANCE]
1300 22b7f6f8 Thomas Thrainer
1301 22b7f6f8 Thomas Thrainer
    if self.op.mode == constants.INSTANCE_IMPORT:
1302 22b7f6f8 Thomas Thrainer
      # Release unused nodes
1303 5eacbcae Thomas Thrainer
      ReleaseLocks(self, locking.LEVEL_NODE, keep=[self.op.src_node])
1304 22b7f6f8 Thomas Thrainer
    else:
1305 22b7f6f8 Thomas Thrainer
      # Release all nodes
1306 5eacbcae Thomas Thrainer
      ReleaseLocks(self, locking.LEVEL_NODE)
1307 22b7f6f8 Thomas Thrainer
1308 22b7f6f8 Thomas Thrainer
    disk_abort = False
1309 22b7f6f8 Thomas Thrainer
    if not self.adopt_disks and self.cfg.GetClusterInfo().prealloc_wipe_disks:
1310 22b7f6f8 Thomas Thrainer
      feedback_fn("* wiping instance disks...")
1311 22b7f6f8 Thomas Thrainer
      try:
1312 5eacbcae Thomas Thrainer
        WipeDisks(self, iobj)
1313 22b7f6f8 Thomas Thrainer
      except errors.OpExecError, err:
1314 22b7f6f8 Thomas Thrainer
        logging.exception("Wiping disks failed")
1315 22b7f6f8 Thomas Thrainer
        self.LogWarning("Wiping instance disks failed (%s)", err)
1316 22b7f6f8 Thomas Thrainer
        disk_abort = True
1317 22b7f6f8 Thomas Thrainer
1318 22b7f6f8 Thomas Thrainer
    if disk_abort:
1319 22b7f6f8 Thomas Thrainer
      # Something is already wrong with the disks, don't do anything else
1320 22b7f6f8 Thomas Thrainer
      pass
1321 22b7f6f8 Thomas Thrainer
    elif self.op.wait_for_sync:
1322 5eacbcae Thomas Thrainer
      disk_abort = not WaitForSync(self, iobj)
1323 22b7f6f8 Thomas Thrainer
    elif iobj.disk_template in constants.DTS_INT_MIRROR:
1324 22b7f6f8 Thomas Thrainer
      # make sure the disks are not degraded (still sync-ing is ok)
1325 22b7f6f8 Thomas Thrainer
      feedback_fn("* checking mirrors status")
1326 5eacbcae Thomas Thrainer
      disk_abort = not WaitForSync(self, iobj, oneshot=True)
1327 22b7f6f8 Thomas Thrainer
    else:
1328 22b7f6f8 Thomas Thrainer
      disk_abort = False
1329 22b7f6f8 Thomas Thrainer
1330 22b7f6f8 Thomas Thrainer
    if disk_abort:
1331 5eacbcae Thomas Thrainer
      RemoveDisks(self, iobj)
1332 22b7f6f8 Thomas Thrainer
      self.cfg.RemoveInstance(iobj.name)
1333 22b7f6f8 Thomas Thrainer
      # Make sure the instance lock gets removed
1334 22b7f6f8 Thomas Thrainer
      self.remove_locks[locking.LEVEL_INSTANCE] = iobj.name
1335 22b7f6f8 Thomas Thrainer
      raise errors.OpExecError("There are some degraded disks for"
1336 22b7f6f8 Thomas Thrainer
                               " this instance")
1337 22b7f6f8 Thomas Thrainer
1338 1d4a4b26 Thomas Thrainer
    # instance disks are now active
1339 1d4a4b26 Thomas Thrainer
    iobj.disks_active = True
1340 1d4a4b26 Thomas Thrainer
1341 22b7f6f8 Thomas Thrainer
    # Release all node resource locks
1342 5eacbcae Thomas Thrainer
    ReleaseLocks(self, locking.LEVEL_NODE_RES)
1343 22b7f6f8 Thomas Thrainer
1344 22b7f6f8 Thomas Thrainer
    if iobj.disk_template != constants.DT_DISKLESS and not self.adopt_disks:
1345 22b7f6f8 Thomas Thrainer
      # we need to set the disks ID to the primary node, since the
1346 22b7f6f8 Thomas Thrainer
      # preceding code might or might have not done it, depending on
1347 22b7f6f8 Thomas Thrainer
      # disk template and other options
1348 22b7f6f8 Thomas Thrainer
      for disk in iobj.disks:
1349 22b7f6f8 Thomas Thrainer
        self.cfg.SetDiskID(disk, pnode_name)
1350 22b7f6f8 Thomas Thrainer
      if self.op.mode == constants.INSTANCE_CREATE:
1351 22b7f6f8 Thomas Thrainer
        if not self.op.no_install:
1352 22b7f6f8 Thomas Thrainer
          pause_sync = (iobj.disk_template in constants.DTS_INT_MIRROR and
1353 22b7f6f8 Thomas Thrainer
                        not self.op.wait_for_sync)
1354 22b7f6f8 Thomas Thrainer
          if pause_sync:
1355 22b7f6f8 Thomas Thrainer
            feedback_fn("* pausing disk sync to install instance OS")
1356 22b7f6f8 Thomas Thrainer
            result = self.rpc.call_blockdev_pause_resume_sync(pnode_name,
1357 22b7f6f8 Thomas Thrainer
                                                              (iobj.disks,
1358 22b7f6f8 Thomas Thrainer
                                                               iobj), True)
1359 22b7f6f8 Thomas Thrainer
            for idx, success in enumerate(result.payload):
1360 22b7f6f8 Thomas Thrainer
              if not success:
1361 22b7f6f8 Thomas Thrainer
                logging.warn("pause-sync of instance %s for disk %d failed",
1362 22b7f6f8 Thomas Thrainer
                             instance, idx)
1363 22b7f6f8 Thomas Thrainer
1364 22b7f6f8 Thomas Thrainer
          feedback_fn("* running the instance OS create scripts...")
1365 22b7f6f8 Thomas Thrainer
          # FIXME: pass debug option from opcode to backend
1366 22b7f6f8 Thomas Thrainer
          os_add_result = \
1367 22b7f6f8 Thomas Thrainer
            self.rpc.call_instance_os_add(pnode_name, (iobj, None), False,
1368 22b7f6f8 Thomas Thrainer
                                          self.op.debug_level)
1369 22b7f6f8 Thomas Thrainer
          if pause_sync:
1370 22b7f6f8 Thomas Thrainer
            feedback_fn("* resuming disk sync")
1371 22b7f6f8 Thomas Thrainer
            result = self.rpc.call_blockdev_pause_resume_sync(pnode_name,
1372 22b7f6f8 Thomas Thrainer
                                                              (iobj.disks,
1373 22b7f6f8 Thomas Thrainer
                                                               iobj), False)
1374 22b7f6f8 Thomas Thrainer
            for idx, success in enumerate(result.payload):
1375 22b7f6f8 Thomas Thrainer
              if not success:
1376 22b7f6f8 Thomas Thrainer
                logging.warn("resume-sync of instance %s for disk %d failed",
1377 22b7f6f8 Thomas Thrainer
                             instance, idx)
1378 22b7f6f8 Thomas Thrainer
1379 22b7f6f8 Thomas Thrainer
          os_add_result.Raise("Could not add os for instance %s"
1380 22b7f6f8 Thomas Thrainer
                              " on node %s" % (instance, pnode_name))
1381 22b7f6f8 Thomas Thrainer
1382 22b7f6f8 Thomas Thrainer
      else:
1383 22b7f6f8 Thomas Thrainer
        if self.op.mode == constants.INSTANCE_IMPORT:
1384 22b7f6f8 Thomas Thrainer
          feedback_fn("* running the instance OS import scripts...")
1385 22b7f6f8 Thomas Thrainer
1386 22b7f6f8 Thomas Thrainer
          transfers = []
1387 22b7f6f8 Thomas Thrainer
1388 22b7f6f8 Thomas Thrainer
          for idx, image in enumerate(self.src_images):
1389 22b7f6f8 Thomas Thrainer
            if not image:
1390 22b7f6f8 Thomas Thrainer
              continue
1391 22b7f6f8 Thomas Thrainer
1392 22b7f6f8 Thomas Thrainer
            # FIXME: pass debug option from opcode to backend
1393 22b7f6f8 Thomas Thrainer
            dt = masterd.instance.DiskTransfer("disk/%s" % idx,
1394 22b7f6f8 Thomas Thrainer
                                               constants.IEIO_FILE, (image, ),
1395 22b7f6f8 Thomas Thrainer
                                               constants.IEIO_SCRIPT,
1396 22b7f6f8 Thomas Thrainer
                                               (iobj.disks[idx], idx),
1397 22b7f6f8 Thomas Thrainer
                                               None)
1398 22b7f6f8 Thomas Thrainer
            transfers.append(dt)
1399 22b7f6f8 Thomas Thrainer
1400 22b7f6f8 Thomas Thrainer
          import_result = \
1401 22b7f6f8 Thomas Thrainer
            masterd.instance.TransferInstanceData(self, feedback_fn,
1402 22b7f6f8 Thomas Thrainer
                                                  self.op.src_node, pnode_name,
1403 22b7f6f8 Thomas Thrainer
                                                  self.pnode.secondary_ip,
1404 22b7f6f8 Thomas Thrainer
                                                  iobj, transfers)
1405 22b7f6f8 Thomas Thrainer
          if not compat.all(import_result):
1406 22b7f6f8 Thomas Thrainer
            self.LogWarning("Some disks for instance %s on node %s were not"
1407 22b7f6f8 Thomas Thrainer
                            " imported successfully" % (instance, pnode_name))
1408 22b7f6f8 Thomas Thrainer
1409 22b7f6f8 Thomas Thrainer
          rename_from = self._old_instance_name
1410 22b7f6f8 Thomas Thrainer
1411 22b7f6f8 Thomas Thrainer
        elif self.op.mode == constants.INSTANCE_REMOTE_IMPORT:
1412 22b7f6f8 Thomas Thrainer
          feedback_fn("* preparing remote import...")
1413 22b7f6f8 Thomas Thrainer
          # The source cluster will stop the instance before attempting to make
1414 22b7f6f8 Thomas Thrainer
          # a connection. In some cases stopping an instance can take a long
1415 22b7f6f8 Thomas Thrainer
          # time, hence the shutdown timeout is added to the connection
1416 22b7f6f8 Thomas Thrainer
          # timeout.
1417 22b7f6f8 Thomas Thrainer
          connect_timeout = (constants.RIE_CONNECT_TIMEOUT +
1418 22b7f6f8 Thomas Thrainer
                             self.op.source_shutdown_timeout)
1419 22b7f6f8 Thomas Thrainer
          timeouts = masterd.instance.ImportExportTimeouts(connect_timeout)
1420 22b7f6f8 Thomas Thrainer
1421 22b7f6f8 Thomas Thrainer
          assert iobj.primary_node == self.pnode.name
1422 22b7f6f8 Thomas Thrainer
          disk_results = \
1423 22b7f6f8 Thomas Thrainer
            masterd.instance.RemoteImport(self, feedback_fn, iobj, self.pnode,
1424 22b7f6f8 Thomas Thrainer
                                          self.source_x509_ca,
1425 22b7f6f8 Thomas Thrainer
                                          self._cds, timeouts)
1426 22b7f6f8 Thomas Thrainer
          if not compat.all(disk_results):
1427 22b7f6f8 Thomas Thrainer
            # TODO: Should the instance still be started, even if some disks
1428 22b7f6f8 Thomas Thrainer
            # failed to import (valid for local imports, too)?
1429 22b7f6f8 Thomas Thrainer
            self.LogWarning("Some disks for instance %s on node %s were not"
1430 22b7f6f8 Thomas Thrainer
                            " imported successfully" % (instance, pnode_name))
1431 22b7f6f8 Thomas Thrainer
1432 22b7f6f8 Thomas Thrainer
          rename_from = self.source_instance_name
1433 22b7f6f8 Thomas Thrainer
1434 22b7f6f8 Thomas Thrainer
        else:
1435 22b7f6f8 Thomas Thrainer
          # also checked in the prereq part
1436 22b7f6f8 Thomas Thrainer
          raise errors.ProgrammerError("Unknown OS initialization mode '%s'"
1437 22b7f6f8 Thomas Thrainer
                                       % self.op.mode)
1438 22b7f6f8 Thomas Thrainer
1439 22b7f6f8 Thomas Thrainer
        # Run rename script on newly imported instance
1440 22b7f6f8 Thomas Thrainer
        assert iobj.name == instance
1441 22b7f6f8 Thomas Thrainer
        feedback_fn("Running rename script for %s" % instance)
1442 22b7f6f8 Thomas Thrainer
        result = self.rpc.call_instance_run_rename(pnode_name, iobj,
1443 22b7f6f8 Thomas Thrainer
                                                   rename_from,
1444 22b7f6f8 Thomas Thrainer
                                                   self.op.debug_level)
1445 22b7f6f8 Thomas Thrainer
        if result.fail_msg:
1446 22b7f6f8 Thomas Thrainer
          self.LogWarning("Failed to run rename script for %s on node"
1447 22b7f6f8 Thomas Thrainer
                          " %s: %s" % (instance, pnode_name, result.fail_msg))
1448 22b7f6f8 Thomas Thrainer
1449 22b7f6f8 Thomas Thrainer
    assert not self.owned_locks(locking.LEVEL_NODE_RES)
1450 22b7f6f8 Thomas Thrainer
1451 22b7f6f8 Thomas Thrainer
    if self.op.start:
1452 22b7f6f8 Thomas Thrainer
      iobj.admin_state = constants.ADMINST_UP
1453 22b7f6f8 Thomas Thrainer
      self.cfg.Update(iobj, feedback_fn)
1454 22b7f6f8 Thomas Thrainer
      logging.info("Starting instance %s on node %s", instance, pnode_name)
1455 22b7f6f8 Thomas Thrainer
      feedback_fn("* starting instance...")
1456 22b7f6f8 Thomas Thrainer
      result = self.rpc.call_instance_start(pnode_name, (iobj, None, None),
1457 22b7f6f8 Thomas Thrainer
                                            False, self.op.reason)
1458 22b7f6f8 Thomas Thrainer
      result.Raise("Could not start instance")
1459 22b7f6f8 Thomas Thrainer
1460 22b7f6f8 Thomas Thrainer
    return list(iobj.all_nodes)
1461 22b7f6f8 Thomas Thrainer
1462 22b7f6f8 Thomas Thrainer
1463 22b7f6f8 Thomas Thrainer
class LUInstanceRename(LogicalUnit):
1464 22b7f6f8 Thomas Thrainer
  """Rename an instance.
1465 22b7f6f8 Thomas Thrainer

1466 22b7f6f8 Thomas Thrainer
  """
1467 22b7f6f8 Thomas Thrainer
  HPATH = "instance-rename"
1468 22b7f6f8 Thomas Thrainer
  HTYPE = constants.HTYPE_INSTANCE
1469 22b7f6f8 Thomas Thrainer
1470 22b7f6f8 Thomas Thrainer
  def CheckArguments(self):
1471 22b7f6f8 Thomas Thrainer
    """Check arguments.
1472 22b7f6f8 Thomas Thrainer

1473 22b7f6f8 Thomas Thrainer
    """
1474 22b7f6f8 Thomas Thrainer
    if self.op.ip_check and not self.op.name_check:
1475 22b7f6f8 Thomas Thrainer
      # TODO: make the ip check more flexible and not depend on the name check
1476 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("IP address check requires a name check",
1477 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
1478 22b7f6f8 Thomas Thrainer
1479 22b7f6f8 Thomas Thrainer
  def BuildHooksEnv(self):
1480 22b7f6f8 Thomas Thrainer
    """Build hooks env.
1481 22b7f6f8 Thomas Thrainer

1482 22b7f6f8 Thomas Thrainer
    This runs on master, primary and secondary nodes of the instance.
1483 22b7f6f8 Thomas Thrainer

1484 22b7f6f8 Thomas Thrainer
    """
1485 5eacbcae Thomas Thrainer
    env = BuildInstanceHookEnvByObject(self, self.instance)
1486 22b7f6f8 Thomas Thrainer
    env["INSTANCE_NEW_NAME"] = self.op.new_name
1487 22b7f6f8 Thomas Thrainer
    return env
1488 22b7f6f8 Thomas Thrainer
1489 22b7f6f8 Thomas Thrainer
  def BuildHooksNodes(self):
1490 22b7f6f8 Thomas Thrainer
    """Build hooks nodes.
1491 22b7f6f8 Thomas Thrainer

1492 22b7f6f8 Thomas Thrainer
    """
1493 22b7f6f8 Thomas Thrainer
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
1494 22b7f6f8 Thomas Thrainer
    return (nl, nl)
1495 22b7f6f8 Thomas Thrainer
1496 22b7f6f8 Thomas Thrainer
  def CheckPrereq(self):
1497 22b7f6f8 Thomas Thrainer
    """Check prerequisites.
1498 22b7f6f8 Thomas Thrainer

1499 22b7f6f8 Thomas Thrainer
    This checks that the instance is in the cluster and is not running.
1500 22b7f6f8 Thomas Thrainer

1501 22b7f6f8 Thomas Thrainer
    """
1502 5eacbcae Thomas Thrainer
    self.op.instance_name = ExpandInstanceName(self.cfg,
1503 5eacbcae Thomas Thrainer
                                               self.op.instance_name)
1504 22b7f6f8 Thomas Thrainer
    instance = self.cfg.GetInstanceInfo(self.op.instance_name)
1505 22b7f6f8 Thomas Thrainer
    assert instance is not None
1506 5eacbcae Thomas Thrainer
    CheckNodeOnline(self, instance.primary_node)
1507 5eacbcae Thomas Thrainer
    CheckInstanceState(self, instance, INSTANCE_NOT_RUNNING,
1508 5eacbcae Thomas Thrainer
                       msg="cannot rename")
1509 22b7f6f8 Thomas Thrainer
    self.instance = instance
1510 22b7f6f8 Thomas Thrainer
1511 22b7f6f8 Thomas Thrainer
    new_name = self.op.new_name
1512 22b7f6f8 Thomas Thrainer
    if self.op.name_check:
1513 22b7f6f8 Thomas Thrainer
      hostname = _CheckHostnameSane(self, new_name)
1514 22b7f6f8 Thomas Thrainer
      new_name = self.op.new_name = hostname.name
1515 22b7f6f8 Thomas Thrainer
      if (self.op.ip_check and
1516 22b7f6f8 Thomas Thrainer
          netutils.TcpPing(hostname.ip, constants.DEFAULT_NODED_PORT)):
1517 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("IP %s of instance %s already in use" %
1518 22b7f6f8 Thomas Thrainer
                                   (hostname.ip, new_name),
1519 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_NOTUNIQUE)
1520 22b7f6f8 Thomas Thrainer
1521 22b7f6f8 Thomas Thrainer
    instance_list = self.cfg.GetInstanceList()
1522 22b7f6f8 Thomas Thrainer
    if new_name in instance_list and new_name != instance.name:
1523 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
1524 22b7f6f8 Thomas Thrainer
                                 new_name, errors.ECODE_EXISTS)
1525 22b7f6f8 Thomas Thrainer
1526 22b7f6f8 Thomas Thrainer
  def Exec(self, feedback_fn):
1527 22b7f6f8 Thomas Thrainer
    """Rename the instance.
1528 22b7f6f8 Thomas Thrainer

1529 22b7f6f8 Thomas Thrainer
    """
1530 22b7f6f8 Thomas Thrainer
    inst = self.instance
1531 22b7f6f8 Thomas Thrainer
    old_name = inst.name
1532 22b7f6f8 Thomas Thrainer
1533 22b7f6f8 Thomas Thrainer
    rename_file_storage = False
1534 22b7f6f8 Thomas Thrainer
    if (inst.disk_template in constants.DTS_FILEBASED and
1535 22b7f6f8 Thomas Thrainer
        self.op.new_name != inst.name):
1536 22b7f6f8 Thomas Thrainer
      old_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1])
1537 22b7f6f8 Thomas Thrainer
      rename_file_storage = True
1538 22b7f6f8 Thomas Thrainer
1539 22b7f6f8 Thomas Thrainer
    self.cfg.RenameInstance(inst.name, self.op.new_name)
1540 22b7f6f8 Thomas Thrainer
    # Change the instance lock. This is definitely safe while we hold the BGL.
1541 22b7f6f8 Thomas Thrainer
    # Otherwise the new lock would have to be added in acquired mode.
1542 22b7f6f8 Thomas Thrainer
    assert self.REQ_BGL
1543 22b7f6f8 Thomas Thrainer
    assert locking.BGL in self.owned_locks(locking.LEVEL_CLUSTER)
1544 22b7f6f8 Thomas Thrainer
    self.glm.remove(locking.LEVEL_INSTANCE, old_name)
1545 22b7f6f8 Thomas Thrainer
    self.glm.add(locking.LEVEL_INSTANCE, self.op.new_name)
1546 22b7f6f8 Thomas Thrainer
1547 22b7f6f8 Thomas Thrainer
    # re-read the instance from the configuration after rename
1548 22b7f6f8 Thomas Thrainer
    inst = self.cfg.GetInstanceInfo(self.op.new_name)
1549 22b7f6f8 Thomas Thrainer
1550 22b7f6f8 Thomas Thrainer
    if rename_file_storage:
1551 22b7f6f8 Thomas Thrainer
      new_file_storage_dir = os.path.dirname(inst.disks[0].logical_id[1])
1552 22b7f6f8 Thomas Thrainer
      result = self.rpc.call_file_storage_dir_rename(inst.primary_node,
1553 22b7f6f8 Thomas Thrainer
                                                     old_file_storage_dir,
1554 22b7f6f8 Thomas Thrainer
                                                     new_file_storage_dir)
1555 22b7f6f8 Thomas Thrainer
      result.Raise("Could not rename on node %s directory '%s' to '%s'"
1556 22b7f6f8 Thomas Thrainer
                   " (but the instance has been renamed in Ganeti)" %
1557 22b7f6f8 Thomas Thrainer
                   (inst.primary_node, old_file_storage_dir,
1558 22b7f6f8 Thomas Thrainer
                    new_file_storage_dir))
1559 22b7f6f8 Thomas Thrainer
1560 5eacbcae Thomas Thrainer
    StartInstanceDisks(self, inst, None)
1561 22b7f6f8 Thomas Thrainer
    # update info on disks
1562 5eacbcae Thomas Thrainer
    info = GetInstanceInfoText(inst)
1563 22b7f6f8 Thomas Thrainer
    for (idx, disk) in enumerate(inst.disks):
1564 22b7f6f8 Thomas Thrainer
      for node in inst.all_nodes:
1565 22b7f6f8 Thomas Thrainer
        self.cfg.SetDiskID(disk, node)
1566 22b7f6f8 Thomas Thrainer
        result = self.rpc.call_blockdev_setinfo(node, disk, info)
1567 22b7f6f8 Thomas Thrainer
        if result.fail_msg:
1568 22b7f6f8 Thomas Thrainer
          self.LogWarning("Error setting info on node %s for disk %s: %s",
1569 22b7f6f8 Thomas Thrainer
                          node, idx, result.fail_msg)
1570 22b7f6f8 Thomas Thrainer
    try:
1571 22b7f6f8 Thomas Thrainer
      result = self.rpc.call_instance_run_rename(inst.primary_node, inst,
1572 22b7f6f8 Thomas Thrainer
                                                 old_name, self.op.debug_level)
1573 22b7f6f8 Thomas Thrainer
      msg = result.fail_msg
1574 22b7f6f8 Thomas Thrainer
      if msg:
1575 22b7f6f8 Thomas Thrainer
        msg = ("Could not run OS rename script for instance %s on node %s"
1576 22b7f6f8 Thomas Thrainer
               " (but the instance has been renamed in Ganeti): %s" %
1577 22b7f6f8 Thomas Thrainer
               (inst.name, inst.primary_node, msg))
1578 22b7f6f8 Thomas Thrainer
        self.LogWarning(msg)
1579 22b7f6f8 Thomas Thrainer
    finally:
1580 5eacbcae Thomas Thrainer
      ShutdownInstanceDisks(self, inst)
1581 22b7f6f8 Thomas Thrainer
1582 22b7f6f8 Thomas Thrainer
    return inst.name
1583 22b7f6f8 Thomas Thrainer
1584 22b7f6f8 Thomas Thrainer
1585 22b7f6f8 Thomas Thrainer
class LUInstanceRemove(LogicalUnit):
1586 22b7f6f8 Thomas Thrainer
  """Remove an instance.
1587 22b7f6f8 Thomas Thrainer

1588 22b7f6f8 Thomas Thrainer
  """
1589 22b7f6f8 Thomas Thrainer
  HPATH = "instance-remove"
1590 22b7f6f8 Thomas Thrainer
  HTYPE = constants.HTYPE_INSTANCE
1591 22b7f6f8 Thomas Thrainer
  REQ_BGL = False
1592 22b7f6f8 Thomas Thrainer
1593 22b7f6f8 Thomas Thrainer
  def ExpandNames(self):
1594 22b7f6f8 Thomas Thrainer
    self._ExpandAndLockInstance()
1595 22b7f6f8 Thomas Thrainer
    self.needed_locks[locking.LEVEL_NODE] = []
1596 22b7f6f8 Thomas Thrainer
    self.needed_locks[locking.LEVEL_NODE_RES] = []
1597 22b7f6f8 Thomas Thrainer
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
1598 22b7f6f8 Thomas Thrainer
1599 22b7f6f8 Thomas Thrainer
  def DeclareLocks(self, level):
1600 22b7f6f8 Thomas Thrainer
    if level == locking.LEVEL_NODE:
1601 22b7f6f8 Thomas Thrainer
      self._LockInstancesNodes()
1602 22b7f6f8 Thomas Thrainer
    elif level == locking.LEVEL_NODE_RES:
1603 22b7f6f8 Thomas Thrainer
      # Copy node locks
1604 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_RES] = \
1605 5eacbcae Thomas Thrainer
        CopyLockList(self.needed_locks[locking.LEVEL_NODE])
1606 22b7f6f8 Thomas Thrainer
1607 22b7f6f8 Thomas Thrainer
  def BuildHooksEnv(self):
1608 22b7f6f8 Thomas Thrainer
    """Build hooks env.
1609 22b7f6f8 Thomas Thrainer

1610 22b7f6f8 Thomas Thrainer
    This runs on master, primary and secondary nodes of the instance.
1611 22b7f6f8 Thomas Thrainer

1612 22b7f6f8 Thomas Thrainer
    """
1613 5eacbcae Thomas Thrainer
    env = BuildInstanceHookEnvByObject(self, self.instance)
1614 22b7f6f8 Thomas Thrainer
    env["SHUTDOWN_TIMEOUT"] = self.op.shutdown_timeout
1615 22b7f6f8 Thomas Thrainer
    return env
1616 22b7f6f8 Thomas Thrainer
1617 22b7f6f8 Thomas Thrainer
  def BuildHooksNodes(self):
1618 22b7f6f8 Thomas Thrainer
    """Build hooks nodes.
1619 22b7f6f8 Thomas Thrainer

1620 22b7f6f8 Thomas Thrainer
    """
1621 22b7f6f8 Thomas Thrainer
    nl = [self.cfg.GetMasterNode()]
1622 22b7f6f8 Thomas Thrainer
    nl_post = list(self.instance.all_nodes) + nl
1623 22b7f6f8 Thomas Thrainer
    return (nl, nl_post)
1624 22b7f6f8 Thomas Thrainer
1625 22b7f6f8 Thomas Thrainer
  def CheckPrereq(self):
1626 22b7f6f8 Thomas Thrainer
    """Check prerequisites.
1627 22b7f6f8 Thomas Thrainer

1628 22b7f6f8 Thomas Thrainer
    This checks that the instance is in the cluster.
1629 22b7f6f8 Thomas Thrainer

1630 22b7f6f8 Thomas Thrainer
    """
1631 22b7f6f8 Thomas Thrainer
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
1632 22b7f6f8 Thomas Thrainer
    assert self.instance is not None, \
1633 22b7f6f8 Thomas Thrainer
      "Cannot retrieve locked instance %s" % self.op.instance_name
1634 22b7f6f8 Thomas Thrainer
1635 22b7f6f8 Thomas Thrainer
  def Exec(self, feedback_fn):
1636 22b7f6f8 Thomas Thrainer
    """Remove the instance.
1637 22b7f6f8 Thomas Thrainer

1638 22b7f6f8 Thomas Thrainer
    """
1639 22b7f6f8 Thomas Thrainer
    instance = self.instance
1640 22b7f6f8 Thomas Thrainer
    logging.info("Shutting down instance %s on node %s",
1641 22b7f6f8 Thomas Thrainer
                 instance.name, instance.primary_node)
1642 22b7f6f8 Thomas Thrainer
1643 22b7f6f8 Thomas Thrainer
    result = self.rpc.call_instance_shutdown(instance.primary_node, instance,
1644 22b7f6f8 Thomas Thrainer
                                             self.op.shutdown_timeout,
1645 22b7f6f8 Thomas Thrainer
                                             self.op.reason)
1646 22b7f6f8 Thomas Thrainer
    msg = result.fail_msg
1647 22b7f6f8 Thomas Thrainer
    if msg:
1648 22b7f6f8 Thomas Thrainer
      if self.op.ignore_failures:
1649 22b7f6f8 Thomas Thrainer
        feedback_fn("Warning: can't shutdown instance: %s" % msg)
1650 22b7f6f8 Thomas Thrainer
      else:
1651 22b7f6f8 Thomas Thrainer
        raise errors.OpExecError("Could not shutdown instance %s on"
1652 22b7f6f8 Thomas Thrainer
                                 " node %s: %s" %
1653 22b7f6f8 Thomas Thrainer
                                 (instance.name, instance.primary_node, msg))
1654 22b7f6f8 Thomas Thrainer
1655 22b7f6f8 Thomas Thrainer
    assert (self.owned_locks(locking.LEVEL_NODE) ==
1656 22b7f6f8 Thomas Thrainer
            self.owned_locks(locking.LEVEL_NODE_RES))
1657 22b7f6f8 Thomas Thrainer
    assert not (set(instance.all_nodes) -
1658 22b7f6f8 Thomas Thrainer
                self.owned_locks(locking.LEVEL_NODE)), \
1659 22b7f6f8 Thomas Thrainer
      "Not owning correct locks"
1660 22b7f6f8 Thomas Thrainer
1661 5eacbcae Thomas Thrainer
    RemoveInstance(self, feedback_fn, instance, self.op.ignore_failures)
1662 22b7f6f8 Thomas Thrainer
1663 22b7f6f8 Thomas Thrainer
1664 22b7f6f8 Thomas Thrainer
class LUInstanceMove(LogicalUnit):
1665 22b7f6f8 Thomas Thrainer
  """Move an instance by data-copying.
1666 22b7f6f8 Thomas Thrainer

1667 22b7f6f8 Thomas Thrainer
  """
1668 22b7f6f8 Thomas Thrainer
  HPATH = "instance-move"
1669 22b7f6f8 Thomas Thrainer
  HTYPE = constants.HTYPE_INSTANCE
1670 22b7f6f8 Thomas Thrainer
  REQ_BGL = False
1671 22b7f6f8 Thomas Thrainer
1672 22b7f6f8 Thomas Thrainer
  def ExpandNames(self):
1673 22b7f6f8 Thomas Thrainer
    self._ExpandAndLockInstance()
1674 5eacbcae Thomas Thrainer
    target_node = ExpandNodeName(self.cfg, self.op.target_node)
1675 22b7f6f8 Thomas Thrainer
    self.op.target_node = target_node
1676 22b7f6f8 Thomas Thrainer
    self.needed_locks[locking.LEVEL_NODE] = [target_node]
1677 22b7f6f8 Thomas Thrainer
    self.needed_locks[locking.LEVEL_NODE_RES] = []
1678 22b7f6f8 Thomas Thrainer
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
1679 22b7f6f8 Thomas Thrainer
1680 22b7f6f8 Thomas Thrainer
  def DeclareLocks(self, level):
1681 22b7f6f8 Thomas Thrainer
    if level == locking.LEVEL_NODE:
1682 22b7f6f8 Thomas Thrainer
      self._LockInstancesNodes(primary_only=True)
1683 22b7f6f8 Thomas Thrainer
    elif level == locking.LEVEL_NODE_RES:
1684 22b7f6f8 Thomas Thrainer
      # Copy node locks
1685 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_RES] = \
1686 5eacbcae Thomas Thrainer
        CopyLockList(self.needed_locks[locking.LEVEL_NODE])
1687 22b7f6f8 Thomas Thrainer
1688 22b7f6f8 Thomas Thrainer
  def BuildHooksEnv(self):
1689 22b7f6f8 Thomas Thrainer
    """Build hooks env.
1690 22b7f6f8 Thomas Thrainer

1691 22b7f6f8 Thomas Thrainer
    This runs on master, primary and secondary nodes of the instance.
1692 22b7f6f8 Thomas Thrainer

1693 22b7f6f8 Thomas Thrainer
    """
1694 22b7f6f8 Thomas Thrainer
    env = {
1695 22b7f6f8 Thomas Thrainer
      "TARGET_NODE": self.op.target_node,
1696 22b7f6f8 Thomas Thrainer
      "SHUTDOWN_TIMEOUT": self.op.shutdown_timeout,
1697 22b7f6f8 Thomas Thrainer
      }
1698 5eacbcae Thomas Thrainer
    env.update(BuildInstanceHookEnvByObject(self, self.instance))
1699 22b7f6f8 Thomas Thrainer
    return env
1700 22b7f6f8 Thomas Thrainer
1701 22b7f6f8 Thomas Thrainer
  def BuildHooksNodes(self):
1702 22b7f6f8 Thomas Thrainer
    """Build hooks nodes.
1703 22b7f6f8 Thomas Thrainer

1704 22b7f6f8 Thomas Thrainer
    """
1705 22b7f6f8 Thomas Thrainer
    nl = [
1706 22b7f6f8 Thomas Thrainer
      self.cfg.GetMasterNode(),
1707 22b7f6f8 Thomas Thrainer
      self.instance.primary_node,
1708 22b7f6f8 Thomas Thrainer
      self.op.target_node,
1709 22b7f6f8 Thomas Thrainer
      ]
1710 22b7f6f8 Thomas Thrainer
    return (nl, nl)
1711 22b7f6f8 Thomas Thrainer
1712 22b7f6f8 Thomas Thrainer
  def CheckPrereq(self):
1713 22b7f6f8 Thomas Thrainer
    """Check prerequisites.
1714 22b7f6f8 Thomas Thrainer

1715 22b7f6f8 Thomas Thrainer
    This checks that the instance is in the cluster.
1716 22b7f6f8 Thomas Thrainer

1717 22b7f6f8 Thomas Thrainer
    """
1718 22b7f6f8 Thomas Thrainer
    self.instance = instance = self.cfg.GetInstanceInfo(self.op.instance_name)
1719 22b7f6f8 Thomas Thrainer
    assert self.instance is not None, \
1720 22b7f6f8 Thomas Thrainer
      "Cannot retrieve locked instance %s" % self.op.instance_name
1721 22b7f6f8 Thomas Thrainer
1722 22b7f6f8 Thomas Thrainer
    if instance.disk_template not in constants.DTS_COPYABLE:
1723 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Disk template %s not suitable for copying" %
1724 22b7f6f8 Thomas Thrainer
                                 instance.disk_template, errors.ECODE_STATE)
1725 22b7f6f8 Thomas Thrainer
1726 22b7f6f8 Thomas Thrainer
    node = self.cfg.GetNodeInfo(self.op.target_node)
1727 22b7f6f8 Thomas Thrainer
    assert node is not None, \
1728 22b7f6f8 Thomas Thrainer
      "Cannot retrieve locked node %s" % self.op.target_node
1729 22b7f6f8 Thomas Thrainer
1730 22b7f6f8 Thomas Thrainer
    self.target_node = target_node = node.name
1731 22b7f6f8 Thomas Thrainer
1732 22b7f6f8 Thomas Thrainer
    if target_node == instance.primary_node:
1733 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Instance %s is already on the node %s" %
1734 22b7f6f8 Thomas Thrainer
                                 (instance.name, target_node),
1735 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_STATE)
1736 22b7f6f8 Thomas Thrainer
1737 22b7f6f8 Thomas Thrainer
    bep = self.cfg.GetClusterInfo().FillBE(instance)
1738 22b7f6f8 Thomas Thrainer
1739 22b7f6f8 Thomas Thrainer
    for idx, dsk in enumerate(instance.disks):
1740 22b7f6f8 Thomas Thrainer
      if dsk.dev_type not in (constants.LD_LV, constants.LD_FILE):
1741 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Instance disk %d has a complex layout,"
1742 22b7f6f8 Thomas Thrainer
                                   " cannot copy" % idx, errors.ECODE_STATE)
1743 22b7f6f8 Thomas Thrainer
1744 5eacbcae Thomas Thrainer
    CheckNodeOnline(self, target_node)
1745 5eacbcae Thomas Thrainer
    CheckNodeNotDrained(self, target_node)
1746 5eacbcae Thomas Thrainer
    CheckNodeVmCapable(self, target_node)
1747 22b7f6f8 Thomas Thrainer
    cluster = self.cfg.GetClusterInfo()
1748 22b7f6f8 Thomas Thrainer
    group_info = self.cfg.GetNodeGroup(node.group)
1749 22b7f6f8 Thomas Thrainer
    ipolicy = ganeti.masterd.instance.CalculateGroupIPolicy(cluster, group_info)
1750 5eacbcae Thomas Thrainer
    CheckTargetNodeIPolicy(self, ipolicy, instance, node, self.cfg,
1751 5eacbcae Thomas Thrainer
                           ignore=self.op.ignore_ipolicy)
1752 22b7f6f8 Thomas Thrainer
1753 22b7f6f8 Thomas Thrainer
    if instance.admin_state == constants.ADMINST_UP:
1754 22b7f6f8 Thomas Thrainer
      # check memory requirements on the secondary node
1755 5eacbcae Thomas Thrainer
      CheckNodeFreeMemory(self, target_node,
1756 5eacbcae Thomas Thrainer
                          "failing over instance %s" %
1757 5eacbcae Thomas Thrainer
                          instance.name, bep[constants.BE_MAXMEM],
1758 5eacbcae Thomas Thrainer
                          instance.hypervisor)
1759 22b7f6f8 Thomas Thrainer
    else:
1760 22b7f6f8 Thomas Thrainer
      self.LogInfo("Not checking memory on the secondary node as"
1761 22b7f6f8 Thomas Thrainer
                   " instance will not be started")
1762 22b7f6f8 Thomas Thrainer
1763 22b7f6f8 Thomas Thrainer
    # check bridge existance
1764 5eacbcae Thomas Thrainer
    CheckInstanceBridgesExist(self, instance, node=target_node)
1765 22b7f6f8 Thomas Thrainer
1766 22b7f6f8 Thomas Thrainer
  def Exec(self, feedback_fn):
1767 22b7f6f8 Thomas Thrainer
    """Move an instance.
1768 22b7f6f8 Thomas Thrainer

1769 22b7f6f8 Thomas Thrainer
    The move is done by shutting it down on its present node, copying
1770 22b7f6f8 Thomas Thrainer
    the data over (slow) and starting it on the new node.
1771 22b7f6f8 Thomas Thrainer

1772 22b7f6f8 Thomas Thrainer
    """
1773 22b7f6f8 Thomas Thrainer
    instance = self.instance
1774 22b7f6f8 Thomas Thrainer
1775 22b7f6f8 Thomas Thrainer
    source_node = instance.primary_node
1776 22b7f6f8 Thomas Thrainer
    target_node = self.target_node
1777 22b7f6f8 Thomas Thrainer
1778 22b7f6f8 Thomas Thrainer
    self.LogInfo("Shutting down instance %s on source node %s",
1779 22b7f6f8 Thomas Thrainer
                 instance.name, source_node)
1780 22b7f6f8 Thomas Thrainer
1781 22b7f6f8 Thomas Thrainer
    assert (self.owned_locks(locking.LEVEL_NODE) ==
1782 22b7f6f8 Thomas Thrainer
            self.owned_locks(locking.LEVEL_NODE_RES))
1783 22b7f6f8 Thomas Thrainer
1784 22b7f6f8 Thomas Thrainer
    result = self.rpc.call_instance_shutdown(source_node, instance,
1785 22b7f6f8 Thomas Thrainer
                                             self.op.shutdown_timeout,
1786 22b7f6f8 Thomas Thrainer
                                             self.op.reason)
1787 22b7f6f8 Thomas Thrainer
    msg = result.fail_msg
1788 22b7f6f8 Thomas Thrainer
    if msg:
1789 22b7f6f8 Thomas Thrainer
      if self.op.ignore_consistency:
1790 22b7f6f8 Thomas Thrainer
        self.LogWarning("Could not shutdown instance %s on node %s."
1791 22b7f6f8 Thomas Thrainer
                        " Proceeding anyway. Please make sure node"
1792 22b7f6f8 Thomas Thrainer
                        " %s is down. Error details: %s",
1793 22b7f6f8 Thomas Thrainer
                        instance.name, source_node, source_node, msg)
1794 22b7f6f8 Thomas Thrainer
      else:
1795 22b7f6f8 Thomas Thrainer
        raise errors.OpExecError("Could not shutdown instance %s on"
1796 22b7f6f8 Thomas Thrainer
                                 " node %s: %s" %
1797 22b7f6f8 Thomas Thrainer
                                 (instance.name, source_node, msg))
1798 22b7f6f8 Thomas Thrainer
1799 22b7f6f8 Thomas Thrainer
    # create the target disks
1800 22b7f6f8 Thomas Thrainer
    try:
1801 5eacbcae Thomas Thrainer
      CreateDisks(self, instance, target_node=target_node)
1802 22b7f6f8 Thomas Thrainer
    except errors.OpExecError:
1803 22b7f6f8 Thomas Thrainer
      self.LogWarning("Device creation failed")
1804 22b7f6f8 Thomas Thrainer
      self.cfg.ReleaseDRBDMinors(instance.name)
1805 22b7f6f8 Thomas Thrainer
      raise
1806 22b7f6f8 Thomas Thrainer
1807 22b7f6f8 Thomas Thrainer
    cluster_name = self.cfg.GetClusterInfo().cluster_name
1808 22b7f6f8 Thomas Thrainer
1809 22b7f6f8 Thomas Thrainer
    errs = []
1810 22b7f6f8 Thomas Thrainer
    # activate, get path, copy the data over
1811 22b7f6f8 Thomas Thrainer
    for idx, disk in enumerate(instance.disks):
1812 22b7f6f8 Thomas Thrainer
      self.LogInfo("Copying data for disk %d", idx)
1813 22b7f6f8 Thomas Thrainer
      result = self.rpc.call_blockdev_assemble(target_node, (disk, instance),
1814 22b7f6f8 Thomas Thrainer
                                               instance.name, True, idx)
1815 22b7f6f8 Thomas Thrainer
      if result.fail_msg:
1816 22b7f6f8 Thomas Thrainer
        self.LogWarning("Can't assemble newly created disk %d: %s",
1817 22b7f6f8 Thomas Thrainer
                        idx, result.fail_msg)
1818 22b7f6f8 Thomas Thrainer
        errs.append(result.fail_msg)
1819 22b7f6f8 Thomas Thrainer
        break
1820 22b7f6f8 Thomas Thrainer
      dev_path = result.payload
1821 22b7f6f8 Thomas Thrainer
      result = self.rpc.call_blockdev_export(source_node, (disk, instance),
1822 22b7f6f8 Thomas Thrainer
                                             target_node, dev_path,
1823 22b7f6f8 Thomas Thrainer
                                             cluster_name)
1824 22b7f6f8 Thomas Thrainer
      if result.fail_msg:
1825 22b7f6f8 Thomas Thrainer
        self.LogWarning("Can't copy data over for disk %d: %s",
1826 22b7f6f8 Thomas Thrainer
                        idx, result.fail_msg)
1827 22b7f6f8 Thomas Thrainer
        errs.append(result.fail_msg)
1828 22b7f6f8 Thomas Thrainer
        break
1829 22b7f6f8 Thomas Thrainer
1830 22b7f6f8 Thomas Thrainer
    if errs:
1831 22b7f6f8 Thomas Thrainer
      self.LogWarning("Some disks failed to copy, aborting")
1832 22b7f6f8 Thomas Thrainer
      try:
1833 5eacbcae Thomas Thrainer
        RemoveDisks(self, instance, target_node=target_node)
1834 22b7f6f8 Thomas Thrainer
      finally:
1835 22b7f6f8 Thomas Thrainer
        self.cfg.ReleaseDRBDMinors(instance.name)
1836 22b7f6f8 Thomas Thrainer
        raise errors.OpExecError("Errors during disk copy: %s" %
1837 22b7f6f8 Thomas Thrainer
                                 (",".join(errs),))
1838 22b7f6f8 Thomas Thrainer
1839 22b7f6f8 Thomas Thrainer
    instance.primary_node = target_node
1840 22b7f6f8 Thomas Thrainer
    self.cfg.Update(instance, feedback_fn)
1841 22b7f6f8 Thomas Thrainer
1842 22b7f6f8 Thomas Thrainer
    self.LogInfo("Removing the disks on the original node")
1843 5eacbcae Thomas Thrainer
    RemoveDisks(self, instance, target_node=source_node)
1844 22b7f6f8 Thomas Thrainer
1845 22b7f6f8 Thomas Thrainer
    # Only start the instance if it's marked as up
1846 22b7f6f8 Thomas Thrainer
    if instance.admin_state == constants.ADMINST_UP:
1847 22b7f6f8 Thomas Thrainer
      self.LogInfo("Starting instance %s on node %s",
1848 22b7f6f8 Thomas Thrainer
                   instance.name, target_node)
1849 22b7f6f8 Thomas Thrainer
1850 5eacbcae Thomas Thrainer
      disks_ok, _ = AssembleInstanceDisks(self, instance,
1851 5eacbcae Thomas Thrainer
                                          ignore_secondaries=True)
1852 22b7f6f8 Thomas Thrainer
      if not disks_ok:
1853 5eacbcae Thomas Thrainer
        ShutdownInstanceDisks(self, instance)
1854 22b7f6f8 Thomas Thrainer
        raise errors.OpExecError("Can't activate the instance's disks")
1855 22b7f6f8 Thomas Thrainer
1856 22b7f6f8 Thomas Thrainer
      result = self.rpc.call_instance_start(target_node,
1857 22b7f6f8 Thomas Thrainer
                                            (instance, None, None), False,
1858 22b7f6f8 Thomas Thrainer
                                            self.op.reason)
1859 22b7f6f8 Thomas Thrainer
      msg = result.fail_msg
1860 22b7f6f8 Thomas Thrainer
      if msg:
1861 5eacbcae Thomas Thrainer
        ShutdownInstanceDisks(self, instance)
1862 22b7f6f8 Thomas Thrainer
        raise errors.OpExecError("Could not start instance %s on node %s: %s" %
1863 22b7f6f8 Thomas Thrainer
                                 (instance.name, target_node, msg))
1864 22b7f6f8 Thomas Thrainer
1865 22b7f6f8 Thomas Thrainer
1866 22b7f6f8 Thomas Thrainer
class LUInstanceMultiAlloc(NoHooksLU):
1867 22b7f6f8 Thomas Thrainer
  """Allocates multiple instances at the same time.
1868 22b7f6f8 Thomas Thrainer

1869 22b7f6f8 Thomas Thrainer
  """
1870 22b7f6f8 Thomas Thrainer
  REQ_BGL = False
1871 22b7f6f8 Thomas Thrainer
1872 22b7f6f8 Thomas Thrainer
  def CheckArguments(self):
1873 22b7f6f8 Thomas Thrainer
    """Check arguments.
1874 22b7f6f8 Thomas Thrainer

1875 22b7f6f8 Thomas Thrainer
    """
1876 22b7f6f8 Thomas Thrainer
    nodes = []
1877 22b7f6f8 Thomas Thrainer
    for inst in self.op.instances:
1878 22b7f6f8 Thomas Thrainer
      if inst.iallocator is not None:
1879 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("iallocator are not allowed to be set on"
1880 22b7f6f8 Thomas Thrainer
                                   " instance objects", errors.ECODE_INVAL)
1881 22b7f6f8 Thomas Thrainer
      nodes.append(bool(inst.pnode))
1882 22b7f6f8 Thomas Thrainer
      if inst.disk_template in constants.DTS_INT_MIRROR:
1883 22b7f6f8 Thomas Thrainer
        nodes.append(bool(inst.snode))
1884 22b7f6f8 Thomas Thrainer
1885 22b7f6f8 Thomas Thrainer
    has_nodes = compat.any(nodes)
1886 22b7f6f8 Thomas Thrainer
    if compat.all(nodes) ^ has_nodes:
1887 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("There are instance objects providing"
1888 22b7f6f8 Thomas Thrainer
                                 " pnode/snode while others do not",
1889 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
1890 22b7f6f8 Thomas Thrainer
1891 0c072225 Thomas Thrainer
    if not has_nodes and self.op.iallocator is None:
1892 22b7f6f8 Thomas Thrainer
      default_iallocator = self.cfg.GetDefaultIAllocator()
1893 0c072225 Thomas Thrainer
      if default_iallocator:
1894 22b7f6f8 Thomas Thrainer
        self.op.iallocator = default_iallocator
1895 22b7f6f8 Thomas Thrainer
      else:
1896 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("No iallocator or nodes on the instances"
1897 22b7f6f8 Thomas Thrainer
                                   " given and no cluster-wide default"
1898 22b7f6f8 Thomas Thrainer
                                   " iallocator found; please specify either"
1899 22b7f6f8 Thomas Thrainer
                                   " an iallocator or nodes on the instances"
1900 22b7f6f8 Thomas Thrainer
                                   " or set a cluster-wide default iallocator",
1901 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
1902 22b7f6f8 Thomas Thrainer
1903 22b7f6f8 Thomas Thrainer
    _CheckOpportunisticLocking(self.op)
1904 22b7f6f8 Thomas Thrainer
1905 22b7f6f8 Thomas Thrainer
    dups = utils.FindDuplicates([op.instance_name for op in self.op.instances])
1906 22b7f6f8 Thomas Thrainer
    if dups:
1907 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("There are duplicate instance names: %s" %
1908 22b7f6f8 Thomas Thrainer
                                 utils.CommaJoin(dups), errors.ECODE_INVAL)
1909 22b7f6f8 Thomas Thrainer
1910 22b7f6f8 Thomas Thrainer
  def ExpandNames(self):
1911 22b7f6f8 Thomas Thrainer
    """Calculate the locks.
1912 22b7f6f8 Thomas Thrainer

1913 22b7f6f8 Thomas Thrainer
    """
1914 5eacbcae Thomas Thrainer
    self.share_locks = ShareAll()
1915 22b7f6f8 Thomas Thrainer
    self.needed_locks = {
1916 22b7f6f8 Thomas Thrainer
      # iallocator will select nodes and even if no iallocator is used,
1917 22b7f6f8 Thomas Thrainer
      # collisions with LUInstanceCreate should be avoided
1918 22b7f6f8 Thomas Thrainer
      locking.LEVEL_NODE_ALLOC: locking.ALL_SET,
1919 22b7f6f8 Thomas Thrainer
      }
1920 22b7f6f8 Thomas Thrainer
1921 22b7f6f8 Thomas Thrainer
    if self.op.iallocator:
1922 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
1923 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_RES] = locking.ALL_SET
1924 22b7f6f8 Thomas Thrainer
1925 22b7f6f8 Thomas Thrainer
      if self.op.opportunistic_locking:
1926 22b7f6f8 Thomas Thrainer
        self.opportunistic_locks[locking.LEVEL_NODE] = True
1927 22b7f6f8 Thomas Thrainer
    else:
1928 22b7f6f8 Thomas Thrainer
      nodeslist = []
1929 22b7f6f8 Thomas Thrainer
      for inst in self.op.instances:
1930 5eacbcae Thomas Thrainer
        inst.pnode = ExpandNodeName(self.cfg, inst.pnode)
1931 22b7f6f8 Thomas Thrainer
        nodeslist.append(inst.pnode)
1932 22b7f6f8 Thomas Thrainer
        if inst.snode is not None:
1933 5eacbcae Thomas Thrainer
          inst.snode = ExpandNodeName(self.cfg, inst.snode)
1934 22b7f6f8 Thomas Thrainer
          nodeslist.append(inst.snode)
1935 22b7f6f8 Thomas Thrainer
1936 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE] = nodeslist
1937 22b7f6f8 Thomas Thrainer
      # Lock resources of instance's primary and secondary nodes (copy to
1938 22b7f6f8 Thomas Thrainer
      # prevent accidential modification)
1939 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_RES] = list(nodeslist)
1940 22b7f6f8 Thomas Thrainer
1941 4289f617 Thomas Thrainer
  def DeclareLocks(self, level):
1942 4289f617 Thomas Thrainer
    if level == locking.LEVEL_NODE_RES and \
1943 4289f617 Thomas Thrainer
      self.opportunistic_locks[locking.LEVEL_NODE]:
1944 4289f617 Thomas Thrainer
      # Even when using opportunistic locking, we require the same set of
1945 4289f617 Thomas Thrainer
      # NODE_RES locks as we got NODE locks
1946 4289f617 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_RES] = \
1947 4289f617 Thomas Thrainer
        self.owned_locks(locking.LEVEL_NODE)
1948 4289f617 Thomas Thrainer
1949 22b7f6f8 Thomas Thrainer
  def CheckPrereq(self):
1950 22b7f6f8 Thomas Thrainer
    """Check prerequisite.
1951 22b7f6f8 Thomas Thrainer

1952 22b7f6f8 Thomas Thrainer
    """
1953 0c072225 Thomas Thrainer
    if self.op.iallocator:
1954 0c072225 Thomas Thrainer
      cluster = self.cfg.GetClusterInfo()
1955 0c072225 Thomas Thrainer
      default_vg = self.cfg.GetVGName()
1956 0c072225 Thomas Thrainer
      ec_id = self.proc.GetECId()
1957 22b7f6f8 Thomas Thrainer
1958 0c072225 Thomas Thrainer
      if self.op.opportunistic_locking:
1959 0c072225 Thomas Thrainer
        # Only consider nodes for which a lock is held
1960 0c072225 Thomas Thrainer
        node_whitelist = list(self.owned_locks(locking.LEVEL_NODE))
1961 0c072225 Thomas Thrainer
      else:
1962 0c072225 Thomas Thrainer
        node_whitelist = None
1963 22b7f6f8 Thomas Thrainer
1964 0c072225 Thomas Thrainer
      insts = [_CreateInstanceAllocRequest(op, ComputeDisks(op, default_vg),
1965 0c072225 Thomas Thrainer
                                           _ComputeNics(op, cluster, None,
1966 0c072225 Thomas Thrainer
                                                        self.cfg, ec_id),
1967 0c072225 Thomas Thrainer
                                           _ComputeFullBeParams(op, cluster),
1968 0c072225 Thomas Thrainer
                                           node_whitelist)
1969 0c072225 Thomas Thrainer
               for op in self.op.instances]
1970 22b7f6f8 Thomas Thrainer
1971 0c072225 Thomas Thrainer
      req = iallocator.IAReqMultiInstanceAlloc(instances=insts)
1972 0c072225 Thomas Thrainer
      ial = iallocator.IAllocator(self.cfg, self.rpc, req)
1973 22b7f6f8 Thomas Thrainer
1974 0c072225 Thomas Thrainer
      ial.Run(self.op.iallocator)
1975 22b7f6f8 Thomas Thrainer
1976 0c072225 Thomas Thrainer
      if not ial.success:
1977 0c072225 Thomas Thrainer
        raise errors.OpPrereqError("Can't compute nodes using"
1978 0c072225 Thomas Thrainer
                                   " iallocator '%s': %s" %
1979 0c072225 Thomas Thrainer
                                   (self.op.iallocator, ial.info),
1980 0c072225 Thomas Thrainer
                                   errors.ECODE_NORES)
1981 22b7f6f8 Thomas Thrainer
1982 0c072225 Thomas Thrainer
      self.ia_result = ial.result
1983 22b7f6f8 Thomas Thrainer
1984 22b7f6f8 Thomas Thrainer
    if self.op.dry_run:
1985 22b7f6f8 Thomas Thrainer
      self.dry_run_result = objects.FillDict(self._ConstructPartialResult(), {
1986 22b7f6f8 Thomas Thrainer
        constants.JOB_IDS_KEY: [],
1987 22b7f6f8 Thomas Thrainer
        })
1988 22b7f6f8 Thomas Thrainer
1989 22b7f6f8 Thomas Thrainer
  def _ConstructPartialResult(self):
1990 22b7f6f8 Thomas Thrainer
    """Contructs the partial result.
1991 22b7f6f8 Thomas Thrainer

1992 22b7f6f8 Thomas Thrainer
    """
1993 0c072225 Thomas Thrainer
    if self.op.iallocator:
1994 0c072225 Thomas Thrainer
      (allocatable, failed_insts) = self.ia_result
1995 0c072225 Thomas Thrainer
      allocatable_insts = map(compat.fst, allocatable)
1996 0c072225 Thomas Thrainer
    else:
1997 0c072225 Thomas Thrainer
      allocatable_insts = [op.instance_name for op in self.op.instances]
1998 0c072225 Thomas Thrainer
      failed_insts = []
1999 0c072225 Thomas Thrainer
2000 22b7f6f8 Thomas Thrainer
    return {
2001 0c072225 Thomas Thrainer
      opcodes.OpInstanceMultiAlloc.ALLOCATABLE_KEY: allocatable_insts,
2002 0c072225 Thomas Thrainer
      opcodes.OpInstanceMultiAlloc.FAILED_KEY: failed_insts,
2003 22b7f6f8 Thomas Thrainer
      }
2004 22b7f6f8 Thomas Thrainer
2005 22b7f6f8 Thomas Thrainer
  def Exec(self, feedback_fn):
2006 22b7f6f8 Thomas Thrainer
    """Executes the opcode.
2007 22b7f6f8 Thomas Thrainer

2008 22b7f6f8 Thomas Thrainer
    """
2009 22b7f6f8 Thomas Thrainer
    jobs = []
2010 0c072225 Thomas Thrainer
    if self.op.iallocator:
2011 0c072225 Thomas Thrainer
      op2inst = dict((op.instance_name, op) for op in self.op.instances)
2012 0c072225 Thomas Thrainer
      (allocatable, failed) = self.ia_result
2013 22b7f6f8 Thomas Thrainer
2014 0c072225 Thomas Thrainer
      for (name, nodes) in allocatable:
2015 0c072225 Thomas Thrainer
        op = op2inst.pop(name)
2016 22b7f6f8 Thomas Thrainer
2017 0c072225 Thomas Thrainer
        if len(nodes) > 1:
2018 0c072225 Thomas Thrainer
          (op.pnode, op.snode) = nodes
2019 0c072225 Thomas Thrainer
        else:
2020 0c072225 Thomas Thrainer
          (op.pnode,) = nodes
2021 0c072225 Thomas Thrainer
2022 0c072225 Thomas Thrainer
        jobs.append([op])
2023 22b7f6f8 Thomas Thrainer
2024 0c072225 Thomas Thrainer
      missing = set(op2inst.keys()) - set(failed)
2025 0c072225 Thomas Thrainer
      assert not missing, \
2026 0c072225 Thomas Thrainer
        "Iallocator did return incomplete result: %s" % \
2027 0c072225 Thomas Thrainer
        utils.CommaJoin(missing)
2028 0c072225 Thomas Thrainer
    else:
2029 0c072225 Thomas Thrainer
      jobs.extend([op] for op in self.op.instances)
2030 22b7f6f8 Thomas Thrainer
2031 22b7f6f8 Thomas Thrainer
    return ResultWithJobs(jobs, **self._ConstructPartialResult())
2032 22b7f6f8 Thomas Thrainer
2033 22b7f6f8 Thomas Thrainer
2034 22b7f6f8 Thomas Thrainer
class _InstNicModPrivate:
2035 22b7f6f8 Thomas Thrainer
  """Data structure for network interface modifications.
2036 22b7f6f8 Thomas Thrainer

2037 22b7f6f8 Thomas Thrainer
  Used by L{LUInstanceSetParams}.
2038 22b7f6f8 Thomas Thrainer

2039 22b7f6f8 Thomas Thrainer
  """
2040 22b7f6f8 Thomas Thrainer
  def __init__(self):
2041 22b7f6f8 Thomas Thrainer
    self.params = None
2042 22b7f6f8 Thomas Thrainer
    self.filled = None
2043 22b7f6f8 Thomas Thrainer
2044 22b7f6f8 Thomas Thrainer
2045 5eacbcae Thomas Thrainer
def _PrepareContainerMods(mods, private_fn):
2046 22b7f6f8 Thomas Thrainer
  """Prepares a list of container modifications by adding a private data field.
2047 22b7f6f8 Thomas Thrainer

2048 22b7f6f8 Thomas Thrainer
  @type mods: list of tuples; (operation, index, parameters)
2049 22b7f6f8 Thomas Thrainer
  @param mods: List of modifications
2050 22b7f6f8 Thomas Thrainer
  @type private_fn: callable or None
2051 22b7f6f8 Thomas Thrainer
  @param private_fn: Callable for constructing a private data field for a
2052 22b7f6f8 Thomas Thrainer
    modification
2053 22b7f6f8 Thomas Thrainer
  @rtype: list
2054 22b7f6f8 Thomas Thrainer

2055 22b7f6f8 Thomas Thrainer
  """
2056 22b7f6f8 Thomas Thrainer
  if private_fn is None:
2057 22b7f6f8 Thomas Thrainer
    fn = lambda: None
2058 22b7f6f8 Thomas Thrainer
  else:
2059 22b7f6f8 Thomas Thrainer
    fn = private_fn
2060 22b7f6f8 Thomas Thrainer
2061 22b7f6f8 Thomas Thrainer
  return [(op, idx, params, fn()) for (op, idx, params) in mods]
2062 22b7f6f8 Thomas Thrainer
2063 22b7f6f8 Thomas Thrainer
2064 22b7f6f8 Thomas Thrainer
def _CheckNodesPhysicalCPUs(lu, nodenames, requested, hypervisor_name):
2065 22b7f6f8 Thomas Thrainer
  """Checks if nodes have enough physical CPUs
2066 22b7f6f8 Thomas Thrainer

2067 22b7f6f8 Thomas Thrainer
  This function checks if all given nodes have the needed number of
2068 22b7f6f8 Thomas Thrainer
  physical CPUs. In case any node has less CPUs or we cannot get the
2069 22b7f6f8 Thomas Thrainer
  information from the node, this function raises an OpPrereqError
2070 22b7f6f8 Thomas Thrainer
  exception.
2071 22b7f6f8 Thomas Thrainer

2072 22b7f6f8 Thomas Thrainer
  @type lu: C{LogicalUnit}
2073 22b7f6f8 Thomas Thrainer
  @param lu: a logical unit from which we get configuration data
2074 22b7f6f8 Thomas Thrainer
  @type nodenames: C{list}
2075 22b7f6f8 Thomas Thrainer
  @param nodenames: the list of node names to check
2076 22b7f6f8 Thomas Thrainer
  @type requested: C{int}
2077 22b7f6f8 Thomas Thrainer
  @param requested: the minimum acceptable number of physical CPUs
2078 22b7f6f8 Thomas Thrainer
  @raise errors.OpPrereqError: if the node doesn't have enough CPUs,
2079 22b7f6f8 Thomas Thrainer
      or we cannot check the node
2080 22b7f6f8 Thomas Thrainer

2081 22b7f6f8 Thomas Thrainer
  """
2082 22b7f6f8 Thomas Thrainer
  nodeinfo = lu.rpc.call_node_info(nodenames, None, [hypervisor_name], None)
2083 22b7f6f8 Thomas Thrainer
  for node in nodenames:
2084 22b7f6f8 Thomas Thrainer
    info = nodeinfo[node]
2085 22b7f6f8 Thomas Thrainer
    info.Raise("Cannot get current information from node %s" % node,
2086 22b7f6f8 Thomas Thrainer
               prereq=True, ecode=errors.ECODE_ENVIRON)
2087 22b7f6f8 Thomas Thrainer
    (_, _, (hv_info, )) = info.payload
2088 22b7f6f8 Thomas Thrainer
    num_cpus = hv_info.get("cpu_total", None)
2089 22b7f6f8 Thomas Thrainer
    if not isinstance(num_cpus, int):
2090 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Can't compute the number of physical CPUs"
2091 22b7f6f8 Thomas Thrainer
                                 " on node %s, result was '%s'" %
2092 22b7f6f8 Thomas Thrainer
                                 (node, num_cpus), errors.ECODE_ENVIRON)
2093 22b7f6f8 Thomas Thrainer
    if requested > num_cpus:
2094 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Node %s has %s physical CPUs, but %s are "
2095 22b7f6f8 Thomas Thrainer
                                 "required" % (node, num_cpus, requested),
2096 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_NORES)
2097 22b7f6f8 Thomas Thrainer
2098 22b7f6f8 Thomas Thrainer
2099 22b7f6f8 Thomas Thrainer
def GetItemFromContainer(identifier, kind, container):
2100 22b7f6f8 Thomas Thrainer
  """Return the item refered by the identifier.
2101 22b7f6f8 Thomas Thrainer

2102 22b7f6f8 Thomas Thrainer
  @type identifier: string
2103 22b7f6f8 Thomas Thrainer
  @param identifier: Item index or name or UUID
2104 22b7f6f8 Thomas Thrainer
  @type kind: string
2105 22b7f6f8 Thomas Thrainer
  @param kind: One-word item description
2106 22b7f6f8 Thomas Thrainer
  @type container: list
2107 22b7f6f8 Thomas Thrainer
  @param container: Container to get the item from
2108 22b7f6f8 Thomas Thrainer

2109 22b7f6f8 Thomas Thrainer
  """
2110 22b7f6f8 Thomas Thrainer
  # Index
2111 22b7f6f8 Thomas Thrainer
  try:
2112 22b7f6f8 Thomas Thrainer
    idx = int(identifier)
2113 22b7f6f8 Thomas Thrainer
    if idx == -1:
2114 22b7f6f8 Thomas Thrainer
      # Append
2115 22b7f6f8 Thomas Thrainer
      absidx = len(container) - 1
2116 22b7f6f8 Thomas Thrainer
    elif idx < 0:
2117 22b7f6f8 Thomas Thrainer
      raise IndexError("Not accepting negative indices other than -1")
2118 22b7f6f8 Thomas Thrainer
    elif idx > len(container):
2119 22b7f6f8 Thomas Thrainer
      raise IndexError("Got %s index %s, but there are only %s" %
2120 22b7f6f8 Thomas Thrainer
                       (kind, idx, len(container)))
2121 22b7f6f8 Thomas Thrainer
    else:
2122 22b7f6f8 Thomas Thrainer
      absidx = idx
2123 22b7f6f8 Thomas Thrainer
    return (absidx, container[idx])
2124 22b7f6f8 Thomas Thrainer
  except ValueError:
2125 22b7f6f8 Thomas Thrainer
    pass
2126 22b7f6f8 Thomas Thrainer
2127 22b7f6f8 Thomas Thrainer
  for idx, item in enumerate(container):
2128 22b7f6f8 Thomas Thrainer
    if item.uuid == identifier or item.name == identifier:
2129 22b7f6f8 Thomas Thrainer
      return (idx, item)
2130 22b7f6f8 Thomas Thrainer
2131 22b7f6f8 Thomas Thrainer
  raise errors.OpPrereqError("Cannot find %s with identifier %s" %
2132 22b7f6f8 Thomas Thrainer
                             (kind, identifier), errors.ECODE_NOENT)
2133 22b7f6f8 Thomas Thrainer
2134 22b7f6f8 Thomas Thrainer
2135 5eacbcae Thomas Thrainer
def _ApplyContainerMods(kind, container, chgdesc, mods,
2136 5eacbcae Thomas Thrainer
                        create_fn, modify_fn, remove_fn):
2137 22b7f6f8 Thomas Thrainer
  """Applies descriptions in C{mods} to C{container}.
2138 22b7f6f8 Thomas Thrainer

2139 22b7f6f8 Thomas Thrainer
  @type kind: string
2140 22b7f6f8 Thomas Thrainer
  @param kind: One-word item description
2141 22b7f6f8 Thomas Thrainer
  @type container: list
2142 22b7f6f8 Thomas Thrainer
  @param container: Container to modify
2143 22b7f6f8 Thomas Thrainer
  @type chgdesc: None or list
2144 22b7f6f8 Thomas Thrainer
  @param chgdesc: List of applied changes
2145 22b7f6f8 Thomas Thrainer
  @type mods: list
2146 5eacbcae Thomas Thrainer
  @param mods: Modifications as returned by L{_PrepareContainerMods}
2147 22b7f6f8 Thomas Thrainer
  @type create_fn: callable
2148 22b7f6f8 Thomas Thrainer
  @param create_fn: Callback for creating a new item (L{constants.DDM_ADD});
2149 22b7f6f8 Thomas Thrainer
    receives absolute item index, parameters and private data object as added
2150 5eacbcae Thomas Thrainer
    by L{_PrepareContainerMods}, returns tuple containing new item and changes
2151 22b7f6f8 Thomas Thrainer
    as list
2152 22b7f6f8 Thomas Thrainer
  @type modify_fn: callable
2153 22b7f6f8 Thomas Thrainer
  @param modify_fn: Callback for modifying an existing item
2154 22b7f6f8 Thomas Thrainer
    (L{constants.DDM_MODIFY}); receives absolute item index, item, parameters
2155 5eacbcae Thomas Thrainer
    and private data object as added by L{_PrepareContainerMods}, returns
2156 22b7f6f8 Thomas Thrainer
    changes as list
2157 22b7f6f8 Thomas Thrainer
  @type remove_fn: callable
2158 22b7f6f8 Thomas Thrainer
  @param remove_fn: Callback on removing item; receives absolute item index,
2159 5eacbcae Thomas Thrainer
    item and private data object as added by L{_PrepareContainerMods}
2160 22b7f6f8 Thomas Thrainer

2161 22b7f6f8 Thomas Thrainer
  """
2162 22b7f6f8 Thomas Thrainer
  for (op, identifier, params, private) in mods:
2163 22b7f6f8 Thomas Thrainer
    changes = None
2164 22b7f6f8 Thomas Thrainer
2165 22b7f6f8 Thomas Thrainer
    if op == constants.DDM_ADD:
2166 22b7f6f8 Thomas Thrainer
      # Calculate where item will be added
2167 22b7f6f8 Thomas Thrainer
      # When adding an item, identifier can only be an index
2168 22b7f6f8 Thomas Thrainer
      try:
2169 22b7f6f8 Thomas Thrainer
        idx = int(identifier)
2170 22b7f6f8 Thomas Thrainer
      except ValueError:
2171 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Only possitive integer or -1 is accepted as"
2172 22b7f6f8 Thomas Thrainer
                                   " identifier for %s" % constants.DDM_ADD,
2173 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
2174 22b7f6f8 Thomas Thrainer
      if idx == -1:
2175 22b7f6f8 Thomas Thrainer
        addidx = len(container)
2176 22b7f6f8 Thomas Thrainer
      else:
2177 22b7f6f8 Thomas Thrainer
        if idx < 0:
2178 22b7f6f8 Thomas Thrainer
          raise IndexError("Not accepting negative indices other than -1")
2179 22b7f6f8 Thomas Thrainer
        elif idx > len(container):
2180 22b7f6f8 Thomas Thrainer
          raise IndexError("Got %s index %s, but there are only %s" %
2181 22b7f6f8 Thomas Thrainer
                           (kind, idx, len(container)))
2182 22b7f6f8 Thomas Thrainer
        addidx = idx
2183 22b7f6f8 Thomas Thrainer
2184 22b7f6f8 Thomas Thrainer
      if create_fn is None:
2185 22b7f6f8 Thomas Thrainer
        item = params
2186 22b7f6f8 Thomas Thrainer
      else:
2187 22b7f6f8 Thomas Thrainer
        (item, changes) = create_fn(addidx, params, private)
2188 22b7f6f8 Thomas Thrainer
2189 22b7f6f8 Thomas Thrainer
      if idx == -1:
2190 22b7f6f8 Thomas Thrainer
        container.append(item)
2191 22b7f6f8 Thomas Thrainer
      else:
2192 22b7f6f8 Thomas Thrainer
        assert idx >= 0
2193 22b7f6f8 Thomas Thrainer
        assert idx <= len(container)
2194 22b7f6f8 Thomas Thrainer
        # list.insert does so before the specified index
2195 22b7f6f8 Thomas Thrainer
        container.insert(idx, item)
2196 22b7f6f8 Thomas Thrainer
    else:
2197 22b7f6f8 Thomas Thrainer
      # Retrieve existing item
2198 22b7f6f8 Thomas Thrainer
      (absidx, item) = GetItemFromContainer(identifier, kind, container)
2199 22b7f6f8 Thomas Thrainer
2200 22b7f6f8 Thomas Thrainer
      if op == constants.DDM_REMOVE:
2201 22b7f6f8 Thomas Thrainer
        assert not params
2202 22b7f6f8 Thomas Thrainer
2203 22b7f6f8 Thomas Thrainer
        if remove_fn is not None:
2204 22b7f6f8 Thomas Thrainer
          remove_fn(absidx, item, private)
2205 22b7f6f8 Thomas Thrainer
2206 22b7f6f8 Thomas Thrainer
        changes = [("%s/%s" % (kind, absidx), "remove")]
2207 22b7f6f8 Thomas Thrainer
2208 22b7f6f8 Thomas Thrainer
        assert container[absidx] == item
2209 22b7f6f8 Thomas Thrainer
        del container[absidx]
2210 22b7f6f8 Thomas Thrainer
      elif op == constants.DDM_MODIFY:
2211 22b7f6f8 Thomas Thrainer
        if modify_fn is not None:
2212 22b7f6f8 Thomas Thrainer
          changes = modify_fn(absidx, item, params, private)
2213 22b7f6f8 Thomas Thrainer
      else:
2214 22b7f6f8 Thomas Thrainer
        raise errors.ProgrammerError("Unhandled operation '%s'" % op)
2215 22b7f6f8 Thomas Thrainer
2216 22b7f6f8 Thomas Thrainer
    assert _TApplyContModsCbChanges(changes)
2217 22b7f6f8 Thomas Thrainer
2218 22b7f6f8 Thomas Thrainer
    if not (chgdesc is None or changes is None):
2219 22b7f6f8 Thomas Thrainer
      chgdesc.extend(changes)
2220 22b7f6f8 Thomas Thrainer
2221 22b7f6f8 Thomas Thrainer
2222 22b7f6f8 Thomas Thrainer
def _UpdateIvNames(base_index, disks):
2223 22b7f6f8 Thomas Thrainer
  """Updates the C{iv_name} attribute of disks.
2224 22b7f6f8 Thomas Thrainer

2225 22b7f6f8 Thomas Thrainer
  @type disks: list of L{objects.Disk}
2226 22b7f6f8 Thomas Thrainer

2227 22b7f6f8 Thomas Thrainer
  """
2228 22b7f6f8 Thomas Thrainer
  for (idx, disk) in enumerate(disks):
2229 22b7f6f8 Thomas Thrainer
    disk.iv_name = "disk/%s" % (base_index + idx, )
2230 22b7f6f8 Thomas Thrainer
2231 22b7f6f8 Thomas Thrainer
2232 22b7f6f8 Thomas Thrainer
class LUInstanceSetParams(LogicalUnit):
2233 22b7f6f8 Thomas Thrainer
  """Modifies an instances's parameters.
2234 22b7f6f8 Thomas Thrainer

2235 22b7f6f8 Thomas Thrainer
  """
2236 22b7f6f8 Thomas Thrainer
  HPATH = "instance-modify"
2237 22b7f6f8 Thomas Thrainer
  HTYPE = constants.HTYPE_INSTANCE
2238 22b7f6f8 Thomas Thrainer
  REQ_BGL = False
2239 22b7f6f8 Thomas Thrainer
2240 22b7f6f8 Thomas Thrainer
  @staticmethod
2241 22b7f6f8 Thomas Thrainer
  def _UpgradeDiskNicMods(kind, mods, verify_fn):
2242 22b7f6f8 Thomas Thrainer
    assert ht.TList(mods)
2243 22b7f6f8 Thomas Thrainer
    assert not mods or len(mods[0]) in (2, 3)
2244 22b7f6f8 Thomas Thrainer
2245 22b7f6f8 Thomas Thrainer
    if mods and len(mods[0]) == 2:
2246 22b7f6f8 Thomas Thrainer
      result = []
2247 22b7f6f8 Thomas Thrainer
2248 22b7f6f8 Thomas Thrainer
      addremove = 0
2249 22b7f6f8 Thomas Thrainer
      for op, params in mods:
2250 22b7f6f8 Thomas Thrainer
        if op in (constants.DDM_ADD, constants.DDM_REMOVE):
2251 22b7f6f8 Thomas Thrainer
          result.append((op, -1, params))
2252 22b7f6f8 Thomas Thrainer
          addremove += 1
2253 22b7f6f8 Thomas Thrainer
2254 22b7f6f8 Thomas Thrainer
          if addremove > 1:
2255 22b7f6f8 Thomas Thrainer
            raise errors.OpPrereqError("Only one %s add or remove operation is"
2256 22b7f6f8 Thomas Thrainer
                                       " supported at a time" % kind,
2257 22b7f6f8 Thomas Thrainer
                                       errors.ECODE_INVAL)
2258 22b7f6f8 Thomas Thrainer
        else:
2259 22b7f6f8 Thomas Thrainer
          result.append((constants.DDM_MODIFY, op, params))
2260 22b7f6f8 Thomas Thrainer
2261 22b7f6f8 Thomas Thrainer
      assert verify_fn(result)
2262 22b7f6f8 Thomas Thrainer
    else:
2263 22b7f6f8 Thomas Thrainer
      result = mods
2264 22b7f6f8 Thomas Thrainer
2265 22b7f6f8 Thomas Thrainer
    return result
2266 22b7f6f8 Thomas Thrainer
2267 22b7f6f8 Thomas Thrainer
  @staticmethod
2268 22b7f6f8 Thomas Thrainer
  def _CheckMods(kind, mods, key_types, item_fn):
2269 22b7f6f8 Thomas Thrainer
    """Ensures requested disk/NIC modifications are valid.
2270 22b7f6f8 Thomas Thrainer

2271 22b7f6f8 Thomas Thrainer
    """
2272 22b7f6f8 Thomas Thrainer
    for (op, _, params) in mods:
2273 22b7f6f8 Thomas Thrainer
      assert ht.TDict(params)
2274 22b7f6f8 Thomas Thrainer
2275 22b7f6f8 Thomas Thrainer
      # If 'key_types' is an empty dict, we assume we have an
2276 22b7f6f8 Thomas Thrainer
      # 'ext' template and thus do not ForceDictType
2277 22b7f6f8 Thomas Thrainer
      if key_types:
2278 22b7f6f8 Thomas Thrainer
        utils.ForceDictType(params, key_types)
2279 22b7f6f8 Thomas Thrainer
2280 22b7f6f8 Thomas Thrainer
      if op == constants.DDM_REMOVE:
2281 22b7f6f8 Thomas Thrainer
        if params:
2282 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("No settings should be passed when"
2283 22b7f6f8 Thomas Thrainer
                                     " removing a %s" % kind,
2284 22b7f6f8 Thomas Thrainer
                                     errors.ECODE_INVAL)
2285 22b7f6f8 Thomas Thrainer
      elif op in (constants.DDM_ADD, constants.DDM_MODIFY):
2286 22b7f6f8 Thomas Thrainer
        item_fn(op, params)
2287 22b7f6f8 Thomas Thrainer
      else:
2288 22b7f6f8 Thomas Thrainer
        raise errors.ProgrammerError("Unhandled operation '%s'" % op)
2289 22b7f6f8 Thomas Thrainer
2290 c5c72215 Dimitris Aragiorgis
  def _VerifyDiskModification(self, op, params):
2291 22b7f6f8 Thomas Thrainer
    """Verifies a disk modification.
2292 22b7f6f8 Thomas Thrainer

2293 22b7f6f8 Thomas Thrainer
    """
2294 22b7f6f8 Thomas Thrainer
    if op == constants.DDM_ADD:
2295 22b7f6f8 Thomas Thrainer
      mode = params.setdefault(constants.IDISK_MODE, constants.DISK_RDWR)
2296 22b7f6f8 Thomas Thrainer
      if mode not in constants.DISK_ACCESS_SET:
2297 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Invalid disk access mode '%s'" % mode,
2298 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
2299 22b7f6f8 Thomas Thrainer
2300 22b7f6f8 Thomas Thrainer
      size = params.get(constants.IDISK_SIZE, None)
2301 22b7f6f8 Thomas Thrainer
      if size is None:
2302 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Required disk parameter '%s' missing" %
2303 22b7f6f8 Thomas Thrainer
                                   constants.IDISK_SIZE, errors.ECODE_INVAL)
2304 22b7f6f8 Thomas Thrainer
2305 22b7f6f8 Thomas Thrainer
      try:
2306 22b7f6f8 Thomas Thrainer
        size = int(size)
2307 22b7f6f8 Thomas Thrainer
      except (TypeError, ValueError), err:
2308 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Invalid disk size parameter: %s" % err,
2309 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
2310 22b7f6f8 Thomas Thrainer
2311 22b7f6f8 Thomas Thrainer
      params[constants.IDISK_SIZE] = size
2312 22b7f6f8 Thomas Thrainer
      name = params.get(constants.IDISK_NAME, None)
2313 22b7f6f8 Thomas Thrainer
      if name is not None and name.lower() == constants.VALUE_NONE:
2314 22b7f6f8 Thomas Thrainer
        params[constants.IDISK_NAME] = None
2315 22b7f6f8 Thomas Thrainer
2316 22b7f6f8 Thomas Thrainer
    elif op == constants.DDM_MODIFY:
2317 22b7f6f8 Thomas Thrainer
      if constants.IDISK_SIZE in params:
2318 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Disk size change not possible, use"
2319 22b7f6f8 Thomas Thrainer
                                   " grow-disk", errors.ECODE_INVAL)
2320 c5c72215 Dimitris Aragiorgis
2321 c5c72215 Dimitris Aragiorgis
      # Disk modification supports changing only the disk name and mode.
2322 c5c72215 Dimitris Aragiorgis
      # Changing arbitrary parameters is allowed only for ext disk template",
2323 c5c72215 Dimitris Aragiorgis
      if self.instance.disk_template != constants.DT_EXT:
2324 c5c72215 Dimitris Aragiorgis
        utils.ForceDictType(params, constants.MODIFIABLE_IDISK_PARAMS_TYPES)
2325 c5c72215 Dimitris Aragiorgis
2326 22b7f6f8 Thomas Thrainer
      name = params.get(constants.IDISK_NAME, None)
2327 22b7f6f8 Thomas Thrainer
      if name is not None and name.lower() == constants.VALUE_NONE:
2328 22b7f6f8 Thomas Thrainer
        params[constants.IDISK_NAME] = None
2329 22b7f6f8 Thomas Thrainer
2330 22b7f6f8 Thomas Thrainer
  @staticmethod
2331 22b7f6f8 Thomas Thrainer
  def _VerifyNicModification(op, params):
2332 22b7f6f8 Thomas Thrainer
    """Verifies a network interface modification.
2333 22b7f6f8 Thomas Thrainer

2334 22b7f6f8 Thomas Thrainer
    """
2335 22b7f6f8 Thomas Thrainer
    if op in (constants.DDM_ADD, constants.DDM_MODIFY):
2336 22b7f6f8 Thomas Thrainer
      ip = params.get(constants.INIC_IP, None)
2337 22b7f6f8 Thomas Thrainer
      name = params.get(constants.INIC_NAME, None)
2338 22b7f6f8 Thomas Thrainer
      req_net = params.get(constants.INIC_NETWORK, None)
2339 22b7f6f8 Thomas Thrainer
      link = params.get(constants.NIC_LINK, None)
2340 22b7f6f8 Thomas Thrainer
      mode = params.get(constants.NIC_MODE, None)
2341 22b7f6f8 Thomas Thrainer
      if name is not None and name.lower() == constants.VALUE_NONE:
2342 22b7f6f8 Thomas Thrainer
        params[constants.INIC_NAME] = None
2343 22b7f6f8 Thomas Thrainer
      if req_net is not None:
2344 22b7f6f8 Thomas Thrainer
        if req_net.lower() == constants.VALUE_NONE:
2345 22b7f6f8 Thomas Thrainer
          params[constants.INIC_NETWORK] = None
2346 22b7f6f8 Thomas Thrainer
          req_net = None
2347 22b7f6f8 Thomas Thrainer
        elif link is not None or mode is not None:
2348 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("If network is given"
2349 22b7f6f8 Thomas Thrainer
                                     " mode or link should not",
2350 22b7f6f8 Thomas Thrainer
                                     errors.ECODE_INVAL)
2351 22b7f6f8 Thomas Thrainer
2352 22b7f6f8 Thomas Thrainer
      if op == constants.DDM_ADD:
2353 22b7f6f8 Thomas Thrainer
        macaddr = params.get(constants.INIC_MAC, None)
2354 22b7f6f8 Thomas Thrainer
        if macaddr is None:
2355 22b7f6f8 Thomas Thrainer
          params[constants.INIC_MAC] = constants.VALUE_AUTO
2356 22b7f6f8 Thomas Thrainer
2357 22b7f6f8 Thomas Thrainer
      if ip is not None:
2358 22b7f6f8 Thomas Thrainer
        if ip.lower() == constants.VALUE_NONE:
2359 22b7f6f8 Thomas Thrainer
          params[constants.INIC_IP] = None
2360 22b7f6f8 Thomas Thrainer
        else:
2361 22b7f6f8 Thomas Thrainer
          if ip.lower() == constants.NIC_IP_POOL:
2362 22b7f6f8 Thomas Thrainer
            if op == constants.DDM_ADD and req_net is None:
2363 22b7f6f8 Thomas Thrainer
              raise errors.OpPrereqError("If ip=pool, parameter network"
2364 22b7f6f8 Thomas Thrainer
                                         " cannot be none",
2365 22b7f6f8 Thomas Thrainer
                                         errors.ECODE_INVAL)
2366 22b7f6f8 Thomas Thrainer
          else:
2367 22b7f6f8 Thomas Thrainer
            if not netutils.IPAddress.IsValid(ip):
2368 22b7f6f8 Thomas Thrainer
              raise errors.OpPrereqError("Invalid IP address '%s'" % ip,
2369 22b7f6f8 Thomas Thrainer
                                         errors.ECODE_INVAL)
2370 22b7f6f8 Thomas Thrainer
2371 22b7f6f8 Thomas Thrainer
      if constants.INIC_MAC in params:
2372 22b7f6f8 Thomas Thrainer
        macaddr = params[constants.INIC_MAC]
2373 22b7f6f8 Thomas Thrainer
        if macaddr not in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
2374 22b7f6f8 Thomas Thrainer
          macaddr = utils.NormalizeAndValidateMac(macaddr)
2375 22b7f6f8 Thomas Thrainer
2376 22b7f6f8 Thomas Thrainer
        if op == constants.DDM_MODIFY and macaddr == constants.VALUE_AUTO:
2377 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("'auto' is not a valid MAC address when"
2378 22b7f6f8 Thomas Thrainer
                                     " modifying an existing NIC",
2379 22b7f6f8 Thomas Thrainer
                                     errors.ECODE_INVAL)
2380 22b7f6f8 Thomas Thrainer
2381 22b7f6f8 Thomas Thrainer
  def CheckArguments(self):
2382 22b7f6f8 Thomas Thrainer
    if not (self.op.nics or self.op.disks or self.op.disk_template or
2383 22b7f6f8 Thomas Thrainer
            self.op.hvparams or self.op.beparams or self.op.os_name or
2384 125e1230 Thomas Thrainer
            self.op.osparams or self.op.offline is not None or
2385 125e1230 Thomas Thrainer
            self.op.runtime_mem or self.op.pnode):
2386 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("No changes submitted", errors.ECODE_INVAL)
2387 22b7f6f8 Thomas Thrainer
2388 22b7f6f8 Thomas Thrainer
    if self.op.hvparams:
2389 5eacbcae Thomas Thrainer
      CheckParamsNotGlobal(self.op.hvparams, constants.HVC_GLOBALS,
2390 5eacbcae Thomas Thrainer
                           "hypervisor", "instance", "cluster")
2391 22b7f6f8 Thomas Thrainer
2392 22b7f6f8 Thomas Thrainer
    self.op.disks = self._UpgradeDiskNicMods(
2393 22b7f6f8 Thomas Thrainer
      "disk", self.op.disks, opcodes.OpInstanceSetParams.TestDiskModifications)
2394 22b7f6f8 Thomas Thrainer
    self.op.nics = self._UpgradeDiskNicMods(
2395 22b7f6f8 Thomas Thrainer
      "NIC", self.op.nics, opcodes.OpInstanceSetParams.TestNicModifications)
2396 22b7f6f8 Thomas Thrainer
2397 22b7f6f8 Thomas Thrainer
    if self.op.disks and self.op.disk_template is not None:
2398 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Disk template conversion and other disk"
2399 22b7f6f8 Thomas Thrainer
                                 " changes not supported at the same time",
2400 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
2401 22b7f6f8 Thomas Thrainer
2402 22b7f6f8 Thomas Thrainer
    if (self.op.disk_template and
2403 22b7f6f8 Thomas Thrainer
        self.op.disk_template in constants.DTS_INT_MIRROR and
2404 22b7f6f8 Thomas Thrainer
        self.op.remote_node is None):
2405 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Changing the disk template to a mirrored"
2406 22b7f6f8 Thomas Thrainer
                                 " one requires specifying a secondary node",
2407 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
2408 22b7f6f8 Thomas Thrainer
2409 22b7f6f8 Thomas Thrainer
    # Check NIC modifications
2410 22b7f6f8 Thomas Thrainer
    self._CheckMods("NIC", self.op.nics, constants.INIC_PARAMS_TYPES,
2411 22b7f6f8 Thomas Thrainer
                    self._VerifyNicModification)
2412 22b7f6f8 Thomas Thrainer
2413 22b7f6f8 Thomas Thrainer
    if self.op.pnode:
2414 5eacbcae Thomas Thrainer
      self.op.pnode = ExpandNodeName(self.cfg, self.op.pnode)
2415 22b7f6f8 Thomas Thrainer
2416 22b7f6f8 Thomas Thrainer
  def ExpandNames(self):
2417 22b7f6f8 Thomas Thrainer
    self._ExpandAndLockInstance()
2418 22b7f6f8 Thomas Thrainer
    self.needed_locks[locking.LEVEL_NODEGROUP] = []
2419 22b7f6f8 Thomas Thrainer
    # Can't even acquire node locks in shared mode as upcoming changes in
2420 22b7f6f8 Thomas Thrainer
    # Ganeti 2.6 will start to modify the node object on disk conversion
2421 22b7f6f8 Thomas Thrainer
    self.needed_locks[locking.LEVEL_NODE] = []
2422 22b7f6f8 Thomas Thrainer
    self.needed_locks[locking.LEVEL_NODE_RES] = []
2423 22b7f6f8 Thomas Thrainer
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
2424 22b7f6f8 Thomas Thrainer
    # Look node group to look up the ipolicy
2425 22b7f6f8 Thomas Thrainer
    self.share_locks[locking.LEVEL_NODEGROUP] = 1
2426 22b7f6f8 Thomas Thrainer
2427 22b7f6f8 Thomas Thrainer
  def DeclareLocks(self, level):
2428 22b7f6f8 Thomas Thrainer
    if level == locking.LEVEL_NODEGROUP:
2429 22b7f6f8 Thomas Thrainer
      assert not self.needed_locks[locking.LEVEL_NODEGROUP]
2430 22b7f6f8 Thomas Thrainer
      # Acquire locks for the instance's nodegroups optimistically. Needs
2431 22b7f6f8 Thomas Thrainer
      # to be verified in CheckPrereq
2432 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODEGROUP] = \
2433 22b7f6f8 Thomas Thrainer
        self.cfg.GetInstanceNodeGroups(self.op.instance_name)
2434 22b7f6f8 Thomas Thrainer
    elif level == locking.LEVEL_NODE:
2435 22b7f6f8 Thomas Thrainer
      self._LockInstancesNodes()
2436 22b7f6f8 Thomas Thrainer
      if self.op.disk_template and self.op.remote_node:
2437 5eacbcae Thomas Thrainer
        self.op.remote_node = ExpandNodeName(self.cfg, self.op.remote_node)
2438 22b7f6f8 Thomas Thrainer
        self.needed_locks[locking.LEVEL_NODE].append(self.op.remote_node)
2439 22b7f6f8 Thomas Thrainer
    elif level == locking.LEVEL_NODE_RES and self.op.disk_template:
2440 22b7f6f8 Thomas Thrainer
      # Copy node locks
2441 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_RES] = \
2442 5eacbcae Thomas Thrainer
        CopyLockList(self.needed_locks[locking.LEVEL_NODE])
2443 22b7f6f8 Thomas Thrainer
2444 22b7f6f8 Thomas Thrainer
  def BuildHooksEnv(self):
2445 22b7f6f8 Thomas Thrainer
    """Build hooks env.
2446 22b7f6f8 Thomas Thrainer

2447 22b7f6f8 Thomas Thrainer
    This runs on the master, primary and secondaries.
2448 22b7f6f8 Thomas Thrainer

2449 22b7f6f8 Thomas Thrainer
    """
2450 22b7f6f8 Thomas Thrainer
    args = {}
2451 22b7f6f8 Thomas Thrainer
    if constants.BE_MINMEM in self.be_new:
2452 22b7f6f8 Thomas Thrainer
      args["minmem"] = self.be_new[constants.BE_MINMEM]
2453 22b7f6f8 Thomas Thrainer
    if constants.BE_MAXMEM in self.be_new:
2454 22b7f6f8 Thomas Thrainer
      args["maxmem"] = self.be_new[constants.BE_MAXMEM]
2455 22b7f6f8 Thomas Thrainer
    if constants.BE_VCPUS in self.be_new:
2456 22b7f6f8 Thomas Thrainer
      args["vcpus"] = self.be_new[constants.BE_VCPUS]
2457 22b7f6f8 Thomas Thrainer
    # TODO: export disk changes. Note: _BuildInstanceHookEnv* don't export disk
2458 22b7f6f8 Thomas Thrainer
    # information at all.
2459 22b7f6f8 Thomas Thrainer
2460 22b7f6f8 Thomas Thrainer
    if self._new_nics is not None:
2461 22b7f6f8 Thomas Thrainer
      nics = []
2462 22b7f6f8 Thomas Thrainer
2463 22b7f6f8 Thomas Thrainer
      for nic in self._new_nics:
2464 22b7f6f8 Thomas Thrainer
        n = copy.deepcopy(nic)
2465 22b7f6f8 Thomas Thrainer
        nicparams = self.cluster.SimpleFillNIC(n.nicparams)
2466 22b7f6f8 Thomas Thrainer
        n.nicparams = nicparams
2467 5eacbcae Thomas Thrainer
        nics.append(NICToTuple(self, n))
2468 22b7f6f8 Thomas Thrainer
2469 22b7f6f8 Thomas Thrainer
      args["nics"] = nics
2470 22b7f6f8 Thomas Thrainer
2471 5eacbcae Thomas Thrainer
    env = BuildInstanceHookEnvByObject(self, self.instance, override=args)
2472 22b7f6f8 Thomas Thrainer
    if self.op.disk_template:
2473 22b7f6f8 Thomas Thrainer
      env["NEW_DISK_TEMPLATE"] = self.op.disk_template
2474 22b7f6f8 Thomas Thrainer
    if self.op.runtime_mem:
2475 22b7f6f8 Thomas Thrainer
      env["RUNTIME_MEMORY"] = self.op.runtime_mem
2476 22b7f6f8 Thomas Thrainer
2477 22b7f6f8 Thomas Thrainer
    return env
2478 22b7f6f8 Thomas Thrainer
2479 22b7f6f8 Thomas Thrainer
  def BuildHooksNodes(self):
2480 22b7f6f8 Thomas Thrainer
    """Build hooks nodes.
2481 22b7f6f8 Thomas Thrainer

2482 22b7f6f8 Thomas Thrainer
    """
2483 22b7f6f8 Thomas Thrainer
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
2484 22b7f6f8 Thomas Thrainer
    return (nl, nl)
2485 22b7f6f8 Thomas Thrainer
2486 22b7f6f8 Thomas Thrainer
  def _PrepareNicModification(self, params, private, old_ip, old_net_uuid,
2487 22b7f6f8 Thomas Thrainer
                              old_params, cluster, pnode):
2488 22b7f6f8 Thomas Thrainer
2489 22b7f6f8 Thomas Thrainer
    update_params_dict = dict([(key, params[key])
2490 22b7f6f8 Thomas Thrainer
                               for key in constants.NICS_PARAMETERS
2491 22b7f6f8 Thomas Thrainer
                               if key in params])
2492 22b7f6f8 Thomas Thrainer
2493 22b7f6f8 Thomas Thrainer
    req_link = update_params_dict.get(constants.NIC_LINK, None)
2494 22b7f6f8 Thomas Thrainer
    req_mode = update_params_dict.get(constants.NIC_MODE, None)
2495 22b7f6f8 Thomas Thrainer
2496 22b7f6f8 Thomas Thrainer
    new_net_uuid = None
2497 22b7f6f8 Thomas Thrainer
    new_net_uuid_or_name = params.get(constants.INIC_NETWORK, old_net_uuid)
2498 22b7f6f8 Thomas Thrainer
    if new_net_uuid_or_name:
2499 22b7f6f8 Thomas Thrainer
      new_net_uuid = self.cfg.LookupNetwork(new_net_uuid_or_name)
2500 22b7f6f8 Thomas Thrainer
      new_net_obj = self.cfg.GetNetwork(new_net_uuid)
2501 22b7f6f8 Thomas Thrainer
2502 22b7f6f8 Thomas Thrainer
    if old_net_uuid:
2503 22b7f6f8 Thomas Thrainer
      old_net_obj = self.cfg.GetNetwork(old_net_uuid)
2504 22b7f6f8 Thomas Thrainer
2505 22b7f6f8 Thomas Thrainer
    if new_net_uuid:
2506 22b7f6f8 Thomas Thrainer
      netparams = self.cfg.GetGroupNetParams(new_net_uuid, pnode)
2507 22b7f6f8 Thomas Thrainer
      if not netparams:
2508 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("No netparams found for the network"
2509 22b7f6f8 Thomas Thrainer
                                   " %s, probably not connected" %
2510 22b7f6f8 Thomas Thrainer
                                   new_net_obj.name, errors.ECODE_INVAL)
2511 22b7f6f8 Thomas Thrainer
      new_params = dict(netparams)
2512 22b7f6f8 Thomas Thrainer
    else:
2513 5eacbcae Thomas Thrainer
      new_params = GetUpdatedParams(old_params, update_params_dict)
2514 22b7f6f8 Thomas Thrainer
2515 22b7f6f8 Thomas Thrainer
    utils.ForceDictType(new_params, constants.NICS_PARAMETER_TYPES)
2516 22b7f6f8 Thomas Thrainer
2517 22b7f6f8 Thomas Thrainer
    new_filled_params = cluster.SimpleFillNIC(new_params)
2518 22b7f6f8 Thomas Thrainer
    objects.NIC.CheckParameterSyntax(new_filled_params)
2519 22b7f6f8 Thomas Thrainer
2520 22b7f6f8 Thomas Thrainer
    new_mode = new_filled_params[constants.NIC_MODE]
2521 22b7f6f8 Thomas Thrainer
    if new_mode == constants.NIC_MODE_BRIDGED:
2522 22b7f6f8 Thomas Thrainer
      bridge = new_filled_params[constants.NIC_LINK]
2523 22b7f6f8 Thomas Thrainer
      msg = self.rpc.call_bridges_exist(pnode, [bridge]).fail_msg
2524 22b7f6f8 Thomas Thrainer
      if msg:
2525 22b7f6f8 Thomas Thrainer
        msg = "Error checking bridges on node '%s': %s" % (pnode, msg)
2526 22b7f6f8 Thomas Thrainer
        if self.op.force:
2527 22b7f6f8 Thomas Thrainer
          self.warn.append(msg)
2528 22b7f6f8 Thomas Thrainer
        else:
2529 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError(msg, errors.ECODE_ENVIRON)
2530 22b7f6f8 Thomas Thrainer
2531 22b7f6f8 Thomas Thrainer
    elif new_mode == constants.NIC_MODE_ROUTED:
2532 22b7f6f8 Thomas Thrainer
      ip = params.get(constants.INIC_IP, old_ip)
2533 22b7f6f8 Thomas Thrainer
      if ip is None:
2534 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Cannot set the NIC IP address to None"
2535 22b7f6f8 Thomas Thrainer
                                   " on a routed NIC", errors.ECODE_INVAL)
2536 22b7f6f8 Thomas Thrainer
2537 22b7f6f8 Thomas Thrainer
    elif new_mode == constants.NIC_MODE_OVS:
2538 22b7f6f8 Thomas Thrainer
      # TODO: check OVS link
2539 22b7f6f8 Thomas Thrainer
      self.LogInfo("OVS links are currently not checked for correctness")
2540 22b7f6f8 Thomas Thrainer
2541 22b7f6f8 Thomas Thrainer
    if constants.INIC_MAC in params:
2542 22b7f6f8 Thomas Thrainer
      mac = params[constants.INIC_MAC]
2543 22b7f6f8 Thomas Thrainer
      if mac is None:
2544 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Cannot unset the NIC MAC address",
2545 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
2546 22b7f6f8 Thomas Thrainer
      elif mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
2547 22b7f6f8 Thomas Thrainer
        # otherwise generate the MAC address
2548 22b7f6f8 Thomas Thrainer
        params[constants.INIC_MAC] = \
2549 22b7f6f8 Thomas Thrainer
          self.cfg.GenerateMAC(new_net_uuid, self.proc.GetECId())
2550 22b7f6f8 Thomas Thrainer
      else:
2551 22b7f6f8 Thomas Thrainer
        # or validate/reserve the current one
2552 22b7f6f8 Thomas Thrainer
        try:
2553 22b7f6f8 Thomas Thrainer
          self.cfg.ReserveMAC(mac, self.proc.GetECId())
2554 22b7f6f8 Thomas Thrainer
        except errors.ReservationError:
2555 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("MAC address '%s' already in use"
2556 22b7f6f8 Thomas Thrainer
                                     " in cluster" % mac,
2557 22b7f6f8 Thomas Thrainer
                                     errors.ECODE_NOTUNIQUE)
2558 22b7f6f8 Thomas Thrainer
    elif new_net_uuid != old_net_uuid:
2559 22b7f6f8 Thomas Thrainer
2560 22b7f6f8 Thomas Thrainer
      def get_net_prefix(net_uuid):
2561 22b7f6f8 Thomas Thrainer
        mac_prefix = None
2562 22b7f6f8 Thomas Thrainer
        if net_uuid:
2563 22b7f6f8 Thomas Thrainer
          nobj = self.cfg.GetNetwork(net_uuid)
2564 22b7f6f8 Thomas Thrainer
          mac_prefix = nobj.mac_prefix
2565 22b7f6f8 Thomas Thrainer
2566 22b7f6f8 Thomas Thrainer
        return mac_prefix
2567 22b7f6f8 Thomas Thrainer
2568 22b7f6f8 Thomas Thrainer
      new_prefix = get_net_prefix(new_net_uuid)
2569 22b7f6f8 Thomas Thrainer
      old_prefix = get_net_prefix(old_net_uuid)
2570 22b7f6f8 Thomas Thrainer
      if old_prefix != new_prefix:
2571 22b7f6f8 Thomas Thrainer
        params[constants.INIC_MAC] = \
2572 22b7f6f8 Thomas Thrainer
          self.cfg.GenerateMAC(new_net_uuid, self.proc.GetECId())
2573 22b7f6f8 Thomas Thrainer
2574 22b7f6f8 Thomas Thrainer
    # if there is a change in (ip, network) tuple
2575 22b7f6f8 Thomas Thrainer
    new_ip = params.get(constants.INIC_IP, old_ip)
2576 22b7f6f8 Thomas Thrainer
    if (new_ip, new_net_uuid) != (old_ip, old_net_uuid):
2577 22b7f6f8 Thomas Thrainer
      if new_ip:
2578 22b7f6f8 Thomas Thrainer
        # if IP is pool then require a network and generate one IP
2579 22b7f6f8 Thomas Thrainer
        if new_ip.lower() == constants.NIC_IP_POOL:
2580 22b7f6f8 Thomas Thrainer
          if new_net_uuid:
2581 22b7f6f8 Thomas Thrainer
            try:
2582 22b7f6f8 Thomas Thrainer
              new_ip = self.cfg.GenerateIp(new_net_uuid, self.proc.GetECId())
2583 22b7f6f8 Thomas Thrainer
            except errors.ReservationError:
2584 22b7f6f8 Thomas Thrainer
              raise errors.OpPrereqError("Unable to get a free IP"
2585 22b7f6f8 Thomas Thrainer
                                         " from the address pool",
2586 22b7f6f8 Thomas Thrainer
                                         errors.ECODE_STATE)
2587 22b7f6f8 Thomas Thrainer
            self.LogInfo("Chose IP %s from network %s",
2588 22b7f6f8 Thomas Thrainer
                         new_ip,
2589 22b7f6f8 Thomas Thrainer
                         new_net_obj.name)
2590 22b7f6f8 Thomas Thrainer
            params[constants.INIC_IP] = new_ip
2591 22b7f6f8 Thomas Thrainer
          else:
2592 22b7f6f8 Thomas Thrainer
            raise errors.OpPrereqError("ip=pool, but no network found",
2593 22b7f6f8 Thomas Thrainer
                                       errors.ECODE_INVAL)
2594 22b7f6f8 Thomas Thrainer
        # Reserve new IP if in the new network if any
2595 22b7f6f8 Thomas Thrainer
        elif new_net_uuid:
2596 22b7f6f8 Thomas Thrainer
          try:
2597 22b7f6f8 Thomas Thrainer
            self.cfg.ReserveIp(new_net_uuid, new_ip, self.proc.GetECId())
2598 22b7f6f8 Thomas Thrainer
            self.LogInfo("Reserving IP %s in network %s",
2599 22b7f6f8 Thomas Thrainer
                         new_ip, new_net_obj.name)
2600 22b7f6f8 Thomas Thrainer
          except errors.ReservationError:
2601 22b7f6f8 Thomas Thrainer
            raise errors.OpPrereqError("IP %s not available in network %s" %
2602 22b7f6f8 Thomas Thrainer
                                       (new_ip, new_net_obj.name),
2603 22b7f6f8 Thomas Thrainer
                                       errors.ECODE_NOTUNIQUE)
2604 22b7f6f8 Thomas Thrainer
        # new network is None so check if new IP is a conflicting IP
2605 22b7f6f8 Thomas Thrainer
        elif self.op.conflicts_check:
2606 22b7f6f8 Thomas Thrainer
          _CheckForConflictingIp(self, new_ip, pnode)
2607 22b7f6f8 Thomas Thrainer
2608 22b7f6f8 Thomas Thrainer
      # release old IP if old network is not None
2609 22b7f6f8 Thomas Thrainer
      if old_ip and old_net_uuid:
2610 22b7f6f8 Thomas Thrainer
        try:
2611 22b7f6f8 Thomas Thrainer
          self.cfg.ReleaseIp(old_net_uuid, old_ip, self.proc.GetECId())
2612 22b7f6f8 Thomas Thrainer
        except errors.AddressPoolError:
2613 22b7f6f8 Thomas Thrainer
          logging.warning("Release IP %s not contained in network %s",
2614 22b7f6f8 Thomas Thrainer
                          old_ip, old_net_obj.name)
2615 22b7f6f8 Thomas Thrainer
2616 22b7f6f8 Thomas Thrainer
    # there are no changes in (ip, network) tuple and old network is not None
2617 22b7f6f8 Thomas Thrainer
    elif (old_net_uuid is not None and
2618 22b7f6f8 Thomas Thrainer
          (req_link is not None or req_mode is not None)):
2619 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Not allowed to change link or mode of"
2620 22b7f6f8 Thomas Thrainer
                                 " a NIC that is connected to a network",
2621 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
2622 22b7f6f8 Thomas Thrainer
2623 22b7f6f8 Thomas Thrainer
    private.params = new_params
2624 22b7f6f8 Thomas Thrainer
    private.filled = new_filled_params
2625 22b7f6f8 Thomas Thrainer
2626 22b7f6f8 Thomas Thrainer
  def _PreCheckDiskTemplate(self, pnode_info):
2627 22b7f6f8 Thomas Thrainer
    """CheckPrereq checks related to a new disk template."""
2628 22b7f6f8 Thomas Thrainer
    # Arguments are passed to avoid configuration lookups
2629 22b7f6f8 Thomas Thrainer
    instance = self.instance
2630 22b7f6f8 Thomas Thrainer
    pnode = instance.primary_node
2631 22b7f6f8 Thomas Thrainer
    cluster = self.cluster
2632 22b7f6f8 Thomas Thrainer
    if instance.disk_template == self.op.disk_template:
2633 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Instance already has disk template %s" %
2634 22b7f6f8 Thomas Thrainer
                                 instance.disk_template, errors.ECODE_INVAL)
2635 22b7f6f8 Thomas Thrainer
2636 22b7f6f8 Thomas Thrainer
    if (instance.disk_template,
2637 22b7f6f8 Thomas Thrainer
        self.op.disk_template) not in self._DISK_CONVERSIONS:
2638 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Unsupported disk template conversion from"
2639 22b7f6f8 Thomas Thrainer
                                 " %s to %s" % (instance.disk_template,
2640 22b7f6f8 Thomas Thrainer
                                                self.op.disk_template),
2641 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
2642 5eacbcae Thomas Thrainer
    CheckInstanceState(self, instance, INSTANCE_DOWN,
2643 5eacbcae Thomas Thrainer
                       msg="cannot change disk template")
2644 22b7f6f8 Thomas Thrainer
    if self.op.disk_template in constants.DTS_INT_MIRROR:
2645 22b7f6f8 Thomas Thrainer
      if self.op.remote_node == pnode:
2646 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Given new secondary node %s is the same"
2647 22b7f6f8 Thomas Thrainer
                                   " as the primary node of the instance" %
2648 22b7f6f8 Thomas Thrainer
                                   self.op.remote_node, errors.ECODE_STATE)
2649 5eacbcae Thomas Thrainer
      CheckNodeOnline(self, self.op.remote_node)
2650 5eacbcae Thomas Thrainer
      CheckNodeNotDrained(self, self.op.remote_node)
2651 22b7f6f8 Thomas Thrainer
      # FIXME: here we assume that the old instance type is DT_PLAIN
2652 22b7f6f8 Thomas Thrainer
      assert instance.disk_template == constants.DT_PLAIN
2653 22b7f6f8 Thomas Thrainer
      disks = [{constants.IDISK_SIZE: d.size,
2654 22b7f6f8 Thomas Thrainer
                constants.IDISK_VG: d.logical_id[0]}
2655 22b7f6f8 Thomas Thrainer
               for d in instance.disks]
2656 5eacbcae Thomas Thrainer
      required = ComputeDiskSizePerVG(self.op.disk_template, disks)
2657 5eacbcae Thomas Thrainer
      CheckNodesFreeDiskPerVG(self, [self.op.remote_node], required)
2658 22b7f6f8 Thomas Thrainer
2659 22b7f6f8 Thomas Thrainer
      snode_info = self.cfg.GetNodeInfo(self.op.remote_node)
2660 22b7f6f8 Thomas Thrainer
      snode_group = self.cfg.GetNodeGroup(snode_info.group)
2661 22b7f6f8 Thomas Thrainer
      ipolicy = ganeti.masterd.instance.CalculateGroupIPolicy(cluster,
2662 22b7f6f8 Thomas Thrainer
                                                              snode_group)
2663 5eacbcae Thomas Thrainer
      CheckTargetNodeIPolicy(self, ipolicy, instance, snode_info, self.cfg,
2664 5eacbcae Thomas Thrainer
                             ignore=self.op.ignore_ipolicy)
2665 22b7f6f8 Thomas Thrainer
      if pnode_info.group != snode_info.group:
2666 22b7f6f8 Thomas Thrainer
        self.LogWarning("The primary and secondary nodes are in two"
2667 22b7f6f8 Thomas Thrainer
                        " different node groups; the disk parameters"
2668 22b7f6f8 Thomas Thrainer
                        " from the first disk's node group will be"
2669 22b7f6f8 Thomas Thrainer
                        " used")
2670 22b7f6f8 Thomas Thrainer
2671 22b7f6f8 Thomas Thrainer
    if not self.op.disk_template in constants.DTS_EXCL_STORAGE:
2672 22b7f6f8 Thomas Thrainer
      # Make sure none of the nodes require exclusive storage
2673 22b7f6f8 Thomas Thrainer
      nodes = [pnode_info]
2674 22b7f6f8 Thomas Thrainer
      if self.op.disk_template in constants.DTS_INT_MIRROR:
2675 22b7f6f8 Thomas Thrainer
        assert snode_info
2676 22b7f6f8 Thomas Thrainer
        nodes.append(snode_info)
2677 5eacbcae Thomas Thrainer
      has_es = lambda n: IsExclusiveStorageEnabledNode(self.cfg, n)
2678 22b7f6f8 Thomas Thrainer
      if compat.any(map(has_es, nodes)):
2679 22b7f6f8 Thomas Thrainer
        errmsg = ("Cannot convert disk template from %s to %s when exclusive"
2680 22b7f6f8 Thomas Thrainer
                  " storage is enabled" % (instance.disk_template,
2681 22b7f6f8 Thomas Thrainer
                                           self.op.disk_template))
2682 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError(errmsg, errors.ECODE_STATE)
2683 22b7f6f8 Thomas Thrainer
2684 22b7f6f8 Thomas Thrainer
  def CheckPrereq(self):
2685 22b7f6f8 Thomas Thrainer
    """Check prerequisites.
2686 22b7f6f8 Thomas Thrainer

2687 22b7f6f8 Thomas Thrainer
    This only checks the instance list against the existing names.
2688 22b7f6f8 Thomas Thrainer

2689 22b7f6f8 Thomas Thrainer
    """
2690 22b7f6f8 Thomas Thrainer
    assert self.op.instance_name in self.owned_locks(locking.LEVEL_INSTANCE)
2691 22b7f6f8 Thomas Thrainer
    instance = self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
2692 22b7f6f8 Thomas Thrainer
2693 22b7f6f8 Thomas Thrainer
    cluster = self.cluster = self.cfg.GetClusterInfo()
2694 22b7f6f8 Thomas Thrainer
    assert self.instance is not None, \
2695 22b7f6f8 Thomas Thrainer
      "Cannot retrieve locked instance %s" % self.op.instance_name
2696 22b7f6f8 Thomas Thrainer
2697 22b7f6f8 Thomas Thrainer
    pnode = instance.primary_node
2698 22b7f6f8 Thomas Thrainer
2699 22b7f6f8 Thomas Thrainer
    self.warn = []
2700 22b7f6f8 Thomas Thrainer
2701 22b7f6f8 Thomas Thrainer
    if (self.op.pnode is not None and self.op.pnode != pnode and
2702 22b7f6f8 Thomas Thrainer
        not self.op.force):
2703 22b7f6f8 Thomas Thrainer
      # verify that the instance is not up
2704 22b7f6f8 Thomas Thrainer
      instance_info = self.rpc.call_instance_info(pnode, instance.name,
2705 22b7f6f8 Thomas Thrainer
                                                  instance.hypervisor)
2706 22b7f6f8 Thomas Thrainer
      if instance_info.fail_msg:
2707 22b7f6f8 Thomas Thrainer
        self.warn.append("Can't get instance runtime information: %s" %
2708 22b7f6f8 Thomas Thrainer
                         instance_info.fail_msg)
2709 22b7f6f8 Thomas Thrainer
      elif instance_info.payload:
2710 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Instance is still running on %s" % pnode,
2711 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_STATE)
2712 22b7f6f8 Thomas Thrainer
2713 22b7f6f8 Thomas Thrainer
    assert pnode in self.owned_locks(locking.LEVEL_NODE)
2714 22b7f6f8 Thomas Thrainer
    nodelist = list(instance.all_nodes)
2715 22b7f6f8 Thomas Thrainer
    pnode_info = self.cfg.GetNodeInfo(pnode)
2716 22b7f6f8 Thomas Thrainer
    self.diskparams = self.cfg.GetInstanceDiskParams(instance)
2717 22b7f6f8 Thomas Thrainer
2718 22b7f6f8 Thomas Thrainer
    #_CheckInstanceNodeGroups(self.cfg, self.op.instance_name, owned_groups)
2719 22b7f6f8 Thomas Thrainer
    assert pnode_info.group in self.owned_locks(locking.LEVEL_NODEGROUP)
2720 22b7f6f8 Thomas Thrainer
    group_info = self.cfg.GetNodeGroup(pnode_info.group)
2721 22b7f6f8 Thomas Thrainer
2722 22b7f6f8 Thomas Thrainer
    # dictionary with instance information after the modification
2723 22b7f6f8 Thomas Thrainer
    ispec = {}
2724 22b7f6f8 Thomas Thrainer
2725 22b7f6f8 Thomas Thrainer
    # Check disk modifications. This is done here and not in CheckArguments
2726 22b7f6f8 Thomas Thrainer
    # (as with NICs), because we need to know the instance's disk template
2727 22b7f6f8 Thomas Thrainer
    if instance.disk_template == constants.DT_EXT:
2728 22b7f6f8 Thomas Thrainer
      self._CheckMods("disk", self.op.disks, {},
2729 22b7f6f8 Thomas Thrainer
                      self._VerifyDiskModification)
2730 22b7f6f8 Thomas Thrainer
    else:
2731 22b7f6f8 Thomas Thrainer
      self._CheckMods("disk", self.op.disks, constants.IDISK_PARAMS_TYPES,
2732 22b7f6f8 Thomas Thrainer
                      self._VerifyDiskModification)
2733 22b7f6f8 Thomas Thrainer
2734 22b7f6f8 Thomas Thrainer
    # Prepare disk/NIC modifications
2735 5eacbcae Thomas Thrainer
    self.diskmod = _PrepareContainerMods(self.op.disks, None)
2736 5eacbcae Thomas Thrainer
    self.nicmod = _PrepareContainerMods(self.op.nics, _InstNicModPrivate)
2737 22b7f6f8 Thomas Thrainer
2738 22b7f6f8 Thomas Thrainer
    # Check the validity of the `provider' parameter
2739 22b7f6f8 Thomas Thrainer
    if instance.disk_template in constants.DT_EXT:
2740 22b7f6f8 Thomas Thrainer
      for mod in self.diskmod:
2741 22b7f6f8 Thomas Thrainer
        ext_provider = mod[2].get(constants.IDISK_PROVIDER, None)
2742 22b7f6f8 Thomas Thrainer
        if mod[0] == constants.DDM_ADD:
2743 22b7f6f8 Thomas Thrainer
          if ext_provider is None:
2744 22b7f6f8 Thomas Thrainer
            raise errors.OpPrereqError("Instance template is '%s' and parameter"
2745 22b7f6f8 Thomas Thrainer
                                       " '%s' missing, during disk add" %
2746 22b7f6f8 Thomas Thrainer
                                       (constants.DT_EXT,
2747 22b7f6f8 Thomas Thrainer
                                        constants.IDISK_PROVIDER),
2748 22b7f6f8 Thomas Thrainer
                                       errors.ECODE_NOENT)
2749 22b7f6f8 Thomas Thrainer
        elif mod[0] == constants.DDM_MODIFY:
2750 22b7f6f8 Thomas Thrainer
          if ext_provider:
2751 22b7f6f8 Thomas Thrainer
            raise errors.OpPrereqError("Parameter '%s' is invalid during disk"
2752 22b7f6f8 Thomas Thrainer
                                       " modification" %
2753 22b7f6f8 Thomas Thrainer
                                       constants.IDISK_PROVIDER,
2754 22b7f6f8 Thomas Thrainer
                                       errors.ECODE_INVAL)
2755 22b7f6f8 Thomas Thrainer
    else:
2756 22b7f6f8 Thomas Thrainer
      for mod in self.diskmod:
2757 22b7f6f8 Thomas Thrainer
        ext_provider = mod[2].get(constants.IDISK_PROVIDER, None)
2758 22b7f6f8 Thomas Thrainer
        if ext_provider is not None:
2759 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("Parameter '%s' is only valid for"
2760 22b7f6f8 Thomas Thrainer
                                     " instances of type '%s'" %
2761 22b7f6f8 Thomas Thrainer
                                     (constants.IDISK_PROVIDER,
2762 22b7f6f8 Thomas Thrainer
                                      constants.DT_EXT),
2763 22b7f6f8 Thomas Thrainer
                                     errors.ECODE_INVAL)
2764 22b7f6f8 Thomas Thrainer
2765 22b7f6f8 Thomas Thrainer
    # OS change
2766 22b7f6f8 Thomas Thrainer
    if self.op.os_name and not self.op.force:
2767 5eacbcae Thomas Thrainer
      CheckNodeHasOS(self, instance.primary_node, self.op.os_name,
2768 5eacbcae Thomas Thrainer
                     self.op.force_variant)
2769 22b7f6f8 Thomas Thrainer
      instance_os = self.op.os_name
2770 22b7f6f8 Thomas Thrainer
    else:
2771 22b7f6f8 Thomas Thrainer
      instance_os = instance.os
2772 22b7f6f8 Thomas Thrainer
2773 22b7f6f8 Thomas Thrainer
    assert not (self.op.disk_template and self.op.disks), \
2774 22b7f6f8 Thomas Thrainer
      "Can't modify disk template and apply disk changes at the same time"
2775 22b7f6f8 Thomas Thrainer
2776 22b7f6f8 Thomas Thrainer
    if self.op.disk_template:
2777 22b7f6f8 Thomas Thrainer
      self._PreCheckDiskTemplate(pnode_info)
2778 22b7f6f8 Thomas Thrainer
2779 22b7f6f8 Thomas Thrainer
    # hvparams processing
2780 22b7f6f8 Thomas Thrainer
    if self.op.hvparams:
2781 22b7f6f8 Thomas Thrainer
      hv_type = instance.hypervisor
2782 5eacbcae Thomas Thrainer
      i_hvdict = GetUpdatedParams(instance.hvparams, self.op.hvparams)
2783 22b7f6f8 Thomas Thrainer
      utils.ForceDictType(i_hvdict, constants.HVS_PARAMETER_TYPES)
2784 22b7f6f8 Thomas Thrainer
      hv_new = cluster.SimpleFillHV(hv_type, instance.os, i_hvdict)
2785 22b7f6f8 Thomas Thrainer
2786 22b7f6f8 Thomas Thrainer
      # local check
2787 22b7f6f8 Thomas Thrainer
      hypervisor.GetHypervisorClass(hv_type).CheckParameterSyntax(hv_new)
2788 5eacbcae Thomas Thrainer
      CheckHVParams(self, nodelist, instance.hypervisor, hv_new)
2789 22b7f6f8 Thomas Thrainer
      self.hv_proposed = self.hv_new = hv_new # the new actual values
2790 22b7f6f8 Thomas Thrainer
      self.hv_inst = i_hvdict # the new dict (without defaults)
2791 22b7f6f8 Thomas Thrainer
    else:
2792 22b7f6f8 Thomas Thrainer
      self.hv_proposed = cluster.SimpleFillHV(instance.hypervisor, instance.os,
2793 22b7f6f8 Thomas Thrainer
                                              instance.hvparams)
2794 22b7f6f8 Thomas Thrainer
      self.hv_new = self.hv_inst = {}
2795 22b7f6f8 Thomas Thrainer
2796 22b7f6f8 Thomas Thrainer
    # beparams processing
2797 22b7f6f8 Thomas Thrainer
    if self.op.beparams:
2798 5eacbcae Thomas Thrainer
      i_bedict = GetUpdatedParams(instance.beparams, self.op.beparams,
2799 5eacbcae Thomas Thrainer
                                  use_none=True)
2800 22b7f6f8 Thomas Thrainer
      objects.UpgradeBeParams(i_bedict)
2801 22b7f6f8 Thomas Thrainer
      utils.ForceDictType(i_bedict, constants.BES_PARAMETER_TYPES)
2802 22b7f6f8 Thomas Thrainer
      be_new = cluster.SimpleFillBE(i_bedict)
2803 22b7f6f8 Thomas Thrainer
      self.be_proposed = self.be_new = be_new # the new actual values
2804 22b7f6f8 Thomas Thrainer
      self.be_inst = i_bedict # the new dict (without defaults)
2805 22b7f6f8 Thomas Thrainer
    else:
2806 22b7f6f8 Thomas Thrainer
      self.be_new = self.be_inst = {}
2807 22b7f6f8 Thomas Thrainer
      self.be_proposed = cluster.SimpleFillBE(instance.beparams)
2808 22b7f6f8 Thomas Thrainer
    be_old = cluster.FillBE(instance)
2809 22b7f6f8 Thomas Thrainer
2810 22b7f6f8 Thomas Thrainer
    # CPU param validation -- checking every time a parameter is
2811 22b7f6f8 Thomas Thrainer
    # changed to cover all cases where either CPU mask or vcpus have
2812 22b7f6f8 Thomas Thrainer
    # changed
2813 22b7f6f8 Thomas Thrainer
    if (constants.BE_VCPUS in self.be_proposed and
2814 22b7f6f8 Thomas Thrainer
        constants.HV_CPU_MASK in self.hv_proposed):
2815 22b7f6f8 Thomas Thrainer
      cpu_list = \
2816 22b7f6f8 Thomas Thrainer
        utils.ParseMultiCpuMask(self.hv_proposed[constants.HV_CPU_MASK])
2817 22b7f6f8 Thomas Thrainer
      # Verify mask is consistent with number of vCPUs. Can skip this
2818 22b7f6f8 Thomas Thrainer
      # test if only 1 entry in the CPU mask, which means same mask
2819 22b7f6f8 Thomas Thrainer
      # is applied to all vCPUs.
2820 22b7f6f8 Thomas Thrainer
      if (len(cpu_list) > 1 and
2821 22b7f6f8 Thomas Thrainer
          len(cpu_list) != self.be_proposed[constants.BE_VCPUS]):
2822 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Number of vCPUs [%d] does not match the"
2823 22b7f6f8 Thomas Thrainer
                                   " CPU mask [%s]" %
2824 22b7f6f8 Thomas Thrainer
                                   (self.be_proposed[constants.BE_VCPUS],
2825 22b7f6f8 Thomas Thrainer
                                    self.hv_proposed[constants.HV_CPU_MASK]),
2826 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
2827 22b7f6f8 Thomas Thrainer
2828 22b7f6f8 Thomas Thrainer
      # Only perform this test if a new CPU mask is given
2829 22b7f6f8 Thomas Thrainer
      if constants.HV_CPU_MASK in self.hv_new:
2830 22b7f6f8 Thomas Thrainer
        # Calculate the largest CPU number requested
2831 22b7f6f8 Thomas Thrainer
        max_requested_cpu = max(map(max, cpu_list))
2832 22b7f6f8 Thomas Thrainer
        # Check that all of the instance's nodes have enough physical CPUs to
2833 22b7f6f8 Thomas Thrainer
        # satisfy the requested CPU mask
2834 22b7f6f8 Thomas Thrainer
        _CheckNodesPhysicalCPUs(self, instance.all_nodes,
2835 22b7f6f8 Thomas Thrainer
                                max_requested_cpu + 1, instance.hypervisor)
2836 22b7f6f8 Thomas Thrainer
2837 22b7f6f8 Thomas Thrainer
    # osparams processing
2838 22b7f6f8 Thomas Thrainer
    if self.op.osparams:
2839 5eacbcae Thomas Thrainer
      i_osdict = GetUpdatedParams(instance.osparams, self.op.osparams)
2840 5eacbcae Thomas Thrainer
      CheckOSParams(self, True, nodelist, instance_os, i_osdict)
2841 22b7f6f8 Thomas Thrainer
      self.os_inst = i_osdict # the new dict (without defaults)
2842 22b7f6f8 Thomas Thrainer
    else:
2843 22b7f6f8 Thomas Thrainer
      self.os_inst = {}
2844 22b7f6f8 Thomas Thrainer
2845 22b7f6f8 Thomas Thrainer
    #TODO(dynmem): do the appropriate check involving MINMEM
2846 22b7f6f8 Thomas Thrainer
    if (constants.BE_MAXMEM in self.op.beparams and not self.op.force and
2847 22b7f6f8 Thomas Thrainer
        be_new[constants.BE_MAXMEM] > be_old[constants.BE_MAXMEM]):
2848 22b7f6f8 Thomas Thrainer
      mem_check_list = [pnode]
2849 22b7f6f8 Thomas Thrainer
      if be_new[constants.BE_AUTO_BALANCE]:
2850 22b7f6f8 Thomas Thrainer
        # either we changed auto_balance to yes or it was from before
2851 22b7f6f8 Thomas Thrainer
        mem_check_list.extend(instance.secondary_nodes)
2852 22b7f6f8 Thomas Thrainer
      instance_info = self.rpc.call_instance_info(pnode, instance.name,
2853 22b7f6f8 Thomas Thrainer
                                                  instance.hypervisor)
2854 22b7f6f8 Thomas Thrainer
      nodeinfo = self.rpc.call_node_info(mem_check_list, None,
2855 22b7f6f8 Thomas Thrainer
                                         [instance.hypervisor], False)
2856 22b7f6f8 Thomas Thrainer
      pninfo = nodeinfo[pnode]
2857 22b7f6f8 Thomas Thrainer
      msg = pninfo.fail_msg
2858 22b7f6f8 Thomas Thrainer
      if msg:
2859 22b7f6f8 Thomas Thrainer
        # Assume the primary node is unreachable and go ahead
2860 22b7f6f8 Thomas Thrainer
        self.warn.append("Can't get info from primary node %s: %s" %
2861 22b7f6f8 Thomas Thrainer
                         (pnode, msg))
2862 22b7f6f8 Thomas Thrainer
      else:
2863 22b7f6f8 Thomas Thrainer
        (_, _, (pnhvinfo, )) = pninfo.payload
2864 22b7f6f8 Thomas Thrainer
        if not isinstance(pnhvinfo.get("memory_free", None), int):
2865 22b7f6f8 Thomas Thrainer
          self.warn.append("Node data from primary node %s doesn't contain"
2866 22b7f6f8 Thomas Thrainer
                           " free memory information" % pnode)
2867 22b7f6f8 Thomas Thrainer
        elif instance_info.fail_msg:
2868 22b7f6f8 Thomas Thrainer
          self.warn.append("Can't get instance runtime information: %s" %
2869 22b7f6f8 Thomas Thrainer
                           instance_info.fail_msg)
2870 22b7f6f8 Thomas Thrainer
        else:
2871 22b7f6f8 Thomas Thrainer
          if instance_info.payload:
2872 22b7f6f8 Thomas Thrainer
            current_mem = int(instance_info.payload["memory"])
2873 22b7f6f8 Thomas Thrainer
          else:
2874 22b7f6f8 Thomas Thrainer
            # Assume instance not running
2875 22b7f6f8 Thomas Thrainer
            # (there is a slight race condition here, but it's not very
2876 22b7f6f8 Thomas Thrainer
            # probable, and we have no other way to check)
2877 22b7f6f8 Thomas Thrainer
            # TODO: Describe race condition
2878 22b7f6f8 Thomas Thrainer
            current_mem = 0
2879 22b7f6f8 Thomas Thrainer
          #TODO(dynmem): do the appropriate check involving MINMEM
2880 22b7f6f8 Thomas Thrainer
          miss_mem = (be_new[constants.BE_MAXMEM] - current_mem -
2881 22b7f6f8 Thomas Thrainer
                      pnhvinfo["memory_free"])
2882 22b7f6f8 Thomas Thrainer
          if miss_mem > 0:
2883 22b7f6f8 Thomas Thrainer
            raise errors.OpPrereqError("This change will prevent the instance"
2884 22b7f6f8 Thomas Thrainer
                                       " from starting, due to %d MB of memory"
2885 22b7f6f8 Thomas Thrainer
                                       " missing on its primary node" %
2886 22b7f6f8 Thomas Thrainer
                                       miss_mem, errors.ECODE_NORES)
2887 22b7f6f8 Thomas Thrainer
2888 22b7f6f8 Thomas Thrainer
      if be_new[constants.BE_AUTO_BALANCE]:
2889 22b7f6f8 Thomas Thrainer
        for node, nres in nodeinfo.items():
2890 22b7f6f8 Thomas Thrainer
          if node not in instance.secondary_nodes:
2891 22b7f6f8 Thomas Thrainer
            continue
2892 22b7f6f8 Thomas Thrainer
          nres.Raise("Can't get info from secondary node %s" % node,
2893 22b7f6f8 Thomas Thrainer
                     prereq=True, ecode=errors.ECODE_STATE)
2894 22b7f6f8 Thomas Thrainer
          (_, _, (nhvinfo, )) = nres.payload
2895 22b7f6f8 Thomas Thrainer
          if not isinstance(nhvinfo.get("memory_free", None), int):
2896 22b7f6f8 Thomas Thrainer
            raise errors.OpPrereqError("Secondary node %s didn't return free"
2897 22b7f6f8 Thomas Thrainer
                                       " memory information" % node,
2898 22b7f6f8 Thomas Thrainer
                                       errors.ECODE_STATE)
2899 22b7f6f8 Thomas Thrainer
          #TODO(dynmem): do the appropriate check involving MINMEM
2900 22b7f6f8 Thomas Thrainer
          elif be_new[constants.BE_MAXMEM] > nhvinfo["memory_free"]:
2901 22b7f6f8 Thomas Thrainer
            raise errors.OpPrereqError("This change will prevent the instance"
2902 22b7f6f8 Thomas Thrainer
                                       " from failover to its secondary node"
2903 22b7f6f8 Thomas Thrainer
                                       " %s, due to not enough memory" % node,
2904 22b7f6f8 Thomas Thrainer
                                       errors.ECODE_STATE)
2905 22b7f6f8 Thomas Thrainer
2906 22b7f6f8 Thomas Thrainer
    if self.op.runtime_mem:
2907 22b7f6f8 Thomas Thrainer
      remote_info = self.rpc.call_instance_info(instance.primary_node,
2908 22b7f6f8 Thomas Thrainer
                                                instance.name,
2909 22b7f6f8 Thomas Thrainer
                                                instance.hypervisor)
2910 22b7f6f8 Thomas Thrainer
      remote_info.Raise("Error checking node %s" % instance.primary_node)
2911 22b7f6f8 Thomas Thrainer
      if not remote_info.payload: # not running already
2912 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Instance %s is not running" %
2913 22b7f6f8 Thomas Thrainer
                                   instance.name, errors.ECODE_STATE)
2914 22b7f6f8 Thomas Thrainer
2915 22b7f6f8 Thomas Thrainer
      current_memory = remote_info.payload["memory"]
2916 22b7f6f8 Thomas Thrainer
      if (not self.op.force and
2917 22b7f6f8 Thomas Thrainer
           (self.op.runtime_mem > self.be_proposed[constants.BE_MAXMEM] or
2918 22b7f6f8 Thomas Thrainer
            self.op.runtime_mem < self.be_proposed[constants.BE_MINMEM])):
2919 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Instance %s must have memory between %d"
2920 22b7f6f8 Thomas Thrainer
                                   " and %d MB of memory unless --force is"
2921 22b7f6f8 Thomas Thrainer
                                   " given" %
2922 22b7f6f8 Thomas Thrainer
                                   (instance.name,
2923 22b7f6f8 Thomas Thrainer
                                    self.be_proposed[constants.BE_MINMEM],
2924 22b7f6f8 Thomas Thrainer
                                    self.be_proposed[constants.BE_MAXMEM]),
2925 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
2926 22b7f6f8 Thomas Thrainer
2927 22b7f6f8 Thomas Thrainer
      delta = self.op.runtime_mem - current_memory
2928 22b7f6f8 Thomas Thrainer
      if delta > 0:
2929 5eacbcae Thomas Thrainer
        CheckNodeFreeMemory(self, instance.primary_node,
2930 5eacbcae Thomas Thrainer
                            "ballooning memory for instance %s" %
2931 5eacbcae Thomas Thrainer
                            instance.name, delta, instance.hypervisor)
2932 22b7f6f8 Thomas Thrainer
2933 22b7f6f8 Thomas Thrainer
    if self.op.disks and instance.disk_template == constants.DT_DISKLESS:
2934 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Disk operations not supported for"
2935 22b7f6f8 Thomas Thrainer
                                 " diskless instances", errors.ECODE_INVAL)
2936 22b7f6f8 Thomas Thrainer
2937 22b7f6f8 Thomas Thrainer
    def _PrepareNicCreate(_, params, private):
2938 22b7f6f8 Thomas Thrainer
      self._PrepareNicModification(params, private, None, None,
2939 22b7f6f8 Thomas Thrainer
                                   {}, cluster, pnode)
2940 22b7f6f8 Thomas Thrainer
      return (None, None)
2941 22b7f6f8 Thomas Thrainer
2942 22b7f6f8 Thomas Thrainer
    def _PrepareNicMod(_, nic, params, private):
2943 22b7f6f8 Thomas Thrainer
      self._PrepareNicModification(params, private, nic.ip, nic.network,
2944 22b7f6f8 Thomas Thrainer
                                   nic.nicparams, cluster, pnode)
2945 22b7f6f8 Thomas Thrainer
      return None
2946 22b7f6f8 Thomas Thrainer
2947 22b7f6f8 Thomas Thrainer
    def _PrepareNicRemove(_, params, __):
2948 22b7f6f8 Thomas Thrainer
      ip = params.ip
2949 22b7f6f8 Thomas Thrainer
      net = params.network
2950 22b7f6f8 Thomas Thrainer
      if net is not None and ip is not None:
2951 22b7f6f8 Thomas Thrainer
        self.cfg.ReleaseIp(net, ip, self.proc.GetECId())
2952 22b7f6f8 Thomas Thrainer
2953 22b7f6f8 Thomas Thrainer
    # Verify NIC changes (operating on copy)
2954 22b7f6f8 Thomas Thrainer
    nics = instance.nics[:]
2955 5eacbcae Thomas Thrainer
    _ApplyContainerMods("NIC", nics, None, self.nicmod,
2956 5eacbcae Thomas Thrainer
                        _PrepareNicCreate, _PrepareNicMod, _PrepareNicRemove)
2957 22b7f6f8 Thomas Thrainer
    if len(nics) > constants.MAX_NICS:
2958 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Instance has too many network interfaces"
2959 22b7f6f8 Thomas Thrainer
                                 " (%d), cannot add more" % constants.MAX_NICS,
2960 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_STATE)
2961 22b7f6f8 Thomas Thrainer
2962 22b7f6f8 Thomas Thrainer
    def _PrepareDiskMod(_, disk, params, __):
2963 22b7f6f8 Thomas Thrainer
      disk.name = params.get(constants.IDISK_NAME, None)
2964 22b7f6f8 Thomas Thrainer
2965 22b7f6f8 Thomas Thrainer
    # Verify disk changes (operating on a copy)
2966 22b7f6f8 Thomas Thrainer
    disks = copy.deepcopy(instance.disks)
2967 5eacbcae Thomas Thrainer
    _ApplyContainerMods("disk", disks, None, self.diskmod, None,
2968 5eacbcae Thomas Thrainer
                        _PrepareDiskMod, None)
2969 22b7f6f8 Thomas Thrainer
    utils.ValidateDeviceNames("disk", disks)
2970 22b7f6f8 Thomas Thrainer
    if len(disks) > constants.MAX_DISKS:
2971 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Instance has too many disks (%d), cannot add"
2972 22b7f6f8 Thomas Thrainer
                                 " more" % constants.MAX_DISKS,
2973 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_STATE)
2974 22b7f6f8 Thomas Thrainer
    disk_sizes = [disk.size for disk in instance.disks]
2975 22b7f6f8 Thomas Thrainer
    disk_sizes.extend(params["size"] for (op, idx, params, private) in
2976 22b7f6f8 Thomas Thrainer
                      self.diskmod if op == constants.DDM_ADD)
2977 22b7f6f8 Thomas Thrainer
    ispec[constants.ISPEC_DISK_COUNT] = len(disk_sizes)
2978 22b7f6f8 Thomas Thrainer
    ispec[constants.ISPEC_DISK_SIZE] = disk_sizes
2979 22b7f6f8 Thomas Thrainer
2980 22b7f6f8 Thomas Thrainer
    if self.op.offline is not None and self.op.offline:
2981 5eacbcae Thomas Thrainer
      CheckInstanceState(self, instance, CAN_CHANGE_INSTANCE_OFFLINE,
2982 5eacbcae Thomas Thrainer
                         msg="can't change to offline")
2983 22b7f6f8 Thomas Thrainer
2984 22b7f6f8 Thomas Thrainer
    # Pre-compute NIC changes (necessary to use result in hooks)
2985 22b7f6f8 Thomas Thrainer
    self._nic_chgdesc = []
2986 22b7f6f8 Thomas Thrainer
    if self.nicmod:
2987 22b7f6f8 Thomas Thrainer
      # Operate on copies as this is still in prereq
2988 22b7f6f8 Thomas Thrainer
      nics = [nic.Copy() for nic in instance.nics]
2989 5eacbcae Thomas Thrainer
      _ApplyContainerMods("NIC", nics, self._nic_chgdesc, self.nicmod,
2990 5eacbcae Thomas Thrainer
                          self._CreateNewNic, self._ApplyNicMods, None)
2991 22b7f6f8 Thomas Thrainer
      # Verify that NIC names are unique and valid
2992 22b7f6f8 Thomas Thrainer
      utils.ValidateDeviceNames("NIC", nics)
2993 22b7f6f8 Thomas Thrainer
      self._new_nics = nics
2994 22b7f6f8 Thomas Thrainer
      ispec[constants.ISPEC_NIC_COUNT] = len(self._new_nics)
2995 22b7f6f8 Thomas Thrainer
    else:
2996 22b7f6f8 Thomas Thrainer
      self._new_nics = None
2997 22b7f6f8 Thomas Thrainer
      ispec[constants.ISPEC_NIC_COUNT] = len(instance.nics)
2998 22b7f6f8 Thomas Thrainer
2999 22b7f6f8 Thomas Thrainer
    if not self.op.ignore_ipolicy:
3000 22b7f6f8 Thomas Thrainer
      ipolicy = ganeti.masterd.instance.CalculateGroupIPolicy(cluster,
3001 22b7f6f8 Thomas Thrainer
                                                              group_info)
3002 22b7f6f8 Thomas Thrainer
3003 22b7f6f8 Thomas Thrainer
      # Fill ispec with backend parameters
3004 22b7f6f8 Thomas Thrainer
      ispec[constants.ISPEC_SPINDLE_USE] = \
3005 22b7f6f8 Thomas Thrainer
        self.be_new.get(constants.BE_SPINDLE_USE, None)
3006 22b7f6f8 Thomas Thrainer
      ispec[constants.ISPEC_CPU_COUNT] = self.be_new.get(constants.BE_VCPUS,
3007 22b7f6f8 Thomas Thrainer
                                                         None)
3008 22b7f6f8 Thomas Thrainer
3009 22b7f6f8 Thomas Thrainer
      # Copy ispec to verify parameters with min/max values separately
3010 22b7f6f8 Thomas Thrainer
      if self.op.disk_template:
3011 22b7f6f8 Thomas Thrainer
        new_disk_template = self.op.disk_template
3012 22b7f6f8 Thomas Thrainer
      else:
3013 22b7f6f8 Thomas Thrainer
        new_disk_template = instance.disk_template
3014 22b7f6f8 Thomas Thrainer
      ispec_max = ispec.copy()
3015 22b7f6f8 Thomas Thrainer
      ispec_max[constants.ISPEC_MEM_SIZE] = \
3016 22b7f6f8 Thomas Thrainer
        self.be_new.get(constants.BE_MAXMEM, None)
3017 22b7f6f8 Thomas Thrainer
      res_max = _ComputeIPolicyInstanceSpecViolation(ipolicy, ispec_max,
3018 22b7f6f8 Thomas Thrainer
                                                     new_disk_template)
3019 22b7f6f8 Thomas Thrainer
      ispec_min = ispec.copy()
3020 22b7f6f8 Thomas Thrainer
      ispec_min[constants.ISPEC_MEM_SIZE] = \
3021 22b7f6f8 Thomas Thrainer
        self.be_new.get(constants.BE_MINMEM, None)
3022 22b7f6f8 Thomas Thrainer
      res_min = _ComputeIPolicyInstanceSpecViolation(ipolicy, ispec_min,
3023 22b7f6f8 Thomas Thrainer
                                                     new_disk_template)
3024 22b7f6f8 Thomas Thrainer
3025 22b7f6f8 Thomas Thrainer
      if (res_max or res_min):
3026 22b7f6f8 Thomas Thrainer
        # FIXME: Improve error message by including information about whether
3027 22b7f6f8 Thomas Thrainer
        # the upper or lower limit of the parameter fails the ipolicy.
3028 22b7f6f8 Thomas Thrainer
        msg = ("Instance allocation to group %s (%s) violates policy: %s" %
3029 22b7f6f8 Thomas Thrainer
               (group_info, group_info.name,
3030 22b7f6f8 Thomas Thrainer
                utils.CommaJoin(set(res_max + res_min))))
3031 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
3032 22b7f6f8 Thomas Thrainer
3033 22b7f6f8 Thomas Thrainer
  def _ConvertPlainToDrbd(self, feedback_fn):
3034 22b7f6f8 Thomas Thrainer
    """Converts an instance from plain to drbd.
3035 22b7f6f8 Thomas Thrainer

3036 22b7f6f8 Thomas Thrainer
    """
3037 22b7f6f8 Thomas Thrainer
    feedback_fn("Converting template to drbd")
3038 22b7f6f8 Thomas Thrainer
    instance = self.instance
3039 22b7f6f8 Thomas Thrainer
    pnode = instance.primary_node
3040 22b7f6f8 Thomas Thrainer
    snode = self.op.remote_node
3041 22b7f6f8 Thomas Thrainer
3042 22b7f6f8 Thomas Thrainer
    assert instance.disk_template == constants.DT_PLAIN
3043 22b7f6f8 Thomas Thrainer
3044 22b7f6f8 Thomas Thrainer
    # create a fake disk info for _GenerateDiskTemplate
3045 22b7f6f8 Thomas Thrainer
    disk_info = [{constants.IDISK_SIZE: d.size, constants.IDISK_MODE: d.mode,
3046 22b7f6f8 Thomas Thrainer
                  constants.IDISK_VG: d.logical_id[0],
3047 22b7f6f8 Thomas Thrainer
                  constants.IDISK_NAME: d.name}
3048 22b7f6f8 Thomas Thrainer
                 for d in instance.disks]
3049 5eacbcae Thomas Thrainer
    new_disks = GenerateDiskTemplate(self, self.op.disk_template,
3050 5eacbcae Thomas Thrainer
                                     instance.name, pnode, [snode],
3051 5eacbcae Thomas Thrainer
                                     disk_info, None, None, 0, feedback_fn,
3052 5eacbcae Thomas Thrainer
                                     self.diskparams)
3053 22b7f6f8 Thomas Thrainer
    anno_disks = rpc.AnnotateDiskParams(constants.DT_DRBD8, new_disks,
3054 22b7f6f8 Thomas Thrainer
                                        self.diskparams)
3055 5eacbcae Thomas Thrainer
    p_excl_stor = IsExclusiveStorageEnabledNodeName(self.cfg, pnode)
3056 5eacbcae Thomas Thrainer
    s_excl_stor = IsExclusiveStorageEnabledNodeName(self.cfg, snode)
3057 5eacbcae Thomas Thrainer
    info = GetInstanceInfoText(instance)
3058 22b7f6f8 Thomas Thrainer
    feedback_fn("Creating additional volumes...")
3059 22b7f6f8 Thomas Thrainer
    # first, create the missing data and meta devices
3060 22b7f6f8 Thomas Thrainer
    for disk in anno_disks:
3061 22b7f6f8 Thomas Thrainer
      # unfortunately this is... not too nice
3062 5eacbcae Thomas Thrainer
      CreateSingleBlockDev(self, pnode, instance, disk.children[1],
3063 5eacbcae Thomas Thrainer
                           info, True, p_excl_stor)
3064 22b7f6f8 Thomas Thrainer
      for child in disk.children:
3065 5eacbcae Thomas Thrainer
        CreateSingleBlockDev(self, snode, instance, child, info, True,
3066 5eacbcae Thomas Thrainer
                             s_excl_stor)
3067 22b7f6f8 Thomas Thrainer
    # at this stage, all new LVs have been created, we can rename the
3068 22b7f6f8 Thomas Thrainer
    # old ones
3069 22b7f6f8 Thomas Thrainer
    feedback_fn("Renaming original volumes...")
3070 22b7f6f8 Thomas Thrainer
    rename_list = [(o, n.children[0].logical_id)
3071 22b7f6f8 Thomas Thrainer
                   for (o, n) in zip(instance.disks, new_disks)]
3072 22b7f6f8 Thomas Thrainer
    result = self.rpc.call_blockdev_rename(pnode, rename_list)
3073 22b7f6f8 Thomas Thrainer
    result.Raise("Failed to rename original LVs")
3074 22b7f6f8 Thomas Thrainer
3075 22b7f6f8 Thomas Thrainer
    feedback_fn("Initializing DRBD devices...")
3076 22b7f6f8 Thomas Thrainer
    # all child devices are in place, we can now create the DRBD devices
3077 22b7f6f8 Thomas Thrainer
    try:
3078 22b7f6f8 Thomas Thrainer
      for disk in anno_disks:
3079 22b7f6f8 Thomas Thrainer
        for (node, excl_stor) in [(pnode, p_excl_stor), (snode, s_excl_stor)]:
3080 22b7f6f8 Thomas Thrainer
          f_create = node == pnode
3081 5eacbcae Thomas Thrainer
          CreateSingleBlockDev(self, node, instance, disk, info, f_create,
3082 5eacbcae Thomas Thrainer
                               excl_stor)
3083 22b7f6f8 Thomas Thrainer
    except errors.GenericError, e:
3084 22b7f6f8 Thomas Thrainer
      feedback_fn("Initializing of DRBD devices failed;"
3085 22b7f6f8 Thomas Thrainer
                  " renaming back original volumes...")
3086 22b7f6f8 Thomas Thrainer
      for disk in new_disks:
3087 22b7f6f8 Thomas Thrainer
        self.cfg.SetDiskID(disk, pnode)
3088 22b7f6f8 Thomas Thrainer
      rename_back_list = [(n.children[0], o.logical_id)
3089 22b7f6f8 Thomas Thrainer
                          for (n, o) in zip(new_disks, instance.disks)]
3090 22b7f6f8 Thomas Thrainer
      result = self.rpc.call_blockdev_rename(pnode, rename_back_list)
3091 22b7f6f8 Thomas Thrainer
      result.Raise("Failed to rename LVs back after error %s" % str(e))
3092 22b7f6f8 Thomas Thrainer
      raise
3093 22b7f6f8 Thomas Thrainer
3094 22b7f6f8 Thomas Thrainer
    # at this point, the instance has been modified
3095 22b7f6f8 Thomas Thrainer
    instance.disk_template = constants.DT_DRBD8
3096 22b7f6f8 Thomas Thrainer
    instance.disks = new_disks
3097 22b7f6f8 Thomas Thrainer
    self.cfg.Update(instance, feedback_fn)
3098 22b7f6f8 Thomas Thrainer
3099 22b7f6f8 Thomas Thrainer
    # Release node locks while waiting for sync
3100 5eacbcae Thomas Thrainer
    ReleaseLocks(self, locking.LEVEL_NODE)
3101 22b7f6f8 Thomas Thrainer
3102 22b7f6f8 Thomas Thrainer
    # disks are created, waiting for sync
3103 5eacbcae Thomas Thrainer
    disk_abort = not WaitForSync(self, instance,
3104 5eacbcae Thomas Thrainer
                                 oneshot=not self.op.wait_for_sync)
3105 22b7f6f8 Thomas Thrainer
    if disk_abort:
3106 22b7f6f8 Thomas Thrainer
      raise errors.OpExecError("There are some degraded disks for"
3107 22b7f6f8 Thomas Thrainer
                               " this instance, please cleanup manually")
3108 22b7f6f8 Thomas Thrainer
3109 22b7f6f8 Thomas Thrainer
    # Node resource locks will be released by caller
3110 22b7f6f8 Thomas Thrainer
3111 22b7f6f8 Thomas Thrainer
  def _ConvertDrbdToPlain(self, feedback_fn):
3112 22b7f6f8 Thomas Thrainer
    """Converts an instance from drbd to plain.
3113 22b7f6f8 Thomas Thrainer

3114 22b7f6f8 Thomas Thrainer
    """
3115 22b7f6f8 Thomas Thrainer
    instance = self.instance
3116 22b7f6f8 Thomas Thrainer
3117 22b7f6f8 Thomas Thrainer
    assert len(instance.secondary_nodes) == 1
3118 22b7f6f8 Thomas Thrainer
    assert instance.disk_template == constants.DT_DRBD8
3119 22b7f6f8 Thomas Thrainer
3120 22b7f6f8 Thomas Thrainer
    pnode = instance.primary_node
3121 22b7f6f8 Thomas Thrainer
    snode = instance.secondary_nodes[0]
3122 22b7f6f8 Thomas Thrainer
    feedback_fn("Converting template to plain")
3123 22b7f6f8 Thomas Thrainer
3124 5eacbcae Thomas Thrainer
    old_disks = AnnotateDiskParams(instance, instance.disks, self.cfg)
3125 22b7f6f8 Thomas Thrainer
    new_disks = [d.children[0] for d in instance.disks]
3126 22b7f6f8 Thomas Thrainer
3127 22b7f6f8 Thomas Thrainer
    # copy over size, mode and name
3128 22b7f6f8 Thomas Thrainer
    for parent, child in zip(old_disks, new_disks):
3129 22b7f6f8 Thomas Thrainer
      child.size = parent.size
3130 22b7f6f8 Thomas Thrainer
      child.mode = parent.mode
3131 22b7f6f8 Thomas Thrainer
      child.name = parent.name
3132 22b7f6f8 Thomas Thrainer
3133 22b7f6f8 Thomas Thrainer
    # this is a DRBD disk, return its port to the pool
3134 22b7f6f8 Thomas Thrainer
    # NOTE: this must be done right before the call to cfg.Update!
3135 22b7f6f8 Thomas Thrainer
    for disk in old_disks:
3136 22b7f6f8 Thomas Thrainer
      tcp_port = disk.logical_id[2]
3137 22b7f6f8 Thomas Thrainer
      self.cfg.AddTcpUdpPort(tcp_port)
3138 22b7f6f8 Thomas Thrainer
3139 22b7f6f8 Thomas Thrainer
    # update instance structure
3140 22b7f6f8 Thomas Thrainer
    instance.disks = new_disks
3141 22b7f6f8 Thomas Thrainer
    instance.disk_template = constants.DT_PLAIN
3142 22b7f6f8 Thomas Thrainer
    _UpdateIvNames(0, instance.disks)
3143 22b7f6f8 Thomas Thrainer
    self.cfg.Update(instance, feedback_fn)
3144 22b7f6f8 Thomas Thrainer
3145 22b7f6f8 Thomas Thrainer
    # Release locks in case removing disks takes a while
3146 5eacbcae Thomas Thrainer
    ReleaseLocks(self, locking.LEVEL_NODE)
3147 22b7f6f8 Thomas Thrainer
3148 22b7f6f8 Thomas Thrainer
    feedback_fn("Removing volumes on the secondary node...")
3149 22b7f6f8 Thomas Thrainer
    for disk in old_disks:
3150 22b7f6f8 Thomas Thrainer
      self.cfg.SetDiskID(disk, snode)
3151 22b7f6f8 Thomas Thrainer
      msg = self.rpc.call_blockdev_remove(snode, disk).fail_msg
3152 22b7f6f8 Thomas Thrainer
      if msg:
3153 22b7f6f8 Thomas Thrainer
        self.LogWarning("Could not remove block device %s on node %s,"
3154 22b7f6f8 Thomas Thrainer
                        " continuing anyway: %s", disk.iv_name, snode, msg)
3155 22b7f6f8 Thomas Thrainer
3156 22b7f6f8 Thomas Thrainer
    feedback_fn("Removing unneeded volumes on the primary node...")
3157 22b7f6f8 Thomas Thrainer
    for idx, disk in enumerate(old_disks):
3158 22b7f6f8 Thomas Thrainer
      meta = disk.children[1]
3159 22b7f6f8 Thomas Thrainer
      self.cfg.SetDiskID(meta, pnode)
3160 22b7f6f8 Thomas Thrainer
      msg = self.rpc.call_blockdev_remove(pnode, meta).fail_msg
3161 22b7f6f8 Thomas Thrainer
      if msg:
3162 22b7f6f8 Thomas Thrainer
        self.LogWarning("Could not remove metadata for disk %d on node %s,"
3163 22b7f6f8 Thomas Thrainer
                        " continuing anyway: %s", idx, pnode, msg)
3164 22b7f6f8 Thomas Thrainer
3165 22b7f6f8 Thomas Thrainer
  def _CreateNewDisk(self, idx, params, _):
3166 22b7f6f8 Thomas Thrainer
    """Creates a new disk.
3167 22b7f6f8 Thomas Thrainer

3168 22b7f6f8 Thomas Thrainer
    """
3169 22b7f6f8 Thomas Thrainer
    instance = self.instance
3170 22b7f6f8 Thomas Thrainer
3171 22b7f6f8 Thomas Thrainer
    # add a new disk
3172 22b7f6f8 Thomas Thrainer
    if instance.disk_template in constants.DTS_FILEBASED:
3173 22b7f6f8 Thomas Thrainer
      (file_driver, file_path) = instance.disks[0].logical_id
3174 22b7f6f8 Thomas Thrainer
      file_path = os.path.dirname(file_path)
3175 22b7f6f8 Thomas Thrainer
    else:
3176 22b7f6f8 Thomas Thrainer
      file_driver = file_path = None
3177 22b7f6f8 Thomas Thrainer
3178 22b7f6f8 Thomas Thrainer
    disk = \
3179 5eacbcae Thomas Thrainer
      GenerateDiskTemplate(self, instance.disk_template, instance.name,
3180 5eacbcae Thomas Thrainer
                           instance.primary_node, instance.secondary_nodes,
3181 5eacbcae Thomas Thrainer
                           [params], file_path, file_driver, idx,
3182 5eacbcae Thomas Thrainer
                           self.Log, self.diskparams)[0]
3183 22b7f6f8 Thomas Thrainer
3184 a365b47f Bernardo Dal Seno
    new_disks = CreateDisks(self, instance, disks=[disk])
3185 22b7f6f8 Thomas Thrainer
3186 22b7f6f8 Thomas Thrainer
    if self.cluster.prealloc_wipe_disks:
3187 22b7f6f8 Thomas Thrainer
      # Wipe new disk
3188 a365b47f Bernardo Dal Seno
      WipeOrCleanupDisks(self, instance,
3189 a365b47f Bernardo Dal Seno
                         disks=[(idx, disk, 0)],
3190 a365b47f Bernardo Dal Seno
                         cleanup=new_disks)
3191 22b7f6f8 Thomas Thrainer
3192 22b7f6f8 Thomas Thrainer
    return (disk, [
3193 22b7f6f8 Thomas Thrainer
      ("disk/%d" % idx, "add:size=%s,mode=%s" % (disk.size, disk.mode)),
3194 22b7f6f8 Thomas Thrainer
      ])
3195 22b7f6f8 Thomas Thrainer
3196 c5c72215 Dimitris Aragiorgis
  def _ModifyDisk(self, idx, disk, params, _):
3197 22b7f6f8 Thomas Thrainer
    """Modifies a disk.
3198 22b7f6f8 Thomas Thrainer

3199 22b7f6f8 Thomas Thrainer
    """
3200 22b7f6f8 Thomas Thrainer
    changes = []
3201 4eef428e Dimitris Aragiorgis
    if constants.IDISK_MODE in params:
3202 4eef428e Dimitris Aragiorgis
      disk.mode = params.get(constants.IDISK_MODE)
3203 22b7f6f8 Thomas Thrainer
      changes.append(("disk.mode/%d" % idx, disk.mode))
3204 22b7f6f8 Thomas Thrainer
3205 4eef428e Dimitris Aragiorgis
    if constants.IDISK_NAME in params:
3206 4eef428e Dimitris Aragiorgis
      disk.name = params.get(constants.IDISK_NAME)
3207 4eef428e Dimitris Aragiorgis
      changes.append(("disk.name/%d" % idx, disk.name))
3208 22b7f6f8 Thomas Thrainer
3209 c5c72215 Dimitris Aragiorgis
    # Modify arbitrary params in case instance template is ext
3210 c5c72215 Dimitris Aragiorgis
    for key, value in params.iteritems():
3211 c5c72215 Dimitris Aragiorgis
      if (key not in constants.MODIFIABLE_IDISK_PARAMS and
3212 c5c72215 Dimitris Aragiorgis
          self.instance.disk_template == constants.DT_EXT):
3213 e228ab9c Dimitris Aragiorgis
        # stolen from GetUpdatedParams: default means reset/delete
3214 e228ab9c Dimitris Aragiorgis
        if value.lower() == constants.VALUE_DEFAULT:
3215 e228ab9c Dimitris Aragiorgis
          try:
3216 e228ab9c Dimitris Aragiorgis
            del disk.params[key]
3217 e228ab9c Dimitris Aragiorgis
          except KeyError:
3218 e228ab9c Dimitris Aragiorgis
            pass
3219 e228ab9c Dimitris Aragiorgis
        else:
3220 e228ab9c Dimitris Aragiorgis
          disk.params[key] = value
3221 c5c72215 Dimitris Aragiorgis
        changes.append(("disk.params:%s/%d" % (key, idx), value))
3222 c5c72215 Dimitris Aragiorgis
3223 22b7f6f8 Thomas Thrainer
    return changes
3224 22b7f6f8 Thomas Thrainer
3225 22b7f6f8 Thomas Thrainer
  def _RemoveDisk(self, idx, root, _):
3226 22b7f6f8 Thomas Thrainer
    """Removes a disk.
3227 22b7f6f8 Thomas Thrainer

3228 22b7f6f8 Thomas Thrainer
    """
3229 5eacbcae Thomas Thrainer
    (anno_disk,) = AnnotateDiskParams(self.instance, [root], self.cfg)
3230 22b7f6f8 Thomas Thrainer
    for node, disk in anno_disk.ComputeNodeTree(self.instance.primary_node):
3231 22b7f6f8 Thomas Thrainer
      self.cfg.SetDiskID(disk, node)
3232 22b7f6f8 Thomas Thrainer
      msg = self.rpc.call_blockdev_remove(node, disk).fail_msg
3233 22b7f6f8 Thomas Thrainer
      if msg:
3234 22b7f6f8 Thomas Thrainer
        self.LogWarning("Could not remove disk/%d on node '%s': %s,"
3235 22b7f6f8 Thomas Thrainer
                        " continuing anyway", idx, node, msg)
3236 22b7f6f8 Thomas Thrainer
3237 22b7f6f8 Thomas Thrainer
    # if this is a DRBD disk, return its port to the pool
3238 22b7f6f8 Thomas Thrainer
    if root.dev_type in constants.LDS_DRBD:
3239 22b7f6f8 Thomas Thrainer
      self.cfg.AddTcpUdpPort(root.logical_id[2])
3240 22b7f6f8 Thomas Thrainer
3241 22b7f6f8 Thomas Thrainer
  def _CreateNewNic(self, idx, params, private):
3242 22b7f6f8 Thomas Thrainer
    """Creates data structure for a new network interface.
3243 22b7f6f8 Thomas Thrainer

3244 22b7f6f8 Thomas Thrainer
    """
3245 22b7f6f8 Thomas Thrainer
    mac = params[constants.INIC_MAC]
3246 22b7f6f8 Thomas Thrainer
    ip = params.get(constants.INIC_IP, None)
3247 22b7f6f8 Thomas Thrainer
    net = params.get(constants.INIC_NETWORK, None)
3248 22b7f6f8 Thomas Thrainer
    name = params.get(constants.INIC_NAME, None)
3249 22b7f6f8 Thomas Thrainer
    net_uuid = self.cfg.LookupNetwork(net)
3250 22b7f6f8 Thomas Thrainer
    #TODO: not private.filled?? can a nic have no nicparams??
3251 22b7f6f8 Thomas Thrainer
    nicparams = private.filled
3252 22b7f6f8 Thomas Thrainer
    nobj = objects.NIC(mac=mac, ip=ip, network=net_uuid, name=name,
3253 22b7f6f8 Thomas Thrainer
                       nicparams=nicparams)
3254 22b7f6f8 Thomas Thrainer
    nobj.uuid = self.cfg.GenerateUniqueID(self.proc.GetECId())
3255 22b7f6f8 Thomas Thrainer
3256 22b7f6f8 Thomas Thrainer
    return (nobj, [
3257 22b7f6f8 Thomas Thrainer
      ("nic.%d" % idx,
3258 22b7f6f8 Thomas Thrainer
       "add:mac=%s,ip=%s,mode=%s,link=%s,network=%s" %
3259 22b7f6f8 Thomas Thrainer
       (mac, ip, private.filled[constants.NIC_MODE],
3260 22b7f6f8 Thomas Thrainer
       private.filled[constants.NIC_LINK],
3261 22b7f6f8 Thomas Thrainer
       net)),
3262 22b7f6f8 Thomas Thrainer
      ])
3263 22b7f6f8 Thomas Thrainer
3264 22b7f6f8 Thomas Thrainer
  def _ApplyNicMods(self, idx, nic, params, private):
3265 22b7f6f8 Thomas Thrainer
    """Modifies a network interface.
3266 22b7f6f8 Thomas Thrainer

3267 22b7f6f8 Thomas Thrainer
    """
3268 22b7f6f8 Thomas Thrainer
    changes = []
3269 22b7f6f8 Thomas Thrainer
3270 22b7f6f8 Thomas Thrainer
    for key in [constants.INIC_MAC, constants.INIC_IP, constants.INIC_NAME]:
3271 22b7f6f8 Thomas Thrainer
      if key in params:
3272 22b7f6f8 Thomas Thrainer
        changes.append(("nic.%s/%d" % (key, idx), params[key]))
3273 22b7f6f8 Thomas Thrainer
        setattr(nic, key, params[key])
3274 22b7f6f8 Thomas Thrainer
3275 22b7f6f8 Thomas Thrainer
    new_net = params.get(constants.INIC_NETWORK, nic.network)
3276 22b7f6f8 Thomas Thrainer
    new_net_uuid = self.cfg.LookupNetwork(new_net)
3277 22b7f6f8 Thomas Thrainer
    if new_net_uuid != nic.network:
3278 22b7f6f8 Thomas Thrainer
      changes.append(("nic.network/%d" % idx, new_net))
3279 22b7f6f8 Thomas Thrainer
      nic.network = new_net_uuid
3280 22b7f6f8 Thomas Thrainer
3281 22b7f6f8 Thomas Thrainer
    if private.filled:
3282 22b7f6f8 Thomas Thrainer
      nic.nicparams = private.filled
3283 22b7f6f8 Thomas Thrainer
3284 22b7f6f8 Thomas Thrainer
      for (key, val) in nic.nicparams.items():
3285 22b7f6f8 Thomas Thrainer
        changes.append(("nic.%s/%d" % (key, idx), val))
3286 22b7f6f8 Thomas Thrainer
3287 22b7f6f8 Thomas Thrainer
    return changes
3288 22b7f6f8 Thomas Thrainer
3289 22b7f6f8 Thomas Thrainer
  def Exec(self, feedback_fn):
3290 22b7f6f8 Thomas Thrainer
    """Modifies an instance.
3291 22b7f6f8 Thomas Thrainer

3292 22b7f6f8 Thomas Thrainer
    All parameters take effect only at the next restart of the instance.
3293 22b7f6f8 Thomas Thrainer

3294 22b7f6f8 Thomas Thrainer
    """
3295 22b7f6f8 Thomas Thrainer
    # Process here the warnings from CheckPrereq, as we don't have a
3296 22b7f6f8 Thomas Thrainer
    # feedback_fn there.
3297 22b7f6f8 Thomas Thrainer
    # TODO: Replace with self.LogWarning
3298 22b7f6f8 Thomas Thrainer
    for warn in self.warn:
3299 22b7f6f8 Thomas Thrainer
      feedback_fn("WARNING: %s" % warn)
3300 22b7f6f8 Thomas Thrainer
3301 22b7f6f8 Thomas Thrainer
    assert ((self.op.disk_template is None) ^
3302 22b7f6f8 Thomas Thrainer
            bool(self.owned_locks(locking.LEVEL_NODE_RES))), \
3303 22b7f6f8 Thomas Thrainer
      "Not owning any node resource locks"
3304 22b7f6f8 Thomas Thrainer
3305 22b7f6f8 Thomas Thrainer
    result = []
3306 22b7f6f8 Thomas Thrainer
    instance = self.instance
3307 22b7f6f8 Thomas Thrainer
3308 22b7f6f8 Thomas Thrainer
    # New primary node
3309 22b7f6f8 Thomas Thrainer
    if self.op.pnode:
3310 22b7f6f8 Thomas Thrainer
      instance.primary_node = self.op.pnode
3311 22b7f6f8 Thomas Thrainer
3312 22b7f6f8 Thomas Thrainer
    # runtime memory
3313 22b7f6f8 Thomas Thrainer
    if self.op.runtime_mem:
3314 22b7f6f8 Thomas Thrainer
      rpcres = self.rpc.call_instance_balloon_memory(instance.primary_node,
3315 22b7f6f8 Thomas Thrainer
                                                     instance,
3316 22b7f6f8 Thomas Thrainer
                                                     self.op.runtime_mem)
3317 22b7f6f8 Thomas Thrainer
      rpcres.Raise("Cannot modify instance runtime memory")
3318 22b7f6f8 Thomas Thrainer
      result.append(("runtime_memory", self.op.runtime_mem))
3319 22b7f6f8 Thomas Thrainer
3320 22b7f6f8 Thomas Thrainer
    # Apply disk changes
3321 5eacbcae Thomas Thrainer
    _ApplyContainerMods("disk", instance.disks, result, self.diskmod,
3322 5eacbcae Thomas Thrainer
                        self._CreateNewDisk, self._ModifyDisk,
3323 5eacbcae Thomas Thrainer
                        self._RemoveDisk)
3324 22b7f6f8 Thomas Thrainer
    _UpdateIvNames(0, instance.disks)
3325 22b7f6f8 Thomas Thrainer
3326 22b7f6f8 Thomas Thrainer
    if self.op.disk_template:
3327 22b7f6f8 Thomas Thrainer
      if __debug__:
3328 22b7f6f8 Thomas Thrainer
        check_nodes = set(instance.all_nodes)
3329 22b7f6f8 Thomas Thrainer
        if self.op.remote_node:
3330 22b7f6f8 Thomas Thrainer
          check_nodes.add(self.op.remote_node)
3331 22b7f6f8 Thomas Thrainer
        for level in [locking.LEVEL_NODE, locking.LEVEL_NODE_RES]:
3332 22b7f6f8 Thomas Thrainer
          owned = self.owned_locks(level)
3333 22b7f6f8 Thomas Thrainer
          assert not (check_nodes - owned), \
3334 22b7f6f8 Thomas Thrainer
            ("Not owning the correct locks, owning %r, expected at least %r" %
3335 22b7f6f8 Thomas Thrainer
             (owned, check_nodes))
3336 22b7f6f8 Thomas Thrainer
3337 5eacbcae Thomas Thrainer
      r_shut = ShutdownInstanceDisks(self, instance)
3338 22b7f6f8 Thomas Thrainer
      if not r_shut:
3339 22b7f6f8 Thomas Thrainer
        raise errors.OpExecError("Cannot shutdown instance disks, unable to"
3340 22b7f6f8 Thomas Thrainer
                                 " proceed with disk template conversion")
3341 22b7f6f8 Thomas Thrainer
      mode = (instance.disk_template, self.op.disk_template)
3342 22b7f6f8 Thomas Thrainer
      try:
3343 22b7f6f8 Thomas Thrainer
        self._DISK_CONVERSIONS[mode](self, feedback_fn)
3344 22b7f6f8 Thomas Thrainer
      except:
3345 22b7f6f8 Thomas Thrainer
        self.cfg.ReleaseDRBDMinors(instance.name)
3346 22b7f6f8 Thomas Thrainer
        raise
3347 22b7f6f8 Thomas Thrainer
      result.append(("disk_template", self.op.disk_template))
3348 22b7f6f8 Thomas Thrainer
3349 22b7f6f8 Thomas Thrainer
      assert instance.disk_template == self.op.disk_template, \
3350 22b7f6f8 Thomas Thrainer
        ("Expected disk template '%s', found '%s'" %
3351 22b7f6f8 Thomas Thrainer
         (self.op.disk_template, instance.disk_template))
3352 22b7f6f8 Thomas Thrainer
3353 22b7f6f8 Thomas Thrainer
    # Release node and resource locks if there are any (they might already have
3354 22b7f6f8 Thomas Thrainer
    # been released during disk conversion)
3355 5eacbcae Thomas Thrainer
    ReleaseLocks(self, locking.LEVEL_NODE)
3356 5eacbcae Thomas Thrainer
    ReleaseLocks(self, locking.LEVEL_NODE_RES)
3357 22b7f6f8 Thomas Thrainer
3358 22b7f6f8 Thomas Thrainer
    # Apply NIC changes
3359 22b7f6f8 Thomas Thrainer
    if self._new_nics is not None:
3360 22b7f6f8 Thomas Thrainer
      instance.nics = self._new_nics
3361 22b7f6f8 Thomas Thrainer
      result.extend(self._nic_chgdesc)
3362 22b7f6f8 Thomas Thrainer
3363 22b7f6f8 Thomas Thrainer
    # hvparams changes
3364 22b7f6f8 Thomas Thrainer
    if self.op.hvparams:
3365 22b7f6f8 Thomas Thrainer
      instance.hvparams = self.hv_inst
3366 22b7f6f8 Thomas Thrainer
      for key, val in self.op.hvparams.iteritems():
3367 22b7f6f8 Thomas Thrainer
        result.append(("hv/%s" % key, val))
3368 22b7f6f8 Thomas Thrainer
3369 22b7f6f8 Thomas Thrainer
    # beparams changes
3370 22b7f6f8 Thomas Thrainer
    if self.op.beparams:
3371 22b7f6f8 Thomas Thrainer
      instance.beparams = self.be_inst
3372 22b7f6f8 Thomas Thrainer
      for key, val in self.op.beparams.iteritems():
3373 22b7f6f8 Thomas Thrainer
        result.append(("be/%s" % key, val))
3374 22b7f6f8 Thomas Thrainer
3375 22b7f6f8 Thomas Thrainer
    # OS change
3376 22b7f6f8 Thomas Thrainer
    if self.op.os_name:
3377 22b7f6f8 Thomas Thrainer
      instance.os = self.op.os_name
3378 22b7f6f8 Thomas Thrainer
3379 22b7f6f8 Thomas Thrainer
    # osparams changes
3380 22b7f6f8 Thomas Thrainer
    if self.op.osparams:
3381 22b7f6f8 Thomas Thrainer
      instance.osparams = self.os_inst
3382 22b7f6f8 Thomas Thrainer
      for key, val in self.op.osparams.iteritems():
3383 22b7f6f8 Thomas Thrainer
        result.append(("os/%s" % key, val))
3384 22b7f6f8 Thomas Thrainer
3385 22b7f6f8 Thomas Thrainer
    if self.op.offline is None:
3386 22b7f6f8 Thomas Thrainer
      # Ignore
3387 22b7f6f8 Thomas Thrainer
      pass
3388 22b7f6f8 Thomas Thrainer
    elif self.op.offline:
3389 22b7f6f8 Thomas Thrainer
      # Mark instance as offline
3390 22b7f6f8 Thomas Thrainer
      self.cfg.MarkInstanceOffline(instance.name)
3391 22b7f6f8 Thomas Thrainer
      result.append(("admin_state", constants.ADMINST_OFFLINE))
3392 22b7f6f8 Thomas Thrainer
    else:
3393 22b7f6f8 Thomas Thrainer
      # Mark instance as online, but stopped
3394 22b7f6f8 Thomas Thrainer
      self.cfg.MarkInstanceDown(instance.name)
3395 22b7f6f8 Thomas Thrainer
      result.append(("admin_state", constants.ADMINST_DOWN))
3396 22b7f6f8 Thomas Thrainer
3397 22b7f6f8 Thomas Thrainer
    self.cfg.Update(instance, feedback_fn, self.proc.GetECId())
3398 22b7f6f8 Thomas Thrainer
3399 22b7f6f8 Thomas Thrainer
    assert not (self.owned_locks(locking.LEVEL_NODE_RES) or
3400 22b7f6f8 Thomas Thrainer
                self.owned_locks(locking.LEVEL_NODE)), \
3401 22b7f6f8 Thomas Thrainer
      "All node locks should have been released by now"
3402 22b7f6f8 Thomas Thrainer
3403 22b7f6f8 Thomas Thrainer
    return result
3404 22b7f6f8 Thomas Thrainer
3405 22b7f6f8 Thomas Thrainer
  _DISK_CONVERSIONS = {
3406 22b7f6f8 Thomas Thrainer
    (constants.DT_PLAIN, constants.DT_DRBD8): _ConvertPlainToDrbd,
3407 22b7f6f8 Thomas Thrainer
    (constants.DT_DRBD8, constants.DT_PLAIN): _ConvertDrbdToPlain,
3408 22b7f6f8 Thomas Thrainer
    }
3409 22b7f6f8 Thomas Thrainer
3410 22b7f6f8 Thomas Thrainer
3411 22b7f6f8 Thomas Thrainer
class LUInstanceChangeGroup(LogicalUnit):
3412 22b7f6f8 Thomas Thrainer
  HPATH = "instance-change-group"
3413 22b7f6f8 Thomas Thrainer
  HTYPE = constants.HTYPE_INSTANCE
3414 22b7f6f8 Thomas Thrainer
  REQ_BGL = False
3415 22b7f6f8 Thomas Thrainer
3416 22b7f6f8 Thomas Thrainer
  def ExpandNames(self):
3417 5eacbcae Thomas Thrainer
    self.share_locks = ShareAll()
3418 22b7f6f8 Thomas Thrainer
3419 22b7f6f8 Thomas Thrainer
    self.needed_locks = {
3420 22b7f6f8 Thomas Thrainer
      locking.LEVEL_NODEGROUP: [],
3421 22b7f6f8 Thomas Thrainer
      locking.LEVEL_NODE: [],
3422 22b7f6f8 Thomas Thrainer
      locking.LEVEL_NODE_ALLOC: locking.ALL_SET,
3423 22b7f6f8 Thomas Thrainer
      }
3424 22b7f6f8 Thomas Thrainer
3425 22b7f6f8 Thomas Thrainer
    self._ExpandAndLockInstance()
3426 22b7f6f8 Thomas Thrainer
3427 22b7f6f8 Thomas Thrainer
    if self.op.target_groups:
3428 22b7f6f8 Thomas Thrainer
      self.req_target_uuids = map(self.cfg.LookupNodeGroup,
3429 22b7f6f8 Thomas Thrainer
                                  self.op.target_groups)
3430 22b7f6f8 Thomas Thrainer
    else:
3431 22b7f6f8 Thomas Thrainer
      self.req_target_uuids = None
3432 22b7f6f8 Thomas Thrainer
3433 5eacbcae Thomas Thrainer
    self.op.iallocator = GetDefaultIAllocator(self.cfg, self.op.iallocator)
3434 22b7f6f8 Thomas Thrainer
3435 22b7f6f8 Thomas Thrainer
  def DeclareLocks(self, level):
3436 22b7f6f8 Thomas Thrainer
    if level == locking.LEVEL_NODEGROUP:
3437 22b7f6f8 Thomas Thrainer
      assert not self.needed_locks[locking.LEVEL_NODEGROUP]
3438 22b7f6f8 Thomas Thrainer
3439 22b7f6f8 Thomas Thrainer
      if self.req_target_uuids:
3440 22b7f6f8 Thomas Thrainer
        lock_groups = set(self.req_target_uuids)
3441 22b7f6f8 Thomas Thrainer
3442 22b7f6f8 Thomas Thrainer
        # Lock all groups used by instance optimistically; this requires going
3443 22b7f6f8 Thomas Thrainer
        # via the node before it's locked, requiring verification later on
3444 22b7f6f8 Thomas Thrainer
        instance_groups = self.cfg.GetInstanceNodeGroups(self.op.instance_name)
3445 22b7f6f8 Thomas Thrainer
        lock_groups.update(instance_groups)
3446 22b7f6f8 Thomas Thrainer
      else:
3447 22b7f6f8 Thomas Thrainer
        # No target groups, need to lock all of them
3448 22b7f6f8 Thomas Thrainer
        lock_groups = locking.ALL_SET
3449 22b7f6f8 Thomas Thrainer
3450 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODEGROUP] = lock_groups
3451 22b7f6f8 Thomas Thrainer
3452 22b7f6f8 Thomas Thrainer
    elif level == locking.LEVEL_NODE:
3453 22b7f6f8 Thomas Thrainer
      if self.req_target_uuids:
3454 22b7f6f8 Thomas Thrainer
        # Lock all nodes used by instances
3455 22b7f6f8 Thomas Thrainer
        self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
3456 22b7f6f8 Thomas Thrainer
        self._LockInstancesNodes()
3457 22b7f6f8 Thomas Thrainer
3458 22b7f6f8 Thomas Thrainer
        # Lock all nodes in all potential target groups
3459 22b7f6f8 Thomas Thrainer
        lock_groups = (frozenset(self.owned_locks(locking.LEVEL_NODEGROUP)) -
3460 22b7f6f8 Thomas Thrainer
                       self.cfg.GetInstanceNodeGroups(self.op.instance_name))
3461 22b7f6f8 Thomas Thrainer
        member_nodes = [node_name
3462 22b7f6f8 Thomas Thrainer
                        for group in lock_groups
3463 22b7f6f8 Thomas Thrainer
                        for node_name in self.cfg.GetNodeGroup(group).members]
3464 22b7f6f8 Thomas Thrainer
        self.needed_locks[locking.LEVEL_NODE].extend(member_nodes)
3465 22b7f6f8 Thomas Thrainer
      else:
3466 22b7f6f8 Thomas Thrainer
        # Lock all nodes as all groups are potential targets
3467 22b7f6f8 Thomas Thrainer
        self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
3468 22b7f6f8 Thomas Thrainer
3469 22b7f6f8 Thomas Thrainer
  def CheckPrereq(self):
3470 22b7f6f8 Thomas Thrainer
    owned_instances = frozenset(self.owned_locks(locking.LEVEL_INSTANCE))
3471 22b7f6f8 Thomas Thrainer
    owned_groups = frozenset(self.owned_locks(locking.LEVEL_NODEGROUP))
3472 22b7f6f8 Thomas Thrainer
    owned_nodes = frozenset(self.owned_locks(locking.LEVEL_NODE))
3473 22b7f6f8 Thomas Thrainer
3474 22b7f6f8 Thomas Thrainer
    assert (self.req_target_uuids is None or
3475 22b7f6f8 Thomas Thrainer
            owned_groups.issuperset(self.req_target_uuids))
3476 22b7f6f8 Thomas Thrainer
    assert owned_instances == set([self.op.instance_name])
3477 22b7f6f8 Thomas Thrainer
3478 22b7f6f8 Thomas Thrainer
    # Get instance information
3479 22b7f6f8 Thomas Thrainer
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_name)
3480 22b7f6f8 Thomas Thrainer
3481 22b7f6f8 Thomas Thrainer
    # Check if node groups for locked instance are still correct
3482 22b7f6f8 Thomas Thrainer
    assert owned_nodes.issuperset(self.instance.all_nodes), \
3483 22b7f6f8 Thomas Thrainer
      ("Instance %s's nodes changed while we kept the lock" %
3484 22b7f6f8 Thomas Thrainer
       self.op.instance_name)
3485 22b7f6f8 Thomas Thrainer
3486 5eacbcae Thomas Thrainer
    inst_groups = CheckInstanceNodeGroups(self.cfg, self.op.instance_name,
3487 5eacbcae Thomas Thrainer
                                          owned_groups)
3488 22b7f6f8 Thomas Thrainer
3489 22b7f6f8 Thomas Thrainer
    if self.req_target_uuids:
3490 22b7f6f8 Thomas Thrainer
      # User requested specific target groups
3491 22b7f6f8 Thomas Thrainer
      self.target_uuids = frozenset(self.req_target_uuids)
3492 22b7f6f8 Thomas Thrainer
    else:
3493 22b7f6f8 Thomas Thrainer
      # All groups except those used by the instance are potential targets
3494 22b7f6f8 Thomas Thrainer
      self.target_uuids = owned_groups - inst_groups
3495 22b7f6f8 Thomas Thrainer
3496 22b7f6f8 Thomas Thrainer
    conflicting_groups = self.target_uuids & inst_groups
3497 22b7f6f8 Thomas Thrainer
    if conflicting_groups:
3498 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Can't use group(s) '%s' as targets, they are"
3499 22b7f6f8 Thomas Thrainer
                                 " used by the instance '%s'" %
3500 22b7f6f8 Thomas Thrainer
                                 (utils.CommaJoin(conflicting_groups),
3501 22b7f6f8 Thomas Thrainer
                                  self.op.instance_name),
3502 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
3503 22b7f6f8 Thomas Thrainer
3504 22b7f6f8 Thomas Thrainer
    if not self.target_uuids:
3505 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("There are no possible target groups",
3506 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
3507 22b7f6f8 Thomas Thrainer
3508 22b7f6f8 Thomas Thrainer
  def BuildHooksEnv(self):
3509 22b7f6f8 Thomas Thrainer
    """Build hooks env.
3510 22b7f6f8 Thomas Thrainer

3511 22b7f6f8 Thomas Thrainer
    """
3512 22b7f6f8 Thomas Thrainer
    assert self.target_uuids
3513 22b7f6f8 Thomas Thrainer
3514 22b7f6f8 Thomas Thrainer
    env = {
3515 22b7f6f8 Thomas Thrainer
      "TARGET_GROUPS": " ".join(self.target_uuids),
3516 22b7f6f8 Thomas Thrainer
      }
3517 22b7f6f8 Thomas Thrainer
3518 5eacbcae Thomas Thrainer
    env.update(BuildInstanceHookEnvByObject(self, self.instance))
3519 22b7f6f8 Thomas Thrainer
3520 22b7f6f8 Thomas Thrainer
    return env
3521 22b7f6f8 Thomas Thrainer
3522 22b7f6f8 Thomas Thrainer
  def BuildHooksNodes(self):
3523 22b7f6f8 Thomas Thrainer
    """Build hooks nodes.
3524 22b7f6f8 Thomas Thrainer

3525 22b7f6f8 Thomas Thrainer
    """
3526 22b7f6f8 Thomas Thrainer
    mn = self.cfg.GetMasterNode()
3527 22b7f6f8 Thomas Thrainer
    return ([mn], [mn])
3528 22b7f6f8 Thomas Thrainer
3529 22b7f6f8 Thomas Thrainer
  def Exec(self, feedback_fn):
3530 22b7f6f8 Thomas Thrainer
    instances = list(self.owned_locks(locking.LEVEL_INSTANCE))
3531 22b7f6f8 Thomas Thrainer
3532 22b7f6f8 Thomas Thrainer
    assert instances == [self.op.instance_name], "Instance not locked"
3533 22b7f6f8 Thomas Thrainer
3534 22b7f6f8 Thomas Thrainer
    req = iallocator.IAReqGroupChange(instances=instances,
3535 22b7f6f8 Thomas Thrainer
                                      target_groups=list(self.target_uuids))
3536 22b7f6f8 Thomas Thrainer
    ial = iallocator.IAllocator(self.cfg, self.rpc, req)
3537 22b7f6f8 Thomas Thrainer
3538 22b7f6f8 Thomas Thrainer
    ial.Run(self.op.iallocator)
3539 22b7f6f8 Thomas Thrainer
3540 22b7f6f8 Thomas Thrainer
    if not ial.success:
3541 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Can't compute solution for changing group of"
3542 22b7f6f8 Thomas Thrainer
                                 " instance '%s' using iallocator '%s': %s" %
3543 22b7f6f8 Thomas Thrainer
                                 (self.op.instance_name, self.op.iallocator,
3544 22b7f6f8 Thomas Thrainer
                                  ial.info), errors.ECODE_NORES)
3545 22b7f6f8 Thomas Thrainer
3546 5eacbcae Thomas Thrainer
    jobs = LoadNodeEvacResult(self, ial.result, self.op.early_release, False)
3547 22b7f6f8 Thomas Thrainer
3548 22b7f6f8 Thomas Thrainer
    self.LogInfo("Iallocator returned %s job(s) for changing group of"
3549 22b7f6f8 Thomas Thrainer
                 " instance '%s'", len(jobs), self.op.instance_name)
3550 22b7f6f8 Thomas Thrainer
3551 22b7f6f8 Thomas Thrainer
    return ResultWithJobs(jobs)