Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add checks for whether nvidia-docker2 is installed #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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