From ef9e8b0e037ac8112b6964461175a1e12a274189 Mon Sep 17 00:00:00 2001 From: Vanessa Sochat Date: Wed, 6 Mar 2019 13:20:49 -0500 Subject: [PATCH] fixing instance name missing when not started --- CHANGELOG.md | 2 ++ spython/instance/__init__.py | 22 +++++++++++++++++----- spython/instance/cmd/__init__.py | 2 +- spython/instance/cmd/start.py | 7 +++---- spython/version.py | 2 +- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17f6c1eb..1f9e692f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ The client here will eventually be released as "spython" (and eventually to singularity on pypi), and the versions here will coincide with these releases. ## [master](https://github.com/singularityhub/singularity-cli/tree/master) + - Fixing bug with instance not having name when not started (0.0.56) + - instance start has been moved to non-private - Added ability for exec and run to return the full output and message (0.0.55) - By default, oci commands (pause, resume, kill) return the return value - Added support and tests for OCI image command group (0.0.54) diff --git a/spython/instance/__init__.py b/spython/instance/__init__.py index 7fd4b97c..6cca5dd4 100644 --- a/spython/instance/__init__.py +++ b/spython/instance/__init__.py @@ -10,7 +10,7 @@ class Instance(ImageBase): - def __init__(self, image, start=True, **kwargs): + def __init__(self, image, start=True, name=None, **kwargs): '''An instance is an image running as an instance with services. This class has functions appended under cmd/__init__ and is instantiated when the user calls Client. @@ -19,10 +19,12 @@ def __init__(self, image, start=True, **kwargs): ========== image: the Singularity image uri to parse (required) start: boolean to start the instance (default is True) - + name: a name for the instance (will generate RobotName + if not provided) ''' super(ImageBase, self).__init__() self.parse_image_name(image) + self.generate_name(name) # Update metadats from arguments self._update_metadata(kwargs) @@ -31,10 +33,20 @@ def __init__(self, image, start=True, **kwargs): # Start the instance if start is True: - self._start(**kwargs) + self.start(**kwargs) # Unique resource identifier + def generate_name(self, name=None): + '''generate a Robot Name for the instance to use, if the user doesn't + supply one. + ''' + # If no name provided, use robot name + if name == None: + name = self.RobotNamer.generate() + self.name = name.replace('-','_') + + def parse_image_name(self, image): ''' simply split the uri from the image. Singularity handles @@ -62,7 +74,7 @@ def _update_metadata(self, kwargs=None): ''' # If not given metadata, use instance.list to get it for container - if kwargs is None: + if kwargs == None and hasattr(self, 'name'): kwargs = self._list(self.name, quiet=True, return_json=True) # Add acceptable arguments @@ -79,7 +91,7 @@ def _update_metadata(self, kwargs=None): def __str__(self): - if hasattr(self, 'uri'): + if hasattr(self, 'name'): if self.uri: return "%s%s" %(self.uri, self.name) return os.path.basename(self._image) diff --git a/spython/instance/cmd/__init__.py b/spython/instance/cmd/__init__.py index db151978..ea09a42b 100644 --- a/spython/instance/cmd/__init__.py +++ b/spython/instance/cmd/__init__.py @@ -30,7 +30,7 @@ def generate_instance_commands(): Instance._run_command = run_command Instance._list = instances # list command is used to get metadata Instance._println = println - Instance._start = start # intended to be called on init, not by user + Instance.start = start # intended to be called on init, not by user Instance.stop = stop # Give an instance the ability to breed :) diff --git a/spython/instance/cmd/start.py b/spython/instance/cmd/start.py index 2c0e4260..044e2a92 100644 --- a/spython/instance/cmd/start.py +++ b/spython/instance/cmd/start.py @@ -29,10 +29,9 @@ def start(self, image=None, name=None, args=None, sudo=False, options=[], captur check_install ) check_install() - # If no name provided, give it an excellent one! - if name is None: - name = self.RobotNamer.generate() - self.name = name.replace('-','_') + # If name provided, over write robot (default) + if name != None: + self.name = name # If an image isn't provided, we have an initialized instance if image is None: diff --git a/spython/version.py b/spython/version.py index 38472069..fe5cefb9 100644 --- a/spython/version.py +++ b/spython/version.py @@ -6,7 +6,7 @@ # with this file, You can obtain one at http://mozilla.org/MPL/2.0/. -__version__ = "0.0.55" +__version__ = "0.0.56" AUTHOR = 'Vanessa Sochat' AUTHOR_EMAIL = 'vsochat@stanford.edu' NAME = 'spython'