diff options
author | Scott Moser <smoser@ubuntu.com> | 2011-12-20 11:39:46 -0500 |
---|---|---|
committer | Scott Moser <smoser@ubuntu.com> | 2011-12-20 11:39:46 -0500 |
commit | 27e88fe213b0a3505ca565cae63d7d7124b7f969 (patch) | |
tree | 378612fbca7362182ffcfd5e22de1a4cf35dddbf /tools/cloud-init-per | |
parent | cc915378bd187c8db63b6c65b2e9c7510b8578f6 (diff) | |
download | vyos-cloud-init-27e88fe213b0a3505ca565cae63d7d7124b7f969.tar.gz vyos-cloud-init-27e88fe213b0a3505ca565cae63d7d7124b7f969.zip |
replace cloud-init-run-module with cloud-init-per
This replaces cloud-init-run-module (which was probably rarely or never used)
with 'cloud-init-per' which does basically the same thing, but doesn't
support "modules".
Diffstat (limited to 'tools/cloud-init-per')
-rwxr-xr-x | tools/cloud-init-per | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/tools/cloud-init-per b/tools/cloud-init-per new file mode 100755 index 00000000..51194d9f --- /dev/null +++ b/tools/cloud-init-per @@ -0,0 +1,60 @@ +#!/bin/sh + +DATA_PRE="/var/lib/cloud/data/bootper" +INST_PRE="/var/lib/cloud/instance/bootper" + +Usage() { + cat <<EOF +Usage: ${0##*/} frequency name cmd [ arg1 [ arg2 [ ... ] ] + run cmd with arguments provided. + + This utility can make it easier to use boothooks or bootcmd + on a per "once" or "always" basis. + + If frequency is: + * once: run only once (do not re-run for new instance-id) + * instance: run only the first boot for a given instance-id + * always: run every boot + +EOF +} +error() { echo "$@" 1>&2; } +fail() { [ $# -eq 0 ] || error "$@"; exit 1; } + +# support the old 'cloud-init-run-module freq name "execute" cmd arg1' +# if < 3 arguments, it will fail below on usage. +if [ "${0##*/}" = "cloud-init-run-module" ]; then + if [ $# -le 2 -o "$3" = "execute" ]; then + error "Warning: ${0##*/} is deprecated. Please use cloud-init-per." + freq=$1; name=$2; + [ $# -le 2 ] || shift 3; + set -- "$freq" "$name" "$@" + else + fail "legacy cloud-init-run-module only supported with module 'execute'" + fi +fi + +[ "$1" = "-h" -o "$1" = "--help" ] && { Usage ; exit 0; } +[ $# -ge 3 ] || { Usage 1>&2; exit 1; } +freq=$1 +name=$2 +shift 2; + +[ "${name#*/}" = "${name}" ] || fail "name cannot contain a /" +[ "$(id -u)" = "0" ] || fail "must be root" + +case "$freq" in + once|always) sem="${DATA_PRE}.$name.$freq";; + instance) sem="${INST_PRE}.$name.$freq";; + *) Usage 1>&2; fail "invalid frequency: $freq";; +esac + +[ -d "${sem%/*}" ] || mkdir -p "${sem%/*}" || + fail "failed to make directory for ${sem}" + +[ "$freq" != "always" -a -e "$sem" ] && exit 0 +"$@" +ret=$? +printf "%s\t%s\n" "$ret" "$(date +%s)" > "$sem" || + fail "failed to write to $sem" +exit $ret |