示例#1
0
    def _check_machine(self, machine: Machine):
        """Method checks if the agent process inside of the container is running."""
        hostname = machine.get_hostname()
        logging.debug(f"Checking connection with machine {hostname}")
        connection = socket.socket()
        connection.settimeout(self._start_timeout)

        retry_counter = 5

        for i in range(retry_counter):
            logging.debug(
                f"Connecting to {machine.get_hostname()}, retry counter: {i}")
            try:
                connection.connect((hostname, machine._port))
            except (ConnectionRefusedError, ConnectionAbortedError):
                sleep(1)
                continue

            logging.debug(f"Connected to agent process at machine {hostname}")
            break  # successfully connected
        else:
            raise PoolManagerError(f"Could not connect to machine {hostname}")

        err = connection.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
        if err:
            connection.close()
            raise PoolManagerError(f"Could not connect to machine {hostname}")

        connection.shutdown(socket.SHUT_RDWR)
        connection.close()

        logging.info(f"Agent process is running at {hostname}")
示例#2
0
    def _create_container(self, name: str, reqs: dict):
        logging.info("Creating container " + name)

        if "rpc_port" in reqs:
            rpc_port = reqs["rpc_port"]
        else:
            rpc_port = None

        self._pool[name] = {
            "interfaces": {},
            "params": {
                "hostname": "",
                "rpc_port": rpc_port
            },
            "security": {
                "auth_type": "none"
            },
            "available": True,
        }

        try:
            container = self._podman_client.containers.create(self.image,
                                                              hostname=name,
                                                              privileged=True)
        except APIError as e:
            raise PoolManagerError(f"Could not create container {name}: {e}")

        machine = Machine(
            name,
            "",
            self._msg_dispatcher,
            self._ctl_config,
            None,
            rpc_port,
            self._pool[name]["security"],
            reqs,
        )  # to get hostname the container needs to run

        self._start_container(container, machine)
        if self._pool_check:
            self._check_machine(
                machine)  # checks if the agent process is already running

        self._pool[name]["params"]["hostname"] = machine.get_hostname()

        return container, machine