Skip to content

Commit dfa60ca

Browse files
committed
support for image generation
1 parent a068a12 commit dfa60ca

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

common.mk

+26
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,27 @@ build = $(common_dir)/build.sh
1111
test = $(common_dir)/test.sh
1212
tag = $(common_dir)/tag.sh
1313
clean = $(common_dir)/clean.sh
14+
generator = $(common_dir)/generate.sh
1415

1516
ifeq ($(TARGET),rhel7)
1617
SKIP_SQUASH ?= 0
1718
OS := rhel7
1819
DOCKERFILE ?= Dockerfile.rhel7
20+
DG_CONF := rhel-7-x86_64.yaml
1921
else ifeq ($(TARGET),fedora)
2022
OS := fedora
2123
DOCKERFILE ?= Dockerfile.fedora
24+
DG_CONF := fedora-26-x86_64.yaml
2225
else
2326
OS := centos7
2427
DOCKERFILE ?= Dockerfile
28+
DG_CONF ?= centos-7-x86_64.yaml
2529
endif
2630

2731
SKIP_SQUASH ?= 1
2832
DOCKER_BUILD_CONTEXT ?= .
33+
DISTGEN_BIN ?= /usr/bin/dg
34+
MANIFEST_FILE ?= manifest.sh
2935

3036
script_env = \
3137
SKIP_SQUASH=$(SKIP_SQUASH) \
@@ -35,6 +41,12 @@ script_env = \
3541
DOCKER_BUILD_CONTEXT=$(DOCKER_BUILD_CONTEXT) \
3642
OPENSHIFT_NAMESPACES="$(OPENSHIFT_NAMESPACES)"
3743

44+
generation_env = \
45+
DG_CONF=$(DG_CONF) \
46+
DG=$(DISTGEN_BIN) \
47+
MANIFEST_FILE=$(MANIFEST_FILE)
48+
49+
3850
# TODO: switch to 'build: build-all' once parallel builds are relatively safe
3951
.PHONY: build build-serial build-all
4052
build: build-serial
@@ -79,3 +91,17 @@ clean:
7991
mkdir -p $(@D)
8092
go-md2man -in "$^" -out "$@"
8193
chmod a+r "$@"
94+
95+
.PHONY: generate
96+
generate:
97+
@$(MAKE) auto_targets.mk
98+
@$(MAKE) exec-gen-rules
99+
rm auto_targets.mk
100+
101+
auto_targets.mk: $(generator) $(MANIFEST_FILE)
102+
VERSIONS="$(VERSIONS)" $(generation_env) $(generator)
103+
104+
include auto_targets.mk
105+
106+
.PHONY: exec-gen-rules
107+
exec-gen-rules: $(DISTGEN_TARGETS) $(COPY_TARGETS) $(SYMLINK_TARGETS)

generate.sh

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/bin/bash
2+
3+
# This script is used to create image directories using distgen, cp or ln
4+
# It requires manifest.sh file to be present in image repository
5+
# The manifest file should contain set of rules in form:
6+
# <rules_type>="
7+
# src=<path to source>
8+
# dest=<path to destination>
9+
# mode=<destination file mode (optional)>;
10+
# ...;
11+
# "
12+
#
13+
# Supported type rules are now COPY_RULES, DISTGEN_RULES and SYMLINKS_RULES
14+
# for real example see https://github.com/sclorg/postgresql-container/blob/master/manifest.sh
15+
16+
source manifest.sh
17+
18+
test -f auto_targets.mk && rm auto_targets.mk
19+
20+
DEST_DIR="${DEST_DIR:-$PWD}"
21+
22+
clean_rule_variables(){
23+
src=""
24+
dest=""
25+
mode=""
26+
link_target=""
27+
link_name=""
28+
}
29+
30+
parse_rules() {
31+
targets=""
32+
OLD_IFS=$IFS
33+
IFS=";"
34+
for rule in $rules; do
35+
if [ -z $(echo "$rule"| tr -d '[:space:]') ]; then
36+
continue
37+
fi
38+
clean_rule_variables
39+
eval $rule
40+
41+
case "$creator" in
42+
copy)
43+
[[ -z "$src" ]] && echo "src has to be specified in copy rule" && exit 1
44+
[[ -z "$dest" ]] && echo "dest has to be specified in copy rule" && exit 1
45+
core_subst=$core
46+
;;
47+
distgen)
48+
[[ -z "$src" ]] && echo "src has to be specified in distgen rule" && exit 1
49+
[[ -z "$dest" ]] && echo "dest has to be specified in distgen rule" && exit 1
50+
core_subst=$core
51+
;;
52+
link)
53+
[[ -z "$link_name" ]] && echo "link_name has to be specified in link rule" && exit 1
54+
[[ -z "$link_target" ]] && echo "link_target has to be specified in link rule" && exit 1
55+
dest="$link_name"
56+
link_target="$DEST_DIR/$version/$link_target"
57+
core_subst=$(echo $core | sed -e "s~__link_target__~"${link_target}"~g")
58+
;;
59+
esac
60+
t=$(echo ${DEST_DIR}/${version}/${dest} | sed 's/ /\\ /g')
61+
src=$(echo $src | sed 's/ /\\ /g' )
62+
echo "$t"
63+
targets+=" $t \\
64+
"
65+
cat >> auto_targets.mk << EOF
66+
$t: $src manifest.sh
67+
@echo ${message} ; \\
68+
mkdir -p "\$\$(dirname \$@)" || exit 1 ; \\
69+
${core_subst}
70+
${mode:+"chmod ${mode} "\$@""}
71+
72+
EOF
73+
done
74+
IFS=$OLD_IFS
75+
}
76+
77+
for version in ${VERSIONS}; do
78+
# copy targets
79+
rules="$COPY_RULES"
80+
core="cp \$< \$@ ; \\"
81+
message="Copying \"\$@\""
82+
creator="copy"
83+
parse_rules
84+
COPY_TARGETS+="$targets"
85+
86+
87+
# distgen targets
88+
rules="$DISTGEN_RULES"
89+
core="${DG} --multispec specs/multispec.yml \\
90+
--template \"\$<\" --distro \"$DG_CONF\" \\
91+
--multispec-selector version=\"$version\" --output \"\$@\" ; \\"
92+
message="Generating \"\$@\" using distgen"
93+
creator="distgen"
94+
parse_rules
95+
DISTGEN_TARGETS+="$targets"
96+
97+
98+
rules=$SYMLINK_RULES
99+
core="ln -fs __link_target__ \$@ ; \\"
100+
message="Creating symlink \"\$@\""
101+
creator="link"
102+
parse_rules
103+
SYMLINK_TARGETS+="$targets"
104+
done
105+
106+
# adding COPY_TARGETS variable at the bottom of auto_targets.mk file
107+
cat -v >> auto_targets.mk << EOF
108+
COPY_TARGETS = \\
109+
$COPY_TARGETS
110+
EOF
111+
112+
# adding DISTGEN_TARGETS variable at the bottom of auto_targets.mk file
113+
cat -v >> auto_targets.mk << EOF
114+
DISTGEN_TARGETS = \\
115+
$DISTGEN_TARGETS
116+
EOF
117+
118+
cat -v >> auto_targets.mk << EOF
119+
SYMLINK_TARGETS = \\
120+
$SYMLINK_TARGETS
121+
EOF

0 commit comments

Comments
 (0)