forked from panubo/docker-apache-mvh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.sh
executable file
·125 lines (102 loc) · 4.1 KB
/
entry.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
#!/usr/bin/env bash
set -e
[ "$DEBUG" == 'true' ] && set -x
# Apache MPM Tuning
export MPM_START=${MPM_START:-5}
export MPM_MINSPARE=${MPM_MINSPARE:-5}
export MPM_MAXSPARE=${MPM_MAXSPARE:-10}
export MPM_MAXWORKERS=${MPM_MAXWORKERS:-150}
export MPM_MAXCONNECTIONS=${MPM_CONNECTIONS:-0}
# SMTP
export SMTP_HOST=${SMTP_PORT_25_TCP_ADDR-${SMTP_HOST:-'localhost'}}
export SMTP_PORT=${SMTP_PORT_25_TCP_PORT-${SMTP_PORT:-'25'}}
cat << EOF > /etc/msmtprc
account default
auto_from on
# The SMTP smarthost.
host ${SMTP_HOST}
port ${SMTP_PORT}
EOF
# Mountfile Support
# LICENSE: MIT License, Copyright (c) 2017-2019 Volt Grid Pty Ltd t/a Panubo
# See: https://github.com/panubo/bash-container
# run_mountfile [FILENAME] [DATADIR]
# run_mountfile Mountfile /srv/remote
run_mountfile() {
# Mount all dirs specified in Mountfile
local mountfile="${1:-"Mountfile"}"
local data_dir="${2:-"/srv/remote"}"
local mount_uid="${MOUNTFILE_MOUNT_UID:-"48"}"
local mount_gid="${MOUNTFILE_MOUNT_GID:-"48"}"
local safety_checks="${MOUNTFILE_SAFETY_CHECKS:-"true"}"
local source_dir=""
local target_dir=""
local working_dir=""
if [[ $EUID -ne 0 ]]; then echo "Must be run as root"; return 1; fi
if [[ ! -e "${mountfile}" ]]; then echo "Mountfile not found"; return 0; fi
if [[ ! -e "${data_dir}" ]]; then echo "Data dir not found"; return 1; fi
# normalise path to Mountfile
mountfile="$(realpath "${mountfile}")"
# normalise data dir
data_dir="$(realpath "${data_dir}")"
# calculate working_dir from Mountfile location
working_dir="$(dirname "${mountfile}")"
# make sure we are operating in the same dir that holds the Mountfile
pushd "${working_dir}" 1> /dev/null || return 1
while read -r line || [[ -n "${line}" ]]; do
if [[ -z "${line}" ]] || [[ "${line}" == \#* ]]; then continue; fi
[[ "${line}" =~ ([[:alnum:]/\.-]*)[[:space:]]?:[[:space:]]?(.*) ]]
s="${BASH_REMATCH[1]}"
t="${BASH_REMATCH[2]}"
# normalise
# remove link if it exists otherwise readlink will follow link to remote
[[ -L "${working_dir}/${t}" ]] && rm -f "${working_dir}/${t}"
target_dir="$(cd "${working_dir}" && readlink -f -m "${t}")"
# handle ephemeral
if [[ "${s}" == "ephemeral" ]]; then
# create ephemeral
source_dir="$(mktemp -d)"
else
# normalise
source_dir="$(cd "${data_dir}" && readlink -f -m "${s}")"
# safety checks
if [[ "${safety_checks}" == "true" ]]; then
[[ ! "${target_dir}" =~ ${working_dir} ]] && { echo "Error: Target outside working directory!" && return 129; }
[[ ! "${source_dir}" =~ ${data_dir} ]] && { echo "Error: Source not within data directory!" && return 129; }
fi
fi
# make remote source dir if not exist
[[ ! -e "${source_dir}" ]] && mkdir -p "${source_dir}"
# create mount target (including parents) if required
mkdir -p "${target_dir}"
# Copy mount to remote, if remote is empty, and target_dir has files
if [[ "$(ls -A "${target_dir}")" ]] && [[ ! "$(ls -A "${source_dir}")" ]]; then
echo "Copying template content ${target_dir} => ${source_dir}"
# gnu cp does not respect trainling / with -a, so we remove the dir
rmdir "${source_dir}"
cp -a "${target_dir}/" "${source_dir}"
# Fix permissions recursively in remote
chown -R ${mount_uid}:${mount_gid} "${source_dir}"
else
# Set permission on remote
chown ${mount_uid}:${mount_gid} "${source_dir}"
fi
(>&1 echo "Mounting remote path ${source_dir} => ${target_dir}")
# Delete target_dir if exists. Create symlink to source_dir
[[ -e "${target_dir}" ]] && rm -rf "${target_dir}"
ln -snf "${source_dir}" "${target_dir}"
done < "${mountfile}"
}
if [ "${PROCESS_MOUNTFILES}" == "true" ]; then
: "${REMOTE_BASE:=/srv/remote}"
: "${MOUNTFILE_MOUNT_UID:=33}"
: "${MOUNTFILE_MOUNT_GID:=33}"
MOUNTFILES=$(find /srv/www -maxdepth 2 -mindepth 2 -type f -name 'Mountfile')
for MOUNTFILE in ${MOUNTFILES}; do
SITE="$(basename $(dirname $MOUNTFILE))"
echo ">> Processing $SITE"
mkdir -p "${REMOTE_BASE}/${SITE}"
run_mountfile ${MOUNTFILE} "${REMOTE_BASE}/${SITE}"
done
fi
exec $@