Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update morpheus dev container to support flag for building with debug python build + source files. #81

Merged
8 commits merged into from
May 17, 2022
7 changes: 4 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ This workflow utilizes a docker container to set up most dependencies ensuring a
DOCKER_IMAGE_TAG=my_tag ./docker/build_container_dev.sh
```
Would build the container `morpheus:my_tag`.
1. To build the container with a debugging version of cpython installed, modify your build command as:
1. To build the container with a debugging version of cpython installed, update the docker target as follows:
```shell
DOCKER_EXTRA_ARGS="--build-arg MORPHEUS_WITH_PYDEBUG=true" docker/build_container_dev.sh
DOCKER_TARGET=development_pydbg ./docker/build_container_dev.sh
```
1. Note: When debugging python code, you just need to add `/usr/local/src/python-dbg` to your debugger's source path.
1. Note: When debugging python code, you just need to add `ci/conda/recipes/python-dbg/source` to your debugger's
source path.
1. Note: Now when running the container, conda should list your python version as `pyxxx_dbg_morpheus`.
```shell
(morpheus) user@host:/workspace# conda list | grep python
Expand Down
4 changes: 2 additions & 2 deletions ci/conda/recipes/python-dbg/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ make install


cd $PREFIX/bin
ln -s python${PYTHON_VER} python
ln -s pydoc${PYTHON_VER} pydoc
ln -s python${PY_VER} python
ln -s pydoc${PY_VER} pydoc
119 changes: 119 additions & 0 deletions ci/conda/recipes/python_dbg_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# SPDX-FileCopyrightText: Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
set -x
drobison00 marked this conversation as resolved.
Show resolved Hide resolved

function usage {
echo "$0 -s [CPYTHON_SOURCE] -p [CONDA_INSTALL_FILE_PATH] -i [SOURCE_INSTALL_PATH] -g [GDBINIT_INSTALL_PATH]"

exit 0
}

function help {
echo <<-DOC
Usage: $0 [OPTION]...
Install debug version of cpython, into the current conda environment, which has previously been downloaded/built.

Arguments:
-s Path to cpython (s)ource tarball, source files will be extracted and installed to path specified by '-i'.
-c Path to cpython (c)onda tarball, this is what will be installed into the conda environment.
-i Path where cpython source files will be installed.
Default: ci/conda/recipes/python-dbg/source
-g Path to install point for cpython 'gdbinit' file. Ignored if empty.
Note: Requires cpython source install point '-i' to be specified so that we can locate the gdb macros.
DOC

exit 0
}

function set_py_ver {
## Get the version information
PYDEBUG_VERSION=$(python --version | cut -d ' ' -f2 )
PYDEBUG_VERSION_MAJ_MIN=$(echo "${PYDEBUG_VERSION}" | awk '{split($0,ver,"."); print ver[1] "." ver[2]}')
}

PYDEBUG_CONDA_INSTALL_FILE=""
PYDEBUG_INSTALL_GDB_PATH=""
PYDEBUG_INSTALL_GDB_PATH=""
PYDEBUG_INSTALL_PATH=${PWD}/ci/conda/recipes/python-dbg/source
PYDEBUG_VERSION=""
PYDEBUG_VERSION_MAJ_MIN=""

while getopts "s:c:i:gh" opt; do
case ${opt} in
s) PYDEBUG_SOURCE=${OPTARG};;
c) PYDEBUG_CONDA_INSTALL_FILE=${OPTARG};;
i) PYDEBUG_INSTALL_PATH=${OPTARG};;
g) PYDEBUG_INSTALL_GDB_PATH=${OPTARG};;
h) help;;
*) usage;;
esac
done

# Install conda package
if [ -n "${PYDEBUG_CONDA_INSTALL_FILE}" ]; then
if [ ! -f "${PYDEBUG_CONDA_INSTALL_FILE}" ]; then
echo "Conda install file does not exist or is inaccessible: ${PYDEBUG_CONDA_INSTALL_FILE}"
exit 1
else
echo "Installing cpython debug build: ${PYDEBUG_CONDA_INSTALL_FILE}"
mamba install --use-local "${PYDEBUG_CONDA_INSTALL_FILE}"

