summaryrefslogtreecommitdiff
path: root/cloudinit/CloudConfig
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2011-02-07 14:42:03 -0500
committerScott Moser <smoser@ubuntu.com>2011-02-07 14:42:03 -0500
commite2045d8c68d0e7086487f3a02b9512e6f2581009 (patch)
tree11c8fc99066267b2a0177332bdc95dc5cfcbbe77 /cloudinit/CloudConfig
parent6e5094de9d87bc69970ff813956886c548211f40 (diff)
downloadvyos-cloud-init-e2045d8c68d0e7086487f3a02b9512e6f2581009.tar.gz
vyos-cloud-init-e2045d8c68d0e7086487f3a02b9512e6f2581009.zip
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.
Diffstat (limited to 'cloudinit/CloudConfig')
-rw-r--r--cloudinit/CloudConfig/__init__.py12
1 files changed, 12 insertions, 0 deletions
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