Skip to content

Commit 7de103f

Browse files
author
Marco Paolini
committed
Improve CLI tools and documentation
1 parent cf14aa4 commit 7de103f

File tree

10 files changed

+61
-45
lines changed

10 files changed

+61
-45
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ RUN python3.5 -m pip install --no-cache-dir --no-index -f wheelhouse-app --no-de
1313
RUN adduser --disabled-password --disabled-login --home /app --system -q app
1414
WORKDIR /app
1515
USER app
16-
ENTRYPOINT ["python3.5", "-m", "pushpull.cli.server"]
16+
ENTRYPOINT ["pushpull-server"]
1717
EXPOSE 8080

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ Websocket to message broker gateway for servers
2727

2828
Run the websocket server:
2929

30-
python -m pushpull.cli.server
30+
pushpull-server
3131

3232
Run the CLI websocket client:
3333

34-
python -m pushpull.cli.client challenge_websocket http://localhost:8080/sock
34+
pushpull-client challenge_websocket http://localhost:8080/ mario
3535

3636
Run the CLI rabbitmq client:
3737

38-
python -m pushpull.cli.client challenge_amqp
38+
pushpull-client challenge_amqp amqp://localhost/ mario
3939

4040
# Build docker image
4141

pushpull/amqp/client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
logger = logging.getLogger(__name__)
88

99

10-
async def challenge(name, fd_in, fd_out):
11-
async with Exchanger(name, Exchanger.ROLE_APP) as (amqp_sender, amqp_receiver):
10+
async def challenge(url, name, fd_in, fd_out):
11+
async with Exchanger(name, Exchanger.ROLE_APP, url=url) as (amqp_sender, amqp_receiver):
1212
sender = send_from_fd_to_amqp(fd_in, amqp_sender)
1313
receiver = send_from_amqp_to_fd(amqp_receiver, fd_out)
1414
_, pending = await asyncio.wait([sender, receiver], return_when=asyncio.FIRST_COMPLETED)

pushpull/amqp/gateway/driver_aioamqp.py

+7-16
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,25 @@
44
import aioamqp
55

66
from ... import config
7+
from .driver_base import ExchangerBase
78

89
logger = logging.getLogger(__name__)
910

1011

11-
class Exchanger:
12-
13-
ROLE_WS = 1
14-
ROLE_APP = 2
15-
16-
def __init__(self, name, role, client_id=0):
17-
if role not in [self.ROLE_WS, self.ROLE_APP]:
18-
raise ValueError('bad role {}'.format(role))
19-
self.role = role
20-
self.client_id = client_id
21-
self.name = name
12+
class Exchanger(ExchangerBase):
2213

2314
async def __aenter__(self):
2415
logger.debug('connecting with role {}'.format(self.role))
25-
params = config.get_amqp_conn_params()
16+
params = config.get_amqp_conn_params(self.url)
2617
params['login'] = params.pop('username')
2718
params['virtualhost'] = params.pop('virtual_host')
2819
self._transport, self._protocol = await aioamqp.connect(**params)
2920
# TODO: handle reconnect awaiting from self._conn
3021
self._chan = await self._protocol.channel()
31-
app_exchange_name = 'pushpull.app'
32-
app_routing_key = ''
33-
ws_exchange_name = 'pushpull.ws'
34-
ws_routing_key = 'pushpull.ws.{}'.format(self.name)
22+
app_exchange_name = self.get_app_exchange_name()
23+
app_routing_key = self.get_app_routing_key()
24+
ws_exchange_name = self.get_ws_exchange_name()
25+
ws_routing_key = self.get_ws_routing_key()
3526
await self._chan.exchange(app_exchange_name, 'fanout', durable=True)
3627
await self._chan.exchange(ws_exchange_name, 'direct', durable=True)
3728
if self.role == self.ROLE_WS:

pushpull/amqp/gateway/driver_asynqp.py

+7-16
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,23 @@
33

44
import asynqp
55

6+
from .driver_base import ExchangerBase
67
from ... import config
78

89
logger = logging.getLogger(__name__)
910

1011

