Skip to content

Commit 1a4a940

Browse files
committed
adding instances.py to list instances, with error codes 0/255/1
1 parent cad19f0 commit 1a4a940

File tree

7 files changed

+100
-11
lines changed

7 files changed

+100
-11
lines changed

spython/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ This will briefly outline the content of the folders here.
55

66
- [main](main) holds the primary client functions to interact with Singularity (e.g., exec, run, pull), and the subfolders within represent command groups (e.g., instance, image) along with the base of the client ([base](main/base)). This folder is where **commands** go!
77
- [image](image) is a class that represents and holds an image object, in the case that the user wants to initialize a client with an image. This holds the ImageClass, which is only needed to instantiate an image.
8-
- [instance](instance) is a class that represents and holds an instance object. The user can instantiate the class, and then run it's sub functions to start, stop, etc. The higher level "list" command is provided on the level of the client.
8+
- [instance](instance) is a class that represents and holds an instance object. The user can instantiate the class, and then run it's sub functions to interact with it. The higher level "list" command is provided on the level of the client.
99
- [cli](cli): is the actual entry point that connects the user to the python API client from the command line. The user inputs are parsed, and then passed into functions from main.
1010

1111
## Supporting

spython/image/cmd/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class ImageClient(object):
3838
group = "image"
3939

4040
from spython.main.base.logger import println
41-
from spython.main.base.commands import ( init_command, run_command )
41+
from spython.main.base.command import ( init_command, run_command )
4242
from .utils import ( compress, decompress )
4343
from .create import create
4444
from .importcmd import importcmd

spython/instance/__init__.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,25 @@
1717

1818

1919
from spython.image import ImageBase
20-
from spython.instance.cmd import *
2120

2221
class Instance(ImageBase):
2322

24-
def __init__(self, image=None):
23+
def __init__(self, image, start=True, **kwargs):
2524
'''An instance is an image running as an instance with services.
25+
This class has functions appended under cmd/__init__ and is
26+
instantiated when the user calls Client.
2627
2728
Parameters
2829
==========
29-
image: the image uri to parse (required)
30+
image: the Singularity image uri to parse (required)
31+
start: boolean to start the instance (default is True)
3032
3133
'''
3234
super(ImageBase, self).__init__()
3335
self.parse_image_name(image)
36+
self.status = 'stopped'
37+
38+
# Start the instance
39+
if start is True:
40+
self.start(**kwargs)
41+

spython/main/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ def get_client(quiet=False, debug=False):
5454
Client.pull = pull
5555

5656
# Command Groups
57-
from spython.image.cmd import image_group # deprecated image commands
58-
from spython.instance import Instance # returns Instance objects
57+
from spython.image.cmd import image_group # deprecated image commands
58+
from spython.instance.cmd import instance_group # returns Instance objects
5959
Client.image = image_group
6060
Client.image.check_install = Client.check_install
61-
Client.instance = Instance
61+
Client.instance = instance_group
6262

6363
# Initialize
6464
cli = Client()

spython/main/instances.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
# Copyright (C) 2018 The Board of Trustees of the Leland Stanford Junior
3+
# University.
4+
# Copyright (C) 2017-2018 Vanessa Sochat.
5+
6+
# This program is free software: you can redistribute it and/or modify it
7+
# under the terms of the GNU Affero General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or (at your
9+
# option) any later version.
10+
11+
# This program is distributed in the hope that it will be useful, but WITHOUT
12+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
14+
# License for more details.
15+
16+
# You should have received a copy of the GNU Affero General Public License
17+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
20+
from spython.logger import bot
21+
from spython.utils import run_command
22+
23+
24+
def instances(self):
25+
'''list instances. For Singularity, this is provided as a command sub
26+
group.
27+
28+
singularity instance.list
29+
30+
Return codes provided are different from standard linux:
31+
see https://github.com/singularityware/singularity/issues/1706
32+
33+
Return Code -- Reason
34+
0 -- Instances Found
35+
1 -- No Instances, libexecdir value not found, functions file not found
36+
255 -- Couldn't get UID
37+
38+
'''
39+
40+
self.check_install()
41+
42+
cmd = self._init_command('instance.list')
43+
output = run_command(cmd, quiet=True)
44+
instances = None
45+
46+
# Success, we have instances
47+
48+
if output['return_code'] == 0:
49+
print(''.join(output['message']))
50+
instances = output['message'][1:]
51+
52+
# Couldn't get UID
53+
54+
elif output['return_code'] == 255:
55+
bot.error("Couldn't get UID")
56+
57+
58+
# Return code of 0
59+
else:
60+
bot.info('No instances found.')
61+
62+
return instances

spython/utils/__init__.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
1-
from .fileio import *
2-
from .terminal import *
1+
from .fileio import (
2+
mkdir_p,
3+
write_file,
4+
write_json,
5+
read_file,
6+
read_json
7+
)
8+
9+
from .terminal import (
10+
check_install,
11+
get_installdir,
12+
stream_command,
13+
run_command,
14+
format_container_name,
15+
remove_uri
16+
)

spython/utils/terminal.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,12 @@ def stream_command(cmd, no_newline_regexp="Progess", sudo=False):
9595
raise subprocess.CalledProcessError(return_code, cmd)
9696

9797

98-
def run_command(cmd, sudo=False, capture=True, no_newline_regexp="Progess", quiet=False):
98+
def run_command(cmd,
99+
sudo=False,
100+
capture=True,
101+
no_newline_regexp="Progess",
102+
quiet=False):
103+
99104
'''run_command uses subprocess to send a command to the terminal. If
100105
capture is True, we use the parent stdout, so the progress bar (and
101106
other commands of interest) are piped to the user. This means we

0 commit comments

Comments
 (0)