Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
singlestore-labs
GitHub Repository: singlestore-labs/singlestoredb-python
Path: blob/main/docs/src/singlestoredb-docker.py
469 views
1
#!/usr/bin/env python3
2
"""
3
Manage SingleStoreDB Docker container for documentation generation.
4
5
Usage:
6
singlestoredb-docker.py start - Start container and output connection URL
7
singlestoredb-docker.py stop - Stop and remove container
8
"""
9
from __future__ import annotations
10
11
import argparse
12
import os
13
import subprocess
14
import sys
15
import time
16
17
18
CONTAINER_INFO_FILE = '.singlestore_docs_container.txt'
19
20
21
def start_container() -> int:
22
"""Start SingleStoreDB Docker container and return connection URL."""
23
try:
24
from singlestoredb.server import docker
25
except ImportError:
26
print('Error: singlestoredb package not installed', file=sys.stderr)
27
print('Please install it with: pip install singlestoredb', file=sys.stderr)
28
return 1
29
30
print('Starting SingleStoreDB Docker container for documentation...', file=sys.stderr)
31
32
# Start the container
33
server = docker.start()
34
35
# Get the connection URL
36
conn_url = server.connection_url
37
38
# Save the container info for stopping later
39
with open(CONTAINER_INFO_FILE, 'w') as f:
40
f.write(f'{server.container.name}\n')
41
f.write(f'{conn_url}\n')
42
43
# Wait for the container to be ready
44
print('Waiting for SingleStoreDB to be ready...', file=sys.stderr)
45
max_retries = 30
46
47
for attempt in range(max_retries):
48
try:
49
test_conn = server.connect()
50
test_conn.close()
51
print(f'SingleStoreDB is ready! (took {attempt + 1} seconds)', file=sys.stderr)
52
break
53
except Exception as e:
54
if attempt == max_retries - 1:
55
print(f'Error: Container failed to start after {max_retries} seconds', file=sys.stderr)
56
print(f'Last error: {e}', file=sys.stderr)
57
return 1
58
time.sleep(1)
59
60
# Output the connection URL (this is what the Makefile will capture)
61
print(conn_url)
62
return 0
63
64
65
def stop_container() -> int:
66
"""Stop and remove the SingleStoreDB Docker container."""
67
if not os.path.exists(CONTAINER_INFO_FILE):
68
print('No container info file found. Nothing to stop.', file=sys.stderr)
69
return 0
70
71
try:
72
with open(CONTAINER_INFO_FILE, 'r') as f:
73
lines = f.readlines()
74
if not lines:
75
print('Container info file is empty. Nothing to stop.', file=sys.stderr)
76
return 0
77
78
container_name = lines[0].strip()
79
80
print(f'Stopping SingleStoreDB container: {container_name}', file=sys.stderr)
81
82
# Stop the container
83
subprocess.run(
84
['docker', 'stop', container_name],
85
stdout=subprocess.DEVNULL,
86
stderr=subprocess.DEVNULL,
87
)
88
89
# Remove the container
90
subprocess.run(
91
['docker', 'rm', container_name],
92
stdout=subprocess.DEVNULL,
93
stderr=subprocess.DEVNULL,
94
)
95
96
# Remove the info file
97
os.remove(CONTAINER_INFO_FILE)
98
99
print('SingleStoreDB container stopped and removed.', file=sys.stderr)
100
return 0
101
102
except Exception as e:
103
print(f'Error stopping container: {e}', file=sys.stderr)
104
# Still try to remove the info file
105
if os.path.exists(CONTAINER_INFO_FILE):
106
os.remove(CONTAINER_INFO_FILE)
107
return 1
108
109
110
def main() -> int:
111
"""Main entry point."""
112
parser = argparse.ArgumentParser(
113
description='Manage SingleStoreDB Docker container for documentation generation',
114
)
115
parser.add_argument(
116
'command',
117
choices=['start', 'stop'],
118
help='Command to execute',
119
)
120
121
args = parser.parse_args()
122
123
if args.command == 'start':
124
return start_container()
125
elif args.command == 'stop':
126
return stop_container()
127
else:
128
parser.print_help()
129
return 1
130
131
132
if __name__ == '__main__':
133
sys.exit(main())
134
135