def add_writable_file_volume( self, runtime, # type: List[Text] volume, # type: MapperEnt host_outdir_tgt, # type: Optional[Text] tmpdir_prefix # type: Text ): """Append a writable file mapping to the runtime option list.""" if self.inplace_update: self._add_volume_binding(volume.resolved, volume.target, writable=True) else: if host_outdir_tgt: # shortcut, just copy to the output directory # which is already going to be mounted log.debug('shutil.copy({}, {})'.format(volume.resolved, host_outdir_tgt)) shutil.copy(volume.resolved, host_outdir_tgt) else: log.debug('tempfile.mkdtemp(dir={})'.format(self.tmpdir)) tmpdir = tempfile.mkdtemp(dir=self.tmpdir) file_copy = os.path.join(tmpdir, os.path.basename(volume.resolved)) log.debug('shutil.copy({}, {})'.format(volume.resolved, file_copy)) shutil.copy(volume.resolved, file_copy) self._add_volume_binding(file_copy, volume.target, writable=True) ensure_writable(host_outdir_tgt or file_copy)
def add_writable_directory_volume(self, runtime, # type: List[Text] volume, # type: MapperEnt host_outdir_tgt, # type: Optional[Text] tmpdir_prefix # type: Text ): """Append a writable directory mapping to the runtime option list.""" if volume.resolved.startswith("_:"): # Synthetic directory that needs creating first if not host_outdir_tgt: log.debug('tempfile.mkdtemp(dir={})'.format(self.tmpdir)) new_dir = os.path.join( tempfile.mkdtemp(dir=self.tmpdir), os.path.basename(volume.target)) self._add_volume_binding(new_dir, volume.target, writable=True) elif not os.path.exists(host_outdir_tgt): log.debug('os.makedirs({}, 0o0755)'.format(host_outdir_tgt)) os.makedirs(host_outdir_tgt, 0o0755) else: if self.inplace_update: self._add_volume_binding(volume.resolved, volume.target, writable=True) else: if not host_outdir_tgt: log.debug('tempfile.mkdtemp(dir={})'.format(self.tmpdir)) tmpdir = tempfile.mkdtemp(dir=self.tmpdir) new_dir = os.path.join( tmpdir, os.path.basename(volume.resolved)) log.debug('shutil.copytree({}, {})'.format(volume.resolved, new_dir)) shutil.copytree(volume.resolved, new_dir) self._add_volume_binding(new_dir, volume.target, writable=True) else: log.debug('shutil.copytree({}, {})'.format(volume.resolved, host_outdir_tgt)) shutil.copytree(volume.resolved, host_outdir_tgt) ensure_writable(host_outdir_tgt or new_dir)
def add_volumes(self, pathmapper): """Customize add volumes for REANA system.""" host_outdir = self.outdir container_outdir = self.builder.outdir for src, vol in pathmapper.items(): if not vol.staged: continue if vol.target.startswith(container_outdir + "/"): host_outdir_tgt = os.path.join( host_outdir, vol.target[len(container_outdir) + 1 :] ) else: host_outdir_tgt = None if vol.type in ("File", "Directory"): if not vol.resolved.startswith("_:"): resolved = vol.resolved if not os.path.exists(resolved): resolved = ( "/".join(vol.resolved.split("/")[:-1]) + "/" + vol.target.split("/")[-1] ) self.volumes.append((resolved, vol.target)) elif vol.type == "WritableFile": if self.inplace_update: self.volumes.append((vol.resolved, vol.target)) else: shutil.copy(vol.resolved, host_outdir_tgt) ensure_writable(host_outdir_tgt) if vol.type == "WritableDirectory": if vol.resolved.startswith("_:"): if not os.path.exists(vol.target): os.makedirs(vol.target) else: if self.inplace_update: pass else: shutil.copytree(vol.resolved, host_outdir_tgt) ensure_writable(host_outdir_tgt) elif vol.type == "CreateFile": if host_outdir_tgt: with open(host_outdir_tgt, "wb") as f: f.write(vol.resolved.encode("utf-8")) else: fd, createtmp = tempfile.mkstemp(dir=self.tmpdir) with os.fdopen(fd, "wb") as f: f.write(vol.resolved.encode("utf-8"))
def add_volumes(self, pathmapper, mounts, secret_store=None): # type: (PathMapper, List[Text], SecretStore) -> None host_outdir = self.outdir container_outdir = self.builder.outdir for src, vol in pathmapper.items(): if not vol.staged: continue host_outdir_tgt = None # type: Optional[Text] if vol.target.startswith(container_outdir+"/"): host_outdir_tgt = os.path.join( host_outdir, vol.target[len(container_outdir)+1:]) if vol.type in ("File", "Directory"): if not vol.resolved.startswith("_:"): mounts.append(u"%s:%s" % ( docker_windows_path_adjust(vol.resolved), docker_windows_path_adjust(vol.target))) elif vol.type == "WritableFile": if self.inplace_update: mounts.append(u"%s:%s" % ( docker_windows_path_adjust(vol.resolved), docker_windows_path_adjust(vol.target))) else: if host_outdir_tgt: shutil.copy(vol.resolved, host_outdir_tgt) ensure_writable(host_outdir_tgt) else: raise WorkflowException( "Unable to compute host_outdir_tgt for " "WriteableFile.") elif vol.type == "WritableDirectory": if vol.resolved.startswith("_:"): if host_outdir_tgt: os.makedirs(host_outdir_tgt, 0o0755) else: raise WorkflowException( "Unable to compute host_outdir_tgt for " "WritableDirectory.") else: if self.inplace_update: mounts.append(u"%s:%s" % ( docker_windows_path_adjust(vol.resolved), docker_windows_path_adjust(vol.target))) else: if host_outdir_tgt: shutil.copytree(vol.resolved, host_outdir_tgt) ensure_writable(host_outdir_tgt) else: raise WorkflowException( "Unable to compute host_outdir_tgt for " "WritableDirectory.") elif vol.type == "CreateFile": if secret_store: contents = secret_store.retrieve(vol.resolved) else: contents = vol.resolved if host_outdir_tgt: with open(host_outdir_tgt, "wb") as f: f.write(contents.encode("utf-8")) else: fd, createtmp = tempfile.mkstemp(dir=self.tmpdir) with os.fdopen(fd, "wb") as f: f.write(contents.encode("utf-8")) mounts.append(u"%s:%s" % ( docker_windows_path_adjust(createtmp), docker_windows_path_adjust(vol.target))) return mounts