Пример #1
0
    def __init__(self,
                 url=None,
                 connector=None,
                 session=None,
                 ssl_context=None,
                 api_version='v1.35'):

        docker_host = url  # rename
        if docker_host is None:
            docker_host = os.environ.get('DOCKER_HOST', None)
        if docker_host is None:
            for sockpath in _sock_search_paths:
                if sockpath.is_socket():
                    docker_host = 'unix://' + str(sockpath)
                    break
        self.docker_host = docker_host

        assert _rx_version.search(api_version) is not None, \
            'Invalid API version format'
        self.api_version = api_version

        if docker_host is None:
            raise ValueError(
                "Missing valid docker_host."
                "Either DOCKER_HOST or local sockets are not available.")

        if connector is None:
            if _rx_tcp_schemes.search(docker_host):
                if os.environ.get('DOCKER_TLS_VERIFY', '0') == '1':
                    ssl_context = self._docker_machine_ssl_context()
                    docker_host = _rx_tcp_schemes.sub('https://', docker_host)
                else:
                    ssl_context = None
                connector = aiohttp.TCPConnector(ssl_context=ssl_context)
                self.docker_host = docker_host
            elif docker_host.startswith('unix://'):
                connector = aiohttp.UnixConnector(docker_host[7:])
                # dummy hostname for URL composition
                self.docker_host = "unix://localhost"
            else:
                raise ValueError('Missing protocol scheme in docker_host.')
        self.connector = connector
        if session is None:
            session = aiohttp.ClientSession(connector=self.connector)
        self.session = session

        self.events = DockerEvents(self)
        self.containers = DockerContainers(self)
        self.swarm = DockerSwarm(self)
        self.services = DockerServices(self)
        self.tasks = DockerTasks(self)
        self.images = DockerImages(self)
        self.volumes = DockerVolumes(self)
        self.networks = DockerNetworks(self)
        self.nodes = DockerSwarmNodes(self)
        self.system = DockerSystem(self)

        # legacy aliases
        self.pull = self.images.pull
        self.push = self.images.push
Пример #2
0
async def is_portal():
    logger.debug("Connecting to captive portal socket...")
    conn = aiohttp.UnixConnector(path=app["captive-portal"])
    async with aiohttp.ClientSession(connector=conn) as session:
        async with session.get('http://localhost/portal') as resp:
            logger.debug("Received captive portal response: %s", resp.status)
            return await resp.json()
Пример #3
0
    def _get_conn(
        self,
        remote_runner_mapping: t.Dict[str, str] = Provide[
            DeploymentContainer.remote_runner_mapping],
    ) -> "BaseConnector":
        import aiohttp

        if (self._loop is None or self._conn is None or self._conn.closed
                or self._loop.is_closed()):
            self._loop = asyncio.get_event_loop()
            bind_uri = remote_runner_mapping[self._runner.name]
            parsed = urlparse(bind_uri)
            if parsed.scheme == "file":
                path = uri_to_path(bind_uri)
                self._conn = aiohttp.UnixConnector(
                    path=path,
                    loop=self._loop,
                    limit=self._runner.batch_options.max_batch_size * 2,
                    keepalive_timeout=self._runner.batch_options.max_latency_ms
                    * 1000 * 10,
                )
                self._addr = "http://127.0.0.1:8000"  # addr doesn't matter with UDS
            elif parsed.scheme == "tcp":
                self._conn = aiohttp.TCPConnector(
                    loop=self._loop,
                    limit=self._runner.batch_options.max_batch_size * 2,
                    verify_ssl=False,
                    keepalive_timeout=self._runner.batch_options.max_latency_ms
                    * 1000 * 10,
                )
                self._addr = f"http://{parsed.netloc}"
            else:
                raise ValueError(f"Unsupported bind scheme: {parsed.scheme}")
        return self._conn
