FROM node:16.15.1-bullseye-slim AS builder-fe
COPY requirements.txt .
RUN sed -i "s@http://\(deb\|security\).debian.org@http://mirrors.aliyun.com@g" /etc/apt/sources.list
RUN apt update
RUN apt update && apt install -y libpython3-dev  python3 python3-pip python-is-python3 make \
    && pip config set global.index-url https://mirror.baidu.com/pypi/simple \
    && pip install -U pip setuptools && pip install -r requirements.txt -i https://mirror.baidu.com/pypi/simple
    #  \
    # && yarn config set registry https://registry.npm.taobao.org \
    # && npm config set registry https://registry.npm.taobao.org \
    # && npm config set disturl https://npm.taobao.org/dist
#https://registry.npmmirror.com
COPY . /src/
ENV PYTHONPATH=/src
RUN cd /src && chmod a+x  ./bdist_wheel.sh && ./bdist_wheel.sh

FROM ubuntu:18.04 AS builder

RUN sed -i 's#archive.ubuntu.com#mirrors.aliyun.com#g' /etc/apt/sources.list  \
    && sed -i 's#security.ubuntu.com#mirrors.aliyun.com#g' /etc/apt/sources.list

ENV LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh LC_ALL=zh_CN.UTF-8 DEBIAN_FRONTEND=noninteractive

RUN rm -rf  /etc/apt/sources.list.d/  && apt update

