def clean_up(files): '''clean up will delete a list of files, only if they exist ''' if not isinstance(files, list): files = [files] for f in files: if os.path.exists(f): bot.verbose3("Cleaning up %s" % f) os.remove(f)
def extract_env(self): '''extract the environment from the manifest, or return None. Used by functions env_extract_image, and env_extract_tar ''' environ = self._get_config('Env') if environ is not None: if not isinstance(environ, list): environ = [environ] lines = [] for line in environ: line = re.findall("(?P<var_name>.+?)=(?P<var_value>.+)", line) line = ['export %s="%s"' % (x[0], x[1]) for x in line] lines = lines + line environ = "\n".join(lines) bot.verbose3("Found Docker container environment!") return environ
def extract_runscript(self): '''extract the runscript (EntryPoint) as first priority, unless the user has specified to use the CMD. If Entrypoint is not defined, we default to None: 1. IF SREGISTRY_DOCKERHUB_CMD is set, use Cmd 2. If not set, or Cmd is None/blank, try Entrypoint 3. If Entrypoint is not set, use default /bin/bash ''' use_cmd = self._get_setting('SREGISTRY_DOCKER_CMD') # Does the user want to use the CMD instead of ENTRYPOINT? commands = ["Entrypoint", "Cmd"] if use_cmd is not None: commands.reverse() # Parse through commands until we hit one for command in commands: cmd = self._get_config(command) if cmd is not None: break # Only continue if command still isn't None if cmd is not None: bot.verbose3("Adding Docker %s as Singularity runscript..." % command.upper()) # If the command is a list, join. (eg ['/usr/bin/python','hello.py'] bot.debug(cmd) if not isinstance(cmd, list): cmd = [cmd] cmd = " ".join(['"%s"' % x for x in cmd]) cmd = 'exec %s "$@"' % cmd cmd = "#!/bin/sh\n\n%s\n" % cmd return cmd bot.debug("CMD and ENTRYPOINT not found, skipping runscript.") return cmd
def create_metadata_tar(self, destination=None, metadata_folder=".singularity.d"): '''create a metadata tar (runscript and environment) to add to the downloaded image. This function uses all functions in this section to obtain key--> values from the manifest config, and write to a .tar.gz Parameters ========== metadata_folder: the metadata folder in the singularity image. default is .singularity.d ''' tar_file = None # We will add these files to it files = [] # Extract and add environment environ = self._extract_env() if environ not in [None, ""]: bot.verbose3('Adding Docker environment to metadata tar') template = get_template('tarinfo') template['name'] = './%s/env/10-docker.sh' % (metadata_folder) template['content'] = environ files.append(template) # Extract and add labels labels = self._extract_labels() if labels is not None: labels = print_json(labels) bot.verbose3('Adding Docker labels to metadata tar') template = get_template('tarinfo') template['name'] = "./%s/labels.json" % metadata_folder template['content'] = labels files.append(template) # Runscript runscript = self._extract_runscript() if runscript is not None: bot.verbose3('Adding Docker runscript to metadata tar') template = get_template('tarinfo') template['name'] = "./%s/runscript" % metadata_folder template['content'] = runscript files.append(template) if len(files) > 0: dest = self._get_download_cache(destination, subfolder='metadata') tar_file = create_tar(files, dest) else: bot.warning("No metadata will be included.") return tar_file