| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
VERSION="0.0.1" |
| 4 |
RELEASE="" |
| 5 |
|
| 6 |
set -u |
| 7 |
# set -x |
| 8 |
|
| 9 |
hosts= |
| 10 |
cache_dir=~/.cdist/cache |
| 11 |
|
| 12 |
do_all=1 |
| 13 |
do_global_explorer= |
| 14 |
do_type_explorer= |
| 15 |
do_script_stdout= |
| 16 |
do_script_stderr= |
| 17 |
do_gencode= |
| 18 |
do_code= |
| 19 |
do_messages= |
| 20 |
do_parameter= |
| 21 |
delimiter=':' |
| 22 |
ln= |
| 23 |
filename_prefix=1 |
| 24 |
verbose=0 |
| 25 |
|
| 26 |
myname=${0##*/} |
| 27 |
|
| 28 |
print_version() |
| 29 |
{ |
| 30 |
printf "%s %s %s\n" "${myname}" "${VERSION}" "${RELEASE}" |
| 31 |
} |
| 32 |
|
| 33 |
usage() |
| 34 |
{ |
| 35 |
cat << eof |
| 36 |
${myname}: [options] [host...] |
| 37 |
eof |
| 38 |
|
| 39 |
print_version |
| 40 |
|
| 41 |
cat << eof |
| 42 |
|
| 43 |
Dump data from cache directories. |
| 44 |
|
| 45 |
host |
| 46 |
Dump data for specified hosts. If not specified then all data |
| 47 |
from cache directory is dumped. |
| 48 |
|
| 49 |
Options |
| 50 |
-a dump all |
| 51 |
-C CACHE-DIR use specified CACHE-DIR (default: ~/.cdist/cache) |
| 52 |
-c dump code-* |
| 53 |
-d DELIMITER delimiter used for filename and line number prefix (default: ':') |
| 54 |
-E dump global explorers |
| 55 |
-e dump type explorers |
| 56 |
-F disable filename prefix (enabled by default) |
| 57 |
-f enable filename prefix (default) |
| 58 |
-g dump gencode-* |
| 59 |
-h show this help screen and exit |
| 60 |
-L disable line number prefix (default) |
| 61 |
-l enable line number prefix (disabled by default) |
| 62 |
-m dump messages |
| 63 |
-o dump executions' stdout |
| 64 |
-p dump parameters |
| 65 |
-r dump executions' stderr |
| 66 |
-V show version and exit |
| 67 |
-v increase verbosity |
| 68 |
eof |
| 69 |
} |
| 70 |
|
| 71 |
exit_err() |
| 72 |
{ |
| 73 |
printf "%s\n" "$1" |
| 74 |
exit 1 |
| 75 |
} |
| 76 |
|
| 77 |
# parse options |
| 78 |
while [ "$#" -ge 1 ] |
| 79 |
do |
| 80 |
case "$1" in |
| 81 |
-a) |
| 82 |
do_all=1 |
| 83 |
;; |
| 84 |
-C) |
| 85 |
if [ "$#" -ge 2 ] |
| 86 |
then |
| 87 |
case "$2" in |
| 88 |
-*) |
| 89 |
exit_err "Missing cache directory" |
| 90 |
;; |
| 91 |
*) |
| 92 |
cache_dir="$2" |
| 93 |
shift |
| 94 |
;; |
| 95 |
esac |
| 96 |
else |
| 97 |
exit_err "Missing cache directory" |
| 98 |
fi |
| 99 |
;; |
| 100 |
-c) |
| 101 |
do_code=1 |
| 102 |
do_all= |
| 103 |
;; |
| 104 |
-d) |
| 105 |
if [ "$#" -ge 2 ] |
| 106 |
then |
| 107 |
case "$2" in |
| 108 |
-*) |
| 109 |
exit_err "Missing delimiter" |
| 110 |
;; |
| 111 |
*) |
| 112 |
delimiter="$2" |
| 113 |
shift |
| 114 |
;; |
| 115 |
esac |
| 116 |
else |
| 117 |
exit_err "Missing delimiter" |
| 118 |
fi |
| 119 |
;; |
| 120 |
-E) |
| 121 |
do_global_explorer=1 |
| 122 |
do_all= |
| 123 |
;; |
| 124 |
-e) |
| 125 |
do_type_explorer=1 |
| 126 |
do_all= |
| 127 |
;; |
| 128 |
-F) |
| 129 |
filename_prefix= |
| 130 |
;; |
| 131 |
-f) |
| 132 |
filename_prefix=1 |
| 133 |
;; |
| 134 |
-g) |
| 135 |
do_gencode=1 |
| 136 |
do_all= |
| 137 |
;; |
| 138 |
-h) |
| 139 |
usage |
| 140 |
exit 0 |
| 141 |
;; |
| 142 |
-L) |
| 143 |
ln= |
| 144 |
;; |
| 145 |
-l) |
| 146 |
ln=1 |
| 147 |
;; |
| 148 |
-m) |
| 149 |
do_messages=1 |
| 150 |
do_all= |
| 151 |
;; |
| 152 |
-o) |
| 153 |
do_script_stdout=1 |
| 154 |
do_all= |
| 155 |
;; |
| 156 |
-p) |
| 157 |
do_parameter=1 |
| 158 |
do_all= |
| 159 |
;; |
| 160 |
-r) |
| 161 |
do_script_stderr=1 |
| 162 |
do_all= |
| 163 |
;; |
| 164 |
-V) |
| 165 |
print_version |
| 166 |
exit 0 |
| 167 |
;; |
| 168 |
-v) |
| 169 |
verbose=$((verbose + 1)) |
| 170 |
;; |
| 171 |
*) |
| 172 |
hosts="${hosts} $1" |
| 173 |
break |
| 174 |
;; |
| 175 |
esac |
| 176 |
shift |
| 177 |
done |
| 178 |
|
| 179 |
if [ "${ln}" = "1" ] |
| 180 |
then |
| 181 |
ln="NR \"${delimiter}\"" |
| 182 |
fi |
| 183 |
|
| 184 |
if [ "${filename_prefix}" = "1" ] |
| 185 |
then |
| 186 |
filename_prefix="{}${delimiter}" |
| 187 |
fi |
| 188 |
|
| 189 |
if [ "${do_all}" = "1" ] |
| 190 |
then |
| 191 |
do_global_explorer=1 |
| 192 |
do_type_explorer=1 |
| 193 |
do_script_stdout=1 |
| 194 |
do_script_stderr=1 |
| 195 |
do_gencode=1 |
| 196 |
do_code=1 |
| 197 |
do_messages=1 |
| 198 |
do_parameter=1 |
| 199 |
fi |
| 200 |
|
| 201 |
set -- -size +0 |
| 202 |
set -- "$@" \( |
| 203 |
or= |
| 204 |
|
| 205 |
print_verbose() |
| 206 |
{ |
| 207 |
if [ "${verbose}" -ge "$1" ] |
| 208 |
then |
| 209 |
printf "%s\n" "$2" |
| 210 |
fi |
| 211 |
} |
| 212 |
|
| 213 |
hor_line() |
| 214 |
{ |
| 215 |
if [ $# -gt 0 ] |
| 216 |
then |
| 217 |
c="$1" |
| 218 |
else |
| 219 |
c='=' |
| 220 |
fi |
| 221 |
printf "%78s\n" "" | tr ' ' "${c}" |
| 222 |
} |
| 223 |
|
| 224 |
if [ "${do_global_explorer}" ] |
| 225 |
then |
| 226 |
print_verbose 2 "Dumping global explorers" |
| 227 |
# shellcheck disable=SC2086 |
| 228 |
set -- "$@" ${or} \( \ |
| 229 |
-path "*/explorer/*" -a \ |
| 230 |
! -path "*/conf/*" -a \ |
| 231 |
! -path "*/object/*/explorer/*" \ |
| 232 |
\) |
| 233 |
or="-o" |
| 234 |
fi |
| 235 |
|
| 236 |
if [ "${do_type_explorer}" ] |
| 237 |
then |
| 238 |
print_verbose 2 "Dumping type explorers" |
| 239 |
set -- "$@" ${or} -path "*/object/*/explorer/*" |
| 240 |
or="-o" |
| 241 |
fi |
| 242 |
|
| 243 |
if [ "${do_script_stdout}" ] |
| 244 |
then |
| 245 |
print_verbose 2 "Dumping execution's stdout" |
| 246 |
set -- "$@" ${or} -path "*/stdout/*" |
| 247 |
or="-o" |
| 248 |
fi |
| 249 |
|
| 250 |
if [ "${do_script_stderr}" ] |
| 251 |
then |
| 252 |
print_verbose 2 "Dumping execution's stderr" |
| 253 |
set -- "$@" ${or} -path "*/stderr/*" |
| 254 |
or="-o" |
| 255 |
fi |
| 256 |
|
| 257 |
if [ "${do_gencode}" ] |
| 258 |
then |
| 259 |
print_verbose 2 "Dumping gencode-*" |
| 260 |
set -- "$@" ${or} \( -name "gencode-*" -a ! -path "*/stdout/*" -a ! -path "*/stderr/*" \) |
| 261 |
or="-o" |
| 262 |
fi |
| 263 |
|
| 264 |
if [ "${do_code}" ] |
| 265 |
then |
| 266 |
print_verbose 2 "Dumping code-*" |
| 267 |
set -- "$@" ${or} \( -name "code-*" -a ! -path "*/stdout/*" -a ! -path "*/stderr/*" \) |
| 268 |
or="-o" |
| 269 |
fi |
| 270 |
|
| 271 |
if [ "${do_messages}" ] |
| 272 |
then |
| 273 |
print_verbose 2 "Dumping messages" |
| 274 |
set -- "$@" ${or} -name "messages" |
| 275 |
or="-o" |
| 276 |
fi |
| 277 |
|
| 278 |
if [ "${do_parameter}" ] |
| 279 |
then |
| 280 |
print_verbose 2 "Dumping parameters" |
| 281 |
set -- "$@" ${or} -path "*/parameter/*" |
| 282 |
or="-o" |
| 283 |
fi |
| 284 |
|
| 285 |
set -- "$@" \) |
| 286 |
set -- '.' "$@" -exec awk -v prefix="${filename_prefix}" "{print prefix ${ln} \$0}" {} \; |
| 287 |
|
| 288 |
# printf "+ %s\n" "$*" |
| 289 |
|
| 290 |
print_verbose 2 "Using cache dir: ${cache_dir}" |
| 291 |
|
| 292 |
OLD_PWD=$(pwd) |
| 293 |
cd "${cache_dir}" || exit |
| 294 |
|
| 295 |
# If no host is specified then search all. |
| 296 |
[ -z "${hosts}" ] && hosts="-" |
| 297 |
|
| 298 |
for host in ${hosts} |
| 299 |
do |
| 300 |
[ "${host}" = "-" ] && host= |
| 301 |
# find host cache directory |
| 302 |
host_dir=$(find . -name target_host -exec grep -l "${host}" {} +) |
| 303 |
print_verbose 3 "found host directory files:" |
| 304 |
print_verbose 3 "${host_dir}" |
| 305 |
|
| 306 |
OLD_IFS="${IFS}" |
| 307 |
IFS=" |
| 308 |
" |
| 309 |
|
| 310 |
for d in ${host_dir} |
| 311 |
do |
| 312 |
dir=$(dirname "${d}") |
| 313 |
|
| 314 |
print_verbose 0 "target host: $(cat "${dir}/target_host"), host directory: ${dir}" |
| 315 |
hor_line '=' |
| 316 |
|
| 317 |
PREV_PWD=$(pwd) |
| 318 |
cd "${dir}" || exit |
| 319 |
# set -x |
| 320 |
find "$@" |
| 321 |
# set +x |
| 322 |
cd "${PREV_PWD}" || exit |
| 323 |
done |
| 324 |
IFS="${OLD_IFS}" |
| 325 |
done |
| 326 |
cd "${OLD_PWD}" || exit |