#
# The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
# (the "License"). You may not use this work except in compliance with the License, which is
# available at www.apache.org/licenses/LICENSE-2.0
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied, as more fully set forth in the License.
#
# See the NOTICE file distributed with this work for information regarding copyright ownership.
#

# See https://hub.docker.com/r/alluxio/alluxio-maven for instructions on running the image.

ARG RUST_VERSION=1.66.1
# Official rust image from https://github.com/rust-lang/docker-rust/blob/master/Dockerfile-debian.template
# This image installs rustup and cargo into /usr/local/{rustup,cargo}
# we install additional targets and components,
# and copy the toolchains from this image into the maven base image in the next stage
FROM rust:${RUST_VERSION} as rust-installer
RUN rustup target add aarch64-unknown-linux-gnu x86_64-unknown-linux-gnu && \
    rustup component add clippy rustfmt && \
    # move the two directories into an empty directory to save one COPY instruction in the next stage
    mkdir /cargo-and-rustup && \
    mv /usr/local/rustup /cargo-and-rustup/rustup && \
    mv /usr/local/cargo /cargo-and-rustup/cargo

FROM maven:3.8.6-jdk-8
# ARGs only last for the build phase of a single image. Redefine it to make it available in this stage as well.
# See https://stackoverflow.com/questions/53681522/share-variable-in-multi-stage-dockerfile-arg-before-from-not-substituted
ARG RUST_VERSION
ENV RUSTUP_HOME=/usr/local/rustup \
    CARGO_HOME=/usr/local/cargo \
    PATH=/usr/local/cargo/bin:$PATH \
    RUST_VERSION=${RUST_VERSION}
COPY --from=rust-installer /cargo-and-rustup /usr/local/

# need to create /.config to avoid npm errors
RUN mkdir -p /home/jenkins && \
    chmod -R 777 /home/jenkins && \
    chmod g+w /etc/passwd && \
    mkdir -p /.config && \
    chmod -R 777 /.config && \
    apt-get update -y && \
    apt-get upgrade -y ca-certificates && \
    apt-get install -y build-essential docker.io fuse3 libfuse3-dev libfuse-dev make ruby ruby-dev
# jekyll for documentation
RUN gem install public_suffix:4.0.7 jekyll:4.2.2 bundler:2.3.18
# golang for tooling
RUN ARCH=$(dpkg --print-architecture) && \
    wget https://go.dev/dl/go1.18.1.linux-${ARCH}.tar.gz && \
    tar -xvf go1.18.1.linux-${ARCH}.tar.gz && \
    mv go /usr/local
ENV GOROOT=/usr/local/go
ENV PATH=$GOROOT/bin:$PATH
# terraform for deployment scripts
RUN ARCH=$(dpkg --print-architecture) && \
    wget --quiet https://releases.hashicorp.com/terraform/1.0.1/terraform_1.0.1_linux_${ARCH}.zip && \
    unzip -o ./terraform_1.0.1_linux_${ARCH}.zip -d /usr/local/bin/ && \
    rm terraform_1.0.1_linux_${ARCH}.zip
