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".
This commit is contained in:
Scott Moser 2011-12-20 11:39:46 -05:00
parent 794cdb0f61
commit eb7ea6c08c
4 changed files with 62 additions and 86 deletions

View File

@ -1,85 +0,0 @@
#!/usr/bin/python
# vi: ts=4 expandtab
#
# Copyright (C) 2009-2010 Canonical Ltd.
#
# Author: Scott Moser <scott.moser@canonical.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import cloudinit
import cloudinit.util as util
import logging
def Usage(out = sys.stdout):
out.write("Usage: cloud-init-run-module freq sem-name mod-name [args]\n")
def main():
util.close_stdin()
# expect to be called with
# <freq> <semaphore-name> <module-name> args
if len(sys.argv) < 4:
Usage(sys.stderr)
sys.exit(1)
(freq,semname,modname)=sys.argv[1:4]
run_args=sys.argv[4:]
cloudinit.logging_set_from_cfg_file()
log = logging.getLogger()
log.info("cloud-init-run-module %s" % sys.argv[1:])
cloud = cloudinit.CloudInit()
try:
cloud.get_data_source()
except Exception as e:
fail("Failed to get instance data\n\t%s" % traceback.format_exc(),log)
if cloud.sem_has_run(semname,freq):
msg="%s already ran %s" % (semname,freq)
sys.stderr.write("%s\n" % msg)
log.debug(msg)
sys.exit(0)
try:
mod = __import__('cloudinit.' + modname)
inst = getattr(mod,modname)
except:
fail("Failed to load module cloudinit.%s\n" % modname)
import os
cfg_path = None
cfg_env_name = cloudinit.cfg_env_name
if os.environ.has_key(cfg_env_name):
cfg_path = os.environ[cfg_env_name]
try:
cloud.sem_and_run(semname, freq, inst.run, [run_args,cfg_path,log], False)
except Exception as e:
fail("Execution of %s failed:%s" % (semname,e), log)
sys.exit(0)
def err(msg,log=None):
if log:
log.error(msg)
sys.stderr.write(msg + "\n")
def fail(msg,log=None):
err(msg,log)
sys.exit(1)
if __name__ == '__main__':
main()

View File

@ -14,6 +14,7 @@ cloud-init-fixups:
for x in $(DEB_DESTDIR)/usr/bin/*.py; do mv "$$x" "$${x%.py}"; done
install -d $(DEB_DESTDIR)/etc/rsyslog.d
cp tools/21-cloudinit.conf $(DEB_DESTDIR)/etc/rsyslog.d/21-cloudinit.conf
ln -sf cloud-init-per $(DEB_DESTDIR)/usr/bin/cloud-init-run-module
# You only need to run this immediately after checking out the package from
# revision control.

View File

@ -34,8 +34,8 @@ setup(name='cloud-init',
url='http://launchpad.net/cloud-init/',
packages=['cloudinit', 'cloudinit.CloudConfig' ],
scripts=['cloud-init.py',
'cloud-init-run-module.py',
'cloud-init-cfg.py',
'tools/cloud-init-per',
],
data_files=[('/etc/cloud', glob('config/*.cfg')),
('/etc/cloud/cloud.cfg.d', glob('config/cloud.cfg.d/*')),

60
tools/cloud-init-per Executable file
View File

@ -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