def start(self): """Start ZAP authentication""" super().start() self.__poller = Poller() self.__poller.register(self.zap_socket, zmq.POLLIN) self.__task = asyncio.ensure_future(self.__handle_zap())
def __init__(self, context, backend_addr, friendly_name, nb_sub_agents, task_directory, ssh_host=None, ssh_ports=None, tmp_dir="./agent_tmp"): """ :param context: ZeroMQ context for this process :param backend_addr: address of the backend (for example, "tcp://127.0.0.1:2222") :param friendly_name: a string containing a friendly name to identify agent :param nb_sub_agents: nb of slots available for this agent :param task_directory: path to the task directory :param ssh_host: hostname/ip/... to which external client should connect to access to an ssh remote debug session :param ssh_ports: iterable containing ports to which the docker instance can assign ssh servers (for remote debugging) :param tmp_dir: temp dir that is used by the agent to start new containers """ self._logger = logging.getLogger("inginious.agent.docker") self._logger.info("Starting agent") self._backend_addr = backend_addr self._context = context self._loop = asyncio.get_event_loop() self._friendly_name = friendly_name self._nb_sub_agents = nb_sub_agents self._max_memory_per_slot = int(psutil.virtual_memory().total/nb_sub_agents/1024/1024) # data about running containers self._containers_running = {} self._student_containers_running = {} self._containers_ending = {} self._student_containers_ending = {} self._container_for_job = {} self._student_containers_for_job = {} self.tmp_dir = tmp_dir self.task_directory = task_directory # Delete tmp_dir, and recreate-it again try: rmtree(tmp_dir) except: pass try: os.mkdir(tmp_dir) except OSError: pass # Docker self._docker = DockerInterface() # Auto discover containers self._logger.info("Discovering containers") self._containers = self._docker.get_containers() # SSH remote debug self.ssh_host = ssh_host if self.ssh_host is None and len(self._containers) != 0: self._logger.info("Guessing external host IP") self.ssh_host = self._docker.get_host_ip(next(iter(self._containers.values()))["id"]) if self.ssh_host is None: self._logger.warning("Cannot find external host IP. Please indicate it in the configuration. Remote SSH debug has been deactivated.") ssh_ports = None else: self._logger.info("External address for SSH remote debug is %s", self.ssh_host) self.ssh_ports = set(ssh_ports) if ssh_ports is not None else set() self.running_ssh_debug = {} # container_id : ssh_port # Sockets self._backend_socket = self._context.socket(zmq.DEALER) self._backend_socket.ipv6 = True self._docker_events_publisher = self._context.socket(zmq.PUB) self._docker_events_subscriber = self._context.socket(zmq.SUB) # Watchers self._killer_watcher_push = PipelinePush(context, "agentpush") self._killer_watcher_pull = PipelinePull(context, "agentpull") self._timeout_watcher = TimeoutWatcher(context, self._docker) self._containers_killed = dict() # Poller self._poller = Poller() self._poller.register(self._backend_socket, zmq.POLLIN) self._poller.register(self._docker_events_subscriber, zmq.POLLIN) self._poller.register(self._killer_watcher_pull.get_pull_socket(), zmq.POLLIN)