Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

WIP Initial support for parametrization of descriptor files #116

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

goldmann
Copy link
Member

@goldmann goldmann commented May 2, 2017

Related to #104.

Consider following descriptor file:

version: 1.0
release: dev
name: jboss/test
from: {{FROM:rhel:7.3}}
packages:
    - make
scripts:
    - package: something
      exec: install

If you execute dogen with this new argument:

dogen -v --param FROM=centos:7 image.yaml target

dogen will generate this Dockerfile:

# Copyright 2017 Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------
#
# This is a Dockerfile for the jboss/test:1.0 image.

FROM centos:7

# Environment variables
ENV JBOSS_IMAGE_NAME="jboss/test" \
    JBOSS_IMAGE_VERSION="1.0" \
    JBOSS_IMAGE_RELEASE="dev" 
# Labels
LABEL name="$JBOSS_IMAGE_NAME" \
      version="$JBOSS_IMAGE_VERSION" \
      release="$JBOSS_IMAGE_RELEASE" \
      architecture="x86_64" \
      com.redhat.component="jboss-test-docker"

USER root


# Install required RPMs and ensure that the packages were installed
RUN yum install -y  make \
    && yum clean all && \
    rpm -q  make

# Add scripts used to configure the image
COPY scripts /tmp/scripts

# Custom scripts
USER 
RUN [ "bash", "-x", "/tmp/scripts/something/install" ]


USER root
RUN rm -rf /tmp/scripts

USER 0

If it is run without the --param switch it uses default values:

# Copyright 2017 Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------
#
# This is a Dockerfile for the jboss/test:1.0 image.

FROM rhel:7.3

# Environment variables
ENV JBOSS_IMAGE_NAME="jboss/test" \
    JBOSS_IMAGE_VERSION="1.0" \
    JBOSS_IMAGE_RELEASE="dev" 
# Labels
LABEL name="$JBOSS_IMAGE_NAME" \
      version="$JBOSS_IMAGE_VERSION" \
      release="$JBOSS_IMAGE_RELEASE" \
      architecture="x86_64" \
      com.redhat.component="jboss-test-docker"

USER root


# Install required RPMs and ensure that the packages were installed
RUN yum install -y  make \
    && yum clean all && \
    rpm -q  make

# Add scripts used to configure the image
COPY scripts /tmp/scripts

# Custom scripts
USER 
RUN [ "bash", "-x", "/tmp/scripts/something/install" ]


USER root
RUN rm -rf /tmp/scripts

USER 0

@rcernich
Copy link

rcernich commented May 2, 2017

This looks pretty good. I think it would also be beneficial to be able to specify these through a properties file. Using a file that's committed with the image.yaml, would provide a place where all the "externals" could be defined, so folks wanting to override some or all know what's been parameterized (e.g. properties section of a pom). I think supporting both command line options and properties file is good, with the options taking precedent over what's in any file. For something like community vs product images, there could be a centos.properties and a rhel.properties. Organizing in this way means the image.yaml contains all the structural elements of the image (scripts, artifacts, etc.), while the properties files identify the specifics (which artifacts, which base image, etc.).

@goldmann
Copy link
Member Author

goldmann commented May 2, 2017

I think I'm ok with adding support for properties file. Stay tuned.

@rwngwn
Copy link
Contributor

rwngwn commented May 3, 2017

This seems weird to me dogen is based on jinja templates - so why second layer of parametrization?
Correct me if I get it wrong, but to be able to introduce new parameter I need to edit the template (it needs to know there is variable)?
If I need small changes there are already include mechanisms in jinja2 and project can maintain there complexities in their templates, putting it to upstream dogen seems very weird.
If we go with this and we will introduce a new parameter to template we should release a new version of dogen? This will probably generate more work than it saves.

k, v = param.split("=", 1)
params[k] = v

args.params = params
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is very hacky - you should not mess with args object and store your values here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I guess the problem is that argparse doesn't support a dict directly. I could leave validation and just pass what I've got via CLI, but I guess I wanted to have the validation as close as possible to where argparse does it. I'm open for suggestions.

self.cfg = yaml.safe_load(stream)
content = stream.read()

for k, v in six.iteritems(self.params):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in python 2.7 we don need six for this. do we support python 2.6 and so?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't support 2.6.


generator = Generator(self.log, self.args)
generator.configure()
generator.render_from_template()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is copy pasted multiple times maybe it should be extracted to common setUp method

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a good idea, yes.

@goldmann
Copy link
Member Author

goldmann commented May 3, 2017

@l-d-j Let's leave it here for a bit. We will see if supporting something like this is a good idea. We need some real use cases so we can make the correct decision. Until then this won't be merged. I'm happy to a discussion about this when we have some more information.

@rwngwn
Copy link
Contributor

rwngwn commented May 3, 2017

@goldmann +1 on tracking it and discussing it here. I still think we should look at jinja inheritance and include statements and try to solve via jinja.

@rcernich
Copy link

rcernich commented May 3, 2017

Here are some "real" use cases that we've received from teams migrating to dogen:

  • Substitute an artifact in the image with one built by a developer, e.g. a new version of kube-ping.
  • Use the same image.yaml for both community and product (e.g. from centos/from rhel; artifacts .Final/.Final-redhat-x)

As stated above, the yaml describes the basic structure and content of the image and the parameters allow for substitution of minor details. From a maintenance perspective, e.g. updating artifact versions, this would entail a change to the properties file, without having to change the yaml itself.

With respect to managing a template for this kind of stuff, I'd start to wonder why I'm using this over just managing a Dockerfile directly. I know docker; now I need to learn dogen and jinja.

The templates raise another issue in that, since we are using dogen to generate the Dockerfile, it means that the version of dogen needs to be specific to the version of the source in order to maintain repeatability of builds.

@goldmann
Copy link
Member Author

goldmann commented May 5, 2017

@rcernich Regarding first use case - you can take a look at this idea: #117 (comment). Additional requirements are:

  1. Skip hash checking to make it easy to do development work
  2. Use target name for the artifact to make it easier to use a "stable" name in the scripts that use this artifact.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants