## 第一阶段 ====> Nodejs打包编译输出前端资源
FROM node:20-alpine AS build-node

# 前端程序
COPY ./omc_web /web
RUN npm config set registry https://registry.npmmirror.com
RUN cd /web && npm install && npm run build
RUN touch /web/dist/config.js && echo '/*use nginx*/' | tee /web/dist/config.js

# 后端程序
# COPY ./omc_api /api
COPY ./omc_api/restagent/config /api/restagent/config
COPY ./omc_api/restagent/etc/restconf.yaml /api/restagent/etc/restconf.yaml
COPY ./omc_api/restagent/restagent.go /api/restagent/restagent.go
COPY ./omc_api/features /api/features
COPY ./omc_api/lib /api/lib
COPY ./omc_api/src /api/src
COPY ./omc_api/go.sum /api/go.sum
COPY ./omc_api/go.mod /api/go.mod

# Docker文件
COPY ./omc_api/docker/omc /docker


## 第二阶段 ====> Go打包编译输出后端程序
FROM ubuntu:22.04 AS build-golang

# 从上个阶段复制过来的文件
COPY --from=build-node /web/dist /frontend
COPY --from=build-node /api /backend
COPY --from=build-node /docker /docker

# 安装 Go 和其他依赖
RUN apt-get update && \
    apt-get install -y build-essential libpcap-dev curl \
    && curl -OL https://dl.google.com/go/go1.22.5.linux-amd64.tar.gz \
    && tar -C /usr/local -xzf go1.22.5.linux-amd64.tar.gz \
    && rm go1.22.5.linux-amd64.tar.gz \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 设置 Go 环境变量
ENV PATH="/usr/local/go/bin:${PATH}"
ENV CGO_ENABLED 1
ENV GOOS linux
ENV GOPROXY https://goproxy.cn,direct

ARG VERSION
RUN cd /backend && go mod download
RUN cd /backend/restagent && go build -o omc -v -ldflags "-s -w \
    -X 'be.ems/lib/global.Version=${VERSION}' \
    -X 'be.ems/lib/global.BuildTime=`date`' \
    -X 'be.ems/lib/global.GoVer=`go version`'"

## 第三阶段 ====> 构建前后端融合镜像
FROM ubuntu:22.04

# 安装必要的软件包
RUN apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y tzdata \
    iputils-ping libpcap-dev \
    nginx openssh-server \
    && ssh-keygen -A \
    && adduser --disabled-password --gecos "" omcuser \
    && echo 'omcuser:password' | chpasswd \
    && mkdir -p /home/omcuser/.ssh \
    && chmod 700 /home/omcuser/.ssh \
    && touch /home/omcuser/.ssh/authorized_keys \
    && chmod 600 /home/omcuser/.ssh/authorized_keys \
    && chown -R omcuser:omcuser /home/omcuser/.ssh \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# 设置时区和语言环境
ENV TZ="Asia/Shanghai"
ENV LANG="en_US.UTF-8"
ENV APPENV="prod"

WORKDIR /usr/local/etc/omc
COPY --from=build-golang /frontend /usr/local/bin/web
COPY --from=build-golang /backend/restagent/omc /usr/local/bin/omc
COPY --from=build-golang /docker/omc.yaml ./omc.yaml
COPY --from=build-golang /docker/static ./static
COPY --from=build-golang /docker/logs /var/log/omc
COPY --from=build-golang /docker/nginx/cert /etc/nginx/cert
COPY --from=build-golang /docker/nginx/nginx.conf /etc/nginx/nginx.conf

EXPOSE 33030 33033 33040 33060 80 22

CMD ["/bin/sh", "-c", "service ssh start && service nginx start && /usr/local/bin/omc --env ${APPENV} -c /usr/local/etc/omc/omc.yaml"]

# 构建
# docker build --build-arg VERSION=241101 -t omc:2.241101 .
# docker run -it omc:2.241101 sh
# service ssh start && service nginx start && /usr/local/bin/omc --env prod -c /usr/local/etc/omc/omc.yaml
