Permalink
Browse files

manual network submask management for > 31 apps

  • Loading branch information...
loomchild committed Sep 4, 2016
1 parent 994e846 commit 77be2d4e2c1e911e160eb62c16d1c2f2fca66b56
Showing with 46 additions and 0 deletions.
  1. +2 −0 puffin/core/__init__.py
  2. +2 −0 puffin/core/docker.py
  3. +42 −0 puffin/core/network.py
View
@@ -6,6 +6,7 @@
from . import applications
from . import machine
from . import compose
from . import network
from . import docker
from . import stats
from . import analytics
@@ -21,6 +22,7 @@ def init():
applications.init()
machine.init()
compose.init()
network.init()
docker.init()
stats.init()
analytics.init()
View
@@ -1,5 +1,6 @@
from .machine import get_machine, get_tls_config
from .compose import compose_start, compose_stop
from .network import create_network
from .applications import Application, ApplicationStatus, get_application, get_application_domain, get_application_list, get_application_name
from .queue import task, task_exists
from .security import User, get_user
@@ -36,6 +37,7 @@ def create_application(client, user, application):
def create_application_task(user_id, application):
user = db.session.query(User).get(user_id)
create_network(get_client(), get_application_name(user, application) + "_default")
compose_start(get_machine(), user, application)
application_url = "http://" + get_application_domain(user, application)
sleep(APPLICATION_SLEEP_AFTER_CREATE)
View
@@ -0,0 +1,42 @@
from ipaddress import ip_network
from docker.utils import create_ipam_pool, create_ipam_config
def init():
pass
def get_next_cidr(client):
networks = client.networks()
last_cidr = ip_network("10.0.0.0/24")
for network in networks:
if (network["IPAM"] and network["IPAM"]["Config"]
and len(network["IPAM"]["Config"]) > 0
and network["IPAM"]["Config"][0]["Subnet"]):
cidr = ip_network(network["IPAM"]["Config"][0]["Subnet"])
if cidr.network_address.packed[0] == 10:
if cidr.prefixlen != 24:
raise Exception(
"Invalid network prefix length {0} for network {1}"
.format(cidr.prefixlen, network["Name"]))
if cidr > last_cidr:
last_cidr = cidr
next_cidr = ip_network((last_cidr.network_address + 256).exploded + "/24")
if next_cidr.network_address.packed[0] > 10:
raise Exception("No more networks available")
last_cidr = next_cidr
return next_cidr
def create_network(client, name):
cidr = get_next_cidr(client)
print("Creating network {0} with subnet {1}".format(name, cidr.exploded))
networks = client.networks(names=(name,))
if len(networks) > 0:
for network in networks:
client.remove_network(name)
ipam_pool = create_ipam_pool(
subnet=cidr.exploded, gateway=(cidr.network_address + 1).exploded)
ipam_config = create_ipam_config(pool_configs=[ipam_pool])
client.create_network(name, ipam=ipam_config)

0 comments on commit 77be2d4

Please sign in to comment.