Added initial files
This commit is contained in:
commit
8042c460ea
56
bin/client.py
Executable file
56
bin/client.py
Executable file
@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import json
|
||||
import socket
|
||||
from gearman import GearmanClient, DataEncoder
|
||||
|
||||
class JSONDataEncoder(DataEncoder):
|
||||
@classmethod
|
||||
def encode(cls, encodable_object):
|
||||
s = json.dumps(encodable_object)
|
||||
print("Encoding JSON object to string: %s" % s)
|
||||
return s
|
||||
|
||||
@classmethod
|
||||
def decode(cls, decodable_string):
|
||||
s = json.loads(decodable_string)
|
||||
print("Decoding string (%s) to JSON object" % s)
|
||||
return s
|
||||
|
||||
class JSONGearmanClient(GearmanClient):
|
||||
data_encoder = JSONDataEncoder
|
||||
|
||||
def check_request_status(job_request):
|
||||
if job_request.complete:
|
||||
print "Job %s finished! Result: %s -\n%s" % (job_request.job.unique,
|
||||
job_request.state,
|
||||
json.dumps(job_request.result, indent=2))
|
||||
elif job_request.timed_out:
|
||||
print "Job %s timed out!" % job_request.unique
|
||||
elif job_request.state == JOB_UNKNOWN:
|
||||
print "Job %s connection failed!" % job_request.unique
|
||||
|
||||
def main():
|
||||
my_ip = socket.gethostbyname(socket.gethostname())
|
||||
task = "lbaas-%s" % my_ip
|
||||
client = JSONGearmanClient(['localhost:4730'])
|
||||
data = """
|
||||
{
|
||||
"name": "a-new-loadbalancer",
|
||||
"nodes": [
|
||||
{
|
||||
"address": "10.1.1.1",
|
||||
"port": "80"
|
||||
},
|
||||
{
|
||||
"address": "10.1.1.2",
|
||||
"port": "81"
|
||||
}
|
||||
]
|
||||
}
|
||||
"""
|
||||
request = client.submit_job(task, data)
|
||||
check_request_status(request)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
0
lbaas/__init__.py
Normal file
0
lbaas/__init__.py
Normal file
22
lbaas/json_gearman.py
Normal file
22
lbaas/json_gearman.py
Normal file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import json
|
||||
from gearman import GearmanWorker, DataEncoder
|
||||
|
||||
class JSONDataEncoder(DataEncoder):
|
||||
""" Class to transform data that the worker either receives or sends. """
|
||||
|
||||
@classmethod
|
||||
def encode(cls, encodable_object):
|
||||
""" Encode JSON object as string """
|
||||
return json.dumps(encodable_object)
|
||||
|
||||
@classmethod
|
||||
def decode(cls, decodable_string):
|
||||
""" Decode string to JSON object """
|
||||
return json.loads(decodable_string)
|
||||
|
||||
|
||||
class JSONGearmanWorker(GearmanWorker):
|
||||
""" Overload the Gearman worker class so we can set the data encoder. """
|
||||
data_encoder = JSONDataEncoder
|
0
lbaas/tests/__init__.py
Normal file
0
lbaas/tests/__init__.py
Normal file
38
lbaas/tests/test_lbaas_task.py
Normal file
38
lbaas/tests/test_lbaas_task.py
Normal file
@ -0,0 +1,38 @@
|
||||
import json
|
||||
import unittest
|
||||
from lbaas.worker import lbaas_task
|
||||
|
||||
class FakeJob(object):
|
||||
def __init__(self, data):
|
||||
"""
|
||||
data: JSON object to convert to a string
|
||||
"""
|
||||
self.data = json.dumps(data)
|
||||
|
||||
class TestLBaaSTask(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def tearDown(self):
|
||||
pass
|
||||
|
||||
def testLBaaSTask(self):
|
||||
""" Test the lbaas_task() function """
|
||||
|
||||
worker = None
|
||||
data = { "name": "a-new-loadbalancer",
|
||||
"nodes": [
|
||||
{
|
||||
"address": "10.1.1.1",
|
||||
"port": "80"
|
||||
},
|
||||
{
|
||||
"address": "10.1.1.2",
|
||||
"port": "81"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
job = FakeJob(data)
|
||||
r = lbaas_task(worker, job)
|
||||
self.assertNotEqual(r, "None")
|
61
lbaas/worker.py
Normal file
61
lbaas/worker.py
Normal file
@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import logging
|
||||
import socket
|
||||
from json_gearman import JSONGearmanWorker
|
||||
|
||||
def lbaas_task(worker, job):
|
||||
""" Main Gearman worker task. """
|
||||
|
||||
# Turn string into JSON object
|
||||
data = json.loads(job.data)
|
||||
|
||||
lb_name = data['name']
|
||||
logging.info("LB name: %s" % lb_name)
|
||||
|
||||
for lb_node in data['nodes']:
|
||||
port, address, status = None, None, None
|
||||
if 'port' in lb_node:
|
||||
port = lb_node['port']
|
||||
if 'address' in lb_node:
|
||||
address = lb_node['address']
|
||||
if 'status' in lb_node:
|
||||
status = lb_node['status']
|
||||
|
||||
logging.info("LB node: %s:%s - %s" % (address, port, status))
|
||||
lb_node['status'] = 'ACTIVE'
|
||||
|
||||
# Return the same JSON object, but with status fields set.
|
||||
return data
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('--debug',
|
||||
help='enable debug output',
|
||||
action='store_true')
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.debug:
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
else:
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
my_ip = socket.gethostbyname(socket.gethostname())
|
||||
task_name = "lbaas-%s" % my_ip
|
||||
logging.debug("Registering task %s" % task_name)
|
||||
|
||||
worker = JSONGearmanWorker(['localhost:4730'])
|
||||
worker.set_client_id(my_ip)
|
||||
worker.register_task(task_name, lbaas_task)
|
||||
|
||||
try:
|
||||
worker.work()
|
||||
except KeyboardInterrupt:
|
||||
logging.debug("Quitting")
|
||||
except Exception as e:
|
||||
logging.critical("Exception: %s, %s" % (e.__class__, e))
|
||||
|
||||
return 0
|
4
setup.cfg
Normal file
4
setup.cfg
Normal file
@ -0,0 +1,4 @@
|
||||
[nosetests]
|
||||
verbosity=2
|
||||
detailed-errors=1
|
||||
where=lbaas/tests
|
17
setup.py
Normal file
17
setup.py
Normal file
@ -0,0 +1,17 @@
|
||||
import setuptools
|
||||
|
||||
setuptools.setup(
|
||||
name = "lbaas_worker",
|
||||
description = "Python LBaaS Gearman Worker",
|
||||
version = "1.0",
|
||||
author = "David Shrewsbury",
|
||||
author_email = "shrewsbury.dave@gmail.com",
|
||||
packages = setuptools.find_packages(exclude=["*.tests"]),
|
||||
test_suite = 'nose.collector',
|
||||
entry_points = {
|
||||
'console_scripts': [
|
||||
'lbaas_worker = lbaas.worker:main'
|
||||
]
|
||||
},
|
||||
install_requires = ['gearman'],
|
||||
)
|
1
tools/pip-requires
Normal file
1
tools/pip-requires
Normal file
@ -0,0 +1 @@
|
||||
gearman
|
1
tools/test-requires
Normal file
1
tools/test-requires
Normal file
@ -0,0 +1 @@
|
||||
nose
|
Loading…
x
Reference in New Issue
Block a user