RUN apt-get update && apt-get install -y --no-install-recommends \
    supervisor \
    iputils-ping \
    wget \
    zsh \
    build-essential \
    cmake \
    git \
    curl \
    vim \
    ca-certificates \
    libjpeg-dev \
    zip \
    unzip \
    libpng-dev \
    openssh-server \
    autojump \
    language-pack-zh-hans \
    ttf-wqy-zenhei \
    libgl1-mesa-glx  \
    libglib2.0-0 \
    locales &&\
    rm -rf /var/lib/apt/lists/*


RUN locale-gen zh_CN.UTF-8
RUN dpkg-reconfigure locales


CMD ["supervisord", "-n"]


FROM builder as builder1

ENV PYTHON_VERSION 3
RUN chsh -s `which zsh`
RUN curl -o ~/miniconda.sh -O  https://repo.anaconda.com/miniconda/Miniconda${PYTHON_VERSION}-latest-Linux-x86_64.sh  && \
    chmod +x ~/miniconda.sh && \
    ~/miniconda.sh -b -p /opt/conda && \
    rm ~/miniconda.sh

RUN ln /opt/conda/bin/conda /usr/local/bin/conda
RUN conda init zsh
RUN conda install mamba -n base -c conda-forge
RUN ln /opt/conda/bin/mamba /usr/local/bin/mamba && mamba init zsh



FROM builder1 as builder2
WORKDIR /workspace


RUN /opt/conda/bin/pip install jupyterlab jupyterlab-language-pack-zh-CN -i https://pypi.douban.com/simple
RUN mamba install nodejs -y
# password 'sx'
RUN /opt/conda/bin/jupyter lab --generate-config && \
    echo "c.NotebookApp.password='argon2:\$argon2id\$v=19\$m=10240,t=10,p=8\$+zIUCF9Uk2FiCHlV8njX5A\$I5Mm/64DORArcXYTXWRVng'">>/root/.jupyter/jupyter_lab_config.py


FROM builder2 as builder3

RUN apt-get update &&  apt-get install -y --no-install-recommends openjdk-8-jdk  krb5-user net-tools iputils-ping
RUN mamba install nodejs  sparkmagic  pyhive thrift sasl thrift_sasl scikit-learn -y
# RUN /opt/conda/bin/pip install sparkmagipc -i https://pypi.douban.com/simple
ENV PATH /opt/conda/bin/:$PATH
RUN jupyter nbextension enable --py --sys-prefix widgetsnbextension
RUN cd /opt/conda/lib/python3.9/site-packages/ \
    && jupyter-kernelspec install sparkmagic/kernels/sparkkernel \
    && jupyter-kernelspec install sparkmagic/kernels/pysparkkernel \
    && /opt/conda/bin/jupyter serverextension enable --py sparkmagic

EXPOSE 8888

# 内网sxkj
FROM builder3 as image-sxkj
COPY --from=builder-fe /src/dist  dist
RUN /opt/conda/bin/pip install dist/*.whl  -i https://mirror.baidu.com/pypi/simple
ADD confs/dev/config.json .
RUN mkdir -p $HOME/.sparkmagic && cp config.json $HOME/.sparkmagic
RUN echo "\
[program:jupyter]\n\
directory=/workspace\n\
command=/bin/bash -c '/opt/conda/bin/jupyter lab --ip 0.0.0.0 --port 8888 --allow-root --no-browser --NotebookApp.allow_origin=* --NotebookApp.allow_remote_access=1' \n\
autorestart=true\n\
startretries=0\n\
redirect_stderr=true\n\
stdout_logfile=/var/log/jupyter.log\n\
stdout_logfile_maxbytes=50MB\n\
" > /etc/supervisor/conf.d/jupyter.conf

# 腾讯云测试
FROM builder3 as image-txtest
COPY --from=builder-fe /src/dist  dist
RUN /opt/conda/bin/pip install dist/*.whl  -i https://mirror.baidu.com/pypi/simple
ADD confs/test/krb5.conf /etc/
ADD confs/test/config.json .
RUN mkdir -p $HOME/.sparkmagic && cp config.json $HOME/.sparkmagic
ADD confs/test/user.keytab /
RUN echo "\
[program:jupyter]\n\
directory=/workspace\n\
command=/bin/bash -c 'kinit -k -t /user.keytab ailab && /opt/conda/bin/jupyter lab --ip 0.0.0.0 --port 8888 --allow-root --no-browser --NotebookApp.allow_origin=* --NotebookApp.allow_remote_access=1' \n\
autorestart=true\n\
startretries=0\n\
redirect_stderr=true\n\
stdout_logfile=/var/log/jupyter.log\n\
stdout_logfile_maxbytes=50MB\n\
" > /etc/supervisor/conf.d/jupyter.conf


# 腾讯云生产
FROM builder3 as image-txprod
COPY --from=builder-fe /src/dist  dist
RUN /opt/conda/bin/pip install dist/*.whl  -i https://mirror.baidu.com/pypi/simple
ADD confs/txprod/krb5.conf /etc/
ADD confs/txprod/config.json .
RUN mkdir -p $HOME/.sparkmagic && cp config.json $HOME/.sparkmagic
ADD confs/txprod/user.keytab /

RUN echo "\
[program:jupyter]\n\
directory=/workspace\n\
command=/bin/bash -c 'kinit -k -t /user.keytab ylaiuser && /opt/conda/bin/jupyter lab --ip 0.0.0.0 --port 8888 --allow-root --no-browser --NotebookApp.allow_origin=* --NotebookApp.allow_remote_access=1' \n\
autorestart=true\n\
startretries=0\n\
redirect_stderr=true\n\
stdout_logfile=/var/log/jupyter.log\n\
stdout_logfile_maxbytes=50MB\n\
" > /etc/supervisor/conf.d/jupyter.conf

# 电信云测试
FROM builder3 as image-idctest
COPY --from=builder-fe /src/dist  dist
RUN /opt/conda/bin/pip install dist/*.whl  -i https://mirror.baidu.com/pypi/simple
ADD confs/idctest/config.json .
RUN mkdir -p $HOME/.sparkmagic && cp config.json $HOME/.sparkmagic
RUN echo "\
[program:jupyter]\n\
directory=/workspace\n\
command=/bin/bash -c '/opt/conda/bin/jupyter lab --ip 0.0.0.0 --port 8888 --allow-root --no-browser --NotebookApp.allow_origin=* --NotebookApp.allow_remote_access=1' \n\
autorestart=true\n\
startretries=0\n\
redirect_stderr=true\n\
stdout_logfile=/var/log/jupyter.log\n\
stdout_logfile_maxbytes=50MB\n\
" > /etc/supervisor/conf.d/jupyter.conf
