-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
executable file
·133 lines (108 loc) · 3.26 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
executable file
·133 lines (108 loc) · 3.26 KB
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
#!/usr/local/bin/dumb-init bash
set -euo pipefail
: ${BRANCH:=master} # Branch to work with when syncing
: ${CHOWN_UID:=} # ID of the user to chown the repo files to
: ${CHOWN_GID:=${CHOWN_UID}} # ID of the group to chown the repo files to
: ${INTERVAL:=300} # How often to sync with remote
: ${LOCAL_DIR:=/data} # Where to find the data to backup
: ${NETRC_CONTENT:=} # Content to put into .netrc file, base64 encoded
: ${PING_DOWN:=} # Send a ping (HTTP GET) to this URL when an exit-error ocurred
: ${PING_MAX_TIME:=5} # Time in seconds to timeout the ping request
: ${PING_UP:=} # Send a ping (HTTP GET) to this URL when backup finished successfully
: ${REMOTE:=} # Remote to sync the branch to
function error() {
if [[ -n $PING_DOWN ]]; then
curl -sS -m ${PING_MAX_TIME} -o /dev/null "${PING_DOWN}"
fi
log E "$@"
}
function fatal() {
log F "$@"
exit 1
}
function info() {
log I "$@"
}
function log() {
local level=$1
shift
echo "[$(date +%H:%M:%S)][$level] $@" >&2
}
function main() {
pushd "${LOCAL_DIR}"
[[ -z $NETRC_CONTENT ]] || {
info "Creating .netrc from ENV..."
echo "${NETRC_CONTENT}" | base64 -d >~/.netrc
chmod 0600 ~/.netrc
}
info "Marking local dir save..."
git config --global --add safe.directory "$(pwd)"
case "${1:-help}" in
sync)
run_sync || fatal "Backup failed."
;;
restore)
run_restore || failed "Restore failed."
;;
*)
usage
fatal "Action ${1:-help} called"
;;
esac
}
function run_restore() {
if [[ -d .git ]]; then
info "Found .git directory, skipping restore."
return 0
fi
info "Initializing empty git repository..."
git init -b ${BRANCH} || fatal "Initializing repo failed (exit $?)"
info "Setting up remote..."
git remote add origin "${REMOTE}" || fatal "Adding remote failed (exit $?)"
info "Fetching remote to reset..."
git fetch origin ${BRANCH} || fatal "Fetch failed (exit $?)"
info "Resetting to remote state..."
git reset --hard FETCH_HEAD || fatal "Resetting onto FETCH_HEAD failed (exit $?)"
git branch -u origin/"${BRANCH}" "${BRANCH}" || fatal "Configuring upstream branch failed (exit $?)"
[[ -z $CHOWN_UID ]] || {
info "Chown-ing files to ${CHOWN_UID}:${CHOWN_GID}..."
chown -R ${CHOWN_UID}:${CHOWN_GID} . || fatal "Chown failed (exit $?)"
}
}
function run_sync() {
while true; do
next_run=$((INTERVAL - $(date +%s) % INTERVAL))
info "Sleeping ${next_run}s to next sync..."
sleep ${next_run}
info "Fetching remote to rebase..."
git fetch ${REMOTE} ${BRANCH} || {
error "Fetch failed (exit $?)"
continue
}
info "Rebasing upon remote..."
git rebase FETCH_HEAD || {
error "Rebase failed (exit $?)"
continue
}
[[ -z $CHOWN_UID ]] || chown -R ${CHOWN_UID}:${CHOWN_GID} .
info "Pushing to remote..."
git push ${REMOTE} ${BRANCH} || {
error "Push failed (exit $?)"
continue
}
if [[ -n $PING_UP ]]; then
curl -sS -m ${PING_MAX_TIME} -o /dev/null "${PING_UP}"
fi
done
}
function usage() {
cat >&2 <<EOF
Usage:
docker run --rm -ti \
-v /mydata:/data:ro \
-w /data \
-e REMOTE_HOST=user@host \
pvc-git <sync|restore>
EOF
}
main "$@"