Skip to content

Commit

Permalink
Sockets and Routers reworked.
Browse files Browse the repository at this point in the history
  • Loading branch information
idlesign committed Oct 11, 2017
1 parent 0ec7c79 commit 7bea02c
Show file tree
Hide file tree
Showing 24 changed files with 641 additions and 354 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ uwsgiconf changelog
===================


Unreleased
----------
+ Sockets and Routers reworked.


v0.10.0
-------
+ Added 'runtime' package preview.
Expand Down
5 changes: 2 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ Let's make ``uwsgicfg.py``. There we configure it using nice ``PythonSection`` p
wsgi_module='/home/idle/myapp/wsgi.py',
).networking.register_socket(
# Make app available at http://127.0.0.1:8000
address='127.0.0.1:8000',
type=PythonSection.networking.socket_types.HTTP,
# Make app available on http://127.0.0.1:8000
PythonSection.networking.sockets.http('127.0.0.1:8000'),
).as_configuration()
Expand Down
3 changes: 1 addition & 2 deletions demos/onefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ def configure():
workers=1

).networking.register_socket(
'127.0.0.1:8000',
type=PythonSection.networking.socket_types.HTTP
PythonSection.networking.sockets.http('127.0.0.1:8000'),

).as_configuration().print_ini()

Expand Down
7 changes: 7 additions & 0 deletions docs/source/grp_networking.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Networking
==========


.. toctree::
:maxdepth: 3

grp_networking_sockets


.. automodule:: uwsgiconf.options.networking
:members:

7 changes: 7 additions & 0 deletions docs/source/grp_networking_sockets.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Sockets
=======


.. automodule:: uwsgiconf.options.networking_sockets
:members:

5 changes: 2 additions & 3 deletions docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ Let's make ``uwsgicfg.py``. There we configure it using nice ``PythonSection`` p
).networking.register_socket(
# Make app available at http://127.0.0.1:8000
address='127.0.0.1:8000',
type=PythonSection.networking.socket_types.HTTP,
PythonSection.networking.sockets.http('127.0.0.1:8000'),
).as_configuration()
Expand Down Expand Up @@ -98,7 +97,7 @@ Let's configure uWSGI to use Emperor Broodlord mode as described here_.
# This section is for Broodlord Emperor.
Section.derive_from(base_section).
networking.register_socket(address=':3031').
networking.register_socket(Section.networking.sockets.default(':3031')).
workers.set_zerg_server_params(socket=BROODLORD_SOCKET).
empire.set_emperor_params(vassals_home='/etc/vassals').
empire.set_mode_broodlord_params(zerg_count=40, vassal_backlog_items_sos=10),
Expand Down
78 changes: 55 additions & 23 deletions tests/options/test_networking.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import pytest

from uwsgiconf.config import Section
from uwsgiconf.exceptions import ConfigurationError


def test_networking_basics(assert_lines):
Expand All @@ -22,41 +19,76 @@ def test_networking_basics(assert_lines):
'reuse-port = true',
], Section().networking.set_bsd_socket_params(port_reuse=True))

sockets = Section.networking.sockets

assert_lines([
'http-socket = :8080',
'map-socket = 0:1',
'http-socket-modifier1 = 8',
'http-socket-modifier2 = 1',

], Section().networking.register_socket(
address=':8080', type=Section.networking.socket_types.HTTP,
bound_workers=1))
sockets.http(':8080', bound_workers=1, modifier=Section.routing.modifiers.jvm(submod=1))
))

assert_lines([
'socket = :8000',
'map-socket = 0:2,3',
], Section().networking.register_socket(address=':8000', bound_workers=[2, 3]))
], Section().networking.register_socket(sockets.default(':8000', bound_workers=[2, 3])))

with pytest.raises(ConfigurationError) as einfo:
assert_lines([
'',
], Section().networking.register_socket(address='127.0.0.1', type='dummy'))
assert 'Unknown' in str(einfo.value) # unknown socket type
assert_lines([
'plugin = http',
'https-export-cert = MYVAR',

], Section().networking.set_ssl_params(client_cert_var='MYVAR'))

