Skip to content

Commit

Permalink
fix: check-docker.py script not working (#2174)
Browse files Browse the repository at this point in the history
Backported-from: main (24.09)
Backported-to: 23.03
Backport-of: 2174
  • Loading branch information
agatha197 authored and rapsealk committed Jul 30, 2024
1 parent 4c9dc2b commit cdfd29f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
56 changes: 29 additions & 27 deletions scripts/check-docker.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import asyncio
import base64
import functools
import hashlib
Expand All @@ -7,10 +8,8 @@
import subprocess
import sys
from pathlib import Path
from urllib.parse import quote

import requests
import requests_unixsocket
import aiohttp

log = functools.partial(print, file=sys.stderr)
run = subprocess.run
Expand Down Expand Up @@ -42,19 +41,22 @@ def simple_hash(data: bytes) -> str:
return base64.b64encode(h.digest()[:12], altchars=b"._").decode()


def detect_snap_docker():
async def detect_snap_docker():
if not Path("/run/snapd.socket").is_socket():
return None
with requests.get("http+unix://%2Frun%2Fsnapd.socket/v2/snaps?names=docker") as r:
if r.status_code != 200:
raise RuntimeError("Failed to query Snapd package information")
response_data = r.json()
for pkg_data in response_data["result"]:
if pkg_data["name"] == "docker":
return pkg_data["version"]


def detect_system_docker():
async with aiohttp.ClientSession(connector=aiohttp.UnixConnector("/run/snapd.socket")) as sess:
async with sess.get("http://localhost/v2/snaps?names=docker") as r:
try:
r.raise_for_status()
except:
raise RuntimeError("Failed to query Snapd package information")
response_data = await r.json()
for pkg_data in response_data["result"]:
if pkg_data["name"] == "docker":
return pkg_data["version"]


async def detect_system_docker():
sock_paths = [
Path("/run/docker.sock"), # Linux default
Path("/var/run/docker.sock"), # macOS default
Expand All @@ -68,12 +70,14 @@ def detect_system_docker():
break
else:
return None
encoded_sock_path = quote(bytes(sock_path), safe="")
with requests.get(f"http+unix://{encoded_sock_path}/version") as r:
if r.status_code != 200:
raise RuntimeError("Failed to query the Docker daemon API")
response_data = r.json()
return response_data["Version"]
async with aiohttp.ClientSession(connector=aiohttp.UnixConnector(sock_path.as_posix())) as sess:
async with sess.get("http://localhost/version") as r:
try:
r.raise_for_status()
except:
raise RuntimeError("Failed to query Snapd package information")
response_data = await r.json()
return response_data["Version"]


def fail_with_snap_docker_refresh_request():
Expand All @@ -94,15 +98,13 @@ def fail_with_compose_install_request():
sys.exit(1)


def main():
requests_unixsocket.monkeypatch()

async def main():
parser = argparse.ArgumentParser()
parser.add_argument("--get-preferred-pants-local-exec-root", action="store_true", default=False)
args = parser.parse_args()

if args.get_preferred_pants_local_exec_root:
docker_version = detect_snap_docker()
docker_version = await detect_snap_docker()
build_root_path = get_build_root()
build_root_name = build_root_path.name
build_root_hash = simple_hash(os.fsencode(build_root_path))
Expand All @@ -114,13 +116,13 @@ def main():
print(f"/tmp/{build_root_name}-{build_root_hash}-pants")
return

docker_version = detect_snap_docker()
docker_version = await detect_snap_docker()
if docker_version is not None:
log(f"Detected Docker installation: Snap package ({docker_version})")
if parse_version(docker_version) < (20, 10, 15):
fail_with_snap_docker_refresh_request()
else:
docker_version = detect_system_docker()
docker_version = await detect_system_docker()
if docker_version is not None:
log(f"Detected Docker installation: System package ({docker_version})")
else:
Expand All @@ -145,4 +147,4 @@ def main():


if __name__ == "__main__":
main()
asyncio.run(main())
2 changes: 1 addition & 1 deletion scripts/install-dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ show_info "Checking prerequisites and script dependencies..."
install_script_deps
$bpython -m ensurepip --upgrade
# FIXME: Remove urllib3<2.0 requirement after docker/docker-py#3113 is resolved
$bpython -m pip --disable-pip-version-check install -q -U 'urllib3<2.0' requests requests-unixsocket
$bpython -m pip --disable-pip-version-check install -q -U 'urllib3<2.0' aiohttp
if [ $CODESPACES != "true" ] || [ $CODESPACES_ON_CREATE -eq 1 ]; then
$docker_sudo $bpython scripts/check-docker.py
if [ $? -ne 0 ]; then
Expand Down

0 comments on commit cdfd29f

Please sign in to comment.