Skip to content

Commit

Permalink
Simplified docker calls to subprocess.run
Browse files Browse the repository at this point in the history
 - revert default logging level to ERROR
  • Loading branch information
spillai committed Oct 25, 2023
1 parent 2a2e2fd commit c98445d
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 58 deletions.
44 changes: 7 additions & 37 deletions agipack/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@

from jinja2 import Environment, FileSystemLoader
from pydantic.dataclasses import dataclass
from rich import print

from agipack.config import AGIPackConfig, ImageConfig
from agipack.constants import AGIPACK_DOCKERFILE_TEMPLATE, AGIPACK_ENV, AGIPACK_TEMPLATE_DIR
from agipack.version import __version__

logging_level = os.environ.get("AGIPACK_LOGGING_LEVEL", "DEBUG")
logging_level = os.environ.get("AGIPACK_LOGGING_LEVEL", "ERROR")
logging.basicConfig(level=logging.getLevelName(logging_level))
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -177,25 +176,15 @@ def build(self, filename: str, target: str, tags: List[str] = None, push: bool =
logger.debug(f"Image tags: {image_tags}")

# Build the Docker image (using buildkit)
cmd = ["docker", "build", "-f", filename, "--target", target]
cmd = f"docker build -f {filename} --target {target}"
for tag in image_tags:
cmd.extend(["-t", tag])
cmd.append(".")
cmd += f" -t {tag}"
cmd += " ."

logger.debug(f"Running command: {cmd}")
env = os.environ.copy()
env.update({"DOCKER_BUILDKIT": "1"})
process = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
env=env,
)
for line in iter(process.stdout.readline, ""):
print(line, end="")
process.wait()
process = subprocess.run(cmd, env=env, shell=True)

if process.returncode != 0:
err_msg = f"Failed to build image [target={target}, e={process.stderr}]"
Expand All @@ -215,16 +204,7 @@ def lint(self, filename: str) -> bool:
cmd = "docker pull hadolint/hadolint && "
cmd += f"docker run --pull=always --rm -i hadolint/hadolint < {filename}"
logger.info("Linting with hadolint")
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
shell=True,
)
process.wait()
for line in iter(process.stdout.readline, ""):
print(line, end="")
process = subprocess.run(cmd, shell=True)
return process.returncode == 0

def push(self, tags: List[str]) -> None:
Expand All @@ -239,16 +219,6 @@ def push(self, tags: List[str]) -> None:
for tag in tags:
cmd = f"docker push {tag}"
logger.debug(f"Running command: {cmd}")
process = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
shell=True,
)
for line in iter(process.stdout.readline, ""):
print(line, end="")
process.wait()
process = subprocess.run(cmd, shell=True)
if process.returncode != 0:
raise Exception(f"Failed to push image [tag={tag}]")
2 changes: 1 addition & 1 deletion examples/generated/Dockerfile-base-cpu
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# >>>>>>>>>>>>>>>>>>>>>>>>>>>
# Auto-generated by agi-pack (version=0.1.18).
# Auto-generated by agi-pack (version=0.1.19).
FROM debian:buster-slim AS base-cpu

# Setup environment variables
Expand Down
2 changes: 1 addition & 1 deletion examples/generated/Dockerfile-base-cu118
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# >>>>>>>>>>>>>>>>>>>>>>>>>>>
# Auto-generated by agi-pack (version=0.1.18).
# Auto-generated by agi-pack (version=0.1.19).
FROM nvidia/cuda:11.8.0-base-ubuntu22.04 AS base-gpu

# Setup environment variables
Expand Down
2 changes: 1 addition & 1 deletion examples/generated/Dockerfile-builder
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# >>>>>>>>>>>>>>>>>>>>>>>>>>>
# Auto-generated by agi-pack (version=0.1.18).
# Auto-generated by agi-pack (version=0.1.19).
FROM debian:buster-slim AS agipack-builder

# Setup environment variables
Expand Down
4 changes: 2 additions & 2 deletions examples/generated/Dockerfile-multistage
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# >>>>>>>>>>>>>>>>>>>>>>>>>>>
# Auto-generated by agi-pack (version=0.1.18).
# Auto-generated by agi-pack (version=0.1.19).
FROM debian:buster-slim AS base-cpu

# Setup environment variables
Expand Down Expand Up @@ -80,7 +80,7 @@ RUN apt-get -y autoclean \
&& rm -rf /tmp/reqs \
&& echo "pip cleanup complete"
# >>>>>>>>>>>>>>>>>>>>>>>>>>>
# Auto-generated by agi-pack (version=0.1.18).
# Auto-generated by agi-pack (version=0.1.19).
FROM base-cpu AS dev-cpu

# Install additional system packages
Expand Down
18 changes: 2 additions & 16 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import os
import subprocess
import tempfile
from pathlib import Path

Expand Down Expand Up @@ -32,21 +31,8 @@ def test_build_all(builder):
dockerfiles = builder.render()
assert "base-cpu" in dockerfiles
assert dockerfiles["base-cpu"] == "Dockerfile"
assert Path(dockerfiles["base-cpu"]).exists()

# Use hadolint within docker to lint/check the generated Dockerfile
cmd = "docker pull hadolint/hadolint && "
cmd += f"docker run --pull=always --rm -i hadolint/hadolint < {dockerfiles['base-cpu']}"
logger.info("Linting with hadolint")
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
shell=True,
)
for line in iter(process.stdout.readline, ""):
logger.info(line.rstrip())
path = Path(dockerfiles["base-cpu"])
assert path.exists()


def test_builder_cls(test_data_dir):
Expand Down

0 comments on commit c98445d

Please sign in to comment.