Пример #4
0
    async def start(self):
        if self.lxd_url.scheme == "unix":
            path = Path(self.lxd_url.path)

            if not path.is_socket():
                raise RuntimeError("Path %s is not a unix socket" % path)
            connector = aiohttp.UnixConnector(path=str(path))
            self._ssl_context = None
            self.lxd_url = URL.build(
                scheme="http",
                host="lxd",
                user=self.lxd_url.user or "",
                password=self.lxd_url.password or "",
                query_string=self.lxd_url.query_string or "",
                authority=self.lxd_url.authority or "",
                port=self.lxd_url.port,
                path="/",
            )
        else:
            connector = aiohttp.TCPConnector()
            self._ssl_context = ssl.SSLContext()
            self._ssl_context.load_verify_locations(
                str(self.lxd_cert.expanduser()), )
            self._ssl_context.load_cert_chain(
                str(self.client_cert.expanduser()),
                str(self.client_key.expanduser()),
            )

        self._client = aiohttp.ClientSession(
            connector=connector,
            connector_owner=True,
            raise_for_status=True,
        )

        await super().start()
Пример #5
0
async def logs(cont, name):
    conn = aiohttp.UnixConnector(path="/var/run/docker.sock")
    async with aiohttp.ClientSession(connector=conn) as session:
        async with session.get(
                f"http://xx/containers/{cont}/logs?follow=1&stdout=1") as resp:
            async for line in resp.content:
                print(name, line)
Пример #6
0
 async def get_page_task():
     conn = aiohttp.UnixConnector(socket_path)
     async with aiohttp.ClientSession(connector=conn,
                                      auto_decompress=False) as session:
         # proxy the request to the module
         async with session.get('http://localhost/') as resp:
             return await resp.text()
Пример #7
0
    def __init__(self, opts):
        if is_linux_tty():
            self.input_filter = KeyCodesFilter()
        else:
            self.input_filter = DummyKeycodesFilter()

        self.help_menu = HelpMenu(self)
        super().__init__(opts)
        self.interactive = None
        self.server_updated = None
        self.restarting_server = False
        self.global_overlays = []

        try:
            self.our_tty = os.ttyname(0)
        except OSError:
            self.our_tty = "not a tty"

        self.conn = aiohttp.UnixConnector(self.opts.socket)
        self.client = make_client_for_conn(API, self.conn, self.resp_hook)

        self.error_reporter = ErrorReporter(
            self.context.child("ErrorReporter"), self.opts.dry_run, self.root,
            self.client)

        self.note_data_for_apport("SnapUpdated", str(self.updated))
        self.note_data_for_apport("UsingAnswers", str(bool(self.answers)))
Пример #8
0
    def __init__(self,
                 host: str = 'localhost',
                 port: int = 8086,
                 mode: str = 'async',
                 output: str = 'raw',
                 db: Optional[str] = None,
                 *,
                 ssl: bool = False,
                 unix_socket: Optional[str] = None,
                 username: Optional[str] = None,
                 password: Optional[str] = None,
                 database: Optional[str] = None,
                 loop: Optional[asyncio.BaseEventLoop] = None,
                 ):
        """
        The InfluxDBClient object holds information necessary to interact with InfluxDB.
        It is async by default, but can also be used as a sync/blocking client.
        When querying, responses are returned as raw JSON by default, but can also be wrapped in easily iterable
        wrapper object or be parsed to Pandas DataFrames.
        The three main public methods are the three endpoints of the InfluxDB API, namely:
        1) InfluxDBClient.ping
        2) InfluxDBClient.write
        3) InfluxDBClient.query
        See each of the above methods documentation for further usage details.
        See also: https://docs.influxdata.com/influxdb/latest/tools/api/

        :param host: Hostname to connect to InfluxDB.
        :param port: Port to connect to InfluxDB.
        :param mode: Mode in which client should run.
            Available options are: 'async', 'blocking' and 'dataframe'.
            - 'async': Default mode. Each query/request to the backend will
            - 'blocking': Behaves in sync/blocking fashion, similar to the official InfluxDB-Python client.
        :param output: Output format of the response received from InfluxDB.
            - 'raw': Default format. Returns JSON as received from InfluxDB.
            - 'iterable': Wraps the raw response in a `InfluxDBResult` or `InfluxDBChunkedResult`,
                          which can be used for easier iteration over retrieved data points.
            - 'dataframe': Parses results into Pandas DataFrames. Not compatible with chunked responses.
        :param db: Default database to be used by the client.
        :param ssl: If https should be used.
        :param unix_socket: Path to the InfluxDB Unix domain socket.
        :param username: Username to use to connect to InfluxDB.
        :param password: User password.
        :param database: Default database to be used by the client.
            This field is for argument consistency with the official InfluxDB Python client.
        :param loop: Asyncio event loop.
        """
        self._loop = asyncio.get_event_loop() if loop is None else loop
        self._connector = aiohttp.UnixConnector(path=unix_socket, loop=self._loop) if unix_socket else None
        self._auth = aiohttp.BasicAuth(username, password) if username and password else None
        self._session = aiohttp.ClientSession(loop=self._loop, auth=self._auth, connector=self._connector)
        self._url = f'{"https" if ssl else "http"}://{host}:{port}/{{endpoint}}'
        self.host = host
        self.port = port
        self._mode = None
        self._output = None
        self._db = None
        self.tag_cache = defaultdict(lambda: defaultdict(dict))
        self.mode = mode
        self.output = output
        self.db = database or db
