Skip to content

Commit

Permalink
Merge pull request #4 from datmo/unique-code
Browse files Browse the repository at this point in the history
unique code
  • Loading branch information
shabazpatel committed Apr 13, 2018
2 parents 0816c2b + 2af3b7f commit 254e8ae
Show file tree
Hide file tree
Showing 26 changed files with 453 additions and 185 deletions.
14 changes: 7 additions & 7 deletions datmo/controller/code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ class CodeController(BaseController):
def __init__(self, home, dal_driver=None):
super(CodeController, self).__init__(home, dal_driver)

def create(self, id=None):
def create(self, commit_id=None):
"""Create a Code object
Parameters
----------
id : str, optional
If id is already present, will not make a new reference
commit_id : str, optional
if commit_id is already present, will not make a new reference and commit
Returns
-------
Expand All @@ -45,12 +45,12 @@ def create(self, id=None):
}

## Required args
required_args = ["id", "driver_type"]
required_args = ["driver_type", "commit_id"]
traversed_args = []
for required_arg in required_args:
# Handle Id if provided or not
if required_arg == "id":
create_dict[required_arg] = id if id else \
if required_arg == "commit_id":
create_dict[required_arg] = commit_id if commit_id else \
self.code_driver.create_code()
traversed_args.append(required_arg)
elif required_arg == "driver_type":
Expand Down Expand Up @@ -93,7 +93,7 @@ def delete(self, id):
"controller.code.delete",
id))
# Remove code reference
delete_code_success = self.code_driver.delete_code(id)
delete_code_success = self.code_driver.delete_code(code_obj.commit_id)
# Delete code object
delete_code_obj_success = self.dal.code.delete(code_obj.id)

Expand Down
56 changes: 28 additions & 28 deletions datmo/controller/code/driver/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,65 +500,65 @@ def delete_code_refs_dir(self):

# Implemented functions for every CodeDriver

def create_code(self, code_id=None):
def create_code(self, commit_id=None):
"""Add remaining files, make a commit and add it to a datmo code ref
Parameters
----------
code_id : str, optional
if code_id is given, it will not add files and not create a commit
commit_id : str, optional
if commit_id is given, it will not add files and not create a commit
Returns
-------
code_id : str
code id for the ref created
commit_id : str
commit id for the ref created
Raises
------
GitCommitDoesNotExist
Code id specified does not match a valid commit within the tree
commit id specified does not match a valid commit within the tree
"""
self.ensure_code_refs_dir()
if not code_id:
if not commit_id:
# add files and commit changes on current branch
self.add("-A")
new_commit_bool = self.commit(options=["-m",
"auto commit by datmo"])
try:
code_id = self.latest_commit()
commit_id = self.latest_commit()
except GitExecutionException as e:
raise GitCommitDoesNotExist(_("error",
"controller.code.driver.git.create_code",
e))
# revert back to the original commit
if new_commit_bool:
self.reset(code_id)
self.reset(commit_id)
# writing git commit into ref if exists
if not self.exists_commit(code_id):
if not self.exists_commit(commit_id):
raise GitCommitDoesNotExist(_("error",
"controller.code.driver.git.create_code",
code_id))
commit_id))
code_ref_path = os.path.join(self.filepath,
".git/refs/datmo/",
code_id)
commit_id)
with open(code_ref_path, "w") as f:
f.write(code_id)
return code_id
f.write(commit_id)
return commit_id

def exists_code(self, code_id):
def exists_code(self, commit_id):
code_ref_path = os.path.join(self.filepath,
".git/refs/datmo/",
code_id)
commit_id)
if not os.path.isfile(code_ref_path):
return False
return True

def delete_code(self, code_id):
def delete_code(self, commit_id):
self.ensure_code_refs_dir()
code_ref_path = os.path.join(self.filepath,
".git/refs/datmo/",
code_id)
if not self.exists_code(code_id):
commit_id)
if not self.exists_code(commit_id):
raise FileIOException(_("error",
"controller.code.driver.git.delete_code"))
os.remove(code_ref_path)
Expand All @@ -572,7 +572,7 @@ def list_code(self):
return code_refs_list

# Datmo specific remote calls
def push_code(self, code_id="*"):
def push_code(self, commit_id="*"):
datmo_ref = "refs/datmo/" + code_id
datmo_ref_map = "+" + datmo_ref + ":" + datmo_ref
try:
Expand All @@ -582,31 +582,31 @@ def push_code(self, code_id="*"):
"controller.code.driver.git.push_code",
str(e)))

def fetch_code(self, code_id):
def fetch_code(self, commit_id):
try:
datmo_ref = "refs/datmo/" + code_id
datmo_ref = "refs/datmo/" + commit_id
datmo_ref_map = "+" + datmo_ref + ":" + datmo_ref
success, err = self.fetch("origin", datmo_ref_map, option="-fup")
if not success:
raise GitExecutionException(_("error",
"controller.code.driver.git.fetch_code",
(code_id, err)))
(commit_id, err)))
except Exception as e:
raise GitExecutionException(_("error",
"controller.code.driver.git.fetch_code",
(code_id, str(e))))
(commit_id, str(e))))
return True

def checkout_code(self, code_id, remote=False):
def checkout_code(self, commit_id, remote=False):
try:
if remote:
self.fetch_code(code_id)
datmo_ref = "refs/datmo/" + code_id
self.fetch_code(commit_id)
datmo_ref = "refs/datmo/" + commit_id
return self.checkout(datmo_ref)
except Exception as e:
raise GitExecutionException(_("error",
"controller.code.driver.git.checkout_code",
(code_id, str(e))))
(commit_id, str(e))))


class GitHostDriver(object):
Expand Down
6 changes: 3 additions & 3 deletions datmo/controller/code/driver/test/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,11 @@ def test_create_code(self):
code_id)
assert code_id and \
os.path.isfile(code_ref_path)
# Test error raised with code_id
random_code_id = "random"
# Test error raised with commit_id
random_commit_id = str("random")
try:
self.git_code_manager.\
create_code(code_id=random_code_id)
create_code(commit_id=random_commit_id)
except GitCommitDoesNotExist:
assert True

Expand Down
4 changes: 2 additions & 2 deletions datmo/controller/code/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def test_create(self):
assert code_obj.driver_type

# Test failing with random id given
random_code_id = "random"
random_commit_id = "random"
try:
self.code.create(id=random_code_id)
self.code.create(commit_id=random_commit_id)
except GitCommitDoesNotExist:
assert True

Expand Down
52 changes: 33 additions & 19 deletions datmo/controller/environment/driver/dockerenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ def init(self):
except Exception as e:
raise EnvironmentExecutionException(_("error",
"controller.environment.driver.docker.init",
e))
str(e)))
return True


def get_tags_for_docker_repository(self, repo_name):
"""
Method to get tags for docker repositories
"""Method to get tags for docker repositories
Parameters
----------
Expand All @@ -93,6 +92,25 @@ def get_tags_for_docker_repository(self, repo_name):
return list_tag_names

def build_image(self, tag, definition_path='Dockerfile'):
"""Builds docker image
Parameters
----------
tag : str
name to tag image with
definition_path : str
absolute file path to the definition
Returns
-------
bool
True if success
Raises
------
EnvironmentExecutionException
"""
try:
docker_shell_cmd_list = list(self.cpu_prefix)
docker_shell_cmd_list.append('build')
Expand All @@ -118,7 +136,7 @@ def build_image(self, tag, definition_path='Dockerfile'):
except Exception as e:
raise EnvironmentExecutionException(_("error",
"controller.environment.driver.docker.build_image",
e))
str(e)))

def get_image(self, image_name):
return self.client.images.get(image_name)
Expand All @@ -141,13 +159,12 @@ def remove_image(self, image_id_or_name, force=False):
except Exception as e:
raise EnvironmentExecutionException(_("error",
"controller.environment.driver.docker.remove_image",
e))
str(e)))
return True

def remove_images(self, name=None, all=False, filters=None,
force=False):
"""
Remove multiple images
"""Remove multiple images
"""
try:
images = self.list_images(name=name, all=all, filters=filters)
Expand All @@ -156,13 +173,12 @@ def remove_images(self, name=None, all=False, filters=None,
except Exception as e:
raise EnvironmentExecutionException(_("error",
"controller.environment.driver.docker.remove_images",
e))
str(e)))
return True

