Statistics
| Branch: | Tag: | Revision:

root / lib / cmdlib / instance.py @ 9808764a

History | View | Annotate | Download (154.4 kB)

1 22b7f6f8 Thomas Thrainer
#
2 22b7f6f8 Thomas Thrainer
#
3 22b7f6f8 Thomas Thrainer
4 81c222af Jose A. Lopes
# Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 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 pathutils
40 6bce7ba2 Santi Raffa
from ganeti import serializer
41 4869595d Petr Pudlak
import ganeti.rpc.node as 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 da4a52a3 Thomas Thrainer
  AnnotateDiskParams, GetUpdatedParams, ExpandInstanceUuidAndName, \
52 1f7c8208 Helga Velroyen
  ComputeIPolicySpecViolation, CheckInstanceState, ExpandNodeUuidAndName, \
53 294254b1 Raffa Santi
  CheckDiskTemplateEnabled, IsValidDiskAccessModeCombination
54 5eacbcae Thomas Thrainer
from ganeti.cmdlib.instance_storage import CreateDisks, \
55 a365b47f Bernardo Dal Seno
  CheckNodesFreeDiskPerVG, WipeDisks, WipeOrCleanupDisks, WaitForSync, \
56 1c3231aa Thomas Thrainer
  IsExclusiveStorageEnabledNodeUuid, CreateSingleBlockDev, ComputeDisks, \
57 5eacbcae Thomas Thrainer
  CheckRADOSFreeSpace, ComputeDiskSizePerVG, GenerateDiskTemplate, \
58 3f3ea14c Bernardo Dal Seno
  StartInstanceDisks, ShutdownInstanceDisks, AssembleInstanceDisks, \
59 3f3ea14c Bernardo Dal Seno
  CheckSpindlesExclusiveStorage
60 5eacbcae Thomas Thrainer
from ganeti.cmdlib.instance_utils import BuildInstanceHookEnvByObject, \
61 5eacbcae Thomas Thrainer
  GetClusterDomainSecret, BuildInstanceHookEnv, NICListToTuple, \
62 5eacbcae Thomas Thrainer
  NICToTuple, CheckNodeNotDrained, RemoveInstance, CopyLockList, \
63 5eacbcae Thomas Thrainer
  ReleaseLocks, CheckNodeVmCapable, CheckTargetNodeIPolicy, \
64 5eacbcae Thomas Thrainer
  GetInstanceInfoText, RemoveDisks, CheckNodeFreeMemory, \
65 5eacbcae Thomas Thrainer
  CheckInstanceBridgesExist, CheckNicsBridgesExist, CheckNodeHasOS
66 22b7f6f8 Thomas Thrainer
67 22b7f6f8 Thomas Thrainer
import ganeti.masterd.instance
68 22b7f6f8 Thomas Thrainer
69 22b7f6f8 Thomas Thrainer
70 5eacbcae Thomas Thrainer
#: Type description for changes as returned by L{_ApplyContainerMods}'s
71 22b7f6f8 Thomas Thrainer
#: callbacks
72 22b7f6f8 Thomas Thrainer
_TApplyContModsCbChanges = \
73 22b7f6f8 Thomas Thrainer
  ht.TMaybeListOf(ht.TAnd(ht.TIsLength(2), ht.TItems([
74 22b7f6f8 Thomas Thrainer
    ht.TNonEmptyString,
75 22b7f6f8 Thomas Thrainer
    ht.TAny,
76 22b7f6f8 Thomas Thrainer
    ])))
77 22b7f6f8 Thomas Thrainer
78 22b7f6f8 Thomas Thrainer
79 22b7f6f8 Thomas Thrainer
def _CheckHostnameSane(lu, name):
80 22b7f6f8 Thomas Thrainer
  """Ensures that a given hostname resolves to a 'sane' name.
81 22b7f6f8 Thomas Thrainer

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

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

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

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

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

120 22b7f6f8 Thomas Thrainer
  @returns: A filled L{iallocator.IAReqInstanceAlloc}
121 22b7f6f8 Thomas Thrainer

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

140 22b7f6f8 Thomas Thrainer
  @param op: The instance opcode
141 22b7f6f8 Thomas Thrainer
  @param cluster: The cluster config object
142 22b7f6f8 Thomas Thrainer

143 22b7f6f8 Thomas Thrainer
  @return: The fully filled beparams
144 22b7f6f8 Thomas Thrainer

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

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

164 22b7f6f8 Thomas Thrainer
  @returns: The build up nics
165 22b7f6f8 Thomas Thrainer

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

255 22b7f6f8 Thomas Thrainer
  @type ip: string
256 22b7f6f8 Thomas Thrainer
  @param ip: IP address
257 1c3231aa Thomas Thrainer
  @type node_uuid: string
258 1c3231aa Thomas Thrainer
  @param node_uuid: node UUID
259 22b7f6f8 Thomas Thrainer

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

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

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

300 22b7f6f8 Thomas Thrainer
  @type os_obj: L{objects.OS}
301 22b7f6f8 Thomas Thrainer
  @param os_obj: OS object to check
302 22b7f6f8 Thomas Thrainer
  @type name: string
303 22b7f6f8 Thomas Thrainer
  @param name: OS name passed by the user, to check for validity
304 22b7f6f8 Thomas Thrainer

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

324 22b7f6f8 Thomas Thrainer
  """
325 22b7f6f8 Thomas Thrainer
  HPATH = "instance-add"
326 22b7f6f8 Thomas Thrainer
  HTYPE = constants.HTYPE_INSTANCE
327 22b7f6f8 Thomas Thrainer
  REQ_BGL = False
328 22b7f6f8 Thomas Thrainer
329 dab6ea3d Helga Velroyen
  def _CheckDiskTemplateValid(self):
330 dab6ea3d Helga Velroyen
    """Checks validity of disk template.
331 22b7f6f8 Thomas Thrainer

332 22b7f6f8 Thomas Thrainer
    """
333 22b7f6f8 Thomas Thrainer
    cluster = self.cfg.GetClusterInfo()
334 72080db1 Helga Velroyen
    if self.op.disk_template is None:
335 72080db1 Helga Velroyen
      # FIXME: It would be better to take the default disk template from the
336 72080db1 Helga Velroyen
      # ipolicy, but for the ipolicy we need the primary node, which we get from
337 72080db1 Helga Velroyen
      # the iallocator, which wants the disk template as input. To solve this
338 72080db1 Helga Velroyen
      # chicken-and-egg problem, it should be possible to specify just a node
339 72080db1 Helga Velroyen
      # group from the iallocator and take the ipolicy from that.
340 72080db1 Helga Velroyen
      self.op.disk_template = cluster.enabled_disk_templates[0]
341 1f7c8208 Helga Velroyen
    CheckDiskTemplateEnabled(cluster, self.op.disk_template)
342 22b7f6f8 Thomas Thrainer
343 dab6ea3d Helga Velroyen
  def _CheckDiskArguments(self):
344 dab6ea3d Helga Velroyen
    """Checks validity of disk-related arguments.
345 dab6ea3d Helga Velroyen

346 dab6ea3d Helga Velroyen
    """
347 dab6ea3d Helga Velroyen
    # check that disk's names are unique and valid
348 dab6ea3d Helga Velroyen
    utils.ValidateDeviceNames("disk", self.op.disks)
349 dab6ea3d Helga Velroyen
350 dab6ea3d Helga Velroyen
    self._CheckDiskTemplateValid()
351 dab6ea3d Helga Velroyen
352 22b7f6f8 Thomas Thrainer
    # check disks. parameter names and consistent adopt/no-adopt strategy
353 22b7f6f8 Thomas Thrainer
    has_adopt = has_no_adopt = False
354 22b7f6f8 Thomas Thrainer
    for disk in self.op.disks:
355 22b7f6f8 Thomas Thrainer
      if self.op.disk_template != constants.DT_EXT:
356 22b7f6f8 Thomas Thrainer
        utils.ForceDictType(disk, constants.IDISK_PARAMS_TYPES)
357 22b7f6f8 Thomas Thrainer
      if constants.IDISK_ADOPT in disk:
358 22b7f6f8 Thomas Thrainer
        has_adopt = True
359 22b7f6f8 Thomas Thrainer
      else:
360 22b7f6f8 Thomas Thrainer
        has_no_adopt = True
361 22b7f6f8 Thomas Thrainer
    if has_adopt and has_no_adopt:
362 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Either all disks are adopted or none is",
363 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
364 22b7f6f8 Thomas Thrainer
    if has_adopt:
365 22b7f6f8 Thomas Thrainer
      if self.op.disk_template not in constants.DTS_MAY_ADOPT:
366 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Disk adoption is not supported for the"
367 22b7f6f8 Thomas Thrainer
                                   " '%s' disk template" %
368 22b7f6f8 Thomas Thrainer
                                   self.op.disk_template,
369 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
370 22b7f6f8 Thomas Thrainer
      if self.op.iallocator is not None:
371 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Disk adoption not allowed with an"
372 22b7f6f8 Thomas Thrainer
                                   " iallocator script", errors.ECODE_INVAL)
373 22b7f6f8 Thomas Thrainer
      if self.op.mode == constants.INSTANCE_IMPORT:
374 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Disk adoption not allowed for"
375 22b7f6f8 Thomas Thrainer
                                   " instance import", errors.ECODE_INVAL)
376 22b7f6f8 Thomas Thrainer
    else:
377 22b7f6f8 Thomas Thrainer
      if self.op.disk_template in constants.DTS_MUST_ADOPT:
378 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Disk template %s requires disk adoption,"
379 22b7f6f8 Thomas Thrainer
                                   " but no 'adopt' parameter given" %
380 22b7f6f8 Thomas Thrainer
                                   self.op.disk_template,
381 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
382 22b7f6f8 Thomas Thrainer
383 22b7f6f8 Thomas Thrainer
    self.adopt_disks = has_adopt
384 22b7f6f8 Thomas Thrainer
385 9f7d5fe4 Sebastian Gebhard
  def _CheckVLANArguments(self):
386 9f7d5fe4 Sebastian Gebhard
    """ Check validity of VLANs if given
387 9f7d5fe4 Sebastian Gebhard

388 9f7d5fe4 Sebastian Gebhard
    """
389 9f7d5fe4 Sebastian Gebhard
    for nic in self.op.nics:
390 5ce6fa9a Sebastian Gebhard
      vlan = nic.get(constants.INIC_VLAN, None)
391 5ce6fa9a Sebastian Gebhard
      if vlan:
392 9f7d5fe4 Sebastian Gebhard
        if vlan[0] == ".":
393 9f7d5fe4 Sebastian Gebhard
          # vlan starting with dot means single untagged vlan,
394 9f7d5fe4 Sebastian Gebhard
          # might be followed by trunk (:)
395 9f7d5fe4 Sebastian Gebhard
          if not vlan[1:].isdigit():
396 9f7d5fe4 Sebastian Gebhard
            vlanlist = vlan[1:].split(':')
397 9f7d5fe4 Sebastian Gebhard
            for vl in vlanlist:
398 9f7d5fe4 Sebastian Gebhard
              if not vl.isdigit():
399 9f7d5fe4 Sebastian Gebhard
                raise errors.OpPrereqError("Specified VLAN parameter is "
400 9f7d5fe4 Sebastian Gebhard
                                           "invalid : %s" % vlan,
401 9f7d5fe4 Sebastian Gebhard
                                             errors.ECODE_INVAL)
402 9f7d5fe4 Sebastian Gebhard
        elif vlan[0] == ":":
403 9f7d5fe4 Sebastian Gebhard
          # Trunk - tagged only
404 9f7d5fe4 Sebastian Gebhard
          vlanlist = vlan[1:].split(':')
405 9f7d5fe4 Sebastian Gebhard
          for vl in vlanlist:
406 9f7d5fe4 Sebastian Gebhard
            if not vl.isdigit():
407 9f7d5fe4 Sebastian Gebhard
              raise errors.OpPrereqError("Specified VLAN parameter is invalid"
408 9f7d5fe4 Sebastian Gebhard
                                           " : %s" % vlan, errors.ECODE_INVAL)
409 9f7d5fe4 Sebastian Gebhard
        elif vlan.isdigit():
410 9f7d5fe4 Sebastian Gebhard
          # This is the simplest case. No dots, only single digit
411 9f7d5fe4 Sebastian Gebhard
          # -> Create untagged access port, dot needs to be added
412 9f7d5fe4 Sebastian Gebhard
          nic[constants.INIC_VLAN] = "." + vlan
413 9f7d5fe4 Sebastian Gebhard
        else:
414 9f7d5fe4 Sebastian Gebhard
          raise errors.OpPrereqError("Specified VLAN parameter is invalid"
415 9f7d5fe4 Sebastian Gebhard
                                       " : %s" % vlan, errors.ECODE_INVAL)
416 9f7d5fe4 Sebastian Gebhard
417 dab6ea3d Helga Velroyen
  def CheckArguments(self):
418 dab6ea3d Helga Velroyen
    """Check arguments.
419 dab6ea3d Helga Velroyen

420 dab6ea3d Helga Velroyen
    """
421 dab6ea3d Helga Velroyen
    # do not require name_check to ease forward/backward compatibility
422 dab6ea3d Helga Velroyen
    # for tools
423 dab6ea3d Helga Velroyen
    if self.op.no_install and self.op.start:
424 dab6ea3d Helga Velroyen
      self.LogInfo("No-installation mode selected, disabling startup")
425 dab6ea3d Helga Velroyen
      self.op.start = False
426 dab6ea3d Helga Velroyen
    # validate/normalize the instance name
427 dab6ea3d Helga Velroyen
    self.op.instance_name = \
428 dab6ea3d Helga Velroyen
      netutils.Hostname.GetNormalizedName(self.op.instance_name)
429 dab6ea3d Helga Velroyen
430 dab6ea3d Helga Velroyen
    if self.op.ip_check and not self.op.name_check:
431 dab6ea3d Helga Velroyen
      # TODO: make the ip check more flexible and not depend on the name check
432 dab6ea3d Helga Velroyen
      raise errors.OpPrereqError("Cannot do IP address check without a name"
433 dab6ea3d Helga Velroyen
                                 " check", errors.ECODE_INVAL)
434 dab6ea3d Helga Velroyen
435 7a51281a Jose A. Lopes
    # add NIC for instance communication
436 81c222af Jose A. Lopes
    if self.op.instance_communication:
437 81c222af Jose A. Lopes
      nic_name = "%s%s" % (constants.INSTANCE_COMMUNICATION_NIC_PREFIX,
438 81c222af Jose A. Lopes
                           self.op.instance_name)
439 81c222af Jose A. Lopes
440 81c222af Jose A. Lopes
      self.op.nics.append({constants.INIC_NAME: nic_name,
441 81c222af Jose A. Lopes
                           constants.INIC_MAC: constants.VALUE_GENERATE,
442 81c222af Jose A. Lopes
                           constants.INIC_IP: constants.NIC_IP_POOL,
443 9a94cee3 Jose A. Lopes
                           constants.INIC_NETWORK:
444 9a94cee3 Jose A. Lopes
                             self.cfg.GetInstanceCommunicationNetwork()})
445 81c222af Jose A. Lopes
446 dab6ea3d Helga Velroyen
    # check nics' parameter names
447 dab6ea3d Helga Velroyen
    for nic in self.op.nics:
448 dab6ea3d Helga Velroyen
      utils.ForceDictType(nic, constants.INIC_PARAMS_TYPES)
449 dab6ea3d Helga Velroyen
    # check that NIC's parameters names are unique and valid
450 dab6ea3d Helga Velroyen
    utils.ValidateDeviceNames("NIC", self.op.nics)
451 dab6ea3d Helga Velroyen
452 9f7d5fe4 Sebastian Gebhard
    self._CheckVLANArguments()
453 9f7d5fe4 Sebastian Gebhard
454 dab6ea3d Helga Velroyen
    self._CheckDiskArguments()
455 66222813 Thomas Thrainer
    assert self.op.disk_template is not None
456 dab6ea3d Helga Velroyen
457 22b7f6f8 Thomas Thrainer
    # instance name verification
458 22b7f6f8 Thomas Thrainer
    if self.op.name_check:
459 da4a52a3 Thomas Thrainer
      self.hostname = _CheckHostnameSane(self, self.op.instance_name)
460 da4a52a3 Thomas Thrainer
      self.op.instance_name = self.hostname.name
461 22b7f6f8 Thomas Thrainer
      # used in CheckPrereq for ip ping check
462 da4a52a3 Thomas Thrainer
      self.check_ip = self.hostname.ip
463 22b7f6f8 Thomas Thrainer
    else:
464 22b7f6f8 Thomas Thrainer
      self.check_ip = None
465 22b7f6f8 Thomas Thrainer
466 22b7f6f8 Thomas Thrainer
    # file storage checks
467 22b7f6f8 Thomas Thrainer
    if (self.op.file_driver and
468 22b7f6f8 Thomas Thrainer
        not self.op.file_driver in constants.FILE_DRIVER):
469 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Invalid file driver name '%s'" %
470 22b7f6f8 Thomas Thrainer
                                 self.op.file_driver, errors.ECODE_INVAL)
471 22b7f6f8 Thomas Thrainer
472 ff34fb97 Michele Tartara
    # set default file_driver if unset and required
473 ff34fb97 Michele Tartara
    if (not self.op.file_driver and
474 a09639d1 Santi Raffa
        self.op.disk_template in constants.DTS_FILEBASED):
475 ff34fb97 Michele Tartara
      self.op.file_driver = constants.FD_LOOP
476 ff34fb97 Michele Tartara
477 22b7f6f8 Thomas Thrainer
    ### Node/iallocator related checks
478 5eacbcae Thomas Thrainer
    CheckIAllocatorOrNode(self, "iallocator", "pnode")
479 22b7f6f8 Thomas Thrainer
480 22b7f6f8 Thomas Thrainer
    if self.op.pnode is not None:
481 22b7f6f8 Thomas Thrainer
      if self.op.disk_template in constants.DTS_INT_MIRROR:
482 22b7f6f8 Thomas Thrainer
        if self.op.snode is None:
483 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("The networked disk templates need"
484 22b7f6f8 Thomas Thrainer
                                     " a mirror node", errors.ECODE_INVAL)
485 22b7f6f8 Thomas Thrainer
      elif self.op.snode:
486 22b7f6f8 Thomas Thrainer
        self.LogWarning("Secondary node will be ignored on non-mirrored disk"
487 22b7f6f8 Thomas Thrainer
                        " template")
488 22b7f6f8 Thomas Thrainer
        self.op.snode = None
489 22b7f6f8 Thomas Thrainer
490 22b7f6f8 Thomas Thrainer
    _CheckOpportunisticLocking(self.op)
491 22b7f6f8 Thomas Thrainer
492 22b7f6f8 Thomas Thrainer
    if self.op.mode == constants.INSTANCE_IMPORT:
493 22b7f6f8 Thomas Thrainer
      # On import force_variant must be True, because if we forced it at
494 22b7f6f8 Thomas Thrainer
      # initial install, our only chance when importing it back is that it
