Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to install linux packages #39

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/pyatsimagebuilder/Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ RUN apt-get -o Acquire::Check-Valid-Until=false -o Acquire::Check-Date=false upd
&& chmod +x /bin/tini \
&& pip3 install --upgrade --no-cache-dir setuptools pip virtualenv \
&& virtualenv ${WORKSPACE} \
&& ${WORKSPACE}/bin/pip install --no-cache-dir psutil \
&& apt-get remove -y curl build-essential \
&& ${WORKSPACE}/bin/pip install --no-cache-dir psutil

{% if image.linux_packages -%}
# Linux packages
RUN apt-get install -y --no-install-recommends {% for item in image.linux_packages -%}{{ item }}{% endfor %}

{% endif %}

RUN apt-get remove -y curl build-essential \
&& apt-get autoremove -y \
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& apt-get clean \
Expand Down
11 changes: 8 additions & 3 deletions src/pyatsimagebuilder/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
IMAGE_BUILD_SUCCESSUL = \
re.compile(r' *Successfully built (?P<image_id>[a-z0-9]{12}) *$')

logger = logging.getLogger(__name__)


class ImageBuilder(object):
def __init__(self, config, logger=logging.getLogger(__name__)):
Expand Down Expand Up @@ -72,19 +74,22 @@ def run(self, keep_context=False, tag=None, no_cache=True, dry_run=False):
self.context = Context(keep=keep_context, logger=self._logger)

with self.context:
logger.debug(f'builder config {self.config}')

# create our installation directory
self.context.mkdir(INSTALLATION)
self.context.mkdir(INSTALLATION / REQUIREMENTS)

self._populate_context()

# Tag for docker image argument (cli) > config (yaml) > None
self.image.tag = tag or self.config.get('tag', None)

# Get Arch for image
self.image.platform = self.config.get('platform', None)

self.image.linux_packages = self.config.get('linux_packages', [])

self._populate_context()

# Start docker build
if not dry_run:
self._logger.info('Building image')
Expand Down Expand Up @@ -409,7 +414,7 @@ def _process_pip_config(self, config):
def _build_image(self, no_cache=False):

# copy entrypoint to the context
self._logger.info('Copying entrypoint to context')
self._logger.debug('Copying entrypoint to context')
self.context.copy(HERE / 'docker-entrypoint.sh',
INSTALLATION / 'entrypoint.sh')

Expand Down
13 changes: 11 additions & 2 deletions src/pyatsimagebuilder/image.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import os
import docker
import logging

from jinja2 import Environment, FileSystemLoader

logger = logging.getLogger(__name__)

JINJA2_ENV = Environment(loader=FileSystemLoader(os.path.dirname(__file__)),
trim_blocks=True,
lstrip_blocks=True)
DEFAULT_BASE_IMAGE = 'python'
DEFAULT_BASE_IMAGE_LABEL = '3.7.9-slim'
DEFAULT_TINI_VERSION = '0.18.0'
DEFAULT_WORKSPACE_NAME = 'pyats'
DEFAULT_LINUX_PACKAGES = []

DOCKERIMAGE_TEMPLATE = 'Dockerfile.template'

Expand All @@ -23,7 +27,8 @@ def __init__(self,
base_image=DEFAULT_BASE_IMAGE,
base_image_label=DEFAULT_BASE_IMAGE_LABEL,
tini_version=DEFAULT_TINI_VERSION,
workspace_name=DEFAULT_WORKSPACE_NAME):
workspace_name=DEFAULT_WORKSPACE_NAME,
linux_packages=DEFAULT_LINUX_PACKAGES):

self._template = JINJA2_ENV.get_template(DOCKERIMAGE_TEMPLATE)

Expand All @@ -40,13 +45,17 @@ def __init__(self,
# environment variables
self.env = env or {}

self.linux_packages = linux_packages

# commands to run before/after pip installation
self.pre_pip_cmds = pre_pip_cmds
self.post_pip_cmds = post_pip_cmds


def manifest(self):
return self._template.render(image=self)
output = self._template.render(image=self)
logger.debug(f'Dockerfile:\n{output}')
return output

def push(self, remote_tag=None, credentials=None):
"""
Expand Down
6 changes: 6 additions & 0 deletions src/pyatsimagebuilder/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
'type': 'string'
}
},
'linux_packages': {
'type': 'array',
'items': {
'type': 'string'
}
},
# pip-config takes either a dict to give to configparser, or an already
# formatted config string
'pip-config': {
Expand Down