improve the cloud-init-run-module code a bit, fix LP:#568139
568139 was fixed because the test for "always" was using "is" instead of "=="
This commit is contained in:
parent
e5b6997fcc
commit
853d20bbbb
@ -41,20 +41,17 @@ def main():
|
|||||||
try:
|
try:
|
||||||
cloud.get_data_source()
|
cloud.get_data_source()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print e
|
fail("Failed to get instance data\n\t%s" % traceback.format_exc(),log)
|
||||||
sys.stderr.write("Failed to get instance data")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
if cloud.sem_has_run(semname,freq):
|
if cloud.sem_has_run(semname,freq):
|
||||||
sys.stderr.write("%s already ran %s\n" % (semname,freq))
|
err("%s already ran %s" % (semname,freq),log)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
mod = __import__('cloudinit.' + modname)
|
mod = __import__('cloudinit.' + modname)
|
||||||
inst = getattr(mod,modname)
|
inst = getattr(mod,modname)
|
||||||
except:
|
except:
|
||||||
sys.stderr.write("Failed to load module cloudinit.%s\n" % modname)
|
fail("Failed to load module cloudinit.%s\n" % modname)
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
@ -63,9 +60,21 @@ def main():
|
|||||||
if os.environ.has_key(cfg_env_name):
|
if os.environ.has_key(cfg_env_name):
|
||||||
cfg_path = os.environ[cfg_env_name]
|
cfg_path = os.environ[cfg_env_name]
|
||||||
|
|
||||||
cloud.sem_and_run(semname, freq, inst.run, [run_args,cfg_path], False)
|
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)
|
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__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
@ -277,7 +277,7 @@ class CloudInit:
|
|||||||
return("%s/%s.%s" % (semdir,name,freqtok))
|
return("%s/%s.%s" % (semdir,name,freqtok))
|
||||||
|
|
||||||
def sem_has_run(self,name,freq):
|
def sem_has_run(self,name,freq):
|
||||||
if freq is "always": return False
|
if freq == "always": return False
|
||||||
semfile = self.sem_getpath(name,freq)
|
semfile = self.sem_getpath(name,freq)
|
||||||
if os.path.exists(semfile):
|
if os.path.exists(semfile):
|
||||||
return True
|
return True
|
||||||
@ -293,7 +293,7 @@ class CloudInit:
|
|||||||
if e.errno != errno.EEXIST:
|
if e.errno != errno.EEXIST:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
if os.path.exists(semfile) and freq is not "always":
|
if os.path.exists(semfile) and freq != "always":
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# race condition
|
# race condition
|
||||||
@ -325,7 +325,7 @@ class CloudInit:
|
|||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
if not self.sem_acquire(semname,freq):
|
if not self.sem_acquire(semname,freq):
|
||||||
raise Exception("Failed to acquire lock on %s\n" % semname)
|
raise Exception("Failed to acquire lock on %s" % semname)
|
||||||
|
|
||||||
func(*args)
|
func(*args)
|
||||||
except:
|
except:
|
||||||
|
@ -15,16 +15,15 @@
|
|||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
def run(list,cfg):
|
def run(args,cfg,log):
|
||||||
import subprocess
|
import subprocess
|
||||||
retcode = subprocess.call(list)
|
import traceback
|
||||||
|
try:
|
||||||
if retcode == 0:
|
subprocess.check_call(args)
|
||||||
return
|
return
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
if retcode < 0:
|
log.debug(traceback.format_exc(e))
|
||||||
str="Cmd terminated by signal %s\n" % -retcode
|
raise Exception("Cmd returned %s: %s" % ( e.returncode, args ))
|
||||||
else:
|
except OSError as e:
|
||||||
str="Cmd returned %s\n" % retcode
|
log.debug(traceback.format_exc(e))
|
||||||
str+=' '.join(list)
|
raise Exception("Cmd failed to execute: %s" % ( args ))
|
||||||
raise Exception(str)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user