Пример #9
0
 async def run(self):
     await self.load_images()
     self.real_cam.start()
     if self.socket != "":
         conn = aiohttp.UnixConnector(path=self.socket)
     else:
         conn = None
     async with aiohttp.ClientSession(connector=conn) as session:
         t0 = time.monotonic()
         print_fps_period = 1
         frame_count = 0
         while True:
             frame = self.real_cam.read()
             if frame is None:
                 await asyncio.sleep(0.1)
                 continue
             frame = await self.mask_frame(session, frame)
             self.put_frame(frame)
             frame_count += 1
             td = time.monotonic() - t0
             if td > print_fps_period:
                 self.current_fps = frame_count / td
                 print("FPS: {:6.2f}".format(self.current_fps), end="\r")
                 frame_count = 0
                 t0 = time.monotonic()
Пример #10
0
 def __ensure_session(self) -> aiohttp.ClientSession:
     if not self.__http_session:
         if self.__unix_path:
             self.__http_session = aiohttp.ClientSession(
                 connector=aiohttp.UnixConnector(path=self.__unix_path))
         else:
             self.__http_session = aiohttp.ClientSession()
     return self.__http_session
Пример #11
0
async def start_ap():
    await sleep(1)
    logger.debug("Connecting to captive portal socket...")
    conn = aiohttp.UnixConnector(path=app["captive-portal"])
    async with aiohttp.ClientSession(connector=conn) as session:
        async with session.get('http://localhost/start-ap') as resp:
            logger.debug("Received captive portal response: %s", resp.status)
            return resp.status
Пример #12
0
 def initSession(self):
     if self.s is None:
         if self.url.startswith("unix:"):
             conn = aiohttp.UnixConnector(path=self.url[5:])
             self.s = aiohttp.ClientSession(connector=conn)
             self.url = "http://unixsocket/"
         else:
             self.s = aiohttp.ClientSession()
Пример #13
0
def test_unix_connector_not_found(loop):
    connector = aiohttp.UnixConnector('/' + uuid.uuid4().hex, loop=loop)

    req = ClientRequest(
        'GET', URL('http://www.python.org'),
        loop=loop,
    )
    with pytest.raises(aiohttp.ClientConnectorError):
        loop.run_until_complete(connector.connect(req))
Пример #14
0
 async def subscription_PUT(self, socket_path: str) -> None:
     log.debug('added subscription %s', socket_path)
     conn = aiohttp.UnixConnector(socket_path)
     client = make_client_for_conn(NetEventAPI, conn)
     lock = asyncio.Lock()
     self.clients[socket_path] = (client, conn, lock)
     self.app.aio_loop.create_task(
         self._call_client(client, conn, lock, "route_watch",
                           self.network_event_receiver.default_routes))
Пример #15
0
 def __make_http_session(self) -> aiohttp.ClientSession:
     kwargs: Dict = {
         "headers": {
             "User-Agent": htclient.make_user_agent("KVMD"),
         },
         "timeout": aiohttp.ClientTimeout(total=self.__timeout),
         "connector": aiohttp.UnixConnector(path=self.__unix_path)
     }
     return aiohttp.ClientSession(**kwargs)
Пример #16
0
    def get_conn(self) -> "BaseConnector":
        import aiohttp

        if self._conn is None or self._conn.closed:
            if self.outbound_unix_socket:
                self._conn = aiohttp.UnixConnector(path=self.outbound_unix_socket,)
            else:
                self._conn = aiohttp.TCPConnector(limit=30)
        return self._conn
