Skip to content

Commit

Permalink
add checks for whether nvidia-docker2 is installed
Browse files Browse the repository at this point in the history
Fixes #88
  • Loading branch information
tfoote committed Nov 19, 2020
1 parent 3585298 commit e519430
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/rocker/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,31 @@ class DependencyMissing(RuntimeError):
pass


class PrerequisiteCheckError(RuntimeError):
pass


class RockerExtension(object):
"""The base class for Rocker extension points"""

def precondition_environment(self, cliargs):
"""Modify the local environment such as setup tempfiles"""
pass

def validate_environment(self, cliargs):
""" Check that the environment is something that can be used.
def check_build_prerequisites(self, cliargs):
""" Check that the environment is something that can be used for the build.
This will check that we're on the right base OS and that the
necessary resources are available, like hardware.
Raises PrerequisiteCheckError on failure
"""
pass

def check_run_prerequisites(self, cliargs):
""" Check that the environment is something that can be used for running the container.
This will check that we're on the right base OS and that the
necessary resources are available, like hardware."""
necessary resources are available, like hardware.
Raises PrerequisiteCheckError on failure
"""
pass

def get_preamble(self, cliargs):
Expand Down Expand Up @@ -203,6 +217,14 @@ def __init__(self, active_extensions, cliargs, base_image):
self.image_id = None

def build(self, **kwargs):
# Check prerequisites
for e in self.active_extensions:
try:
e.check_build_prerequisites(self.cliargs)
except PrerequisiteCheckError as ex:
print("Failed to validate prerequisites to build for extension [%s] with error: %s\nNot executing run." % (e.get_name(), ex))
return 1

with tempfile.TemporaryDirectory() as td:
df = os.path.join(td, 'Dockerfile')
print("Writing dockerfile to %s" % df)
Expand Down Expand Up @@ -240,10 +262,14 @@ def run(self, command='', **kwargs):

for e in self.active_extensions:
try:
e.check_run_prerequisites(self.cliargs)
e.precondition_environment(self.cliargs)
except PrerequisiteCheckError as ex:
print("Failed to validate prerequisites to run for extension [%s] with error: %s\nNot executing run." % (e.get_name(), ex))
return 1
except subprocess.CalledProcessError as ex:
print("Failed to precondition for extension [%s] with error: %s\ndeactivating" % (e.get_name(), ex))
# TODO(tfoote) remove the extension from the list
print("Failed to precondition environment for extension [%s] with error: %s\nNot executing run." % (e.get_name(), ex))
return 1


docker_args = ''
Expand Down
19 changes: 19 additions & 0 deletions src/rocker/nvidia_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from .extensions import name_to_argument
from .core import get_docker_client
from .core import RockerExtension
from .core import PrerequisiteCheckError

def get_docker_version():
docker_version_raw = get_docker_client().version()['Version']
Expand Down Expand Up @@ -125,6 +126,24 @@ def get_docker_args(self, cliargs):
return " --gpus all"
return " --runtime=nvidia"

def check_run_prerequisites(self, cliargs):
nvidia_flag = ''
if get_docker_version() >= Version("19.03"):
nvidia_flag = " --gpus all"
else:
nvidia_flag = " --runtime=nvidia"

cmd = 'docker run --rm %s nvidia/cuda:11.0-base nvidia-smi' % nvidia_flag
try:
subprocess.check_call(cmd.split())
except:
errstr = 'Failed to detect nvidia hardware.'
if get_docker_version() >= Version("19.03"):
errstr += ' Is nvidia-container-toolkit installed?'
else:
errstr += 'Is nvidia-docker2 installed?'
raise PrerequisiteCheckError(errstr)

@staticmethod
def register_arguments(parser, defaults={}):
parser.add_argument(name_to_argument(Nvidia.get_name()),
Expand Down

0 comments on commit e519430

Please sign in to comment.