diff options
author | Scott Moser <smoser@nelson> | 2010-01-06 12:34:22 -0500 |
---|---|---|
committer | Scott Moser <smoser@nelson> | 2010-01-06 12:34:22 -0500 |
commit | 86b6aad19f3796402dbc2f6e90a47081e8b309a0 (patch) | |
tree | de14f870ddc2d96a824ddcf65be172fe3a0cbd0c | |
parent | 42f6a1f2ab248527c3df15dbead50d7a69fc9585 (diff) | |
download | vyos-cloud-init-86b6aad19f3796402dbc2f6e90a47081e8b309a0.tar.gz vyos-cloud-init-86b6aad19f3796402dbc2f6e90a47081e8b309a0.zip |
add semaphore utilities to ec2init module
-rw-r--r-- | ec2init/__init__.py | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/ec2init/__init__.py b/ec2init/__init__.py index 60a87275..77f96760 100644 --- a/ec2init/__init__.py +++ b/ec2init/__init__.py @@ -22,8 +22,12 @@ from configobj import ConfigObj import boto.utils import cPickle +import sys +import os.path +import errno datadir = '/var/lib/cloud/data' +semdir = '/var/lib/cloud/sem' cachedir = datadir + '/cache' user_data = datadir + '/user-data.txt' user_data_raw = datadir + '/user-data.raw' @@ -93,4 +97,47 @@ def decomp_str(str): # preprocess the user data (include / uncompress) def preprocess_user_data(ud): - return(decomp_str(ud)) + return(decomp_str(ud)) + +def sem_getpath(name,freq): + # TODO: freqtok must represent "once-per-instance" somehow + freqtok = freq + return("%s/%s.%s" % (semdir,name,freqtok)) + +def sem_has_run(name,freq): + semfile = sem_getpath(name,freq) + if os.path.exists(semfile): + return True + return False + +def sem_acquire(name,freq): + from time import time + semfile = sem_getpath(name,freq) + + try: + os.makedirs(os.path.dirname(semfile)) + except OSError as e: + if e.errno != errno.EEXIST: + raise e + + if os.path.exists(semfile): + return False + + # race condition + try: + f = open(semfile,"w") + f.write(str(time())) + f.close() + except: + return(False) + return(True) + +def sem_clear(name,freq): + semfile = sem_getpath(name,freq) + try: + os.unlink(semfile) + except OSError as e: + if e.errno != errno.ENOENT: + return False + + return True |