remove dependency on docker-compose when starting up archgw (#305)

This commit is contained in:
Adil Hafeez 2024-11-26 13:13:02 -08:00 committed by GitHub
parent 726f1a3185
commit 0ff3d43008
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 3761 additions and 274 deletions

View file

@ -56,7 +56,6 @@ def validate_and_render_schema():
"port": 80, # default port
}
print(inferred_clusters)
endpoints = config_yaml.get("endpoints", {})
# override the inferred clusters with the ones defined in the config
@ -88,7 +87,6 @@ def validate_and_render_schema():
}
rendered = template.render(data)
print(rendered)
print(ENVOY_CONFIG_FILE_RENDERED)
with open(ENVOY_CONFIG_FILE_RENDERED, "w") as file:
file.write(rendered)
@ -108,7 +106,7 @@ def validate_prompt_config(arch_config_file, arch_config_schema_file):
validate(config_yaml, config_schema_yaml)
except Exception as e:
print(
f"Error validating arch_config file: {arch_config_file}, error: {e.message}"
f"Error validating arch_config file: {arch_config_file}, schema file: {arch_config_schema_file}, error: {e.message}"
)
raise e

View file

@ -9,3 +9,5 @@ SERVICE_NAME_MODEL_SERVER = "model_server"
SERVICE_ALL = "all"
MODEL_SERVER_LOG_FILE = "~/archgw_logs/modelserver.log"
ACCESS_LOG_FILES = "~/archgw_logs/access*"
ARCHGW_DOCKER_NAME = "archgw"
ARCHGW_DOCKER_IMAGE = "katanemo/archgw:latest"

View file

