Archiving a bunch of old stuff

This commit is contained in:
Dan Buch
2015-06-22 13:15:42 -05:00
parent a6ec1d560e
commit bd1abd8734
395 changed files with 1 additions and 76 deletions

2
oldstuff/sylvilagus/python/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
/.env
/*.egg-info/

View File

@@ -0,0 +1,13 @@
from setuptools import setup
setup(
name='sylvilagus',
version='0.1.0',
author='Dan Buch',
author_email='dan@meatballhat.com',
description='crap accumulated while reading through RabbitMQ in Action',
packages=['sylvilagus'],
install_requires=[
'pika == 0.9.6'
]
)

View File

@@ -0,0 +1,37 @@
import os
import pika
def get_conn_params():
return pika.URLParameters(os.environ['SYLVILAGUS_AMQP_URI'])
def get_nonblocking_channel(declare_exchange=True):
channel = pika.SelectConnection(get_conn_params()).channel()
if declare_exchange:
channel.exchange_declare(
exchange='hello-exchange',
exchange_type='direct',
passive=False,
durable=True,
auto_delete=False
)
return channel
def get_channel(declare_exchange=True):
channel = pika.BlockingConnection(get_conn_params()).channel()
if declare_exchange:
channel.exchange_declare(
exchange='hello-exchange',
exchange_type='direct',
passive=False,
durable=True,
auto_delete=False
)
return channel

View File

@@ -0,0 +1,39 @@
from __future__ import print_function
import sys
import pika
from sylvilagus.ch02 import hello_world
def msg_consumer(channel, method, header, body):
channel.basic_ack(delivery_tag=method.delivery_tag)
if body == 'quit':
channel.basic_cancel(consumer_tag='hello-consumer')
channel.stop_consuming()
else:
print(body)
return
def main():
channel = hello_world.get_channel()
channel.queue_declare(queue='hello-queue')
channel.queue_bind(queue='hello-queue',
exchange='hello-exchange',
routing_key='hola')
channel.basic_consume(msg_consumer,
queue='hello-queue',
consumer_tag='hello-consumer')
print('consuming...')
channel.start_consuming()
return 0
if __name__ == '__main__':
sys.exit(main())

View File

@@ -0,0 +1,27 @@
from __future__ import print_function
import sys
import pika
from sylvilagus.ch02 import hello_world
def main(args=sys.argv[:]):
channel = hello_world.get_channel()
msg = args[1]
msg_props = pika.BasicProperties()
msg_props.content_type = 'text/plain'
channel.basic_publish(body=msg,
exchange='hello-exchange',
properties=msg_props,
routing_key='hola')
print('published {!r}'.format(msg))
return 0
if __name__ == '__main__':
sys.exit(main())

View File

@@ -0,0 +1,37 @@
from __future__ import print_function
import logging
import sys
import pika
from sylvilagus.ch02 import hello_world
def main(sysargs=sys.argv[:]):
msg = sysargs[1]
logging.basicConfig()
channel = hello_world.get_channel()
if channel.basic_publish(
body=msg,
exchange='hello-exchange',
properties=pika.BasicProperties(
content_type='text/plain',
delivery_mode=1
),
routing_key='hola',
mandatory=True):
print('Message delivered!')
else:
print('Message returned!')
channel.close()
return 0
if __name__ == '__main__':
sys.exit(main())

View File

@@ -0,0 +1,100 @@
from __future__ import print_function
import json
import smtplib
import sys
import pika
AMQP_SERVER = 'localhost'
AMQP_USER = 'alert_user'
AMQP_PASS = 'alertme'
AMQP_VHOST = '/'
AMQP_EXCHANGE = 'alerts'
OPS_EMAILS = ['me@localhost']
ADMIN_EMAILS = ['me@localhost']
def main():
creds_broker = pika.PlainCredentials(AMQP_USER, AMQP_PASS)
conn_params = pika.ConnectionParameters(AMQP_SERVER,
virtual_host=AMQP_VHOST,
credentials=creds_broker)
conn_broker = pika.BlockingConnection(conn_params)
channel = conn_broker.channel()
channel.exchange_declare(exchange=AMQP_EXCHANGE,
exchange_type='topic',
auto_delete=False)
channel.queue_declare(queue='critical', auto_delete=False)
channel.queue_bind(queue='critical',
exchange='alerts',
routing_key='critical.*')
channel.queue_declare(queue='rate_limit', auto_delete=False)
channel.queue_bind(queue='rate_limit',
exchange='alerts',
routing_key='*.rate_limit')
channel.basic_consume(critical_notify,
queue='critical',
no_ack=False,
consumer_tag='critical')
channel.basic_consume(rate_limit_notify,
queue='rate_limit',
no_ack=False,
consumer_tag='rate_limit')
try:
print('Ready for alerts!')
channel.start_consuming()
except KeyboardInterrupt:
conn_broker.close()
return 0
def send_mail(recipients, subject, message):
"""Email generator for received alerts."""
headers = '\r\n'.join([
'From: alerts@sylvilagus.local',
'To: ',
'Date: ',
'Subject: {}'.format(subject)
]) + '\r\n\r\n'
smtp_server = smtplib.SMTP()
smtp_server.connect('localhost', 25)
smtp_server.sendmail('alerts@sylvilagus.local',
recipients,
headers + str(message))
smtp_server.close()
def critical_notify(channel, method, header, body):
"""Sends CRITICAL alerts to administrators via email."""
message = json.loads(body)
send_mail(OPS_EMAILS, 'CRITICAL ALERT', message)
print('Sent alert via email! Alert Text: {} \nRecipients: {}'.format(
message, OPS_EMAILS
)
)
channel.basic_ack(delivery_tag=method.delivery_tag)
def rate_limit_notify(channel, method, header, body):
"""Sends the message to the administrators via email."""
message = json.loads(body)
send_mail(ADMIN_EMAILS, 'RATE LIMIT ALERT!', message)
print('Sent alert via email! Alert Text: {} \nRecipients: {}'.format(
message, ADMIN_EMAILS
)
)
channel.basic_ack(delivery_tag=method.delivery_tag)
if __name__ == '__main__':
sys.exit(main())

View File

@@ -0,0 +1,51 @@
from __future__ import print_function
import json
import sys
import pika
from argparse import ArgumentParser
def main(sysargs=sys.argv[:]):
parser = ArgumentParser()
parser.add_argument('-r',
'--routing-key',
help='Routing key for message (e.g. myalert.im)')
parser.add_argument('-m',
'--message',
help='Message text for alert.')
args = parser.parse_args(sysargs[1:])
creds_broker = pika.PlainCredentials('alert_user', 'alertme')
conn_params = pika.ConnectionParameters('localhost',
virtual_host='/',
credentials=creds_broker)
conn_broker = pika.BlockingConnection(conn_params)
channel = conn_broker.channel()
msg = json.dumps({'message': args.message})
msg_props = pika.BasicProperties()
msg_props.content_type = 'application/json'
msg_props.durable = False
channel.basic_publish(body=msg,
exchange='alerts',
properties=msg_props,
routing_key=args.routing_key)
print(
('Sent message {} tagged with routing key {!r} to ' +
'exchange "alerts" on vhost "/".').format(msg, args.routing_key)
)
conn_broker.close()
return 0
if __name__ == '__main__':
sys.exit(main())