Skip to content

Commit

Permalink
First implementation of Docker creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ckunki committed Nov 3, 2023
1 parent d040a56 commit 175a3b9
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 5 deletions.
3 changes: 3 additions & 0 deletions exasol/ds/sandbox/lib/dss_docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM ubuntu:20.04
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get install --no-install-recommends -y python3 python3-pexpect
87 changes: 87 additions & 0 deletions exasol/ds/sandbox/lib/dss_docker/create_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import docker
import logging
import tempfile
from docker.types import Mount
from importlib_metadata import version
from pathlib import Path

from exasol.ds.sandbox.lib.config import ConfigObject, SLC_VERSION
# renaming proposal:
# import exasol.ds.sandbox.lib.ansible
# file ansible.__init__.py with content "import ...ansible.repository"
# enables usage: ansible.repository.default
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


CONTAINER_NAME = "ds-sandbox-docker"
CONTAINER_IMAGE_TAG = "ds-sandbox:latest"
ANSIBLE_CONFIG = ConfigObject(
time_to_wait_for_polling=0.1,
slc_version=SLC_VERSION,
)


_logger = logging.getLogger(__name__)
_logger.setLevel(logging.INFO)
logging.basicConfig(format="%(asctime)s %(levelname)s %(message)s")


def create_image():
with tempfile.TemporaryDirectory() as tmp_path:
docker_env = docker.from_env()
path = Path(__file__).parent
_logger.info(
f"Creating docker image {CONTAINER_IMAGE_TAG}"
f" from {path / 'Dockerfile'}"
f" in tmp path {tmp_path}"
)
docker_env.images.build(path=str(path), tag=CONTAINER_IMAGE_TAG)
socket_mount = Mount("/var/run/docker.sock", "/var/run/docker.sock", type="bind")
tmp_mount = Mount(tmp_path, tmp_path, type="bind")
container = docker_env.containers.create(
image=CONTAINER_IMAGE_TAG,
name=CONTAINER_NAME,
mounts=[socket_mount, tmp_mount],
command="sleep infinity",
detach=True,
)
_logger.info("Starting container")
container.start()
extra_vars = {
"docker_container": container.name,
# Is it wise / required to use a temp folder here
# and mount it to docker container?
"slc_dest_folder": f"{tmp_path}/script-languages-release",
}
ansible_run_context = AnsibleRunContext(
playbook="slc_docker_playbook.yml", extra_vars=extra_vars)
try:
run_install_dependencies(
AnsibleAccess(),
configuration=ANSIBLE_CONFIG,
host_infos=tuple(),
ansible_run_context=ansible_run_context,
ansible_repositories=ansible_repository.default_repositories,
)
# Note: script-languages-release will be cloned by ansible within the docker container.
# Because the docker container runs as root, the repository will be owned by root.
# For simplicity, we delete the folder from within the Docker container (as root).
# Otherwise, we get a permission problem when tmp_path tries to clean-up itself.
except Exception as ex:
raise ex
finally:
container.commit(
repository=f"exasol/{CONTAINER_IMAGE_TAG}",
tag="latest",
)
_logger.info("Cleaning up")
container.exec_run(f"rm -rf {tmp_path}/script-languages-release")
container.stop()
container.remove()


if __name__ == "__main__":
create_image()
3 changes: 3 additions & 0 deletions exasol/ds/sandbox/runtime/ansible/ec2_setup_tasks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- name: Update netplan
include_role:
name: netplan
26 changes: 26 additions & 0 deletions exasol/ds/sandbox/runtime/ansible/slc_docker_playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- 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}}"
ansible_connection: docker
- name: add pexepect
add_host:
name: "{{docker_container}}"
ansible_connection: docker

- name: Setup Docker Container
hosts: ds-sandbox-docker # cannot use variable here
gather_facts: false
vars:
ansible_python_interpreter: python3
user_name: root
user_home: /root
need_sudo: false
docker_integration_test: true
tasks:
- import_tasks: slc_setup_tasks.yml
1 change: 1 addition & 0 deletions exasol/ds/sandbox/runtime/ansible/slc_setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
remote_user: ubuntu
tasks:
- import_tasks: slc_setup_tasks.yml
- import_tasks: ec2_setup_tasks.yml
8 changes: 4 additions & 4 deletions exasol/ds/sandbox/runtime/ansible/slc_setup_tasks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- name: Install Script_languages
include_role:
name: script_languages
- name: Update netplan
include_role:
name: netplan

- name: Clear pip cache
ansible.builtin.file:
path: /root/.cache/pip
state: absent
3 changes: 2 additions & 1 deletion test/ansible/slc_setup_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
name: "{{test_docker_container}}"
ansible_connection: docker

- name: Run test
- name: Setup Docker Container
hosts: ansible-test
gather_facts: false
vars:
Expand All @@ -26,3 +26,4 @@
state: present
update_cache: yes
- import_tasks: slc_setup_tasks.yml
- import_tasks: ec2_setup_tasks.yml
1 change: 1 addition & 0 deletions test/unit/ansible_conflict/slc_setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@
state: present
update_cache: yes
- import_tasks: slc_setup_tasks.yml
- import_tasks: ec2_setup_tasks.yml

0 comments on commit 175a3b9

Please sign in to comment.