Skip to content

Commit

Permalink
Added support for paasta CLI plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
solarkennedy committed Oct 16, 2019
1 parent 9471649 commit c03bb5b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 18 deletions.
1 change: 1 addition & 0 deletions debian/paasta-tools.links
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ opt/venvs/paasta-tools/bin/get_mesos_leader.py usr/bin/get_mesos_leader
opt/venvs/paasta-tools/bin/kill_orphaned_docker_containers.py usr/bin/kill_orphaned_docker_containers
opt/venvs/paasta-tools/bin/list_marathon_service_instances.py usr/bin/list_marathon_service_instances
opt/venvs/paasta-tools/bin/paasta-api usr/bin/paasta-api
opt/venvs/paasta-tools/bin/paasta-fsm usr/bin/paasta-fsm
opt/venvs/paasta-tools/bin/paasta_autoscale_cluster usr/bin/paasta_autoscale_cluster
opt/venvs/paasta-tools/bin/paasta_cleanup_maintenance usr/bin/paasta_cleanup_maintenance
opt/venvs/paasta-tools/bin/paasta_cleanup_tron_namespaces usr/bin/paasta_cleanup_tron_namespaces
Expand Down
4 changes: 2 additions & 2 deletions general_itests/steps/fsm_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from behave import when
from service_configuration_lib import read_services_configuration

from paasta_tools.cli.cmds.fsm import paasta_fsm
from paasta_tools.cli.fsm_cmd import paasta_fsm
from paasta_tools.utils import SystemPaastaConfig


Expand All @@ -47,7 +47,7 @@ def step_impl_when_fsm_auto(context):

fake_args = mock.Mock(yelpsoa_config_root=context.fake_yelpsoa_configs)
with mock.patch(
"paasta_tools.cli.cmds.fsm.load_system_paasta_config", autospec=True
"paasta_tools.cli.fsm_cmd.load_system_paasta_config", autospec=True
) as mock_load_system_paasta_config:
mock_load_system_paasta_config.return_value = SystemPaastaConfig(
config={}, directory=context.fake_yelpsoa_configs
Expand Down
40 changes: 40 additions & 0 deletions paasta_tools/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"""A command line tool for viewing information from the PaaSTA stack."""
import argparse
import logging
import os
import subprocess
import sys
import warnings

Expand All @@ -39,6 +41,34 @@ def error(self, message):
sys.exit(1)


def list_external_commands():
p = subprocess.check_output(["/bin/bash", "-p", "-c", "compgen -A command paasta-"])
lines = p.decode("utf-8").strip().split("\n")
return {l.replace("paasta-", "", 1) for l in lines}


def calling_external_command():
if len(sys.argv) > 1:
return sys.argv[1] in list_external_commands()
else:
return False


def get_command_help(command):
return f"(run 'paasta {command} -h' for usage)"


def external_commands_items():
for command in list_external_commands():
command_help = get_command_help(command)
yield command, command_help


def exec_subcommand(argv):
command = sys.argv[1]
os.execlp(f"paasta-{command}", *argv[1:])


def add_subparser(command, subparsers):
"""Given a command name, paasta_cmd, execute the add_subparser method
implemented in paasta_cmd.py.
Expand Down Expand Up @@ -94,6 +124,9 @@ def get_argparser():
for command in sorted(paasta_commands_dir(cmds)):
add_subparser(command, subparsers)

for command, command_help in external_commands_items():
subparsers.add_parser(command, help=command_help)

return parser


Expand All @@ -117,6 +150,13 @@ def main(argv=None):
"""
logging.basicConfig()
warnings.filterwarnings("ignore", category=DeprecationWarning)

# if we are an external command, we need to exec out early.
# The reason we exec out early is so we don't bother trying to parse
# "foreign" arguments, which would cause a stack trace.
if calling_external_command():
exec_subcommand(sys.argv)

try:
args, parser = parse_args(argv)
if args.command is None:
Expand Down
16 changes: 13 additions & 3 deletions paasta_tools/cli/cmds/fsm.py → paasta_tools/cli/fsm_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# 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 argparse
import contextlib
import shutil
import sys
Expand Down Expand Up @@ -50,10 +51,9 @@ def symlink_aware_copymode(*args, **kwargs):
shutil.copymode = orig_copymode


def add_subparser(subparsers):
fsm_parser = subparsers.add_parser(
def parse_args():
fsm_parser = argparse.ArgumentParser(
"fsm",
help="Generate boilerplate configs for a new PaaSTA Service",
description=(
"'paasta fsm' is used to generate example soa-configs, which is useful during initial "
"service creation. Currently 'fsm' generates 'yelp-specific' configuration, but can still "
Expand All @@ -73,6 +73,7 @@ def add_subparser(subparsers):
),
)
fsm_parser.set_defaults(command=paasta_fsm)
return fsm_parser.parse_args()


def get_paasta_config(yelpsoa_config_root):
Expand Down Expand Up @@ -110,3 +111,12 @@ def paasta_fsm(args):
paasta_print("Customize Them If It Makes You Happy -- http://y/paasta For Details")
paasta_print("Remember To Add, Commit, And Push When You're Done:")
paasta_print()


def main():
args = parse_args()
paasta_fsm(args)


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def get_install_requires():
"paasta=paasta_tools.cli.cli:main",
"paasta-api=paasta_tools.api.api:main",
"paasta-deployd=paasta_tools.deployd.master:main",
"paasta-fsm=paasta_tools.cli.fsm_cmd:main",
"paasta_autoscale_cluster=paasta_tools.autoscale_cluster:main",
"paasta_cleanup_tron_namespaces=paasta_tools.cleanup_tron_namespaces:main",
"paasta_list_kubernetes_service_instances=paasta_tools.list_kubernetes_service_instances:main",
Expand Down
13 changes: 0 additions & 13 deletions tests/cli/fsm/test_fsm.py

This file was deleted.

0 comments on commit c03bb5b

Please sign in to comment.