495 22b7f6f8 Thomas Thrainer
      # works again!
496 22b7f6f8 Thomas Thrainer
      self.op.force_variant = True
497 22b7f6f8 Thomas Thrainer
498 22b7f6f8 Thomas Thrainer
      if self.op.no_install:
499 22b7f6f8 Thomas Thrainer
        self.LogInfo("No-installation mode has no effect during import")
500 22b7f6f8 Thomas Thrainer
501 22b7f6f8 Thomas Thrainer
    elif self.op.mode == constants.INSTANCE_CREATE:
502 22b7f6f8 Thomas Thrainer
      if self.op.os_type is None:
503 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("No guest OS specified",
504 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
505 22b7f6f8 Thomas Thrainer
      if self.op.os_type in self.cfg.GetClusterInfo().blacklisted_os:
506 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Guest OS '%s' is not allowed for"
507 22b7f6f8 Thomas Thrainer
                                   " installation" % self.op.os_type,
508 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_STATE)
509 22b7f6f8 Thomas Thrainer
    elif self.op.mode == constants.INSTANCE_REMOTE_IMPORT:
510 66222813 Thomas Thrainer
      self._cds = GetClusterDomainSecret()
511 66222813 Thomas Thrainer
512 22b7f6f8 Thomas Thrainer
      # Check handshake to ensure both clusters have the same domain secret
513 22b7f6f8 Thomas Thrainer
      src_handshake = self.op.source_handshake
514 22b7f6f8 Thomas Thrainer
      if not src_handshake:
515 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Missing source handshake",
516 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
517 22b7f6f8 Thomas Thrainer
518 22b7f6f8 Thomas Thrainer
      errmsg = masterd.instance.CheckRemoteExportHandshake(self._cds,
519 22b7f6f8 Thomas Thrainer
                                                           src_handshake)
520 22b7f6f8 Thomas Thrainer
      if errmsg:
521 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Invalid handshake: %s" % errmsg,
522 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
523 22b7f6f8 Thomas Thrainer
524 22b7f6f8 Thomas Thrainer
      # Load and check source CA
525 22b7f6f8 Thomas Thrainer
      self.source_x509_ca_pem = self.op.source_x509_ca
526 22b7f6f8 Thomas Thrainer
      if not self.source_x509_ca_pem:
527 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Missing source X509 CA",
528 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
529 22b7f6f8 Thomas Thrainer
530 22b7f6f8 Thomas Thrainer
      try:
531 22b7f6f8 Thomas Thrainer
        (cert, _) = utils.LoadSignedX509Certificate(self.source_x509_ca_pem,
532 22b7f6f8 Thomas Thrainer
                                                    self._cds)
533 22b7f6f8 Thomas Thrainer
      except OpenSSL.crypto.Error, err:
534 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Unable to load source X509 CA (%s)" %
535 22b7f6f8 Thomas Thrainer
                                   (err, ), errors.ECODE_INVAL)
536 22b7f6f8 Thomas Thrainer
537 22b7f6f8 Thomas Thrainer
      (errcode, msg) = utils.VerifyX509Certificate(cert, None, None)
538 22b7f6f8 Thomas Thrainer
      if errcode is not None:
539 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Invalid source X509 CA (%s)" % (msg, ),
540 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
541 22b7f6f8 Thomas Thrainer
542 22b7f6f8 Thomas Thrainer
      self.source_x509_ca = cert
543 22b7f6f8 Thomas Thrainer
544 22b7f6f8 Thomas Thrainer
      src_instance_name = self.op.source_instance_name
545 22b7f6f8 Thomas Thrainer
      if not src_instance_name:
546 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Missing source instance name",
547 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
548 22b7f6f8 Thomas Thrainer
549 22b7f6f8 Thomas Thrainer
      self.source_instance_name = \
550 22b7f6f8 Thomas Thrainer
        netutils.GetHostname(name=src_instance_name).name
551 22b7f6f8 Thomas Thrainer
552 22b7f6f8 Thomas Thrainer
    else:
553 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Invalid instance creation mode %r" %
554 22b7f6f8 Thomas Thrainer
                                 self.op.mode, errors.ECODE_INVAL)
555 22b7f6f8 Thomas Thrainer
556 22b7f6f8 Thomas Thrainer
  def ExpandNames(self):
557 22b7f6f8 Thomas Thrainer
    """ExpandNames for CreateInstance.
558 22b7f6f8 Thomas Thrainer

559 22b7f6f8 Thomas Thrainer
    Figure out the right locks for instance creation.
560 22b7f6f8 Thomas Thrainer

561 22b7f6f8 Thomas Thrainer
    """
562 22b7f6f8 Thomas Thrainer
    self.needed_locks = {}
563 22b7f6f8 Thomas Thrainer
564 22b7f6f8 Thomas Thrainer
    # this is just a preventive check, but someone might still add this
565 22b7f6f8 Thomas Thrainer
    # instance in the meantime, and creation will fail at lock-add time
566 da4a52a3 Thomas Thrainer
    if self.op.instance_name in\
567 da4a52a3 Thomas Thrainer
      [inst.name for inst in self.cfg.GetAllInstancesInfo().values()]:
568 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
569 d0d7d7cf Thomas Thrainer
                                 self.op.instance_name, errors.ECODE_EXISTS)
570 22b7f6f8 Thomas Thrainer
571 d0d7d7cf Thomas Thrainer
    self.add_locks[locking.LEVEL_INSTANCE] = self.op.instance_name
572 22b7f6f8 Thomas Thrainer
573 22b7f6f8 Thomas Thrainer
    if self.op.iallocator:
574 22b7f6f8 Thomas Thrainer
      # TODO: Find a solution to not lock all nodes in the cluster, e.g. by
575 22b7f6f8 Thomas Thrainer
      # specifying a group on instance creation and then selecting nodes from
576 22b7f6f8 Thomas Thrainer
      # that group
577 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
578 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_ALLOC] = locking.ALL_SET
579 22b7f6f8 Thomas Thrainer
580 22b7f6f8 Thomas Thrainer
      if self.op.opportunistic_locking:
581 22b7f6f8 Thomas Thrainer
        self.opportunistic_locks[locking.LEVEL_NODE] = True
582 22b7f6f8 Thomas Thrainer
    else:
583 1c3231aa Thomas Thrainer
      (self.op.pnode_uuid, self.op.pnode) = \
584 1c3231aa Thomas Thrainer
        ExpandNodeUuidAndName(self.cfg, self.op.pnode_uuid, self.op.pnode)
585 1c3231aa Thomas Thrainer
      nodelist = [self.op.pnode_uuid]
586 22b7f6f8 Thomas Thrainer
      if self.op.snode is not None:
587 1c3231aa Thomas Thrainer
        (self.op.snode_uuid, self.op.snode) = \
588 1c3231aa Thomas Thrainer
          ExpandNodeUuidAndName(self.cfg, self.op.snode_uuid, self.op.snode)
589 1c3231aa Thomas Thrainer
        nodelist.append(self.op.snode_uuid)
590 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE] = nodelist
591 22b7f6f8 Thomas Thrainer
592 22b7f6f8 Thomas Thrainer
    # in case of import lock the source node too
593 22b7f6f8 Thomas Thrainer
    if self.op.mode == constants.INSTANCE_IMPORT:
594 22b7f6f8 Thomas Thrainer
      src_node = self.op.src_node
595 22b7f6f8 Thomas Thrainer
      src_path = self.op.src_path
596 22b7f6f8 Thomas Thrainer
597 22b7f6f8 Thomas Thrainer
      if src_path is None:
598 22b7f6f8 Thomas Thrainer
        self.op.src_path = src_path = self.op.instance_name
599 22b7f6f8 Thomas Thrainer
600 22b7f6f8 Thomas Thrainer
      if src_node is None:
601 22b7f6f8 Thomas Thrainer
        self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
602 22b7f6f8 Thomas Thrainer
        self.needed_locks[locking.LEVEL_NODE_ALLOC] = locking.ALL_SET
603 22b7f6f8 Thomas Thrainer
        self.op.src_node = None
604 22b7f6f8 Thomas Thrainer
        if os.path.isabs(src_path):
605 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("Importing an instance from a path"
606 22b7f6f8 Thomas Thrainer
                                     " requires a source node option",
607 22b7f6f8 Thomas Thrainer
                                     errors.ECODE_INVAL)
608 22b7f6f8 Thomas Thrainer
      else:
609 1c3231aa Thomas Thrainer
        (self.op.src_node_uuid, self.op.src_node) = (_, src_node) = \
610 1c3231aa Thomas Thrainer
          ExpandNodeUuidAndName(self.cfg, self.op.src_node_uuid, src_node)
611 22b7f6f8 Thomas Thrainer
        if self.needed_locks[locking.LEVEL_NODE] is not locking.ALL_SET:
612 1c3231aa Thomas Thrainer
          self.needed_locks[locking.LEVEL_NODE].append(self.op.src_node_uuid)
613 22b7f6f8 Thomas Thrainer
        if not os.path.isabs(src_path):
614 a61a0813 Thomas Thrainer
          self.op.src_path = \
615 22b7f6f8 Thomas Thrainer
            utils.PathJoin(pathutils.EXPORT_DIR, src_path)
616 22b7f6f8 Thomas Thrainer
617 22b7f6f8 Thomas Thrainer
    self.needed_locks[locking.LEVEL_NODE_RES] = \
618 5eacbcae Thomas Thrainer
      CopyLockList(self.needed_locks[locking.LEVEL_NODE])
619 22b7f6f8 Thomas Thrainer
620 8b9887c5 Petr Pudlak
    # Optimistically acquire shared group locks (we're reading the
621 8b9887c5 Petr Pudlak
    # configuration).  We can't just call GetInstanceNodeGroups, because the
622 8b9887c5 Petr Pudlak
    # instance doesn't exist yet. Therefore we lock all node groups of all
623 8b9887c5 Petr Pudlak
    # nodes we have.
624 8b9887c5 Petr Pudlak
    if self.needed_locks[locking.LEVEL_NODE] == locking.ALL_SET:
625 8b9887c5 Petr Pudlak
      # In the case we lock all nodes for opportunistic allocation, we have no
626 8b9887c5 Petr Pudlak
      # choice than to lock all groups, because they're allocated before nodes.
627 8b9887c5 Petr Pudlak
      # This is sad, but true. At least we release all those we don't need in
628 8b9887c5 Petr Pudlak
      # CheckPrereq later.
629 8b9887c5 Petr Pudlak
      self.needed_locks[locking.LEVEL_NODEGROUP] = locking.ALL_SET
630 8b9887c5 Petr Pudlak
    else:
631 8b9887c5 Petr Pudlak
      self.needed_locks[locking.LEVEL_NODEGROUP] = \
632 8b9887c5 Petr Pudlak
        list(self.cfg.GetNodeGroupsFromNodes(
633 8b9887c5 Petr Pudlak
          self.needed_locks[locking.LEVEL_NODE]))
634 8b9887c5 Petr Pudlak
    self.share_locks[locking.LEVEL_NODEGROUP] = 1
635 8b9887c5 Petr Pudlak
636 4289f617 Thomas Thrainer
  def DeclareLocks(self, level):
637 4289f617 Thomas Thrainer
    if level == locking.LEVEL_NODE_RES and \
638 4289f617 Thomas Thrainer
      self.opportunistic_locks[locking.LEVEL_NODE]:
639 4289f617 Thomas Thrainer
      # Even when using opportunistic locking, we require the same set of
640 4289f617 Thomas Thrainer
      # NODE_RES locks as we got NODE locks
641 4289f617 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_RES] = \
642 4289f617 Thomas Thrainer
        self.owned_locks(locking.LEVEL_NODE)
643 4289f617 Thomas Thrainer
644 22b7f6f8 Thomas Thrainer
  def _RunAllocator(self):
645 22b7f6f8 Thomas Thrainer
    """Run the allocator based on input opcode.
646 22b7f6f8 Thomas Thrainer

647 22b7f6f8 Thomas Thrainer
    """
648 22b7f6f8 Thomas Thrainer
    if self.op.opportunistic_locking:
649 22b7f6f8 Thomas Thrainer
      # Only consider nodes for which a lock is held
650 4665b94f Thomas Thrainer
      node_name_whitelist = self.cfg.GetNodeNames(
651 4665b94f Thomas Thrainer
        self.owned_locks(locking.LEVEL_NODE))
652 22b7f6f8 Thomas Thrainer
    else:
653 4665b94f Thomas Thrainer
      node_name_whitelist = None
654 22b7f6f8 Thomas Thrainer
655 22b7f6f8 Thomas Thrainer
    req = _CreateInstanceAllocRequest(self.op, self.disks,
656 22b7f6f8 Thomas Thrainer
                                      self.nics, self.be_full,
657 4665b94f Thomas Thrainer
                                      node_name_whitelist)
658 22b7f6f8 Thomas Thrainer
    ial = iallocator.IAllocator(self.cfg, self.rpc, req)
659 22b7f6f8 Thomas Thrainer
660 22b7f6f8 Thomas Thrainer
    ial.Run(self.op.iallocator)
661 22b7f6f8 Thomas Thrainer
662 22b7f6f8 Thomas Thrainer
    if not ial.success:
663 22b7f6f8 Thomas Thrainer
      # When opportunistic locks are used only a temporary failure is generated
664 22b7f6f8 Thomas Thrainer
      if self.op.opportunistic_locking:
665 22b7f6f8 Thomas Thrainer
        ecode = errors.ECODE_TEMP_NORES
666 22b7f6f8 Thomas Thrainer
      else:
667 22b7f6f8 Thomas Thrainer
        ecode = errors.ECODE_NORES
668 22b7f6f8 Thomas Thrainer
669 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Can't compute nodes using"
670 22b7f6f8 Thomas Thrainer
                                 " iallocator '%s': %s" %
671 22b7f6f8 Thomas Thrainer
                                 (self.op.iallocator, ial.info),
672 22b7f6f8 Thomas Thrainer
                                 ecode)
673 22b7f6f8 Thomas Thrainer
674 1c3231aa Thomas Thrainer
    (self.op.pnode_uuid, self.op.pnode) = \
675 1c3231aa Thomas Thrainer
      ExpandNodeUuidAndName(self.cfg, None, ial.result[0])
676 22b7f6f8 Thomas Thrainer
    self.LogInfo("Selected nodes for instance %s via iallocator %s: %s",
677 22b7f6f8 Thomas Thrainer
                 self.op.instance_name, self.op.iallocator,
678 22b7f6f8 Thomas Thrainer
                 utils.CommaJoin(ial.result))
679 22b7f6f8 Thomas Thrainer
680 22b7f6f8 Thomas Thrainer
    assert req.RequiredNodes() in (1, 2), "Wrong node count from iallocator"
681 22b7f6f8 Thomas Thrainer
682 22b7f6f8 Thomas Thrainer
    if req.RequiredNodes() == 2:
683 1c3231aa Thomas Thrainer
      (self.op.snode_uuid, self.op.snode) = \
684 1c3231aa Thomas Thrainer
        ExpandNodeUuidAndName(self.cfg, None, ial.result[1])
685 22b7f6f8 Thomas Thrainer
686 22b7f6f8 Thomas Thrainer
  def BuildHooksEnv(self):
687 22b7f6f8 Thomas Thrainer
    """Build hooks env.
688 22b7f6f8 Thomas Thrainer

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

691 22b7f6f8 Thomas Thrainer
    """
692 22b7f6f8 Thomas Thrainer
    env = {
693 22b7f6f8 Thomas Thrainer
      "ADD_MODE": self.op.mode,
694 22b7f6f8 Thomas Thrainer
      }
695 22b7f6f8 Thomas Thrainer
    if self.op.mode == constants.INSTANCE_IMPORT:
696 22b7f6f8 Thomas Thrainer
      env["SRC_NODE"] = self.op.src_node
697 22b7f6f8 Thomas Thrainer
      env["SRC_PATH"] = self.op.src_path
698 22b7f6f8 Thomas Thrainer
      env["SRC_IMAGES"] = self.src_images
699 22b7f6f8 Thomas Thrainer
700 5eacbcae Thomas Thrainer
    env.update(BuildInstanceHookEnv(
701 22b7f6f8 Thomas Thrainer
      name=self.op.instance_name,
702 1c3231aa Thomas Thrainer
      primary_node_name=self.op.pnode,
703 1c3231aa Thomas Thrainer
      secondary_node_names=self.cfg.GetNodeNames(self.secondaries),
704 22b7f6f8 Thomas Thrainer
      status=self.op.start,
705 22b7f6f8 Thomas Thrainer
      os_type=self.op.os_type,
706 22b7f6f8 Thomas Thrainer
      minmem=self.be_full[constants.BE_MINMEM],
707 22b7f6f8 Thomas Thrainer
      maxmem=self.be_full[constants.BE_MAXMEM],
708 22b7f6f8 Thomas Thrainer
      vcpus=self.be_full[constants.BE_VCPUS],
709 5eacbcae Thomas Thrainer
      nics=NICListToTuple(self, self.nics),
710 22b7f6f8 Thomas Thrainer
      disk_template=self.op.disk_template,
711 8a348b15 Christos Stavrakakis
      disks=[(d[constants.IDISK_NAME], d.get("uuid", ""),
712 8a348b15 Christos Stavrakakis
              d[constants.IDISK_SIZE], d[constants.IDISK_MODE])
713 8a348b15 Christos Stavrakakis
             for d in self.disks],
714 22b7f6f8 Thomas Thrainer
      bep=self.be_full,
715 22b7f6f8 Thomas Thrainer
      hvp=self.hv_full,
716 22b7f6f8 Thomas Thrainer
      hypervisor_name=self.op.hypervisor,
717 22b7f6f8 Thomas Thrainer
      tags=self.op.tags,
718 22b7f6f8 Thomas Thrainer
      ))
719 22b7f6f8 Thomas Thrainer
720 22b7f6f8 Thomas Thrainer
    return env
721 22b7f6f8 Thomas Thrainer
722 22b7f6f8 Thomas Thrainer
  def BuildHooksNodes(self):
723 22b7f6f8 Thomas Thrainer
    """Build hooks nodes.
724 22b7f6f8 Thomas Thrainer

725 22b7f6f8 Thomas Thrainer
    """
726 1c3231aa Thomas Thrainer
    nl = [self.cfg.GetMasterNode(), self.op.pnode_uuid] + self.secondaries
727 22b7f6f8 Thomas Thrainer
    return nl, nl
728 22b7f6f8 Thomas Thrainer
729 22b7f6f8 Thomas Thrainer
  def _ReadExportInfo(self):
730 22b7f6f8 Thomas Thrainer
    """Reads the export information from disk.
731 22b7f6f8 Thomas Thrainer

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

735 22b7f6f8 Thomas Thrainer
    @return: the export information
736 22b7f6f8 Thomas Thrainer

737 22b7f6f8 Thomas Thrainer
    """
738 22b7f6f8 Thomas Thrainer
    assert self.op.mode == constants.INSTANCE_IMPORT
