diff --git a/roles/kubernetes/preinstall/defaults/main.yml b/roles/kubernetes/preinstall/defaults/main.yml index 3e503a246b9fee2cb280d4a2178a93df54f218d4..be0857ce19b288d79c3a97f30793ab0c024b7159 100644 --- a/roles/kubernetes/preinstall/defaults/main.yml +++ b/roles/kubernetes/preinstall/defaults/main.yml @@ -1,4 +1,6 @@ --- +run_gitinfos: false + common_required_pkgs: - python-httplib2 - openssl diff --git a/roles/kubernetes/preinstall/gen-gitinfos.sh b/roles/kubernetes/preinstall/gen-gitinfos.sh new file mode 100755 index 0000000000000000000000000000000000000000..34b977f7e9a1ed369bd2a530aec11ddcc57b31ed --- /dev/null +++ b/roles/kubernetes/preinstall/gen-gitinfos.sh @@ -0,0 +1,73 @@ +#!/bin/sh +set -e + +# Text color variables +txtbld=$(tput bold) # Bold +bldred=${txtbld}$(tput setaf 1) # red +bldgre=${txtbld}$(tput setaf 2) # green +bldylw=${txtbld}$(tput setaf 3) # yellow +txtrst=$(tput sgr0) # Reset +err=${bldred}ERROR${txtrst} +info=${bldgre}INFO${txtrst} +warn=${bldylw}WARNING${txtrst} + +usage() +{ + cat << EOF +Generates a file which contains useful git informations + +Usage : $(basename $0) [global|diff] + ex : + Generate git information + $(basename $0) global + Generate diff from latest tag + $(basename $0) diff +EOF +} + +if [ $# != 1 ]; then + printf "\n$err : Needs 1 argument\n" + usage + exit 2 +fi; + +current_commit=$(git rev-parse HEAD) +latest_tag=$(git describe --abbrev=0 --tags) +latest_tag_commit=$(git show-ref -s ${latest_tag}) +tags_list=$(git tag --points-at "${latest_tag}") + +case ${1} in + "global") +cat<<EOF +deployment date="$(date '+%d-%m-%Y %Hh%M')" +deployment_timestamp=$(date '+%s') +user="$USER" +current commit (HEAD)="${current_commit}" +current_commit_timestamp=$(git log -1 --pretty=format:%ct) +latest tag(s) (current branch)="${tags_list}" +latest tag commit="${latest_tag_commit}" +current branch="$(git rev-parse --abbrev-ref HEAD)" +branches list="$(git describe --contains --all HEAD)" +git root directory="$(git rev-parse --show-toplevel)" +EOF + if ! git diff-index --quiet HEAD --; then + printf "unstaged changes=\"/etc/.git-ansible.diff\"" + fi + + if [ ${current_commit} == ${latest_tag_commit} ]; then + printf "\ncurrent_commit_tag=\"${latest_tag}\"" + else + printf "\nlast tag was "$(git describe --tags | awk -F- '{print $2}')" commits ago =\"" + printf "$(git log --pretty=format:" %h - %s" ${latest_tag}..HEAD)\"" + fi + ;; + + "diff") + git diff + ;; + *) + usage + printf "$err: Unknown argument ${1}" + exit 1; + ;; +esac diff --git a/roles/kubernetes/preinstall/tasks/gitinfos.yml b/roles/kubernetes/preinstall/tasks/gitinfos.yml new file mode 100644 index 0000000000000000000000000000000000000000..60a12f8fa06a2690020125337831593fff668dbf --- /dev/null +++ b/roles/kubernetes/preinstall/tasks/gitinfos.yml @@ -0,0 +1,25 @@ +--- +# Deploy git infos +# ---------------- + +- name: 'GIT | generate git informations' + local_action: command {{ role_path }}/gen-gitinfos.sh global + register: gitinfo + always_run: yes + +- name: 'GIT | copy ansible information' + template: + src: ansible_git.j2 + dest: /etc/.ansible.ini + backup: yes + +- name: 'GIT | generate diff file' + local_action: command {{ role_path }}/gen-gitinfos.sh diff + register: gitdiff + always_run: yes + +- name: 'GIT | copy git diff file' + copy: + content: "{{ gitdiff.stdout }}" + dest: /etc/.git-ansible.diff + backup: yes diff --git a/roles/kubernetes/preinstall/tasks/main.yml b/roles/kubernetes/preinstall/tasks/main.yml index b0f952d6f6063d29899d7eac24361b2109ef6bcf..72efa116af5598777713cd0db2b020efc3b81f79 100644 --- a/roles/kubernetes/preinstall/tasks/main.yml +++ b/roles/kubernetes/preinstall/tasks/main.yml @@ -1,4 +1,7 @@ --- +- include: gitinfos.yml + when: run_gitinfos + - name: gather os specific variables include_vars: "{{ item }}" with_first_found: diff --git a/roles/kubernetes/preinstall/templates/ansible_git.j2 b/roles/kubernetes/preinstall/templates/ansible_git.j2 new file mode 100644 index 0000000000000000000000000000000000000000..abf92a74ad8c19a6a2ce7cd92eff107efa0fd8c9 --- /dev/null +++ b/roles/kubernetes/preinstall/templates/ansible_git.j2 @@ -0,0 +1,3 @@ +; This file contains the information which identifies the deployment state relative to the git repo +[default] +{{ gitinfo.stdout }}