vantage6 4.2.2__py3-none-any.whl → 4.3.0b3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of vantage6 might be problematic. Click here for more details.

Files changed (49) hide show
  1. tests_cli/test_node_cli.py +1 -1
  2. tests_cli/test_server_cli.py +5 -5
  3. tests_cli/test_wizard.py +8 -9
  4. vantage6/cli/__build__ +1 -1
  5. vantage6/cli/_version.py +1 -1
  6. vantage6/cli/algostore/attach.py +32 -0
  7. vantage6/cli/algostore/new.py +55 -0
  8. vantage6/cli/algostore/start.py +102 -0
  9. vantage6/cli/algostore/stop.py +60 -0
  10. vantage6/cli/cli.py +20 -0
  11. vantage6/cli/common/decorator.py +92 -0
  12. vantage6/cli/common/start.py +232 -0
  13. vantage6/cli/configuration_manager.py +13 -23
  14. vantage6/cli/configuration_wizard.py +134 -60
  15. vantage6/cli/context/__init__.py +86 -0
  16. vantage6/cli/context/algorithm_store.py +130 -0
  17. vantage6/cli/context/base_server.py +89 -0
  18. vantage6/cli/{context.py → context/node.py} +12 -174
  19. vantage6/cli/context/server.py +127 -0
  20. vantage6/cli/dev/create.py +4 -3
  21. vantage6/cli/dev/remove.py +5 -3
  22. vantage6/cli/dev/start.py +2 -1
  23. vantage6/cli/dev/stop.py +2 -1
  24. vantage6/cli/globals.py +24 -0
  25. vantage6/cli/node/common/__init__.py +10 -4
  26. vantage6/cli/node/create_private_key.py +1 -1
  27. vantage6/cli/node/files.py +1 -1
  28. vantage6/cli/node/list.py +1 -1
  29. vantage6/cli/node/new.py +3 -2
  30. vantage6/cli/node/remove.py +1 -1
  31. vantage6/cli/node/set_api_key.py +1 -1
  32. vantage6/cli/node/start.py +7 -6
  33. vantage6/cli/rabbitmq/queue_manager.py +1 -1
  34. vantage6/cli/server/attach.py +3 -3
  35. vantage6/cli/server/common/__init__.py +5 -3
  36. vantage6/cli/server/files.py +1 -1
  37. vantage6/cli/server/import_.py +7 -3
  38. vantage6/cli/server/list.py +9 -5
  39. vantage6/cli/server/new.py +3 -2
  40. vantage6/cli/server/shell.py +3 -3
  41. vantage6/cli/server/start.py +26 -110
  42. vantage6/cli/server/stop.py +3 -4
  43. vantage6/cli/server/version.py +2 -2
  44. {vantage6-4.2.2.dist-info → vantage6-4.3.0b3.dist-info}/METADATA +3 -3
  45. vantage6-4.3.0b3.dist-info/RECORD +68 -0
  46. vantage6-4.2.2.dist-info/RECORD +0 -58
  47. {vantage6-4.2.2.dist-info → vantage6-4.3.0b3.dist-info}/WHEEL +0 -0
  48. {vantage6-4.2.2.dist-info → vantage6-4.3.0b3.dist-info}/entry_points.txt +0 -0
  49. {vantage6-4.2.2.dist-info → vantage6-4.3.0b3.dist-info}/top_level.txt +0 -0
@@ -39,7 +39,7 @@ class NodeCLITest(unittest.TestCase):
39
39
  # check exit code
40
40
  self.assertEqual(result.exit_code, 1)
41
41
 
42
- @patch("vantage6.cli.context.NodeContext.available_configurations")
42
+ @patch("vantage6.cli.context.node.NodeContext.available_configurations")
43
43
  @patch("docker.DockerClient.ping")
44
44
  @patch("docker.DockerClient.containers")
45
45
  def test_list(self, containers, docker_ping, available_configurations):
@@ -4,7 +4,7 @@ from unittest.mock import MagicMock, patch
4
4
  from pathlib import Path
5
5
  from click.testing import CliRunner
6
6
 