set_py_ver
## Conda package will install python source to python3.xd (for development), which the CMake configure files won't find
## Copy the includes to the python3.x folder so CMake can find them.
cp -R /opt/conda/envs/morpheus/include/python${PYDEBUG_VERSION_MAJ_MIN}d/* \
/opt/conda/envs/morpheus/include/python${PYDEBUG_VERSION_MAJ_MIN}
fi
else
echo "No Conda install file specified, skipping..."
set_py_ver
fi

# Install cpython source files
if [[ -n ${PYDEBUG_SOURCE} && -n ${PYDEBUG_INSTALL_PATH} ]]; then
if [[ ! -f ${PYDEBUG_SOURCE} ]]; then
echo "Cpython source file does not exist or is inaccessible: ${PYDEBUG_SOURCE}"
exit 1
fi

if [[ ! -f ${PYDEBUG_INSTALL_PATH} ]]; then
mkdir -p "${PYDEBUG_INSTALL_PATH}"
fi

# Extract cpython source to /workspace \
for src_dir in Include Misc Modules Objects Python; do
tar --strip-components=1 --extract --wildcards --file="${PYDEBUG_SOURCE}" Python-${PYDEBUG_VERSION}/${src_dir}/*
mv ./${src_dir} "${PYDEBUG_INSTALL_PATH}/${src_dir}"
done
else
echo "Missing cpython source tarball or install path, skipping..."
fi

# Install GDB init macros
if [[ -f ${PYDEBUG_INSTALL_PATH} ]]; then
# Move cpython gdb helper macros to ${HOME}/.gdbinit \
# See: https://github.com/python/cpython/blob/main/Misc/gdbinit
if [[ "${PYDEBUG_INSTALL_GDB_PATH}" != "" ]]; then
GDB_SRC_PATH="${PYDEBUG_INSTALL_PATH}/Misc/gdbinit"
if [[ ! -f "${GDB_SRC_PATH}" ]]; then
echo "gdbinit path does not exist or is inaccessible: ${GDB_SRC_PATH}"
exit 1
fi

cp "${GDB_SRC_PATH}" "${PYDEBUG_INSTALL_GDB_PATH}"
fi
fi
9 changes: 9 additions & 0 deletions ci/conda/recipes/run_conda_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,12 @@ if hasArg morpheus; then
conda ${CONDA_COMMAND} "${CONDA_ARGS_ARRAY[@]}" ${CONDA_ARGS} ci/conda/recipes/morpheus
set +x
fi

if hasArg pydebug; then
export MORPHEUS_PYTHON_VER=$(python --version | cut -d ' ' -f 2)

echo "Running conda-build for python-dbg..."
set -x
conda ${CONDA_COMMAND} "${CONDA_ARGS_ARRAY[@]}" ${CONDA_ARGS} ./ci/conda/recipes/python-dbg
set +x
fi
71 changes: 23 additions & 48 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ RUN --mount=type=ssh \
source activate base &&\
# Run with --no-test for now until we can build with builtkit and default nvidia runtime
# Temp add CONDA_CHANNEL_ALIAS to get around conda-build 404 errors
MORPHEUS_ROOT=/workspace CONDA_BLD_DIR=/opt/conda/conda-bld CONDA_ARGS="--no-test" ./ci/conda/recipes/run_conda_build.sh libcudf cudf
MORPHEUS_ROOT=/workspace CONDA_BLD_PATH=/opt/conda/conda-bld CONDA_ARGS="--no-test" ./ci/conda/recipes/run_conda_build.sh libcudf cudf

# ============ Stage: conda_env ============
# Create the conda environment and install all dependencies
Expand Down Expand Up @@ -146,7 +146,7 @@ RUN --mount=type=ssh \
--mount=type=cache,id=conda_pkgs,target=/opt/conda/pkgs,sharing=locked \
source activate base &&\
# Temp add CONDA_CHANNEL_ALIAS to get around conda-build 404 errors
MORPHEUS_ROOT=/workspace CONDA_BLD_DIR=/opt/conda/conda-bld CONDA_ARGS="--no-test" ./ci/conda/recipes/run_conda_build.sh morpheus
MORPHEUS_ROOT=/workspace CONDA_BLD_PATH=/opt/conda/conda-bld CONDA_ARGS="--no-test" ./ci/conda/recipes/run_conda_build.sh morpheus

# ============ Stage: runtime ============
# Setup container for runtime environment
Expand Down Expand Up @@ -178,31 +178,9 @@ COPY ["*.md", "LICENSE", "./"]
# Use morpheus by default
CMD [ "morpheus" ]

# ============ Stage: development_deps ===========
# Configure and build cpython with debugging symbols
FROM conda_env_dev as development_deps

# Capture python version
ARG PYTHON_VER
COPY ci/conda/recipes/python-dbg/ ./ci/conda/recipes/python-dbg

# Flag indicating if we're going to build cpython
ARG MORPHEUS_WITH_PYDEBUG=0

RUN if [[ "${MORPHEUS_WITH_PYDEBUG}" =~ ^(TRUE|True|true|1)$ ]]; then \
## Figure out which python version is in the existing environment -- we will build its debug equivalent \
source activate morpheus \
&& \
MORPHEUS_PYTHON_VER=$(python --version | cut -d ' ' -f 2) \
CONDA_BLD_PATH=/opt/conda/conda-bld \
CONDA_ARGS="--no-test" \
conda mambabuild -c conda-forge -c system ./ci/conda/recipes/python-dbg \
; \
fi

# ============ Stage: development ============
# Install and configure development only packages
FROM development_deps as development
FROM conda_env_dev as development
# Copy the source
# COPY . ./

Expand All @@ -214,28 +192,25 @@ RUN npm install -g camouflage-server
# development
RUN git config --global --add safe.directory "*"

# ============ Stage: python_debug_bld ===========
# Configure and build cpython with debugging symbols
FROM development as development_pydbg

COPY ci/conda/recipes/python-dbg/ ./ci/conda/recipes/python-dbg
COPY ci/conda/recipes/run_conda_build.sh ./ci/conda/recipes/run_conda_build.sh
COPY ci/conda/recipes/python_dbg_install.sh ./ci/conda/recipes/python_dbg_install.sh

# Temporary until #68 goes in
ARG MORPHEUS_USER="root"
RUN --mount=type=bind,from=development_deps,source=/opt/conda/conda-bld,target=/opt/conda/conda-bld \
# swap out python binary if it was built \
if [[ "${MORPHEUS_WITH_PYDEBUG}" =~ ^(TRUE|True|true|1)$ ]]; then \
source activate morpheus \
&& export source_file=$( ls /opt/conda/conda-bld/src_cache/Python-${PYTHON_VER}*.tar.xz ) \
&& export install_file=$( ls /opt/conda/conda-bld/linux-64/python-${PYTHON_VER}*.tar.bz2 ) \
&& mamba install --use-local ${install_file} \
&& export debug_py_ver=$(python --version | cut -d ' ' -f2) \
## Copy python includes from python3.xd to python3.x so CMake can find them \
&& cp -R /opt/conda/envs/morpheus/include/python${PYTHON_VER}d/* \
/opt/conda/envs/morpheus/include/python${PYTHON_VER} \
# Extract cpython source to /workspace \
&& for src in Include Misc Modules Objects Python; do \
tar --strip-components=1 --extract --wildcards --file=${source_file} Python-${debug_py_ver}/${src}/* \
&& mkdir -p /usr/local/src/python-dbg/${src} \
&& cp -R ./${src} /usr/local/src/python-dbg/${src} \
; \
done \
# Move cpython gdb helper macros to ${HOME}/.gdbinit \
# See: https://github.com/python/cpython/blob/main/Misc/gdbinit
&& cp Misc/gdbinit $(eval echo "~${MORPHEUS_USER}")/.gdbinit \
; \
fi

# Build and install debug cpython
RUN source activate morpheus \
&& MORPHEUS_ROOT=/workspace \
CONDA_BLD_PATH=/opt/conda/conda-bld \
CONDA_ARGS="--no-test" \
./ci/conda/recipes/run_conda_build.sh pydebug \
&& ./ci/conda/recipes/python_dbg_install.sh \
-s $( ls /opt/conda/conda-bld/src_cache/Python-${PYTHON_VER}*.tar.xz ) \
-c $( ls /opt/conda/conda-bld/linux-64/python-${PYTHON_VER}*.tar.bz2 ) \
-i ./ci/conda/recipes/python-dbg/source \
-g $(eval echo "~${MORPHEUS_USER}")/.gdbinit