kolla/docker/macros.j2
Michal Arbet 11f65c6c1d Add mechanism for patching files in containers
This patch adds a way to patch files in a Docker
image built by Kolla. This is very useful for several
reasons, specifically:

- Custom modifications
- The stable branch of some library has a fix but no pip
  package has been released
- Eliminates the need to package your own pip packages
- Eliminates the need to invent your own versioning to prevent
  upstream versioning
- Eliminates the need to manage a pip server
- In other words, it eliminates the need to get a wheel into
  the image and install it manually using any method not
  previously mentioned

It is also highly desirable because, although Kolla can replace
the source for a service with a custom URL for a tarball or its
own Git repo, it cannot do this for dependencies pulled from pip.

I would also like to point out that this is a feature with its own
code path and works only if the user "inserts" a patch into the folder
patches/docker-image/something.patch and creates an analogous series
file for patch source code.

Simply said, this code will never interfere with the upstream build process
since this feature is not intended for use in upstream.
It is rather meant for downstream users who know what they are doing.
Now they just have an option to patch their images.

Everything works on all layers of the Docker image and stores a report
of applied patches which can then be seen in /etc.

This mechanism is similar as debian patch quilt.

Change-Id: I61d0790c5d4d070b7ea9e8c99c0a76ff5d22bf9d
2024-11-15 08:04:30 +00:00

95 lines
3.3 KiB
Django/Jinja

{% macro install_packages(packages, chain=False, clean=clean_package_cache) -%}
{% if packages is defined and packages|length > 0 -%}
{% if not chain -%} RUN {% endif -%}
{%- if base_package_type == 'rpm' -%}
{{ distro_package_manager }} -y install {{ packages | join(' ') }}
{%- if clean %} \
&& {{ distro_package_manager }} clean all && rm -rf /var/cache/{{ distro_package_manager }}{% endif -%}
{%- elif base_package_type == 'deb' -%}
{#-
debian_package_install is a utility method to build up an appropriate
set of commands to install packages in a debian-based environment that
may include URL links to a .deb package
-#}
{{ debian_package_install(packages, clean) }}
{%- endif %}
{%- endif %}
{%- endmacro %}
{# Be Extra vigilant about introducing any extra whitespace at the
end of the macro for chaining purposes -#}
{% macro rpm_security_update(cleanup) -%}
{{ distro_package_manager }} -y distro-sync --security --sec-severity=Important --sec-severity=Critical
{%- if cleanup %} \
&& {{ distro_package_manager }} clean all && rm -rf /var/cache/{{ distro_package_manager }}{% endif -%}
{%- endmacro %}
{% macro sed(file='', expressions=[]) -%}
{% if file != '' and expressions|length >0 %}
RUN {% for expression in expressions %} sed -i "{{ file }}" -e "{{ expression }}" {% if not loop.last %} && {% endif %} {% endfor %}
{% else %}
RUN echo "file and expressions must be set to use the sed macro"; /bin/false
{% endif %}
{%- endmacro %}
{% macro install_pip(packages, constraints=true, python_version='3') %}
{%- if packages is sequence and packages|length > 0 -%}
python{{ python_version }} -m pip --no-cache-dir install --upgrade{{ ' ' }}
{%- if constraints %}-c /requirements/upper-constraints.txt {% endif -%}
{{ packages | join(' ') }}
{%- else -%}
true
{%- endif -%}
{% endmacro %}
{% macro configure_user(name, groups=None, shell=None, homedir=None) %}
{% set user=users[name] %}
{%- if not homedir %}
{% set homedir='/var/lib/' + name %}
{%- endif %}
RUN usermod --append --home {{ homedir }} --groups kolla {{ name }} \
{%- if groups %}
&& usermod --append --groups {{ groups }} {{ name }} \
{%- endif %}
{%- if shell %}
&& chsh --shell {{ shell }} {{ name }} \
{%- endif %}
&& mkdir -p {{ homedir }} \
&& chown -R {{ user.uid }}:{{ user.gid }} {{ homedir }}
{% endmacro %}
{% macro install_fluent_plugins(plugins, chain=False) -%}
{% if plugins is defined and plugins|length > 0 -%}
{% if not chain -%} RUN {% endif -%}
ulimit -n 1024 && fluent-gem install {{ plugins | join(' ') }}
{%- endif %}
{%- endmacro %}
{% macro enable_extra_repos(repos) %}
{{ handle_repos(repos, 'enable') }}
{% endmacro %}
{% macro disable_extra_repos(repos) %}
{{ handle_repos(repos, 'disable') }}
{% endmacro %}
{% macro upper_constraints_remove(package_name) -%}
sed -e "/^{{ package_name }}===/d" -i requirements/upper-constraints.txt
{%- endmacro %}
{# NOTE(hrw): we change to "==" to allow for "21.*" syntax #}
{% macro upper_constraints_version_change(package_name, from, to) -%}
sed -e "s/^{{package_name}}==={{ from }}/{{package_name}}=={{ to }}/g" -i requirements/upper-constraints.txt
{%- endmacro %}
{% macro kolla_patch_sources() -%}
{% if patches_path %}
COPY patches /patches
RUN kolla_patch
{% endif %}
{%- endmacro %}