739 22b7f6f8 Thomas Thrainer
740 d0d7d7cf Thomas Thrainer
    if self.op.src_node_uuid is None:
741 22b7f6f8 Thomas Thrainer
      locked_nodes = self.owned_locks(locking.LEVEL_NODE)
742 22b7f6f8 Thomas Thrainer
      exp_list = self.rpc.call_export_list(locked_nodes)
743 22b7f6f8 Thomas Thrainer
      found = False
744 a61a0813 Thomas Thrainer
      for node_uuid in exp_list:
745 a61a0813 Thomas Thrainer
        if exp_list[node_uuid].fail_msg:
746 22b7f6f8 Thomas Thrainer
          continue
747 a61a0813 Thomas Thrainer
        if self.op.src_path in exp_list[node_uuid].payload:
748 22b7f6f8 Thomas Thrainer
          found = True
749 a61a0813 Thomas Thrainer
          self.op.src_node = self.cfg.GetNodeInfo(node_uuid).name
750 a61a0813 Thomas Thrainer
          self.op.src_node_uuid = node_uuid
751 d0d7d7cf Thomas Thrainer
          self.op.src_path = utils.PathJoin(pathutils.EXPORT_DIR,
752 d0d7d7cf Thomas Thrainer
                                            self.op.src_path)
753 22b7f6f8 Thomas Thrainer
          break
754 22b7f6f8 Thomas Thrainer
      if not found:
755 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("No export found for relative path %s" %
756 d0d7d7cf Thomas Thrainer
                                   self.op.src_path, errors.ECODE_INVAL)
757 22b7f6f8 Thomas Thrainer
758 d0d7d7cf Thomas Thrainer
    CheckNodeOnline(self, self.op.src_node_uuid)
759 d0d7d7cf Thomas Thrainer
    result = self.rpc.call_export_info(self.op.src_node_uuid, self.op.src_path)
760 d0d7d7cf Thomas Thrainer
    result.Raise("No export or invalid export found in dir %s" %
761 d0d7d7cf Thomas Thrainer
                 self.op.src_path)
762 22b7f6f8 Thomas Thrainer
763 22b7f6f8 Thomas Thrainer
    export_info = objects.SerializableConfigParser.Loads(str(result.payload))
764 22b7f6f8 Thomas Thrainer
    if not export_info.has_section(constants.INISECT_EXP):
765 22b7f6f8 Thomas Thrainer
      raise errors.ProgrammerError("Corrupted export config",
766 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_ENVIRON)
767 22b7f6f8 Thomas Thrainer
768 22b7f6f8 Thomas Thrainer
    ei_version = export_info.get(constants.INISECT_EXP, "version")
769 1c3231aa Thomas Thrainer
    if int(ei_version) != constants.EXPORT_VERSION:
770 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Wrong export version %s (wanted %d)" %
771 22b7f6f8 Thomas Thrainer
                                 (ei_version, constants.EXPORT_VERSION),
772 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_ENVIRON)
773 22b7f6f8 Thomas Thrainer
    return export_info
774 22b7f6f8 Thomas Thrainer
775 22b7f6f8 Thomas Thrainer
  def _ReadExportParams(self, einfo):
776 22b7f6f8 Thomas Thrainer
    """Use export parameters as defaults.
777 22b7f6f8 Thomas Thrainer

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

782 22b7f6f8 Thomas Thrainer
    """
783 22b7f6f8 Thomas Thrainer
    self.op.os_type = einfo.get(constants.INISECT_EXP, "os")
784 22b7f6f8 Thomas Thrainer
785 22b7f6f8 Thomas Thrainer
    if not self.op.disks:
786 22b7f6f8 Thomas Thrainer
      disks = []
787 22b7f6f8 Thomas Thrainer
      # TODO: import the disk iv_name too
788 22b7f6f8 Thomas Thrainer
      for idx in range(constants.MAX_DISKS):
789 22b7f6f8 Thomas Thrainer
        if einfo.has_option(constants.INISECT_INS, "disk%d_size" % idx):
790 22b7f6f8 Thomas Thrainer
          disk_sz = einfo.getint(constants.INISECT_INS, "disk%d_size" % idx)
791 22b7f6f8 Thomas Thrainer
          disks.append({constants.IDISK_SIZE: disk_sz})
792 22b7f6f8 Thomas Thrainer
      self.op.disks = disks
793 22b7f6f8 Thomas Thrainer
      if not disks and self.op.disk_template != constants.DT_DISKLESS:
794 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("No disk info specified and the export"
795 22b7f6f8 Thomas Thrainer
                                   " is missing the disk information",
796 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
797 22b7f6f8 Thomas Thrainer
798 22b7f6f8 Thomas Thrainer
    if not self.op.nics:
799 22b7f6f8 Thomas Thrainer
      nics = []
800 22b7f6f8 Thomas Thrainer
      for idx in range(constants.MAX_NICS):
801 22b7f6f8 Thomas Thrainer
        if einfo.has_option(constants.INISECT_INS, "nic%d_mac" % idx):
802 22b7f6f8 Thomas Thrainer
          ndict = {}
803 22b7f6f8 Thomas Thrainer
          for name in list(constants.NICS_PARAMETERS) + ["ip", "mac"]:
804 a61a0813 Thomas Thrainer
            nic_param_name = "nic%d_%s" % (idx, name)
805 a61a0813 Thomas Thrainer
            if einfo.has_option(constants.INISECT_INS, nic_param_name):
806 a61a0813 Thomas Thrainer
              v = einfo.get(constants.INISECT_INS, nic_param_name)
807 a61a0813 Thomas Thrainer
              ndict[name] = v
808 22b7f6f8 Thomas Thrainer
          nics.append(ndict)
809 22b7f6f8 Thomas Thrainer
        else:
810 22b7f6f8 Thomas Thrainer
          break
811 22b7f6f8 Thomas Thrainer
      self.op.nics = nics
812 22b7f6f8 Thomas Thrainer
813 22b7f6f8 Thomas Thrainer
    if not self.op.tags and einfo.has_option(constants.INISECT_INS, "tags"):
814 22b7f6f8 Thomas Thrainer
      self.op.tags = einfo.get(constants.INISECT_INS, "tags").split()
815 22b7f6f8 Thomas Thrainer
816 22b7f6f8 Thomas Thrainer
    if (self.op.hypervisor is None and
817 22b7f6f8 Thomas Thrainer
        einfo.has_option(constants.INISECT_INS, "hypervisor")):
818 22b7f6f8 Thomas Thrainer
      self.op.hypervisor = einfo.get(constants.INISECT_INS, "hypervisor")
819 22b7f6f8 Thomas Thrainer
820 22b7f6f8 Thomas Thrainer
    if einfo.has_section(constants.INISECT_HYP):
821 22b7f6f8 Thomas Thrainer
      # use the export parameters but do not override the ones
822 22b7f6f8 Thomas Thrainer
      # specified by the user
823 22b7f6f8 Thomas Thrainer
      for name, value in einfo.items(constants.INISECT_HYP):
824 22b7f6f8 Thomas Thrainer
        if name not in self.op.hvparams:
825 22b7f6f8 Thomas Thrainer
          self.op.hvparams[name] = value
826 22b7f6f8 Thomas Thrainer
827 22b7f6f8 Thomas Thrainer
    if einfo.has_section(constants.INISECT_BEP):
828 22b7f6f8 Thomas Thrainer
      # use the parameters, without overriding
829 22b7f6f8 Thomas Thrainer
      for name, value in einfo.items(constants.INISECT_BEP):
830 22b7f6f8 Thomas Thrainer
        if name not in self.op.beparams:
831 22b7f6f8 Thomas Thrainer
          self.op.beparams[name] = value
832 22b7f6f8 Thomas Thrainer
        # Compatibility for the old "memory" be param
833 22b7f6f8 Thomas Thrainer
        if name == constants.BE_MEMORY:
834 22b7f6f8 Thomas Thrainer
          if constants.BE_MAXMEM not in self.op.beparams:
835 22b7f6f8 Thomas Thrainer
            self.op.beparams[constants.BE_MAXMEM] = value
836 22b7f6f8 Thomas Thrainer
          if constants.BE_MINMEM not in self.op.beparams:
837 22b7f6f8 Thomas Thrainer
            self.op.beparams[constants.BE_MINMEM] = value
838 22b7f6f8 Thomas Thrainer
    else:
839 22b7f6f8 Thomas Thrainer
      # try to read the parameters old style, from the main section
840 22b7f6f8 Thomas Thrainer
      for name in constants.BES_PARAMETERS:
841 22b7f6f8 Thomas Thrainer
        if (name not in self.op.beparams and
842 22b7f6f8 Thomas Thrainer
            einfo.has_option(constants.INISECT_INS, name)):
843 22b7f6f8 Thomas Thrainer
          self.op.beparams[name] = einfo.get(constants.INISECT_INS, name)
844 22b7f6f8 Thomas Thrainer
845 22b7f6f8 Thomas Thrainer
    if einfo.has_section(constants.INISECT_OSP):
846 22b7f6f8 Thomas Thrainer
      # use the parameters, without overriding
847 22b7f6f8 Thomas Thrainer
      for name, value in einfo.items(constants.INISECT_OSP):
848 22b7f6f8 Thomas Thrainer
        if name not in self.op.osparams:
849 22b7f6f8 Thomas Thrainer
          self.op.osparams[name] = value
850 22b7f6f8 Thomas Thrainer
851 6bce7ba2 Santi Raffa
    if einfo.has_section(constants.INISECT_OSP_PRIVATE):
852 6bce7ba2 Santi Raffa
      # use the parameters, without overriding
853 6bce7ba2 Santi Raffa
      for name, value in einfo.items(constants.INISECT_OSP_PRIVATE):
854 6bce7ba2 Santi Raffa
        if name not in self.op.osparams_private:
855 6bce7ba2 Santi Raffa
          self.op.osparams_private[name] = serializer.Private(value, descr=name)
856 6bce7ba2 Santi Raffa
857 22b7f6f8 Thomas Thrainer
  def _RevertToDefaults(self, cluster):
858 22b7f6f8 Thomas Thrainer
    """Revert the instance parameters to the default values.
859 22b7f6f8 Thomas Thrainer

860 22b7f6f8 Thomas Thrainer
    """
861 22b7f6f8 Thomas Thrainer
    # hvparams
862 22b7f6f8 Thomas Thrainer
    hv_defs = cluster.SimpleFillHV(self.op.hypervisor, self.op.os_type, {})
863 22b7f6f8 Thomas Thrainer
    for name in self.op.hvparams.keys():
864 22b7f6f8 Thomas Thrainer
      if name in hv_defs and hv_defs[name] == self.op.hvparams[name]:
865 22b7f6f8 Thomas Thrainer
        del self.op.hvparams[name]
866 22b7f6f8 Thomas Thrainer
    # beparams
867 22b7f6f8 Thomas Thrainer
    be_defs = cluster.SimpleFillBE({})
868 22b7f6f8 Thomas Thrainer
    for name in self.op.beparams.keys():
869 22b7f6f8 Thomas Thrainer
      if name in be_defs and be_defs[name] == self.op.beparams[name]:
870 22b7f6f8 Thomas Thrainer
        del self.op.beparams[name]
871 22b7f6f8 Thomas Thrainer
    # nic params
872 22b7f6f8 Thomas Thrainer
    nic_defs = cluster.SimpleFillNIC({})
873 22b7f6f8 Thomas Thrainer
    for nic in self.op.nics:
874 22b7f6f8 Thomas Thrainer
      for name in constants.NICS_PARAMETERS:
875 22b7f6f8 Thomas Thrainer
        if name in nic and name in nic_defs and nic[name] == nic_defs[name]:
876 22b7f6f8 Thomas Thrainer
          del nic[name]
877 22b7f6f8 Thomas Thrainer
    # osparams
878 22b7f6f8 Thomas Thrainer
    os_defs = cluster.SimpleFillOS(self.op.os_type, {})
879 22b7f6f8 Thomas Thrainer
    for name in self.op.osparams.keys():
880 22b7f6f8 Thomas Thrainer
      if name in os_defs and os_defs[name] == self.op.osparams[name]:
881 22b7f6f8 Thomas Thrainer
        del self.op.osparams[name]
882 22b7f6f8 Thomas Thrainer
883 6bce7ba2 Santi Raffa
    os_defs_ = cluster.SimpleFillOS(self.op.os_type, {},
884 6bce7ba2 Santi Raffa
                                    os_params_private={})
885 6bce7ba2 Santi Raffa
    for name in self.op.osparams_private.keys():
886 6bce7ba2 Santi Raffa
      if name in os_defs_ and os_defs_[name] == self.op.osparams_private[name]:
887 6bce7ba2 Santi Raffa
        del self.op.osparams_private[name]
888 6bce7ba2 Santi Raffa
889 22b7f6f8 Thomas Thrainer
  def _CalculateFileStorageDir(self):
890 22b7f6f8 Thomas Thrainer
    """Calculate final instance file storage dir.
891 22b7f6f8 Thomas Thrainer

892 22b7f6f8 Thomas Thrainer
    """
893 22b7f6f8 Thomas Thrainer
    # file storage dir calculation/check
894 22b7f6f8 Thomas Thrainer
    self.instance_file_storage_dir = None
895 22b7f6f8 Thomas Thrainer
    if self.op.disk_template in constants.DTS_FILEBASED:
896 22b7f6f8 Thomas Thrainer
      # build the full file storage dir path
897 22b7f6f8 Thomas Thrainer
      joinargs = []
898 22b7f6f8 Thomas Thrainer
899 ac156ecd Santi Raffa
      cfg_storage = None
900 ac156ecd Santi Raffa
      if self.op.disk_template == constants.DT_FILE:
901 ac156ecd Santi Raffa
        cfg_storage = self.cfg.GetFileStorageDir()
902 ac156ecd Santi Raffa
      elif self.op.disk_template == constants.DT_SHARED_FILE:
903 ac156ecd Santi Raffa
        cfg_storage = self.cfg.GetSharedFileStorageDir()
904 ac156ecd Santi Raffa
      elif self.op.disk_template == constants.DT_GLUSTER:
905 ac156ecd Santi Raffa
        cfg_storage = self.cfg.GetGlusterStorageDir()
906 ac156ecd Santi Raffa
907 ac156ecd Santi Raffa
      if not cfg_storage:
908 ac156ecd Santi Raffa
        raise errors.OpPrereqError(
909 ac156ecd Santi Raffa
          "Cluster file storage dir for {tpl} storage type not defined".format(
910 ac156ecd Santi Raffa
            tpl=repr(self.op.disk_template)
911 ac156ecd Santi Raffa
          ),
912 ac156ecd Santi Raffa
          errors.ECODE_STATE
913 ac156ecd Santi Raffa
      )
914 ac156ecd Santi Raffa
915 ac156ecd Santi Raffa
      joinargs.append(cfg_storage)
916 22b7f6f8 Thomas Thrainer
917 22b7f6f8 Thomas Thrainer
      if self.op.file_storage_dir is not None:
918 22b7f6f8 Thomas Thrainer
        joinargs.append(self.op.file_storage_dir)
919 22b7f6f8 Thomas Thrainer
920 ac156ecd Santi Raffa
      if self.op.disk_template != constants.DT_GLUSTER:
921 ac156ecd Santi Raffa
        joinargs.append(self.op.instance_name)
922 22b7f6f8 Thomas Thrainer
923 ac156ecd Santi Raffa
      if len(joinargs) > 1:
924 ac156ecd Santi Raffa
        # pylint: disable=W0142
925 ac156ecd Santi Raffa
        self.instance_file_storage_dir = utils.PathJoin(*joinargs)
926 ac156ecd Santi Raffa
      else:
927 ac156ecd Santi Raffa
        self.instance_file_storage_dir = joinargs[0]
928 22b7f6f8 Thomas Thrainer
929 22b7f6f8 Thomas Thrainer
  def CheckPrereq(self): # pylint: disable=R0914
930 22b7f6f8 Thomas Thrainer
    """Check prerequisites.
931 22b7f6f8 Thomas Thrainer

932 22b7f6f8 Thomas Thrainer
    """
933 8b9887c5 Petr Pudlak
    # Check that the optimistically acquired groups are correct wrt the
934 8b9887c5 Petr Pudlak
    # acquired nodes
935 8b9887c5 Petr Pudlak
    owned_groups = frozenset(self.owned_locks(locking.LEVEL_NODEGROUP))
936 8b9887c5 Petr Pudlak
    owned_nodes = frozenset(self.owned_locks(locking.LEVEL_NODE))
937 8b9887c5 Petr Pudlak
    cur_groups = list(self.cfg.GetNodeGroupsFromNodes(owned_nodes))
938 8b9887c5 Petr Pudlak
    if not owned_groups.issuperset(cur_groups):
939 8b9887c5 Petr Pudlak
      raise errors.OpPrereqError("New instance %s's node groups changed since"
940 8b9887c5 Petr Pudlak
                                 " locks were acquired, current groups are"
941 8b9887c5 Petr Pudlak
                                 " are '%s', owning groups '%s'; retry the"
942 8b9887c5 Petr Pudlak
                                 " operation" %
943 8b9887c5 Petr Pudlak
                                 (self.op.instance_name,
944 8b9887c5 Petr Pudlak
                                  utils.CommaJoin(cur_groups),
945 8b9887c5 Petr Pudlak
                                  utils.CommaJoin(owned_groups)),
946 8b9887c5 Petr Pudlak
                                 errors.ECODE_STATE)
947 8b9887c5 Petr Pudlak
948 22b7f6f8 Thomas Thrainer
    self._CalculateFileStorageDir()
949 22b7f6f8 Thomas Thrainer
950 22b7f6f8 Thomas Thrainer
    if self.op.mode == constants.INSTANCE_IMPORT:
951 22b7f6f8 Thomas Thrainer
      export_info = self._ReadExportInfo()
952 22b7f6f8 Thomas Thrainer
      self._ReadExportParams(export_info)
953 22b7f6f8 Thomas Thrainer
      self._old_instance_name = export_info.get(constants.INISECT_INS, "name")
954 22b7f6f8 Thomas Thrainer
    else:
955 22b7f6f8 Thomas Thrainer
      self._old_instance_name = None
956 22b7f6f8 Thomas Thrainer
957 22b7f6f8 Thomas Thrainer
    if (not self.cfg.GetVGName() and
958 22b7f6f8 Thomas Thrainer
        self.op.disk_template not in constants.DTS_NOT_LVM):
959 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Cluster does not support lvm-based"
960 22b7f6f8 Thomas Thrainer
                                 " instances", errors.ECODE_STATE)
961 22b7f6f8 Thomas Thrainer
962 22b7f6f8 Thomas Thrainer
    if (self.op.hypervisor is None or
963 22b7f6f8 Thomas Thrainer
        self.op.hypervisor == constants.VALUE_AUTO):
964 22b7f6f8 Thomas Thrainer
      self.op.hypervisor = self.cfg.GetHypervisorType()
965 22b7f6f8 Thomas Thrainer
966 22b7f6f8 Thomas Thrainer
    cluster = self.cfg.GetClusterInfo()
