def get_environment_tar(self): '''return the environment.tar generated with the Singularity software. We first try the Linux Filesystem expected location in /usr/libexec If not found, we detect the system archicture dirname $(singularity selftest 2>&1 | grep 'lib' | awk '{print $4}' | sed -e 's@\(.*/singularity\).*@\1@') ''' from sregistry.utils import ( which, run_command ) # First attempt - look at File System Hierarchy Standard (FHS) res = which('singularity')['message'] libexec = res.replace('/bin/singularity','') envtar = '%s/libexec/singularity/bootstrap-scripts/environment.tar' %libexec if os.path.exists(envtar): return envtar # Second attempt, debian distribution will identify folder try: res = which('dpkg-architecture')['message'] if res is not None: cmd = ['dpkg-architecture', '-qDEB_HOST_MULTIARCH'] triplet = run_command(cmd)['message'].strip('\n') envtar = '/usr/lib/%s/singularity/bootstrap-scripts/environment.tar' %triplet if os.path.exists(envtar): return envtar except: pass # Final, return environment.tar provided in package return "%s/environment.tar" %os.path.abspath(os.path.dirname(__file__))
def test_which(): print("Testing utils.which") from sregistry.utils import which result = which('echo') assert result['message'].endswith('echo') assert result['return_code'] == 0 result = which('singularityaaaa') assert result['message'] == '' assert result['return_code'] == 1
def test_which(): print("Testing utils.which") from sregistry.utils import which result = which("echo") assert result["message"].endswith("echo") assert result["return_code"] == 0 result = which("singularityaaaa") assert result["message"] == "" assert result["return_code"] == 1
def get_metadata(self, image_file, names=None): '''extract metadata using Singularity inspect, if the executable is found. If not, return a reasonable default (the parsed image name) Parameters ========== image_file: the full path to a Singularity image names: optional, an extracted or otherwise created dictionary of variables for the image, likely from utils.parse_image_name ''' if names is None: names = {} metadata = {} # We can't return anything without image_file or names if image_file: if not os.path.exists(image_file): bot.error('Cannot find %s.' % image_file) return names or metadata # The user provided a file, but no names if not names: names = parse_image_name(remove_uri(image_file)) # Look for the Singularity Executable singularity = which('singularity')['message'] # Inspect the image, or return names only if os.path.exists(singularity) and image_file: from spython.main import Client as Singularity # Store the original quiet setting is_quiet = Singularity.quiet # We try and inspect, but not required (wont work within Docker) try: Singularity.quiet = True updates = Singularity.inspect(image=image_file) except: bot.warning( 'Inspect command not supported, metadata not included.') updates = None # Restore the original quiet setting Singularity.quiet = is_quiet # Try loading the metadata if updates is not None: try: updates = json.loads(updates) metadata.update(updates) except: pass metadata.update(names) return metadata
def get_metadata(self, image_file, names={}): '''extract metadata using Singularity inspect, if the executable is found. If not, return a reasonable default (the parsed image name) Parameters ========== image_file: the full path to a Singularity image names: optional, an extracted or otherwise created dictionary of variables for the image, likely from utils.parse_image_name ''' metadata = dict() # We can't return anything without image_file or names if image_file is not None: if not os.path.exists(image_file): bot.error('Cannot find %s.' % image_file) return names # The user provided a file, but no names if not names: names = parse_image_name(remove_uri(image_file)) # Look for the Singularity Executable singularity = which('singularity')['message'] # Inspect the image, or return names only if os.path.exists(singularity) and image_file is not None: from sregistry.client import Singularity cli = Singularity() updates = cli.inspect(image_path=image_file, quiet=True) # Try loading the metadata if updates is not None: try: updates = json.loads(updates) metadata.update(updates) except: pass metadata.update(names) return metadata
def get_metadata(self, image_file, names=None): """extract metadata using Singularity inspect, if the executable is found. If not, return a reasonable default (the parsed image name) Parameters ========== image_file: the full path to a Singularity image names: optional, an extracted or otherwise created dictionary of variables for the image, likely from utils.parse_image_name """ if names is None: names = {} metadata = {} # We can't return anything without image_file or names if image_file: if not os.path.exists(image_file): bot.error("Cannot find %s." % image_file) return names or metadata # The user provided a file, but no names if not names: names = parse_image_name(remove_uri(image_file)) # Look for the Singularity Executable singularity = which("singularity")["message"] # Inspect the image, or return names only if os.path.exists(singularity) and image_file: from spython.main import Client as Singularity # Store the original quiet setting is_quiet = Singularity.quiet # We try and inspect, but not required (wont work within Docker) try: Singularity.quiet = True updates = Singularity.inspect(image=image_file) except: bot.warning( "Inspect command not supported, metadata not included.") updates = None # Restore the original quiet setting Singularity.quiet = is_quiet # Try loading the metadata if updates is not None: try: updates = json.loads(updates) # Singularity 3.x bug with missing top level if "data" in updates: updates = updates["data"] metadata.update(updates) # Flatten labels if "attributes" in metadata: if "labels" in metadata["attributes"]: metadata.update(metadata["attributes"]["labels"]) del metadata["attributes"] except: pass metadata.update(names) # Add the type to the container metadata["type"] = "container" return metadata