Пример #17
0
    def __init__(
        self,
        host: str = 'localhost',
        port: int = 8086,
        mode: str = 'async',
        db: str = 'testdb',
        *,
        ssl: bool = False,
        unix_socket: Optional[str] = None,
        username: Optional[str] = None,
        password: Optional[str] = None,
        database: Optional[str] = None,
        loop: Optional[asyncio.BaseEventLoop] = None,
    ):
        """
        The InfluxDBClient object holds information necessary to interact with InfluxDB.
        It is async by default, but can also be used as a sync/blocking client and even generate
        Pandas DataFrames from queries.
        The three main public methods are the three endpoints of the InfluxDB API, namely:
        1) InfluxDBClient.ping
        2) InfluxDBClient.write
        3) InfluxDBClient.query
        See each of the above methods documentation for further usage details.
        See also: https://docs.influxdata.com/influxdb/latest/tools/api/

        :param host: Hostname to connect to InfluxDB.
        :param port: Port to connect to InfluxDB.
        :param mode: Mode in which client should run.
            Available options are: 'async', 'blocking' and 'dataframe'.
            - 'async': Default mode. Each query/request to the backend will
            - 'blocking': Behaves in sync/blocking fashion, similar to the official InfluxDB-Python client.
            - 'dataframe': Behaves in a sync/blocking fashion, but parsing results into Pandas DataFrames.
                           Similar to InfluxDB-Python's `DataFrameClient`.
        :param db: Default database to be used by the client.
        :param ssl: If https should be used.
        :param unix_socket: Path to the InfluxDB Unix domain socket.
        :param username: Username to use to connect to InfluxDB.
        :param password: User password.
        :param database: Default database to be used by the client.
            This field is for argument consistency with the official InfluxDB Python client.
        :param loop: Event loop used for processing HTTP requests.
        """
        self._loop = asyncio.get_event_loop() if loop is None else loop
        self._connector = aiohttp.UnixConnector(
            path=unix_socket, loop=self._loop) if unix_socket else None
        self._auth = aiohttp.BasicAuth(
            username, password) if username and password else None
        self._session = aiohttp.ClientSession(loop=self._loop,
                                              auth=self._auth,
                                              connector=self._connector)
        self._url = f'{"https" if ssl else "http"}://{host}:{port}/{{endpoint}}'
        self.host = host
        self.port = port
        self.db = database or db
        self._mode = None
        self.mode = mode
Пример #18
0
 async def request_coroutine():
     if socktype == 'unix':
         connector = aiohttp.UnixConnector(path=path)
     elif socktype == 'tcp':
         connector = None # This will transform into TCP.
     else:
         raise Exception(f"impossible socktype ({socktype!r})")
     async with aiohttp.ClientSession(auth=auth, connector=connector) as session:
         c = util.JsonRPCClient(session, server_url)
         return await c.request(endpoint, *args)
Пример #19
0
 def __ensure_http_session(self) -> aiohttp.ClientSession:
     if not self.__http_session:
         kwargs: Dict = {
             "headers": {"User-Agent": htclient.make_user_agent("KVMD")},
             "timeout": aiohttp.ClientTimeout(total=self.__timeout),
         }
         if self.__unix_path:
             kwargs["connector"] = aiohttp.UnixConnector(path=self.__unix_path)
         self.__http_session = aiohttp.ClientSession(**kwargs)
     return self.__http_session
Пример #20
0
 def __make_session(self) -> aiohttp.ClientSession:
     kwargs: Dict = {
         "timeout": aiohttp.ClientTimeout(
             connect=self.__timeout,
             sock_read=self.__timeout,
         ),
     }
     if self.__unix_path:
         kwargs["connector"] = aiohttp.UnixConnector(path=self.__unix_path)
     return aiohttp.ClientSession(**kwargs)
Пример #21
0
 def __make_http_session(self) -> aiohttp.ClientSession:
     kwargs: Dict = {
         "headers": {"User-Agent": self.__user_agent},
         "timeout": aiohttp.ClientTimeout(
             connect=self.__timeout,
             sock_read=self.__timeout,
         ),
     }
     if self.__unix_path:
         kwargs["connector"] = aiohttp.UnixConnector(path=self.__unix_path)
     return aiohttp.ClientSession(**kwargs)
