Skip to content

Commit

Permalink
Merge pull request #17 from dave-shawley/rm-consulate
Browse files Browse the repository at this point in the history
Replace consulate with requests
  • Loading branch information
aremm committed Sep 2, 2021
2 parents c51a653 + a2c16e3 commit c8d5991
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 44 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016-2018 AWeber Communications
Copyright (c) 2016-2021 AWeber Communications
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
Expand Down
91 changes: 50 additions & 41 deletions bandoleers/prepit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
import re
import sys
try:
from urllib.parse import urlsplit, urlunsplit
from urllib.parse import urljoin, urlsplit, urlunsplit
except ImportError:
from urlparse import urlsplit, urlunsplit
from urlparse import urljoin, urlsplit, urlunsplit

from redis import StrictRedis
import consulate
import json
import psycopg2
import requests
Expand All @@ -42,6 +41,7 @@ def prep_redis(file_):
for command, entries in config.items():
for name, values in entries.items():
redis.execute_command(command, name, *values)
redis.connection_pool.disconnect()
except Exception:
LOGGER.exception('Failed to execute redis commands.')
sys.exit(-1)
Expand All @@ -50,13 +50,20 @@ def prep_redis(file_):
def prep_consul(file):
try:
LOGGER.info('Processing %s', file)
consul = consulate.Consul(os.environ.get('CONSUL_HOST', 'localhost'),
int(os.environ.get('CONSUL_PORT', '8500')))
kv_root = urlunsplit((
'http',
'{}:{}'.format(os.environ.get('CONSUL_HOST', 'localhost'),
os.environ.get('CONSUL_PORT', '8500')),
'/v1/kv/', None, None))

with open(file) as fh:
config = json.load(fh)
LOGGER.debug('%r', config)
for k, v in config.items():
consul.kv[k] = v
with requests.Session() as session:
for k, v in config.items():
rsp = session.put(urljoin(kv_root, k.lstrip('/')),
data=str(v).encode())
rsp.raise_for_status()
except Exception:
LOGGER.exception('Failed to load consul data.')
sys.exit(-1)
Expand Down Expand Up @@ -98,13 +105,14 @@ def prep_rabbit(file):
host = os.environ.get('RABBITMQ', 'localhost')
with open(file) as fh:
config = json.load(fh)
for action in config:
uri = 'http://{0}/{1}'.format(host, action['path'])
LOGGER.debug('%s', action)
r = requests.request(
url=uri, method=action['method'],
auth=('guest', 'guest'), json=action['body'])
r.raise_for_status()
with requests.Session() as session:
for action in config:
uri = 'http://{0}/{1}'.format(host, action['path'])
LOGGER.debug('%s', action)
r = session.request(url=uri, method=action['method'],
auth=('guest', 'guest'),
json=action['body'])
r.raise_for_status()
except Exception:
LOGGER.exception('Failed to configure rabbit.')
sys.exit(-1)
Expand All @@ -116,33 +124,34 @@ def prep_http(file):
re.IGNORECASE)
with open(file) as fh:
input_requests = json.load(fh)
for request in input_requests:
try:
url = request['url']
matches = var_pattern.findall(url)
for var_name in matches:
if var_name in os.environ:
url = url.replace('${}'.format(var_name),
os.environ[var_name])
parts = urlsplit(url)
user, password = parts.username, parts.password
hostname, port = parts.hostname, parts.port
if port:
netloc = '{}:{}'.format(hostname, port)
else:
netloc = hostname

request['url'] = urlunsplit((
parts.scheme, netloc, parts.path, parts.query,
parts.fragment))
request.setdefault('method', 'GET')
request.setdefault('auth', (user, password))
LOGGER.debug('making HTTP request %r', request)
r = requests.request(**request)
r.raise_for_status()
except Exception:
LOGGER.exception('HTTP request %r failed.', request)
sys.exit(-1)
with requests.Session() as session:
for request in input_requests:
try:
url = request['url']
matches = var_pattern.findall(url)
for var_name in matches:
if var_name in os.environ:
url = url.replace('${}'.format(var_name),
os.environ[var_name])
parts = urlsplit(url)
user, password = parts.username, parts.password
hostname, port = parts.hostname, parts.port
if port:
netloc = '{}:{}'.format(hostname, port)
else:
netloc = hostname

request['url'] = urlunsplit((
parts.scheme, netloc, parts.path, parts.query,
parts.fragment))
request.setdefault('method', 'GET')
request.setdefault('auth', (user, password))
LOGGER.debug('making HTTP request %r', request)
r = session.request(**request)
r.raise_for_status()
except Exception:
LOGGER.exception('HTTP request %r failed.', request)
sys.exit(-1)


def run():
Expand Down
8 changes: 7 additions & 1 deletion docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
Release History
===============

`3.3.0`_ (2021-09-02)
---------------------
- Replace consulate usage with requests
- Add classifiers for 3.8 & 3.9.

`3.2.0`_ (2019-04-05)
---------------------
- Use ``psycopg2`` instead of ``queries``
Expand Down Expand Up @@ -73,7 +78,8 @@ Release History
- Initial release of the PrepIt package
- Import @briank's work on prepit.

.. _Next Release: https://github.com/aweber/bandoleers/compare/3.2.0...HEAD
.. _Next Release: https://github.com/aweber/bandoleers/compare/3.3.0...HEAD
.. _3.3.0: https://github.com/aweber/bandoleers/compare/3.2.0...3.3.0
.. _3.2.0: https://github.com/aweber/bandoleers/compare/3.1.0...3.2.0
.. _3.1.0: https://github.com/aweber/bandoleers/compare/3.0.0...3.1.0
.. _3.0.0: https://github.com/aweber/bandoleers/compare/2.1.0...3.0.0
Expand Down
1 change: 0 additions & 1 deletion requires/installation.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
consulate>=0.5.1,<2
psycopg2>=2.6,<3
requests>=2.5.1,<3
redis>=2,<4
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,7 @@ def read_requirements(name):
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
],
)

0 comments on commit c8d5991

Please sign in to comment.