Path: blob/main/docs/src/singlestoredb-docker.py
469 views
#!/usr/bin/env python31"""2Manage SingleStoreDB Docker container for documentation generation.34Usage:5singlestoredb-docker.py start - Start container and output connection URL6singlestoredb-docker.py stop - Stop and remove container7"""8from __future__ import annotations910import argparse11import os12import subprocess13import sys14import time151617CONTAINER_INFO_FILE = '.singlestore_docs_container.txt'181920def start_container() -> int:21"""Start SingleStoreDB Docker container and return connection URL."""22try:23from singlestoredb.server import docker24except ImportError:25print('Error: singlestoredb package not installed', file=sys.stderr)26print('Please install it with: pip install singlestoredb', file=sys.stderr)27return 12829print('Starting SingleStoreDB Docker container for documentation...', file=sys.stderr)3031# Start the container32server = docker.start()3334# Get the connection URL35conn_url = server.connection_url3637# Save the container info for stopping later38with open(CONTAINER_INFO_FILE, 'w') as f:39f.write(f'{server.container.name}\n')40f.write(f'{conn_url}\n')4142# Wait for the container to be ready43print('Waiting for SingleStoreDB to be ready...', file=sys.stderr)44max_retries = 304546for attempt in range(max_retries):47try:48test_conn = server.connect()49test_conn.close()50print(f'SingleStoreDB is ready! (took {attempt + 1} seconds)', file=sys.stderr)51break52except Exception as e:53if attempt == max_retries - 1:54print(f'Error: Container failed to start after {max_retries} seconds', file=sys.stderr)55print(f'Last error: {e}', file=sys.stderr)56return 157time.sleep(1)5859# Output the connection URL (this is what the Makefile will capture)60print(conn_url)61return 0626364def stop_container() -> int:65"""Stop and remove the SingleStoreDB Docker container."""66if not os.path.exists(CONTAINER_INFO_FILE):67print('No container info file found. Nothing to stop.', file=sys.stderr)68return 06970try:71with open(CONTAINER_INFO_FILE, 'r') as f:72lines = f.readlines()73if not lines:74print('Container info file is empty. Nothing to stop.', file=sys.stderr)75return 07677container_name = lines[0].strip()7879print(f'Stopping SingleStoreDB container: {container_name}', file=sys.stderr)8081# Stop the container82subprocess.run(83['docker', 'stop', container_name],84stdout=subprocess.DEVNULL,85stderr=subprocess.DEVNULL,86)8788# Remove the container89subprocess.run(90['docker', 'rm', container_name],91stdout=subprocess.DEVNULL,92stderr=subprocess.DEVNULL,93)9495# Remove the info file96os.remove(CONTAINER_INFO_FILE)9798print('SingleStoreDB container stopped and removed.', file=sys.stderr)99return 0100101except Exception as e:102print(f'Error stopping container: {e}', file=sys.stderr)103# Still try to remove the info file104if os.path.exists(CONTAINER_INFO_FILE):105os.remove(CONTAINER_INFO_FILE)106return 1107108109def main() -> int:110"""Main entry point."""111parser = argparse.ArgumentParser(112description='Manage SingleStoreDB Docker container for documentation generation',113)114parser.add_argument(115'command',116choices=['start', 'stop'],117help='Command to execute',118)119120args = parser.parse_args()121122if args.command == 'start':123return start_container()124elif args.command == 'stop':125return stop_container()126else:127parser.print_help()128return 1129130131if __name__ == '__main__':132sys.exit(main())133134135