outliner/entrypoint.sh

66 lines
1.4 KiB
Bash
Raw Normal View History

#!/bin/sh
set -eu
if [ $# -eq 0 ]; then
2019-08-14 09:39:11 +00:00
echo "Please run with outliner [export|import|sync] arguments"
exit
fi
2019-08-12 13:57:32 +00:00
setup_git() {
2019-08-13 04:59:25 +00:00
if [ -f "$HOME/.ssh/id_rsa" ]; then
2019-08-14 09:39:11 +00:00
# This is required because Kubernetes secret mounts can't
# have file permissions set
chmod 0400 "$HOME/.ssh/id_rsa"
2019-08-12 13:57:32 +00:00
2019-08-13 04:59:25 +00:00
if [ ! -d "$HOME/.ssh/id_rsa.pub" ]; then
ssh-keygen -y -f "$HOME/.ssh/id_rsa" > "$HOME/.ssh/id_rsa.pub"
2019-08-12 13:57:32 +00:00
fi
echo "[+] Using SSH key for git pushes"
else
echo "[E] Git credentials not available, quitting"
exit 1
2019-08-12 13:57:32 +00:00
fi
eval $(ssh-agent)
ssh-add "$HOME/.ssh/id_rsa"
2019-08-12 13:57:32 +00:00
}
update_git_config() {
EMAIL=${GIT_EMAIL:-outliner@example.invalid}
git config --global user.email "$EMAIL"
git config --global user.name "Outliner Backup"
git remote add origin "$GIT_REMOTE_URL"
2019-08-12 13:57:32 +00:00
}
case $1 in
export)
shift
2019-08-12 13:57:32 +00:00
bundle exec outliner-export "$@"
;;
import)
shift
2019-08-12 13:57:32 +00:00
bundle exec outliner-import "$@"
;;
sync)
tmp_dir=$(mktemp -d)
if [ -z "$GIT_REMOTE_URL" ]; then
echo "[E] GIT_REMOTE_URL not set"
exit 1
else
setup_git
bundle exec outliner-export "$tmp_dir"
cd "$tmp_dir"
git init
update_git_config
git add .
2019-08-14 09:39:11 +00:00
git commit --message "Backup: $(date)" > /dev/null
BRANCH=${GIT_BRANCH:-master}
git checkout -b "$BRANCH"
2019-08-14 09:39:11 +00:00
git status
git push origin --force "HEAD:$BRANCH"
fi
;;
*)
echo "Invalid command, please check README"
;;
esac