This is an automated email from the ASF dual-hosted git repository. klueska pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mesos.git
The following commit(s) were added to refs/heads/master by this push: new 051a138 Refactored new CLI. 051a138 is described below commit 051a138d08ba3b9e28fd6ec4e4f707cbd4bb1563 Author: Armand Grillet <agril...@mesosphere.io> AuthorDate: Tue Oct 2 10:09:54 2018 -0400 Refactored new CLI. This adds a new file called `mesos.py` to the CLI library that abstracts calling various Mesos APIs. The functions added here in this commit are then used in the 'task' and 'agent' plugins to simplify them. Review: https://reviews.apache.org/r/68734/ --- src/python/cli_new/lib/cli/mesos.py | 67 ++++++++++++++++++++++++ src/python/cli_new/lib/cli/plugins/agent/main.py | 11 +--- src/python/cli_new/lib/cli/plugins/task/main.py | 11 +--- src/python/cli_new/lib/cli/tasks.py | 33 ++++++++++++ 4 files changed, 104 insertions(+), 18 deletions(-) diff --git a/src/python/cli_new/lib/cli/mesos.py b/src/python/cli_new/lib/cli/mesos.py new file mode 100644 index 0000000..068d694 --- /dev/null +++ b/src/python/cli_new/lib/cli/mesos.py @@ -0,0 +1,67 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +""" +Functions to handle agents. +""" + +from cli import http +from cli.exceptions import CLIException + + +def get_agents(master): + """ + Get the agents in a Mesos cluster. + """ + endpoint = "slaves" + key = "slaves" + + try: + data = http.get_json(master, endpoint) + except Exception as exception: + raise CLIException( + "Could not open '/{endpoint}' on master address '{addr}': {error}" + .format(endpoint=endpoint, addr=master, error=exception)) + + if not key in data: + raise CLIException( + "Missing '{key}' key in data retrieved" + " from master on '/{endpoint}'" + .format(key=key, endpoint=endpoint)) + + return data[key] + +def get_tasks(master): + """ + Get the tasks in a Mesos cluster. + """ + endpoint = "tasks" + key = "tasks" + + try: + data = http.get_json(master, endpoint) + except Exception as exception: + raise CLIException( + "Could not open '/{endpoint}' on master address '{addr}': {error}" + .format(endpoint=endpoint, addr=master, error=exception)) + + if not key in data: + raise CLIException( + "Missing '{key}' key in data retrieved" + " from master on '/{endpoint}'" + .format(key=key, endpoint=endpoint)) + + return data[key] diff --git a/src/python/cli_new/lib/cli/plugins/agent/main.py b/src/python/cli_new/lib/cli/plugins/agent/main.py index fc62d9f..5e821b3 100644 --- a/src/python/cli_new/lib/cli/plugins/agent/main.py +++ b/src/python/cli_new/lib/cli/plugins/agent/main.py @@ -18,9 +18,8 @@ The agent plugin. """ -from cli import http - from cli.exceptions import CLIException +from cli.mesos import get_agents from cli.plugins import PluginBase from cli.util import Table @@ -58,13 +57,7 @@ class Agent(PluginBase): raise CLIException("Unable to get leading master address: {error}" .format(error=exception)) - try: - agents = http.get_json(master, "slaves")["slaves"] - except Exception as exception: - raise CLIException("Could not open '/slaves'" - " endpoint at '{addr}': {error}" - .format(addr=master, error=exception)) - + agents = get_agents(master) if not agents: print("The cluster does not have any agents.") return diff --git a/src/python/cli_new/lib/cli/plugins/task/main.py b/src/python/cli_new/lib/cli/plugins/task/main.py index 644e256..a47a8c5 100644 --- a/src/python/cli_new/lib/cli/plugins/task/main.py +++ b/src/python/cli_new/lib/cli/plugins/task/main.py @@ -18,9 +18,8 @@ The task plugin. """ -from cli import http - from cli.exceptions import CLIException +from cli.mesos import get_tasks from cli.plugins import PluginBase from cli.util import Table @@ -57,13 +56,7 @@ class Task(PluginBase): raise CLIException("Unable to get leading master address: {error}" .format(error=exception)) - try: - tasks = http.get_json(master, "tasks")["tasks"] - except Exception as exception: - raise CLIException("Could not open '/tasks'" - " endpoint at '{addr}': {error}" - .format(addr=master, error=exception)) - + tasks = get_tasks(master) if not tasks: print("There are no tasks running in the cluster.") return diff --git a/src/python/cli_new/lib/cli/tasks.py b/src/python/cli_new/lib/cli/tasks.py new file mode 100644 index 0000000..531e001 --- /dev/null +++ b/src/python/cli_new/lib/cli/tasks.py @@ -0,0 +1,33 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +""" +Functions to handle tasks. +""" + +from cli import http +from cli.exceptions import CLIException + +def get_tasks(master): + """ + Get the tasks in a Mesos cluster. + """ + try: + return http.get_json(master, "tasks")["tasks"] + except Exception as exception: + raise CLIException("Could not open '/tasks'" + " endpoint at '{addr}': {error}" + .format(addr=master, error=exception))