New type __staged_file: Manage staged files

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
This commit is contained in:
Steven Armstrong 2015-02-24 13:01:48 +01:00
parent 041569b41c
commit 4f7ae8425d
9 changed files with 252 additions and 0 deletions

View file

@ -0,0 +1,98 @@
#!/bin/sh
#
# 2015 Steven Armstrong (steven-cdist at armstrong.cc)
#
# 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")"
cksum="$(cat "$__object/parameter/cksum")"
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() {
printf "$fetch_command" "$source"
printf ' > "%s"\n' "$stage_file"
}
fetch_and_prepare_file() {
printf 'tmpdir="$(mktemp -d --tmpdir="/tmp" "%s")"\n' "${__type##*/}.XXXXXXXXXX"
printf 'cd "$tmpdir"\n'
printf "$fetch_command > \"%s\"\n" "$source" "$source_file_name"
prepare_command="$(cat "$__object/parameter/prepare-command")"
printf "$prepare_command > \"%s\"\n" "$source_file_name" "$stage_file"
printf 'cd - >/dev/null\n'
printf 'rm -rf "$tmpdir"\n'
}
cat << DONE
verify_cksum() {
cksum_is="\$(cksum "$stage_file" | cut -d' ' -f1,2)"
cksum_should="$(cat "$__object/parameter/cksum" | cut -d' ' -f1,2)"
if [ "\$cksum_is" == "\$cksum_should" ]; then
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

View file

@ -0,0 +1,103 @@
cdist-type__staged_file(7)
==========================
Steven Armstrong <steven-cdist--@--armstrong.cc>
NAME
----
cdist-type__staged_file - manage staged files
DESCRIPTION
-----------
Manages a staged file that is downloaded on the server (the machine running
cdist) and then deployed to the target host using the __file type.
REQUIRED PARAMETERS
-------------------
source::
the URL from which to retreive the source file.
e.g.
https://dl.bintray.com/mitchellh/consul/0.4.1_linux_amd64.zip
file:///path/to/local/file
cksum::
the output of running the command: `cksum $source-file`
e.g.
$ echo foobar > /tmp/foobar
$ cksum /tmp/foobar
857691210 7 /tmp/foobar
If either checksum or file size has changed the file will be
(re)fetched from the --source. The file name can be omitted and is
ignored if given.
OPTIONAL PARAMETERS
-------------------
fetch-command::
the command used to fetch the staged file using printf formatting.
Where a single %s will be replaced with the value of the given --source
parameter. The --fetch-command is expected to output the fetched file to
stdout.
Defaults to 'curl -s -L "%s"'.
group::
see cdist-type__file
owner::
see cdist-type__file
mode::
see cdist-type__file
prepare-command::
the optional command used to prepare or preprocess the staged file for later
use by the file type.
If given, it must be a string in printf formatting where a single %s will
be replaced with the last segment (filename) of the value of the given
--source parameter.
It is executed in the same directory into which the fetched file has been
saved. The --prepare-command is expected to output the final file to stdout.
So for example given a --source of https://example.com/my-zip.zip, and a
--prepare-command of 'unzip -p "%s"', the code `unzip -p "my-zip.zip"` will
be executed in the folder containing the downloaded file my-zip.zip.
A more complex example might be --prepare-command 'tar -xz "%s"; cat path/from/archive'
stage-dir::
the directory in which to store downloaded and prepared files.
Defaults to '/var/tmp/cdist/__staged_file'
state::
see cdist-type__file
EXAMPLES
--------
--------------------------------------------------------------------------------
__staged_file /usr/local/bin/consul \
--source file:///path/to/local/copy/consul \
--cksum '428915666 15738724' \
--state present \
--group root \
--owner root \
--mode 755
__staged_file /usr/local/bin/consul \
--source https://dl.bintray.com/mitchellh/consul/0.4.1_linux_amd64.zip \
--cksum '428915666 15738724' \
--fetch-command 'curl -s -L "%s"' \
--prepare-command 'unzip -p "%s"' \
--state present \
--group root \
--owner root \
--mode 755
--------------------------------------------------------------------------------
SEE ALSO
--------
- cdist-type(7)
- cdist-type__file(7)
COPYING
-------
Copyright \(C) 2015 Steven Armstrong. Free use of this software is
granted under the terms of the GNU General Public License version 3 (GPLv3).

View file

@ -0,0 +1,38 @@
#!/bin/sh
#
# 2015 Steven Armstrong (steven-cdist at armstrong.cc)
#
# 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/>.
#
destination="$__object_id"
source="$(cat "$__object/parameter/source")"
cksum="$(cat "$__object/parameter/cksum")"
stage_dir="$(cat "$__object/parameter/stage-dir")"
state="$(cat "$__object/parameter/state")"
fetch_command="$(cat "$__object/parameter/fetch-command")"
stage_file="${stage_dir}/${destination}"
set -- "/${destination}"
for param in owner group mode state; do
if [ -f "$__object/parameter/$param" ]; then
set -- "$@" "--${param}" "$(cat "$__object/parameter/$param")"
fi
done
set -- "$@" --source "$stage_file"
require="$__object_name" \
__file "$@"

View file

@ -0,0 +1 @@
curl -s -L "%s"

View file

@ -0,0 +1 @@
/var/tmp/cdist/__staged_file

View file

@ -0,0 +1 @@
present

View file

@ -0,0 +1,7 @@
fetch-command
group
owner
mode
prepare-command
stage-dir
state

View file

@ -0,0 +1,2 @@
cksum
source

View file

@ -5,6 +5,7 @@ Changelog
* Exception: No braces means author == Nico Schottelius
next:
* New type __staged_file: Manage staged files
* New type __config_file: Manage configuration files and run code on change
3.1.11: