-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
460 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
target/ | ||
test-output/ | ||
build.log | ||
docker/http-proxy/usr/local/bin/proxytpl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
tests/ | ||
README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
FROM alpine:3.6 | ||
|
||
ENV LANG=en_US.utf8 \ | ||
DOCKER_USER=dockeru \ | ||
DOCKER_USER_ID=1000 \ | ||
DOCKER_USER_GROUP_ID=1000 | ||
|
||
COPY rootfs/ / | ||
|
||
RUN apk --no-cache add curl bash && \ | ||
apk --no-cache --repository http://dl-2.alpinelinux.org/alpine/edge/community add shadow && \ | ||
mkdir /opt /docker-init.d && \ | ||
addgroup -g $DOCKER_USER_GROUP_ID $DOCKER_USER && \ | ||
adduser $DOCKER_USER -u $DOCKER_USER_ID -S -G $DOCKER_USER -s /bin/bash && \ | ||
sed -i -e 's|/bin/sh|/bin/bash|' -e 's|/bin/ash|/bin/bash|' /etc/passwd && \ | ||
curl -o /usr/local/bin/gosu -fsSL "https://github.com/tianon/gosu/releases/download/1.9/gosu-amd64" && \ | ||
chmod +x /entrypoint.sh /usr/local/bin/gosu /usr/local/bin/*.sh | ||
|
||
ENTRYPOINT ["/entrypoint.sh"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Base image for all Sirma Enterprise Systems docker images | ||
|
||
This image provides the main entry point for assigning user permissions and contains useful scripts. | ||
|
||
Creates a dedicated user **dockeru** that will obtain ownership of all: | ||
* volumes | ||
* services | ||
* scripts placed in /docker-init.d | ||
|
||
## Useful scripts | ||
### replace-env.sh - Replacing environment variables inside files | ||
|
||
Example usage: `replace-env.sh file1.conf file2.xml` | ||
|
||
When invoked, this script will be executed for every line in the provided files. | ||
|
||
The script supports replacing environment variables only in the following format: `${VARIABLE}` | ||
(In other words - uppercase variable in single curly brackets starting with a dollar sign.) | ||
|
||
The script will preserve any non binded variables. | ||
|
||
### wait-for-service.sh - Wait for specific service to become reachable | ||
|
||
Example usage: `wait-for-service.sh -h localhost -p 5432 -t 5 -m 10` | ||
|
||
Command summary | ||
-h, --host - the service hostname | ||
-p, --port - the service port | ||
-t, --timeout - the timeout in seconds between tries | ||
-m, --max-tries - the maximum tries to reach the service | ||
|
||
|
||
This script uses netcat internally. | ||
|
||
## Built in user | ||
The container comes with a user called `dockeru` which is used to run the actual service. | ||
The user's UID and GID are `1000` by default, but they can be changed by setting the `DOCKER_USER_ID` and `DOCKER_USER_GROUP_ID` environment variables. | ||
__NOTE__: if you change the UID or GID - you must manually change ownership of any volues. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
|
||
changeown() { | ||
for dir in $1; do | ||
# if the root dir has the correct ownership - assume alles gut | ||
if [ "$(stat -c '%U:%G' $dir)" != "$DOCKER_USER:$DOCKER_USER" ]; then | ||
echo "Changing ownership of $dir" | ||
chown -R $DOCKER_USER:$DOCKER_USER $dir | ||
fi | ||
done | ||
} | ||
|
||
echo -e "\n################# env #################" | ||
env | ||
echo -e "#######################################\n" | ||
|
||
usermod -u $DOCKER_USER_ID $DOCKER_USER | ||
groupmod -g $DOCKER_USER_GROUP_ID $DOCKER_USER | ||
|
||
# syncing here to workaround "resource busy" problems for mounted files | ||
sync | ||
|
||
echo "Setting ownership ($DOCKER_USER:$DOCKER_USER) on volume and service dirs" | ||
changeown "$(env | grep -e ^SERVICE_DIR_ -e ^VOLUME_ | cut -d'=' -f2)" | ||
|
||
echo "Running /docker-init.d scripts" | ||
if [ -d /docker-init.d ]; then | ||
for f in /docker-init.d/*.sh; do | ||
[ -f "$f" ] && . "$f" | ||
done | ||
fi | ||
|
||
sync | ||
|
||
set +u | ||
|
||
if [ -n "$RUN_AS_ROOT" ]; then | ||
echo "Running $@ as root" | ||
gosu root "$@" | ||
else | ||
echo "Running $@" | ||
gosu $DOCKER_USER "$@" | ||
fi |
22 changes: 22 additions & 0 deletions
22
docker/base/2.0.1/rootfs/usr/local/bin/replace-env-vars.awk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
string = $0; | ||
start = index(string, "${"); | ||
|
||
while(start > 0) { | ||
end = index(string, "}"); | ||
if(end > 0) { | ||
variable = substr(string, start + 2, end - start - 2); | ||
evaluated = ENVIRON[variable]; | ||
if(length(evaluated) > 0) { | ||
toreplace = "[$]{"variable"}"; | ||
gsub(toreplace, evaluated, $0); | ||
} | ||
string = substr(string, end + 1); | ||
start = index(string, "${"); | ||
} else { | ||
string = substr(string, start + 2); | ||
start = index(string, "${"); | ||
} | ||
} | ||
print $0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -eu | ||
|
||
script_dir="$( cd "$( dirname "$0" )" && pwd )" | ||
|
||
for file in $@; do | ||
if [ -s $file ]; then | ||
awk -f "$script_dir/replace-env-vars.awk" "${file}" > "${file}.tmp"; | ||
sync | ||
mv "${file}.tmp" "${file}" | ||
else | ||
echo "The specified file ${file} does NOT exist!"; | ||
fi | ||
done |
63 changes: 63 additions & 0 deletions
63
docker/base/2.0.1/rootfs/usr/local/bin/wait-for-service.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -eu | ||
|
||
# TODO: Add completion ? | ||
# TODO: Add blocking wait ? | ||
|
||
# Defaults | ||
: ${tries=1} | ||
: ${timeout=2} | ||
: ${max_tries=20} | ||
|
||
|
||
# Reading command line arguments | ||
while [ $# -gt 1 ]; do | ||
case $1 in | ||
-h|--host) | ||
host="$2" | ||
shift | ||
;; | ||
-p|--port) | ||
port="$2" | ||
shift | ||
;; | ||
-t|--timeout) | ||
timeout="$2" | ||
shift | ||
;; | ||
-m|--max_tries) | ||
max_tries="$2" | ||
shift | ||
;; | ||
*) | ||
echo "Argument $1 is not supported"; | ||
;; | ||
esac | ||
shift | ||
done | ||
|
||
if [ -z ${host} ]; then | ||
echo "Provide the service host!"; | ||
exit 1; | ||
fi | ||
|
||
if [ -z ${port} ]; then | ||
echo "Provide the service port!"; | ||
exit 1; | ||
fi | ||
|
||
service="${host}:${port}" | ||
|
||
echo "Going to wait for ${service} to be reachable..."; | ||
until $(nc -z -w 1 ${host} ${port} > /dev/null); do | ||
echo "${service} is not visible yet... retrying every $timeout seconds... ($tries/$max_tries)"; | ||
sleep $timeout | ||
|
||
if [ $tries -eq $max_tries ]; then | ||
echo "ERROR: After [${tries}] retries ${service} couldn't be reached!"; | ||
exit 1 | ||
fi | ||
tries=$((tries+1)) | ||
done | ||
echo "${service} is successfully reached after [${tries}] tries"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sleep 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
user: | ||
dockeru: | ||
exists: true | ||
|
||
package: | ||
curl: | ||
installed: true | ||
bash: | ||
installed: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/usr/bin/env bats | ||
|
||
DIR=${BATS_TEST_DIRNAME} | ||
|
||
function replace() { | ||
awk -f ${DIR}/../rootfs/usr/local/bin/replace-env-vars.awk ${DIR}/$1 > ${DIR}/$2 | ||
} | ||
|
||
@test "Replacing environment variables in an empty file" { | ||
replace resources/1-empty 1-result | ||
result=$(comm -23 ${DIR}/1-result ${DIR}/resources/1-empty | wc -l) | ||
[ "$result" -eq 0 ] | ||
} | ||
|
||
@test "Replacing environment variables in a file without declared variables" { | ||
replace resources/2-nothing-to-replace 2-result | ||
result=$(comm -23 ${DIR}/2-result ${DIR}/resources/2-nothing-to-replace | wc -l) | ||
[ "$result" -eq 0 ] | ||
} | ||
|
||
@test "Replacing environment variables in a file having some declared variables" { | ||
export SOMETHING="SoMeThiNG" | ||
export SECOND="SeconD" | ||
export END="EnD" | ||
export OR="Or" | ||
export QUOTES="quoteees" | ||
replace resources/3-proper 3-result | ||
result=$(comm -23 ${DIR}/3-result ${DIR}/resources/3-proper-expected | wc -l) | ||
[ "$result" -eq 0 ] | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
this file has nothing for replacing | ||
even on the second line | ||
seriously, nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
this file has ${SOMETHING} for replacing | ||
even on the ${SECOND} line in the ${END} | ||
${OR} in the beginning of the third | ||
indeed ${SOMETHING} is replaced multiple times | ||
but having more ${{QUOTES}} does not work | ||
or having ${something} in different case too |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
this file has SoMeThiNG for replacing | ||
even on the SeconD line in the EnD | ||
Or in the beginning of the third | ||
indeed SoMeThiNG is replaced multiple times | ||
but having more ${{QUOTES}} does not work | ||
or having ${something} in different case too |
Oops, something went wrong.