summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog3
-rwxr-xr-xcloud-init-run-module.py85
-rw-r--r--cloudinit/CloudConfig/cc_bootcmd.py4
-rw-r--r--cloudinit/execute.py29
-rwxr-xr-xdebian.trunk/rules1
-rw-r--r--doc/examples/cloud-config.txt6
-rwxr-xr-xsetup.py2
-rwxr-xr-xtools/cloud-init-per60
8 files changed, 73 insertions, 117 deletions
diff --git a/ChangeLog b/ChangeLog
index e0cec684..02c9e1ff 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
diff --git a/cloud-init-run-module.py b/cloud-init-run-module.py
deleted file mode 100755
index 87fee4f7..00000000
--- a/cloud-init-run-module.py
+++ /dev/null
@@ -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()
diff --git a/cloudinit/CloudConfig/cc_bootcmd.py b/cloudinit/CloudConfig/cc_bootcmd.py
index 11e9938c..5a9e4356 100644
--- a/cloudinit/CloudConfig/cc_bootcmd.py
+++ b/cloudinit/CloudConfig/cc_bootcmd.py
@@ -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")
diff --git a/cloudinit/execute.py b/cloudinit/execute.py
deleted file mode 100644
index 17c3ad69..00000000
--- a/cloudinit/execute.py
+++ /dev/null
@@ -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 ))
diff --git a/debian.trunk/rules b/debian.trunk/rules
index 200b7abb..95bff792 100755
--- a/debian.trunk/rules
+++ b/debian.trunk/rules
@@ -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.
diff --git a/doc/examples/cloud-config.txt b/doc/examples/cloud-config.txt
index d67b572c..4f621274 100644
--- a/doc/examples/cloud-config.txt
+++ b/doc/examples/cloud-config.txt
@@ -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:
diff --git a/setup.py b/setup.py
index b18e2975..a01f2501 100755
--- a/setup.py
+++ b/setup.py
@@ -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/*')),
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