Files
libra/bin/client.py
Monty Taylor d9c6b5805e Update to pbr and flake8.
As part of this, found a discrepancy between the git tags and what was in
libra.__version__ - which is the whole reason to do tag-based-versions anyway.
After landing this, someone should do a "tag -s 1.2" or something similar
to release a properly tagged version of the code.

Change-Id: I2bb678bc6928dffebb0abc7c67cec50642b1f43d
2013-04-08 21:21:37 -04:00

73 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python
import json
import socket
from gearman import GearmanClient, DataEncoder, JOB_UNKNOWN
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():
hostname = socket.gethostname()
task = hostname
client = JSONGearmanClient(['localhost:4730'])
data = """
{
"hpcs_action": "update",
"loadbalancers": [
{
"name": "a-new-loadbalancer",
"protocol": "http",
"nodes": [
{
"address": "10.1.1.1",
"port": "80"
},
{
"address": "10.1.1.2",
"port": "81"
}
]
}
]
}
"""
# Worker class expects the data as a JSON object, not string
json_data = json.loads(data)
request = client.submit_job(task, json_data)
check_request_status(request)
if __name__ == "__main__":
main()