7
- from vantage6.cli.globals import APPNAME
7
+ from vantage6.common.globals import APPNAME, InstanceType
8
8
  from vantage6.cli.server.start import cli_server_start
9
9
  from vantage6.cli.server.list import cli_server_configuration_list
10
10
  from vantage6.cli.server.files import cli_server_files
@@ -18,10 +18,10 @@ class ServerCLITest(unittest.TestCase):
18
18
  @patch("vantage6.cli.server.start.NetworkManager")
19
19
  @patch("vantage6.cli.server.start.docker.types.Mount")
20
20
  @patch("os.makedirs")
21
- @patch("vantage6.cli.server.start.pull_if_newer")
21
+ @patch("vantage6.cli.common.start.pull_if_newer")
22
22
  @patch("vantage6.cli.server.common.ServerContext")
23
23
  @patch("vantage6.cli.server.start.docker.from_env")
24
- @patch("vantage6.cli.server.start.check_docker_running", return_value=True)
24
+ @patch("vantage6.cli.common.start.check_docker_running", return_value=True)
25
25
  def test_start(
26
26
  self,
27
27
  docker_check,
@@ -131,7 +131,7 @@ class ServerCLITest(unittest.TestCase):
131
131
  """Stop server without errors."""
132
132
 
133
133
  container1 = MagicMock()
134
- container1.name = f"{APPNAME}-iknl-system-server"
134
+ container1.name = f"{APPNAME}-iknl-system-{InstanceType.SERVER}"
135
135
  containers.containers.list.return_value = [container1]
136
136
 
137
137
  runner = CliRunner()
@@ -145,7 +145,7 @@ class ServerCLITest(unittest.TestCase):
145
145
  def test_attach(self, containers, sleep):
146
146
  """Attach log to the console without errors."""
147
147
  container1 = MagicMock()
148
- container1.name = f"{APPNAME}-iknl-system-server"
148
+ container1.name = f"{APPNAME}-iknl-system-{InstanceType.SERVER}"
149
149
  containers.list.return_value = [container1]
150
150
 
151
151
  sleep.side_effect = KeyboardInterrupt("Boom!")
tests_cli/test_wizard.py CHANGED
@@ -9,6 +9,7 @@ from vantage6.cli.configuration_wizard import (
9
9
  configuration_wizard,
10
10
  select_configuration_questionaire,
11
11
  )
12
+ from vantage6.common.globals import InstanceType
12
13
 
13
14
  module_path = "vantage6.cli.configuration_wizard"
14
15
 
@@ -86,29 +87,27 @@ class WizardTest(unittest.TestCase):
86
87
  @patch(f"{module_path}.server_configuration_questionaire")
87
88
  @patch(f"{module_path}.ServerConfigurationManager")
88
89
  @patch(f"{module_path}.NodeConfigurationManager")
89
- @patch(f"{module_path}.NodeContext")
90
+ @patch("vantage6.cli.configuration_wizard.AppContext")
90
91
  def test_configuration_wizard_interface(
91
92
  self, context, node_m, server_m, server_q, node_q
92
93
  ):
93
94
  context.instance_folders.return_value = {"config": "/some/path/"}
94
95
 
95
- file_ = configuration_wizard("node", "vtg6", False)
96
+ file_ = configuration_wizard(InstanceType.NODE, "vtg6", False)
96
97
  self.assertEqual(Path("/some/path/vtg6.yaml"), file_)
97
98
 
98
- file_ = configuration_wizard("server", "vtg6", True)
99
+ file_ = configuration_wizard(InstanceType.SERVER, "vtg6", True)
99
100
  self.assertEqual(Path("/some/path/vtg6.yaml"), file_)
100
101
 
101
- @patch(f"{module_path}.NodeContext")
102
- @patch(f"{module_path}.ServerContext")
103
- def test_select_configuration(self, server_c, node_c):
102
+ @patch("vantage6.cli.configuration_wizard.AppContext.available_configurations")
103
+ def test_select_configuration(self, available_configurations):
104
104
  config = MagicMock()
105
105
  config.name = "vtg6"
106
106
 
107
- server_c.available_configurations.return_value = [[config], []]
108
- node_c.available_configurations.return_value = [[config], []]
107
+ available_configurations.return_value = [[config], []]
109
108
 
110
109
  with patch(f"{module_path}.q") as q:
111
110
  q.select.return_value.ask.return_value = "vtg6"
112
- name = select_configuration_questionaire("node", True)
111
+ name = select_configuration_questionaire(InstanceType.NODE, True)
113
112
 
114
113
  self.assertEqual(name, "vtg6")
vantage6/cli/__build__ CHANGED
@@ -1 +1 @@
1
- 0
1
+ 3
vantage6/cli/_version.py CHANGED
@@ -7,7 +7,7 @@ with open(os.path.join(here, "__build__")) as fp:
7
7
  __build__ = json.load(fp)
8
8
 
9
9
  # Module version
10
- version_info = (4, 2, 2, "final", __build__, 0)
10
+ version_info = (4, 3, 0, "beta", __build__, 0)
11
11
 
12
12
  # Module version stage suffix map
13
13
  _specifier_ = {"alpha": "a", "beta": "b", "candidate": "rc", "final": ""}
@@ -0,0 +1,32 @@
1
+ import click
2
+ import docker
3
+
4
+ from colorama import Fore, Style
5
+
6
+ from vantage6.common import error
7
+ from vantage6.common.docker.addons import check_docker_running
8
+ from vantage6.common.globals import APPNAME, InstanceType
9
+ from vantage6.cli.common.decorator import insert_context
10
+ from vantage6.cli.common.start import attach_logs
11
+ from vantage6.cli.context.algorithm_store import AlgorithmStoreContext
12
+
13
+
14
+ @click.command()
15
+ @insert_context(InstanceType.ALGORITHM_STORE)
16
+ def cli_algo_store_attach(ctx: AlgorithmStoreContext) -> None:
17
+ """
18
+ Show the server logs in the current console.
19
+ """
20
+ check_docker_running()
21
+ client = docker.from_env()
22
+
23
+ running_servers = client.containers.list(
24
+ filters={"label": f"{APPNAME}-type={InstanceType.ALGORITHM_STORE}"}
25
+ )
26
+ running_server_names = [container.name for container in running_servers]
27
+
28
+ if ctx.docker_container_name in running_server_names:
29
+ container = client.containers.get(ctx.docker_container_name)
30
+ attach_logs(container, InstanceType.ALGORITHM_STORE)
31
+ else:
32
+ error(f"{Fore.RED}{ctx.name}{Style.RESET_ALL} is not running!")
@@ -0,0 +1,55 @@
1
+ import click
2
+ from colorama import Fore, Style
3
+
4
+ from vantage6.common import info, error, check_config_writeable
5
+ from vantage6.common.globals import InstanceType
6
+ from vantage6.cli.globals import DEFAULT_SERVER_SYSTEM_FOLDERS
7
+ from vantage6.cli.context.algorithm_store import AlgorithmStoreContext
8
+ from vantage6.cli.configuration_wizard import configuration_wizard
9
+ from vantage6.cli.utils import check_config_name_allowed, prompt_config_name
10
+
11
+
12
+ @click.command()
13
+ @click.option(
14
+ "-n", "--name", default=None, help="name of the configuration you want to use."
15
+ )
16
+ @click.option("--system", "system_folders", flag_value=True)
17
+ @click.option(
18
+ "--user", "system_folders", flag_value=False, default=DEFAULT_SERVER_SYSTEM_FOLDERS
19
+ )
20
+ def cli_algo_store_new(name: str, system_folders: bool) -> None:
21
+ """
22
+ Create a new server configuration.
23
+ """
24
+ name = prompt_config_name(name)
25
+
26
+ # check if name is allowed for docker volume, else exit
27
+ check_config_name_allowed(name)
28
+
29
+ # check that this config does not exist
30
+ try:
31
+ if AlgorithmStoreContext.config_exists(name, system_folders):
32
+ error(f"Configuration {Fore.RED}{name}{Style.RESET_ALL} already " "exists!")
33
+ exit(1)
34
+ except Exception as e:
35
+ error(e)
36
+ exit(1)
37
+
38
+ # Check that we can write in this folder
39
+ if not check_config_writeable(system_folders):
40
+ error("Your user does not have write access to all folders. Exiting")
41
+ info(
42
+ f"Create a new server using '{Fore.GREEN}v6 algorithm-store new "
43
+ f"--user{Style.RESET_ALL}' instead!"
44
+ )
45
+ exit(1)
46
+
47
+ # create config in ctx location
48
+ cfg_file = configuration_wizard(InstanceType.ALGORITHM_STORE, name, system_folders)
49
+ info(f"New configuration created: {Fore.GREEN}{cfg_file}{Style.RESET_ALL}")
50
+
51
+ flag = "" if system_folders else "--user"
52
+ info(
53
+ f"You can start the algorithm store by running {Fore.GREEN}v6 "
54
+ f"algorithm-store start {flag}{Style.RESET_ALL}"
55
+ )
@@ -0,0 +1,102 @@
1
+ import click
2
+
3
+ from vantage6.common import info
4
+ from vantage6.common.globals import APPNAME, DEFAULT_ALGO_STORE_IMAGE, InstanceType
5
+ from vantage6.cli.common.start import (
6
+ attach_logs,
7
+ check_for_start,
8
+ get_image,
9
+ mount_config_file,
10
+ mount_database,
11
+ mount_source,
12
+ pull_image,
13
+ )
14
+ from vantage6.cli.globals import AlgoStoreGlobals
15
+ from vantage6.cli.context.algorithm_store import AlgorithmStoreContext
16
+ from vantage6.cli.common.decorator import insert_context
17
+
18
+
19
+ @click.command()
20
+ @click.option("--ip", default=None, help="IP address to listen on")
21
+ @click.option("-p", "--port", default=None, type=int, help="Port to listen on")
22
+ @click.option("-i", "--image", default=None, help="Algorithm store Docker image to use")
23
+ @click.option(
24
+ "--keep/--auto-remove",
25
+ default=False,
26
+ help="Keep image after algorithm store has been stopped. Useful " "for debugging",
27
+ )
28
+ @click.option(
29
+ "--mount-src",
30
+ default="",
31
+ help="Override vantage6 source code in container with the source"
32
+ " code in this path",
33
+ )
34
+ @click.option(
35
+ "--attach/--detach",
36
+ default=False,
37
+ help="Print server logs to the console after start",
38
+ )
39
+ @insert_context(InstanceType.ALGORITHM_STORE)
40
+ def cli_algo_store_start(
41
+ ctx: AlgorithmStoreContext,
42
+ ip: str,
43
+ port: int,
44
+ image: str,
45
+ keep: bool,
46
+ mount_src: str,
47
+ attach: bool,
48
+ ) -> None:
49
+ """
50
+ Start the algorithm store server.
51
+ """
52
+ info("Starting algorithm store...")
53
+ docker_client = check_for_start(ctx, InstanceType.ALGORITHM_STORE)
54
+
55
+ image = get_image(image, ctx, "algorithm-store", DEFAULT_ALGO_STORE_IMAGE)
56
+
57
+ pull_image(docker_client, image)
58
+
59
+ config_file = "/mnt/config.yaml"
60
+ mounts = mount_config_file(ctx, config_file)
61
+
62
+ src_mount = mount_source(mount_src)
63
+ if src_mount:
64
+ mounts.append(src_mount)
65
+
66
+ mount, environment_vars = mount_database(ctx, InstanceType.ALGORITHM_STORE)
67
+ if mount:
68
+ mounts.append(mount)
69
+
70
+ # The `ip` and `port` refer here to the ip and port within the container.
71
+ # So we do not really care that is it listening on all interfaces.
72
+ internal_port = 5000
73
+ cmd = (
74
+ f"uwsgi --http :{internal_port} --gevent 1000 --http-websockets "
75
+ "--master --callable app --disable-logging "
76
+ "--wsgi-file /vantage6/vantage6-algorithm-store/vantage6/algorithm"
77
+ f"/store/wsgi.py --pyargv {config_file}"
78
+ )
79
+ info(cmd)
80
+
81
+ info("Run Docker container")
82
+ port_ = str(port or ctx.config["port"] or AlgoStoreGlobals.PORT)
83
+ container = docker_client.containers.run(
84
+ image,
85
+ command=cmd,
86
+ mounts=mounts,
87
+ detach=True,
88
+ labels={
89
+ f"{APPNAME}-type": InstanceType.ALGORITHM_STORE,
90
+ "name": ctx.config_file_name,
91
+ },
92
+ environment=environment_vars,
93
+ ports={f"{internal_port}/tcp": (ip, port_)},
94
+ name=ctx.docker_container_name,
95
+ auto_remove=not keep,
96
+ tty=True,
97
+ )
98
+
99
+ info(f"Success! container id = {container.id}")
100
+
101
+ if attach:
102
+ attach_logs(container, InstanceType.ALGORITHM_STORE)
@@ -0,0 +1,60 @@
1
+ import click
2
+ import docker
3
+ from colorama import Fore, Style
4
+
5
+ from vantage6.common import info, warning, error
6
+ from vantage6.common.docker.addons import (
7
+ check_docker_running,
8
+ remove_container_if_exists,
9
+ )
10
+ from vantage6.common.globals import APPNAME, InstanceType
11
+ from vantage6.cli.common.decorator import insert_context
12
+ from vantage6.cli.context.algorithm_store import AlgorithmStoreContext
13
+
14
+
15
+ @click.command()
16
+ @insert_context(InstanceType.ALGORITHM_STORE)
17
+ @click.option("--all", "all_servers", flag_value=True, help="Stop all servers")
18
+ def cli_algo_store_stop(ctx: AlgorithmStoreContext, all_servers: bool):
19
+ """
20
+ Stop one or all running server(s).
21
+ """
22
+ check_docker_running()
23
+ client = docker.from_env()
24
+
25
+ running_servers = client.containers.list(
26
+ filters={"label": f"{APPNAME}-type={InstanceType.ALGORITHM_STORE}"}
27
+ )
28
+
29
+ if not running_servers:
30
+ warning("No servers are currently running.")
31
+ return
32
+
33
+ running_server_names = [server.name for server in running_servers]
34
+
35
+ if all_servers:
36
+ for container_name in running_server_names:
37
+ _stop_algorithm_store(client, container_name)
38
+ return
39
+
40
+ container_name = ctx.docker_container_name
41
+ if container_name not in running_server_names:
42
+ error(f"{Fore.RED}{ctx.name}{Style.RESET_ALL} is not running!")
43
+ return
44
+
45
+ _stop_algorithm_store(client, container_name)
46
+
47
+
48
+ def _stop_algorithm_store(client, container_name) -> None:
49
+ """
50
+ Stop the algorithm store server.
51
+
52
+ Parameters
53
+ ----------
54
+ client : DockerClient
55
+ The docker client
56
+ container_name : str
57
+ The name of the container to stop
58
+ """
59
+ remove_container_if_exists(client, name=container_name)
60
+ info(f"Stopped the {Fore.GREEN}{container_name}{Style.RESET_ALL} server.")
vantage6/cli/cli.py CHANGED
@@ -29,6 +29,10 @@ from vantage6.cli.algorithm.create import cli_algorithm_create
29
29
  from vantage6.cli.algorithm.update import cli_algorithm_update
30
30
  from vantage6.cli.test.feature_tester import cli_test_features
31
31
  from vantage6.cli.test.integration_test import cli_test_integration
32
+ from vantage6.cli.algostore.attach import cli_algo_store_attach
33
+ from vantage6.cli.algostore.new import cli_algo_store_new
34
+ from vantage6.cli.algostore.start import cli_algo_store_start
35
+ from vantage6.cli.algostore.stop import cli_algo_store_stop
32
36
 
33
37
 
34
38
  # Define the server group
@@ -117,6 +121,21 @@ cli_test.add_command(cli_test_features, name="feature-test")
117
121
  cli_test.add_command(cli_test_integration, name="integration-test")
118
122
 
119
123
 
124
+ # Define the algorithm-store group
125
+ @click.group(name="algorithm-store")
126
+ def cli_algo_store() -> None:
127
+ """
128
+ Manage your vantage6 algorithm store server instances.
129
+ """
130
+
131
+
132
+ # Define the commands for the test group
133
+ cli_algo_store.add_command(cli_algo_store_attach, name="attach")
134
+ cli_algo_store.add_command(cli_algo_store_new, name="new")
135
+ cli_algo_store.add_command(cli_algo_store_start, name="start")
136
+ cli_algo_store.add_command(cli_algo_store_stop, name="stop")
137
+
138
+
120
139
  # Define the overall group
121
140
  @click.group(name="cli")
122
141
  def cli_complete() -> None:
@@ -134,3 +153,4 @@ cli_complete.add_command(cli_server)
134
153
  cli_complete.add_command(cli_dev)
135
154
  cli_complete.add_command(cli_algorithm)
136
155
  cli_complete.add_command(cli_test)
156
+ cli_complete.add_command(cli_algo_store)
@@ -0,0 +1,92 @@
1
+ from functools import wraps
2
+ import click
3
+
4
+ from vantage6.common import error
5
+ from vantage6.common.globals import InstanceType
6
+ from vantage6.cli.configuration_wizard import select_configuration_questionaire
7
+ from vantage6.cli.globals import DEFAULT_SERVER_SYSTEM_FOLDERS
8
+ from vantage6.cli.context import select_context_class, get_context
9
+
10
+
11
+ # TODO to make this decorator usable by nodes as well, we should make the
12
+ # default for --user/--system configurable
13
+ def insert_context(type_: InstanceType) -> callable:
14
+ """
15
+ Supply the Click function with an additional context parameter. The context
16
+ is passed to the function as the first argument.
17
+
18
+ Parameters
19
+ ----------
20
+ type_ : InstanceType
21
+ The type of instance for which the context should be inserted
22
+
23
+ Returns
24
+ -------
25
+ Callable
26
+ Click function with context
27
+
28
+ Examples
29
+ --------
30
+ >>> @insert_context(InstanceType.SERVER)
31
+ >>> def cli_server_start(ctx: ServerContext, *args, **kwargs) -> None:
32
+ >>> pass
33
+ """
34
+
35
+ def protection_decorator(func: callable) -> callable:
36
+ @click.option("-n", "--name", default=None, help="Name of the configuration.")
37
+ @click.option(
38
+ "-c",
39
+ "--config",
40
+ default=None,
41
+ help="Absolute path to " "configuration-file; overrides --name",
42
+ )
43
+ @click.option(
44
+ "--system",
45
+ "system_folders",
46
+ flag_value=True,
47
+ help="Use system folders instead of user folders. This " "is the default",
48
+ )
49
+ @click.option(
50
+ "--user",
51
+ "system_folders",
52
+ flag_value=False,
53
+ default=DEFAULT_SERVER_SYSTEM_FOLDERS,
54
+ help="Use user folders instead of system folders",
55
+ )
56
+ @wraps(func)
57
+ def decorator(
58
+ name: str, config: str, system_folders: bool, *args, **kwargs
59
+ ) -> callable:
60
+ """
61
+ Decorator function that adds the context to the function.
62
+
63
+ Returns
64
+ -------
65
+ Callable
66
+ Decorated function
67
+ """
68
+ ctx_class = select_context_class(type_)
69
+ # path to configuration file always overrides name
70
+ if config:
71
+ ctx = ctx_class.from_external_config_file(config, system_folders)
72
+ elif "ctx" in kwargs:
73
+ # if ctx is already in kwargs (typically when one click command
74
+ # calls another internally), use that existing ctx
75
+ ctx = kwargs.pop("ctx")
76
+ else:
77
+ # in case no name, ctx or config file is supplied, ask the user
78
+ # to select an existing config by name
79
+ if not name:
80
+ try:
81
+ # select configuration if none supplied
82
+ name = select_configuration_questionaire(type_, system_folders)
83
+ except Exception:
84
+ error("No configurations could be found!")
85
+ exit(1)
86
+
87
+ ctx = get_context(type_, name, system_folders)
88
+ return func(ctx, *args, **kwargs)
89
+
90
+ return decorator
91
+
92
+ return protection_decorator