@ -1,40 +1,74 @@
import subprocess
import os
import time
import pkg_resources
import select
import sys
import glob
from cli.utils import run_docker_compose_ps, print_service_status, check_services_state
import docker
from cli.utils import getLogger
from cli.consts import (
ARCHGW_DOCKER_IMAGE,
ARCHGW_DOCKER_NAME,
KATANEMO_LOCAL_MODEL_LIST,
MODEL_SERVER_LOG_FILE,
ACCESS_LOG_FILES,
)
from huggingface_hub import snapshot_download
from dotenv import dotenv_values
log = getLogger(__name__)
def start_archgw_docker(client, arch_config_file, env):
logs_path = "~/archgw_logs"
logs_path_abs = os.path.expanduser(logs_path)
return client.containers.run(
name=ARCHGW_DOCKER_NAME,
image=ARCHGW_DOCKER_IMAGE,
detach=True, # Run in detached mode
ports={
"10000/tcp": 10000,
"10001/tcp": 10001,
"11000/tcp": 11000,
"12000/tcp": 12000,
"19901/tcp": 19901,
},
volumes={
f"{arch_config_file}": {
"bind": "/app/arch_config.yaml",
"mode": "ro",
},
"/etc/ssl/cert.pem": {"bind": "/etc/ssl/cert.pem", "mode": "ro"},
logs_path_abs: {"bind": "/var/log"},
},
environment={
"OTEL_TRACING_HTTP_ENDPOINT": "http://host.docker.internal:4318/v1/traces",
**env,
},
extra_hosts={"host.docker.internal": "host-gateway"},
healthcheck={
"test": ["CMD", "curl", "-f", "http://localhost:10000/healthz"],
"interval": 5000000000, # 5 seconds
"timeout": 1000000000, # 1 seconds
"retries": 3,
},
)
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"]
options = ["docker", "logs", "archgw"]
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,
@ -88,42 +122,20 @@ def start_arch(arch_config_file, env, log_timeout=120):
Start Docker Compose in detached mode and stream logs until services are healthy.
Args:
path (str): The path where the prompt_confi.yml file is located.
path (str): The path where the prompt_config.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"
)
try:
# Run the Docker Compose command in detached mode (-d)
subprocess.run(
[
"docker",
"compose",
"-p",
"arch",
"up",
"-d",
],
cwd=os.path.dirname(
compose_file
), # 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,
)
log.info(f"Arch docker-compose started in detached.")
client = docker.from_env()
container = start_archgw_docker(client, arch_config_file, env)
start_time = time.time()
services_status = {}
services_running = (
False # assume that the services are not running at the moment
)
while True:
container = client.containers.get(container.id)
current_time = time.time()
elapsed_time = current_time - start_time
@ -132,53 +144,16 @@ def start_arch(arch_config_file, env, log_timeout=120):
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:
log.info(
"Status for the services could not be detected. Something went wrong. Please run docker logs"
)
container_status = container.attrs["State"]["Health"]["Status"]
if container_status == "healthy":
log.info("Container is healthy!")
break
else:
log.info(f"Container health status: {container_status}")
time.sleep(1)
if not services_status:
services_status = current_services_status # set the first time
print_service_status(
services_status
) # print the services status and proceed.
# check if anyone service is failed or exited state, if so print and break out
unhealthy_states = ["unhealthy", "exit", "exited", "dead", "bad"]
running_states = ["running", "up"]
if check_services_state(current_services_status, running_states):
log.info("Arch gateway is up and running!")
break
if check_services_state(current_services_status, unhealthy_states):
log.info(
"One or more Arch services are unhealthy. Please run `docker logs` for more information"
)
print_service_status(
current_services_status
) # print the services status and proceed.
break
# check to see if the status of one of the services has changed from prior. Print and loop over until finish, or error
for service_name in services_status.keys():
if (
services_status[service_name]["State"]
!= current_services_status[service_name]["State"]
):
log.info(
"One or more Arch services have changed state. Printing current state"
)
print_service_status(current_services_status)
break
services_status = current_services_status
except subprocess.CalledProcessError as e:
except docker.errors.APIError as e:
log.info(f"Failed to start Arch: {str(e)}")
@ -189,21 +164,16 @@ def stop_arch():
Args:
path (str): The path where the docker-compose.yml file is located.
"""
compose_file = pkg_resources.resource_filename(
__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,
["docker", "stop", "archgw"],
)
subprocess.run(
["docker", "remove", "archgw"],
)
log.info("Successfully shut down arch gateway service.")
except subprocess.CalledProcessError as e:

View file

@ -1,13 +1,16 @@
import click
import os
import pkg_resources
import sys
import subprocess
import multiprocessing
import importlib.metadata
from cli import targets
from cli import config_generator
from cli.utils import getLogger, get_llm_provider_access_keys, load_env_file_to_dict
from cli.utils import (
getLogger,
get_llm_provider_access_keys,
load_env_file_to_dict,
validate_schema,
)
from cli.core import (
start_arch_modelserver,
stop_arch_modelserver,
@ -160,17 +163,12 @@ def up(file, path, service):
return
log.info(f"Validating {arch_config_file}")
arch_schema_config = pkg_resources.resource_filename(
__name__, "../config/arch_config_schema.yaml"
)
try:
config_generator.validate_prompt_config(
arch_config_file=arch_config_file,
arch_config_schema_file=arch_schema_config,
)
validate_schema(arch_config_file)
except Exception as e:
log.info(f"Exiting archgw up: validation failed")
log.info(f"Error: {str(e)}")
sys.exit(1)
log.info("Starging arch model server and arch gateway")
@ -213,14 +211,7 @@ def up(file, path, service):
else:
env_stage[access_key] = env_file_dict[access_key]
with open(
pkg_resources.resource_filename(__name__, "../config/env.list"), "w"
) as file:
for key, value in env_stage.items():
file.write(f"{key}={value}\n")
env.update(env_stage)
env["ARCH_CONFIG_FILE"] = arch_config_file
if service == SERVICE_NAME_ARCHGW:
start_arch(arch_config_file, env)

View file

@ -1,11 +1,8 @@
import subprocess
import os
import time
import select
import shlex
import yaml
import json
import logging
import docker
from cli.consts import ARCHGW_DOCKER_IMAGE, ARCHGW_DOCKER_NAME
logging.basicConfig(
level=logging.INFO,
@ -22,72 +19,39 @@ def getLogger(name="cli"):
log = getLogger(__name__)
def run_docker_compose_ps(compose_file, env):
"""
Check if all Docker Compose services are in a healthy state.
Args:
path (str): The path where the docker-compose.yml file is located.
"""
def validate_schema(arch_config_file: str) -> None:
try:
# Run `docker compose ps` to get the health status of each service.
# This should be a non-blocking call so using subprocess.Popen(...)
ps_process = subprocess.Popen(
[
"docker",
"compose",
"-p",
"arch",
"ps",
"--format",
"table{{.Service}}\t{{.State}}\t{{.Ports}}",
],
cwd=os.path.dirname(compose_file),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
start_new_session=True,
env=env,
client = docker.from_env()
# Run the container with detach=True to avoid blocking main process
container = client.containers.run(
image=ARCHGW_DOCKER_IMAGE,
volumes={
f"{arch_config_file}": {
"bind": "/app/arch_config.yaml",
"mode": "ro",
},
},
entrypoint=["python", "config_generator.py"],
detach=True,
)
# Capture the output of `docker-compose ps`
services_status, error_output = ps_process.communicate()
# Check if there is any error output
if error_output:
log.info(
f"Error while checking service status:\n{error_output}",
file=os.sys.stderr,
# Wait for the container to finish and get the exit code
exit_code = container.wait()
# Check exit code for validation success
if exit_code["StatusCode"] != 0:
# Validation failed (non-zero exit code)
logs = container.logs().decode() # Get container logs for debugging
raise ValueError(
f"Validation failed. Container exited with code {exit_code}.\nLogs:\n{logs}"
)
return {}
services = parse_docker_compose_ps_output(services_status)
return services
# Successful validation (exit code 0)
log.info("Schema validation successful!")
except subprocess.CalledProcessError as e:
log.info(f"Failed to check service status. Error:\n{e.stderr}")
return e
# Helper method to print service status
def print_service_status(services):
log.info(f"{'Service Name':<25} {'State':<20} {'Ports'}")
log.info("=" * 72)
for service_name, info in services.items():
status = info["STATE"]
ports = info["PORTS"]
log.info(f"{service_name:<25} {status:<20} {ports}")
# check for states based on the states passed in
def check_services_state(services, states):
for service_name, service_info in services.items():
status = service_info[
"STATE"
].lower() # Convert status to lowercase for easier comparison
if any(state in status for state in states):
return True
return False
except docker.errors.APIError as e:
# Handle container creation error
raise ValueError(f"Failed to create container: {e}")
def get_llm_provider_access_keys(arch_config_file):
@ -127,28 +91,3 @@ def load_env_file_to_dict(file_path):
env_dict[key] = value
return env_dict
def parse_docker_compose_ps_output(output):
# Split the output into lines
lines = output.strip().splitlines()
# Extract the headers (first row) and the rest of the data
headers = lines[0].split()
service_data = lines[1:]
# Initialize the result dictionary
services = {}
# Iterate over each line of data after the headers
for line in service_data:
# Split the line by tabs or multiple spaces
parts = line.split()
# Create a dictionary entry using the header names
service_info = {headers[1]: parts[1], headers[2]: parts[2]} # State # Ports
# Add to the result dictionary using the service name as the key
services[parts[0]] = service_info
return services