967 22b7f6f8 Thomas Thrainer
    enabled_hvs = cluster.enabled_hypervisors
968 22b7f6f8 Thomas Thrainer
    if self.op.hypervisor not in enabled_hvs:
969 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Selected hypervisor (%s) not enabled in the"
970 22b7f6f8 Thomas Thrainer
                                 " cluster (%s)" %
971 22b7f6f8 Thomas Thrainer
                                 (self.op.hypervisor, ",".join(enabled_hvs)),
972 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_STATE)
973 22b7f6f8 Thomas Thrainer
974 22b7f6f8 Thomas Thrainer
    # Check tag validity
975 22b7f6f8 Thomas Thrainer
    for tag in self.op.tags:
976 22b7f6f8 Thomas Thrainer
      objects.TaggableObject.ValidateTag(tag)
977 22b7f6f8 Thomas Thrainer
978 22b7f6f8 Thomas Thrainer
    # check hypervisor parameter syntax (locally)
979 22b7f6f8 Thomas Thrainer
    utils.ForceDictType(self.op.hvparams, constants.HVS_PARAMETER_TYPES)
980 22b7f6f8 Thomas Thrainer
    filled_hvp = cluster.SimpleFillHV(self.op.hypervisor, self.op.os_type,
981 22b7f6f8 Thomas Thrainer
                                      self.op.hvparams)
982 22b7f6f8 Thomas Thrainer
    hv_type = hypervisor.GetHypervisorClass(self.op.hypervisor)
983 22b7f6f8 Thomas Thrainer
    hv_type.CheckParameterSyntax(filled_hvp)
984 22b7f6f8 Thomas Thrainer
    self.hv_full = filled_hvp
985 22b7f6f8 Thomas Thrainer
    # check that we don't specify global parameters on an instance
986 5eacbcae Thomas Thrainer
    CheckParamsNotGlobal(self.op.hvparams, constants.HVC_GLOBALS, "hypervisor",
987 5eacbcae Thomas Thrainer
                         "instance", "cluster")
988 22b7f6f8 Thomas Thrainer
989 22b7f6f8 Thomas Thrainer
    # fill and remember the beparams dict
990 22b7f6f8 Thomas Thrainer
    self.be_full = _ComputeFullBeParams(self.op, cluster)
991 22b7f6f8 Thomas Thrainer
992 22b7f6f8 Thomas Thrainer
    # build os parameters
993 6bce7ba2 Santi Raffa
    if self.op.osparams_private is None:
994 6bce7ba2 Santi Raffa
      self.op.osparams_private = serializer.PrivateDict()
995 6bce7ba2 Santi Raffa
    if self.op.osparams_secret is None:
996 6bce7ba2 Santi Raffa
      self.op.osparams_secret = serializer.PrivateDict()
997 6bce7ba2 Santi Raffa
998 6bce7ba2 Santi Raffa
    self.os_full = cluster.SimpleFillOS(
999 6bce7ba2 Santi Raffa
      self.op.os_type,
1000 6bce7ba2 Santi Raffa
      self.op.osparams,
1001 6bce7ba2 Santi Raffa
      os_params_private=self.op.osparams_private,
1002 6bce7ba2 Santi Raffa
      os_params_secret=self.op.osparams_secret
1003 6bce7ba2 Santi Raffa
    )
1004 22b7f6f8 Thomas Thrainer
1005 22b7f6f8 Thomas Thrainer
    # now that hvp/bep are in final format, let's reset to defaults,
1006 22b7f6f8 Thomas Thrainer
    # if told to do so
1007 22b7f6f8 Thomas Thrainer
    if self.op.identify_defaults:
1008 22b7f6f8 Thomas Thrainer
      self._RevertToDefaults(cluster)
1009 22b7f6f8 Thomas Thrainer
1010 22b7f6f8 Thomas Thrainer
    # NIC buildup
1011 22b7f6f8 Thomas Thrainer
    self.nics = _ComputeNics(self.op, cluster, self.check_ip, self.cfg,
1012 22b7f6f8 Thomas Thrainer
                             self.proc.GetECId())
1013 22b7f6f8 Thomas Thrainer
1014 22b7f6f8 Thomas Thrainer
    # disk checks/pre-build
1015 22b7f6f8 Thomas Thrainer
    default_vg = self.cfg.GetVGName()
1016 5eacbcae Thomas Thrainer
    self.disks = ComputeDisks(self.op, default_vg)
1017 22b7f6f8 Thomas Thrainer
1018 22b7f6f8 Thomas Thrainer
    if self.op.mode == constants.INSTANCE_IMPORT:
1019 22b7f6f8 Thomas Thrainer
      disk_images = []
1020 22b7f6f8 Thomas Thrainer
      for idx in range(len(self.disks)):
1021 22b7f6f8 Thomas Thrainer
        option = "disk%d_dump" % idx
1022 22b7f6f8 Thomas Thrainer
        if export_info.has_option(constants.INISECT_INS, option):
1023 22b7f6f8 Thomas Thrainer
          # FIXME: are the old os-es, disk sizes, etc. useful?
1024 22b7f6f8 Thomas Thrainer
          export_name = export_info.get(constants.INISECT_INS, option)
1025 22b7f6f8 Thomas Thrainer
          image = utils.PathJoin(self.op.src_path, export_name)
1026 22b7f6f8 Thomas Thrainer
          disk_images.append(image)
1027 22b7f6f8 Thomas Thrainer
        else:
1028 22b7f6f8 Thomas Thrainer
          disk_images.append(False)
1029 22b7f6f8 Thomas Thrainer
1030 22b7f6f8 Thomas Thrainer
      self.src_images = disk_images
1031 22b7f6f8 Thomas Thrainer
1032 22b7f6f8 Thomas Thrainer
      if self.op.instance_name == self._old_instance_name:
1033 22b7f6f8 Thomas Thrainer
        for idx, nic in enumerate(self.nics):
1034 22b7f6f8 Thomas Thrainer
          if nic.mac == constants.VALUE_AUTO:
1035 22b7f6f8 Thomas Thrainer
            nic_mac_ini = "nic%d_mac" % idx
1036 22b7f6f8 Thomas Thrainer
            nic.mac = export_info.get(constants.INISECT_INS, nic_mac_ini)
1037 22b7f6f8 Thomas Thrainer
1038 22b7f6f8 Thomas Thrainer
    # ENDIF: self.op.mode == constants.INSTANCE_IMPORT
1039 22b7f6f8 Thomas Thrainer
1040 22b7f6f8 Thomas Thrainer
    # ip ping checks (we use the same ip that was resolved in ExpandNames)
1041 22b7f6f8 Thomas Thrainer
    if self.op.ip_check:
1042 22b7f6f8 Thomas Thrainer
      if netutils.TcpPing(self.check_ip, constants.DEFAULT_NODED_PORT):
1043 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("IP %s of instance %s already in use" %
1044 22b7f6f8 Thomas Thrainer
                                   (self.check_ip, self.op.instance_name),
1045 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_NOTUNIQUE)
1046 22b7f6f8 Thomas Thrainer
1047 22b7f6f8 Thomas Thrainer
    #### mac address generation
1048 22b7f6f8 Thomas Thrainer
    # By generating here the mac address both the allocator and the hooks get
1049 22b7f6f8 Thomas Thrainer
    # the real final mac address rather than the 'auto' or 'generate' value.
1050 22b7f6f8 Thomas Thrainer
    # There is a race condition between the generation and the instance object
1051 22b7f6f8 Thomas Thrainer
    # creation, which means that we know the mac is valid now, but we're not
1052 22b7f6f8 Thomas Thrainer
    # sure it will be when we actually add the instance. If things go bad
1053 22b7f6f8 Thomas Thrainer
    # adding the instance will abort because of a duplicate mac, and the
1054 22b7f6f8 Thomas Thrainer
    # creation job will fail.
1055 22b7f6f8 Thomas Thrainer
    for nic in self.nics:
1056 22b7f6f8 Thomas Thrainer
      if nic.mac in (constants.VALUE_AUTO, constants.VALUE_GENERATE):
1057 22b7f6f8 Thomas Thrainer
        nic.mac = self.cfg.GenerateMAC(nic.network, self.proc.GetECId())
1058 22b7f6f8 Thomas Thrainer
1059 22b7f6f8 Thomas Thrainer
    #### allocator run
1060 22b7f6f8 Thomas Thrainer
1061 22b7f6f8 Thomas Thrainer
    if self.op.iallocator is not None:
1062 22b7f6f8 Thomas Thrainer
      self._RunAllocator()
1063 22b7f6f8 Thomas Thrainer
1064 22b7f6f8 Thomas Thrainer
    # Release all unneeded node locks
1065 1c3231aa Thomas Thrainer
    keep_locks = filter(None, [self.op.pnode_uuid, self.op.snode_uuid,
1066 1c3231aa Thomas Thrainer
                               self.op.src_node_uuid])
1067 5eacbcae Thomas Thrainer
    ReleaseLocks(self, locking.LEVEL_NODE, keep=keep_locks)
1068 5eacbcae Thomas Thrainer
    ReleaseLocks(self, locking.LEVEL_NODE_RES, keep=keep_locks)
1069 5eacbcae Thomas Thrainer
    ReleaseLocks(self, locking.LEVEL_NODE_ALLOC)
1070 8b9887c5 Petr Pudlak
    # Release all unneeded group locks
1071 8b9887c5 Petr Pudlak
    ReleaseLocks(self, locking.LEVEL_NODEGROUP,
1072 8b9887c5 Petr Pudlak
                 keep=self.cfg.GetNodeGroupsFromNodes(keep_locks))
1073 22b7f6f8 Thomas Thrainer
1074 22b7f6f8 Thomas Thrainer
    assert (self.owned_locks(locking.LEVEL_NODE) ==
1075 22b7f6f8 Thomas Thrainer
            self.owned_locks(locking.LEVEL_NODE_RES)), \
1076 22b7f6f8 Thomas Thrainer
      "Node locks differ from node resource locks"
1077 22b7f6f8 Thomas Thrainer
1078 22b7f6f8 Thomas Thrainer
    #### node related checks
1079 22b7f6f8 Thomas Thrainer
1080 22b7f6f8 Thomas Thrainer
    # check primary node
1081 1c3231aa Thomas Thrainer
    self.pnode = pnode = self.cfg.GetNodeInfo(self.op.pnode_uuid)
1082 22b7f6f8 Thomas Thrainer
    assert self.pnode is not None, \
1083 1c3231aa Thomas Thrainer
      "Cannot retrieve locked node %s" % self.op.pnode_uuid
1084 22b7f6f8 Thomas Thrainer
    if pnode.offline:
1085 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Cannot use offline primary node '%s'" %
1086 22b7f6f8 Thomas Thrainer
                                 pnode.name, errors.ECODE_STATE)
1087 22b7f6f8 Thomas Thrainer
    if pnode.drained:
1088 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Cannot use drained primary node '%s'" %
1089 22b7f6f8 Thomas Thrainer
                                 pnode.name, errors.ECODE_STATE)
1090 22b7f6f8 Thomas Thrainer
    if not pnode.vm_capable:
1091 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Cannot use non-vm_capable primary node"
1092 22b7f6f8 Thomas Thrainer
                                 " '%s'" % pnode.name, errors.ECODE_STATE)
1093 22b7f6f8 Thomas Thrainer
1094 22b7f6f8 Thomas Thrainer
    self.secondaries = []
1095 22b7f6f8 Thomas Thrainer
1096 22b7f6f8 Thomas Thrainer
    # Fill in any IPs from IP pools. This must happen here, because we need to
1097 22b7f6f8 Thomas Thrainer
    # know the nic's primary node, as specified by the iallocator
1098 22b7f6f8 Thomas Thrainer
    for idx, nic in enumerate(self.nics):
1099 22b7f6f8 Thomas Thrainer
      net_uuid = nic.network
1100 22b7f6f8 Thomas Thrainer
      if net_uuid is not None:
1101 22b7f6f8 Thomas Thrainer
        nobj = self.cfg.GetNetwork(net_uuid)
1102 1c3231aa Thomas Thrainer
        netparams = self.cfg.GetGroupNetParams(net_uuid, self.pnode.uuid)
1103 22b7f6f8 Thomas Thrainer
        if netparams is None:
1104 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("No netparams found for network"
1105 66222813 Thomas Thrainer
                                     " %s. Probably not connected to"
1106 22b7f6f8 Thomas Thrainer
                                     " node's %s nodegroup" %
1107 22b7f6f8 Thomas Thrainer
                                     (nobj.name, self.pnode.name),
1108 22b7f6f8 Thomas Thrainer
                                     errors.ECODE_INVAL)
1109 22b7f6f8 Thomas Thrainer
        self.LogInfo("NIC/%d inherits netparams %s" %
1110 22b7f6f8 Thomas Thrainer
                     (idx, netparams.values()))
1111 22b7f6f8 Thomas Thrainer
        nic.nicparams = dict(netparams)
1112 22b7f6f8 Thomas Thrainer
        if nic.ip is not None:
1113 22b7f6f8 Thomas Thrainer
          if nic.ip.lower() == constants.NIC_IP_POOL:
1114 22b7f6f8 Thomas Thrainer
            try:
1115 22b7f6f8 Thomas Thrainer
              nic.ip = self.cfg.GenerateIp(net_uuid, self.proc.GetECId())
1116 22b7f6f8 Thomas Thrainer
            except errors.ReservationError:
1117 22b7f6f8 Thomas Thrainer
              raise errors.OpPrereqError("Unable to get a free IP for NIC %d"
1118 22b7f6f8 Thomas Thrainer
                                         " from the address pool" % idx,
1119 22b7f6f8 Thomas Thrainer
                                         errors.ECODE_STATE)
1120 22b7f6f8 Thomas Thrainer
            self.LogInfo("Chose IP %s from network %s", nic.ip, nobj.name)
1121 22b7f6f8 Thomas Thrainer
          else:
1122 22b7f6f8 Thomas Thrainer
            try:
1123 031d2db1 Dimitris Aragiorgis
              self.cfg.ReserveIp(net_uuid, nic.ip, self.proc.GetECId(),
1124 031d2db1 Dimitris Aragiorgis
                                 check=self.op.conflicts_check)
1125 22b7f6f8 Thomas Thrainer
            except errors.ReservationError:
1126 22b7f6f8 Thomas Thrainer
              raise errors.OpPrereqError("IP address %s already in use"
1127 22b7f6f8 Thomas Thrainer
                                         " or does not belong to network %s" %
1128 22b7f6f8 Thomas Thrainer
                                         (nic.ip, nobj.name),
1129 22b7f6f8 Thomas Thrainer
                                         errors.ECODE_NOTUNIQUE)
1130 22b7f6f8 Thomas Thrainer
1131 22b7f6f8 Thomas Thrainer
      # net is None, ip None or given
1132 22b7f6f8 Thomas Thrainer
      elif self.op.conflicts_check:
1133 1c3231aa Thomas Thrainer
        _CheckForConflictingIp(self, nic.ip, self.pnode.uuid)
1134 22b7f6f8 Thomas Thrainer
1135 22b7f6f8 Thomas Thrainer
    # mirror node verification
1136 22b7f6f8 Thomas Thrainer
    if self.op.disk_template in constants.DTS_INT_MIRROR:
1137 1c3231aa Thomas Thrainer
      if self.op.snode_uuid == pnode.uuid:
1138 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("The secondary node cannot be the"
1139 22b7f6f8 Thomas Thrainer
                                   " primary node", errors.ECODE_INVAL)
1140 1c3231aa Thomas Thrainer
      CheckNodeOnline(self, self.op.snode_uuid)
1141 1c3231aa Thomas Thrainer
      CheckNodeNotDrained(self, self.op.snode_uuid)
1142 1c3231aa Thomas Thrainer
      CheckNodeVmCapable(self, self.op.snode_uuid)
1143 1c3231aa Thomas Thrainer
      self.secondaries.append(self.op.snode_uuid)
1144 22b7f6f8 Thomas Thrainer
1145 1c3231aa Thomas Thrainer
      snode = self.cfg.GetNodeInfo(self.op.snode_uuid)
1146 22b7f6f8 Thomas Thrainer
      if pnode.group != snode.group:
1147 22b7f6f8 Thomas Thrainer
        self.LogWarning("The primary and secondary nodes are in two"
1148 22b7f6f8 Thomas Thrainer
                        " different node groups; the disk parameters"
1149 22b7f6f8 Thomas Thrainer
                        " from the first disk's node group will be"
1150 22b7f6f8 Thomas Thrainer
                        " used")
1151 22b7f6f8 Thomas Thrainer
1152 1bb99a33 Bernardo Dal Seno
    nodes = [pnode]
1153 1bb99a33 Bernardo Dal Seno
    if self.op.disk_template in constants.DTS_INT_MIRROR:
1154 1bb99a33 Bernardo Dal Seno
      nodes.append(snode)
1155 1bb99a33 Bernardo Dal Seno
    has_es = lambda n: IsExclusiveStorageEnabledNode(self.cfg, n)
1156 3f3ea14c Bernardo Dal Seno
    excl_stor = compat.any(map(has_es, nodes))
1157 3f3ea14c Bernardo Dal Seno
    if excl_stor and not self.op.disk_template in constants.DTS_EXCL_STORAGE:
1158 3f3ea14c Bernardo Dal Seno
      raise errors.OpPrereqError("Disk template %s not supported with"
1159 3f3ea14c Bernardo Dal Seno
                                 " exclusive storage" % self.op.disk_template,
1160 3f3ea14c Bernardo Dal Seno
                                 errors.ECODE_STATE)
1161 3f3ea14c Bernardo Dal Seno
    for disk in self.disks:
1162 7c848a6a Bernardo Dal Seno
      CheckSpindlesExclusiveStorage(disk, excl_stor, True)
1163 22b7f6f8 Thomas Thrainer
1164 1c3231aa Thomas Thrainer
    node_uuids = [pnode.uuid] + self.secondaries
1165 22b7f6f8 Thomas Thrainer
1166 22b7f6f8 Thomas Thrainer
    if not self.adopt_disks:
1167 22b7f6f8 Thomas Thrainer
      if self.op.disk_template == constants.DT_RBD:
1168 22b7f6f8 Thomas Thrainer
        # _CheckRADOSFreeSpace() is just a placeholder.
1169 22b7f6f8 Thomas Thrainer
        # Any function that checks prerequisites can be placed here.
1170 22b7f6f8 Thomas Thrainer
        # Check if there is enough space on the RADOS cluster.
1171 5eacbcae Thomas Thrainer
        CheckRADOSFreeSpace()
1172 22b7f6f8 Thomas Thrainer
      elif self.op.disk_template == constants.DT_EXT:
1173 22b7f6f8 Thomas Thrainer
        # FIXME: Function that checks prereqs if needed
1174 22b7f6f8 Thomas Thrainer
        pass
1175 d48c944b Helga Velroyen
      elif self.op.disk_template in constants.DTS_LVM:
