cdist-dump 6.24 KB
Newer Older
Darko Poljak's avatar
Darko Poljak committed
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
#!/bin/sh

VERSION="0.0.1"
RELEASE=""

set -u
# set -x

hosts=
cache_dir=~/.cdist/cache

do_all=1
do_global_explorer=
do_type_explorer=
do_script_stdout=
do_script_stderr=
do_gencode=
do_code=
do_messages=
do_parameter=
delimiter=':'
ln=
filename_prefix=1
verbose=0

myname=${0##*/}

print_version()
{
    printf "%s %s %s\n" "${myname}" "${VERSION}" "${RELEASE}"
}

usage()
{
    cat << eof
${myname}: [options] [host...]
eof

    print_version

    cat << eof

Dump data from cache directories.

host      
    Dump data for specified hosts. If not specified then all data
    from cache directory is dumped.

Options
 -a             dump all
 -C CACHE-DIR   use specified CACHE-DIR (default: ~/.cdist/cache)
 -c             dump code-*
 -d             delimiter used for filename and line number prefix (default: ':')
 -E             dump global explorers
 -e             dump type explorers
 -F             disable filename prefix (enabled by default)
 -f             enable filename prefix (default)
 -g             dump gencode-*
 -h             show this help screen and exit
 -L             disable line number prefix (default)
 -l             enable line number prefix (disabled by default)
 -m             dump messages
 -o             dump executions' stdout
 -p             dump parameters
 -r             dump executions' stderr
 -V             show version and exit
 -v             increase verbosity
eof
}

exit_err()
{
    printf "%s\n" "$1"
    exit 1
}

# parse options
while [ "$#" -ge 1 ]
do
    case "$1" in
        -a)
            do_all=1
            ;;
        -C)
            if [ "$#" -ge 2 ]
            then
                case "$2" in
                    -*)
                        exit_err "Missing cache directory"
                        ;;
                    *)
                        cache_dir="$2"
                        shift
                        ;;
                esac
            else
                exit_err "Missing cache directory"
            fi
            ;;
        -c)
            do_code=1
            do_all=
            ;;
        -d)
            if [ "$#" -ge 2 ]
            then
                case "$2" in
                    -*)
                        exit_err "Missing delimiter"
                        ;;
                    *)
                        delimiter="$2"
                        shift
                        ;;
                esac
            else
                exit_err "Missing delimiter"
            fi
            ;;
        -E)
            do_global_explorer=1
            do_all=
            ;;
        -e)
            do_type_explorer=1
            do_all=
            ;;
        -F)
            filename_prefix=
            ;;
        -f)
            filename_prefix=1
            ;;
        -g)
            do_gencode=1
            do_all=
            ;;
        -h)
            usage
            exit 0
            ;;
        -L)
            ln=
            ;;
        -l)
            ln=1
            ;;
        -m)
            do_messages=1
            do_all=
            ;;
        -o)
            do_script_stdout=1
            do_all=
            ;;
        -p)
            do_parameter=1
            do_all=
            ;;
        -r)
            do_script_stderr=1
            do_all=
            ;;
        -V)
            print_version
            exit 0
            ;;
        -v)
            verbose=$((verbose + 1))
            ;;
        *)
            hosts="${hosts} $1"
            break
            ;;
    esac
    shift
done

if [ "${ln}" = "1" ]
then
    ln="NR \"${delimiter}\""
fi

if [ "${filename_prefix}" = "1" ]
then
    filename_prefix="{}${delimiter}"
fi

if [ "${do_all}" = "1" ]
then
    do_global_explorer=1
    do_type_explorer=1
    do_script_stdout=1
    do_script_stderr=1
    do_gencode=1
    do_code=1
    do_messages=1
    do_parameter=1
fi

