Kosasih commited on
Commit
0460e86
·
verified ·
1 Parent(s): cadd1df

Create deploy/deploy_utils.py

Browse files
Files changed (1) hide show
  1. deploy/deploy_utils.py +218 -0
deploy/deploy_utils.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ OmniCoreX Deployment Utilities
3
+
4
+ This module contains advanced deployment configurations and helper functions
5
+ to facilitate containerization, cloud deployment, and robust model serving
6
+ for the OmniCoreX system.
7
+
8
+ Features:
9
+ - Dockerfile and Kubernetes manifest generation helpers.
10
+ - Environment variables management with secure handling.
11
+ - Health check and readiness probe utilities.
12
+ - Automated setup scripts for cloud environments.
13
+ - Utilities for scalable and fault-tolerant serving.
14
+ """
15
+
16
+ import os
17
+ import json
18
+ import subprocess
19
+ from typing import Dict, Optional
20
+
21
+ def generate_dockerfile(base_image: str = "python:3.10-slim",
22
+ requirements_file: str = "requirements.txt",
23
+ workdir: str = "/app",
24
+ entry_point: str = "scripts/deploy_server.py",
25
+ expose_port: int = 8000) -> str:
26
+ """
27
+ Generates a Dockerfile string for OmniCoreX deployment.
28
+
29
+ Args:
30
+ base_image: Base Docker image.
31
+ requirements_file: Path to Python requirements file.
32
+ workdir: Working directory inside container.
33
+ entry_point: Path to container entry script.
34
+ expose_port: Port to expose.
35
+
36
+ Returns:
37
+ Dockerfile content as string.
38
+ """
39
+ dockerfile = f"""
40
+ FROM {base_image}
41
+
42
+ WORKDIR {workdir}
43
+
44
+ COPY . {workdir}/
45
+
46
+ RUN pip install --no-cache-dir -r {requirements_file}
47
+
48
+ EXPOSE {expose_port}
49
+
50
+ CMD ["python", "{entry_point}", "--host", "0.0.0.0", "--port", "{expose_port}"]
51
+ """
52
+ return dockerfile.strip()
53
+
54
+ def save_dockerfile(path: str = "Dockerfile", **kwargs) -> None:
55
+ """
56
+ Saves generated Dockerfile to specified path.
57
+
58
+ Args:
59
+ path: File path to save Dockerfile.
60
+ kwargs: Arguments to generate_dockerfile.
61
+ """
62
+ content = generate_dockerfile(**kwargs)
63
+ with open(path, "w", encoding="utf-8") as f:
64
+ f.write(content)
65
+ print(f"Dockerfile saved to {path}")
66
+
67
+ def generate_k8s_deployment(deployment_name: str,
68
+ image_name: str,
69
+ replicas: int = 1,
70
+ container_port: int = 8000,
71
+ cpu_request: str = "500m",
72
+ mem_request: str = "1Gi",
73
+ cpu_limit: str = "1",
74
+ mem_limit: str = "2Gi") -> Dict:
75
+ """
76
+ Generates a Kubernetes deployment manifest dictionary for OmniCoreX.
77
+
78
+ Args:
79
+ deployment_name: Name of K8s deployment.
80
+ image_name: Docker image to use.
81
+ replicas: Number of pods.
82
+ container_port: Port exposed by container.
83
+ cpu_request: Requested CPU.
84
+ mem_request: Requested memory.
85
+ cpu_limit: CPU limit.
86
+ mem_limit: Memory limit.
87
+
88
+ Returns:
89
+ Dict representing K8s deployment YAML.
90
+ """
91
+ deployment = {
92
+ "apiVersion": "apps/v1",
93
+ "kind": "Deployment",
94
+ "metadata": {"name": deployment_name},
95
+ "spec": {
96
+ "replicas": replicas,
97
+ "selector": {"matchLabels": {"app": deployment_name}},
98
+ "template": {
99
+ "metadata": {"labels": {"app": deployment_name}},
100
+ "spec": {
101
+ "containers": [{
102
+ "name": deployment_name,
103
+ "image": image_name,
104
+ "ports": [{"containerPort": container_port}],
105
+ "resources": {
106
+ "requests": {
107
+ "cpu": cpu_request,
108
+ "memory": mem_request
109
+ },
110
+ "limits": {
111
+ "cpu": cpu_limit,
112
+ "memory": mem_limit
113
+ }
114
+ },
115
+ "readinessProbe": {
116
+ "httpGet": {
117
+ "path": "/health",
118
+ "port": container_port
119
+ },
120
+ "initialDelaySeconds": 5,
121
+ "periodSeconds": 10
122
+ },
123
+ "livenessProbe": {
124
+ "httpGet": {
125
+ "path": "/health",
126
+ "port": container_port
127
+ },
128
+ "initialDelaySeconds": 10,
129
+ "periodSeconds": 30
130
+ }
131
+ }]
132
+ }
133
+ }
134
+ }
135
+ }
136
+ return deployment
137
+
138
+ def save_k8s_manifest(manifest: Dict, path: str = "k8s_deployment.yaml") -> None:
139
+ """
140
+ Saves K8s manifest dict as YAML file.
141
+
142
+ Args:
143
+ manifest: Dictionary of K8s manifest.
144
+ path: File path to save manifest YAML.
145
+ """
146
+ import yaml
147
+ with open(path, "w", encoding="utf-8") as f:
148
+ yaml.dump(manifest, f)
149
+ print(f"Kubernetes manifest saved to {path}")
150
+
151
+ def build_docker_image(image_tag: str, dockerfile_path: str = "Dockerfile", context: str = ".") -> None:
152
+ """
153
+ Builds a docker image using docker CLI.
154
+
155
+ Args:
156
+ image_tag: Name and tag of docker image (e.g., "omnicorex:latest").
157
+ dockerfile_path: Path to Dockerfile.
158
+ context: Build context directory.
159
+ """
160
+ cmd = ["docker", "build", "-t", image_tag, "-f", dockerfile_path, context]
161
+ print(f"Running: {' '.join(cmd)}")
162
+ subprocess.run(cmd, check=True)
163
+ print(f"Built docker image: {image_tag}")
164
+
165
+ def push_docker_image(image_tag: str) -> None:
166
+ """
167
+ Pushes docker image to registry.
168
+
169
+ Args:
170
+ image_tag: Docker image tag.
171
+ """
172
+ cmd = ["docker", "push", image_tag]
173
+ print(f"Running: {' '.join(cmd)}")
174
+ subprocess.run(cmd, check=True)
175
+ print(f"Pushed docker image: {image_tag}")
176
+
177
+ def setup_env_vars(env_vars: Dict[str, str], path: str = ".env") -> None:
178
+ """
179
+ Writes environment variables to a .env file for deployment configuration.
180
+
181
+ Args:
182
+ env_vars: Dict of environment variables to write.
183
+ path: Path to .env file.
184
+ """
185
+ with open(path, "w", encoding="utf-8") as f:
186
+ for key, val in env_vars.items():
187
+ f.write(f"{key}={val}\n")
188
+ print(f"Environment variables saved in {path}")
189
+
190
+ if __name__ == "__main__":
191
+ # Example usage: generate dockerfile and k8s manifest
192
+
193
+ save_dockerfile(
194
+ base_image="python:3.10-slim",
195
+ requirements_file="requirements.txt",
196
+ workdir="/app",
197
+ entry_point="scripts/deploy_server.py",
198
+ expose_port=8000
199
+ )
200
+
201
+ deployment_manifest = generate_k8s_deployment(
202
+ deployment_name="omnicorex",
203
+ image_name="kosasih/omnicorex:latest",
204
+ replicas=2,
205
+ container_port=8000
206
+ )
207
+ save_k8s_manifest(deployment_manifest)
208
+
209
+ # Example environment variables setup
210
+ env_vars = {
211
+ "MODEL_PATH": "/app/checkpoints/checkpoint.pt",
212
+ "LOG_LEVEL": "INFO",
213
+ "NUM_WORKERS": "4"
214
+ }
215
+ setup_env_vars(env_vars)
216
+
217
+ print("Deployment utilities demo complete.")
218
+