1176 22b7f6f8 Thomas Thrainer
        # Check lv size requirements, if not adopting
1177 5eacbcae Thomas Thrainer
        req_sizes = ComputeDiskSizePerVG(self.op.disk_template, self.disks)
1178 1c3231aa Thomas Thrainer
        CheckNodesFreeDiskPerVG(self, node_uuids, req_sizes)
1179 4b92e992 Helga Velroyen
      else:
1180 4b92e992 Helga Velroyen
        # FIXME: add checks for other, non-adopting, non-lvm disk templates
1181 4b92e992 Helga Velroyen
        pass
1182 22b7f6f8 Thomas Thrainer
1183 22b7f6f8 Thomas Thrainer
    elif self.op.disk_template == constants.DT_PLAIN: # Check the adoption data
1184 22b7f6f8 Thomas Thrainer
      all_lvs = set(["%s/%s" % (disk[constants.IDISK_VG],
1185 22b7f6f8 Thomas Thrainer
                                disk[constants.IDISK_ADOPT])
1186 22b7f6f8 Thomas Thrainer
                     for disk in self.disks])
1187 22b7f6f8 Thomas Thrainer
      if len(all_lvs) != len(self.disks):
1188 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Duplicate volume names given for adoption",
1189 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
1190 22b7f6f8 Thomas Thrainer
      for lv_name in all_lvs:
1191 22b7f6f8 Thomas Thrainer
        try:
1192 22b7f6f8 Thomas Thrainer
          # FIXME: lv_name here is "vg/lv" need to ensure that other calls
1193 22b7f6f8 Thomas Thrainer
          # to ReserveLV uses the same syntax
1194 22b7f6f8 Thomas Thrainer
          self.cfg.ReserveLV(lv_name, self.proc.GetECId())
1195 22b7f6f8 Thomas Thrainer
        except errors.ReservationError:
1196 22b7f6f8 Thomas Thrainer
          raise errors.OpPrereqError("LV named %s used by another instance" %
1197 22b7f6f8 Thomas Thrainer
                                     lv_name, errors.ECODE_NOTUNIQUE)
1198 22b7f6f8 Thomas Thrainer
1199 1c3231aa Thomas Thrainer
      vg_names = self.rpc.call_vg_list([pnode.uuid])[pnode.uuid]
1200 22b7f6f8 Thomas Thrainer
      vg_names.Raise("Cannot get VG information from node %s" % pnode.name)
1201 22b7f6f8 Thomas Thrainer
1202 1c3231aa Thomas Thrainer
      node_lvs = self.rpc.call_lv_list([pnode.uuid],
1203 1c3231aa Thomas Thrainer
                                       vg_names.payload.keys())[pnode.uuid]
1204 22b7f6f8 Thomas Thrainer
      node_lvs.Raise("Cannot get LV information from node %s" % pnode.name)
1205 22b7f6f8 Thomas Thrainer
      node_lvs = node_lvs.payload
1206 22b7f6f8 Thomas Thrainer
1207 22b7f6f8 Thomas Thrainer
      delta = all_lvs.difference(node_lvs.keys())
1208 22b7f6f8 Thomas Thrainer
      if delta:
1209 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Missing logical volume(s): %s" %
1210 22b7f6f8 Thomas Thrainer
                                   utils.CommaJoin(delta),
1211 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
1212 22b7f6f8 Thomas Thrainer
      online_lvs = [lv for lv in all_lvs if node_lvs[lv][2]]
1213 22b7f6f8 Thomas Thrainer
      if online_lvs:
1214 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Online logical volumes found, cannot"
1215 22b7f6f8 Thomas Thrainer
                                   " adopt: %s" % utils.CommaJoin(online_lvs),
1216 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_STATE)
1217 22b7f6f8 Thomas Thrainer
      # update the size of disk based on what is found
1218 22b7f6f8 Thomas Thrainer
      for dsk in self.disks:
1219 22b7f6f8 Thomas Thrainer
        dsk[constants.IDISK_SIZE] = \
1220 22b7f6f8 Thomas Thrainer
          int(float(node_lvs["%s/%s" % (dsk[constants.IDISK_VG],
1221 22b7f6f8 Thomas Thrainer
                                        dsk[constants.IDISK_ADOPT])][0]))
1222 22b7f6f8 Thomas Thrainer
1223 22b7f6f8 Thomas Thrainer
    elif self.op.disk_template == constants.DT_BLOCK:
1224 22b7f6f8 Thomas Thrainer
      # Normalize and de-duplicate device paths
1225 22b7f6f8 Thomas Thrainer
      all_disks = set([os.path.abspath(disk[constants.IDISK_ADOPT])
1226 22b7f6f8 Thomas Thrainer
                       for disk in self.disks])
1227 22b7f6f8 Thomas Thrainer
      if len(all_disks) != len(self.disks):
1228 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Duplicate disk names given for adoption",
1229 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
1230 22b7f6f8 Thomas Thrainer
      baddisks = [d for d in all_disks
1231 22b7f6f8 Thomas Thrainer
                  if not d.startswith(constants.ADOPTABLE_BLOCKDEV_ROOT)]
1232 22b7f6f8 Thomas Thrainer
      if baddisks:
1233 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Device node(s) %s lie outside %s and"
1234 22b7f6f8 Thomas Thrainer
                                   " cannot be adopted" %
1235 22b7f6f8 Thomas Thrainer
                                   (utils.CommaJoin(baddisks),
1236 22b7f6f8 Thomas Thrainer
                                    constants.ADOPTABLE_BLOCKDEV_ROOT),
1237 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
1238 22b7f6f8 Thomas Thrainer
1239 1c3231aa Thomas Thrainer
      node_disks = self.rpc.call_bdev_sizes([pnode.uuid],
1240 1c3231aa Thomas Thrainer
                                            list(all_disks))[pnode.uuid]
1241 22b7f6f8 Thomas Thrainer
      node_disks.Raise("Cannot get block device information from node %s" %
1242 22b7f6f8 Thomas Thrainer
                       pnode.name)
1243 22b7f6f8 Thomas Thrainer
      node_disks = node_disks.payload
1244 22b7f6f8 Thomas Thrainer
      delta = all_disks.difference(node_disks.keys())
1245 22b7f6f8 Thomas Thrainer
      if delta:
1246 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Missing block device(s): %s" %
1247 22b7f6f8 Thomas Thrainer
                                   utils.CommaJoin(delta),
1248 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
1249 22b7f6f8 Thomas Thrainer
      for dsk in self.disks:
1250 22b7f6f8 Thomas Thrainer
        dsk[constants.IDISK_SIZE] = \
1251 22b7f6f8 Thomas Thrainer
          int(float(node_disks[dsk[constants.IDISK_ADOPT]]))
1252 22b7f6f8 Thomas Thrainer
1253 294254b1 Raffa Santi
    # Check disk access param to be compatible with specified hypervisor
1254 294254b1 Raffa Santi
    node_info = self.cfg.GetNodeInfo(self.op.pnode_uuid)
1255 294254b1 Raffa Santi
    node_group = self.cfg.GetNodeGroup(node_info.group)
1256 294254b1 Raffa Santi
    disk_params = self.cfg.GetGroupDiskParams(node_group)
1257 294254b1 Raffa Santi
    access_type = disk_params[self.op.disk_template].get(
1258 294254b1 Raffa Santi
      constants.RBD_ACCESS, constants.DISK_KERNELSPACE
1259 294254b1 Raffa Santi
    )
1260 294254b1 Raffa Santi
1261 294254b1 Raffa Santi
    if not IsValidDiskAccessModeCombination(self.op.hypervisor,
1262 294254b1 Raffa Santi
                                            self.op.disk_template,
1263 294254b1 Raffa Santi
                                            access_type):
1264 294254b1 Raffa Santi
      raise errors.OpPrereqError("Selected hypervisor (%s) cannot be"
1265 294254b1 Raffa Santi
                                 " used with %s disk access param" %
1266 294254b1 Raffa Santi
                                 (self.op.hypervisor, access_type),
1267 294254b1 Raffa Santi
                                  errors.ECODE_STATE)
1268 294254b1 Raffa Santi
1269 22b7f6f8 Thomas Thrainer
    # Verify instance specs
1270 22b7f6f8 Thomas Thrainer
    spindle_use = self.be_full.get(constants.BE_SPINDLE_USE, None)
1271 22b7f6f8 Thomas Thrainer
    ispec = {
1272 22b7f6f8 Thomas Thrainer
      constants.ISPEC_MEM_SIZE: self.be_full.get(constants.BE_MAXMEM, None),
1273 22b7f6f8 Thomas Thrainer
      constants.ISPEC_CPU_COUNT: self.be_full.get(constants.BE_VCPUS, None),
1274 22b7f6f8 Thomas Thrainer
      constants.ISPEC_DISK_COUNT: len(self.disks),
1275 22b7f6f8 Thomas Thrainer
      constants.ISPEC_DISK_SIZE: [disk[constants.IDISK_SIZE]
1276 22b7f6f8 Thomas Thrainer
                                  for disk in self.disks],
1277 22b7f6f8 Thomas Thrainer
      constants.ISPEC_NIC_COUNT: len(self.nics),
1278 22b7f6f8 Thomas Thrainer
      constants.ISPEC_SPINDLE_USE: spindle_use,
1279 22b7f6f8 Thomas Thrainer
      }
1280 22b7f6f8 Thomas Thrainer
1281 22b7f6f8 Thomas Thrainer
    group_info = self.cfg.GetNodeGroup(pnode.group)
1282 22b7f6f8 Thomas Thrainer
    ipolicy = ganeti.masterd.instance.CalculateGroupIPolicy(cluster, group_info)
1283 22b7f6f8 Thomas Thrainer
    res = _ComputeIPolicyInstanceSpecViolation(ipolicy, ispec,
1284 22b7f6f8 Thomas Thrainer
                                               self.op.disk_template)
1285 22b7f6f8 Thomas Thrainer
    if not self.op.ignore_ipolicy and res:
1286 22b7f6f8 Thomas Thrainer
      msg = ("Instance allocation to group %s (%s) violates policy: %s" %
1287 22b7f6f8 Thomas Thrainer
             (pnode.group, group_info.name, utils.CommaJoin(res)))
1288 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError(msg, errors.ECODE_INVAL)
1289 22b7f6f8 Thomas Thrainer
1290 1c3231aa Thomas Thrainer
    CheckHVParams(self, node_uuids, self.op.hypervisor, self.op.hvparams)
1291 22b7f6f8 Thomas Thrainer
1292 1c3231aa Thomas Thrainer
    CheckNodeHasOS(self, pnode.uuid, self.op.os_type, self.op.force_variant)
1293 22b7f6f8 Thomas Thrainer
    # check OS parameters (remotely)
1294 1c3231aa Thomas Thrainer
    CheckOSParams(self, True, node_uuids, self.op.os_type, self.os_full)
1295 22b7f6f8 Thomas Thrainer
1296 1c3231aa Thomas Thrainer
    CheckNicsBridgesExist(self, self.nics, self.pnode.uuid)
1297 22b7f6f8 Thomas Thrainer
1298 22b7f6f8 Thomas Thrainer
    #TODO: _CheckExtParams (remotely)
1299 22b7f6f8 Thomas Thrainer
    # Check parameters for extstorage
1300 22b7f6f8 Thomas Thrainer
1301 22b7f6f8 Thomas Thrainer
    # memory check on primary node
1302 22b7f6f8 Thomas Thrainer
    #TODO(dynmem): use MINMEM for checking
1303 22b7f6f8 Thomas Thrainer
    if self.op.start:
1304 a295eb80 Helga Velroyen
      hvfull = objects.FillDict(cluster.hvparams.get(self.op.hypervisor, {}),
1305 a295eb80 Helga Velroyen
                                self.op.hvparams)
1306 1c3231aa Thomas Thrainer
      CheckNodeFreeMemory(self, self.pnode.uuid,
1307 5eacbcae Thomas Thrainer
                          "creating instance %s" % self.op.instance_name,
1308 5eacbcae Thomas Thrainer
                          self.be_full[constants.BE_MAXMEM],
1309 a295eb80 Helga Velroyen
                          self.op.hypervisor, hvfull)
1310 22b7f6f8 Thomas Thrainer
1311 1c3231aa Thomas Thrainer
    self.dry_run_result = list(node_uuids)
1312 22b7f6f8 Thomas Thrainer
1313 22b7f6f8 Thomas Thrainer
  def Exec(self, feedback_fn):
1314 22b7f6f8 Thomas Thrainer
    """Create and add the instance to the cluster.
1315 22b7f6f8 Thomas Thrainer

1316 22b7f6f8 Thomas Thrainer
    """
1317 22b7f6f8 Thomas Thrainer
    assert not (self.owned_locks(locking.LEVEL_NODE_RES) -
1318 22b7f6f8 Thomas Thrainer
                self.owned_locks(locking.LEVEL_NODE)), \
1319 22b7f6f8 Thomas Thrainer
      "Node locks differ from node resource locks"
1320 22b7f6f8 Thomas Thrainer
    assert not self.glm.is_owned(locking.LEVEL_NODE_ALLOC)
1321 22b7f6f8 Thomas Thrainer
1322 22b7f6f8 Thomas Thrainer
    ht_kind = self.op.hypervisor
1323 22b7f6f8 Thomas Thrainer
    if ht_kind in constants.HTS_REQ_PORT:
1324 22b7f6f8 Thomas Thrainer
      network_port = self.cfg.AllocatePort()
1325 22b7f6f8 Thomas Thrainer
    else:
1326 22b7f6f8 Thomas Thrainer
      network_port = None
1327 22b7f6f8 Thomas Thrainer
1328 da4a52a3 Thomas Thrainer
    instance_uuid = self.cfg.GenerateUniqueID(self.proc.GetECId())
1329 da4a52a3 Thomas Thrainer
1330 22b7f6f8 Thomas Thrainer
    # This is ugly but we got a chicken-egg problem here
1331 22b7f6f8 Thomas Thrainer
    # We can only take the group disk parameters, as the instance
1332 22b7f6f8 Thomas Thrainer
    # has no disks yet (we are generating them right here).
1333 1c3231aa Thomas Thrainer
    nodegroup = self.cfg.GetNodeGroup(self.pnode.group)
1334 5eacbcae Thomas Thrainer
    disks = GenerateDiskTemplate(self,
1335 5eacbcae Thomas Thrainer
                                 self.op.disk_template,
1336 da4a52a3 Thomas Thrainer
                                 instance_uuid, self.pnode.uuid,
1337 5eacbcae Thomas Thrainer
                                 self.secondaries,
1338 5eacbcae Thomas Thrainer
                                 self.disks,
1339 5eacbcae Thomas Thrainer
                                 self.instance_file_storage_dir,
1340 5eacbcae Thomas Thrainer
                                 self.op.file_driver,
1341 5eacbcae Thomas Thrainer
                                 0,
1342 5eacbcae Thomas Thrainer
                                 feedback_fn,
1343 5eacbcae Thomas Thrainer
                                 self.cfg.GetGroupDiskParams(nodegroup))
1344 22b7f6f8 Thomas Thrainer
1345 da4a52a3 Thomas Thrainer
    iobj = objects.Instance(name=self.op.instance_name,
1346 da4a52a3 Thomas Thrainer
                            uuid=instance_uuid,
1347 da4a52a3 Thomas Thrainer
                            os=self.op.os_type,
1348 1c3231aa Thomas Thrainer
                            primary_node=self.pnode.uuid,
1349 22b7f6f8 Thomas Thrainer
                            nics=self.nics, disks=disks,
1350 22b7f6f8 Thomas Thrainer
                            disk_template=self.op.disk_template,
1351 1d4a4b26 Thomas Thrainer
                            disks_active=False,
1352 22b7f6f8 Thomas Thrainer
                            admin_state=constants.ADMINST_DOWN,
1353 22b7f6f8 Thomas Thrainer
                            network_port=network_port,
1354 22b7f6f8 Thomas Thrainer
                            beparams=self.op.beparams,
1355 22b7f6f8 Thomas Thrainer
                            hvparams=self.op.hvparams,
1356 22b7f6f8 Thomas Thrainer
                            hypervisor=self.op.hypervisor,
1357 22b7f6f8 Thomas Thrainer
                            osparams=self.op.osparams,
1358 6bce7ba2 Santi Raffa
                            osparams_private=self.op.osparams_private,
1359 22b7f6f8 Thomas Thrainer
                            )
1360 22b7f6f8 Thomas Thrainer
1361 22b7f6f8 Thomas Thrainer
    if self.op.tags:
1362 22b7f6f8 Thomas Thrainer
      for tag in self.op.tags:
1363 22b7f6f8 Thomas Thrainer
        iobj.AddTag(tag)
1364 22b7f6f8 Thomas Thrainer
1365 22b7f6f8 Thomas Thrainer
    if self.adopt_disks:
1366 22b7f6f8 Thomas Thrainer
      if self.op.disk_template == constants.DT_PLAIN:
1367 22b7f6f8 Thomas Thrainer
        # rename LVs to the newly-generated names; we need to construct
1368 22b7f6f8 Thomas Thrainer
        # 'fake' LV disks with the old data, plus the new unique_id
1369 22b7f6f8 Thomas Thrainer
        tmp_disks = [objects.Disk.FromDict(v.ToDict()) for v in disks]
1370 22b7f6f8 Thomas Thrainer
        rename_to = []
1371 22b7f6f8 Thomas Thrainer
        for t_dsk, a_dsk in zip(tmp_disks, self.disks):
1372 22b7f6f8 Thomas Thrainer
          rename_to.append(t_dsk.logical_id)
1373 22b7f6f8 Thomas Thrainer
          t_dsk.logical_id = (t_dsk.logical_id[0], a_dsk[constants.IDISK_ADOPT])
1374 1c3231aa Thomas Thrainer
        result = self.rpc.call_blockdev_rename(self.pnode.uuid,
1375 22b7f6f8 Thomas Thrainer
                                               zip(tmp_disks, rename_to))
1376 22b7f6f8 Thomas Thrainer
        result.Raise("Failed to rename adoped LVs")
1377 22b7f6f8 Thomas Thrainer
    else:
1378 22b7f6f8 Thomas Thrainer
      feedback_fn("* creating instance disks...")
1379 22b7f6f8 Thomas Thrainer
      try:
1380 5eacbcae Thomas Thrainer
        CreateDisks(self, iobj)
1381 22b7f6f8 Thomas Thrainer
      except errors.OpExecError:
1382 22b7f6f8 Thomas Thrainer
        self.LogWarning("Device creation failed")
1383 d0d7d7cf Thomas Thrainer
        self.cfg.ReleaseDRBDMinors(self.op.instance_name)
1384 22b7f6f8 Thomas Thrainer
        raise
1385 22b7f6f8 Thomas Thrainer
1386 d0d7d7cf Thomas Thrainer
    feedback_fn("adding instance %s to cluster config" % self.op.instance_name)
1387 22b7f6f8 Thomas Thrainer
1388 22b7f6f8 Thomas Thrainer
    self.cfg.AddInstance(iobj, self.proc.GetECId())
