forked from Cross-PLN-Technical-Working-Group/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-do-staging
executable file
·153 lines (115 loc) · 4.75 KB
/
adpn-do-staging
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
#
# adpn-do-staging: script to handle adpn staging commands
#
# @version 2021.0826
SCRIPTPATH="$(which "$0")"
SCRIPTPATH="$( readlink --canonicalize "${SCRIPTPATH}" )"
SCRIPTDIR="$(dirname "${SCRIPTPATH}")"
SCRIPT="$(basename "${SCRIPTPATH}")"
source "${SCRIPTDIR}/adpn-define-aliases"
__USAGE__="Usage: ${SCRIPT} [--version] [--help] <CMD> [<ARGS>]"
__HELP__="[${SCRIPT}] Try '${SCRIPT} help' for more information."
__DOC__="""${__USAGE__}
--version Display the version of the script
--help Display these usage notes
The most commonly used ${SCRIPT} commands are:
free Check free space available on the staging server
Exit codes:
0 = success (successful operation and expected result)
1-254 = error in executing command
255 = command not supported
"""
##########################################################################################
### COMMAND LINE: loop through switches ##################################################
##########################################################################################
declare -a _ARGV ; _ARGV=("$0")
declare -a _CMDLINE ; _CMDLINE=("$0")
declare -A _PARAM=()
shopt -s lastpipe
declare -a SWITCHFILES ; SWITCHFILES=()
if [[ -r "${CONFFILE}" ]] ; then
SWITCHFILES+=(${CONFFILE})
fi
CMDLINETXT=$(mktemp)
until [[ "$#" -lt 1 ]] ; do
_CMDLINE+=("$1")
printf "%s\n" "$1" >> "${CMDLINETXT}"
shift
done
SWITCHFILES+=(${CMDLINETXT})
adpn_command_line "${SWITCHFILES[@]}"
rm "${CMDLINETXT}"
##########################################################################################
### SCRIPT: DETERMINE COMMAND, THEN EXECUTE PIPELINE ####################################
##########################################################################################
EXITCODE=0
CMD="${_ARGV[1]}"
adpn_set_display_settings # V, VV, Q, QQ, DBG, DBGLEVEL, DDBG, SCRIPT_CMD_NAME / @see adpn-define-aliases
if [[ ! -z "${_PARAM[version]}" ]] ; then
VERSION=$(grep "^# @version" $0 | head --lines=1 | cut --only-delimited --fields=3- --delimiter=" ")
echo "${SCRIPT_CMD_NAME} version ${VERSION}"
CMD="version"
fi
if [[ ! -z "${_PARAM[help]}" ]] ; then
echo "${__DOC__}"
CMD="help"
fi
case "${CMD}" in
"free")
# @method adpn staging free
# Display information about free space available on the staging server.
#
# Usage: adpn staging free
#
# @version 2021.0826
declare P_ARGS=( "${_ARGV[@]:2}" )
STAGING="$( adpn_parameter_from "--on" "--to" "--remote" )"
[[ -z "${STAGING}" ]] && STAGING="${P_ARGS[0]}" && P_ARGS=( "${P_ARGS[@]:1}" )
[[ -z "${STAGING}" ]] && STAGING="$( adpn_parameter_from ".stage/base" )"
adpn-stage-content.py --volume "${STAGING}" | adpn-json.py
#printf "[%s] Available cache volumes:\n\n" "${SCRIPT} ${CMD}"
#printf "%s" "${GAMMAS}" | xargs df --human-readable
EXITCODE="$?"
;;
"fit")
declare P_ARGS=( "${_ARGV[@]:2}" )
LOCAL="$( adpn_parameter_from "--from" "--local" )"
[[ -z "${LOCAL}" ]] && LOCAL="${P_ARGS[0]}" && P_ARGS=( "${P_ARGS[@]:1}" )
STAGING="$( adpn_parameter_from "--on" "--to" "--remote" )"
[[ -z "${STAGING}" ]] && STAGING="${P_ARGS[0]}" && P_ARGS=( "${P_ARGS[@]:1}" )
[[ -z "${STAGING}" ]] && STAGING="$( adpn_parameter_from ".stage/base" )"
if [[ -d "${LOCAL}" ]] ; then
LOCAL="$( readlink -f "${LOCAL}" )"
LOCAL_BASE="$( basename "${LOCAL}" )"
fi
EXTENT="$( du --bytes --max-depth=0 "${LOCAL}" )"
EXTENT_BYTES="$( printf "%s" "${EXTENT}" | cut --field=1 )"
EXTENT_HUMAN="$( numfmt --to=iec-i --suffix=B "${EXTENT_BYTES}" | sed -E 's/([0-9.])([^0-9.])/\1 \2/' )"
VOLUME="$( "${ME}" free --remote="${STAGING}" )"
FREE_HUMAN="$( printf "%s\n" "${VOLUME}" | grep -E '^unused_space_available_to_user' | cut -s --field=2 )"
FREE_BYTES="$( printf "%s\n" "${VOLUME}" | grep -E '^unused_bytes_available_to_user' | cut -s --field=2 )"
if [[ "${EXTENT_BYTES}" =~ ^[0-9]+$ ]] ; then
if [[ "${FREE_BYTES}" =~ ^[0-9]+$ ]] ; then
test "${FREE_BYTES}" -gt "${EXTENT_BYTES}" ; EXITCODE="$?"
if [[ "${EXITCODE}" -eq 0 ]] ; then
printf "OK: local AU [%s] (%s) should fit available space on staging area [%s] (%s free)\n" "${LOCAL_BASE}" "${EXTENT_HUMAN}" "${STAGING}" "${FREE_HUMAN}"
else
printf "WARNING: local AU [%s] (%s) WILL NOT FIT available space on staging area [%s] (%s free)\n" "${LOCAL_BASE}" "${EXTENT_HUMAN}" "${STAGING}" "${FREE_HUMAN}"
fi
fi
fi
;;
"version"|"help")
EXITCODE=0
;;
*)
echo "[${SCRIPT_CMD_NAME}] '${CMD}' command not understood." 1>&2
echo "${__HELP__}" 1>&2
EXITCODE=255
;;
esac
##########################################################################################
### CLEANUP: remove temporary output file. ###############################################
##########################################################################################
exit ${EXITCODE}