Drop Python 3.4 and switch to async / await syntax for asyncio. Fixes #1425

This commit is contained in:
grossmj committed 2018-10-15 17:05:49 +07:00
1 parent 8baa480b79
commit 76af98404a
119 files changed
+2432 -3084

No files matched your search

+19 -26
View File
@@ -54,14 +54,13 @@ class Docker(BaseManager):
self._session = None
self._api_version = DOCKER_MINIMUM_API_VERSION
@asyncio.coroutine
def _check_connection(self):
async def _check_connection(self):
if not self._connected:
try:
self._connected = True
connector = self.connector()
version = yield from self.query("GET", "version")
version = await self.query("GET", "version")
except (aiohttp.ClientOSError, FileNotFoundError):
self._connected = False
raise DockerError("Can't connect to docker daemon")
@@ -88,16 +87,14 @@ class Docker(BaseManager):
raise DockerError("Can't connect to docker daemon")
return self._connector
@asyncio.coroutine
def unload(self):
async def unload(self):
yield from super().unload()
await super().unload()
if self._connected:
if self._connector and not self._connector.closed:
self._connector.close()
@asyncio.coroutine
def query(self, method, path, data={}, params={}):
async def query(self, method, path, data={}, params={}):
"""
Makes a query to the Docker daemon and decode the request
@@ -107,8 +104,8 @@ class Docker(BaseManager):
:param params: Parameters added as a query arg
"""
response = yield from self.http_query(method, path, data=data, params=params)
body = yield from response.read()
response = await self.http_query(method, path, data=data, params=params)
body = await response.read()
if body and len(body):
if response.headers['CONTENT-TYPE'] == 'application/json':
body = json.loads(body.decode("utf-8"))
@@ -117,8 +114,7 @@ class Docker(BaseManager):
log.debug("Query Docker %s %s params=%s data=%s Response: %s", method, path, params, data, body)
return body
@asyncio.coroutine
def http_query(self, method, path, data={}, params={}, timeout=300):
async def http_query(self, method, path, data={}, params={}, timeout=300):
"""
Makes a query to the docker daemon
@@ -140,11 +136,11 @@ class Docker(BaseManager):
url = "http://docker/v" + DOCKER_MINIMUM_API_VERSION + "/" + path
try:
if path != "version": # version is use by check connection
yield from self._check_connection()
await self._check_connection()
if self._session is None or self._session.closed:
connector = self.connector()
self._session = aiohttp.ClientSession(connector=connector)
response = yield from self._session.request(
response = await self._session.request(
method,
url,
params=params,
@@ -157,7 +153,7 @@ class Docker(BaseManager):
except (asyncio.TimeoutError):
raise DockerError("Docker timeout " + method + " " + path)
if response.status >= 300:
body = yield from response.read()
body = await response.read()
try:
body = json.loads(body.decode("utf-8"))["message"]
except ValueError:
@@ -171,8 +167,7 @@ class Docker(BaseManager):
raise DockerError("Docker has returned an error: {} {}".format(response.status, body))
return response
@asyncio.coroutine
def websocket_query(self, path, params={}):
async def websocket_query(self, path, params={}):
"""
Opens a websocket connection
@@ -182,14 +177,13 @@ class Docker(BaseManager):
"""
url = "http://docker/v" + self._api_version + "/" + path
connection = yield from self._session.ws_connect(url,
connection = await self._session.ws_connect(url,
origin="http://docker",
autoping=True)
return connection
@locking
@asyncio.coroutine
def pull_image(self, image, progress_callback=None):
async def pull_image(self, image, progress_callback=None):
"""
Pulls an image from the Docker repository
@@ -198,19 +192,19 @@ class Docker(BaseManager):
"""
try:
yield from self.query("GET", "images/{}/json".format(image))
await self.query("GET", "images/{}/json".format(image))
return # We already have the image skip the download
except DockerHttp404Error:
pass
if progress_callback:
progress_callback("Pulling '{}' from docker hub".format(image))
response = yield from self.http_query("POST", "images/create", params={"fromImage": image}, timeout=None)
response = await self.http_query("POST", "images/create", params={"fromImage": image}, timeout=None)
# The pull api will stream status via an HTTP JSON stream
content = ""
while True:
try:
chunk = yield from response.content.read(1024)
chunk = await response.content.read(1024)
except aiohttp.ServerDisconnectedError:
log.error("Disconnected from server while pulling Docker image '{}' from docker hub".format(image))
break
@@ -234,8 +228,7 @@ class Docker(BaseManager):
if progress_callback:
progress_callback("Success pulling image {}".format(image))
@asyncio.coroutine
def list_images(self):
async def list_images(self):
"""
Gets Docker image list.
@@ -244,7 +237,7 @@ class Docker(BaseManager):
"""
images = []
for image in (yield from self.query("GET", "images/json", params={"all": 0})):
for image in (await self.query("GET", "images/json", params={"all": 0})):
if image['RepoTags']:
for tag in image['RepoTags']:
if tag != "<none>:<none>":