1389 22b7f6f8 Thomas Thrainer
1390 22b7f6f8 Thomas Thrainer
    # Declare that we don't want to remove the instance lock anymore, as we've
1391 22b7f6f8 Thomas Thrainer
    # added the instance to the config
1392 22b7f6f8 Thomas Thrainer
    del self.remove_locks[locking.LEVEL_INSTANCE]
1393 22b7f6f8 Thomas Thrainer
1394 22b7f6f8 Thomas Thrainer
    if self.op.mode == constants.INSTANCE_IMPORT:
1395 22b7f6f8 Thomas Thrainer
      # Release unused nodes
1396 1c3231aa Thomas Thrainer
      ReleaseLocks(self, locking.LEVEL_NODE, keep=[self.op.src_node_uuid])
1397 22b7f6f8 Thomas Thrainer
    else:
1398 22b7f6f8 Thomas Thrainer
      # Release all nodes
1399 5eacbcae Thomas Thrainer
      ReleaseLocks(self, locking.LEVEL_NODE)
1400 22b7f6f8 Thomas Thrainer
1401 22b7f6f8 Thomas Thrainer
    disk_abort = False
1402 22b7f6f8 Thomas Thrainer
    if not self.adopt_disks and self.cfg.GetClusterInfo().prealloc_wipe_disks:
1403 22b7f6f8 Thomas Thrainer
      feedback_fn("* wiping instance disks...")
1404 22b7f6f8 Thomas Thrainer
      try:
1405 5eacbcae Thomas Thrainer
        WipeDisks(self, iobj)
1406 22b7f6f8 Thomas Thrainer
      except errors.OpExecError, err:
1407 22b7f6f8 Thomas Thrainer
        logging.exception("Wiping disks failed")
1408 22b7f6f8 Thomas Thrainer
        self.LogWarning("Wiping instance disks failed (%s)", err)
1409 22b7f6f8 Thomas Thrainer
        disk_abort = True
1410 22b7f6f8 Thomas Thrainer
1411 22b7f6f8 Thomas Thrainer
    if disk_abort:
1412 22b7f6f8 Thomas Thrainer
      # Something is already wrong with the disks, don't do anything else
1413 22b7f6f8 Thomas Thrainer
      pass
1414 22b7f6f8 Thomas Thrainer
    elif self.op.wait_for_sync:
1415 5eacbcae Thomas Thrainer
      disk_abort = not WaitForSync(self, iobj)
1416 22b7f6f8 Thomas Thrainer
    elif iobj.disk_template in constants.DTS_INT_MIRROR:
1417 22b7f6f8 Thomas Thrainer
      # make sure the disks are not degraded (still sync-ing is ok)
1418 22b7f6f8 Thomas Thrainer
      feedback_fn("* checking mirrors status")
1419 5eacbcae Thomas Thrainer
      disk_abort = not WaitForSync(self, iobj, oneshot=True)
1420 22b7f6f8 Thomas Thrainer
    else:
1421 22b7f6f8 Thomas Thrainer
      disk_abort = False
1422 22b7f6f8 Thomas Thrainer
1423 22b7f6f8 Thomas Thrainer
    if disk_abort:
1424 5eacbcae Thomas Thrainer
      RemoveDisks(self, iobj)
1425 da4a52a3 Thomas Thrainer
      self.cfg.RemoveInstance(iobj.uuid)
1426 22b7f6f8 Thomas Thrainer
      # Make sure the instance lock gets removed
1427 22b7f6f8 Thomas Thrainer
      self.remove_locks[locking.LEVEL_INSTANCE] = iobj.name
1428 22b7f6f8 Thomas Thrainer
      raise errors.OpExecError("There are some degraded disks for"
1429 22b7f6f8 Thomas Thrainer
                               " this instance")
1430 22b7f6f8 Thomas Thrainer
1431 1d4a4b26 Thomas Thrainer
    # instance disks are now active
1432 1d4a4b26 Thomas Thrainer
    iobj.disks_active = True
1433 1d4a4b26 Thomas Thrainer
1434 22b7f6f8 Thomas Thrainer
    # Release all node resource locks
1435 5eacbcae Thomas Thrainer
    ReleaseLocks(self, locking.LEVEL_NODE_RES)
1436 22b7f6f8 Thomas Thrainer
1437 22b7f6f8 Thomas Thrainer
    if iobj.disk_template != constants.DT_DISKLESS and not self.adopt_disks:
1438 22b7f6f8 Thomas Thrainer
      if self.op.mode == constants.INSTANCE_CREATE:
1439 22b7f6f8 Thomas Thrainer
        if not self.op.no_install:
1440 22b7f6f8 Thomas Thrainer
          pause_sync = (iobj.disk_template in constants.DTS_INT_MIRROR and
1441 22b7f6f8 Thomas Thrainer
                        not self.op.wait_for_sync)
1442 22b7f6f8 Thomas Thrainer
          if pause_sync:
1443 22b7f6f8 Thomas Thrainer
            feedback_fn("* pausing disk sync to install instance OS")
1444 1c3231aa Thomas Thrainer
            result = self.rpc.call_blockdev_pause_resume_sync(self.pnode.uuid,
1445 22b7f6f8 Thomas Thrainer
                                                              (iobj.disks,
1446 22b7f6f8 Thomas Thrainer
                                                               iobj), True)
1447 22b7f6f8 Thomas Thrainer
            for idx, success in enumerate(result.payload):
1448 22b7f6f8 Thomas Thrainer
              if not success:
1449 22b7f6f8 Thomas Thrainer
                logging.warn("pause-sync of instance %s for disk %d failed",
1450 d0d7d7cf Thomas Thrainer
                             self.op.instance_name, idx)
1451 22b7f6f8 Thomas Thrainer
1452 22b7f6f8 Thomas Thrainer
          feedback_fn("* running the instance OS create scripts...")
1453 22b7f6f8 Thomas Thrainer
          # FIXME: pass debug option from opcode to backend
1454 22b7f6f8 Thomas Thrainer
          os_add_result = \
1455 6bce7ba2 Santi Raffa
            self.rpc.call_instance_os_add(self.pnode.uuid,
1456 6bce7ba2 Santi Raffa
                                          (iobj, self.op.osparams_secret),
1457 6bce7ba2 Santi Raffa
                                          False,
1458 22b7f6f8 Thomas Thrainer
                                          self.op.debug_level)
1459 22b7f6f8 Thomas Thrainer
          if pause_sync:
1460 22b7f6f8 Thomas Thrainer
            feedback_fn("* resuming disk sync")
1461 1c3231aa Thomas Thrainer
            result = self.rpc.call_blockdev_pause_resume_sync(self.pnode.uuid,
1462 22b7f6f8 Thomas Thrainer
                                                              (iobj.disks,
1463 22b7f6f8 Thomas Thrainer
                                                               iobj), False)
1464 22b7f6f8 Thomas Thrainer
            for idx, success in enumerate(result.payload):
1465 22b7f6f8 Thomas Thrainer
              if not success:
1466 22b7f6f8 Thomas Thrainer
                logging.warn("resume-sync of instance %s for disk %d failed",
1467 d0d7d7cf Thomas Thrainer
                             self.op.instance_name, idx)
1468 22b7f6f8 Thomas Thrainer
1469 22b7f6f8 Thomas Thrainer
          os_add_result.Raise("Could not add os for instance %s"
1470 d0d7d7cf Thomas Thrainer
                              " on node %s" % (self.op.instance_name,
1471 d0d7d7cf Thomas Thrainer
                                               self.pnode.name))
1472 22b7f6f8 Thomas Thrainer
1473 22b7f6f8 Thomas Thrainer
      else:
1474 22b7f6f8 Thomas Thrainer
        if self.op.mode == constants.INSTANCE_IMPORT:
1475 22b7f6f8 Thomas Thrainer
          feedback_fn("* running the instance OS import scripts...")
1476 22b7f6f8 Thomas Thrainer
1477 22b7f6f8 Thomas Thrainer
          transfers = []
1478 22b7f6f8 Thomas Thrainer
1479 22b7f6f8 Thomas Thrainer
          for idx, image in enumerate(self.src_images):
1480 22b7f6f8 Thomas Thrainer
            if not image:
1481 22b7f6f8 Thomas Thrainer
              continue
1482 22b7f6f8 Thomas Thrainer
1483 22b7f6f8 Thomas Thrainer
            # FIXME: pass debug option from opcode to backend
1484 22b7f6f8 Thomas Thrainer
            dt = masterd.instance.DiskTransfer("disk/%s" % idx,
1485 22b7f6f8 Thomas Thrainer
                                               constants.IEIO_FILE, (image, ),
1486 22b7f6f8 Thomas Thrainer
                                               constants.IEIO_SCRIPT,
1487 0c3d9c7c Thomas Thrainer
                                               ((iobj.disks[idx], iobj), idx),
1488 22b7f6f8 Thomas Thrainer
                                               None)
1489 22b7f6f8 Thomas Thrainer
            transfers.append(dt)
1490 22b7f6f8 Thomas Thrainer
1491 22b7f6f8 Thomas Thrainer
          import_result = \
1492 22b7f6f8 Thomas Thrainer
            masterd.instance.TransferInstanceData(self, feedback_fn,
1493 1c3231aa Thomas Thrainer
                                                  self.op.src_node_uuid,
1494 1c3231aa Thomas Thrainer
                                                  self.pnode.uuid,
1495 22b7f6f8 Thomas Thrainer
                                                  self.pnode.secondary_ip,
1496 51d7ac96 Thomas Thrainer
                                                  self.op.compress,
1497 22b7f6f8 Thomas Thrainer
                                                  iobj, transfers)
1498 22b7f6f8 Thomas Thrainer
          if not compat.all(import_result):
1499 22b7f6f8 Thomas Thrainer
            self.LogWarning("Some disks for instance %s on node %s were not"
1500 d0d7d7cf Thomas Thrainer
                            " imported successfully" % (self.op.instance_name,
1501 d0d7d7cf Thomas Thrainer
                                                        self.pnode.name))
1502 22b7f6f8 Thomas Thrainer
1503 22b7f6f8 Thomas Thrainer
          rename_from = self._old_instance_name
1504 22b7f6f8 Thomas Thrainer
1505 22b7f6f8 Thomas Thrainer
        elif self.op.mode == constants.INSTANCE_REMOTE_IMPORT:
1506 22b7f6f8 Thomas Thrainer
          feedback_fn("* preparing remote import...")
1507 22b7f6f8 Thomas Thrainer
          # The source cluster will stop the instance before attempting to make
1508 22b7f6f8 Thomas Thrainer
          # a connection. In some cases stopping an instance can take a long
1509 22b7f6f8 Thomas Thrainer
          # time, hence the shutdown timeout is added to the connection
1510 22b7f6f8 Thomas Thrainer
          # timeout.
1511 22b7f6f8 Thomas Thrainer
          connect_timeout = (constants.RIE_CONNECT_TIMEOUT +
1512 22b7f6f8 Thomas Thrainer
                             self.op.source_shutdown_timeout)
1513 22b7f6f8 Thomas Thrainer
          timeouts = masterd.instance.ImportExportTimeouts(connect_timeout)
1514 22b7f6f8 Thomas Thrainer
1515 1c3231aa Thomas Thrainer
          assert iobj.primary_node == self.pnode.uuid
1516 22b7f6f8 Thomas Thrainer
          disk_results = \
1517 22b7f6f8 Thomas Thrainer
            masterd.instance.RemoteImport(self, feedback_fn, iobj, self.pnode,
1518 22b7f6f8 Thomas Thrainer
                                          self.source_x509_ca,
1519 88acff3f Thomas Thrainer
                                          self._cds, self.op.compress, timeouts)
1520 22b7f6f8 Thomas Thrainer
          if not compat.all(disk_results):
1521 22b7f6f8 Thomas Thrainer
            # TODO: Should the instance still be started, even if some disks
1522 22b7f6f8 Thomas Thrainer
            # failed to import (valid for local imports, too)?
1523 22b7f6f8 Thomas Thrainer
            self.LogWarning("Some disks for instance %s on node %s were not"
1524 d0d7d7cf Thomas Thrainer
                            " imported successfully" % (self.op.instance_name,
1525 d0d7d7cf Thomas Thrainer
                                                        self.pnode.name))
1526 22b7f6f8 Thomas Thrainer
1527 22b7f6f8 Thomas Thrainer
          rename_from = self.source_instance_name
1528 22b7f6f8 Thomas Thrainer
1529 22b7f6f8 Thomas Thrainer
        else:
1530 22b7f6f8 Thomas Thrainer
          # also checked in the prereq part
1531 22b7f6f8 Thomas Thrainer
          raise errors.ProgrammerError("Unknown OS initialization mode '%s'"
1532 22b7f6f8 Thomas Thrainer
                                       % self.op.mode)
1533 22b7f6f8 Thomas Thrainer
1534 22b7f6f8 Thomas Thrainer
        # Run rename script on newly imported instance
1535 d0d7d7cf Thomas Thrainer
        assert iobj.name == self.op.instance_name
1536 d0d7d7cf Thomas Thrainer
        feedback_fn("Running rename script for %s" % self.op.instance_name)
1537 1c3231aa Thomas Thrainer
        result = self.rpc.call_instance_run_rename(self.pnode.uuid, iobj,
1538 22b7f6f8 Thomas Thrainer
                                                   rename_from,
1539 22b7f6f8 Thomas Thrainer
                                                   self.op.debug_level)
1540 c7dd65be Klaus Aehlig
        result.Warn("Failed to run rename script for %s on node %s" %
1541 d0d7d7cf Thomas Thrainer
                    (self.op.instance_name, self.pnode.name), self.LogWarning)
1542 22b7f6f8 Thomas Thrainer
1543 22b7f6f8 Thomas Thrainer
    assert not self.owned_locks(locking.LEVEL_NODE_RES)
1544 22b7f6f8 Thomas Thrainer
1545 22b7f6f8 Thomas Thrainer
    if self.op.start:
1546 22b7f6f8 Thomas Thrainer
      iobj.admin_state = constants.ADMINST_UP
1547 22b7f6f8 Thomas Thrainer
      self.cfg.Update(iobj, feedback_fn)
1548 d0d7d7cf Thomas Thrainer
      logging.info("Starting instance %s on node %s", self.op.instance_name,
1549 d0d7d7cf Thomas Thrainer
                   self.pnode.name)
1550 22b7f6f8 Thomas Thrainer
      feedback_fn("* starting instance...")
1551 1c3231aa Thomas Thrainer
      result = self.rpc.call_instance_start(self.pnode.uuid, (iobj, None, None),
1552 22b7f6f8 Thomas Thrainer
                                            False, self.op.reason)
1553 22b7f6f8 Thomas Thrainer
      result.Raise("Could not start instance")
1554 22b7f6f8 Thomas Thrainer
1555 b7a990e3 Hrvoje Ribicic
    return self.cfg.GetNodeNames(list(iobj.all_nodes))
1556 22b7f6f8 Thomas Thrainer
1557 22b7f6f8 Thomas Thrainer
1558 22b7f6f8 Thomas Thrainer
class LUInstanceRename(LogicalUnit):
1559 22b7f6f8 Thomas Thrainer
  """Rename an instance.
1560 22b7f6f8 Thomas Thrainer

1561 22b7f6f8 Thomas Thrainer
  """
1562 22b7f6f8 Thomas Thrainer
  HPATH = "instance-rename"
1563 22b7f6f8 Thomas Thrainer
  HTYPE = constants.HTYPE_INSTANCE
1564 22b7f6f8 Thomas Thrainer
1565 22b7f6f8 Thomas Thrainer
  def CheckArguments(self):
1566 22b7f6f8 Thomas Thrainer
    """Check arguments.
1567 22b7f6f8 Thomas Thrainer

1568 22b7f6f8 Thomas Thrainer
    """
1569 22b7f6f8 Thomas Thrainer
    if self.op.ip_check and not self.op.name_check:
1570 22b7f6f8 Thomas Thrainer
      # TODO: make the ip check more flexible and not depend on the name check
1571 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("IP address check requires a name check",
1572 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
1573 22b7f6f8 Thomas Thrainer
1574 22b7f6f8 Thomas Thrainer
  def BuildHooksEnv(self):
1575 22b7f6f8 Thomas Thrainer
    """Build hooks env.
1576 22b7f6f8 Thomas Thrainer

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

1579 22b7f6f8 Thomas Thrainer
    """
1580 5eacbcae Thomas Thrainer
    env = BuildInstanceHookEnvByObject(self, self.instance)
1581 22b7f6f8 Thomas Thrainer
    env["INSTANCE_NEW_NAME"] = self.op.new_name
1582 22b7f6f8 Thomas Thrainer
    return env
1583 22b7f6f8 Thomas Thrainer
1584 22b7f6f8 Thomas Thrainer
  def BuildHooksNodes(self):
1585 22b7f6f8 Thomas Thrainer
    """Build hooks nodes.
1586 22b7f6f8 Thomas Thrainer

1587 22b7f6f8 Thomas Thrainer
    """
1588 22b7f6f8 Thomas Thrainer
    nl = [self.cfg.GetMasterNode()] + list(self.instance.all_nodes)
1589 22b7f6f8 Thomas Thrainer
    return (nl, nl)
1590 22b7f6f8 Thomas Thrainer
1591 22b7f6f8 Thomas Thrainer
  def CheckPrereq(self):
1592 22b7f6f8 Thomas Thrainer
    """Check prerequisites.
1593 22b7f6f8 Thomas Thrainer

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

1596 22b7f6f8 Thomas Thrainer
    """
1597 da4a52a3 Thomas Thrainer
    (self.op.instance_uuid, self.op.instance_name) = \
1598 da4a52a3 Thomas Thrainer
      ExpandInstanceUuidAndName(self.cfg, self.op.instance_uuid,
1599 da4a52a3 Thomas Thrainer
                                self.op.instance_name)
1600 da4a52a3 Thomas Thrainer
    instance = self.cfg.GetInstanceInfo(self.op.instance_uuid)
1601 22b7f6f8 Thomas Thrainer
    assert instance is not None
1602 1f7c8208 Helga Velroyen
1603 1f7c8208 Helga Velroyen
    # It should actually not happen that an instance is running with a disabled
1604 1f7c8208 Helga Velroyen
    # disk template, but in case it does, the renaming of file-based instances
1605 1f7c8208 Helga Velroyen
    # will fail horribly. Thus, we test it before.
1606 1f7c8208 Helga Velroyen
    if (instance.disk_template in constants.DTS_FILEBASED and
1607 1f7c8208 Helga Velroyen
        self.op.new_name != instance.name):
1608 1f7c8208 Helga Velroyen
      CheckDiskTemplateEnabled(self.cfg.GetClusterInfo(),
1609 1f7c8208 Helga Velroyen
                               instance.disk_template)
1610 1f7c8208 Helga Velroyen
1611 5eacbcae Thomas Thrainer
    CheckNodeOnline(self, instance.primary_node)
1612 5eacbcae Thomas Thrainer
    CheckInstanceState(self, instance, INSTANCE_NOT_RUNNING,
1613 5eacbcae Thomas Thrainer
                       msg="cannot rename")
1614 22b7f6f8 Thomas Thrainer
    self.instance = instance
1615 22b7f6f8 Thomas Thrainer
1616 22b7f6f8 Thomas Thrainer
    new_name = self.op.new_name
1617 22b7f6f8 Thomas Thrainer
    if self.op.name_check:
1618 22b7f6f8 Thomas Thrainer
      hostname = _CheckHostnameSane(self, new_name)
1619 22b7f6f8 Thomas Thrainer
      new_name = self.op.new_name = hostname.name
1620 22b7f6f8 Thomas Thrainer
      if (self.op.ip_check and
1621 22b7f6f8 Thomas Thrainer
          netutils.TcpPing(hostname.ip, constants.DEFAULT_NODED_PORT)):
1622 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("IP %s of instance %s already in use" %
1623 22b7f6f8 Thomas Thrainer
                                   (hostname.ip, new_name),
1624 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_NOTUNIQUE)
1625 22b7f6f8 Thomas Thrainer
1626 da4a52a3 Thomas Thrainer
    instance_names = [inst.name for
1627 da4a52a3 Thomas Thrainer
                      inst in self.cfg.GetAllInstancesInfo().values()]
1628 da4a52a3 Thomas Thrainer
    if new_name in instance_names and new_name != instance.name:
1629 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Instance '%s' is already in the cluster" %
1630 22b7f6f8 Thomas Thrainer
                                 new_name, errors.ECODE_EXISTS)
