Skip to content

Commit 49e9872

Browse files
authored
Merge pull request #26 from forcedotcom/allow-native-dependencies
Build requirements as native dependencies
2 parents 99b8e9e + 32ef3e3 commit 49e9872

File tree

4 files changed

+436
-20
lines changed

4 files changed

+436
-20
lines changed

src/datacustomcode/deploy.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import json
1919
import os
2020
import shutil
21-
import tarfile
2221
import tempfile
2322
import time
2423
from typing import (
@@ -140,8 +139,13 @@ def create_deployment(
140139
raise
141140

142141

143-
DOCKER_IMAGE_NAME = "datacloud-custom-code"
144-
DEPENDENCIES_ARCHIVE_NAME = "dependencies.tar.gz"
142+
PLATFORM_ENV_VAR = "DOCKER_DEFAULT_PLATFORM=linux/amd64"
143+
DOCKER_IMAGE_NAME = "datacloud-custom-code-dependency-builder"
144+
DEPENDENCIES_ARCHIVE_NAME = "native_dependencies"
145+
DEPENDENCIES_ARCHIVE_FULL_NAME = f"{DEPENDENCIES_ARCHIVE_NAME}.tar.gz"
146+
DEPENDENCIES_ARCHIVE_PATH = os.path.join(
147+
"payload", "archives", DEPENDENCIES_ARCHIVE_FULL_NAME
148+
)
145149
ZIP_FILE_NAME = "deployment.zip"
146150

147151

@@ -150,31 +154,28 @@ def prepare_dependency_archive(directory: str) -> None:
150154
image_exists = cmd_output(cmd)
151155

152156
if not image_exists:
153-
logger.debug("Building docker image...")
154-
cmd = f"docker build -t {DOCKER_IMAGE_NAME} ."
157+
logger.info("Building docker image...")
158+
cmd = (
159+
f"{PLATFORM_ENV_VAR} docker build -t {DOCKER_IMAGE_NAME} "
160+
f"-f Dockerfile.dependencies ."
161+
)
155162
cmd_output(cmd)
156163

157164
with tempfile.TemporaryDirectory() as temp_dir:
165+
logger.info("Building dependencies archive")
158166
shutil.copy("requirements.txt", temp_dir)
167+
shutil.copy("build_native_dependencies.sh", temp_dir)
159168
cmd = (
160-
f"docker run --rm "
161-
f"-v {temp_dir}:/dependencies "
162-
f"{DOCKER_IMAGE_NAME} "
163-
f'/bin/bash -c "cd /dependencies && pip download -r requirements.txt"'
169+
f"{PLATFORM_ENV_VAR} docker run --rm "
170+
f"-v {temp_dir}:/workspace "
171+
f"{DOCKER_IMAGE_NAME}"
164172
)
165173
cmd_output(cmd)
174+
archives_temp_path = os.path.join(temp_dir, DEPENDENCIES_ARCHIVE_FULL_NAME)
175+
os.makedirs(os.path.dirname(DEPENDENCIES_ARCHIVE_PATH), exist_ok=True)
176+
shutil.copy(archives_temp_path, DEPENDENCIES_ARCHIVE_PATH)
166177

167-
archives_dir = os.path.join(directory, "archives")
168-
os.makedirs(archives_dir, exist_ok=True)
169-
archive_file = os.path.join(archives_dir, DEPENDENCIES_ARCHIVE_NAME)
170-
with tarfile.open(archive_file, "w:gz") as tar:
171-
for file in os.listdir(temp_dir):
172-
# Exclude requirements.txt from the archive
173-
if file == "requirements.txt":
174-
continue
175-
tar.add(os.path.join(temp_dir, file), arcname=file)
176-
177-
logger.debug(f"Dependencies downloaded and archived to {archive_file}")
178+
logger.info(f"Dependencies archived to {DEPENDENCIES_ARCHIVE_PATH}")
178179

179180

180181
class DeploymentsResponse(BaseModel):
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM public.ecr.aws/emr-on-eks/spark/emr-7.3.0:latest
2+
3+
USER root
4+
5+
RUN pip3.11 install venv-pack
6+
7+
# Create workspace directory
8+
RUN mkdir /workspace
9+
WORKDIR /workspace
10+
11+
CMD ["./build_native_dependencies.sh"]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
# Description: build native dependencies
4+
5+
python3.11 -m venv --copies .venv
6+
source .venv/bin/activate
7+
pip install -r requirements.txt
8+
venv-pack -o native_dependencies.tar.gz -f

0 commit comments

Comments
 (0)