init
This commit is contained in:
commit
43c6c25657
|
|
@ -0,0 +1,31 @@
|
|||
#!/usr/bin/env -S docker build . --tag=jcosmao/flatnotes_gitinotify --network=host --file
|
||||
|
||||
# docker push jcosmao/flatnotes_gitinotify
|
||||
|
||||
FROM alpine:latest
|
||||
|
||||
ENV GIT_SSH_URL=""
|
||||
ENV GIT_BRANCH=""
|
||||
ENV GIT_SSH_KEY_FILE=""
|
||||
ENV FLATNOTES_ROOT=""
|
||||
|
||||
ARG UNAME=user
|
||||
ARG UID=1000
|
||||
ARG GID=1000
|
||||
|
||||
RUN apk add --no-cache git inotify-tools bash openssh-client
|
||||
|
||||
COPY flatnotes_gitinotify.sh /app/flatnotes_gitinotify.sh
|
||||
RUN chmod +x /app/flatnotes_gitinotify.sh
|
||||
|
||||
|
||||
RUN addgroup --system --gid ${GID} ${UNAME}
|
||||
RUN adduser --system --disabled-password --home /home/${UNAME} \
|
||||
--uid ${UID} --ingroup ${UNAME} ${UNAME}
|
||||
|
||||
USER $UNAME
|
||||
|
||||
ENTRYPOINT ["/app/flatnotes_gitinotify.sh", "GIT_SSH_URL=${GIT_SSH_URL}", \
|
||||
"GIT_BRANCH=${GIT_BRANCH}", \
|
||||
"GIT_SSH_KEY_FILE=${GIT_SSH_KEY_FILE}", \
|
||||
"FLATNOTES_ROOT=${FLATNOTES_ROOT}"]
|
||||
|
|
@ -0,0 +1,117 @@
|
|||
#!/bin/bash
|
||||
|
||||
for kv in $*; do
|
||||
[[ $kv =~ ^.*=.*$ ]] && eval $kv
|
||||
done
|
||||
|
||||
[[ -z $GIT_SSH_URL ]] && echo "missing env GIT_SSH_URL" && exit 1
|
||||
[[ -z $GIT_BRANCH ]] && echo "missing env GIT_BRANCH" && exit 1
|
||||
[[ -z $GIT_SSH_KEY_FILE ]] && echo "missing env GIT_SSH_KEY_FILE" && exit 1
|
||||
[[ -z $FLATNOTES_ROOT ]] && echo "missing env FLATNOTES_ROOT" && exit 1
|
||||
[[ ! -e $FLATNOTES_ROOT ]] && echo "flatnotes directory does not exist" && exit 1
|
||||
[[ ! -e $GIT_SSH_KEY_FILE ]] && echo "ssh key file not found" && exit 1
|
||||
|
||||
|
||||
function url_parser {
|
||||
url=$1
|
||||
|
||||
protocol=$(echo "$1" | grep "://" | sed -e's,^\(.*://\).*,\1,g')
|
||||
url_no_protocol=$(echo "${1/$protocol/}")
|
||||
protocol=$(echo "$protocol" | tr '[:upper:]' '[:lower:]' | cut -d: -f1)
|
||||
userpass=$(echo "$url_no_protocol" | grep "@" | cut -d"/" -f1 | rev | cut -d"@" -f2- | rev)
|
||||
pass=$(echo "$userpass" | grep ":" | cut -d":" -f2)
|
||||
if [ -n "$pass" ]; then
|
||||
user=$(echo "$userpass" | grep ":" | cut -d":" -f1)
|
||||
else
|
||||
user="$userpass"
|
||||
fi
|
||||
hostport=$(echo "${url_no_protocol/$userpass@/}" | cut -d"/" -f1)
|
||||
host=$(echo "$hostport" | cut -d":" -f1)
|
||||
port=$(echo "$hostport" | grep ":" | cut -d":" -f2)
|
||||
path=$(echo "$url_no_protocol" | grep "/" | cut -d"/" -f2-)
|
||||
|
||||
export url_parser_proto=$protocol
|
||||
export url_parser_user=$user
|
||||
export url_parser_host=$host
|
||||
export url_parser_port=$port
|
||||
}
|
||||
|
||||
function ssh_init
|
||||
{
|
||||
mkdir -p $HOME/.ssh && chmod 700 $HOME/.ssh
|
||||
touch $HOME/.ssh/config
|
||||
chmod 600 $HOME/.ssh/config
|
||||
cp $GIT_SSH_KEY_FILE $HOME/.ssh/privkey
|
||||
chmod 400 $HOME/.ssh/privkey
|
||||
|
||||
echo "
|
||||
Host $url_parser_host
|
||||
IdentityFile $HOME/.ssh/privkey
|
||||
User $url_parser_user
|
||||
Port $url_parser_port
|
||||
UserKnownHostsFile /dev/null
|
||||
StrictHostKeyChecking no
|
||||
" > $HOME/.ssh/config
|
||||
}
|
||||
|
||||
function git_init
|
||||
{
|
||||
git config --global user.name "Flatnotes"
|
||||
git config --global user.email "flatnotes@autocommit"
|
||||
git config --global --add safe.directory $FLATNOTES_ROOT
|
||||
git config --global pull.rebase true
|
||||
|
||||
cd $FLATNOTES_ROOT
|
||||
if [[ ! -d ${FLATNOTES_ROOT}/.git ]]; then
|
||||
echo ".flatnotes" > .gitignore
|
||||
git init
|
||||
git branch -m $GIT_BRANCH
|
||||
git remote add origin $GIT_SSH_URL
|
||||
git fetch && git reset --hard origin/$GIT_BRANCH
|
||||
else
|
||||
git remote set-url origin $GIT_SSH_URL
|
||||
git branch -m $GIT_BRANCH
|
||||
git branch --set-upstream-to=origin/$GIT_BRANCH $GIT_BRANCH
|
||||
git pull
|
||||
fi
|
||||
}
|
||||
|
||||
function start_inotify
|
||||
{
|
||||
inotifywait -q -m $FLATNOTES_ROOT -e access -e modify -e create -e delete | \
|
||||
while read -r directory action file; do
|
||||
if [[ "$file" =~ .*md$ || "$directory" == "${FLATNOTES_ROOT}/attachments" ]]; then
|
||||
echo "[inotifywait] event catched action=$action on directory=$directory file=$file"
|
||||
autocommit_on_event "$directory" "$action" "$file"
|
||||
sleep 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
function autocommit_on_event
|
||||
{
|
||||
directory="$1"
|
||||
action="$2"
|
||||
file="$3"
|
||||
|
||||
if [[ $action == ACCESS ]]; then
|
||||
# update local repo in case some file has been updated by other method than flatnotes
|
||||
local_change=$(git status --short | wc -l)
|
||||
[[ $local_change > 0 ]] && git stash save -a local_changes
|
||||
git -C $FLATNOTES_ROOT pull -X ours
|
||||
[[ $local_change > 0 ]] && git stash pop && git stash clear
|
||||
else
|
||||
git -C $FLATNOTES_ROOT add .
|
||||
git -C $FLATNOTES_ROOT commit -am "Autocommit action=$action on file=$file detected"
|
||||
git -C $FLATNOTES_ROOT push
|
||||
fi
|
||||
}
|
||||
|
||||
url_parser $GIT_SSH_URL
|
||||
if [[ $url_parser_proto != ssh ]]; then
|
||||
echo "Need a git ssh url"
|
||||
exit 1
|
||||
fi
|
||||
ssh_init
|
||||
git_init
|
||||
start_inotify
|
||||
Loading…
Reference in New Issue