summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2011-12-20 12:02:39 -0500
committerScott Moser <smoser@ubuntu.com>2011-12-20 12:02:39 -0500
commitb17a271da699230be8dad5d8c0227a5c7c238503 (patch)
treefc0f9419f83f8533cf6d6259b45c195795f4bb94 /tools
parent13015ef25fa7e748e916c99f083e338b28861a18 (diff)
parent57a28850fae6098a32e7c16ab90541fd785684cb (diff)
downloadvyos-cloud-init-b17a271da699230be8dad5d8c0227a5c7c238503.tar.gz
vyos-cloud-init-b17a271da699230be8dad5d8c0227a5c7c238503.zip
add INSTANCE_ID to env of bootcmd, add cloud-init-per
the environment varible INSTANCE_ID is set when invoking boothooks from multi-part input. However, previously that was not the case for things run via bootcmd. This adds cloud-init-per, which makes it easy for user in bootcmd or boothook to do something per 'instance', 'always', or 'once'. The functionality in cloud-init-per mostly duplicated what was in cloud-init-run-module. That supported "modules", but it is unlikely that it was used for anything other than "execute". So, cloud-init-per now replaces cloud-init-run-module and provides legacy support for the 'execute' path.
Diffstat (limited to 'tools')
-rwxr-xr-xtools/cloud-init-per60
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..5d9a2864
--- /dev/null
+++ b/tools/cloud-init-per
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+DATA_PRE="/var/lib/cloud/sem/bootper"
+INST_PRE="/var/lib/cloud/instance/sem/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