-
Notifications
You must be signed in to change notification settings - Fork 0
/
oc
executable file
·70 lines (56 loc) · 1.91 KB
/
oc
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
#!/bin/bash
LOGFILE="/var/log/oc.log"
OC_CMD="/opt/oc"
USER="$(whoami)"
DATE="$(date "+%d-%b-%Y %T")"
SESSION="$(shuf -i 1-100000 -n 1)"
ARGS="${@}"
test -x "${OC_CMD}" || \
echo "ERROR: '${OC_CMD}' not found or not executable"
# Log command invoked by the user
echo "${DATE} ${USER}:${SESSION} ${0} ${ARGS}" >> ${LOGFILE}
# Find the current oc user
OC_USER=$(${OC_CMD} whoami 2>&1)
RET="$?"
if [ "${RET}" != "0" ]; then
echo "${OC_USER}"
exit "${RET}"
fi
# Find the current oc server
OC_SERVER=$(${OC_CMD} whoami --show-server 2>&1)
RET="$?"
if [ "${RET}" != "0" ]; then
echo "${OC_SERVER}"
exit "${RET}"
fi
# Log command invoked by the user
echo "${DATE} ${USER}:${SESSION} OC_USER=${OC_USER} OC_SERVER=${OC_SERVER})" >> ${LOGFILE}
# Check if delete operation is present
echo " ${ARGS} " | grep -q " delete " &>> /dev/null
DEL="$?"
if [ "${DEL}" == "0" ]; then
# Dry run the delete operation
DRY_DEL_OUT=$(${OC_CMD} ${ARGS} --dry-run=client 2>&1)
DRY_DEL_RET="$?"
if [ "${DRY_DEL_RET}" == "0" ]; then
NUM_DEL=$(echo "${DRY_DEL_OUT}" | grep "deleted" | wc -l)
echo "============="
echo "== DRY RUN =="
echo "============="
echo "${DRY_DEL_OUT}"
echo "============================================="
echo "${NUM_DEL} objects will be deleted!"
echo -n "Press [Enter] to continue or [Ctrl+C] to exit "
read confirm
echo "${DATE} ${USER}:${SESSION} ${0} ${ARGS} Confirmed deletion of ${NUM_DEL} objects: ${DRY_DEL_OUT}" >> ${LOGFILE}
else
echo "ERROR: '${OC_CMD} ${ARGS} --dry-run=client' failed (${DRY_DEL_RET}) with the following error:"
echo "${DRY_DEL_OUT}"
exit ${DRY_DEL_RET}
fi
fi
# No delete operation detected, execute the oc command as it is
${OC_CMD} ${ARGS}
OC_CMD_RET="$?"
echo "${DATE} ${USER}:${SESSION} ${OC_CMD} ${ARGS} Returned: ${OC_CMD_RET}" >> ${LOGFILE}
exit ${OC_CMD_RET}