Skip to content

Commit

Permalink
Adds tags parameter to publish command (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
sshookman authored Nov 20, 2019
1 parent 236bd1f commit d7f6fd6
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Documenting All Changes to the Skelebot Project


---

## v1.9.1
#### Added
- **Publish Tags Parameter** | Added "tags" parameter to the publish command to allow for multiple custom tags to be used when publishing the image

---

## v1.9.0
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.9.0
1.9.1
7 changes: 7 additions & 0 deletions docs/publishing.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ skelebot publish

If not logged-in to the provided host in the registry, you will be prompted to enter your username and password.

If you would like to make use of any custom tags when publishing the image, the tags parameter
(`-t --tags`) can be used to specify a list of tag values.

```
skelebot publish --tags LOCAL DEV STAGE
```

---

<center><< <a href="versioning.html">Versioning</a> | <a href="artifacts.html">Artifacts</a> >></center>
5 changes: 3 additions & 2 deletions skelebot/components/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ def addParsers(self, subparsers):
"""

helpMessage = "Publish your versioned Docker Image to the registry"
subparsers.add_parser("publish", help=helpMessage)
registryParser = subparsers.add_parser("publish", help=helpMessage)
registryParser.add_argument("-t", "--tags", nargs='*', help="Additional image tags")

return subparsers

Expand All @@ -55,4 +56,4 @@ def execute(self, config, args):

docker.login(self.host)
docker.build(config)
docker.push(config, self.host, self.port, self.user)
docker.push(config, self.host, self.port, self.user, tags=args.tags)
25 changes: 13 additions & 12 deletions skelebot/systems/execution/docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
BUILD_CMD = "docker build -t {image} ."
RUN_CMD = "docker run --name {image}-{jobName} --rm {params} {image} /bin/bash -c \"{command}\""
SAVE_CMD = "docker save -o {filename} {image}"
TAG_CMD = "docker tag {src} {image}:{version}"
PUSH_CMD = "docker push {image}:{version}"
TAG_CMD = "docker tag {src} {image}:{tag}"
PUSH_CMD = "docker push {image}:{tag}"

def execute(cmd):
status = os.system(cmd)
if (status != 0):
raise Exception("Docker Command Failed")

def login(host=None):
"""Login to the given Docker Host"""
Expand Down Expand Up @@ -79,7 +84,7 @@ def save(config, filename="image.img"):

return os.system(SAVE_CMD.format(image=config.getImageName(), filename=filename))

def push(config, host=None, port=None, user=None):
def push(config, host=None, port=None, user=None, tags=None):
"""Tag with version and latest and push the project Image to the provided Docker Image Host"""

imageName = config.getImageName()
Expand All @@ -88,13 +93,9 @@ def push(config, host=None, port=None, user=None):
user = "{user}/".format(user=user) if user is not None else ""
image = "{host}{user}{name}".format(host=host, port=port, user=user, name=imageName)

status = os.system(TAG_CMD.format(src=imageName, image=image, version=config.version))
if (status == 0):
status = os.system(TAG_CMD.format(src=imageName, image=image, version="latest"))
if (status == 0):
status = os.system(PUSH_CMD.format(image=image, version=config.version))
if (status == 0):
status = os.system(PUSH_CMD.format(image=image, version="latest"))
tags = [] if tags is None else tags
tags = tags + [config.version, "latest"]

if (status != 0):
raise Exception("Docker Push Failed")
for tag in tags:
execute(TAG_CMD.format(src=imageName, image=image, tag=tag))
execute(PUSH_CMD.format(image=image, tag=tag))
18 changes: 16 additions & 2 deletions test/test_components_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,28 @@ def test_execute(self, mock_docker):
mock_docker.build.return_value = 0

config = sb.objects.config.Config(language="R")
args = argparse.Namespace()
args = argparse.Namespace(tags=None)

registry = sb.components.registry.Registry(host="docker.io", port=88, user="skelebot")
registry.execute(config, args)

mock_docker.login.assert_called_with("docker.io")
mock_docker.build.assert_called_with(config)
mock_docker.push.assert_called_with(config, "docker.io", 88, "skelebot")
mock_docker.push.assert_called_with(config, "docker.io", 88, "skelebot", tags=None)

@mock.patch('skelebot.components.registry.docker')
def test_execute_tags(self, mock_docker):
mock_docker.build.return_value = 0

config = sb.objects.config.Config(language="R")
args = argparse.Namespace(tags=["test", "dev", "stage"])

registry = sb.components.registry.Registry(host="docker.io", port=88, user="skelebot")
registry.execute(config, args)

mock_docker.login.assert_called_with("docker.io")
mock_docker.build.assert_called_with(config)
mock_docker.push.assert_called_with(config, "docker.io", 88, "skelebot", tags=['test', 'dev', 'stage'])

def test_validate_valid(self):
try:
Expand Down
27 changes: 26 additions & 1 deletion test/test_systems_execution_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ def test_push(self, mock_getcwd, mock_system, mock_expanduser):
mock_system.assert_any_call("docker push docker.io:8888/skelebot/test:6.6.6")
mock_system.assert_any_call("docker push docker.io:8888/skelebot/test:latest")

@mock.patch('os.path.expanduser')
@mock.patch('os.system')
@mock.patch('os.getcwd')
def test_push_tags(self, mock_getcwd, mock_system, mock_expanduser):
host = "docker.io"
port = 8888
user = "skelebot"
folderPath = "{path}/test/files".format(path=self.path)

mock_expanduser.return_value = "{path}/test/plugins".format(path=self.path)
mock_getcwd.return_value = folderPath
mock_system.return_value = 0

config = sb.systems.generators.yaml.loadConfig()
sb.systems.execution.docker.push(config, host, port, user, tags=["DEV", "STG"])

mock_system.assert_any_call("docker tag test docker.io:8888/skelebot/test:6.6.6")
mock_system.assert_any_call("docker push docker.io:8888/skelebot/test:6.6.6")
mock_system.assert_any_call("docker tag test docker.io:8888/skelebot/test:latest")
mock_system.assert_any_call("docker push docker.io:8888/skelebot/test:latest")
mock_system.assert_any_call("docker tag test docker.io:8888/skelebot/test:DEV")
mock_system.assert_any_call("docker push docker.io:8888/skelebot/test:DEV")
mock_system.assert_any_call("docker tag test docker.io:8888/skelebot/test:STG")
mock_system.assert_any_call("docker push docker.io:8888/skelebot/test:STG")

@mock.patch('os.path.expanduser')
@mock.patch('os.system')
@mock.patch('os.getcwd')
Expand All @@ -79,7 +104,7 @@ def test_push_error(self, mock_getcwd, mock_system, mock_expanduser):

config = sb.systems.generators.yaml.loadConfig()

with self.assertRaisesRegex(Exception, "Docker Push Failed"):
with self.assertRaisesRegex(Exception, "Docker Command Failed"):
sb.systems.execution.docker.push(config, host, port, user)

@mock.patch('os.path.expanduser')
Expand Down

0 comments on commit d7f6fd6

Please sign in to comment.