From e2045d8c68d0e7086487f3a02b9512e6f2581009 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Mon, 7 Feb 2011 14:42:03 -0500 Subject: add 'run_per_instance' to CloudConfig This method aloows the caller to run easily run something "once per instance". Its location in CloudConfig rather than 'util' is really only because it needs access to cloudinit.get_ipath_cur to get the 'data' path. --- cloudinit/CloudConfig/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'cloudinit/CloudConfig/__init__.py') diff --git a/cloudinit/CloudConfig/__init__.py b/cloudinit/CloudConfig/__init__.py index 7b01f00a..c8743ac3 100644 --- a/cloudinit/CloudConfig/__init__.py +++ b/cloudinit/CloudConfig/__init__.py @@ -23,6 +23,7 @@ import sys import traceback import os import subprocess +import time per_instance="once-per-instance" per_always="always" @@ -219,3 +220,14 @@ def redirect_output(outfmt,errfmt, o_out=sys.stdout, o_err=sys.stderr): if o_err: os.dup2(new_fp.fileno(), o_err.fileno()) return + +def run_per_instance(name, func, args, clear_on_fail=False): + semfile = "%s/%s" % (cloudinit.get_ipath_cur("data"),name) + if os.path.exists(semfile): return + + util.write_file(semfile,str(time.time())) + try: + func(*args) + except: + if clear_on_fail: os.unlink(semfile) + raise -- cgit v1.2.3