Beispiel #1
0
 def test_check_get_singularity_version(self):
     '''check that the singularity version is found to be that installed'''
     print("Testing utils.get_singularity_version")
     from spython.utils import get_singularity_version
     version = get_singularity_version()
     self.assertTrue(version != "")
     os.environ['SPYTHON_SINGULARITY_VERSION'] = "3.0"
     version = get_singularity_version()
     self.assertTrue(version == "3.0")
Beispiel #2
0
def test_check_get_singularity_version():
    """check that the singularity version is found to be that installed"""
    from spython.utils import get_singularity_version

    version = get_singularity_version()
    assert version != ""
    with ScopedEnvVar("SPYTHON_SINGULARITY_VERSION", "3.0"):
        version = get_singularity_version()
    assert version == "3.0"
Beispiel #3
0
def get(self, name, return_json=False, quiet=False):
    '''get is a list for a single instance. It is assumed to be running,
       and we need to look up the PID, etc.
    '''
    from spython.utils import (check_install, get_singularity_version)
    check_install()

    # Ensure compatible for singularity prior to 3.0, and after 3.0
    subgroup = "instance.list"
    if (get_singularity_version().find("version 3") != -1):
        subgroup = ["instance", "list"]

    cmd = self._init_command(subgroup)

    cmd.append(name)
    output = run_command(cmd, quiet=True)

    # Success, we have instances

    if output['return_code'] == 0:

        # Only print the table if we are returning json
        if quiet is False:
            print(''.join(output['message']))

        # Prepare json result from table

        header = ['daemon_name', 'pid', 'container_image']
        instances = parse_table(output['message'][0], header)

        # Does the user want instance objects instead?
        listing = []
        if return_json is False:
            for i in instances:
                new_instance = Instance(pid=i['pid'],
                                        name=i['daemon_name'],
                                        image=i['container_image'],
                                        start=False)

                listing.append(new_instance)
            instances = listing

    # Couldn't get UID

    elif output['return_code'] == 255:
        bot.error("Couldn't get UID")

    # Return code of 0
    else:
        bot.info('No instances found.')

    # If we are given a name, return just one
    if name is not None and len(instances) == 1:
        instances = instances[0]

    return instances
Beispiel #4
0
def stop(self, name=None, sudo=False):
    '''start an instance. This is done by default when an instance is created.

       Parameters
       ==========
       image: optionally, an image uri (if called as a command from Client)
       name: a name for the instance
       sudo: if the user wants to run the command with sudo

       USAGE: 
       singularity [...] instance.start [...] <container path> <instance name>

    '''        
    from spython.utils import ( check_install, 
                                run_command, 
                                get_singularity_version )
    check_install()

    subgroup = 'instance.stop'
    if get_singularity_version().find("version 3"):
        subgroup = ["instance", "stop"]

    cmd = self._init_command(subgroup)

    # If name is provided assume referencing an instance
    instance_name = self.name
    if name is not None:
        instance_name = name     
    cmd = cmd + [instance_name]
    
    output = run_command(cmd, sudo=sudo, quiet=True)

    if output['return_code'] != 0:
        message = '%s : return code %s' %(output['message'], 
                                          output['return_code'])
        bot.error(message)
        return output['return_code']

    return output['return_code']
Beispiel #5
0
 def version(self):
     """Shortcut to get_singularity_version, takes no arguments.
     """
     return get_singularity_version()
Beispiel #6
0
def instances(self, name=None, return_json=False, quiet=False):
    '''list instances. For Singularity, this is provided as a command sub
       group.

       singularity instance.list

       Return codes provided are different from standard linux:
       see https://github.com/singularityware/singularity/issues/1706

       Parameters
       ==========
       return_json: return a json list of instances instead of objects (False)
       name: if defined, return the list for just one instance (used to ged pid)

       Return Code  --   Reason
       0 -- Instances Found
       1 -- No Instances, libexecdir value not found, functions file not found
       255 -- Couldn't get UID

    '''
    from spython.instance.cmd.iutils import parse_table
    from spython.utils import check_install
    check_install()

    subgroup = 'instance.list'
    if get_singularity_version().find("version 3"):
        subgroup = ["instance", "list"]

    cmd = self._init_command(subgroup)

    # If the user has provided a name, we want to see a particular instance
    if name is not None:
        cmd.append(name)

    output = run_command(cmd, quiet=True)
    instances = None

    # Success, we have instances

    if output['return_code'] == 0:

        # Only print the table if we are returning json
        if quiet is False:
            print(''.join(output['message']))

        # Prepare json result from table

        header = ['daemon_name', 'pid', 'container_image']
        instances = parse_table(output['message'][0], header)

        # Does the user want instance objects instead?
        listing = []
        if return_json is False:
            for i in instances:

                new_instance = self.instance(pid=i['pid'],
                                             name=i['daemon_name'],
                                             image=i['container_image'],
                                             start=False)

                listing.append(new_instance)
            instances = listing

    # Couldn't get UID

    elif output['return_code'] == 255:
        bot.error("Couldn't get UID")

    # Return code of 0
    else:
        bot.info('No instances found.')

    # If we are given a name, return just one
    if name is not None and instances is not None:
        if len(instances) == 1:
            instances = instances[0]

    return instances
