Revision b246cea8 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
......
15178 15180

  
15179 15181
# Network LUs
15180 15182
class LUNetworkAdd(LogicalUnit):
15183
  """Logical unit for creating networks.
15184

  
15185
  """
15186
  HPATH = "network-add"
15187
  HTYPE = constants.HTYPE_NETWORK
15188
  REQ_BGL = False
15189

  
15181 15190
  def BuildHooksNodes(self):
15182
    pass
15191
    """Build hooks nodes.
15192

  
15193
    """
15194
    mn = self.cfg.GetMasterNode()
15195
    return ([mn], [mn])
15196

  
15197
  def ExpandNames(self):
15198
    self.network_uuid = self.cfg.GenerateUniqueID(self.proc.GetECId())
15199
    self.needed_locks = {}
15200
    self.add_locks[locking.LEVEL_NETWORK] = self.network_uuid
15201

  
15202
  def CheckPrereq(self):
15203
    """Check prerequisites.
15204

  
15205
    This checks that the given group name is not an existing node group
15206
    already.
15207

  
15208
    """
15209
    if self.op.network is None:
15210
      raise errors.OpPrereqError("Network must be given",
15211
                                 errors.ECODE_INVAL)
15212

  
15213
    uuid = self.cfg.LookupNetwork(self.op.network_name)
15214

  
15215
    if uuid:
15216
      raise errors.OpPrereqError("Network '%s' already defined" %
15217
                                 self.op.network, errors.ECODE_EXISTS)
15218

  
15183 15219

  
15184 15220
  def BuildHooksEnv(self):
15185
    pass
15221
    """Build hooks env.
15222

  
15223
    """
15224
    env = {
15225
      "NETWORK_NAME": self.op.network_name,
15226
      "NETWORK_SUBNET": self.op.network,
15227
      "NETWORK_GATEWAY": self.op.gateway,
15228
      "NETWORK_SUBNET6": self.op.network6,
15229
      "NETWORK_GATEWAY6": self.op.gateway6,
15230
      "NETWORK_MAC_PREFIX": self.op.mac_prefix,
15231
      "NETWORK_TYPE": self.op.network_type,
15232
      }
15233
    return env
15234

  
15235
  def Exec(self, feedback_fn):
15236
    """Add the ip pool to the cluster.
15237

  
15238
    """
15239
    nobj = objects.Network(name=self.op.network_name,
15240
                           network=self.op.network,
15241
                           gateway=self.op.gateway,
15242
                           network6=self.op.network6,
15243
                           gateway6=self.op.gateway6,
15244
                           mac_prefix=self.op.mac_prefix,
15245
                           network_type=self.op.network_type,
15246
                           uuid=self.network_uuid,
15247
                           family=4)
15248
    # Initialize the associated address pool
15249
    try:
15250
      pool = network.AddressPool.InitializeNetwork(nobj)
15251
    except errors.AddressPoolError, e:
15252
      raise errors.OpExecError("Cannot create IP pool for this network. %s" % e)
15253

  
15254
    # Check if we need to reserve the nodes and the cluster master IP
15255
    # These may not be allocated to any instances in routed mode, as
15256
    # they wouldn't function anyway.
15257
    for node in self.cfg.GetAllNodesInfo().values():
15258
      for ip in [node.primary_ip, node.secondary_ip]:
15259
        try:
15260
          pool.Reserve(ip)
15261
          self.LogInfo("Reserved node %s's IP (%s)", node.name, ip)
15262

  
15263
        except errors.AddressPoolError:
15264
          pass
15265

  
15266
    master_ip = self.cfg.GetClusterInfo().master_ip
15267
    try:
15268
      pool.Reserve(master_ip)
15269
      self.LogInfo("Reserved cluster master IP (%s)", master_ip)
15270
    except errors.AddressPoolError:
15271
      pass
15272

  
15273
    if self.op.add_reserved_ips:
15274
      for ip in self.op.add_reserved_ips:
15275
        try:
15276
          pool.Reserve(ip, external=True)
15277
        except errors.AddressPoolError, e:
15278
          raise errors.OpExecError("Cannot reserve IP %s. %s " % (ip, e))
15279

  
15280
    self.cfg.AddNetwork(nobj, self.proc.GetECId(), check_uuid=False)
15281
    del self.remove_locks[locking.LEVEL_NETWORK]
15186 15282

  
15187 15283

  
15188 15284
class LUNetworkRemove(LogicalUnit):
15189
  def BuildHooksNodes(self):
15190
    pass
15285
  HPATH = "network-remove"
15286
  HTYPE = constants.HTYPE_NETWORK
15287
  REQ_BGL = False
15288

  
15289
  def ExpandNames(self):
15290
    self.network_uuid = self.cfg.LookupNetwork(self.op.network_name)
15291

  
15292
    self.needed_locks = {
15293
      locking.LEVEL_NETWORK: [self.network_uuid],
15294
      }
15295

  
15296

  
15297
  def CheckPrereq(self):
15298
    """Check prerequisites.
15299

  
15300
    This checks that the given network name exists as a network, that is
15301
    empty (i.e., contains no nodes), and that is not the last group of the
15302
    cluster.
15303

  
15304
    """
15305
    if not self.network_uuid:
15306
      raise errors.OpPrereqError("Network %s not found" % self.op.network_name,
15307
                                 errors.ECODE_INVAL)
15308

  
15309
    # Verify that the network is not conncted.
15310
    node_groups = [group.name
15311
                   for group in self.cfg.GetAllNodeGroupsInfo().values()
15312
                   for network in group.networks.keys()
15313
                   if network == self.network_uuid]
15314

  
15315
    if node_groups:
15316
      self.LogWarning("Nework '%s' is connected to the following"
15317
                      " node groups: %s" % (self.op.network_name,
15318
                      utils.CommaJoin(utils.NiceSort(node_groups))))
15319
      raise errors.OpPrereqError("Network still connected",
15320
                                 errors.ECODE_STATE)
15191 15321

  
15192 15322
  def BuildHooksEnv(self):
15193
    pass
15323
    """Build hooks env.
15324

  
15325
    """
15326
    return {
15327
      "NETWORK_NAME": self.op.network_name,
15328
      }
15329

  
15330
  def BuildHooksNodes(self):
15331
    """Build hooks nodes.
15332

  
15333
    """
15334
    mn = self.cfg.GetMasterNode()
15335
    return ([mn], [mn])
15336

  
15337
  def Exec(self, feedback_fn):
15338
    """Remove the network.
15339

  
15340
    """
15341
    try:
15342
      self.cfg.RemoveNetwork(self.network_uuid)
15343
    except errors.ConfigurationError:
15344
      raise errors.OpExecError("Network '%s' with UUID %s disappeared" %
15345
                               (self.op.network_name, self.network_uuid))
15194 15346

  
15195 15347

  
15196 15348
class LUNetworkSetParams(LogicalUnit):

Also available in: Unified diff