This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
marvin
143 lines (112 loc) · 4.73 KB
/
marvin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
# coding=utf-8
# Copyright [2020] [Apache Software Foundation]
#
# 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.
import os
import click
from marvin_cli.utils.misc import package_to_name
from marvin_cli.utils.misc import kill_persisted_process
from marvin_cli.utils.config import parse_ini
from marvin_cli.utils.config import read_cli_conf, generate_default_conf
from marvin_cli.utils.docker import search_engine_container
from marvin_cli.utils.docker import search_engine_images
from marvin_cli.utils.docker import create_engine_image
from marvin_cli.utils.docker import create_daemon_container
from marvin_cli.management.engine import cli as cli_engine
from marvin_cli.management.notebook import cli as cli_notebook
from marvin_cli.management.edit import cli as cli_edit
from marvin_cli.management.test import cli as cli_test
EXCLUDE_BY_TYPE = {
'toolbox': ['engine-dryrun', 'engine-grpcserver', 'engine-httpserver', 'notebook',
'lab', 'project-export', 'engine-logs', 'edit-metadata', 'test', 'test-tdd',
'test-tox', 'engine-bumpversion', 'benchmark', 'benchmark-plot'],
'engine': ['project-import']
}
ENVIRONMENT_VARIABLES = {
'MARVIN_HOME': "{0}/marvin".format(os.environ['HOME']),
'MARVIN_DATA_PATH': "{0}/marvin/data".format(os.environ['HOME']),
'MARVIN_LOG': "{0}/marvin/data/.logs".format(os.environ['HOME'])
}
def _define_environ():
for var in ENVIRONMENT_VARIABLES.keys():
if os.environ.get(var) is None:
os.environ[var] = ENVIRONMENT_VARIABLES[var]
def _create_folders():
_exports_folder = os.path.join(os.environ['MARVIN_DATA_PATH'], 'exports')
_conf_folder = os.path.join(os.environ['MARVIN_DATA_PATH'], '.conf')
_artifacts_folder = os.path.join(os.environ['MARVIN_DATA_PATH'], '.artifacts')
_paths_list = [os.environ['MARVIN_HOME'], os.environ['MARVIN_DATA_PATH'],
os.environ['MARVIN_LOG'], _exports_folder, _conf_folder, _artifacts_folder]
for path in _paths_list:
if not os.path.exists(path):
os.makedirs(path)
def _initial_check_engine_mode(engine_package):
if not search_engine_images(engine_package):
if click.confirm('Do you want to create the engine docker image?', default=True):
create_engine_image(engine_package)
if not search_engine_container(engine_package):
if click.confirm('Do you want to create the engine docker container?', default=True):
create_daemon_container(engine_package, package_to_name(engine_package))
def _read_config(engine_package):
filepath = os.path.join(os.environ['MARVIN_DATA_PATH'], '.conf', 'cli_conf.json')
if not os.path.isfile(filepath):
conf = generate_default_conf()
else:
conf = read_cli_conf()
return conf
sigh = """___ ___ _ _____ _ _____
| \/ | (_) / __ \| | |_ _|
| . . | __ _ _ ____ ___ _ __ | / \/| | | |
| |\/| |/ _` | '__\ \ / / | '_ \ | | | | | |
| | | | (_| | | \ V /| | | | | | \__/\| |_____| |_
\_| |_/\__,_|_| \_/ |_|_| |_| \____/\_____/\___/\n\n"""
ini_path = os.path.join(os.getcwd(), 'marvin.ini')
config = None
package_name = None
exclude = None
print(sigh)
_define_environ()
_create_folders()
if os.path.exists(ini_path):
config = parse_ini(ini_path)
package_name = config['marvin_package']
exclude = EXCLUDE_BY_TYPE['engine']
print("[+] engine mode - {}".format(package_name))
_initial_check_engine_mode(package_name)
print("\n")
else:
exclude = EXCLUDE_BY_TYPE['toolbox']
print("[+] toolbox mode\n")
config = _read_config(package_name)
@click.group('main')
@click.pass_context
def cli(ctx):
ctx.obj = {
'package_name': package_name,
'editor': config['editor'],
'default_host': config['default_host'],
'executor_url': config['executor_url']
}
@cli.command("system-kill", help="Kill all marvin cli background process.")
def kill():
kill_persisted_process()
commands = {}
commands.update(cli_engine.commands)
commands.update(cli_notebook.commands)
commands.update(cli_edit.commands)
commands.update(cli_test.commands)
for name, command in commands.items():
if name not in exclude:
cli.add_command(command, name=name)
cli()