File size: 5,314 Bytes
85b7ac8 | 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 | """Docker build/run simulator with deterministic rule-based validation."""
from __future__ import annotations
from typing import Dict, List, Optional, Set
from server.models import FileContent
class DockerSimulator:
VALID_INSTRUCTIONS: Set[str] = {
"FROM",
"RUN",
"CMD",
"LABEL",
"MAINTAINER",
"EXPOSE",
"ENV",
"ADD",
"COPY",
"ENTRYPOINT",
"VOLUME",
"USER",
"WORKDIR",
"ARG",
"ONBUILD",
"STOPSIGNAL",
"HEALTHCHECK",
"SHELL",
}
def _split_lines(self, content: str) -> List[str]:
return [line.rstrip() for line in content.split("\n")]
def _non_empty_non_comment_lines(self, lines: List[str]) -> List[str]:
return [line.strip() for line in lines if line.strip() and not line.strip().startswith("#")]
def _source_exists(self, source: str, context_files: Dict[str, FileContent]) -> bool:
if source in {".", "./"}:
return True
if "*" in source:
prefix = source.replace("*", "")
return any(path.startswith(prefix) for path in context_files)
return source in context_files
def validate(self, dockerfile: Optional[FileContent], context_files: Dict[str, FileContent]):
if dockerfile is None:
return {"build_success": False, "run_success": False, "error": "Dockerfile missing"}
content = dockerfile.content
lines = self._split_lines(content)
active_lines = self._non_empty_non_comment_lines(lines)
if not active_lines:
return {"build_success": False, "run_success": False, "error": "Dockerfile is empty"}
if not active_lines[0].upper().startswith("FROM "):
return {
"build_success": False,
"run_success": False,
"error": "Dockerfile must start with FROM",
}
for idx, raw in enumerate(active_lines, start=1):
token = raw.split()[0].upper()
if token.startswith("&&"):
return {
"build_success": False,
"run_success": False,
"error": f"Dockerfile parse error: unknown instruction: {token}",
"line": idx,
}
if token not in self.VALID_INSTRUCTIONS:
return {
"build_success": False,
"run_success": False,
"error": f"Dockerfile parse error: unknown instruction: {token}",
"line": idx,
}
if "FROM python:3.9-slimm" in content:
return {
"build_success": False,
"run_success": False,
"error": "pull access denied for python:3.9-slimm",
}
for raw in active_lines:
upper = raw.upper()
if upper.startswith("COPY "):
parts = raw.split()
if len(parts) < 3:
return {
"build_success": False,
"run_success": False,
"error": "COPY requires source and destination",
}
src = parts[1]
if src.startswith("--from=") and len(parts) >= 4:
src = parts[2]
if src.startswith("--"):
continue
if not self._source_exists(src, context_files):
return {
"build_success": False,
"run_success": False,
"error": f"COPY failed: file not found in build context: {src}",
}
if "--platform=$BUILDPLATFORM" in content and "ARG BUILDPLATFORM" not in content:
return {
"build_success": False,
"run_success": False,
"error": "failed to parse platform: BUILDPLATFORM not declared",
}
if "--platform=$TARGETPLATFORM" in content and "ARG TARGETPLATFORM" not in content:
return {
"build_success": False,
"run_success": False,
"error": "failed to parse platform: TARGETPLATFORM not declared",
}
if "COPY --from=builder /app/dist" in content:
pkg = context_files.get("package.json")
if pkg and "react-scripts build" in pkg.content:
return {
"build_success": False,
"run_success": False,
"error": "COPY failed: stat app/dist: file does not exist",
}
if "requirments.txt" in content:
return {
"build_success": False,
"run_success": False,
"error": "COPY failed: file not found in build context: requirments.txt",
}
if ("npm start" in content or 'CMD ["npm", "start"]' in content) and "WORKDIR /app" not in content:
return {
"build_success": True,
"run_success": False,
"run_error": "Error: Cannot find module '/package.json'",
}
return {"build_success": True, "run_success": True}
|