1631 22b7f6f8 Thomas Thrainer
1632 22b7f6f8 Thomas Thrainer
  def Exec(self, feedback_fn):
1633 22b7f6f8 Thomas Thrainer
    """Rename the instance.
1634 22b7f6f8 Thomas Thrainer

1635 22b7f6f8 Thomas Thrainer
    """
1636 d0d7d7cf Thomas Thrainer
    old_name = self.instance.name
1637 22b7f6f8 Thomas Thrainer
1638 22b7f6f8 Thomas Thrainer
    rename_file_storage = False
1639 845b7ed1 Santi Raffa
    if (self.instance.disk_template in (constants.DT_FILE,
1640 845b7ed1 Santi Raffa
                                        constants.DT_SHARED_FILE) and
1641 d0d7d7cf Thomas Thrainer
        self.op.new_name != self.instance.name):
1642 d0d7d7cf Thomas Thrainer
      old_file_storage_dir = os.path.dirname(
1643 d0d7d7cf Thomas Thrainer
                               self.instance.disks[0].logical_id[1])
1644 22b7f6f8 Thomas Thrainer
      rename_file_storage = True
1645 22b7f6f8 Thomas Thrainer
1646 da4a52a3 Thomas Thrainer
    self.cfg.RenameInstance(self.instance.uuid, self.op.new_name)
1647 22b7f6f8 Thomas Thrainer
    # Change the instance lock. This is definitely safe while we hold the BGL.
1648 22b7f6f8 Thomas Thrainer
    # Otherwise the new lock would have to be added in acquired mode.
1649 22b7f6f8 Thomas Thrainer
    assert self.REQ_BGL
1650 22b7f6f8 Thomas Thrainer
    assert locking.BGL in self.owned_locks(locking.LEVEL_CLUSTER)
1651 22b7f6f8 Thomas Thrainer
    self.glm.remove(locking.LEVEL_INSTANCE, old_name)
1652 22b7f6f8 Thomas Thrainer
    self.glm.add(locking.LEVEL_INSTANCE, self.op.new_name)
1653 22b7f6f8 Thomas Thrainer
1654 22b7f6f8 Thomas Thrainer
    # re-read the instance from the configuration after rename
1655 da4a52a3 Thomas Thrainer
    renamed_inst = self.cfg.GetInstanceInfo(self.instance.uuid)
1656 22b7f6f8 Thomas Thrainer
1657 22b7f6f8 Thomas Thrainer
    if rename_file_storage:
1658 d0d7d7cf Thomas Thrainer
      new_file_storage_dir = os.path.dirname(
1659 d0d7d7cf Thomas Thrainer
                               renamed_inst.disks[0].logical_id[1])
1660 d0d7d7cf Thomas Thrainer
      result = self.rpc.call_file_storage_dir_rename(renamed_inst.primary_node,
1661 22b7f6f8 Thomas Thrainer
                                                     old_file_storage_dir,
1662 22b7f6f8 Thomas Thrainer
                                                     new_file_storage_dir)
1663 22b7f6f8 Thomas Thrainer
      result.Raise("Could not rename on node %s directory '%s' to '%s'"
1664 22b7f6f8 Thomas Thrainer
                   " (but the instance has been renamed in Ganeti)" %
1665 d0d7d7cf Thomas Thrainer
                   (self.cfg.GetNodeName(renamed_inst.primary_node),
1666 1c3231aa Thomas Thrainer
                    old_file_storage_dir, new_file_storage_dir))
1667 22b7f6f8 Thomas Thrainer
1668 d0d7d7cf Thomas Thrainer
    StartInstanceDisks(self, renamed_inst, None)
1669 22b7f6f8 Thomas Thrainer
    # update info on disks
1670 d0d7d7cf Thomas Thrainer
    info = GetInstanceInfoText(renamed_inst)
1671 d0d7d7cf Thomas Thrainer
    for (idx, disk) in enumerate(renamed_inst.disks):
1672 d0d7d7cf Thomas Thrainer
      for node_uuid in renamed_inst.all_nodes:
1673 0c3d9c7c Thomas Thrainer
        result = self.rpc.call_blockdev_setinfo(node_uuid,
1674 0c3d9c7c Thomas Thrainer
                                                (disk, renamed_inst), info)
1675 1c3231aa Thomas Thrainer
        result.Warn("Error setting info on node %s for disk %s" %
1676 1c3231aa Thomas Thrainer
                    (self.cfg.GetNodeName(node_uuid), idx), self.LogWarning)
1677 22b7f6f8 Thomas Thrainer
    try:
1678 d0d7d7cf Thomas Thrainer
      result = self.rpc.call_instance_run_rename(renamed_inst.primary_node,
1679 d0d7d7cf Thomas Thrainer
                                                 renamed_inst, old_name,
1680 d0d7d7cf Thomas Thrainer
                                                 self.op.debug_level)
1681 c7dd65be Klaus Aehlig
      result.Warn("Could not run OS rename script for instance %s on node %s"
1682 c7dd65be Klaus Aehlig
                  " (but the instance has been renamed in Ganeti)" %
1683 d0d7d7cf Thomas Thrainer
                  (renamed_inst.name,
1684 d0d7d7cf Thomas Thrainer
                   self.cfg.GetNodeName(renamed_inst.primary_node)),
1685 1c3231aa Thomas Thrainer
                  self.LogWarning)
1686 22b7f6f8 Thomas Thrainer
    finally:
1687 d0d7d7cf Thomas Thrainer
      ShutdownInstanceDisks(self, renamed_inst)
1688 22b7f6f8 Thomas Thrainer
1689 d0d7d7cf Thomas Thrainer
    return renamed_inst.name
1690 22b7f6f8 Thomas Thrainer
1691 22b7f6f8 Thomas Thrainer
1692 22b7f6f8 Thomas Thrainer
class LUInstanceRemove(LogicalUnit):
1693 22b7f6f8 Thomas Thrainer
  """Remove an instance.
1694 22b7f6f8 Thomas Thrainer

1695 22b7f6f8 Thomas Thrainer
  """
1696 22b7f6f8 Thomas Thrainer
  HPATH = "instance-remove"
1697 22b7f6f8 Thomas Thrainer
  HTYPE = constants.HTYPE_INSTANCE
1698 22b7f6f8 Thomas Thrainer
  REQ_BGL = False
1699 22b7f6f8 Thomas Thrainer
1700 22b7f6f8 Thomas Thrainer
  def ExpandNames(self):
1701 22b7f6f8 Thomas Thrainer
    self._ExpandAndLockInstance()
1702 22b7f6f8 Thomas Thrainer
    self.needed_locks[locking.LEVEL_NODE] = []
1703 22b7f6f8 Thomas Thrainer
    self.needed_locks[locking.LEVEL_NODE_RES] = []
1704 22b7f6f8 Thomas Thrainer
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_REPLACE
1705 22b7f6f8 Thomas Thrainer
1706 22b7f6f8 Thomas Thrainer
  def DeclareLocks(self, level):
1707 22b7f6f8 Thomas Thrainer
    if level == locking.LEVEL_NODE:
1708 22b7f6f8 Thomas Thrainer
      self._LockInstancesNodes()
1709 22b7f6f8 Thomas Thrainer
    elif level == locking.LEVEL_NODE_RES:
1710 22b7f6f8 Thomas Thrainer
      # Copy node locks
1711 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_RES] = \
1712 5eacbcae Thomas Thrainer
        CopyLockList(self.needed_locks[locking.LEVEL_NODE])
1713 22b7f6f8 Thomas Thrainer
1714 22b7f6f8 Thomas Thrainer
  def BuildHooksEnv(self):
1715 22b7f6f8 Thomas Thrainer
    """Build hooks env.
1716 22b7f6f8 Thomas Thrainer

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

1719 22b7f6f8 Thomas Thrainer
    """
1720 5eacbcae Thomas Thrainer
    env = BuildInstanceHookEnvByObject(self, self.instance)
1721 22b7f6f8 Thomas Thrainer
    env["SHUTDOWN_TIMEOUT"] = self.op.shutdown_timeout
1722 22b7f6f8 Thomas Thrainer
    return env
1723 22b7f6f8 Thomas Thrainer
1724 22b7f6f8 Thomas Thrainer
  def BuildHooksNodes(self):
1725 22b7f6f8 Thomas Thrainer
    """Build hooks nodes.
1726 22b7f6f8 Thomas Thrainer

1727 22b7f6f8 Thomas Thrainer
    """
1728 22b7f6f8 Thomas Thrainer
    nl = [self.cfg.GetMasterNode()]
1729 22b7f6f8 Thomas Thrainer
    nl_post = list(self.instance.all_nodes) + nl
1730 22b7f6f8 Thomas Thrainer
    return (nl, nl_post)
1731 22b7f6f8 Thomas Thrainer
1732 22b7f6f8 Thomas Thrainer
  def CheckPrereq(self):
1733 22b7f6f8 Thomas Thrainer
    """Check prerequisites.
1734 22b7f6f8 Thomas Thrainer

1735 22b7f6f8 Thomas Thrainer
    This checks that the instance is in the cluster.
1736 22b7f6f8 Thomas Thrainer

1737 22b7f6f8 Thomas Thrainer
    """
1738 da4a52a3 Thomas Thrainer
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_uuid)
1739 22b7f6f8 Thomas Thrainer
    assert self.instance is not None, \
1740 22b7f6f8 Thomas Thrainer
      "Cannot retrieve locked instance %s" % self.op.instance_name
1741 22b7f6f8 Thomas Thrainer
1742 22b7f6f8 Thomas Thrainer
  def Exec(self, feedback_fn):
1743 22b7f6f8 Thomas Thrainer
    """Remove the instance.
1744 22b7f6f8 Thomas Thrainer

1745 22b7f6f8 Thomas Thrainer
    """
1746 d0d7d7cf Thomas Thrainer
    logging.info("Shutting down instance %s on node %s", self.instance.name,
1747 d0d7d7cf Thomas Thrainer
                 self.cfg.GetNodeName(self.instance.primary_node))
1748 22b7f6f8 Thomas Thrainer
1749 d0d7d7cf Thomas Thrainer
    result = self.rpc.call_instance_shutdown(self.instance.primary_node,
1750 d0d7d7cf Thomas Thrainer
                                             self.instance,
1751 22b7f6f8 Thomas Thrainer
                                             self.op.shutdown_timeout,
1752 22b7f6f8 Thomas Thrainer
                                             self.op.reason)
1753 c7dd65be Klaus Aehlig
    if self.op.ignore_failures:
1754 c7dd65be Klaus Aehlig
      result.Warn("Warning: can't shutdown instance", feedback_fn)
1755 c7dd65be Klaus Aehlig
    else:
1756 c7dd65be Klaus Aehlig
      result.Raise("Could not shutdown instance %s on node %s" %
1757 d0d7d7cf Thomas Thrainer
                   (self.instance.name,
1758 d0d7d7cf Thomas Thrainer
                    self.cfg.GetNodeName(self.instance.primary_node)))
1759 22b7f6f8 Thomas Thrainer
1760 22b7f6f8 Thomas Thrainer
    assert (self.owned_locks(locking.LEVEL_NODE) ==
1761 22b7f6f8 Thomas Thrainer
            self.owned_locks(locking.LEVEL_NODE_RES))
1762 d0d7d7cf Thomas Thrainer
    assert not (set(self.instance.all_nodes) -
1763 22b7f6f8 Thomas Thrainer
                self.owned_locks(locking.LEVEL_NODE)), \
1764 22b7f6f8 Thomas Thrainer
      "Not owning correct locks"
1765 22b7f6f8 Thomas Thrainer
1766 d0d7d7cf Thomas Thrainer
    RemoveInstance(self, feedback_fn, self.instance, self.op.ignore_failures)
1767 22b7f6f8 Thomas Thrainer
1768 22b7f6f8 Thomas Thrainer
1769 22b7f6f8 Thomas Thrainer
class LUInstanceMove(LogicalUnit):
1770 22b7f6f8 Thomas Thrainer
  """Move an instance by data-copying.
1771 22b7f6f8 Thomas Thrainer

1772 22b7f6f8 Thomas Thrainer
  """
1773 22b7f6f8 Thomas Thrainer
  HPATH = "instance-move"
1774 22b7f6f8 Thomas Thrainer
  HTYPE = constants.HTYPE_INSTANCE
1775 22b7f6f8 Thomas Thrainer
  REQ_BGL = False
1776 22b7f6f8 Thomas Thrainer
1777 22b7f6f8 Thomas Thrainer
  def ExpandNames(self):
1778 22b7f6f8 Thomas Thrainer
    self._ExpandAndLockInstance()
1779 1c3231aa Thomas Thrainer
    (self.op.target_node_uuid, self.op.target_node) = \
1780 1c3231aa Thomas Thrainer
      ExpandNodeUuidAndName(self.cfg, self.op.target_node_uuid,
1781 1c3231aa Thomas Thrainer
                            self.op.target_node)
1782 b9aae98b Thomas Thrainer
    self.needed_locks[locking.LEVEL_NODE] = [self.op.target_node_uuid]
1783 22b7f6f8 Thomas Thrainer
    self.needed_locks[locking.LEVEL_NODE_RES] = []
1784 22b7f6f8 Thomas Thrainer
    self.recalculate_locks[locking.LEVEL_NODE] = constants.LOCKS_APPEND
1785 22b7f6f8 Thomas Thrainer
1786 22b7f6f8 Thomas Thrainer
  def DeclareLocks(self, level):
1787 22b7f6f8 Thomas Thrainer
    if level == locking.LEVEL_NODE:
1788 22b7f6f8 Thomas Thrainer
      self._LockInstancesNodes(primary_only=True)
1789 22b7f6f8 Thomas Thrainer
    elif level == locking.LEVEL_NODE_RES:
1790 22b7f6f8 Thomas Thrainer
      # Copy node locks
1791 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_RES] = \
1792 5eacbcae Thomas Thrainer
        CopyLockList(self.needed_locks[locking.LEVEL_NODE])
1793 22b7f6f8 Thomas Thrainer
1794 22b7f6f8 Thomas Thrainer
  def BuildHooksEnv(self):
1795 22b7f6f8 Thomas Thrainer
    """Build hooks env.
1796 22b7f6f8 Thomas Thrainer

1797 83266db6 Thomas Thrainer
    This runs on master, primary and target nodes of the instance.
1798 22b7f6f8 Thomas Thrainer

1799 22b7f6f8 Thomas Thrainer
    """
1800 22b7f6f8 Thomas Thrainer
    env = {
1801 22b7f6f8 Thomas Thrainer
      "TARGET_NODE": self.op.target_node,
1802 22b7f6f8 Thomas Thrainer
      "SHUTDOWN_TIMEOUT": self.op.shutdown_timeout,
1803 22b7f6f8 Thomas Thrainer
      }
1804 5eacbcae Thomas Thrainer
    env.update(BuildInstanceHookEnvByObject(self, self.instance))
1805 22b7f6f8 Thomas Thrainer
    return env
1806 22b7f6f8 Thomas Thrainer
1807 22b7f6f8 Thomas Thrainer
  def BuildHooksNodes(self):
1808 22b7f6f8 Thomas Thrainer
    """Build hooks nodes.
1809 22b7f6f8 Thomas Thrainer

1810 22b7f6f8 Thomas Thrainer
    """
1811 22b7f6f8 Thomas Thrainer
    nl = [
1812 22b7f6f8 Thomas Thrainer
      self.cfg.GetMasterNode(),
1813 22b7f6f8 Thomas Thrainer
      self.instance.primary_node,
1814 1c3231aa Thomas Thrainer
      self.op.target_node_uuid,
1815 22b7f6f8 Thomas Thrainer
      ]
1816 22b7f6f8 Thomas Thrainer
    return (nl, nl)
1817 22b7f6f8 Thomas Thrainer
1818 22b7f6f8 Thomas Thrainer
  def CheckPrereq(self):
1819 22b7f6f8 Thomas Thrainer
    """Check prerequisites.
1820 22b7f6f8 Thomas Thrainer

1821 22b7f6f8 Thomas Thrainer
    This checks that the instance is in the cluster.
1822 22b7f6f8 Thomas Thrainer

1823 22b7f6f8 Thomas Thrainer
    """
1824 da4a52a3 Thomas Thrainer
    self.instance = self.cfg.GetInstanceInfo(self.op.instance_uuid)
1825 22b7f6f8 Thomas Thrainer
    assert self.instance is not None, \
1826 22b7f6f8 Thomas Thrainer
      "Cannot retrieve locked instance %s" % self.op.instance_name
1827 22b7f6f8 Thomas Thrainer
1828 d0d7d7cf Thomas Thrainer
    if self.instance.disk_template not in constants.DTS_COPYABLE:
1829 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Disk template %s not suitable for copying" %
1830 d0d7d7cf Thomas Thrainer
                                 self.instance.disk_template,
1831 d0d7d7cf Thomas Thrainer
                                 errors.ECODE_STATE)
1832 22b7f6f8 Thomas Thrainer
1833 1c3231aa Thomas Thrainer
    target_node = self.cfg.GetNodeInfo(self.op.target_node_uuid)
1834 1c3231aa Thomas Thrainer
    assert target_node is not None, \
1835 22b7f6f8 Thomas Thrainer
      "Cannot retrieve locked node %s" % self.op.target_node
1836 22b7f6f8 Thomas Thrainer
1837 1c3231aa Thomas Thrainer
    self.target_node_uuid = target_node.uuid
