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.
This commit is contained in:
Scott Moser 2011-12-20 12:02:39 -05:00
commit be446abf45
8 changed files with 73 additions and 117 deletions

View File

@ -11,6 +11,9 @@
- close stdin in all cloud-init programs that are launched at boot (LP: #903993)
- revert management of /etc/hosts to 0.6.1 style (LP: #890501, LP: #871966)
- write full ssh keys to console for easy machine consumption (LP: #893400)
- put INSTANCE_ID environment variable in bootcmd scripts
- add 'cloud-init-per' script for easily running things with a given frequency
- replace cloud-init-run-module with cloud-init-per
0.6.2:
- fix bug where update was not done unless update was explicitly set.
It would not be run if 'upgrade' or packages were set to be installed

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

@ -35,7 +35,9 @@ def handle(name,cfg,cloud,log,args):
raise
try:
subprocess.check_call(['/bin/sh'], stdin=tmpf)
env=os.environ.copy()
env['INSTANCE_ID']=cloud.get_instance_id()
subprocess.check_call(['/bin/sh'], env=env, stdin=tmpf)
tmpf.close()
except:
log.warn("failed to run commands from bootcmd")

View File

@ -1,29 +0,0 @@
# 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/>.
def run(args,cfg,log):
import subprocess
import traceback
try:
subprocess.check_call(args)
return
except subprocess.CalledProcessError as e:
log.debug(traceback.format_exc(e))
raise Exception("Cmd returned %s: %s" % ( e.returncode, args ))
except OSError as e:
log.debug(traceback.format_exc(e))
raise Exception("Cmd failed to execute: %s" % ( args ))

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

@ -228,9 +228,13 @@ runcmd:
# in the boot process, only slightly after a 'boothook' would run.
# bootcmd should really only be used for things that could not be
# done later in the boot process. bootcmd is very much like
# boothook, but possibly with more friendly
# boothook, but possibly with more friendly.
# * bootcmd will run on every boot
# * the INSTANCE_ID variable will be set to the current instance id.
# * you can use 'cloud-init-boot-per' command to help only run once
bootcmd:
- echo 192.168.1.130 us.archive.ubuntu.com > /etc/hosts
- [ cloud-init-per, once, mymkfs, mkfs, /dev/vdb ]
# cloud_config_modules:
# default:

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