-
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.
Browse files
Browse the repository at this point in the history
* First implementation of Docker creation * Added cli. * Remove containers and image after running integration test. * Changed order of tests to run fast unit tests before * Apply suggestions from code review * Removed (scope="session") from @pytest.fixture for test Docker containers --------- Co-authored-by: Christoph Pirkl <[email protected]>
- Loading branch information
1 parent
58d5c86
commit f83662f
Showing
27 changed files
with
397 additions
and
156 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
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,44 @@ | ||
import click | ||
|
||
from exasol.ds.sandbox.cli.cli import cli | ||
from exasol.ds.sandbox.cli.options.logging import logging_options | ||
from exasol.ds.sandbox.cli.common import add_options | ||
from exasol.ds.sandbox.lib.dss_docker import DssDockerImage | ||
from exasol.ds.sandbox.lib.logging import SUPPORTED_LOG_LEVELS | ||
from exasol.ds.sandbox.lib.logging import set_log_level | ||
|
||
|
||
@cli.command() | ||
@add_options([ | ||
click.option( | ||
'--repository', type=str, metavar="ORG/REPO", show_default=True, | ||
default="exasol/data-science-sandbox", | ||
help="Organization and repository on hub.docker.com to publish the docker image to"), | ||
click.option('--version', type=str, help="Docker image version tag"), | ||
click.option( | ||
'--publish', type=bool, is_flag=True, | ||
help="Whether to publish the created Docker image"), | ||
click.option( | ||
'--keep-container', type=bool, is_flag=True, | ||
help="""Keep the Docker Container running after creating the image. | ||
Otherwise stop and remove the container."""), | ||
]) | ||
@add_options(logging_options) | ||
def create_docker_image( | ||
repository: str, | ||
version: str, | ||
publish: bool, | ||
keep_container: bool, | ||
log_level: str, | ||
): | ||
""" | ||
Create a Docker image for data-science-sandbox and deploy | ||
it to a Docker repository. | ||
""" | ||
set_log_level(log_level) | ||
DssDockerImage( | ||
repository=repository, | ||
version=version, | ||
publish=publish, | ||
keep_container=keep_container, | ||
).create() |
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,4 @@ | ||
FROM ubuntu:20.04 | ||
ENV DEBIAN_FRONTEND noninteractive | ||
RUN apt-get update && apt-get install --no-install-recommends --assume-yes python3 python3-pexpect | ||
EXPOSE 8888/tcp |
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 .create_image import DssDockerImage |
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,103 @@ | ||
import docker | ||
import humanfriendly | ||
import importlib_resources | ||
|
||
from datetime import datetime | ||
from docker.types import Mount | ||
from exasol.ds.sandbox.lib import pretty_print | ||
from importlib_metadata import version | ||
from pathlib import Path | ||
|
||
from exasol.ds.sandbox.lib.config import ConfigObject, SLC_VERSION | ||
from exasol.ds.sandbox.lib.logging import get_status_logger, LogType | ||
from exasol.ds.sandbox.lib.ansible import ansible_repository | ||
from exasol.ds.sandbox.lib.ansible.ansible_run_context import AnsibleRunContext | ||
from exasol.ds.sandbox.lib.ansible.ansible_access import AnsibleAccess | ||
from exasol.ds.sandbox.lib.setup_ec2.run_install_dependencies import run_install_dependencies | ||
|
||
|
||
DSS_VERSION = version("exasol-data-science-sandbox") | ||
_logger = get_status_logger(LogType.DOCKER_IMAGE) | ||
|
||
|
||
class DssDockerImage: | ||
@classmethod | ||
def timestamp(cls) -> str: | ||
return f'{datetime.now().timestamp():.0f}' | ||
|
||
def __init__( | ||
self, | ||
repository: str, | ||
version: str = None, | ||
publish: bool = False, | ||
keep_container: bool = False, | ||
): | ||
version = version if version else DSS_VERSION | ||
self.container_name = f"ds-sandbox-{DssDockerImage.timestamp()}" | ||
self.image_name = f"{repository}:{version}" | ||
self.publish = publish | ||
self.keep_container = keep_container | ||
|
||
def _ansible_run_context(self) -> AnsibleRunContext: | ||
extra_vars = { | ||
"docker_container": self.container_name, | ||
} | ||
return AnsibleRunContext( | ||
playbook="dss_docker_playbook.yml", | ||
extra_vars=extra_vars, | ||
) | ||
|
||
def _ansible_config(self) -> ConfigObject: | ||
return ConfigObject( | ||
time_to_wait_for_polling=0.1, | ||
slc_version=SLC_VERSION, | ||
) | ||
|
||
def _docker_file(self) -> importlib_resources.abc.Traversable: | ||
return ( | ||
importlib_resources | ||
.files("exasol.ds.sandbox.lib.dss_docker") | ||
.joinpath("Dockerfile") | ||
) | ||
|
||
def create(self): | ||
docker_file = self._docker_file() | ||
try: | ||
start = datetime.now() | ||
docker_client = docker.from_env() | ||
_logger.info(f"Creating docker image {self.image_name} from {docker_file}") | ||
with docker_file.open("rb") as fileobj: | ||
docker_client.images.build(fileobj=fileobj, tag=self.image_name) | ||
container = docker_client.containers.create( | ||
image=self.image_name, | ||
name=self.container_name, | ||
command="sleep infinity", | ||
detach=True, | ||
) | ||
_logger.info("Starting container") | ||
container.start() | ||
_logger.info("Installing dependencies") | ||
run_install_dependencies( | ||
AnsibleAccess(), | ||
configuration=self._ansible_config(), | ||
host_infos=tuple(), | ||
ansible_run_context=self._ansible_run_context(), | ||
ansible_repositories=ansible_repository.default_repositories, | ||
) | ||
_logger.info("Committing changes to docker container") | ||
image = container.commit( | ||
repository=self.image_name, | ||
) | ||
except Exception as ex: | ||
raise ex | ||
finally: | ||
if self.keep_container: | ||
_logger.info("Keeping container running") | ||
else: | ||
_logger.info("Stopping container") | ||
container.stop() | ||
_logger.info("Removing container") | ||
container.remove() | ||
size = humanfriendly.format_size(image.attrs["Size"]) | ||
elapsed = pretty_print.elapsed(start) | ||
_logger.info(f"Built Docker image {self.image_name} size {size} in {elapsed}.") |
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,8 @@ | ||
from datetime import datetime, timedelta | ||
|
||
|
||
def elapsed(start: datetime, round_to_seconds=True) -> str: | ||
d = datetime.now() - start | ||
if round_to_seconds: | ||
d = d - timedelta(microseconds=d.microseconds) | ||
return str(d) |
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,23 @@ | ||
- name: Prepare environment | ||
hosts: localhost | ||
gather_facts: false | ||
vars: | ||
ansible_python_interpreter: python3 | ||
tasks: | ||
- name: Add docker container to inventory | ||
add_host: | ||
name: "{{docker_container}}" | ||
groups: docker_container_group | ||
ansible_connection: docker | ||
|
||
- name: Setup DSS Docker Container | ||
hosts: docker_container_group | ||
gather_facts: false | ||
vars: | ||
ansible_python_interpreter: python3 | ||
user_name: root | ||
user_home: /root | ||
need_sudo: false | ||
docker_integration_test: true | ||
tasks: | ||
- import_tasks: general_setup_tasks.yml |
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,6 @@ | ||
- name: Install Script_languages | ||
include_role: | ||
name: script_languages | ||
- name: Update netplan | ||
include_role: | ||
name: netplan |
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
2 changes: 1 addition & 1 deletion
2
exasol/ds/sandbox/runtime/ansible/roles/jupyter/files/requirements_dependencies.txt
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,4 +1,4 @@ | ||
uncertainties==3.1.7 | ||
numpy==1.23.1 | ||
pandas==1.4.3 | ||
exasol-notebook-connector==0.1.0 | ||
exasol-notebook-connector==0.2.0 |
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
2 changes: 1 addition & 1 deletion
2
exasol/ds/sandbox/runtime/ansible/roles/script_languages/tasks/main.yml
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.