Skip to content

Commit

Permalink
[sonic-platform-common] Install Python 3 package in host OS and PMon …
Browse files Browse the repository at this point in the history
…container (sonic-net#5461)

Signed-off-by: Volodymyr Boyko <[email protected]>
  • Loading branch information
vboykox authored and santhosh-kt committed Feb 25, 2021
1 parent af75ba5 commit ffc3a10
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions dockers/docker-platform-monitor/Dockerfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && \
apt-get install -y \
python-pip \
python3-dev \
ipmitool \
librrd8 \
librrd-dev \
Expand All @@ -21,6 +22,9 @@ RUN apt-get update && \
i2c-tools && \
pip install enum34

# Install python3 required packages
RUN python3 -m pip install --no-cache-dir setuptools wheel

{% if docker_platform_monitor_debs.strip() -%}
# Copy locally-built Debian package dependencies
{{ copy_files("debs/", docker_platform_monitor_debs.split(' '), "/debs/") }}
Expand All @@ -47,6 +51,7 @@ RUN apt-get update && \

# Clean up
RUN apt-get purge -y \
python3-dev \
python-pip && \
apt-get clean -y && \
apt-get autoclean -y && \
Expand Down
17 changes: 17 additions & 0 deletions dockers/docker-platform-monitor/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,20 @@ if [ $? -ne 0 ]; then
echo "Error: Unable to locate ${SONIC_PLATFORM_WHEEL}"
fi
fi

# If the python3 sonic-platform package is not installed, try to install it
python3 -m pip show sonic-platform > /dev/null 2>&1
if [ $? -ne 0 ]; then
SONIC_PLATFORM_WHEEL="/usr/share/sonic/platform/sonic_platform-1.0-py3-none-any.whl"
echo "sonic-platform package not installed, attempting to install..."
if [ -e ${SONIC_PLATFORM_WHEEL} ]; then
python3 -m pip install ${SONIC_PLATFORM_WHEEL}
if [ $? -eq 0 ]; then
echo "Successfully installed ${SONIC_PLATFORM_WHEEL}"
else
echo "Error: Failed to install ${SONIC_PLATFORM_WHEEL}"
fi
else
echo "Error: Unable to locate ${SONIC_PLATFORM_WHEEL}"
fi
fi
19 changes: 18 additions & 1 deletion dockers/dockerfile-macros.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,31 @@ RUN dpkg_apt() { [ -f $1 ] && { dpkg -i $1 || apt-get -y install -f; } || return
{%- endfor %}
{%- endmacro %}

{% macro install_python_wheels(packages) -%}
{% macro install_python2_wheels(packages) -%}
RUN cd /python-wheels/ && pip install {{ packages | join(' ') }}
{%- endmacro %}

{% macro install_python3_wheels(packages) -%}
RUN cd /python-wheels/ && pip3 install {{ packages | join(' ') }}
{%- endmacro %}

{% macro install_python_wheels(packages) -%}
{%- set py2_pkgs, py3_pkgs = [], [] %}
{%- for pkg in packages %}
{%- if 'py3' in pkg %}
{{- py3_pkgs.append(pkg) or '' }}
{%- else %}
{{- py2_pkgs.append(pkg) or '' }}
{%- endif %}
{%- endfor %}
{%- if py3_pkgs | length %}
{{ install_python3_wheels(py3_pkgs) }}
{%- endif %}
{%- if py2_pkgs | length %}
{{ install_python2_wheels(py2_pkgs) }}
{%- endif %}
{%- endmacro %}

{% macro copy_files(prefix, files, dest) -%}
COPY \
{%- for file in files %}
Expand Down
6 changes: 6 additions & 0 deletions files/build_templates/sonic_debian_extension.j2
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ sudo cp {{platform_common_py2_wheel_path}} $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY2
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip install $PLATFORM_COMMON_PY2_WHEEL_NAME
sudo rm -rf $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY2_WHEEL_NAME

# Install sonic-platform-common Python 3 package
PLATFORM_COMMON_PY3_WHEEL_NAME=$(basename {{platform_common_py3_wheel_path}})
sudo cp {{platform_common_py3_wheel_path}} $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY3_WHEEL_NAME
sudo https_proxy=$https_proxy LANG=C chroot $FILESYSTEM_ROOT pip3 install $PLATFORM_COMMON_PY3_WHEEL_NAME
sudo rm -rf $FILESYSTEM_ROOT/$PLATFORM_COMMON_PY3_WHEEL_NAME

# Install Debian packages and their dependencies which are needed by sonic-utilities
sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install -f python-m2crypto

Expand Down
3 changes: 3 additions & 0 deletions rules/docker-platform-monitor.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ ifeq ($(CONFIGURED_PLATFORM),barefoot)
$(DOCKER_PLATFORM_MONITOR)_DEPENDS += $(PYTHON_THRIFT)
endif
$(DOCKER_PLATFORM_MONITOR)_PYTHON_WHEELS += $(SONIC_PLATFORM_COMMON_PY2)
$(DOCKER_PLATFORM_MONITOR)_PYTHON_WHEELS += $(SONIC_PLATFORM_COMMON_PY3)
$(DOCKER_PLATFORM_MONITOR)_PYTHON_WHEELS += $(SWSSSDK_PY2)
$(DOCKER_PLATFORM_MONITOR)_PYTHON_WHEELS += $(SWSSSDK_PY3)
$(DOCKER_PLATFORM_MONITOR)_PYTHON_WHEELS += $(SONIC_PY_COMMON_PY2)
$(DOCKER_PLATFORM_MONITOR)_PYTHON_WHEELS += $(SONIC_PY_COMMON_PY3)
$(DOCKER_PLATFORM_MONITOR)_PYTHON_WHEELS += $(SONIC_PLATFORM_API_PY2)
$(DOCKER_PLATFORM_MONITOR)_PYTHON_WHEELS += $(SONIC_LEDD)
$(DOCKER_PLATFORM_MONITOR)_PYTHON_WHEELS += $(SONIC_PCIED)
Expand Down
2 changes: 2 additions & 0 deletions slave.mk
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PY_COMMON_PY3)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_CONFIG_ENGINE)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_COMMON_PY2)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_COMMON_PY3)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(REDIS_DUMP_LOAD_PY2)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_API_PY2)) \
$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_YANG_MODELS_PY3)) \
Expand Down Expand Up @@ -851,6 +852,7 @@ $(addprefix $(TARGET_PATH)/, $(SONIC_INSTALLERS)) : $(TARGET_PATH)/% : \
export swsssdk_py2_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(SWSSSDK_PY2))"
export swsssdk_py3_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(SWSSSDK_PY3))"
export platform_common_py2_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_COMMON_PY2))"
export platform_common_py3_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(SONIC_PLATFORM_COMMON_PY3))"
export redis_dump_load_py2_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(REDIS_DUMP_LOAD_PY2))"
export redis_dump_load_py3_wheel_path="$(addprefix $(PYTHON_WHEELS_PATH)/,$(REDIS_DUMP_LOAD_PY3))"
export install_debug_image="$(INSTALL_DEBUG_TOOLS)"
Expand Down

0 comments on commit ffc3a10

Please sign in to comment.