Improve cli (#179)

This commit is contained in:
Adil Hafeez 2024-10-10 17:44:41 -07:00 committed by GitHub
parent ceca0dba28
commit 7d5f760884
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 611 additions and 445 deletions

View file

@ -4,6 +4,37 @@ import time
import pkg_resources
import select
from cli.utils import run_docker_compose_ps, print_service_status, check_services_state
from cli.utils import getLogger
import sys
log = getLogger(__name__)
def stream_gateway_logs(follow):
"""
Stream logs from the arch gateway service.
"""
compose_file = pkg_resources.resource_filename(
__name__, "../config/docker-compose.yaml"
)
log.info("Logs from arch gateway service.")
options = ["docker", "compose", "-p", "arch", "logs"]
if follow:
options.append("-f")
try:
# Run `docker-compose logs` to stream logs from the gateway service
subprocess.run(
options,
cwd=os.path.dirname(compose_file),
check=True,
stdout=sys.stdout,
stderr=sys.stderr,
)
except subprocess.CalledProcessError as e:
log.info(f"Failed to stream logs: {str(e)}")
def start_arch(arch_config_file, env, log_timeout=120):
@ -14,7 +45,7 @@ def start_arch(arch_config_file, env, log_timeout=120):
path (str): The path where the prompt_confi.yml file is located.
log_timeout (int): Time in seconds to show logs before checking for healthy state.
"""
log.info("Starting arch gateway")
compose_file = pkg_resources.resource_filename(
__name__, "../config/docker-compose.yaml"
)
@ -35,9 +66,10 @@ def start_arch(arch_config_file, env, log_timeout=120):
), # Ensure the Docker command runs in the correct path
env=env, # Pass the modified environment
check=True, # Raise an exception if the command fails
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
)
print(f"Arch docker-compose started in detached.")
print("Monitoring `docker-compose ps` logs...")
log.info(f"Arch docker-compose started in detached.")
start_time = time.time()
services_status = {}
@ -51,14 +83,14 @@ def start_arch(arch_config_file, env, log_timeout=120):
# Check if timeout is reached
if elapsed_time > log_timeout:
print(f"Stopping log monitoring after {log_timeout} seconds.")
log.info(f"Stopping log monitoring after {log_timeout} seconds.")
break
current_services_status = run_docker_compose_ps(
compose_file=compose_file, env=env
)
if not current_services_status:
print(
log.info(
"Status for the services could not be detected. Something went wrong. Please run docker logs"
)
break
@ -74,11 +106,11 @@ def start_arch(arch_config_file, env, log_timeout=120):
running_states = ["running", "up"]
if check_services_state(current_services_status, running_states):
print("Arch is up and running!")
log.info("Arch gateway is up and running!")
break
if check_services_state(current_services_status, unhealthy_states):
print(
log.info(
"One or more Arch services are unhealthy. Please run `docker logs` for more information"
)
print_service_status(
@ -92,7 +124,7 @@ def start_arch(arch_config_file, env, log_timeout=120):
services_status[service_name]["State"]
!= current_services_status[service_name]["State"]
):
print(
log.info(
"One or more Arch services have changed state. Printing current state"
)
print_service_status(current_services_status)
@ -101,7 +133,7 @@ def start_arch(arch_config_file, env, log_timeout=120):
services_status = current_services_status
except subprocess.CalledProcessError as e:
print(f"Failed to start Arch: {str(e)}")
log.info(f"Failed to start Arch: {str(e)}")
def stop_arch():
@ -115,17 +147,21 @@ def stop_arch():
__name__, "../config/docker-compose.yaml"
)
log.info("Shutting down arch gateway service.")
try:
# Run `docker-compose down` to shut down all services
subprocess.run(
["docker", "compose", "-p", "arch", "down"],
cwd=os.path.dirname(compose_file),
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
print("Successfully shut down all services.")
log.info("Successfully shut down arch gateway service.")
except subprocess.CalledProcessError as e:
print(f"Failed to shut down services: {str(e)}")
log.info(f"Failed to shut down services: {str(e)}")
def start_arch_modelserver():
@ -134,12 +170,13 @@ def start_arch_modelserver():
"""
try:
log.info("archgw_modelserver restart")
subprocess.run(
["archgw_modelserver", "restart"], check=True, start_new_session=True
)
print("Successfull run the archgw model_server")
log.info("Successfull ran model_server")
except subprocess.CalledProcessError as e:
print(f"Failed to start model_server. Please check archgw_modelserver logs")
log.info(f"Failed to start model_server. Please check archgw_modelserver logs")
sys.exit(1)
@ -153,7 +190,7 @@ def stop_arch_modelserver():
["archgw_modelserver", "stop"],
check=True,
)
print("Successfull stopped the archgw model_server")
log.info("Successfull stopped the archgw model_server")
except subprocess.CalledProcessError as e:
print(f"Failed to start model_server. Please check archgw_modelserver logs")
log.info(f"Failed to start model_server. Please check archgw_modelserver logs")
sys.exit(1)