From 925cece6ba20bcf90724b53815953725b6cae2a2 Mon Sep 17 00:00:00 2001 From: Dohyung Kim Date: Tue, 21 Nov 2017 15:45:37 +0900 Subject: [PATCH] if post scripts fails, CreatorError is raised to catch script errors such as signing error $ cat PLATFORM.ks ... %post # if you want to catch script error, add set command and trap command at the top of script set -e trap 'echo KS POST ERROR: command \"$BASH_COMMAND\" failed with error code $?' ERR # it will cause an error /usr/bin/error_command %end ... $ sudo mic cr auto PLATFORM.ks ... INFO: Running post scripts ... INFO: /tmp/ks-postscript-FShzJp: line 5: /usr/bin/error_command: No such file or directory INFO: KS POST ERROR: command "/usr/bin/error_command" failed with error code 1 INFO: ERROR: Failed to execute %post script with '/bin/sh' Change-Id: I1a34a343b8bdd044ca85939e446c43cce783c900 Signed-off-by: Dohyung Kim --- mic/imager/baseimager.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mic/imager/baseimager.py b/mic/imager/baseimager.py index 257af8e..d9bc2cf 100755 --- a/mic/imager/baseimager.py +++ b/mic/imager/baseimager.py @@ -1210,6 +1210,9 @@ class BaseImageCreator(object): dir = self._instroot + "/tmp") s.script = s.script.replace("\r", "") os.write(fd, s.script) + if s.interp == '/bin/sh' or s.interp == '/bin/bash': + os.write(fd, '\n') + os.write(fd, 'exit 0\n') os.close(fd) os.chmod(path, 0700) for item in os.listdir(self._imgdir): @@ -1226,6 +1229,9 @@ class BaseImageCreator(object): stderr = subprocess.STDOUT) while p.poll() == None: msger.info(p.stdout.readline().strip()) + if p.returncode != 0: + raise CreatorError("Failed to execute %%sign script " + "with '%s'" % (s.interp)) except OSError, (err, msg): raise CreatorError("Failed to execute %%sign script " "with '%s' : %s" % (s.interp, msg)) @@ -1246,6 +1252,9 @@ class BaseImageCreator(object): s.script = s.script.replace("\r", "") os.write(fd, s.script) + if s.interp == '/bin/sh' or s.interp == '/bin/bash': + os.write(fd, '\n') + os.write(fd, 'exit 0\n') os.close(fd) os.chmod(path, 0700) @@ -1273,6 +1282,9 @@ class BaseImageCreator(object): end_time = time.time() if (end_time - start_time)/60 > MAX_RUN_TIME: raise CreatorError("Your post script is executed more than "+MAX_RUN_TIME+"mins, please check it!") + if p.returncode != 0: + raise CreatorError("Failed to execute %%post script " + "with '%s'" % (s.interp)) except OSError, (err, msg): raise CreatorError("Failed to execute %%post script " "with '%s' : %s" % (s.interp, msg)) -- 2.7.4