set -- -size +0
set -- "$@" \(
or=

print_verbose()
{
Darko Poljak's avatar
Darko Poljak committed
207
    if [ "${verbose}" -ge "$1" ]
Darko Poljak's avatar
Darko Poljak committed
208 209 210 211 212 213 214 215 216
    then
        printf "%s\n" "$2"
    fi
}

hor_line()
{
    if [ $# -gt 0 ]
    then
Darko Poljak's avatar
Darko Poljak committed
217
        c="$1"
Darko Poljak's avatar
Darko Poljak committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    else
        c='='
    fi
    printf "%78s\n" "" | tr ' ' "${c}"
}

if [ "${do_global_explorer}" ]
then
    print_verbose 2 "Dumping global explorers"
    set -- "$@" ${or} \( \
                -path "*/explorer/*" -a \
                ! -path "*/conf/*" -a \
                ! -path "*/object/*/explorer/*" \
            \)
    or="-o"
fi

if [ "${do_type_explorer}" ]
then
    print_verbose 2 "Dumping type explorers"
    set -- "$@" ${or} -path "*/object/*/explorer/*"
    or="-o"
fi

if [ "${do_script_stdout}" ]
then
    print_verbose 2 "Dumping execution's stdout"
    set -- "$@" ${or} -path "*/stdout/*"
    or="-o"
fi

if [ "${do_script_stderr}" ]
then
    print_verbose 2 "Dumping execution's stderr"
    set -- "$@" ${or} -path "*/stderr/*"
    or="-o"
fi

if [ "${do_gencode}" ]
then
    print_verbose 2 "Dumping gencode-*"
    set -- "$@" ${or} \( -name "gencode-*" -a ! -path "*/stdout/*" -a ! -path "*/stderr/*" \)
    or="-o"
fi

if [ "${do_code}" ]
then
    print_verbose 2 "Dumping code-*"
    set -- "$@" ${or} \( -name "code-*" -a ! -path "*/stdout/*" -a ! -path "*/stderr/*" \)
    or="-o"
fi

if [ "${do_messages}" ]
then
    print_verbose 2 "Dumping messages"
    set -- "$@" ${or} -name "messages"
    or="-o"
fi

if [ "${do_parameter}" ]
then
    print_verbose 2 "Dumping parameters"
    set -- "$@" ${or} -path "*/parameter/*"
    or="-o"
fi

set -- "$@" \)
set -- '.' "$@" -exec awk -v prefix="${filename_prefix}" "{print prefix ${ln} \$0}" {} \;

# printf "+ %s\n" "$*"

print_verbose 2 "Using cache dir: ${cache_dir}"

OLD_PWD=$(pwd)
Darko Poljak's avatar
Darko Poljak committed
292
cd "${cache_dir}" || exit
Darko Poljak's avatar
Darko Poljak committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312

# If no host is specified then search all.
[ -z "${hosts}" ] && hosts="-"

for host in ${hosts}
do
    [ "${host}" = "-" ] && host=
    # find host cache directory
    host_dir=$(find . -name target_host -exec grep -l "${host}" {} +)
    print_verbose 3 "found host directory files:"
    print_verbose 3 "${host_dir}"

    OLD_IFS="${IFS}"
    IFS="
    "

    for d in ${host_dir}
    do
        dir=$(dirname "${d}")

Darko Poljak's avatar
Darko Poljak committed
313 314
        print_verbose 0 "target host: $(cat "${dir}/target_host"), host directory: ${dir}"
        hor_line '='
Darko Poljak's avatar
Darko Poljak committed
315 316

        PREV_PWD=$(pwd)
Darko Poljak's avatar
Darko Poljak committed
317
        cd "${dir}" || exit
Darko Poljak's avatar
Darko Poljak committed
318 319 320
        # set -x
        find "$@"
        # set +x
Darko Poljak's avatar
Darko Poljak committed
321
        cd "${PREV_PWD}" || exit
Darko Poljak's avatar
Darko Poljak committed
322 323 324
    done
    IFS="${OLD_IFS}"
done
Darko Poljak's avatar
Darko Poljak committed
325
cd "${OLD_PWD}" || exit