-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supported pushing Docker image to a Docker registry
- Loading branch information
Showing
11 changed files
with
330 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
import click | ||
import os | ||
|
||
|
||
@click.group() | ||
def cli(): | ||
pass | ||
|
||
|
||
def option_with_env_default(envvar: str, *args, **kwargs): | ||
kwargs["help"] = f"{kwargs['help']} [defaults to environment variable '{envvar}']" | ||
return click.option( | ||
*args, **kwargs, | ||
default=lambda: os.environ.get(envvar, ""), | ||
) |
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 |
---|---|---|
@@ -1 +1 @@ | ||
from .create_image import DssDockerImage | ||
from .create_image import DssDockerImage, DockerRegistry |
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,79 @@ | ||
import docker | ||
import json | ||
import logging | ||
import requests | ||
|
||
from docker.client import DockerClient | ||
|
||
from typing import Callable, Dict, Optional | ||
from exasol.ds.sandbox.lib.logging import get_status_logger, LogType | ||
|
||
|
||
_logger = get_status_logger(LogType.DOCKER_IMAGE) | ||
|
||
|
||
def get_from_dict(d: Dict[str, any], *keys: str) -> str: | ||
for key in keys: | ||
if not key in d: | ||
return None | ||
d = d[key] | ||
return d | ||
|
||
|
||
class ProgressReporter: | ||
def __init__(self, verbose: bool): | ||
self.last_status = None | ||
self.verbose = verbose | ||
self.need_linefeed = False | ||
|
||
def _report(self, printer: Callable, msg: Optional[str], **kwargs): | ||
if msg is not None: | ||
printer(msg, **kwargs) | ||
|
||
def _linefeed(self): | ||
if self.need_linefeed: | ||
self.need_linefeed = False | ||
print() | ||
|
||
def report(self, status: Optional[str], progress: Optional[str]): | ||
if not self.verbose: | ||
return | ||
if status == self.last_status: | ||
self._report(print, progress, end="\r") | ||
self.need_linefeed = progress | ||
else: | ||
self.last_status = status | ||
self._linefeed() | ||
self._report(_logger.info, status) | ||
|
||
|
||
class DockerRegistry: | ||
def __init__(self, repository: str, username: str, password: str): | ||
self.repository = repository | ||
self.username = username | ||
self.password = password | ||
self._client = None | ||
|
||
def client(self): | ||
if self._client is None: | ||
self._client = docker.from_env() | ||
return self._client | ||
|
||
def push(self, tag: str): | ||
auth_config = { | ||
"username": self.username, | ||
"password": self.password, | ||
} | ||
resp = self.client().images.push( | ||
repository=self.repository, | ||
tag=tag, | ||
auth_config=auth_config, | ||
stream=True, | ||
decode=True, | ||
) | ||
reporter = ProgressReporter(_logger.isEnabledFor(logging.INFO)) | ||
for el in resp: | ||
reporter.report( | ||
el.get("status", None), | ||
el.get("progress", None), | ||
) |
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 |
---|---|---|
@@ -1,6 +1,41 @@ | ||
import docker | ||
import pytest | ||
|
||
from exasol.ds.sandbox.lib.dss_docker import DssDockerImage | ||
|
||
|
||
def pytest_addoption(parser): | ||
parser.addoption( | ||
"--dss-docker-image", default=None, | ||
help="Name and version of existing Docker image to use for tests", | ||
) | ||
parser.addoption( | ||
"--docker-registry", default=None, metavar="HOST:PORT", | ||
help="Docker registry for pushing Docker images to", | ||
) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def dss_docker_image(request): | ||
""" | ||
If dss_docker_image_name is provided then don't create an image but | ||
reuse the existing image as specified by cli option | ||
--ds-docker-image-name. | ||
""" | ||
existing = request.config.getoption("--dss-docker-image") | ||
if existing and ":" in existing: | ||
name, version = existing.split(":") | ||
yield DssDockerImage(name, version) | ||
return | ||
|
||
testee = DssDockerImage( | ||
"my-repo/dss-test-image", | ||
version=f"{DssDockerImage.timestamp()}", | ||
publish=False, | ||
keep_container=False, | ||
) | ||
testee.create() | ||
try: | ||
yield testee | ||
finally: | ||
docker.from_env().images.remove(testee.image_name) |
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.