Revision 6c0a75db lib/cmdlib.py

b/lib/cmdlib.py
40 40
import shutil
41 41
import itertools
42 42
import operator
43
import ipaddr
43 44

  
44 45
from ganeti import ssh
45 46
from ganeti import utils
......
61 62
from ganeti import runtime
62 63
from ganeti import pathutils
63 64
from ganeti import vcluster
65
from ganeti import network
64 66
from ganeti.masterd import iallocator
65 67

  
66 68
import ganeti.masterd.instance # pylint: disable=W0611
......
15303 15305

  
15304 15306
# Network LUs
15305 15307
class LUNetworkAdd(LogicalUnit):
15308
  """Logical unit for creating networks.
15309

  
15310
  """
15311
  HPATH = "network-add"
15312
  HTYPE = constants.HTYPE_NETWORK
15313
  REQ_BGL = False
15314

  
15306 15315
  def BuildHooksNodes(self):
15307
    pass
15316
    """Build hooks nodes.
15317

  
15318
    """
15319
    mn = self.cfg.GetMasterNode()
15320
    return ([mn], [mn])
15321

  
15322
  def ExpandNames(self):
15323
    self.network_uuid = self.cfg.GenerateUniqueID(self.proc.GetECId())
15324
    self.needed_locks = {}
15325
    self.add_locks[locking.LEVEL_NETWORK] = self.network_uuid
15326

  
15327
  def CheckPrereq(self):
15328
    """Check prerequisites.
15329

  
15330
    This checks that the given group name is not an existing node group
15331
    already.
15332

  
15333
    """
15334
    if self.op.network is None:
15335
      raise errors.OpPrereqError("Network must be given",
15336
                                 errors.ECODE_INVAL)
15337

  
15338
    uuid = self.cfg.LookupNetwork(self.op.network_name)
15339

  
15340
    if uuid:
15341
      raise errors.OpPrereqError("Network '%s' already defined" %
15342
                                 self.op.network, errors.ECODE_EXISTS)
15343

  
15308 15344

  
15309 15345
  def BuildHooksEnv(self):
15310
    pass
15346
    """Build hooks env.
15347

  
15348
    """
15349
    env = {
15350
      "NETWORK_NAME": self.op.network_name,
15351
      "NETWORK_SUBNET": self.op.network,
15352
      "NETWORK_GATEWAY": self.op.gateway,
15353
      "NETWORK_SUBNET6": self.op.network6,
15354
      "NETWORK_GATEWAY6": self.op.gateway6,
15355
      "NETWORK_MAC_PREFIX": self.op.mac_prefix,
15356
      "NETWORK_TYPE": self.op.network_type,
15357
      }
15358
    return env
15359

  
15360
  def Exec(self, feedback_fn):
15361
    """Add the ip pool to the cluster.
15362

  
15363
    """
15364
    nobj = objects.Network(name=self.op.network_name,
15365
                           network=self.op.network,
15366
                           gateway=self.op.gateway,
15367
                           network6=self.op.network6,
15368
                           gateway6=self.op.gateway6,
15369
                           mac_prefix=self.op.mac_prefix,
15370
                           network_type=self.op.network_type,
15371
                           uuid=self.network_uuid,
15372
                           family=4)
15373
    # Initialize the associated address pool
15374
    try:
15375
      pool = network.AddressPool.InitializeNetwork(nobj)
15376
    except errors.AddressPoolError, e:
15377
      raise errors.OpExecError("Cannot create IP pool for this network. %s" % e)
15378

  
15379
    # Check if we need to reserve the nodes and the cluster master IP
15380
    # These may not be allocated to any instances in routed mode, as
15381
    # they wouldn't function anyway.
15382
    for node in self.cfg.GetAllNodesInfo().values():
15383
      for ip in [node.primary_ip, node.secondary_ip]:
15384
        try:
15385
          pool.Reserve(ip)
15386
          self.LogInfo("Reserved node %s's IP (%s)", node.name, ip)
15387

  
15388
        except errors.AddressPoolError:
15389
          pass
15390

  
15391
    master_ip = self.cfg.GetClusterInfo().master_ip
15392
    try:
15393
      pool.Reserve(master_ip)
15394
      self.LogInfo("Reserved cluster master IP (%s)", master_ip)
15395
    except errors.AddressPoolError:
15396
      pass
15397

  
15398
    if self.op.add_reserved_ips:
15399
      for ip in self.op.add_reserved_ips:
15400
        try:
15401
          pool.Reserve(ip, external=True)
15402
        except errors.AddressPoolError, e:
15403
          raise errors.OpExecError("Cannot reserve IP %s. %s " % (ip, e))
15404

  
15405
    self.cfg.AddNetwork(nobj, self.proc.GetECId(), check_uuid=False)
15406
    del self.remove_locks[locking.LEVEL_NETWORK]
15311 15407

  
15312 15408

  
15313 15409
class LUNetworkRemove(LogicalUnit):
15314
  def BuildHooksNodes(self):
15315
    pass
15410
  HPATH = "network-remove"
15411
  HTYPE = constants.HTYPE_NETWORK
15412
  REQ_BGL = False
15413

  
15414
  def ExpandNames(self):
15415
    self.network_uuid = self.cfg.LookupNetwork(self.op.network_name)
15416

  
15417
    self.needed_locks = {
15418
      locking.LEVEL_NETWORK: [self.network_uuid],
15419
      }
15420

  
15421

  
15422
  def CheckPrereq(self):
15423
    """Check prerequisites.
15424

  
15425
    This checks that the given network name exists as a network, that is
15426
    empty (i.e., contains no nodes), and that is not the last group of the
15427
    cluster.
15428

  
15429
    """
15430
    if not self.network_uuid:
15431
      raise errors.OpPrereqError("Network %s not found" % self.op.network_name,
15432
                                 errors.ECODE_INVAL)
15433

  
15434
    # Verify that the network is not conncted.
15435
    node_groups = [group.name
15436
                   for group in self.cfg.GetAllNodeGroupsInfo().values()
15437
                   for network in group.networks.keys()
15438
                   if network == self.network_uuid]
15439

  
15440
    if node_groups:
15441
      self.LogWarning("Nework '%s' is connected to the following"
15442
                      " node groups: %s" % (self.op.network_name,
15443
                      utils.CommaJoin(utils.NiceSort(node_groups))))
15444
      raise errors.OpPrereqError("Network still connected",
15445
                                 errors.ECODE_STATE)
15316 15446

  
15317 15447
  def BuildHooksEnv(self):
15318
    pass
15448
    """Build hooks env.
15449

  
15450
    """
15451
    return {
15452
      "NETWORK_NAME": self.op.network_name,
15453
      }
15454

  
15455
  def BuildHooksNodes(self):
15456
    """Build hooks nodes.
15457

  
15458
    """
15459
    mn = self.cfg.GetMasterNode()
15460
    return ([mn], [mn])
15461

  
15462
  def Exec(self, feedback_fn):
15463
    """Remove the network.
15464

  
15465
    """
15466
    try:
15467
      self.cfg.RemoveNetwork(self.network_uuid)
15468
    except errors.ConfigurationError:
15469
      raise errors.OpExecError("Network '%s' with UUID %s disappeared" %
15470
                               (self.op.network_name, self.network_uuid))
15319 15471

  
15320 15472

  
15321 15473
class LUNetworkSetParams(LogicalUnit):

Also available in: Unified diff