FROM ubuntu:22.04 AS build

ARG VERSION=26.1.0
ARG TARGETPLATFORM

RUN apt update && apt install -y wget unzip &&\
    apt-get clean &&\
    rm -rf /var/lib/apt/lists/* &&\
    rm -rf /var/tmp/* &&\
    rm -rf /tmp/* &&\
    ARCH=$(case "${TARGETPLATFORM}" in \
        "linux/amd64") echo "x86_64" ;; \
        "linux/arm64") echo "aarch64" ;; \
        *) echo "Unsupported TARGETPLATFORM: ${TARGETPLATFORM}" && exit 1 ;; \
    esac) &&\
    wget https://gitcode.com/ascend/mind-cluster/releases/download/v${VERSION}/Ascend-mindxdl-noded_${VERSION}_linux-${ARCH}.zip &&\
    unzip Ascend-mindxdl-noded_${VERSION}_linux-${ARCH}.zip -d /build

FROM ubuntu:22.04

RUN usermod root -s /usr/sbin/nologin

ENV LD_LIBRARY_PATH /usr/local/Ascend/driver/lib64:/usr/local/Ascend/driver/lib64/driver:/usr/local/Ascend/driver/lib64/common

COPY --from=build /build/noded /usr/local/bin/noded
COPY --from=build /build/NodeDConfiguration.json /build/fdConfig.yaml /build/agreement.txt /usr/local/

RUN chmod 550 /usr/local/bin/noded &&\
    chmod 550 /usr/local/bin &&\
    chmod 440 /usr/local/NodeDConfiguration.json &&\
    chmod 440 /usr/local/fdConfig.yaml &&\
    chmod 440 /usr/local/agreement.txt &&\
    echo 'umask 027' >> /etc/profile &&\
    echo 'source /etc/profile' >> ~/.bashrc

ENTRYPOINT ["/bin/bash", "-c", "cat /usr/local/agreement.txt; exec /bin/bash"]