Skip to content

Commit

Permalink
move j2 template to erb in 7.17 (#16274)
Browse files Browse the repository at this point in the history
This should simplify CI infrastructure by not requiring python.
  • Loading branch information
jsvd authored Jul 3, 2024
1 parent 70be957 commit 92a601c
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 198 deletions.
1 change: 1 addition & 0 deletions Gemfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ gem "ruby-progressbar", "~> 1"
gem "logstash-output-elasticsearch", ">= 10.4.2"
gem "childprocess", "~> 4", :group => :build
gem "fpm", "~> 1", ">= 1.14.1", :group => :build # compound due to bugfix https://github.com/jordansissel/fpm/pull/1856
gem "erb", "~> 2", :group => :build
gem "gems", "~> 1", :group => :build
gem "octokit", "~> 4", :group => :build
gem "rubyzip", "~> 1", :group => :build
Expand Down
139 changes: 59 additions & 80 deletions docker/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
SHELL=/bin/bash
ELASTIC_REGISTRY ?= docker.elastic.co
PY_VERSION ?= 3.6.13
export PATH := ./bin:$(HOME)/.pyenv/bin:$(HOME)/.pyenv/shims:./venv/bin:$(PATH)

# Determine the version to build.
ELASTIC_VERSION := $(shell ../vendor/jruby/bin/jruby bin/elastic-version)
Expand All @@ -24,42 +22,34 @@ DEFAULT_IMAGE_FLAVOR ?= full
IMAGE_TAG := $(ELASTIC_REGISTRY)/logstash/logstash
HTTPD ?= logstash-docker-artifact-server

FIGLET := pyfiglet -w 160 -f puffy

all: build-from-local-artifacts build-from-local-oss-artifacts public-dockerfiles

lint: venv
flake8 tests

# Build from artifacts on the local filesystem, using an http server (running
# in a container) to provide the artifacts to the Dockerfile.
build-from-local-full-artifacts: venv dockerfile env2yaml
build-from-local-full-artifacts: dockerfile env2yaml
docker run --rm -d --name=$(HTTPD) \
-p 8000:8000 --expose=8000 -v $(ARTIFACTS_DIR):/mnt \
python:3 bash -c 'cd /mnt && python3 -m http.server'
timeout 120 bash -c 'until curl -s localhost:8000 > /dev/null; do sleep 1; done'
pyfiglet -f puffy -w 160 "Building: full"; \
docker build --network=host -t $(IMAGE_TAG)-full:$(VERSION_TAG) -f $(ARTIFACTS_DIR)/Dockerfile-full data/logstash || \
(docker kill $(HTTPD); false); \
docker tag $(IMAGE_TAG)-full:$(VERSION_TAG) $(IMAGE_TAG):$(VERSION_TAG);
docker kill $(HTTPD)

build-from-local-oss-artifacts: venv dockerfile env2yaml
build-from-local-oss-artifacts: dockerfile env2yaml
docker run --rm -d --name=$(HTTPD) \
-p 8000:8000 --expose=8000 -v $(ARTIFACTS_DIR):/mnt \
python:3 bash -c 'cd /mnt && python3 -m http.server'
timeout 120 bash -c 'until curl -s localhost:8000 > /dev/null; do sleep 1; done'
pyfiglet -f puffy -w 160 "Building: oss"; \
docker build --network=host -t $(IMAGE_TAG)-oss:$(VERSION_TAG) -f $(ARTIFACTS_DIR)/Dockerfile-oss data/logstash || \
(docker kill $(HTTPD); false);
-docker kill $(HTTPD)

build-from-local-ubi8-artifacts: venv dockerfile env2yaml
build-from-local-ubi8-artifacts: dockerfile env2yaml
docker run --rm -d --name=$(HTTPD) \
-p 8000:8000 --expose=8000 -v $(ARTIFACTS_DIR):/mnt \
python:3 bash -c 'cd /mnt && python3 -m http.server'
timeout 120 bash -c 'until curl -s localhost:8000 > /dev/null; do sleep 1; done'
pyfiglet -f puffy -w 160 "Building: ubi8"; \
docker build --network=host -t $(IMAGE_TAG)-ubi8:$(VERSION_TAG) -f $(ARTIFACTS_DIR)/Dockerfile-ubi8 data/logstash || \
(docker kill $(HTTPD); false);
-docker kill $(HTTPD)
Expand Down Expand Up @@ -111,61 +101,61 @@ ironbank_docker_paths:

public-dockerfiles: public-dockerfiles_oss public-dockerfiles_full public-dockerfiles_ubi8 public-dockerfiles_ironbank

public-dockerfiles_full: venv templates/Dockerfile.j2 docker_paths $(COPY_FILES)
jinja2 \
-D created_date='$(BUILD_DATE)' \
-D elastic_version='$(ELASTIC_VERSION)' \
-D arch='${ARCHITECTURE}' \
-D version_tag='$(VERSION_TAG)' \
-D image_flavor='full' \
-D local_artifacts='false' \
-D release='$(RELEASE)' \
templates/Dockerfile.j2 > $(ARTIFACTS_DIR)/Dockerfile-full && \
public-dockerfiles_full: templates/Dockerfile.erb docker_paths $(COPY_FILES)
../vendor/jruby/bin/jruby -S ../bin/bundle exec erb -T "-" \
created_date="${BUILD_DATE}" \
elastic_version="${ELASTIC_VERSION}" \
arch="${ARCHITECTURE}" \
version_tag="${VERSION_TAG}" \
release="${RELEASE}" \
image_flavor="full" \
local_artifacts="false" \
templates/Dockerfile.erb > "${ARTIFACTS_DIR}/Dockerfile-full" && \
cd $(ARTIFACTS_DIR)/docker && \
cp $(ARTIFACTS_DIR)/Dockerfile-full Dockerfile && \
tar -zcf ../logstash-$(VERSION_TAG)-docker-build-context.tar.gz Dockerfile bin config env2yaml pipeline

public-dockerfiles_oss: venv templates/Dockerfile.j2 docker_paths $(COPY_FILES)
jinja2 \
-D created_date='$(BUILD_DATE)' \
-D elastic_version='$(ELASTIC_VERSION)' \
-D arch='${ARCHITECTURE}' \
-D version_tag='$(VERSION_TAG)' \
-D image_flavor='oss' \
-D local_artifacts='false' \
-D release='$(RELEASE)' \
templates/Dockerfile.j2 > $(ARTIFACTS_DIR)/Dockerfile-oss && \
public-dockerfiles_oss: templates/Dockerfile.erb docker_paths $(COPY_FILES)
../vendor/jruby/bin/jruby -S ../bin/bundle exec erb -T "-" \
created_date="${BUILD_DATE}" \
elastic_version="${ELASTIC_VERSION}" \
arch="${ARCHITECTURE}" \
version_tag="${VERSION_TAG}" \
release="${RELEASE}" \
image_flavor="oss" \
local_artifacts="false" \
templates/Dockerfile.erb > "${ARTIFACTS_DIR}/Dockerfile-oss" && \
cd $(ARTIFACTS_DIR)/docker && \
cp $(ARTIFACTS_DIR)/Dockerfile-oss Dockerfile && \
tar -zcf ../logstash-oss-$(VERSION_TAG)-docker-build-context.tar.gz Dockerfile bin config env2yaml pipeline

public-dockerfiles_ubi8: venv templates/Dockerfile.j2 docker_paths $(COPY_FILES)
jinja2 \
-D created_date='$(BUILD_DATE)' \
-D elastic_version='$(ELASTIC_VERSION)' \
-D arch='${ARCHITECTURE}' \
-D version_tag='$(VERSION_TAG)' \
-D image_flavor='ubi8' \
-D local_artifacts='false' \
-D release='$(RELEASE)' \
templates/Dockerfile.j2 > $(ARTIFACTS_DIR)/Dockerfile-ubi8 && \
public-dockerfiles_ubi8: templates/Dockerfile.erb docker_paths $(COPY_FILES)
../vendor/jruby/bin/jruby -S ../bin/bundle exec erb -T "-" \
created_date="${BUILD_DATE}" \
elastic_version="${ELASTIC_VERSION}" \
arch="${ARCHITECTURE}" \
version_tag="${VERSION_TAG}" \
release="${RELEASE}" \
image_flavor="ubi8" \
local_artifacts="false" \
templates/Dockerfile.erb > "${ARTIFACTS_DIR}/Dockerfile-ubi8" && \
cd $(ARTIFACTS_DIR)/docker && \
cp $(ARTIFACTS_DIR)/Dockerfile-ubi8 Dockerfile && \
tar -zcf ../logstash-ubi8-$(VERSION_TAG)-docker-build-context.tar.gz Dockerfile bin config env2yaml pipeline

public-dockerfiles_ironbank: templates/hardening_manifest.yaml templates/Dockerfile.j2 ironbank_docker_paths $(COPY_IRONBANK_FILES)
jinja2 \
-D elastic_version='$(ELASTIC_VERSION)' \
templates/hardening_manifest.yaml > $(ARTIFACTS_DIR)/ironbank/hardening_manifest.yaml && \
jinja2 \
-D created_date='$(BUILD_DATE)' \
-D elastic_version='$(ELASTIC_VERSION)' \
-D arch='${ARCHITECTURE}' \
-D version_tag='$(VERSION_TAG)' \
-D image_flavor='ironbank' \
-D local_artifacts='false' \
-D release='$(RELEASE)' \
templates/Dockerfile.j2 > $(ARTIFACTS_DIR)/Dockerfile-ironbank && \
public-dockerfiles_ironbank: templates/hardening_manifest.yaml.erb templates/Dockerfile.erb ironbank_docker_paths $(COPY_IRONBANK_FILES)
../vendor/jruby/bin/jruby -S ../bin/bundle exec erb -T "-" \
elastic_version="${ELASTIC_VERSION}" \
templates/hardening_manifest.yaml.erb > $(ARTIFACTS_DIR)/ironbank/hardening_manifest.yaml && \
../vendor/jruby/bin/jruby -S ../bin/bundle exec erb -T "-" \
created_date="${BUILD_DATE}" \
elastic_version="${ELASTIC_VERSION}" \
arch="${ARCHITECTURE}" \
version_tag="${VERSION_TAG}" \
release="${RELEASE}" \
image_flavor="ironbank" \
local_artifacts="false" \
templates/Dockerfile.erb > "${ARTIFACTS_DIR}/Dockerfile-ironbank" && \
cd $(ARTIFACTS_DIR)/ironbank && \
cp $(ARTIFACTS_DIR)/Dockerfile-ironbank Dockerfile && \
tar -zcf ../logstash-ironbank-$(VERSION_TAG)-docker-build-context.tar.gz scripts Dockerfile hardening_manifest.yaml LICENSE README.md
Expand All @@ -182,39 +172,28 @@ push:
docker push push.$(IMAGE_TAG):$(VERSION_TAG);
docker rmi push.$(IMAGE_TAG):$(VERSION_TAG);

# The tests are written in Python. Make a virtualenv to handle the dependencies.
venv: requirements.txt
LOCAL_PY_VER=`python3 --version 2>&1`&&\
echo "Was using $$LOCAL_PY_VER" &&\
eval "$$(pyenv init -)" && eval "$$(pyenv init --path)" &&\
pyenv install --skip-existing $(PY_VERSION) &&\
pyenv local $(PY_VERSION) &&\
python3 -mvenv venv && \
for i in 0 1 2 3 4 5; do sleep "$i"; pip install -r requirements.txt && break; done &&\
touch venv

# Compile "env2yaml", the helper for configuring logstash.yml via environment
# variables.
env2yaml:
docker run --rm \
-v "$(PWD)/data/logstash/env2yaml:/usr/src/env2yaml" \
-w /usr/src/env2yaml golang:1 go build
-w /usr/src/env2yaml golang:1 go build

# Generate the Dockerfiles from Jinja2 templates.
dockerfile: venv templates/Dockerfile.j2
# Generate the Dockerfiles from ERB templates.
dockerfile: templates/Dockerfile.erb
$(foreach FLAVOR, $(IMAGE_FLAVORS), \
jinja2 \
-D created_date='$(BUILD_DATE)' \
-D elastic_version='$(ELASTIC_VERSION)' \
-D arch='${ARCHITECTURE}' \
-D version_tag='$(VERSION_TAG)' \
-D image_flavor='$(FLAVOR)' \
-D local_artifacts='true' \
templates/Dockerfile.j2 > $(ARTIFACTS_DIR)/Dockerfile-$(FLAVOR); \
../vendor/jruby/bin/jruby -S ../bin/bundle exec erb -T "-" \
created_date="${BUILD_DATE}" \
elastic_version="${ELASTIC_VERSION}" \
arch="${ARCHITECTURE}" \
version_tag="${VERSION_TAG}" \
image_flavor="${FLAVOR}" \
local_artifacts="true" \
templates/Dockerfile.erb > "${ARTIFACTS_DIR}/Dockerfile-${FLAVOR}" ; \
)

clean:
rm -f ${ARTIFACTS_DIR}/env2yaml/env2yaml ${ARTIFACTS_DIR}/Dockerfile
rm -rf venv
rm -f ${ARTIFACTS_DIR}/env2yaml/env2yaml-* ${ARTIFACTS_DIR}/Dockerfile


.PHONY: clean push
5 changes: 0 additions & 5 deletions docker/bin/pytest

This file was deleted.

6 changes: 0 additions & 6 deletions docker/requirements.txt

This file was deleted.

Loading

0 comments on commit 92a601c

Please sign in to comment.