aboutsummaryrefslogtreecommitdiffstats
path: root/runner
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--runner/consume.py68
-rw-r--r--snekbox/config.py (renamed from runner/config.py)4
-rw-r--r--snekbox/publish.py (renamed from runner/publish.py)36
3 files changed, 23 insertions, 85 deletions
diff --git a/runner/consume.py b/runner/consume.py
deleted file mode 100644
index 0e4d79a..0000000
--- a/runner/consume.py
+++ /dev/null
@@ -1,68 +0,0 @@
-import pika
-import traceback
-import sys
-from io import StringIO
-
-from config import (
- USERNAME,
- PASSWORD,
- HOST,
- PORT,
- EXCHANGE,
- EXCHANGE_TYPE,
- QUEUE,
- ROUTING_KEY,
-)
-
-def execute(snippet):
- old_stdout = sys.stdout
- redirected_output = sys.stdout = StringIO()
- failed = False
- try:
- exec(snippet)
- except Exception as e:
- failed = e
- finally:
- sys.stdout = old_stdout
-
- if failed:
- return failed
- return redirected_output.getvalue()
-
-
-def message_handler(ch, method, properties, body):
- msg = body.decode('utf-8')
-
- # Execute code snippets here
- print(f"incoming: {msg}", flush=True)
- result = execute(msg)
- print(result, flush=True)
-
- ch.basic_ack(delivery_tag = method.delivery_tag)
-
-def rabbitmq_consume():
- credentials = pika.PlainCredentials(USERNAME, PASSWORD)
- connection = pika.BlockingConnection(pika.ConnectionParameters(HOST, PORT, '/', credentials))
-
- channel = connection.channel()
- channel.queue_declare(queue=QUEUE, durable=False)
- channel.basic_qos(prefetch_count=1)
- channel.basic_consume(message_handler, queue=QUEUE)
-
- try:
- print(f"""Connecting to
- host: {HOST}
- port: {PORT}
- exchange: {EXCHANGE}
- queue: {QUEUE}""", flush=True)
-
- channel.start_consuming()
-
- except Exception:
- exc = traceback.format_exc()
- print(exc, flush=True)
-
- finally:
- connection.close()
-
-rabbitmq_consume()
diff --git a/runner/config.py b/snekbox/config.py
index 75b3c28..d0bd4f6 100644
--- a/runner/config.py
+++ b/snekbox/config.py
@@ -1,6 +1,8 @@
+import os
+
USERNAME = 'guest'
PASSWORD = 'guest'
-HOST = '172.17.0.2'
+HOST = os.environ.get('RMQ_HOST', '172.17.0.2')
PORT = 5672
EXCHANGE = 'exchange'
EXCHANGE_TYPE = 'direct'
diff --git a/runner/publish.py b/snekbox/publish.py
index fc18d03..0598976 100644
--- a/runner/publish.py
+++ b/snekbox/publish.py
@@ -1,14 +1,22 @@
import pika
-from config import (
- USERNAME,
- PASSWORD,
- HOST,
- PORT,
- EXCHANGE,
- EXCHANGE_TYPE,
- QUEUE,
- ROUTING_KEY,
-)
+from config import USERNAME
+from config import PASSWORD
+from config import HOST
+from config import PORT
+from config import EXCHANGE
+from config import EXCHANGE_TYPE
+from config import QUEUE
+from config import ROUTING_KEY
+
+try:
+ import docker
+ client = docker.from_env()
+ containers = client.containers.get('snekbox_pdrmq_1')
+ print("Attempting to get rabbitmq host automatically")
+ HOST = list(containers.attrs.get('NetworkSettings').get('Networks').values())[0]['IPAddress']
+ print(f"found {HOST}")
+except:
+ pass
def send(message):
credentials = pika.PlainCredentials(USERNAME, PASSWORD)
@@ -28,15 +36,11 @@ def send(message):
)
if result:
- print(f"""Connecting to
- host: {HOST}
- port: {PORT}
- exchange: {EXCHANGE}
- queue: {QUEUE}""", flush=True)
+ print(f"""Connecting to\nhost: {HOST}\nport: {PORT}\nexchange: {EXCHANGE}\nqueue: {QUEUE}""", flush=True)
print(f"Sent: '{message}'")
else:
print("not delivered")
connection.close()
-send('print "bacon is delicious"')
+#send('print("bacon is delicious")')