lib/network.rb in forj-0.0.34 vs lib/network.rb in forj-0.0.35
- old
+ new
@@ -17,153 +17,315 @@
require 'rubygems'
require 'require_relative'
require_relative 'connection.rb'
-include Connection
-require_relative 'log.rb'
-include Logging
+
#
# Network module
#
module Network
- def get_or_create_network(name)
- network = get_network(name)
- if network == nil
- network = create_network(name)
+
+ # Network management
+ def get_or_create_network(oFC, name)
+ Logging.state("Searching for network '%s'" % [name])
+ network = get_network(oFC, name)
+ if not network
+ network = create_network(oFC, name)
end
network
end
- def get_network(name)
+ def get_network(oFC, name)
begin
- info = 'getting network %s' % [name]
- Logging.info(info)
- Connection.network.networks.all(:name => name)[0]
+ networks = oFC.oNetwork.networks.all(:name => name)
+ case networks.length()
+ when 0
+ Logging.debug("No network found")
+ nil
+ when 1
+ Logging.debug("Found network '%s'" % [networks[0].name])
+ networks[0]
+ else
+ Logging.warning("Several network was found with '%s'. Selecting the first one '%s'." % [name, networks[0].name])
+ networks[0]
+ end
rescue => e
- puts e.message
- Logging.error(e.message)
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
end
end
- def create_network(name)
+ def create_network(oFC, name)
begin
- info = 'creating network %s' % [name]
- Logging.info(info)
- Connection.network.networks.create(:name => name)
+ Logging.debug('creating network %s' % [name])
+ oFC.oNetwork.networks.create(:name => name)
rescue => e
- Logging.error(e.message)
+ Logging.fatal(1, "%s\n%s" % [e.inspect, e.message])
end
end
- def delete_network(network_name)
+ def delete_network(oFC, network_name)
begin
- network = get_network(network_name)
- Connection.network.networks.get(network.id).destroy
+ network = get_network(oFC, network_name)
+ oFC.oNetwork.networks.get(network.id).destroy
rescue => e
- Logging.error(e.message)
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
end
end
- def get_or_create_subnet(network_id, name)
+
+ # Subnet management
+ def get_or_create_subnet(oFC, network_id, name)
+ Logging.state("Searching for sub-network attached '%s'." % [name])
begin
- subnet = get_subnet(name)
- if subnet == nil
- subnet = create_subnet(network_id, name)
- end
- subnet
+ subnets = oFC.oNetwork.subnets.all(:network_id => network_id)
rescue => e
- Logging.error(e.message)
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
end
+ if subnets
+ case subnets.length()
+ when 0
+ Logging.debug("No subnet found from '%s' network" % [name])
+ subnet = nil
+ when 1
+ Logging.debug("Found '%s' subnet from '%s' network" % [subnets[0].name, name])
+ subnet = subnets[0]
+ else
+ Logging.warning("Several subnet was found on '%s'. Choosing the first one = '%s'" % [name, subnets[0].name])
+ subnet = subnets[0]
+ end
+ end
+ if not subnet
+ # Create the subnet with 'sub-' prefixing the network name.
+ begin
+ subnet = create_subnet(oFC, network_id, 'sub-%s' % [name])
+ rescue => e
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
+ end
+ end
+ return subnet
end
- def create_subnet(network_id, name)
+ def create_subnet(oFC, network_id, name)
+ Logging.debug("Creating subnet '%s'" % [name])
begin
- Connection.network.subnets.create(
+ oFC.oNetwork.subnets.create(
:network_id => network_id,
:name => name,
- :cidr => get_next_subnet,
+ :cidr => get_next_subnet(oFC),
:ip_version => '4'
)
rescue => e
- Logging.error(e.message)
+ Logging.fatal(1, "%s\n%s" % [e.class.name, e.message])
end
end
- def delete_subnet(subnet_id)
+ def delete_subnet(oFC, subnet)
+ Logging.debug("Deleting subnet '%s'" % [subnet.name])
begin
- Connection.network.subnets.get(subnet_id).destroy
+ oFC.oNetwork.subnets.get(subnet.id).destroy
rescue => e
- Logging.error(e.message)
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
end
end
- def get_subnet(name)
+ def get_subnet(oFC, name)
begin
- Connection.network.subnets.all(:name => name)[0]
+ oFC.oNetwork.subnets.all(:name => name)[0]
rescue => e
- Logging.error(e.message)
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
end
end
- def get_router(name)
- begin
- routers = Connection.network.routers.all({:name => name})
- router = nil
- routers.each do|r|
- router = r
+ # Router management
+ def get_or_create_router(oFC, network, subnet)
+ port = get_router_interface_attached(oFC, network)
+ if not port
+ # Trying to get router
+ router = get_router(oFC, 'router-%s' % network.name)
+ router = create_router(oFC, 'router-%s' % network.name) if not router
+ create_router_interface(subnet, router) if router
+ else
+ routers = oFC.oNetwork.routers.all({:id => port.device_id})
+ if routers.length() == 1
+ if routers[0].external_gateway_info
+ Logging.debug("Found router '%s' attached to an external gateway." % [ routers[0].name ] )
+ else
+ Logging.debug("Found router '%s' but need to be attached to an external gateway. Attaching..." % [ routers[0].name ] )
+ netty=search_gateway(oFC)
+ if netty
+ routers[0].external_gateway_info = { 'network_id' => netty.id }
+ routers[0].save
+ Logging.debug("Router '%s' attached to the external network '%s'." % [ routers[0].name, netty.name ] )
+ else
+ Logging.fatal(1, "Unable to attach router '%s' to an external gateway. Required for boxes to get internet access. " % [ routers[0].name ] )
+ end
+ end
+ router = routers[0]
+ else
+ Logging.warning("Unable to found the router id '%s'" % [ port.device_id ])
+ router = nil
+ end
+ end
+ router
+ end
+
+ def get_router(oFC, name)
+ Logging.state("Searching for router '%s'..." % [name] )
+ begin
+ routers = oFC.oNetwork.routers.all({:name => name})
+ if routers.length() == 1
+ if routers[0].external_gateway_info.has_key?('id')
+ Logging.debug("Found router '%s' attached to an external gateway." % [ routers[0].name ] )
+ else
+ Logging.warning("Found router '%s' but not attached to an external." % [ routers[0].name ] )
+ end
+ routers[0]
+ else
+ Logging.debug("Router '%s' not found." % [ name ] )
+ nil
end
+ rescue => e
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
+ end
+ end
- router
+ def create_router(oFC, name)
+
+ netty = search_gateway(oFC)
+
+ begin
+ if netty
+ Logging.debug("Creating router '%s' attached to the external Network '%s'." % [name, netty.name])
+ oFC.oNetwork.routers.create(
+ :name => name,
+ :admin_state_up => true,
+ :external_gateway_info => { 'network_id' => netty.id }
+ )
+ else
+ Logging.debug("Creating router '%s' without external Network." % [name])
+ oFC.oNetwork.routers.create(
+ :name => name,
+ :admin_state_up => true
+ )
+ end
rescue => e
- Logging.error(e.message)
+ Logging.fatal(1, "%s\n%s" % [e.inspect, e.message])
end
end
- def create_router_interface(subnet_id, router)
+ def delete_router(oFC, router_id)
+ Logging.debug("Deleting router '%s'" % [router.name])
begin
- router.add_interface(subnet_id, nil)
+ oFC.oNetwork.routers.get(router.id).destroy
rescue => e
- Logging.error(e.message)
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
end
end
+
+ # Router interface to connect to the network
+ def create_router_interface(subnet, router)
+ raise "Internal Error: subnet/router object not passed." if not subnet or not router
+
+ Logging.debug("Attaching subnet '%s' to router '%s'" % [subnet.name, router.name])
+ begin
+ router.add_interface(subnet.id, nil)
+ rescue => e
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
+ end
+ end
+
def delete_router_interface(subnet_id, router)
+ Logging.debug("Removing subnet '%s' from router '%s'" % [subnet.name, router.name])
begin
router.remove_interface(subnet_id)
rescue => e
- Logging.error(e.message)
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
end
end
-
- def create_router(name)
+
+ def get_router_interface(oFC, network_id, device_id)
begin
- Connection.network.routers.create(
- :name => name,
- :admin_state_up => true
- )
+ # Searching for router port attached
+ ports=oFC.oNetwork.ports.all({:network_id => network_id, :device_id => device_id})
+ case ports.length()
+ when 0
+ nil
+ else
+ port[0]
+ end
rescue => e
- Logging.error(e.message)
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
end
end
- def delete_router(router_id)
+ def get_router_interface_attached(oFC, network)
+ Logging.state("Searching for router port attached to the network '%s'" % [network.name] )
begin
- Connection.network.routers.get(router_id).destroy
+ # Searching for router port attached
+ ports=oFC.oNetwork.ports.all({:network_id => network.id, :device_type => 'network:router_interface'})
+ case ports.length()
+ when 0
+ Logging.debug("No router port attached to the network '%s'" % [network.name] )
+ nil
+ else
+ Logging.debug("Found a router port attached to the network '%s' " % [network.name] )
+ ports[0]
+ end
rescue => e
- Logging.error(e.message)
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
end
end
+
+ # Gateway management
+ def get_gateway(oFC, name)
+
+ return nil if not name or not oFC
+
+ Logging.state("Getting gateway '%s'" % [name])
+ networks = oFC.oNetwork
+ begin
+ netty = networks.get(name)
+ rescue => e
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
+ end
+ Logging.state("Found gateway '%s'" % [name]) if netty
+ Logging.state("Unable to found gateway '%s'" % [name]) if not netty
+ return netty
+ end
+
+ def search_gateway(oFC)
+ Logging.state("Identifying External gateway ...")
+ begin
+ # Searching for router port attached
+ networks=oFC.oNetwork.networks.all({ :router_external => true })
+ case networks.length()
+ when 0
+ Logging.debug("No external network")
+ nil
+ when 1
+ Logging.debug("Found external network '%s'." % [networks[0].name] )
+ networks[0]
+ else
+ Logging.debug("Found several external networks. Selecting the first one '%s'" % [networks[0].name] )
+ networks[0]
+ end
+ rescue => e
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
+ end
+ end
+
end
-def get_next_subnet
+def get_next_subnet(oFC)
begin
subnet_values = Array.new
- subnets = Connection.network.subnets.all
+ subnets = oFC.oNetwork.subnets.all
subnets.each do|s|
subnet_values.push(s.cidr)
end
@@ -196,8 +358,8 @@
new_subnet = max_value.to_i + 1
new_cidr = '10.0.%s.0/24' % [new_subnet]
end
new_cidr
rescue => e
- Logging.error(e.message)
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
end
end