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