|
| 1 | +# Copyright 2023 VMware, Inc. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +""" |
| 4 | +These commands are used for the examples. |
| 5 | +""" |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import logging |
| 9 | +import pathlib |
| 10 | +import shutil |
| 11 | +from typing import Optional |
| 12 | + |
| 13 | +from ptscripts import Context |
| 14 | +from ptscripts import command_group |
| 15 | + |
| 16 | +log = logging.getLogger(__name__) |
| 17 | + |
| 18 | +REPO_ROOT = pathlib.Path(__file__).parent.parent |
| 19 | + |
| 20 | +# Define the command group |
| 21 | +cgroup = command_group(name="examples", help="Examples Related Commands", description=__doc__) |
| 22 | + |
| 23 | + |
| 24 | +@cgroup.command( |
| 25 | + name="elastic", |
| 26 | + arguments={ |
| 27 | + "command": { |
| 28 | + "help": "The docker-compose command to run", |
| 29 | + }, |
| 30 | + "rebuild_pkg": { |
| 31 | + "help": "Force salt-analytics package rebuild.", |
| 32 | + }, |
| 33 | + "rebuild_examples_pkg": { |
| 34 | + "help": "Force salt-analytics.examples package rebuild.", |
| 35 | + }, |
| 36 | + "docker_compose_args": { |
| 37 | + "help": "Extra arguments to pass to docker-compose", |
| 38 | + "nargs": "*", |
| 39 | + }, |
| 40 | + }, |
| 41 | +) |
| 42 | +def elastic( |
| 43 | + ctx: Context, |
| 44 | + command: str, |
| 45 | + rebuild_pkg: bool = False, |
| 46 | + rebuild_examples_pkg: bool = False, |
| 47 | + docker_compose_args: Optional[list[str]] = None, |
| 48 | +): |
| 49 | + """ |
| 50 | + Elastic Search Example docker-compose. |
| 51 | + """ |
| 52 | + if docker_compose_args is None: |
| 53 | + docker_compose_args = [] |
| 54 | + |
| 55 | + if command == "build": |
| 56 | + nox = shutil.which("nox") |
| 57 | + if nox is None: |
| 58 | + ctx.error( |
| 59 | + "The 'nox' binary was not found. Please install it: python -m pip install nox" |
| 60 | + ) |
| 61 | + ctx.exit(1) |
| 62 | + existing_whl = list(REPO_ROOT.joinpath("dist").glob("*.whl")) |
| 63 | + if not existing_whl or (existing_whl and rebuild_pkg): |
| 64 | + ret = ctx.run(nox, "--force-color", "-e", "build") |
| 65 | + if ret.returncode: |
| 66 | + ctx.error("Failed to build the salt-analytics package") |
| 67 | + ctx.exit(1) |
| 68 | + existing_whl = list(REPO_ROOT.joinpath("examples", "dist").glob("*.whl")) |
| 69 | + if not existing_whl or (existing_whl and rebuild_examples_pkg): |
| 70 | + ret = ctx.run(nox, "--force-color", "-e", "build", "--", "examples") |
| 71 | + if ret.returncode: |
| 72 | + ctx.error("Failed to build the salt-analytics.examples package") |
| 73 | + ctx.exit(1) |
| 74 | + |
| 75 | + docker_compose = shutil.which("docker-compose") |
| 76 | + if docker_compose is None: |
| 77 | + ctx.error("The 'docker-compose' binary was not found. Please install it") |
| 78 | + ctx.exit(1) |
| 79 | + |
| 80 | + ret = ctx.run( |
| 81 | + docker_compose, |
| 82 | + "-f", |
| 83 | + "docker/elastic/docker-compose.yml", |
| 84 | + command, |
| 85 | + *docker_compose_args, |
| 86 | + interactive=True, |
| 87 | + ) |
| 88 | + if ret.returncode: |
| 89 | + ctx.error(f"Failed to run 'docker-compose {command}'") |
| 90 | + ctx.exit(1) |
| 91 | + ctx.exit(0) |
0 commit comments