2017-06-28 11:19:32 +00:00
|
|
|
#!/bin/sh -e
|
2015-02-24 12:01:48 +00:00
|
|
|
#
|
|
|
|
# 2015 Steven Armstrong (steven-cdist at armstrong.cc)
|
2015-05-16 15:40:27 +00:00
|
|
|
# 2015 Nico Schottelius (nico-cdist at schottelius.org)
|
2015-02-24 12:01:48 +00:00
|
|
|
#
|
|
|
|
# This file is part of cdist.
|
|
|
|
#
|
|
|
|
# cdist is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# cdist is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
#set -x
|
|
|
|
|
|
|
|
destination="$__object_id"
|
|
|
|
source="$(cat "$__object/parameter/source")"
|
|
|
|
stage_dir="$(cat "$__object/parameter/stage-dir")"
|
|
|
|
state="$(cat "$__object/parameter/state")"
|
|
|
|
fetch_command="$(cat "$__object/parameter/fetch-command")"
|
|
|
|
stage_file="${stage_dir}/${destination}"
|
|
|
|
stage_file_dir="${stage_file%/*}"
|
|
|
|
source_file_name="${source##*/}"
|
|
|
|
|
|
|
|
if [ "$state" = "absent" ]; then
|
|
|
|
# nothing to do
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
#printf 'set -x\n'
|
|
|
|
|
|
|
|
if [ ! -d "$stage_dir" ]; then
|
|
|
|
printf 'mkdir -p "%s"\n' "$stage_dir"
|
|
|
|
printf 'chmod 700 "%s"\n' "$stage_dir"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -d "$stage_file_dir" ]; then
|
|
|
|
printf 'mkdir -p "%s"\n' "$stage_file_dir"
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
get_file() {
|
|
|
|
if [ -f "$__object/parameter/prepare-command" ]; then
|
|
|
|
fetch_and_prepare_file
|
|
|
|
else
|
|
|
|
fetch_file
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch_file() {
|
2018-10-04 19:30:28 +00:00
|
|
|
# shellcheck disable=SC2059
|
2015-02-24 12:01:48 +00:00
|
|
|
printf "$fetch_command" "$source"
|
|
|
|
printf ' > "%s"\n' "$stage_file"
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch_and_prepare_file() {
|
2018-10-04 21:24:30 +00:00
|
|
|
# shellcheck disable=SC2016
|
2019-03-25 21:36:55 +00:00
|
|
|
printf 'tmpdir="$(mktemp -d -p "/tmp" "%s")"\n' "${__type##*/}.XXXXXXXXXX"
|
2018-10-04 21:24:30 +00:00
|
|
|
# shellcheck disable=SC2016
|
2015-02-24 12:01:48 +00:00
|
|
|
printf 'cd "$tmpdir"\n'
|
2018-10-04 19:30:28 +00:00
|
|
|
# shellcheck disable=SC2059
|
2018-10-06 11:41:09 +00:00
|
|
|
printf "$fetch_command > \"%s\"\\n" "$source" "$source_file_name"
|
2015-02-24 12:01:48 +00:00
|
|
|
prepare_command="$(cat "$__object/parameter/prepare-command")"
|
2018-10-04 19:30:28 +00:00
|
|
|
# shellcheck disable=SC2059
|
2018-10-06 11:41:09 +00:00
|
|
|
printf "$prepare_command > \"%s\"\\n" "$source_file_name" "$stage_file"
|
2015-02-24 12:01:48 +00:00
|
|
|
printf 'cd - >/dev/null\n'
|
2018-10-04 21:24:30 +00:00
|
|
|
# shellcheck disable=SC2016
|
2015-02-24 12:01:48 +00:00
|
|
|
printf 'rm -rf "$tmpdir"\n'
|
|
|
|
}
|
|
|
|
|
|
|
|
cat << DONE
|
|
|
|
verify_cksum() {
|
|
|
|
cksum_is="\$(cksum "$stage_file" | cut -d' ' -f1,2)"
|
2018-10-02 18:44:46 +00:00
|
|
|
cksum_should="$(cut -d' ' -f1,2 "$__object/parameter/cksum")"
|
2015-05-16 15:40:27 +00:00
|
|
|
if [ "\$cksum_is" = "\$cksum_should" ]; then
|
2015-02-24 12:01:48 +00:00
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
DONE
|
|
|
|
|
|
|
|
if [ ! -f "$stage_file" ]; then
|
|
|
|
get_file
|
|
|
|
else
|
|
|
|
printf 'verify_cksum || {\n'
|
|
|
|
get_file
|
|
|
|
printf '}\n'
|
|
|
|
fi
|
|
|
|
|
|
|
|
cat << DONE
|
|
|
|
verify_cksum || {
|
|
|
|
echo "Failed to verify checksum for $__object_name" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
DONE
|