FROM nvidia/cuda:11.6.1-devel-ubuntu20.04
ENV DEBIAN_FRONTEND=noninteractive

#RUN echo $SSH_PRIVATE_KEY >> /root/.ssh/id_ed25519 && chmod 600 /root/.ssh/id_ed25519
#RUN echo $SSH_PUBLIC_KEY >> /root/.ssh/id_ed25519.pub && chmod 600 /root/.ssh/id_ed25519.pub

#Install core packages
RUN apt-get update && apt-get install -y libncurses5 python3 python3-pip curl git apt-utils ssh ca-certificates tmux nano vim sudo bash tini

RUN apt-get install -y htop tmux curl wget unzip nano && \
    update-alternatives --install /usr/bin/python python /usr/bin/python3 1 && \
    update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 1 && \
    pip install --upgrade pip && \
    pip install gpustat

#RUN apt-get install -y nginx certbot python3-certbot-nginx

RUN curl -fsSL https://code-server.dev/install.sh | sh

#COPY core-config /etc/nginx/sites-available/core-server
#RUN cat /etc/nginx/sites-available/core-server

#RUN sudo ln -s /etc/nginx/sites-available/core-server /etc/nginx/sites-enabled/code-server

ARG USERNAME=xuser
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Creating the user and usergroup
RUN groupadd --gid $USER_GID $USERNAME \
    && useradd --uid $USER_UID --gid $USERNAME -m $USERNAME \
    && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
    && chmod 0440 /etc/sudoers.d/$USERNAME

RUN chmod g+rw /home && \
    mkdir -p /home/workspace && \
    mkdir -p /home/xuser && \
    mkdir -p /home/xuser/.ssh && \
    chown -R $USERNAME:$USERNAME /home/workspace && \
    chown -R $USERNAME:$USERNAME /home/xuser && \
    chown -R $USERNAME:$USERNAME /home/xuser/.ssh

USER $USERNAME

ARG SSH_PRIVATE_KEY
ARG SSH_PUBLIC_KEY
ARG PAT_KEY

RUN sudo chmod 700 /home/xuser/.ssh
RUN echo $SSH_PRIVATE_KEY > /home/xuser/.ssh/id_ed25519 && sudo chmod 600 /home/xuser/.ssh/id_ed25519
RUN echo ${SSH_PUBLIC_KEY} > /home/xuser/.ssh/id_ed25519.pub && sudo chmod 600 /home/xuser/.ssh/id_ed25519.pub
RUN cat /home/xuser/.ssh/id_ed25519
RUN echo $SSH_PRIVATE_KEY
RUN eval `ssh-agent -s` && ssh-add /home/xuser/.ssh/id_ed25519
RUN touch /home/xuser/.ssh/known_hosts
RUN ssh-keyscan github.com >> /home/xuser/.ssh/known_hosts

RUN git config --global user.name "novelailab"
RUN git config --global user.email "erendogan@novelai.net"
RUN git config --global credential.helper store

#Install light deps
RUN pip3 install icecream torch numpy
#RUN pip3 install git+https://github.com/EleutherAI/pyfra git+ssh://git@github.com/NovelAI/novelutils.git
RUN pip3 install git+https://github.com/EleutherAI/pyfra git+https://${PAT_KEY}@github.com/NovelAI/novelutils
RUN sudo apt-get install -y rsync

#install vscode extensions
#RUN code --install-extension ms-python.python

WORKDIR /home/xuser
RUN sudo chsh -s /bin/bash
ENV LANG=C.UTF-8 \
    LC_ALL=C.UTF-8 \
    HOME=/home/xuser \
    EDITOR=code \
    VISUAL=code \
    GIT_EDITOR="code --wait"

EXPOSE 3000

USER root

RUN sudo mkdir /var/run/sshd && \
    sudo rm /etc/ssh/sshd_config && \
    rm /home/xuser/.bashrc

COPY sshconf /etc/ssh/sshd_config
COPY bashconf /home/xuser/.bashrc

RUN sudo sed -i 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' /etc/pam.d/sshd && \
    # FIX SUDO BUG: https://github.com/sudo-project/sudo/issues/42
    sudo echo "Set disable_coredump false" >> /etc/sudo.conf

RUN chsh -s /bin/bash root
RUN chsh -s /bin/bash $USERNAME

USER xuser

# SSH config and bashrc
RUN echo 'Host *' > /home/xuser/.ssh/config && \
    echo '    StrictHostKeyChecking no' >> /home/xuser/.ssh/config

COPY publickeys /home/xuser/.ssh/authorized_keys

# Expose SSH port
EXPOSE 22
EXPOSE 80
EXPOSE 8080

#keep docker container alive
ARG PASSWORD=hellohelloxd123
#ENTRYPOINT sudo certbot --non-interactive --redirect --agree-tos --nginx -d vscworkspace.novelai.net -m erendogan@novelai.net && \
#    code-server && \
#    sudo service start ssh && \
#    tail -f /dev/null

#install kubectl for ubuntu 20.04
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
RUN sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

#install gcloud SDK
RUN sudo apt-get install -y apt-transport-https ca-certificates gnupg
RUN echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
RUN sudo apt-get update -y && sudo apt-get install -y google-cloud-sdk
#copy kubernetes config
RUN mkdir /home/xuser/.kube
RUN rm -rf /home/.config/gcloud
#copy GCP
ENTRYPOINT sudo service ssh start