Beispiel #7
0
def get_client(quiet=False, debug=False):
    """
    get the client and perform imports not on init, in case there are any
    initialization or import errors.

    Parameters
    ==========
    quiet: if True, suppress most output about the client
    debug: turn on debugging mode

    """
    from spython.utils import get_singularity_version
    from .base import Client as client

    client.quiet = quiet
    client.debug = debug

    # Do imports here, can be customized
    from .apps import apps
    from .build import build
    from .execute import execute, shell
    from .help import helpcmd
    from .inspect import inspect
    from .instances import list_instances, stopall  # global instance commands
    from .run import run
    from .pull import pull
    from .export import export, _export

    # Actions
    client.apps = apps
    client.build = build
    client.execute = execute
    client.export = export
    client._export = _export
    client.help = helpcmd
    client.inspect = inspect
    client.instances = list_instances
    client.run = run
    client.shell = shell
    client.pull = pull

    # Command Groups, Images
    from spython.image.cmd import generate_image_commands  # deprecated

    client.image = generate_image_commands()

    # Commands Groups, Instances
    from spython.instance.cmd import (
        generate_instance_commands, )  # instance level commands

    client.instance = generate_instance_commands()
    client.instance_stopall = stopall
    client.instance.version = client.version

    # Commands Groups, OCI (Singularity version 3 and up)
    if "version 3" in get_singularity_version():
        from spython.oci.cmd import generate_oci_commands

        client.oci = generate_oci_commands()(
        )  # first () runs function, second
        # initializes OciImage class
        client.oci.debug = client.debug
        client.oci.quiet = client.quiet
        client.oci.OciImage.quiet = client.quiet
        client.oci.OciImage.debug = client.debug

    # Initialize
    cli = client()

    # Pass on verbosity
    for subclient in [cli.image, cli.instance]:
        subclient.debug = cli.debug
        subclient.quiet = cli.quiet
        subclient.version = cli.version

    return cli
Beispiel #8
0
def start(self, image=None, name=None, sudo=False, options=[], capture=False):
    '''start an instance. This is done by default when an instance is created.

       Parameters
       ==========
       image: optionally, an image uri (if called as a command from Client)
       name: a name for the instance
       sudo: if the user wants to run the command with sudo
       capture: capture output, default is False. With True likely to hang.
       options: a list of tuples, each an option to give to the start command
                [("--bind", "/tmp"),...]

       USAGE: 
       singularity [...] instance.start [...] <container path> <instance name>

    '''
    from spython.utils import (run_command, check_install,
                               get_singularity_version)
    check_install()

    # If no name provided, give it an excellent one!
    if name is None:
        name = self.RobotNamer.generate()
    self.name = name.replace('-', '_')

    # If an image isn't provided, we have an initialized instance
    if image is None:

        # Not having this means it was called as a command, without an image
        if not hasattr(self, "_image"):
            bot.error('Please provide an image, or create an Instance first.')
            sys.exit(1)

        image = self._image

    # Derive subgroup command based on singularity version
    subgroup = 'instance.start'
    if get_singularity_version().find("version 3"):
        subgroup = ["instance", "start"]

    cmd = self._init_command(subgroup)

    # Add options, if they are provided
    if not isinstance(options, list):
        options = options.split(' ')

    # Assemble the command!
    cmd = cmd + options + [image, self.name]

    # Save the options and cmd, if the user wants to see them later
    self.options = options
    self.cmd = cmd

    output = run_command(cmd, sudo=sudo, quiet=True, capture=capture)

    if output['return_code'] == 0:
        self._update_metadata()

    else:
        message = '%s : return code %s' % (output['message'],
                                           output['return_code'])
        bot.error(message)

    return self
Beispiel #9
0
 def version(self):
     '''Shortcut to get_singularity_version, takes no arguments.
     '''
     return get_singularity_version()