Setup environment to perform signing script
authorSoonKyu Park <sk7.park@samsung.com>
Wed, 8 Nov 2017 06:58:24 +0000 (15:58 +0900)
committeryuhuan.yang <yuhuan.yang@samsung.com>
Fri, 26 Jan 2018 05:52:27 +0000 (13:52 +0800)
Change-Id: I8b7a5916a6db8863a03b24d297a479e0d990adad

mic/bootstrap.py
mic/imager/baseimager.py

index 5cfa8c7..6fbc495 100644 (file)
@@ -244,6 +244,34 @@ class Bootstrap(object):
             except:
                 pass
 
+        def sync_hostfile(rootdir):
+            try:
+                # sync host info to bootstrap
+                if os.path.exists(rootdir + "/etc/hosts"):
+                    os.unlink(rootdir + "/etc/hosts")
+                shutil.copyfile("/etc/hosts", rootdir + "/etc/hosts")
+            except:
+                pass
+
+        def sync_signfile(rootdir,homedir):
+            if os.path.exists(homedir + "/.sign"):
+                signfile = rootdir + homedir + "/.sign"
+                try:
+                    # sync sign info to bootstrap
+                    if os.path.exists(signfile):
+                        if os.path.isdir(signfile):
+                            shutil.rmtree(signfile)
+                        else:
+                            os.unlink(signfile)
+                    try:
+                        shutil.copytree(homedir + "/.sign", signfile)
+                    except OSError as exc:
+                        if exc.errno == errno.ENOTDIR:
+                            shutil.copy(homedir + "/.sign", signfile)
+                        else: raise
+                except:
+                    pass
+
         if not rootdir:
             rootdir = self.rootdir
 
@@ -262,6 +290,12 @@ class Bootstrap(object):
             gloablmounts = setup_chrootenv(rootdir, bindmounts)
             sync_timesetting(rootdir)
             sync_passwdfile(rootdir)
+            sync_hostfile(rootdir)
+            env['SUDO_USER'] = 'root'
+            if 'SUDO_USER' in env:
+                sync_signfile(rootdir, os.path.expanduser('~' + env['SUDO_USER']))
+            else:
+                sync_signfile(rootdir, os.path.expanduser('~'))
             retcode = subprocess.call(cmd, preexec_fn=mychroot, env=env, shell=shell)
         except (OSError, IOError):
             # add additional information to original exception
index c02e74d..843fdd5 100755 (executable)
@@ -90,6 +90,7 @@ class BaseImageCreator(object):
         self.pack_to = None
         self.repourl = {}
         self.multiple_partitions = False
+        self._imgdir = None
 
         # If the kernel is save to the destdir when copy_kernel cmd is called.
         self._need_copy_kernel = False
@@ -1154,6 +1155,35 @@ class BaseImageCreator(object):
     def postinstall(self):
         self.copy_attachment()
 
+    def _get_sign_scripts_env(self):
+        """Return an environment dict for %post-umount scripts.
+
+        This is the hook where subclasses may specify some environment
+        variables for %post-umount scripts by return a dict containing the
+        desired environment.
+        """
+
+        env = {}
+
+        # Directory path of images
+        if self._imgdir:
+            env['IMG_DIR_PATH'] = str(self._imgdir)
+
+        imgfiles = []
+        imgpaths = []
+        for item in self._instloops:
+            imgfiles.append(item['name'])
+            if self._imgdir:
+                imgpaths.append(os.path.join(self._imgdir, item['name']))
+
+        # Images file name
+        env['IMG_FILES'] = ' '.join(imgfiles)
+
+        # Absolute path of images
+        env['IMG_PATHS'] = ' '.join(imgpaths)
+
+        return env
+
     def run_sign_scripts(self):
         if kickstart.get_sign_scripts(self.ks)==[]:
             return
@@ -1171,6 +1201,7 @@ class BaseImageCreator(object):
                 os.write(fd, 'exit 0\n')
             os.close(fd)
             os.chmod(path, 0700)
+
             for item in os.listdir(self._imgdir):
                 sub = os.path.splitext(item)[1]
                 if sub == ".img":
@@ -1178,9 +1209,14 @@ class BaseImageCreator(object):
                                 os.path.join(self._instroot + "/tmp", item))
             oldoutdir = os.getcwd()
             os.chdir(self._instroot + "/tmp")
+
+            env = self._get_sign_scripts_env()
+            #*.img files are moved to self._instroot + "/tmp" directory in running runscripts
+            env['IMG_PATHS'] = env['IMG_PATHS'].replace(self._imgdir,self._instroot + "/tmp")
             try:
                 try:
                     p = subprocess.Popen([s.interp, path],
+                                       env = env,
                                        stdout = subprocess.PIPE,
                                        stderr = subprocess.STDOUT)
                     while p.poll() == None:
@@ -1197,6 +1233,7 @@ class BaseImageCreator(object):
                 for item in os.listdir(self._instroot + "/tmp"):
                     shutil.move(os.path.join(self._instroot + "/tmp", item),
                                 os.path.join(self._imgdir, item))
+
     def __run_post_scripts(self):
         msger.info("Running post scripts ...")
         if os.path.exists(self._instroot + "/tmp"):