etc-git: evocommit has an Ansible mode to report changes

This commit is contained in:
Jérémy Lecour 2021-10-12 11:15:29 +02:00 committed by Jérémy Lecour
parent 9aff38c0a7
commit 520cba9c5b
2 changed files with 50 additions and 6 deletions

View file

@ -82,8 +82,14 @@ remount_repository_readonly() {
is_dry_run() { is_dry_run() {
test "${DRY_RUN}" = "1" test "${DRY_RUN}" = "1"
} }
is_verbose() {
test "${VERBOSE}" = "1"
}
is_ansible() {
test "${ANSIBLE}" = "1"
}
main() { main() {
rc=0
lock="${GIT_DIR}/index.lock" lock="${GIT_DIR}/index.lock"
if [ -f "${lock}" ]; then if [ -f "${lock}" ]; then
limit=$(date +"%s" -d "now - 1 hour") limit=$(date +"%s" -d "now - 1 hour")
@ -108,18 +114,45 @@ main() {
author=$(logname) author=$(logname)
email=$(git config --get user.email) email=$(git config --get user.email)
email=${email:-"${author}@evolix.net"} email=${email:-"${author}@evolix.net"}
# commit changes # commit changes
${GIT_BIN} add --all git_add_result=$(${GIT_BIN} add --all)
${GIT_BIN} commit --message "${MESSAGE}" --author "${author} <${email}>" --quiet git_add_rc=$?
if is_ansible; then
if [ ${git_add_rc} -ne 0 ]; then
printf "FAILED: %s\n%s" "can't add changes in ${REPOSITORY}" "${git_add_result}"
rc=1
fi
fi
git_commit_result=$(${GIT_BIN} commit --message "${MESSAGE}" --author "${author} <${email}>")
git_commit_rc=$?
if is_ansible; then
if [ ${git_commit_rc} -eq 0 ]; then
printf "CHANGED: %s\n" "commit done in ${REPOSITORY} with \`${MESSAGE}'"
else
printf "FAILED: %s\n%s" "can't commit in ${REPOSITORY} \`${MESSAGE}'" "${git_commit_result}"
rc=1
fi
fi
# remount mount point read-only if it was before # remount mount point read-only if it was before
if [ ${readonly_orig} -eq 1 ]; then if [ ${readonly_orig} -eq 1 ]; then
remount_repository_readonly "${REPOSITORY}" remount_repository_readonly "${REPOSITORY}"
fi fi
fi fi
else
if is_ansible; then
printf "INFO: %s\n" "no commit in ${REPOSITORY}'"
fi
fi fi
unset GIT_DIR unset GIT_DIR
unset GIT_WORK_TREE unset GIT_WORK_TREE
exit ${rc}
} }
# Parse options # Parse options
# based on https://gist.github.com/deshion/10d3cb5f88a21671e17a # based on https://gist.github.com/deshion/10d3cb5f88a21671e17a
@ -179,6 +212,10 @@ while :; do
# print verbose information # print verbose information
VERBOSE=1 VERBOSE=1
;; ;;
--ansible)
# print information for Ansible
ANSIBLE=1
;;
--) --)
# End of all options. # End of all options.
shift shift
@ -209,6 +246,7 @@ if [ -z "${REPOSITORY}" ]; then
fi fi
DRY_RUN=${DRY_RUN:-0} DRY_RUN=${DRY_RUN:-0}
VERBOSE=${VERBOSE:-0} VERBOSE=${VERBOSE:-0}
ANSIBLE=${ANSIBLE:-0}
GIT_BIN=$(command -v git) GIT_BIN=$(command -v git)
readonly GIT_BIN readonly GIT_BIN

View file

@ -7,8 +7,10 @@
register: _etc_git register: _etc_git
- name: "evocommit /etc" - name: "evocommit /etc"
command: "/usr/local/bin/evocommit --repository /etc --message \"{{ commit_message | mandatory }}\"" command: "/usr/local/bin/evocommit --ansible --repository /etc --message \"{{ commit_message | mandatory }}\""
changed_when: "'CHANGED:' in _etc_git_commit.stdout"
ignore_errors: yes ignore_errors: yes
register: _etc_git_commit
when: when:
- _etc_git.stat.exists - _etc_git.stat.exists
- _etc_git.stat.isdir - _etc_git.stat.isdir
@ -20,8 +22,10 @@
register: _etc_bind_git register: _etc_bind_git
- name: "evocommit /etc/bind" - name: "evocommit /etc/bind"
command: "/usr/local/bin/evocommit --repository /etc/bind --message \"{{ commit_message | mandatory }}\"" command: "/usr/local/bin/evocommit --ansible --repository /etc/bind --message \"{{ commit_message | mandatory }}\""
changed_when: "'CHANGED:' in _etc_bind_git_commit.stdout"
ignore_errors: yes ignore_errors: yes
register: _etc_bind_git_commit
when: when:
- _etc_bind_git.stat.exists - _etc_bind_git.stat.exists
- _etc_bind_git.stat.isdir - _etc_bind_git.stat.isdir
@ -33,8 +37,10 @@
register: _usr_share_scripts_git register: _usr_share_scripts_git
- name: "evocommit /usr/share/scripts" - name: "evocommit /usr/share/scripts"
command: "/usr/local/bin/evocommit --repository /usr/share/scripts --message \"{{ commit_message | mandatory }}\"" command: "/usr/local/bin/evocommit --ansible --repository /usr/share/scripts --message \"{{ commit_message | mandatory }}\""
changed_when: "'CHANGED:' in _usr_share_scripts_git_commit.stdout"
ignore_errors: yes ignore_errors: yes
register: _usr_share_scripts_git_commit
when: when:
- _usr_share_scripts_git.stat.exists - _usr_share_scripts_git.stat.exists
- _usr_share_scripts_git.stat.isdir - _usr_share_scripts_git.stat.isdir