def run_container(self, image_name, command=None, ports=None, name=None, volumes=None,
detach=False, stdin_open=False, tty=False, gpu=False, api=False):
"""
Run Docker container with parameters given as defined below
"""Run Docker container with parameters given as defined below
Parameters
----------
Expand Down Expand Up @@ -283,7 +299,7 @@ def run_container(self, image_name, command=None, ports=None, name=None, volumes
except Exception as e:
raise EnvironmentExecutionException(_("error",
"controller.environment.driver.docker.run_container",
e))
str(e)))
return return_code, container_id

def get_container(self, container_id):
Expand All @@ -302,7 +318,7 @@ def stop_container(self, container_id):
except Exception as e:
raise EnvironmentExecutionException(_("error",
"controller.environment.driver.docker.stop_container",
e))
str(e)))
return True

def remove_container(self, container_id, force=False):
Expand All @@ -316,13 +332,12 @@ def remove_container(self, container_id, force=False):
except Exception as e:
raise EnvironmentExecutionException(_("error",
"controller.environment.driver.docker.remove_container",
e))
str(e)))
return True

def log_container(self, container_id, filepath, api=False,
follow=True):
"""
Log capture at a particular point `docker logs`. Can also use `--follow` for real time logs
"""Log capture at a particular point `docker logs`. Can also use `--follow` for real time logs
Parameters
----------
Expand Down Expand Up @@ -370,8 +385,7 @@ def log_container(self, container_id, filepath, api=False,
return return_code, logs

def stop_remove_containers_by_term(self, term, force=False):
"""
Stops and removes containers by term
"""Stops and removes containers by term
"""
try:
running_docker_container_cmd_list = list(self.cpu_prefix)
Expand Down Expand Up @@ -412,13 +426,13 @@ def stop_remove_containers_by_term(self, term, force=False):
except Exception as e:
raise EnvironmentExecutionException(_("error",
"controller.environment.driver.docker.stop_remove_containers_by_term",
e))
str(e)))
return True

def form_datmo_definition_file(self, input_definition_path="Dockerfile",
output_definition_path="datmoDockerfile"):
"""
in order to create intermediate dockerfile to run
n order to create intermediate dockerfile to run
"""
base_dockerfile_filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"templates", "baseDockerfile")
Expand Down
11 changes: 3 additions & 8 deletions datmo/controller/environment/environment.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os

from datmo.util.i18n import get as _
from datmo.util.misc_functions import get_filehash
from datmo.controller.base import BaseController
from datmo.controller.file.file_collection import FileCollectionController
from datmo.util.exceptions import RequiredArgumentMissing, \
Expand Down Expand Up @@ -54,18 +53,14 @@ def create(self, dictionary):
}

## Required args
required_args = ["id", "driver_type", "file_collection_id", "definition_filename"]
required_args = ["driver_type", "file_collection_id", "definition_filename"]
for required_arg in required_args:
# Add in any values that are
if required_arg in dictionary:
create_dict[required_arg] = dictionary[required_arg]
else:
# Id creation from filehash
if required_arg == "id":
create_dict[required_arg] = \
get_filehash(dictionary["definition_filepath"])
# Pull in driver type from base
elif required_arg == "driver_type":
if required_arg == "driver_type":
create_dict[required_arg] = self.environment_driver.type
# File setup
elif required_arg == "file_collection_id":
Expand Down Expand Up @@ -118,7 +113,7 @@ def build(self, id):
file_collection_obj = self.dal.file_collection.\
get_by_id(environment_obj.file_collection_id)
# Build the Environment with the driver
datmo_definition_filepath = os.path.join(file_collection_obj.path,
datmo_definition_filepath = os.path.join(self.home, file_collection_obj.path,
"datmo" + environment_obj.definition_filename)
result = self.environment_driver.build_image(id, definition_path=datmo_definition_filepath)
return result
Expand Down

0 comments on commit 254e8ae

Please sign in to comment.