# batch socket registration
assert_lines([
'socket = :8001',
'socket = :8002',
'socket = :8003',
'http11-socket = :8001',
], Section().networking.register_socket(sockets.http(':8001', http11=True)))

], Section().networking.register_sockets(
dict(address=':8001'),
dict(address=':8002'),
dict(address=':8003'),
))
assert_lines([
'https-socket = :8001,cert.crt,key.key,,!clientca',
], Section().networking.register_socket(
sockets.https(
':8001', 'cert.crt', 'key.key', client_ca='!clientca')))

assert_lines([
'plugin = http',
'https-export-cert = MYVAR',
'puwsgi-socket = :8001',
], Section().networking.register_socket(sockets.uwsgi(':8001', persistent=True)))

], Section().networking.set_ssl_params(client_cert_var='MYVAR'))
assert_lines([
'fastcgi-nph-socket = :8001',
], Section().networking.register_socket(sockets.fastcgi(':8001', nph=True)))

assert_lines([
'scgi-nph-socket = :8001',
], Section().networking.register_socket(sockets.scgi(':8001', nph=True)))

shared1 = sockets.shared(':80', undeferred=True)
shared2 = sockets.shared(':81')

assert_lines([
'undeferred-shared-socket = :80\nsocket = =0\nuwsgi-socket = =0',
'shared-socket = :81\nscgi-socket = =1',
],
Section().networking.register_socket([
sockets.default(shared1),
sockets.uwsgi(shared1),
sockets.scgi(shared2),
])
)

assert_lines([
'shared-socket = :80\nshared-socket = :443',
'http = =0\nhttps2 = cert=foobar.crt,key=foobar.key,addr==1',
],
Section().routing.use_router(
Section.routing.routers.http(sockets.shared(':80'))

).routing.use_router(
Section.routing.routers.https(sockets.shared(':443'), cert='foobar.crt', key='foobar.key')
)
)


def test_networking_sni(assert_lines):
Expand Down
56 changes: 25 additions & 31 deletions tests/options/test_routing_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ def test_http(assert_lines):

routers = Section.routing.routers

