FROM morgan1971/lofar-software:0.1.0
LABEL Description="This image is used to execute the LOFAR DDF 2.0"
LABEL maintainer="Giuliano Taffoni <giuliano.taffoni@inaf.it>"
ENV CONTAINER_NAME='Lofar DDF Software'
# Set non-interactive
ENV DEBIAN_FRONTEND noninteractive
ENV DDF_VERSION=5b95cdd
ENV PYBDSF_VERSION=v1.9.1

USER root
#####################################################################
# Add deployment keys
#####################################################################
RUN mkdir /pyenv-py3
COPY files/requirements3.txt /pyenv-py3
COPY files/requirements3_ddf.txt /pyenv-py3
RUN chown -R lofar.lofar /pyenv-py3



RUN mv /lofar_home_vanilla /home/lofar && mkdir -p /home/lofar/.ssh/
COPY keys/lofarit_ddf2_rsa /home/lofar/.ssh/lofarit_ddf2_rsa
COPY keys/lofarit_killms_rsa /home/lofar/.ssh/lofarit_killms_rsa
RUN  chmod 600 /home/lofar/.ssh/lofarit_ddf2_rsa && \
     chmod 600 /home/lofar/.ssh/lofarit_killms_rsa && \
     chown -R lofar.lofar /home/lofar/
RUN apt -y install python3-psycopg2
RUN python3 -m pip install -U pip
RUN python3 -m pip install -U pip setuptools wheel

USER lofar
#####################################################################
# Install requirements as local user
#####################################################################
RUN python3 -m pip install --upgrade pip
RUN pip3 install -r /pyenv-py3/requirements3.txt
RUN pip3 install -r /pyenv-py3/requirements3_ddf.txt

#####################################################################
# Install pybdfs 1.9.1
#####################################################################
RUN mkdir /tmp/pybdfs  && cd /tmp/pybdfs && \
    git clone https://github.com/lofar-astron/PyBDSF.git && \
    cd /tmp/pybdfs/PyBDSF && git checkout tags/${PYBDSF_VERSION} && \
    pip3 install /tmp/pybdfs/PyBDSF/ && rm -fr /tmp/pybdfs
LABEL it.inaf.lofar.software.pybdfs=1.9.1


#####################################################################
# Install DDF from private repo
#####################################################################
RUN ssh-keyscan github.com >> ~/.ssh/known_hosts && \
    ssh-agent bash -c 'ssh-add /home/lofar/.ssh/lofarit_ddf2_rsa && cd /tmp/ && git clone git@github.com:cyriltasse/DDFacet' && \
    ssh-agent bash -c 'ssh-add /home/lofar/.ssh/lofarit_ddf2_rsa && cd /tmp/DDFacet && git pull && git checkout $DDF_VERSION'
RUN python3 -c "import bdsf" && python3 -c "import siamese"
RUN pip3 install /tmp/DDFacet/[dft-support,moresane-support,testing-requirements,fits-beam-support,kms-support]
RUN rm -rf /tmp/DDFacet
#RUN DDF.py --help && MakeMask.py --help &&  MakeCatalog.py --help && MakeModel.py --help && \
#    MaskDicoModel.py --help && ClusterCat.py --help
LABEL it.inaf.lofar.software.ddf=5b95cdd

#####################################################################
#  Install killMs
#####################################################################
RUN ssh-agent bash -c 'ssh-add /home/lofar/.ssh/lofarit_killms_rsa && cd /tmp/ && git clone git@github.com:cyriltasse/killMS' && \
    ssh-agent bash -c 'ssh-add /home/lofar/.ssh/lofarit_killms_rsa && cd /tmp/killMS && git pull && git checkout InWeights'
RUN pip3 install /tmp/killMS && rm -fr /tmp/killMS
LABEL it.inaf.lofar.software.killMS=InWeights

#####################################################################
# DDF-PIPELINE
#####################################################################
RUN cd /pyenv-py3 &&  git clone https://github.com/mhardcastle/ddf-pipeline && \
    cd /pyenv-py3/ddf-pipeline && git checkout 8969b9f
RUN cd /pyenv-py3 && git clone https://www.ict.inaf.it/gitlab/lofarit/container-data.git

#####################################################################
# MH lotss-query
#####################################################################
RUN mkdir /pyenv-py3/lotss-query && cd /pyenv-py3/lotss-query && \
    git clone https://github.com/mhardcastle/lotss-query.git && \
    cp /pyenv-py3/lotss-query/lotss-query/surveys_db.py /pyenv-py3/ddf-pipeline/utils

RUN pip3 cache purge
#
# Final configurarations
#
USER root



RUN sudo apt-get -y clean
#RUN sudo ln -s /usr/share/lxde/wallpapers/lxde_blue.jpg /etc/alternatives/desktop-background
#
# PATHS and enviroments
#
RUN sed -e "s|INSTALLDIR|/pyenv-py3|" /pyenv-py3/ddf-pipeline/misc/DDF.sh > /pyenv-py3/DDF.sh
RUN echo "source /pyenv-py3/DDF.sh" >> /home/lofar/.bashrc
ENV PATH /usr/local/bin:/home/lofar/.local/bin:$PATH
ENV LD_LIBRARY_PATH /usr/local/lib:/opt/dysco/lib/:$LD_LIBRARY_PATH
ENV DDF_PIPELINE_CATALOGS='/pyenv-py3/container-data/bootstrap-cats/'
ENV MEQTREES_CATTERY_PATH /usr/local/lib/python3.6/dist-packages/Cattery/
RUN echo "echo PYTHONPATH $MEQTREES_CATTERY_PATH:$PYTHONPATH" >> /home/lofar/.bashrc
RUN echo "echo PYTHONPATH /usr/local/lib/python3.6/site-packages:$PYTHONPATH" >> /home/lofar/.bashrc
RUN echo "echo PYTHONPATH $MEQTREES_CATTERY_PATH:$PYTHONPATH" >> /home/lofar/.bashrc
# Rename user home folder as a "vanilla" home folder
RUN mv /home/lofar /lofar_home_vanilla
RUN chmod -R ugo+r /lofar_home_vanilla && \
    chmod  ugo+x /lofar_home_vanilla/.local && \
    chmod  ugo+x /lofar_home_vanilla/.local/lib && \
    chmod  ugo+x  /lofar_home_vanilla/.local/lib/python3.6 && \
    chmod  ugo+x  /lofar_home_vanilla/.local/lib/python3.6/site-packages
USER lofar
LABEL Vendor="INAF"
LABEL Version="0.1.0"
