This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apexec.sh
executable file
·117 lines (95 loc) · 3.04 KB
/
apexec.sh
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
#!/usr/bin/env bash
[ ! -x "$(command -v pwgen)" ] && \
echo "pwgen not found!" && \
exit 1
function show_help {
echo "
Usage: apexec.sh PLAYBOOK_URL
PLAYBOOK_FILE
SSH_USER
SLACK_TOKEN
SLACK_CHANNEL
VAULT_PASSWORD_FILE (optional)
ANSIBLE_EXTRA_ARGS (optional)
[-h|--help]
Ansible playbook execution script for https://github.com/adnanh/webhook.
All arguments are positional!"
}
function init {
[ ! -d /tmp/apexec ] && mkdir -p /tmp/apexec
JOB_ID=$(pwgen 6 1)
WORK_DIR=/tmp/apexec/${JOB_ID}
PLAYBOOK_URL=${1}
PLAYBOOK_FILE=${2}
SSH_USER=${3}
SLACK_TOKEN=${4}
SLACK_CHANNEL=${5}
VAULT_PASSWORD_FILE=${6}
ANSIBLE_EXTRA_ARGS=${7}
[ -z "${PLAYBOOK_URL}" ] && \
echo "argument 'PLAYBOOK_URL' not set!" && \
exit 1
[ -z "${PLAYBOOK_FILE}" ] && \
echo "argument 'PLAYBOOK_FILE' not set!" && \
exit 1
[ -z "${SSH_USER}" ] && \
echo "argument 'SSH_USER' not set!" && \
exit 1
[ -n "${VAULT_PASSWORD_FILE}" ] && \
VAULT_PASSWORD_FILE="--vault-password-file=${VAULT_PASSWORD_FILE}"
PLAYBOOK_NAME=${PLAYBOOK_URL##*/}
PLAYBOOK_NAME=${PLAYBOOK_NAME%.git}
LOG_FILE="/tmp/${PLAYBOOK_NAME}-${JOB_ID}.log"
}
function pull_playbook {
git clone --recursive "${PLAYBOOK_URL}" "${WORK_DIR}"
cd "${WORK_DIR}" || return
}
function install_requirements {
[ -f requirements.yml ] && ansible-galaxy install -r requirements.yml --force
}
function execute_ansible_playbook {
ansible-playbook "${PLAYBOOK_FILE}" --diff "${ANSIBLE_EXTRA_ARGS}" --extra-vars=ansible_user="${SSH_USER}" "${VAULT_PASSWORD_FILE}" &> "${LOG_FILE}"
cat "${LOG_FILE}"
}
function send_notification {
[ -z "${SLACK_TOKEN}" ] && \
echo "argument 'SLACK_TOKEN' not set" && \
return
[ -z "${SLACK_CHANNEL}" ] && \
echo "argument 'SLACK_TOKEN' not set" && \
return
[ ! -f "${LOG_FILE}" ] && \
echo "cannot send Slack notification. File '${LOG_FILE}' not found!" && \
return
grep --quiet "failed=0" "${LOG_FILE}" || ERROR_MSG=" failed"
grep --quiet "unreachable=0" "${LOG_FILE}" || ERROR_MSG=" failed"
response=$(curl \
--silent \
--show-error \
--no-progress-meter \
--form file=@"${LOG_FILE}" \
--form "initial_comment=Ansible Playbook execution${ERROR_MSG}: ${PLAYBOOK_NAME}" \
--form "channels=#${SLACK_CHANNEL}" \
--header "Authorization: Bearer ${SLACK_TOKEN}" \
https://slack.com/api/files.upload)
[ $(jq .ok? <<< "$response") == true ] && \
echo "Slack notification successfully send" || \
echo "Error sending Slack notification. $(jq -r <<< "$response")"
}
function cleanup {
rm -rf "${WORK_DIR}"
}
((!$#)) && \
echo "No arguments supplied!" && \
show_help && \
exit 1
[[ " $* " =~ " -h " ]] || [[ " $* " =~ " --help " ]] && \
show_help && \
exit 0
init "${@}"
pull_playbook
install_requirements
execute_ansible_playbook
send_notification
cleanup