if post scripts fails, CreatorError is raised to catch script errors such as signing... 94/163694/1
authorDohyung Kim <dohyung2.kim@samsung.com>
Tue, 21 Nov 2017 06:45:37 +0000 (15:45 +0900)
committerpark <sk7.park@samsung.com>
Wed, 13 Dec 2017 02:24:28 +0000 (02:24 +0000)
  $ 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 <dohyung2.kim@samsung.com>
mic/imager/baseimager.py

index 9d61663..c02e74d 100755 (executable)
@@ -1166,6 +1166,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):
@@ -1182,6 +1185,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))
@@ -1202,6 +1208,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)
 
@@ -1229,6 +1238,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))