11-
class Exchanger:
12-
13-
ROLE_WS = 1
14-
ROLE_APP = 2
15-
16-
def __init__(self, name, role, client_id=0):
17-
if role not in [self.ROLE_WS, self.ROLE_APP]:
18-
raise ValueError('bad role {}'.format(role))
19-
self.role = role
20-
self.client_id = client_id
21-
self.name = name
12+
class Exchanger(ExchangerBase):
2213

2314
async def __aenter__(self):
2415
logger.debug('connecting with role {}'.format(self.role))
25-
params = config.get_amqp_conn_params()
16+
params = config.get_amqp_conn_params(self.url)
2617
self._conn = await asynqp.connect(**params)
2718
self._chan = await self._conn.open_channel()
28-
app_exchange_name = 'pushpull.app'
29-
app_routing_key = ''
30-
ws_exchange_name = 'pushpull.ws'
31-
ws_routing_key = 'pushpull.ws.{}'.format(self.name)
19+
app_exchange_name = self.get_app_exchange_name()
20+
app_routing_key = self.get_app_routing_key()
21+
ws_exchange_name = self.get_ws_exchange_name()
22+
ws_routing_key = self.get_ws_routing_key()
3223
app_exchange = await self._chan.declare_exchange(app_exchange_name, 'fanout')
3324
ws_exchange = await self._chan.declare_exchange(ws_exchange_name, 'direct')
3425
if self.role == self.ROLE_WS:

pushpull/amqp/gateway/driver_base.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class ExchangerBase:
2+
3+
ROLE_WS = 1
4+
ROLE_APP = 2
5+
6+
def __init__(self, name, role, client_id=0, url=None):
7+
if role not in [self.ROLE_WS, self.ROLE_APP]:
8+
raise ValueError('bad role {}'.format(role))
9+
self.role = role
10+
self.client_id = client_id
11+
self.name = name
12+
self.url = url
13+
14+
def get_app_exchange_name(self):
15+
return 'pushpull.app'
16+
17+
def get_app_routing_key(self):
18+
return ''
19+
20+
def get_ws_exchange_name(self):
21+
return 'pushpull.ws'
22+
23+
def get_ws_routing_key(self):
24+
return 'pushpull.ws.{}'.format(self.name)

pushpull/cli/client.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ def challenge_websocket(url, name):
2424

2525

2626
@click.command()
27+
@click.argument('url')
2728
@click.argument('name')
28-
def challenge_amqp(name):
29+
def challenge_amqp(url, name):
2930
logging.basicConfig(level=logging.DEBUG)
30-
click.echo(asyncio.get_event_loop().run_until_complete(amqp_client.challenge(name, sys.stdin, sys.stdout)))
31+
click.echo(asyncio.get_event_loop().run_until_complete(amqp_client.challenge(url, name, sys.stdin, sys.stdout)))
3132
client.add_command(challenge_amqp)
3233

3334

pushpull/config.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ def get_url_path():
3333
return path
3434

3535

36-
def get_amqp_conn_params():
37-
url_string = BROKER_URL or 'amqp://guest:guest@localhost:5672/'
38-
url = urllib.parse.urlparse(url_string)
36+
def get_amqp_conn_params(url=None):
37+
if url is None:
38+
url = BROKER_URL or 'amqp://guest:guest@localhost:5672/'
39+
url = urllib.parse.urlparse(url)
3940
return {
4041
'host': url.hostname or 'localhost',
4142
'port': url.port,

pushpull/websocket/server.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ def serve(argv):
1313
cors.add(
1414
route,
1515
{
16-
origin: aiohttp_cors.ResourceOptions(allow_credentials=True)
16+
origin: aiohttp_cors.ResourceOptions(
17+
allow_credentials=True,
18+
)
1719
for origin in config.get_cors_allowed_origins()
1820
}
1921
)

setup.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,11 @@
2121
url='https://github.com/elastic-coders/pushpull/',
2222
license='MIT',
2323
include_package_data=True,
24-
install_requires=reqs
24+
install_requires=reqs,
25+
entry_points={
26+
'console_scripts': [
27+
'pushpull-server = pushpull.cli.server:serve',
28+
'pushpull-client = pushpull.cli.client:client',
29+
]
30+
}
2531
)

0 commit comments

Comments
 (0)