-
Notifications
You must be signed in to change notification settings - Fork 97
add the docker specific arg primitive. #334
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
Open
aavbsouza
wants to merge
4
commits into
NVIDIA:master
Choose a base branch
from
aavbsouza:docker_arg_keyword
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
19a501f
add the docker specific arg primitive. This primitive return an empty…
aavbsouza e84a259
documentation for the arg primitive
aavbsouza 8f6b483
change the arg primitive to work with singularity and bash containers
aavbsouza 910a56a
fix missing recipe.sh
aavbsouza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# pylint: disable=invalid-name, too-few-public-methods | ||
|
||
"""Arg primitive""" | ||
|
||
from __future__ import absolute_import | ||
from __future__ import unicode_literals | ||
from __future__ import print_function | ||
|
||
import logging # pylint: disable=unused-import | ||
|
||
import hpccm.config | ||
|
||
from hpccm.common import container_type | ||
|
||
class arg(object): | ||
"""The `arg` primitive sets the corresponding environment | ||
variables during the build time of a docker container. | ||
Singularity and "bash" containers does not have a strict version of the | ||
ARG keyword found on Dockerfiles but is possible to simulate | ||
the behavior of this keyword as a build time parameter for the | ||
Singularity and bash containers using environment variables. | ||
|
||
# Parameters | ||
|
||
variables: A dictionary of key / value pairs. The default is an | ||
empty dictionary. | ||
|
||
# Examples | ||
|
||
```python | ||
arg(variables={'HTTP_PROXY':'proxy.example.com', 'NO_PROXY':'example.com'}) | ||
|
||
```bash | ||
SINGULARITYENV_HTTP_PROXY="proxy.example.com" \ | ||
SINGULARITYENV_NO_PROXY="example.com \ | ||
singularity build image.sif recipe.def" | ||
``` | ||
|
||
```bash | ||
HTTP_PROXY="proxy.example.com" \ | ||
NO_PROXY="example.com \ | ||
recipe.sh" | ||
``` | ||
|
||
""" | ||
def __init__(self, **kwargs): | ||
"""Initialize primitive""" | ||
self.__variables = kwargs.get('variables', {}) | ||
|
||
def __str__(self): | ||
"""String representation of the primitive""" | ||
if self.__variables: | ||
string = "" | ||
num_vars = len(self.__variables) | ||
variables = self.__variables | ||
if hpccm.config.g_ctype == container_type.SINGULARITY: | ||
if num_vars > 0: | ||
string += "%post" + "\n" | ||
for count, (key, val) in enumerate(sorted(variables.items())): | ||
eol = "" if count == num_vars - 1 else "\n" | ||
string += ' {0}=${{{0}:-"{1}"}}'.format(key, val) + eol | ||
return string | ||
elif hpccm.config.g_ctype == container_type.BASH: | ||
for count, (key, val) in enumerate(sorted(variables.items())): | ||
eol = "" if count == num_vars - 1 else "\n" | ||
string += '{0}=${{{0}:-"{1}"}}'.format(key, val) + eol | ||
return string | ||
elif hpccm.config.g_ctype == container_type.DOCKER: | ||
for count, (key, val) in enumerate(sorted(variables.items())): | ||
eol = "" if count == num_vars - 1 else "\n" | ||
if val == "": | ||
string += 'ARG {0}'.format(key) + eol | ||
else: | ||
string += 'ARG {0}={1}'.format(key, val) + eol | ||
return string | ||
else: | ||
raise RuntimeError('Unknown container type') | ||
else: | ||
return '' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
# Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# pylint: disable=invalid-name, too-few-public-methods, bad-continuation | ||
|
||
"""Test cases for the arg module""" | ||
|
||
from __future__ import unicode_literals | ||
from __future__ import print_function | ||
|
||
import logging # pylint: disable=unused-import | ||
import unittest | ||
|
||
from helpers import bash, docker, invalid_ctype, singularity | ||
|
||
from hpccm.primitives.arg import arg | ||
|
||
class Test_arg(unittest.TestCase): | ||
def setUp(self): | ||
"""Disable logging output messages""" | ||
logging.disable(logging.ERROR) | ||
|
||
@docker | ||
def test_empty(self): | ||
"""No arg specified""" | ||
e = arg() | ||
self.assertEqual(str(e), '') | ||
|
||
@invalid_ctype | ||
def test_invalid_ctype(self): | ||
"""Invalid container type specified""" | ||
e = arg(variables={'A': 'B'}) | ||
with self.assertRaises(RuntimeError): | ||
str(e) | ||
|
||
@docker | ||
def test_single_docker(self): | ||
"""Single arg variable specified""" | ||
e = arg(variables={'A': 'B'}) | ||
self.assertEqual(str(e), 'ARG A=B') | ||
|
||
@docker | ||
def test_single_docker_nodefault(self): | ||
"""Single arg variable specified (no default value)""" | ||
e = arg(variables={'A': ''}) | ||
self.assertEqual(str(e), 'ARG A') | ||
|
||
@singularity | ||
def test_single_singularity(self): | ||
"""Single arg variable specified""" | ||
e = arg(variables={'A': 'B'}) | ||
self.assertEqual(str(e), '%post\n A=${A:-"B"}') | ||
|
||
@singularity | ||
def test_single_singularity_nodefault(self): | ||
"""Single arg variable specified""" | ||
e = arg(variables={'A': ''}) | ||
self.assertEqual(str(e), '%post\n A=${A:-""}') | ||
|
||
@bash | ||
def test_single_bash(self): | ||
"""Single arg variable specified""" | ||
e = arg(variables={'A': 'B'}) | ||
self.assertEqual(str(e), 'A=${A:-"B"}') | ||
|
||
@bash | ||
def test_single_bash_nodefault(self): | ||
"""Single arg variable specified""" | ||
e = arg(variables={'A': ''}) | ||
self.assertEqual(str(e), 'A=${A:-""}') | ||
|
||
@docker | ||
def test_multiple_docker(self): | ||
"""Multiple arg variables specified""" | ||
e = arg(variables={'ONE': 1, 'TWO': 2, 'THREE': 3}) | ||
self.assertEqual(str(e), | ||
'''ARG ONE=1 | ||
ARG THREE=3 | ||
ARG TWO=2''') | ||
|
||
@docker | ||
def test_multiple_docker_nodefault(self): | ||
"""Multiple arg variables specified (no default value)""" | ||
e = arg(variables={'ONE': '', 'TWO': '', 'THREE': ''}) | ||
self.assertEqual(str(e), | ||
'''ARG ONE | ||
ARG THREE | ||
ARG TWO''') | ||
|
||
@singularity | ||
def test_multiple_singularity(self): | ||
"""Multiple arg variables specified""" | ||
e = arg(variables={'ONE': 1, 'TWO': 2, 'THREE': 3}) | ||
self.assertEqual(str(e), | ||
'''%post | ||
ONE=${ONE:-"1"} | ||
THREE=${THREE:-"3"} | ||
TWO=${TWO:-"2"}''') | ||
|
||
@singularity | ||
def test_multiple_singularity_nodefault(self): | ||
"""Multiple arg variables specified""" | ||
e = arg(variables={'ONE':"", 'TWO':"", 'THREE':""}) | ||
self.assertEqual(str(e), | ||
'''%post | ||
ONE=${ONE:-""} | ||
THREE=${THREE:-""} | ||
TWO=${TWO:-""}''') | ||
|
||
@bash | ||
def test_multiple_bash(self): | ||
"""Multiple arg variables specified""" | ||
e = arg(variables={'ONE': 1, 'TWO': 2, 'THREE': 3}) | ||
self.assertEqual(str(e), | ||
'''ONE=${ONE:-"1"} | ||
THREE=${THREE:-"3"} | ||
TWO=${TWO:-"2"}''') | ||
|
||
@bash | ||
def test_multiple_bash_nodefault(self): | ||
"""Multiple arg variables specified""" | ||
e = arg(variables={'ONE': "", 'TWO': "", 'THREE': ""}) | ||
self.assertEqual(str(e), | ||
'''ONE=${ONE:-""} | ||
THREE=${THREE:-""} | ||
TWO=${TWO:-""}''') |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do the bash and Singularity args need to be exported? (I'm not sure)