Пример #22
0
 def __init__(self, *args, loop=None, **kwargs):
     super(HTTPClient, self).__init__(*args, **kwargs)
     self._loop = loop or asyncio.get_event_loop()
     if 'unix://' in self.base_uri:
         pr = urllib.parse.urlparse(self.base_uri)
         self.base_uri = 'http://consul'
         connector = aiohttp.UnixConnector(loop=self._loop, path=pr.path)
     else:
         connector = aiohttp.TCPConnector(loop=self._loop,
                                          verify_ssl=self.verify)
     self._session = aiohttp.ClientSession(connector=connector)
Пример #23
0
def test_unix_connector_permission(loop):
    loop.create_unix_connection = make_mocked_coro(
        raise_exception=PermissionError())
    connector = aiohttp.UnixConnector('/' + uuid.uuid4().hex, loop=loop)

    req = ClientRequest(
        'GET', URL('http://www.python.org'),
        loop=loop,
    )
    with pytest.raises(aiohttp.ClientConnectorError):
        loop.run_until_complete(connector.connect(req))
Пример #24
0
 def get_client(self):
     if self._client is None:
         jar = aiohttp.DummyCookieJar()
         if self.outbound_unix_socket:
             conn = aiohttp.UnixConnector(path=self.outbound_unix_socket,)
         else:
             conn = aiohttp.TCPConnector(limit=30)
         self._client = aiohttp.ClientSession(
             connector=conn, auto_decompress=False, cookie_jar=jar,
         )
     return self._client
Пример #25
0
async def client(request, unix_socket, event_loop):
    conn = aiohttp.UnixConnector(path=unix_socket)
    session = aiohttp.ClientSession(connector=conn)

    def close():
        async def aclose():
            await session.close()
        event_loop.run_until_complete(aclose())
    request.addfinalizer(close)

    return session
Пример #26
0
def init(cfg: Config, app: web.Application) -> None:
    global http, host, config
    config = cfg
    host = cfg["docker.host"]
    if host.startswith("unix://"):
        http = aiohttp.ClientSession(connector=aiohttp.UnixConnector(path=host[len("unix://"):]),
                                     loop=asyncio.get_event_loop())
        host = "unix://localhost"
    else:
        http = aiohttp.ClientSession(loop=asyncio.get_event_loop())
    host = URL(host)
    app.add_routes(routes)
Пример #27
0
 def _make_session(self, user: str, passwd: str) -> aiohttp.ClientSession:
     kwargs: Dict = {
         "headers": {
             "X-KVMD-User": user,
             "X-KVMD-Passwd": passwd,
             "User-Agent": self.__user_agent,
         },
         "timeout": aiohttp.ClientTimeout(total=self.__timeout),
     }
     if self.__unix_path:
         kwargs["connector"] = aiohttp.UnixConnector(path=self.__unix_path)
     return aiohttp.ClientSession(**kwargs)
Пример #28
0
 async def _wait_container(self):
     with aiohttp.UnixConnector('/var/run/docker.sock') as conn:
         timeout = aiohttp.ClientTimeout(total=self.time)
         async with aiohttp.ClientSession(connector=conn,
                                          timeout=timeout) as session:
             async with session.post(
                     'http://localhost/containers/{}/wait'.format(
                         self._container.id)) as resp:
                 try:
                     return False, await resp.json()
                 except asyncio.TimeoutError:
                     return True, None
Пример #29
0
 def _serve(self, *args):
     with serve("-q", *args) as (loop, server):
         host, port = parse_sockname(server.sockets[0].getsockname())
         if host == "unix":
             connector = aiohttp.UnixConnector(path=port, loop=loop)
         else:
             connector = aiohttp.TCPConnector(loop=loop)
         session = loop.run_until_complete(create_client_session(connector=connector, loop=loop))
         try:
             yield TestClient(self, loop, host, port, session)
         finally:
             loop.run_until_complete(session.close())
             connector.close()
Пример #30
0
 def _run_server(self, *args, **kwargs):
     with run_server(*args, **kwargs) as (loop, site):
         host, port = parse_sockname(site._server.sockets[0].getsockname())
         if host == "unix":
             connector = aiohttp.UnixConnector(path=port, loop=loop)
         else:
             connector = aiohttp.TCPConnector(loop=loop)
         session = aiohttp.ClientSession(connector=connector, loop=loop)
         try:
             yield TestClient(self, loop, host, port, session)
         finally:
             loop.run_until_complete(session.close())
             connector.close()