-
Notifications
You must be signed in to change notification settings - Fork 6
/
entrypoint.sh
executable file
·155 lines (134 loc) · 5.72 KB
/
entrypoint.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env sh
# INPUT ARGUMENT SPLITTING:
#
# By default, the entrypoint assumes that the PHP entrypoint "/opt/guides/vendor/bin/guides"
# is executed, and all arguments passed on to that PHP script.
#
# When executing Docker via:
#
# $ docker run --rm -v $(pwd):/project -it typo3-docs:local --progress SomeDocsFolder"
#
# then this script receives the input "/opt/guides/vendor/bin/guides --progress SomeDocsFolder"
# as "$@" input. This is passed on then to the PHP binary.
#
# Before the final destination script is executed, this entrypoint performs some basic
# invocation checks, i.e. if Docker is run as root, or as a custom user, and if the
# internal user needs to be changed. This is needed so that the parameters
# "--user=X:Y" (i.e. "--user=$(id -u):$(id -g)" are not required to be passed to Docker.
#
# Several internal commands are allowed to be added as a prefix to alter the entrypoint:
#
# * render [... arguments/options/input] (default; just as if omitted)
# * migrate [... arguments/options/input] (migrates Settings.cfg to guides.xml)
# * lint-guides-xml [... arguments/options/input] (lints all guides.xml files found in the directory)
# * configure [... arguments/options/input] (allows to alter guides.xml variables)
#
# All of the commands can be prefixed with "verbose" to trigger additional information to be shown:
#
# $ docker run --rm -v $(pwd):/project -it typo3-docs:local verbose migrate SomeDocsFolderWithSettings"
#
# Only those commands shall be provided here that users of the "official Docker container"
# need to perform the rendering. The Container-microservice concept suggests to only have one
# "concern" (entrypoint) per container, so this concept is bending the idea of multiple commands
# and shall be used with care.
#
# ONLY PHP-SCRIPTS will (and shall) be executed by this entrypoint.
MY_UID="$(id -u)"
if [ "$1" = "verbose" ]; then
echo "Full input arguments: \$ $@"
SHELL_VERBOSITY="3"
# Removes "verbose" from the input argument list
shift
echo "Parsed input: \$ $@"
elif [ -z "${SHELL_VERBOSITY}" ]; then
SHELL_VERBOSITY="0"
else
# SHELL_VERBOSITY is triggered via call-time pass (i.e. `SHELL_VERBOSITY=yes docker run ...`)
SHELL_VERBOSITY="${SHELL_VERBOSITY}"
fi
if [ "${SHELL_VERBOSITY}" -gt 0 ]; then
echo "SHELL_VERBOSITY is: ${SHELL_VERBOSITY}"
echo "UID of executing user: ${MY_UID}"
echo "GITHUB_ACTIONS: ${GITHUB_ACTIONS}"
echo "CDN VERSION: ${TYPO3AZUREEDGEURIVERSION}"
fi
ENTRYPOINT_DEFAULT="/opt/guides/vendor/bin/guides"
ENTRYPOINT_SYMFONY_COMMANDS="/opt/guides/packages/typo3-guides-cli/bin/typo3-guides"
# This is intentionally a specified list of allowed scripts.
# If an allowed initial entrypoint is found, shift arguments by one.
if [ "$1" = "migrate" ]; then
ENTRYPOINT="${ENTRYPOINT_SYMFONY_COMMANDS} migrate"
shift
elif [ "$1" = "lint-guides-xml" ]; then
ENTRYPOINT="${ENTRYPOINT_SYMFONY_COMMANDS} lint-guides-xml"
shift
elif [ "$1" = "configure" ]; then
ENTRYPOINT="${ENTRYPOINT_SYMFONY_COMMANDS} configure"
shift
elif [ "$1" = "render" ]; then
ENTRYPOINT="${ENTRYPOINT_DEFAULT}"
shift
else
# Default: "render"; no shifting.
ENTRYPOINT="${ENTRYPOINT_DEFAULT}"
fi
if [ "${SHELL_VERBOSITY}" -gt 0 ]; then
# Also pass shell verbosity as a fixed argument to the execution.
ENTRYPOINT="${ENTRYPOINT} -vvv $@"
echo "ENTRYPOINT: \$ ${ENTRYPOINT}"
else
ENTRYPOINT="${ENTRYPOINT} $@"
fi
if [ "${MY_UID}" -eq "0" ]; then
UID=$(stat -c "%u" $(pwd))
GID=$(stat -c "%g" $(pwd))
if [ "$UID" -eq "0" ]; then
if [ "${SHELL_VERBOSITY}" -gt 0 ]; then
echo "Run-as: root"
echo "Invocation: php ${ENTRYPOINT}"
fi
php ${ENTRYPOINT}
else
if [ "${SHELL_VERBOSITY}" -gt 0 ]; then
echo "Run-as: $UID (custom invocation)"
fi
addgroup typo3 --gid=$GID;
if [ $? -ne 0 ]; then
echo "Error: Failed to add group 'typo3' inside docker container."
echo "This can happen if docker does not run as root. Please add the"
echo "argument '--user=\$(id -u):\$(id -g)' to your 'docker run...' call."
exit 1
fi
adduser -h $(pwd) -D -G typo3 --uid=$UID typo3;
if [ $? -ne 0 ]; then
echo "Error: Failed to add user 'typo3' inside docker container."
echo "This can happen if docker does not run as root. Please add the"
echo "argument '--user=\$(id -u):\$(id -g)' to your 'docker run...' call."
exit 1
fi
# su behaves inconsistently with -c followed by flags
# Workaround: run the entrypoint and commands as a standalone script
echo "#!/usr/bin/env sh" > /usr/local/bin/invocation.sh
echo >> /usr/local/bin/invocation.sh
echo "export SHELL_VERBOSITY=${SHELL_VERBOSITY}" >> /usr/local/bin/invocation.sh
echo "export TYPO3AZUREEDGEURIVERSION=${TYPO3AZUREEDGEURIVERSION}" >> /usr/local/bin/invocation.sh
echo "export GITHUB_ACTIONS=${GITHUB_ACTIONS}" >> /usr/local/bin/invocation.sh
for ARG in "${ENTRYPOINT}"; do
printf "${ARG} " >> /usr/local/bin/invocation.sh
done
chmod a+x /usr/local/bin/invocation.sh
if [ "${SHELL_VERBOSITY}" -gt 0 ]; then
echo "Run-as: root"
echo "Invocation:"
cat /usr/local/bin/invocation.sh
echo ""
fi
su - typo3 -c "/usr/local/bin/invocation.sh"
fi
else
if [ "${SHELL_VERBOSITY}" -gt 0 ]; then
echo "Run-as: Owner ${MY_UID}"
echo "Invocation: php ${ENTRYPOINT}"
fi
php ${ENTRYPOINT}
fi