Skip to content

Commit

Permalink
support for image generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dhodovsk committed Jan 8, 2018
1 parent a068a12 commit 6545ac2
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
26 changes: 26 additions & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@ build = $(common_dir)/build.sh
test = $(common_dir)/test.sh
tag = $(common_dir)/tag.sh
clean = $(common_dir)/clean.sh
generator = $(common_dir)/generate.sh

ifeq ($(TARGET),rhel7)
SKIP_SQUASH ?= 0
OS := rhel7
DOCKERFILE ?= Dockerfile.rhel7
DG_CONF := rhel-7-x86_64.yaml
else ifeq ($(TARGET),fedora)
OS := fedora
DOCKERFILE ?= Dockerfile.fedora
DG_CONF := fedora-27-x86_64.yaml
else
OS := centos7
DOCKERFILE ?= Dockerfile
DG_CONF ?= centos-7-x86_64.yaml
endif

SKIP_SQUASH ?= 1
DOCKER_BUILD_CONTEXT ?= .
DISTGEN_BIN ?= /usr/bin/dg
MANIFEST_FILE ?= manifest.sh

script_env = \
SKIP_SQUASH=$(SKIP_SQUASH) \
Expand All @@ -35,6 +41,12 @@ script_env = \
DOCKER_BUILD_CONTEXT=$(DOCKER_BUILD_CONTEXT) \
OPENSHIFT_NAMESPACES="$(OPENSHIFT_NAMESPACES)"

generation_env = \
DG_CONF=$(DG_CONF) \
DG=$(DISTGEN_BIN) \
MANIFEST_FILE=$(MANIFEST_FILE)


# TODO: switch to 'build: build-all' once parallel builds are relatively safe
.PHONY: build build-serial build-all
build: build-serial
Expand Down Expand Up @@ -79,3 +91,17 @@ clean:
mkdir -p $(@D)
go-md2man -in "$^" -out "$@"
chmod a+r "$@"

.PHONY: generate
generate:
@$(MAKE) auto_targets.mk
@$(MAKE) exec-gen-rules
rm auto_targets.mk

auto_targets.mk: $(generator) $(MANIFEST_FILE)
VERSIONS="$(VERSIONS)" $(generation_env) $(generator)

include auto_targets.mk

.PHONY: exec-gen-rules
exec-gen-rules: $(DISTGEN_TARGETS) $(COPY_TARGETS) $(SYMLINK_TARGETS)
119 changes: 119 additions & 0 deletions generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/bin/bash

# This script is used to create image directories using distgen, cp or ln
# It requires manifest.sh file to be present in image repository
# The manifest file should contain set of rules in form:
# <rules_type>="
# src=<path to source>
# dest=<path to destination>
# mode=<destination file mode (optional)>;
# ...;
# "
#
# Supported type rules are now COPY_RULES, DISTGEN_RULES and SYMLINKS_RULES
# for real example see https://github.com/sclorg/postgresql-container/blob/master/manifest.sh

source manifest.sh

test -f auto_targets.mk && rm auto_targets.mk

DESTDIR="${DESTDIR:-$PWD}"

clean_rule_variables(){
src=""
dest=""
mode=""
link_target=""
link_name=""
}

parse_rules() {
targets=""
OLD_IFS=$IFS
IFS=";"
for rule in $rules; do
if [ -z $(echo "$rule"| tr -d '[:space:]') ]; then
continue
fi
clean_rule_variables
eval $rule

case "$creator" in
copy)
[[ -z "$src" ]] && echo "src has to be specified in copy rule" && exit 1
[[ -z "$dest" ]] && echo "dest has to be specified in copy rule" && exit 1
core_subst=$core
;;
distgen)
[[ -z "$src" ]] && echo "src has to be specified in distgen rule" && exit 1
[[ -z "$dest" ]] && echo "dest has to be specified in distgen rule" && exit 1
core_subst=$core
;;
link)
[[ -z "$link_name" ]] && echo "link_name has to be specified in link rule" && exit 1
[[ -z "$link_target" ]] && echo "link_target has to be specified in link rule" && exit 1
dest="$link_name"
core_subst=$(echo $core | sed -e "s~__link_target__~"${link_target}"~g")
;;
esac
t=$(echo ${DESTDIR}/${version}/${dest} | sed 's/ /\\ /g')
src=$(echo $src | sed 's/ /\\ /g' )
targets+=" $t \\
"
cat >> auto_targets.mk << EOF
$t: $src manifest.sh
@echo ${message} ; \\
mkdir -p "\$\$(dirname \$@)" || exit 1 ; \\
${core_subst}
${mode:+"chmod ${mode} "\$@""}
EOF
done
IFS=$OLD_IFS
}

for version in ${VERSIONS}; do
# copy targets
rules="$COPY_RULES"
core="cp \$< \$@ ; \\"
message="Copying \"\$@\""
creator="copy"
parse_rules
COPY_TARGETS+="$targets"


# distgen targets
rules="$DISTGEN_RULES"
core="${DG} --multispec specs/multispec.yml \\
--template \"\$<\" --distro \"$DG_CONF\" \\
--multispec-selector version=\"$version\" --output \"\$@\" ; \\"
message="Generating \"\$@\" using distgen"
creator="distgen"
parse_rules
DISTGEN_TARGETS+="$targets"


rules=$SYMLINK_RULES
core="ln -fs __link_target__ \$@ ; \\"
message="Creating symlink \"\$@\""
creator="link"
parse_rules
SYMLINK_TARGETS+="$targets"
done

# adding COPY_TARGETS variable at the bottom of auto_targets.mk file
cat -v >> auto_targets.mk << EOF
COPY_TARGETS = \\
$COPY_TARGETS
EOF

# adding DISTGEN_TARGETS variable at the bottom of auto_targets.mk file
cat -v >> auto_targets.mk << EOF
DISTGEN_TARGETS = \\
$DISTGEN_TARGETS
EOF

cat -v >> auto_targets.mk << EOF
SYMLINK_TARGETS = \\
$SYMLINK_TARGETS
EOF

0 comments on commit 6545ac2

Please sign in to comment.