Skip to content

Commit be05473

Browse files
committed
Add rest server/client
1 parent 37233cb commit be05473

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

restclient.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import requests
2+
import json
3+
import logging
4+
from collections import namedtuple
5+
6+
GrowDecision = namedtuple("GrowDecision", "cores_to_grow nodes_to_grow sockets_to_grow")
7+
IdleNode = namedtuple("IdleNode", "node_name idle_since")
8+
9+
class AutoScaleRestClient(object):
10+
def __init__(self, hostname="localhost"):
11+
self.hostname = hostname
12+
self.grow_decision_api_route = "https://{}/HpcManager/api/auto-scale/grow-decision"
13+
self.check_nodes_idle_route = "https://{}/HpcManager/api/auto-scale/check-nodes-idle"
14+
self.logger = logging.getLogger("hpcframwork.restclient")
15+
self.logger.setLevel(logging.DEBUG)
16+
fh = logging.FileHandler('hpcframwork.restclient.log')
17+
fh.setLevel(logging.DEBUG)
18+
ch = logging.StreamHandler()
19+
ch.setLevel(logging.ERROR)
20+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
21+
fh.setFormatter(formatter)
22+
ch.setFormatter(formatter)
23+
self.logger.addHandler(fh)
24+
self.logger.addHandler(ch)
25+
26+
27+
def get_grow_decision(self):
28+
url = self.grow_decision_api_route.format(self.hostname)
29+
res = requests.post(url, verify = False)
30+
if res.ok:
31+
self.logger.info(res.content)
32+
jobj = json.loads(res.content)
33+
return GrowDecision(jobj['CoresToGrow'], jobj['NodesToGrow'], jobj['SocketsToGrow'])
34+
else:
35+
self.logger.error("status_code:{} content:{}".format(res.status_code, res.content))
36+
37+
def check_nodes_idle(self, nodes):
38+
headers = {"Content-Type": "application/json"}
39+
url = self.check_nodes_idle_route.format(self.hostname)
40+
res = requests.post(url, data = nodes, headers = headers, verify = False)
41+
if res.ok:
42+
self.logger.info(res.content)
43+
jobjs = json.loads(res.content)
44+
return [IdleNode(idle_info['NodeName'], idle_info['IdleSince']) for idle_info in jobjs]
45+
else:
46+
self.logger.error("status_code:{} content:{}".format(res.status_code, res.content))
47+
48+
if __name__ == '__main__':
49+
client = AutoScaleRestClient()
50+
ans = client.get_grow_decision()
51+
print ans.cores_to_grow
52+
print client.check_nodes_idle(json.dumps(['mesoswinagent', 'mesoswinagent2']))

restserver.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
2+
import json
3+
4+
class RestServer(object):
5+
class S(BaseHTTPRequestHandler):
6+
def _set_headers(self):
7+
self.send_response(200)
8+
self.send_header('Content-type', 'text/html')
9+
self.end_headers()
10+
11+
def do_GET(self):
12+
self._set_headers()
13+
self.wfile.write("<html><body><h1>hi!</h1></body></html>")
14+
15+
def do_HEAD(self):
16+
self._set_headers()
17+
18+
def do_POST(self):
19+
# Doesn't do anything with posted data
20+
content_length = int(self.headers['Content-Length']) # <--- Gets the size of data
21+
post_data = self.rfile.read(content_length) # <--- Gets the data itself
22+
self._set_headers()
23+
json_obj = json.loads(post_data)
24+
self.wfile.write("<html><body><h1>POST!</h1><pre>" + str(json_obj) + "</pre></body></html>")
25+
26+
def __init__(self, port = 80):
27+
self._server_address = ('', port)
28+
self._server_class = HTTPServer
29+
self._handler_class = self.S
30+
self._port = port
31+
self._httpd = self._server_class(self._server_address, self._handler_class)
32+
33+
def run(self):
34+
print 'Starting httpd...'
35+
self._httpd.serve_forever()
36+
37+
def stop(self):
38+
self._httpd.shutdown()
39+
40+
# if __name__ == "__main__":
41+
# from sys import argv
42+
#
43+
# if len(argv) == 2:
44+
# run(port=int(argv[1]))
45+
# else:
46+
# run()

0 commit comments

Comments
 (0)