Skip to content

Commit cfa266a

Browse files
committed
Allow to use configure_user macro for external templates
Commit 79a2342 introduced an ability to build 'in-house'/'not-built-in' projects with Kolla. This commits extends this feature with an ability to use configure_user macro for 'not-built-in' users. The implementation is done the same way as for 'sources', i.e. engine after registers new section structure as soon as it finds unknown '<project>-user' section. In addition, the documentation and CI are extended to cover ``--docker-dir`` option. Change-Id: I690d9f3829083f2493bf286a1c45764b9699219b
1 parent 2941ea2 commit cfa266a

File tree

8 files changed

+175
-1
lines changed

8 files changed

+175
-1
lines changed

doc/source/admin/image-building.rst

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,98 @@ The template becomes now:
525525
RUN cp /additions/jenkins/jenkins.json /jenkins.json
526526
{% endblock %}
527527
528+
Custom docker templates
529+
-----------------------
530+
531+
In order to unify the process of managing OpenStack-related projects, Kolla
532+
provides a way of building images for external 'non-built-in' projects.
533+
534+
If the template for a 'non-built-in' project meets Kolla template standards,
535+
an operator can provide a root directory with a template via the
536+
``--docker-dir`` CLI option (can be specified multiple times).
537+
538+
All Kolla's jinja2 macros should be available the same as for built-in
539+
projects with some notes:
540+
541+
- The ``configure_user`` macro. As the 'non-built-in' user is unknown to Kolla,
542+
there are no default values for user ID and group ID to use.
543+
To use this macro, an operator should specify "non-default" user details
544+
with ``<custom_user_name>-user`` configuration section and include info
545+
for ``uid`` and ``gid`` at least.
546+
547+
Let's look into how an operator can build an image for an in-house project
548+
with Kolla using `openstack/releases <https://opendev.org/openstack/releases>`_
549+
project.
550+
551+
First, create a ``Dockerfile.j2`` template for the project.
552+
553+
.. path /home/kolla/custom-kolla-docker-templates/releaser/Dockerfile.j2
554+
.. code-block:: jinja
555+
556+
FROM {{ namespace }}/{{ image_prefix }}openstack-base:{{ tag }}
557+
558+
{% block labels %}
559+
LABEL maintainer="{{ maintainer }}" name="{{ image_name }}" build-date="{{ build_date }}"
560+
{% endblock %}
561+
562+
{% block releaser_header %}{% endblock %}
563+
564+
{% import "macros.j2" as macros with context %}
565+
566+
{{ macros.configure_user(name='releaser') }}
567+
568+
RUN ln -s releaser-source/* /releaser \
569+
&& {{ macros.install_pip(['/releaser-source'] | customizable("pip_packages")) }} \
570+
&& mkdir -p /etc/releaser \
571+
&& chown -R releaser: /etc/releaser \
572+
&& chmod 750 /etc/sudoers.d \
573+
&& touch /usr/local/bin/kolla_releaser_extend_start \
574+
&& chmod 644 /usr/local/bin/kolla_extend_start /usr/local/bin/kolla_releaser_extend_start
575+
576+
{% block footer %}{% endblock %}
577+
578+
Suggested directory structure:
579+
580+
.. code-block:: console
581+
582+
custom-kolla-docker-templates
583+
|__ releaser
584+
|__ Dockerfile.j2
585+
586+
Then, modify Kolla's configuration so the engine can download sources and
587+
configure users.
588+
589+
.. path /etc/kolla/kolla-build.conf
590+
.. code-block:: ini
591+
592+
[releaser]
593+
type = git
594+
location = https://opendev.org/openstack/releases
595+
reference = master
596+
597+
[releaser-user]
598+
uid = 53001
599+
gid = 53001
600+
601+
Last pre-check before building a new image - ensure that the new template
602+
is visible for Kolla:
603+
604+
.. code-block:: console
605+
606+
$ kolla-build --list-images --docker-dir custom-kolla-docker-templates "^releaser$"
607+
1 : base
608+
2 : releaser
609+
3 : openstack-base
610+
611+
And finally, build the ``releaser`` image, passing the ``--docker-dir``
612+
argument:
613+
614+
.. code-block:: console
615+
616+
kolla-build --docker-dir custom-kolla-docker-templates "^releaser$"
617+
618+
Can I use the ``--template-override`` option for custom templates? Yes!
619+
528620
Custom repos
529621
------------
530622

docker/macros.j2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
{% endmacro %}
4444

4545
{% macro configure_user(name, groups=None, shell=None, homedir=None) %}
46+
{%- if name not in users %}
47+
{{ raise_error("Failed to find configuration for '" + name + "' user. Try specifying '" + name + "-user' config section.") }}
48+
{%- endif %}
4649
{% set user=users[name] %}
4750
{%- if not homedir %}
4851
{% set homedir='/var/lib/' + name %}

kolla/image/kolla_worker.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ def _get_methods(self):
271271
return {
272272
'debian_package_install': jinja_methods.debian_package_install,
273273
'handle_repos': jinja_methods.handle_repos,
274+
'raise_error': jinja_methods.raise_error,
274275
}
275276

276277
def get_users(self):
@@ -280,7 +281,17 @@ def get_users(self):
280281
for section in all_sections:
281282
match = re.search('^.*-user$', section)
282283
if match:
283-
user = self.conf[match.group(0)]
284+
cfg_group_name = match.group(0)
285+
286+
if cfg_group_name not in self.conf._groups:
287+
self.conf.register_opts(
288+
common_config.get_user_opts(
289+
None, None,
290+
# cut `-user` suffix
291+
group=cfg_group_name[:-5]),
292+
group=cfg_group_name
293+
)
294+
user = self.conf[cfg_group_name]
284295
ret[match.group(0)[:-5]] = {
285296
'uid': user.uid,
286297
'gid': user.gid,

kolla/template/methods.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
# limitations under the License.
1212

1313
import os
14+
import typing as t
15+
1416
import yaml
1517

1618
from jinja2 import pass_context
@@ -150,3 +152,7 @@ def handle_repos(context, reponames, mode):
150152
commands = "RUN %s" % commands
151153

152154
return commands
155+
156+
157+
def raise_error(msg: str) -> t.NoReturn:
158+
raise Exception(msg)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
features:
3+
- |
4+
Extends the support of externally-managed projects provided by the
5+
``--docker-dir`` option with an ability to use ``configure_user``
6+
jinja2 macros like Kolla built-in projects.
7+
The operator should specify "non-default" user details with
8+
``<custom_user_name>-user`` configuration section and include info for
9+
``uid`` and ``gid`` at least.

tests/playbooks/run.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@
4444
when:
4545
- publisher
4646

47+
- name: Add external docker dir config
48+
vars:
49+
kolla_build_external_docker_config:
50+
DEFAULT:
51+
docker_dir: "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/tests/templates/docker"
52+
releaser:
53+
type: git
54+
reference: master
55+
location: "https://opendev.org/openstack/releases.git"
56+
releaser-user:
57+
uid: 56000
58+
gid: 56000
59+
set_fact:
60+
kolla_build_config: "{{ kolla_build_config | combine(kolla_build_external_docker_config, recursive=True) }}"
61+
when:
62+
- not publisher
63+
4764
- import_role:
4865
name: kolla-build-config
4966

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM {{ namespace }}/{{ image_prefix }}openstack-base:{{ tag }}
2+
3+
{% block labels %}
4+
LABEL maintainer="{{ maintainer }}" name="{{ image_name }}" build-date="{{ build_date }}"
5+
{% endblock %}
6+
7+
{% block releaser_header %}{% endblock %}
8+
9+
{% import "macros.j2" as macros with context %}
10+
11+
{{ macros.configure_user(name='releaser') }}
12+
13+
COPY extend_start.sh /usr/local/bin/kolla_extend_start
14+
15+
ADD releaser-archive /releaser-source
16+
17+
RUN ln -s releaser-source/* /releaser \
18+
&& {{ macros.install_pip(['/releaser'] | customizable("pip_packages")) }} \
19+
&& mkdir -p /etc/releaser \
20+
&& chown -R releaser: /etc/releaser \
21+
&& chmod 750 /etc/sudoers.d \
22+
&& touch /usr/local/bin/kolla_releaser_extend_start \
23+
&& chmod 644 /usr/local/bin/kolla_extend_start /usr/local/bin/kolla_releaser_extend_start
24+
25+
{% block footer %}{% endblock %}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
if [[ ! -d "/var/log/kolla/releaser" ]]; then
4+
mkdir -p /var/log/kolla/releaser
5+
fi
6+
7+
if [[ $(stat -c %a /var/log/kolla/releaser) != "755" ]]; then
8+
chmod 755 /var/log/kolla/releaser
9+
fi
10+
11+
. /usr/local/bin/kolla_releaser_extend_start

0 commit comments

Comments
 (0)