-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Pydantic and interface usages
- Loading branch information
Showing
14 changed files
with
377 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .models import IImageBuilder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from abc import ABC, abstractmethod | ||
from subprocess import CompletedProcess | ||
|
||
|
||
class IImageBuilder(ABC): | ||
"""An interface for Docker image builder.""" | ||
|
||
@abstractmethod | ||
def _builder_instance_clear(self) -> list[CompletedProcess | str]: | ||
"""Clear the builder instance from the host machine.""" | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def _builder_instance_create(self) -> CompletedProcess | str | None: | ||
"""Create new builder instance.""" | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def _gen_build_cmds(self) -> list[str]: | ||
"""Generate a list of Docker Buildx commands.""" | ||
raise NotImplementedError() | ||
|
||
@abstractmethod | ||
def run(self) -> None: | ||
"""Run the logic.""" | ||
raise NotImplementedError() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .builder import ImageBuilder | ||
from .image_builder import ImageBuilder |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
from pathlib import Path | ||
from pydantic import BaseModel | ||
from subprocess import CompletedProcess | ||
|
||
from mdocker.tools import commands as ccmd, messages as msg | ||
from mdocker.interfaces import IImageBuilder | ||
|
||
|
||
class ImageBuilder(BaseModel, IImageBuilder): | ||
"""A class for building Docker images. | ||
:param name: Docker image name. | ||
:param file: Path to Dockerfile. | ||
:param context: Path to build context. | ||
:param platforms: List of target platforms. | ||
:param push: Flag to push built Docker images to registry. | ||
""" | ||
|
||
_instance: str = "multi_instance" | ||
|
||
name: str | ||
dfile: Path | ||
bcontext: Path | ||
push: bool | ||
platforms: list[str] | ||
|
||
def _builder_instance_clear(self) -> list[CompletedProcess | str]: | ||
# collect completed processes | ||
c_pcs = [] | ||
c_pcs.append(ccmd.launch(f"docker buildx stop {self._instance}", quiet=True, dont_exit=True)) | ||
c_pcs.append(ccmd.launch(f"docker buildx rm {self._instance}", quiet=True, dont_exit=True)) | ||
c_pcs.append( | ||
ccmd.launch( | ||
"docker buildx create --use --name {} --platform {} --driver-opt network=host"\ | ||
.format(self._instance, self.platforms) | ||
) | ||
) | ||
return c_pcs | ||
|
||
def _builder_instance_create(self) -> CompletedProcess | str | None: | ||
return ccmd.launch( | ||
"docker buildx create --use --name {} --platform {} --driver-opt network=host"\ | ||
.format(self._instance, self.platforms) | ||
) | ||
|
||
def _gen_build_cmds(self) -> list[str]: | ||
all_b_cmds = [] | ||
for platform in self.platforms: | ||
# only <arch> value is used in tag extension | ||
tag = f'{self.name}:{platform.split("/")[1]}' | ||
# define build commands | ||
b_cmds = [ | ||
"docker buildx build --no-cache --platform {} --load -f {} {} -t {}"\ | ||
.format(platform, self.dfile, self.bcontext, tag), | ||
"docker buildx stop {}"\ | ||
.format(self._instance), | ||
"docker buildx rm {}"\ | ||
.format(self._instance), | ||
"docker buildx prune --force" | ||
] | ||
# optionally push image to registry | ||
if self.push: | ||
b_cmds.append(f"docker push {tag}") | ||
all_b_cmds.extend(b_cmds) | ||
return all_b_cmds | ||
|
||
def run(self) -> None: | ||
msg.note("Launching multi-platform Docker image build..") | ||
self._builder_instance_clear() | ||
self._builder_instance_create() | ||
[ccmd.launch(cmd) for cmd in self._gen_build_cmds()] | ||
self._builder_instance_clear() | ||
msg.done("Multiarch Docker image build finished!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +0,0 @@ | ||
from .commands import launch | ||
from .messages import note, error, done, cmd | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.