# Build the ffs binary
FROM golang:1.17 as builder

WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download

# Copy the go source
COPY .git/ .git/
COPY client/ client/
COPY cmd/ cmd/
COPY config/ config/
COPY pkg/ pkg/
COPY proto/ proto/
COPY realm/ realm/
COPY throttler/ throttler/
COPY universe/ universe/
COPY util/ util/
COPY zone/ zone/

# Build
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -tags codes -ldflags "-X 'main.gitHash=`git rev-parse HEAD`'" -o bin/universe-server-linux cmd/universe-server/main.go
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -tags codes -ldflags "-X 'main.gitHash=`git rev-parse HEAD`'" -o bin/zone-server-linux cmd/zone-server/main.go
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -tags codes -ldflags "-X 'main.gitHash=`git rev-parse HEAD`'" -o bin/realm-server-linux cmd/realm-server/main.go
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -tags codes -ldflags "-X 'main.gitHash=`git rev-parse HEAD`'" -o bin/throttler-server-linux cmd/throttler-server/main.go
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -tags codes -ldflags "-X 'main.gitHash=`git rev-parse HEAD`'" -o bin/ffs-client-linux cmd/ffs-client/main.go

# Use centos7 as minimal base image to package the ffs binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM centos:7 as universe
WORKDIR /
COPY --from=builder /workspace/bin/universe-server-linux /usr/local/bin/
USER root

ENTRYPOINT ["/bin/bash"]

FROM centos:7 as zone
WORKDIR /
COPY --from=builder /workspace/bin/zone-server-linux /usr/local/bin/
USER root

ENTRYPOINT ["/bin/bash"]

FROM centos:7 as realm
WORKDIR /
COPY --from=builder /workspace/bin/realm-server-linux /usr/local/bin/
USER root

ENTRYPOINT ["/bin/bash"]

FROM centos:7 as throttler
WORKDIR /
COPY --from=builder /workspace/bin/throttler-server-linux /usr/local/bin/
USER root

ENTRYPOINT ["/bin/bash"]

FROM centos:7 as client
WORKDIR /
COPY --from=builder /workspace/bin/ffs-client-linux /usr/local/bin/
USER root
RUN yum -y install fuse

ENTRYPOINT ["/bin/bash"]