[__download] support multiple checksum formats and download utilities, add --onchange and other minor changes

This commit is contained in:
ander 2020-06-21 01:13:30 +03:00
parent a6543a72ad
commit 26dfdf37c2
7 changed files with 116 additions and 22 deletions

View file

@ -2,19 +2,71 @@
dst="/$__object_id" dst="/$__object_id"
# shellcheck disable=SC2059 if [ ! -f "$dst" ]
cmd="$( printf "$( cat "$__object/parameter/cmd-sum" )" "$dst" )"
sum="$( cat "$__object/parameter/sum" )"
if [ -f "$dst" ]
then then
if [ "$( eval "$cmd" )" = "$sum" ]
then
echo 'present'
else
echo 'mismatch'
fi
else
echo 'absent' echo 'absent'
exit 0
fi
sum_should="$( cat "$__object/parameter/sum" )"
if [ -f "$__object/parameter/cmd-sum" ]
then
# shellcheck disable=SC2059
sum_is="$( eval "$( printf \
"$( cat "$__object/parameter/cmd-sum" )" \
"$dst" )" )"
else
os="$( "$__explorer/os" )"
if echo "$sum_should" | grep -Eq '^[0-9]+\s[0-9]+$'
then
sum_is="$( cksum "$dst" | awk '{print $1" "$2}' )"
elif echo "$sum_should" | grep -Eiq '^md5:[a-f0-9]{32}$'
then
case "$os" in
freebsd)
sum_is="md5:$( md5 -q "$dst" )"
;;
*)
sum_is="md5:$( md5sum "$dst" | awk '{print $1}' )"
;;
esac
elif echo "$sum_should" | grep -Eiq '^sha1:[a-f0-9]{40}$'
then
case "$os" in
freebsd)
sum_is="sha1:$( sha1 -q "$dst" )"
;;
*)
sum_is="sha1:$( sha1sum "$dst" | awk '{print $1}' )"
;;
esac
elif echo "$sum_should" | grep -Eiq '^sha256:[a-f0-9]{64}$'
then
case "$os" in
freebsd)
sum_is="sha256:$( sha256 -q "$dst" )"
;;
*)
sum_is="sha256:$( sha256sum "$dst" | awk '{print $1}' )"
;;
esac
fi
fi
if [ -z "$sum_is" ]
then
echo 'no checksum from target' >&2
exit 1
fi
if [ "$sum_is" = "$sum_should" ]
then
echo 'present'
else
echo 'mismatch'
fi fi

View file

@ -9,12 +9,31 @@ fi
url="$( cat "$__object/parameter/url" )" url="$( cat "$__object/parameter/url" )"
cmd="$( cat "$__object/parameter/cmd-get" )"
tmp="$( mktemp )" tmp="$( mktemp )"
dst="/$__object_id" dst="/$__object_id"
if [ -f "$__object/parameter/cmd-get" ]
then
cmd="$( cat "$__object/parameter/cmd-get" )"
elif command -v wget > /dev/null
then
cmd="wget -O - '%s'"
elif command -v curl > /dev/null
then
cmd="curl -o - '%s'"
elif command -v fetch > /dev/null
then
cmd="fetch -o - '%s'"
else
echo 'no usable locally installed utility for downloading' >&2
exit 1
fi
printf "$cmd > %s\n" \ printf "$cmd > %s\n" \
"$url" \ "$url" \
"$tmp" "$tmp"
@ -33,3 +52,5 @@ printf '%s %s %s:%s\n' \
"$dst" "$dst"
echo "rm -f '$tmp'" echo "rm -f '$tmp'"
echo 'downloaded' > "$__messages_out"

View file

@ -0,0 +1,7 @@
#!/bin/sh -e
if [ -f "$__object/parameter/onchange" ] \
&& grep -Fq "$__object_id:downloaded" "$__messages_in"
then
cat "$__object/parameter/onchange"
fi

View file

@ -10,7 +10,13 @@ DESCRIPTION
----------- -----------
You must use persistent storage in target host for destination file You must use persistent storage in target host for destination file
(``$__object_id``) because it will be used for checksum calculation (``$__object_id``) because it will be used for checksum calculation
in order to decide if file must be downloaded. in order to decide if file must be (re-)downloaded.
By default type will try to use following locally installed utilities
for downloading (in order): ``wget``, ``curl`` or ``fetch``.
Environment variables like ``{http,https,ftp}_proxy`` etc can be used on
cdist execution (``http_proxy=foo cdist config ...``).
REQUIRED PARAMETERS REQUIRED PARAMETERS
@ -19,20 +25,29 @@ url
URL from which to download the file. URL from which to download the file.
sum sum
Checksum of downloaded file. Checksum of file going to be downloaded.
By default output of ``cksum`` without filename is expected.
Other hash formats supported with prefixes: ``md5:``, ``sha1:`` and ``sha256:``.
onchange
Execute this command after download.
OPTIONAL PARAMETERS OPTIONAL PARAMETERS
------------------- -------------------
cmd-get cmd-get
Command used for downloading. Command used for downloading.
Default is ``wget -O- '%s'``.
Command must output to ``stdout``. Command must output to ``stdout``.
Parameter will be used for ``printf`` and must include only one
variable ``%s`` which will become URL.
For example: ``wget -O - '%s'``.
cmd-sum cmd-sum
Command used for checksum calculation. Command used for checksum calculation.
Default is ``md5sum '%s' | awk '{print $1}'``.
Command output and ``--sum`` parameter must match. Command output and ``--sum`` parameter must match.
Parameter will be used for ``printf`` and must include only one
variable ``%s`` which will become destination.
For example: ``md5sum '%s' | awk '{print $1}'``.
EXAMPLES EXAMPLES
@ -45,7 +60,7 @@ EXAMPLES
require='__directory/opt/cpma' \ require='__directory/opt/cpma' \
__download /opt/cpma/cnq3.zip \ __download /opt/cpma/cnq3.zip \
--url https://cdn.playmorepromode.com/files/cnq3/cnq3-1.51.zip \ --url https://cdn.playmorepromode.com/files/cnq3/cnq3-1.51.zip \
--sum 46da3021ca9eace277115ec9106c5b46 --sum md5:46da3021ca9eace277115ec9106c5b46
require='__download/opt/cpma/cnq3.zip' \ require='__download/opt/cpma/cnq3.zip' \
__unpack /opt/cpma/cnq3.zip \ __unpack /opt/cpma/cnq3.zip \

View file

@ -1 +0,0 @@
wget -O- '%s'

View file

@ -1 +0,0 @@
md5sum '%s' | awk '{print $1}'

View file

@ -1,2 +1,3 @@
cmd-get cmd-get
cmd-sum cmd-sum
onchange