import glob import json import requests from utils import Question class Host(object): def __init__(self, config): self.type = 'host' self.config = config self.questions = [] self._load_questions(config['question_path']) self.worker = dict() self.setup_workers() def __repr__(self): return "Host" def _load_questions(self, path): questions_path = glob.glob(path) questions_path = sorted(questions_path) for v in questions_path: question = Question(v) self.questions.append(question) def setup_workers(self): for v in self.config['worker']: worker_name = v['name'] self.worker[worker_name] = Worker(v) def get_questions(self): return self.questions def call_api(self, method, cmd, params): if cmd == 'role': return {'role': self.type} else: return None class Worker(object): def __init__(self, params): self.address = params['address'] self.name = params['name'] self.role = None self.set_role(params['role']) def post(self, path, data): response = requests.post( f'http://{self.address}/api/{path}', json.dumps(data), headers={'Content-Type': 'application/json'}) return response def set_role(self, role): r = self.post('role', {'role': role})