1838 d0d7d7cf Thomas Thrainer
    if target_node.uuid == self.instance.primary_node:
1839 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("Instance %s is already on the node %s" %
1840 d0d7d7cf Thomas Thrainer
                                 (self.instance.name, target_node.name),
1841 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_STATE)
1842 22b7f6f8 Thomas Thrainer
1843 d29f52a6 Thomas Thrainer
    cluster = self.cfg.GetClusterInfo()
1844 d29f52a6 Thomas Thrainer
    bep = cluster.FillBE(self.instance)
1845 22b7f6f8 Thomas Thrainer
1846 d0d7d7cf Thomas Thrainer
    for idx, dsk in enumerate(self.instance.disks):
1847 cd3b4ff4 Helga Velroyen
      if dsk.dev_type not in (constants.DT_PLAIN, constants.DT_FILE,
1848 8106dd64 Santi Raffa
                              constants.DT_SHARED_FILE, constants.DT_GLUSTER):
1849 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("Instance disk %d has a complex layout,"
1850 22b7f6f8 Thomas Thrainer
                                   " cannot copy" % idx, errors.ECODE_STATE)
1851 22b7f6f8 Thomas Thrainer
1852 1c3231aa Thomas Thrainer
    CheckNodeOnline(self, target_node.uuid)
1853 1c3231aa Thomas Thrainer
    CheckNodeNotDrained(self, target_node.uuid)
1854 1c3231aa Thomas Thrainer
    CheckNodeVmCapable(self, target_node.uuid)
1855 1c3231aa Thomas Thrainer
    group_info = self.cfg.GetNodeGroup(target_node.group)
1856 22b7f6f8 Thomas Thrainer
    ipolicy = ganeti.masterd.instance.CalculateGroupIPolicy(cluster, group_info)
1857 d0d7d7cf Thomas Thrainer
    CheckTargetNodeIPolicy(self, ipolicy, self.instance, target_node, self.cfg,
1858 5eacbcae Thomas Thrainer
                           ignore=self.op.ignore_ipolicy)
1859 22b7f6f8 Thomas Thrainer
1860 d0d7d7cf Thomas Thrainer
    if self.instance.admin_state == constants.ADMINST_UP:
1861 83266db6 Thomas Thrainer
      # check memory requirements on the target node
1862 a295eb80 Helga Velroyen
      CheckNodeFreeMemory(
1863 1c3231aa Thomas Thrainer
          self, target_node.uuid, "failing over instance %s" %
1864 d0d7d7cf Thomas Thrainer
          self.instance.name, bep[constants.BE_MAXMEM],
1865 d0d7d7cf Thomas Thrainer
          self.instance.hypervisor,
1866 d29f52a6 Thomas Thrainer
          cluster.hvparams[self.instance.hypervisor])
1867 22b7f6f8 Thomas Thrainer
    else:
1868 22b7f6f8 Thomas Thrainer
      self.LogInfo("Not checking memory on the secondary node as"
1869 22b7f6f8 Thomas Thrainer
                   " instance will not be started")
1870 22b7f6f8 Thomas Thrainer
1871 22b7f6f8 Thomas Thrainer
    # check bridge existance
1872 d0d7d7cf Thomas Thrainer
    CheckInstanceBridgesExist(self, self.instance, node_uuid=target_node.uuid)
1873 22b7f6f8 Thomas Thrainer
1874 22b7f6f8 Thomas Thrainer
  def Exec(self, feedback_fn):
1875 22b7f6f8 Thomas Thrainer
    """Move an instance.
1876 22b7f6f8 Thomas Thrainer

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

1880 22b7f6f8 Thomas Thrainer
    """
1881 d0d7d7cf Thomas Thrainer
    source_node = self.cfg.GetNodeInfo(self.instance.primary_node)
1882 1c3231aa Thomas Thrainer
    target_node = self.cfg.GetNodeInfo(self.target_node_uuid)
1883 22b7f6f8 Thomas Thrainer
1884 22b7f6f8 Thomas Thrainer
    self.LogInfo("Shutting down instance %s on source node %s",
1885 d0d7d7cf Thomas Thrainer
                 self.instance.name, source_node.name)
1886 22b7f6f8 Thomas Thrainer
1887 22b7f6f8 Thomas Thrainer
    assert (self.owned_locks(locking.LEVEL_NODE) ==
1888 22b7f6f8 Thomas Thrainer
            self.owned_locks(locking.LEVEL_NODE_RES))
1889 22b7f6f8 Thomas Thrainer
1890 d0d7d7cf Thomas Thrainer
    result = self.rpc.call_instance_shutdown(source_node.uuid, self.instance,
1891 22b7f6f8 Thomas Thrainer
                                             self.op.shutdown_timeout,
1892 22b7f6f8 Thomas Thrainer
                                             self.op.reason)
1893 c7dd65be Klaus Aehlig
    if self.op.ignore_consistency:
1894 c7dd65be Klaus Aehlig
      result.Warn("Could not shutdown instance %s on node %s. Proceeding"
1895 c7dd65be Klaus Aehlig
                  " anyway. Please make sure node %s is down. Error details" %
1896 d0d7d7cf Thomas Thrainer
                  (self.instance.name, source_node.name, source_node.name),
1897 1c3231aa Thomas Thrainer
                  self.LogWarning)
1898 c7dd65be Klaus Aehlig
    else:
1899 c7dd65be Klaus Aehlig
      result.Raise("Could not shutdown instance %s on node %s" %
1900 d0d7d7cf Thomas Thrainer
                   (self.instance.name, source_node.name))
1901 22b7f6f8 Thomas Thrainer
1902 22b7f6f8 Thomas Thrainer
    # create the target disks
1903 22b7f6f8 Thomas Thrainer
    try:
1904 d0d7d7cf Thomas Thrainer
      CreateDisks(self, self.instance, target_node_uuid=target_node.uuid)
1905 22b7f6f8 Thomas Thrainer
    except errors.OpExecError:
1906 22b7f6f8 Thomas Thrainer
      self.LogWarning("Device creation failed")
1907 da4a52a3 Thomas Thrainer
      self.cfg.ReleaseDRBDMinors(self.instance.uuid)
1908 22b7f6f8 Thomas Thrainer
      raise
1909 22b7f6f8 Thomas Thrainer
1910 22b7f6f8 Thomas Thrainer
    errs = []
1911 063613aa Thomas Thrainer
    transfers = []
1912 063613aa Thomas Thrainer
    # activate, get path, create transfer jobs
1913 d0d7d7cf Thomas Thrainer
    for idx, disk in enumerate(self.instance.disks):
1914 063613aa Thomas Thrainer
      # FIXME: pass debug option from opcode to backend
1915 063613aa Thomas Thrainer
      dt = masterd.instance.DiskTransfer("disk/%s" % idx,
1916 063613aa Thomas Thrainer
                                         constants.IEIO_RAW_DISK,
1917 063613aa Thomas Thrainer
                                         (disk, self.instance),
1918 063613aa Thomas Thrainer
                                         constants.IEIO_RAW_DISK,
1919 063613aa Thomas Thrainer
                                         (disk, self.instance),
1920 063613aa Thomas Thrainer
                                         None)
1921 063613aa Thomas Thrainer
      transfers.append(dt)
1922 063613aa Thomas Thrainer
1923 063613aa Thomas Thrainer
    import_result = \
1924 063613aa Thomas Thrainer
      masterd.instance.TransferInstanceData(self, feedback_fn,
1925 063613aa Thomas Thrainer
                                            source_node.uuid,
1926 063613aa Thomas Thrainer
                                            target_node.uuid,
1927 063613aa Thomas Thrainer
                                            target_node.secondary_ip,
1928 f198cf91 Thomas Thrainer
                                            self.op.compress,
1929 063613aa Thomas Thrainer
                                            self.instance, transfers)
1930 063613aa Thomas Thrainer
    if not compat.all(import_result):
1931 063613aa Thomas Thrainer
      errs.append("Failed to transfer instance data")
1932 22b7f6f8 Thomas Thrainer
1933 22b7f6f8 Thomas Thrainer
    if errs:
1934 22b7f6f8 Thomas Thrainer
      self.LogWarning("Some disks failed to copy, aborting")
1935 22b7f6f8 Thomas Thrainer
      try:
1936 d0d7d7cf Thomas Thrainer
        RemoveDisks(self, self.instance, target_node_uuid=target_node.uuid)
1937 22b7f6f8 Thomas Thrainer
      finally:
1938 da4a52a3 Thomas Thrainer
        self.cfg.ReleaseDRBDMinors(self.instance.uuid)
1939 22b7f6f8 Thomas Thrainer
        raise errors.OpExecError("Errors during disk copy: %s" %
1940 22b7f6f8 Thomas Thrainer
                                 (",".join(errs),))
1941 22b7f6f8 Thomas Thrainer
1942 d0d7d7cf Thomas Thrainer
    self.instance.primary_node = target_node.uuid
1943 d0d7d7cf Thomas Thrainer
    self.cfg.Update(self.instance, feedback_fn)
1944 22b7f6f8 Thomas Thrainer
1945 22b7f6f8 Thomas Thrainer
    self.LogInfo("Removing the disks on the original node")
1946 d0d7d7cf Thomas Thrainer
    RemoveDisks(self, self.instance, target_node_uuid=source_node.uuid)
1947 22b7f6f8 Thomas Thrainer
1948 22b7f6f8 Thomas Thrainer
    # Only start the instance if it's marked as up
1949 d0d7d7cf Thomas Thrainer
    if self.instance.admin_state == constants.ADMINST_UP:
1950 22b7f6f8 Thomas Thrainer
      self.LogInfo("Starting instance %s on node %s",
1951 d0d7d7cf Thomas Thrainer
                   self.instance.name, target_node.name)
1952 22b7f6f8 Thomas Thrainer
1953 d0d7d7cf Thomas Thrainer
      disks_ok, _ = AssembleInstanceDisks(self, self.instance,
1954 5eacbcae Thomas Thrainer
                                          ignore_secondaries=True)
1955 22b7f6f8 Thomas Thrainer
      if not disks_ok:
1956 d0d7d7cf Thomas Thrainer
        ShutdownInstanceDisks(self, self.instance)
1957 22b7f6f8 Thomas Thrainer
        raise errors.OpExecError("Can't activate the instance's disks")
1958 22b7f6f8 Thomas Thrainer
1959 1c3231aa Thomas Thrainer
      result = self.rpc.call_instance_start(target_node.uuid,
1960 d0d7d7cf Thomas Thrainer
                                            (self.instance, None, None), False,
1961 22b7f6f8 Thomas Thrainer
                                            self.op.reason)
1962 22b7f6f8 Thomas Thrainer
      msg = result.fail_msg
1963 22b7f6f8 Thomas Thrainer
      if msg:
1964 d0d7d7cf Thomas Thrainer
        ShutdownInstanceDisks(self, self.instance)
1965 22b7f6f8 Thomas Thrainer
        raise errors.OpExecError("Could not start instance %s on node %s: %s" %
1966 d0d7d7cf Thomas Thrainer
                                 (self.instance.name, target_node.name, msg))
1967 22b7f6f8 Thomas Thrainer
1968 22b7f6f8 Thomas Thrainer
1969 22b7f6f8 Thomas Thrainer
class LUInstanceMultiAlloc(NoHooksLU):
1970 22b7f6f8 Thomas Thrainer
  """Allocates multiple instances at the same time.
1971 22b7f6f8 Thomas Thrainer

1972 22b7f6f8 Thomas Thrainer
  """
1973 22b7f6f8 Thomas Thrainer
  REQ_BGL = False
1974 22b7f6f8 Thomas Thrainer
1975 22b7f6f8 Thomas Thrainer
  def CheckArguments(self):
1976 22b7f6f8 Thomas Thrainer
    """Check arguments.
1977 22b7f6f8 Thomas Thrainer

1978 22b7f6f8 Thomas Thrainer
    """
1979 22b7f6f8 Thomas Thrainer
    nodes = []
1980 22b7f6f8 Thomas Thrainer
    for inst in self.op.instances:
1981 22b7f6f8 Thomas Thrainer
      if inst.iallocator is not None:
1982 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("iallocator are not allowed to be set on"
1983 22b7f6f8 Thomas Thrainer
                                   " instance objects", errors.ECODE_INVAL)
1984 22b7f6f8 Thomas Thrainer
      nodes.append(bool(inst.pnode))
1985 22b7f6f8 Thomas Thrainer
      if inst.disk_template in constants.DTS_INT_MIRROR:
1986 22b7f6f8 Thomas Thrainer
        nodes.append(bool(inst.snode))
1987 22b7f6f8 Thomas Thrainer
1988 22b7f6f8 Thomas Thrainer
    has_nodes = compat.any(nodes)
1989 22b7f6f8 Thomas Thrainer
    if compat.all(nodes) ^ has_nodes:
1990 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("There are instance objects providing"
1991 22b7f6f8 Thomas Thrainer
                                 " pnode/snode while others do not",
1992 22b7f6f8 Thomas Thrainer
                                 errors.ECODE_INVAL)
1993 22b7f6f8 Thomas Thrainer
1994 0c072225 Thomas Thrainer
    if not has_nodes and self.op.iallocator is None:
1995 22b7f6f8 Thomas Thrainer
      default_iallocator = self.cfg.GetDefaultIAllocator()
1996 0c072225 Thomas Thrainer
      if default_iallocator:
1997 22b7f6f8 Thomas Thrainer
        self.op.iallocator = default_iallocator
1998 22b7f6f8 Thomas Thrainer
      else:
1999 22b7f6f8 Thomas Thrainer
        raise errors.OpPrereqError("No iallocator or nodes on the instances"
2000 22b7f6f8 Thomas Thrainer
                                   " given and no cluster-wide default"
2001 22b7f6f8 Thomas Thrainer
                                   " iallocator found; please specify either"
2002 22b7f6f8 Thomas Thrainer
                                   " an iallocator or nodes on the instances"
2003 22b7f6f8 Thomas Thrainer
                                   " or set a cluster-wide default iallocator",
2004 22b7f6f8 Thomas Thrainer
                                   errors.ECODE_INVAL)
2005 22b7f6f8 Thomas Thrainer
2006 22b7f6f8 Thomas Thrainer
    _CheckOpportunisticLocking(self.op)
2007 22b7f6f8 Thomas Thrainer
2008 22b7f6f8 Thomas Thrainer
    dups = utils.FindDuplicates([op.instance_name for op in self.op.instances])
2009 22b7f6f8 Thomas Thrainer
    if dups:
2010 22b7f6f8 Thomas Thrainer
      raise errors.OpPrereqError("There are duplicate instance names: %s" %
2011 22b7f6f8 Thomas Thrainer
                                 utils.CommaJoin(dups), errors.ECODE_INVAL)
2012 22b7f6f8 Thomas Thrainer
2013 22b7f6f8 Thomas Thrainer
  def ExpandNames(self):
2014 22b7f6f8 Thomas Thrainer
    """Calculate the locks.
2015 22b7f6f8 Thomas Thrainer

2016 22b7f6f8 Thomas Thrainer
    """
2017 5eacbcae Thomas Thrainer
    self.share_locks = ShareAll()
2018 22b7f6f8 Thomas Thrainer
    self.needed_locks = {
2019 22b7f6f8 Thomas Thrainer
      # iallocator will select nodes and even if no iallocator is used,
2020 22b7f6f8 Thomas Thrainer
      # collisions with LUInstanceCreate should be avoided
2021 22b7f6f8 Thomas Thrainer
      locking.LEVEL_NODE_ALLOC: locking.ALL_SET,
2022 22b7f6f8 Thomas Thrainer
      }
2023 22b7f6f8 Thomas Thrainer
2024 22b7f6f8 Thomas Thrainer
    if self.op.iallocator:
2025 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE] = locking.ALL_SET
2026 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_RES] = locking.ALL_SET
2027 22b7f6f8 Thomas Thrainer
2028 22b7f6f8 Thomas Thrainer
      if self.op.opportunistic_locking:
2029 22b7f6f8 Thomas Thrainer
        self.opportunistic_locks[locking.LEVEL_NODE] = True
2030 22b7f6f8 Thomas Thrainer
    else:
2031 22b7f6f8 Thomas Thrainer
      nodeslist = []
2032 22b7f6f8 Thomas Thrainer
      for inst in self.op.instances:
2033 1c3231aa Thomas Thrainer
        (inst.pnode_uuid, inst.pnode) = \
2034 1c3231aa Thomas Thrainer
          ExpandNodeUuidAndName(self.cfg, inst.pnode_uuid, inst.pnode)
2035 6869f673 Thomas Thrainer
        nodeslist.append(inst.pnode_uuid)
2036 22b7f6f8 Thomas Thrainer
        if inst.snode is not None:
2037 1c3231aa Thomas Thrainer
          (inst.snode_uuid, inst.snode) = \
2038 1c3231aa Thomas Thrainer
            ExpandNodeUuidAndName(self.cfg, inst.snode_uuid, inst.snode)
2039 6869f673 Thomas Thrainer
          nodeslist.append(inst.snode_uuid)
2040 22b7f6f8 Thomas Thrainer
2041 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE] = nodeslist
2042 22b7f6f8 Thomas Thrainer
      # Lock resources of instance's primary and secondary nodes (copy to
2043 22b7f6f8 Thomas Thrainer
      # prevent accidential modification)
2044 22b7f6f8 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_RES] = list(nodeslist)
2045 22b7f6f8 Thomas Thrainer
2046 4289f617 Thomas Thrainer
  def DeclareLocks(self, level):
2047 4289f617 Thomas Thrainer
    if level == locking.LEVEL_NODE_RES and \
2048 4289f617 Thomas Thrainer
      self.opportunistic_locks[locking.LEVEL_NODE]:
2049 4289f617 Thomas Thrainer
      # Even when using opportunistic locking, we require the same set of
2050 4289f617 Thomas Thrainer
      # NODE_RES locks as we got NODE locks
2051 4289f617 Thomas Thrainer
      self.needed_locks[locking.LEVEL_NODE_RES] = \
2052 4289f617 Thomas Thrainer
        self.owned_locks(locking.LEVEL_NODE)
2053 4289f617 Thomas Thrainer
2054 22b7f6f8 Thomas Thrainer
  def CheckPrereq(self):
2055 22b7f6f8 Thomas Thrainer
    """Check prerequisite.
2056 22b7f6f8 Thomas Thrainer

2057 22b7f6f8 Thomas Thrainer
    """
2058 0c072225 Thomas Thrainer
    if self.op.iallocator:
2059 0c072225 Thomas Thrainer
      cluster = self.cfg.GetClusterInfo()
2060 0c072225 Thomas Thrainer
      default_vg = self.cfg.GetVGName()
2061 0c072225 Thomas Thrainer
      ec_id = self.proc.GetECId()
2062 22b7f6f8 Thomas Thrainer
2063 0c072225 Thomas Thrainer
      if self.op.opportunistic_locking:
2064 0c072225 Thomas Thrainer
        # Only consider nodes for which a lock is held
2065 804d72eb Thomas Thrainer
        node_whitelist =