From 1ac423c13506b1dd27414d42171f778dd0595eee Mon Sep 17 00:00:00 2001 From: Abhay Agarwal Date: Fri, 3 Apr 2015 11:31:16 -0700 Subject: [PATCH] DCOS package list-install functionality --- cli/dcoscli/package/main.py | 39 +++++++++++++------- cli/tests/integrations/cli/test_package.py | 42 ++++++++++++++++++---- dcos/api/package.py | 19 ++++++++++ 3 files changed, 81 insertions(+), 19 deletions(-) diff --git a/cli/dcoscli/package/main.py b/cli/dcoscli/package/main.py index 780338d..41863b7 100644 --- a/cli/dcoscli/package/main.py +++ b/cli/dcoscli/package/main.py @@ -6,7 +6,7 @@ Usage: dcos package info dcos package install [--options= --app-id=] - dcos package list-installed + dcos package list-installed [--endpoints --app-id= ] dcos package search dcos package sources dcos package uninstall [--all | --app-id=] @@ -99,7 +99,7 @@ def _cmds(): cmds.Command( hierarchy=['package', 'list-installed'], - arg_keys=[], + arg_keys=['--endpoints', '--app-id', ''], function=_list), cmds.Command( @@ -202,7 +202,7 @@ def _update(): def _describe(package_name): """Describe the specified package. - :param package_name: The package to configure + :param package_name: The package to describe :type package_name: str :returns: Process status :rtype: int @@ -230,12 +230,6 @@ def _describe(package_name): version_map, version_error = pkg.software_versions() - if version_error is not None: - emitter.publish(version_error) - return 1 - - version_map, version_error = pkg.software_versions() - if version_error is not None: emitter.publish(version_error) return 1 @@ -304,9 +298,16 @@ def _install(package_name, options_file, app_id): return 0 -def _list(): - """Describe the specified package. +def _list(endpoints, app_id, package_name): + """Show installed apps + :param endpoints: Whether to include a list of + endpoints as port-host pairs + :type endpoints: boolean + :param package_name: The package to show + :type package_name: str + :param app_id: App ID of app to show + :type app_id: str :returns: Process status :rtype: int """ @@ -314,12 +315,26 @@ def _list(): config = _load_config() init_client = marathon.create_client(config) - installed, error = package.list_installed_packages(init_client) + + def keep(pkg): + if package_name and pkg.get('name', '') != package_name: + return False + if app_id and pkg.get('appId', '') != app_id: + return False + return True + + installed, error = package.list_installed_packages(init_client, keep) if error is not None: emitter.publish(error) return 1 + if endpoints: + installed, error = package.get_tasks_multiple(init_client, installed) + if error is not None: + emitter.publish(error) + return 1 + emitter.publish(installed) return 0 diff --git a/cli/tests/integrations/cli/test_package.py b/cli/tests/integrations/cli/test_package.py index 9f0ff73..ac3a5e5 100644 --- a/cli/tests/integrations/cli/test_package.py +++ b/cli/tests/integrations/cli/test_package.py @@ -17,7 +17,7 @@ Usage: dcos package info dcos package install [--options= --app-id=] - dcos package list-installed + dcos package list-installed [--endpoints --app-id= ] dcos package search dcos package sources dcos package uninstall [--all | --app-id=] @@ -256,6 +256,20 @@ def test_list_installed(): assert stdout == b'[]\n' assert stderr == b'' + returncode, stdout, stderr = exec_command( + ['dcos', 'package', 'list-installed', 'xyzzy']) + + assert returncode == 0 + assert stdout == b'[]\n' + assert stderr == b'' + + returncode, stdout, stderr = exec_command( + ['dcos', 'package', 'list-installed', '--app-id=/xyzzy']) + + assert returncode == 0 + assert stdout == b'[]\n' + assert stderr == b'' + returncode, stdout, stderr = exec_command( ['dcos', 'package', @@ -267,12 +281,7 @@ def test_list_installed(): assert stdout == b'' assert stderr == b'' - returncode, stdout, stderr = exec_command(['dcos', - 'package', - 'list-installed']) - - assert returncode == 0 - assert stdout == b"""\ + expected_output = b"""\ [ { "appId": "/mesos-dns", @@ -293,7 +302,26 @@ further setup requirements: http://mesosphere.github.io/mesos-dns/docs\ } ] """ + returncode, stdout, stderr = exec_command( + ['dcos', 'package', 'list-installed']) + + assert returncode == 0 assert stderr == b'' + assert stdout == expected_output + + returncode, stdout, stderr = exec_command( + ['dcos', 'package', 'list-installed', 'mesos-dns']) + + assert returncode == 0 + assert stderr == b'' + assert stdout == expected_output + + returncode, stdout, stderr = exec_command( + ['dcos', 'package', 'list-installed', '--app-id=/mesos-dns']) + + assert returncode == 0 + assert stderr == b'' + assert stdout == expected_output def test_search(): diff --git a/dcos/api/package.py b/dcos/api/package.py index a213de9..9922b02 100644 --- a/dcos/api/package.py +++ b/dcos/api/package.py @@ -246,6 +246,25 @@ def list_installed_packages(init_client, result_predicate=lambda x: True): return (pkgs, None) +def get_tasks_multiple(init_client, apps): + """Adds tasks to app dictionary + :param init_client: The program to use to list packages + :type init_client: object + :param apps: list of dict + :type apps: object + :rtype: (list, Error) + """ + + for app in apps: + tasks, err = init_client.get_tasks(app["appId"]) + if err is not None: + return (None, err) + app["endpoints"] = [{"host": t["host"], "ports": t["ports"]} + for t in tasks] + + return (apps, None) + + def search(query, cfg): """Returns a list of index entry collections, one for each registry in the supplied config.