-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from datmo/environment
environment update and addition of cli environment command
- Loading branch information
Showing
42 changed files
with
1,362 additions
and
836 deletions.
There are no files selected for viewing
This file contains 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 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,54 @@ | ||
from __future__ import print_function | ||
|
||
import prettytable | ||
|
||
from datmo.core.util.i18n import get as __ | ||
from datmo.core.controller.environment.environment import EnvironmentController | ||
from datmo.core.util.misc_functions import printable_string | ||
from datmo.cli.command.project import ProjectCommand | ||
|
||
|
||
class EnvironmentCommand(ProjectCommand): | ||
def __init__(self, home, cli_helper): | ||
super(EnvironmentCommand, self).__init__(home, cli_helper) | ||
# dest="subcommand" argument will populate a "subcommand" property with the subparsers name | ||
# example "subcommand"="create" or "subcommand"="ls" | ||
self.environment_controller = EnvironmentController(home=home) | ||
|
||
def environment(self): | ||
self.parse(["--help"]) | ||
return True | ||
|
||
def create(self, **kwargs): | ||
self.cli_helper.echo(__("info", "cli.environment.create")) | ||
environment_obj = self.environment_controller.create(kwargs) | ||
created_environment_id = environment_obj.id | ||
environments = self.environment_controller.list() | ||
for environment_obj in environments: | ||
if created_environment_id == environment_obj.id: | ||
self.cli_helper.echo(__("info", "cli.environment.create.alreadyexist", created_environment_id)) | ||
return created_environment_id | ||
self.cli_helper.echo(__("info", "cli.environment.create.success", environment_obj.id)) | ||
return created_environment_id | ||
|
||
def delete(self, **kwargs): | ||
environment_id = kwargs.get('environment_id') | ||
if self.environment_controller.delete(environment_id): | ||
self.cli_helper.echo(__("info", "cli.environment.delete.success", environment_id)) | ||
return True | ||
|
||
def ls(self): | ||
environments = self.environment_controller.list() | ||
header_list = ["id", "created at", "message"] | ||
t = prettytable.PrettyTable(header_list) | ||
environment_ids = [] | ||
for environment_obj in environments: | ||
environment_ids.append(environment_obj.id) | ||
environment_created_at = printable_string( | ||
environment_obj.created_at.strftime("%Y-%m-%d %H:%M:%S")) | ||
environment_message = printable_string(environment_obj.description) \ | ||
if environment_obj.description is not None else '' | ||
t.add_row([environment_obj.id, environment_created_at, environment_message]) | ||
|
||
self.cli_helper.echo(t) | ||
return environment_ids |
This file contains 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 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 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,146 @@ | ||
""" | ||
Tests for EnvironmentCommand | ||
""" | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
import uuid | ||
import tempfile | ||
import shutil | ||
import platform | ||
try: | ||
to_unicode = unicode | ||
except NameError: | ||
to_unicode = str | ||
|
||
import os | ||
from datmo.cli.driver.helper import Helper | ||
from datmo.cli.command.environment import EnvironmentCommand | ||
from datmo.cli.command.project import ProjectCommand | ||
from datmo.core.util.misc_functions import pytest_docker_environment_failed_instantiation | ||
|
||
# provide mountable tmp directory for docker | ||
tempfile.tempdir = "/tmp" if not platform.system() == "Windows" else None | ||
test_datmo_dir = os.environ.get('TEST_DATMO_DIR', tempfile.gettempdir()) | ||
|
||
|
||
class TestEnvironment(): | ||
def setup_class(self): | ||
self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir) | ||
self.cli_helper = Helper() | ||
|
||
def teardown_class(self): | ||
pass | ||
|
||
def __set_variables(self): | ||
self.project = ProjectCommand(self.temp_dir, self.cli_helper) | ||
self.project.parse( | ||
["init", "--name", "foobar", "--description", "test model"]) | ||
self.project.execute() | ||
self.environment_command = EnvironmentCommand(self.temp_dir, | ||
self.cli_helper) | ||
|
||
def test_environment_create(self): | ||
# 1) Environment definition file in `datmo_environment` folder | ||
# 2) Environment definition file passed as an option | ||
# 3) Environment definition file in root project folder | ||
# 4) Environment definition file in root project folder (should return the same environment) | ||
# 5) No environment definition file present | ||
# 6) No environment definition file present (should return the same environment) | ||
self.__set_variables() | ||
# Test option 1 | ||
# Create environment definition in `datmo_environment` folder | ||
datmo_environment_folder = os.path.join(self.temp_dir, | ||
"datmo_environment") | ||
os.makedirs(datmo_environment_folder) | ||
|
||
definition_filepath = os.path.join(datmo_environment_folder, | ||
"Dockerfile") | ||
random_text = str(uuid.uuid1()) | ||
with open(definition_filepath, "w") as f: | ||
f.write(to_unicode("FROM datmo/xgboost:cpu" + "\n")) | ||
f.write(to_unicode(str("RUN echo " + random_text))) | ||
|
||
self.environment_command.parse(["environment", "create"]) | ||
result = self.environment_command.execute() | ||
|
||
assert result | ||
|
||
# remove datmo_environment directory | ||
shutil.rmtree(datmo_environment_folder) | ||
|
||
# Test option 2 | ||
random_datmo_environment_folder = os.path.join(self.temp_dir, | ||
"random_datmo_dir") | ||
os.makedirs(random_datmo_environment_folder) | ||
|
||
definition_filepath = os.path.join(random_datmo_environment_folder, | ||
"Dockerfile") | ||
random_text = str(uuid.uuid1()) | ||
with open(definition_filepath, "w") as f: | ||
f.write(to_unicode("FROM datmo/xgboost:cpu" + "\n")) | ||
f.write(to_unicode(str("RUN echo " + random_text))) | ||
|
||
self.environment_command.parse([ | ||
"environment", "create", "--environment-def", definition_filepath | ||
]) | ||
result = self.environment_command.execute() | ||
|
||
assert result | ||
|
||
# remove datmo environment directory | ||
shutil.rmtree(random_datmo_environment_folder) | ||
|
||
# Test option 3 | ||
definition_filepath = os.path.join(self.temp_dir, "Dockerfile") | ||
random_text = str(uuid.uuid1()) | ||
with open(definition_filepath, "w") as f: | ||
f.write(to_unicode("FROM datmo/xgboost:cpu" + "\n")) | ||
f.write(to_unicode(str("RUN echo " + random_text))) | ||
|
||
self.environment_command.parse(["environment", "create"]) | ||
result = self.environment_command.execute() | ||
|
||
assert result | ||
|
||
# Test option 4 | ||
self.environment_command.parse(["environment", "create"]) | ||
result_2 = self.environment_command.execute() | ||
assert result == result_2 | ||
|
||
os.remove(definition_filepath) | ||
|
||
# Test option 5 | ||
self.environment_command.parse(["environment", "create"]) | ||
result = self.environment_command.execute() | ||
|
||
assert result | ||
|
||
# Test option 6 | ||
self.environment_command.parse(["environment", "create"]) | ||
result_2 = self.environment_command.execute() | ||
|
||
assert result == result_2 | ||
|
||
@pytest_docker_environment_failed_instantiation(test_datmo_dir) | ||
def test_environment_delete(self): | ||
self.__set_variables() | ||
self.environment_command.parse(["environment", "create"]) | ||
environment_id = self.environment_command.execute() | ||
|
||
self.environment_command.parse( | ||
["environment", "delete", "--id", environment_id]) | ||
result = self.environment_command.execute() | ||
|
||
assert result | ||
|
||
def test_environment_ls(self): | ||
self.__set_variables() | ||
self.environment_command.parse(["environment", "create"]) | ||
environment_id = self.environment_command.execute() | ||
|
||
self.environment_command.parse(["environment", "ls"]) | ||
environment_ids = self.environment_command.execute() | ||
|
||
assert environment_id in environment_ids |
This file contains 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
Oops, something went wrong.