router = routers.http().on(
'127.0.0.1:3111'
router = routers.http(
on='127.0.0.1:3111'
).set_basic_params(
keepalive=20
).set_connections_params(
Expand All @@ -25,14 +25,14 @@ def test_http(assert_lines):
Section().routing.use_router(router)
)

router = routers.http().https_on(
address='127.0.0.1:3112',
router = routers.https(
on='127.0.0.1:3112',
cert='/here/mysert.cer',
key='mykey',
session_context='somectx')

assert_lines([
'https2 = addr=127.0.0.1:3112,cert=/here/mysert.cer,key=mykey',
'https2 = cert=/here/mysert.cer,key=mykey,addr=127.0.0.1:3112',
'http-session-context = somectx',
],
Section().routing.use_router(router)
Expand All @@ -43,16 +43,16 @@ def test_ssl(assert_lines):

routers = Section.routing.routers

router = routers.ssl().on(
address='127.0.0.1:3112',
router = routers.ssl(
on='127.0.0.1:3112',
cert='/here/mysert.cer',
key='mykey',
).set_connections_params(
retry_max=5
)

assert_lines([
'sslrouter2 = addr=127.0.0.1:3112,cert=/here/mysert.cer,key=mykey',
'sslrouter2 = cert=/here/mysert.cer,key=mykey,addr=127.0.0.1:3112',
'sslrouter-max-retries = 5',
],
Section().routing.use_router(router)
Expand All @@ -63,8 +63,8 @@ def test_raw(assert_lines):

routers = Section.routing.routers

router = routers.raw().on(
address='127.0.0.1:3115'
router = routers.raw(
on='127.0.0.1:3115'
).set_connections_params(
retry_max=5
)
Expand All @@ -81,8 +81,8 @@ def test_fast(assert_lines):

routers = Section.routing.routers

router = routers.fast().on(
address='127.0.0.1:3113',
router = routers.fast(
on='127.0.0.1:3113',
forward_to=['127.0.0.1:3123', '127.0.0.1:3124'],
).set_connections_params(
defer=5
Expand All @@ -109,8 +109,8 @@ def test_forkpty(assert_lines):

routers = Section.routing.routers

router = routers.forkpty().on(
address='127.0.0.1:3118',
router = routers.forkpty(
on='127.0.0.1:3118',
undeferred=True
).set_basic_params(
run_command='/bin/zsh'
Expand All @@ -133,13 +133,12 @@ def test_tuntap(assert_lines):
routers = Section.routing.routers

router = routers.tuntap(
).set_basic_params(
use_credentials='some'
).on(
on='/tmp/tuntap.socket',
device='emperor0',
socket='/tmp/tuntap.socket',
stats_server='127.0.0.1:3030',
gateway='127.0.0.1:3032',
).set_basic_params(
use_credentials='some'
).add_firewall_rule(
direction='out',
action='allow',
Expand All @@ -162,7 +161,7 @@ def test_tuntap(assert_lines):

router = routers.tuntap().device_connect(
device_name='uwsgi0',
address='/tmp/tuntap.socket',
socket='/tmp/tuntap.socket',
). device_add_rule(
direction='in',
action='route',
Expand All @@ -183,44 +182,39 @@ def test_forwarders(assert_lines):

routers = Section.routing.routers

router = routers.fast()
router = routers.fast
assert_lines(
'fastrouter-use-base = /tmp/sockets/',

Section().routing.use_router(router.on(address='127.0.0.1:3110',
Section().routing.use_router(router(on='127.0.0.1:3110',
forward_to=router.forwarders.path('/tmp/sockets/'))))

router = routers.fast()
assert_lines(
'fastrouter-use-pattern = /tmp/sockets/%s/uwsgi.sock',

Section().routing.use_router(router.on(address='127.0.0.1:3110',
Section().routing.use_router(router(on='127.0.0.1:3110',
forward_to=router.forwarders.path('/tmp/sockets/%s/uwsgi.sock'))))

router = routers.fast()
assert_lines(
'fastrouter-use-code-string = 0:some.py:funcname',

Section().routing.use_router(router.on(address='127.0.0.1:3110',
Section().routing.use_router(router(on='127.0.0.1:3110',
forward_to=router.forwarders.code('some.py', 'funcname'))))

router = routers.fast()
assert_lines(
'fastrouter-use-cache = mine',

Section().routing.use_router(router.on(address='127.0.0.1:3110',
Section().routing.use_router(router(on='127.0.0.1:3110',
forward_to=router.forwarders.cache('mine'))))

router = routers.fast()
assert_lines(
'fastrouter-use-socket = /there/here.sock',

Section().routing.use_router(router.on(address='127.0.0.1:3110',
Section().routing.use_router(router(on='127.0.0.1:3110',
forward_to=router.forwarders.socket('/there/here.sock'))))

router = routers.fast()
assert_lines(
'fastrouter-subscription-server = 192.168.0.100:7000',

Section().routing.use_router(router.on(address='127.0.0.1:3110',
Section().routing.use_router(router(on='127.0.0.1:3110',
forward_to=router.forwarders.subscription_server('192.168.0.100:7000'))))
11 changes: 9 additions & 2 deletions uwsgiconf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def handle_priority(value, use_list=False):
def handle_plugin_required(val):

if isinstance(val, ParametrizedValue):
key.swap(val.opt_key or key)
key.swap(val.opt_key or key) # todo

if val.plugin:
# Automatic plugin activation.
Expand Down Expand Up @@ -228,6 +228,13 @@ def handle_plugin_required(val):
if not handle_priority(value):
opts[key] = value

def _make_section_like(self):
self._section = type('SectionLike', (object,), {'_opts': OrderedDict()})

def _contribute_to_opts(self, target):
target_section = target._section
target_section._opts.update(self._section._opts)


class ParametrizedValue(OptionsGroup):
"""Represents parametrized option value."""
Expand Down Expand Up @@ -257,7 +264,7 @@ def __str__(self):

result = ''

if not self.opt_key:
if self.opt_key is None:
result += self._get_name() + self.name_separator

result += self.args_joiner.join(args)
Expand Down
4 changes: 2 additions & 2 deletions uwsgiconf/options/alarm_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ..base import ParametrizedValue
from ..utils import listify, filter_locals, make_key_val_string
from ..utils import listify, filter_locals, KeyValue


class AlarmType(ParametrizedValue):
Expand Down Expand Up @@ -58,7 +58,7 @@ def __init__(
timeout=None, conn_timeout=None,
mail_from=None, mail_to=None, subject=None):

opts = make_key_val_string(
opts = KeyValue(
filter_locals(locals(), drop=['alias', 'url']),
bool_keys=['ssl', 'ssl_insecure'],
items_separator=self.args_joiner,
Expand Down

0 comments on commit 7bea02c

Please sign in to comment.