hpcpack-mesos/restclient.py

47 строки
1.9 KiB
Python
Исходник Обычный вид История

2018-03-09 06:26:38 +03:00
import json
import logging
from collections import namedtuple
2018-03-13 12:24:55 +03:00
import requests
2018-03-09 07:31:51 +03:00
import logging_aux
2018-03-09 06:26:38 +03:00
GrowDecision = namedtuple("GrowDecision", "cores_to_grow nodes_to_grow sockets_to_grow")
IdleNode = namedtuple("IdleNode", "node_name timestamp server_name")
2018-03-09 06:26:38 +03:00
2018-03-13 12:24:55 +03:00
class HpcRestClient(object):
2018-03-09 06:26:38 +03:00
def __init__(self, hostname="localhost"):
self.hostname = hostname
self.grow_decision_api_route = "https://{}/HpcManager/api/auto-scale/grow-decision"
self.check_nodes_idle_route = "https://{}/HpcManager/api/auto-scale/check-nodes-idle"
2018-03-09 07:31:51 +03:00
self.logger = logging_aux.init_logger_aux("hpcframework.restclient", 'hpcframework.restclient.log')
2018-03-09 06:26:38 +03:00
def get_grow_decision(self):
url = self.grow_decision_api_route.format(self.hostname)
2018-03-13 12:24:55 +03:00
res = requests.post(url, verify=False)
2018-03-09 06:26:38 +03:00
if res.ok:
self.logger.info(res.content)
jobj = json.loads(res.content)
return GrowDecision(jobj['CoresToGrow'], jobj['NodesToGrow'], jobj['SocketsToGrow'])
else:
self.logger.error("status_code:{} content:{}".format(res.status_code, res.content))
2018-03-13 12:24:55 +03:00
2018-03-09 06:26:38 +03:00
def check_nodes_idle(self, nodes):
headers = {"Content-Type": "application/json"}
url = self.check_nodes_idle_route.format(self.hostname)
2018-03-13 12:24:55 +03:00
res = requests.post(url, data=nodes, headers=headers, verify=False)
2018-03-09 06:26:38 +03:00
if res.ok:
2018-03-15 12:12:02 +03:00
self.logger.info("check_nodes_idle:" + res.content)
2018-03-13 12:24:55 +03:00
jobjs = json.loads(res.content)
return [IdleNode(idle_info['NodeName'], idle_info['TimeStamp'], idle_info['ServerName']) for idle_info in jobjs]
2018-03-09 06:26:38 +03:00
else:
self.logger.error("status_code:{} content:{}".format(res.status_code, res.content))
2018-03-13 12:24:55 +03:00
2018-03-09 06:26:38 +03:00
if __name__ == '__main__':
client = HpcRestClient()
2018-03-09 06:26:38 +03:00
ans = client.get_grow_decision()
print ans.cores_to_grow
2018-03-13 12:24:55 +03:00
print client.check_nodes_idle(json.dumps(['mesoswinagent', 'mesoswinagent2']))