Skip to content

Commit

Permalink
feat: manager embed frontend assets (dragonflyoss#1523)
Browse files Browse the repository at this point in the history
Signed-off-by: Gaius <[email protected]>
  • Loading branch information
gaius-qi authored Aug 4, 2022
1 parent 8f3ce6c commit 12b9957
Show file tree
Hide file tree
Showing 18 changed files with 117 additions and 54 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ jobs:

- name: Checkout code
uses: actions/checkout@v2
with:
submodules: recursive

- name: Build manager console assets
run: |-
make build-manager-console
- name: Run Unit tests
run: |-
Expand Down Expand Up @@ -55,6 +61,12 @@ jobs:

- name: Check out code
uses: actions/checkout@v2
with:
submodules: recursive

- name: Build manager console assets
run: |-
make build-manager-console
- name: Build
run: make build
6 changes: 6 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
submodules: recursive

- name: Build manager console assets
run: |-
make build-manager-console
- name: Golangci lint
uses: golangci/golangci-lint-action@v2
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ jobs:
with:
go-version: 1.18

- name: Build manager console assets
run: |-
make build-manager-console
- name: Check GoReleaser config
uses: goreleaser/goreleaser-action@v2
with:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ vendor/
*.DS_Store
target/
scheduler/test/misc
manager/dist/*
!manager/dist/.gitkeep
.chglog

# mysql
Expand Down
6 changes: 6 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
before:
hooks:
- hack/update-version-gorelease.sh
- make build-manager-console
- go mod download

builds:
Expand All @@ -12,6 +13,7 @@ builds:
- darwin
goarch:
- amd64
- arm64
ldflags:
- -X d7y.io/dragonfly/v2/version.Major={{ .Major }}
- -X d7y.io/dragonfly/v2/version.Minor={{ .Minor }}
Expand All @@ -29,6 +31,7 @@ builds:
- darwin
goarch:
- amd64
- arm64
ldflags:
- -X d7y.io/dragonfly/v2/version.Major={{ .Major }}
- -X d7y.io/dragonfly/v2/version.Minor={{ .Minor }}
Expand All @@ -46,6 +49,7 @@ builds:
- darwin
goarch:
- amd64
- arm64
ldflags:
- -X d7y.io/dragonfly/v2/version.Major={{ .Major }}
- -X d7y.io/dragonfly/v2/version.Minor={{ .Minor }}
Expand All @@ -63,6 +67,7 @@ builds:
- darwin
goarch:
- amd64
- arm64
ldflags:
- -X d7y.io/dragonfly/v2/version.Major={{ .Major }}
- -X d7y.io/dragonfly/v2/version.Minor={{ .Minor }}
Expand All @@ -80,6 +85,7 @@ builds:
- darwin
goarch:
- amd64
- arm64
ldflags:
- -X d7y.io/dragonfly/v2/version.Major={{ .Major }}
- -X d7y.io/dragonfly/v2/version.Minor={{ .Minor }}
Expand Down
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,20 @@ build-scheduler: build-dirs
.PHONY: build-scheduler

# Build manager
build-manager: build-dirs
build-manager: build-dirs build-manager-console
@echo "Begin to build manager."
make build-manager-server
.PHONY: build-manager

# Build manager server
build-manager-server: build-dirs
@echo "Begin to build manager server."
./hack/build.sh manager
.PHONY: build-manager

# Build manager console
build-manager-console: build-dirs
@echo "Begin to build manager."
@echo "Begin to build manager console."
./hack/build.sh manager-console
.PHONY: build-manager-console

Expand Down Expand Up @@ -396,8 +402,10 @@ help:
@echo "make docker-push push dragonfly image"
@echo "make docker-build-dfdaemon build dfdaemon image"
@echo "make docker-build-scheduler build scheduler image"
@echo "make docker-build-manager build manager image"
@echo "make docker-push-dfdaemon push dfdaemon image"
@echo "make docker-push-scheduler push scheduler image"
@echo "make docker-push-manager push manager image"
@echo "make build build dragonfly"
@echo "make build-dfget build dfget"
@echo "make build-linux-dfget build linux dfget"
Expand All @@ -407,6 +415,7 @@ help:
@echo "make build-linux-dfstore build linux dfstore"
@echo "make build-scheduler build scheduler"
@echo "make build-manager build manager"
@echo "make build-manager-server build manager server"
@echo "make build-manager-console build manager console"
@echo "make build-e2e-sha256sum build sha256sum test tool"
@echo "make install-dfget install dfget"
Expand Down
27 changes: 14 additions & 13 deletions build/images/manager/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
ARG BASE_IMAGE=alpine:3.16

FROM node:12-alpine as console-builder

WORKDIR /build

COPY ./manager/console/package.json /build

RUN npm install --loglevel warn --progress false

COPY ./manager/console /build

RUN npm run build

FROM golang:1.18.3-alpine3.16 as server-builder

ARG GOPROXY
Expand All @@ -12,19 +24,9 @@ RUN apk --no-cache add bash make gcc libc-dev git

COPY . /go/src/d7y.io/dragonfly/v2

RUN make build-manager && make install-manager
COPY --from=console-builder /build/dist /go/src/d7y.io/dragonfly/v2/manager/dist

FROM node:12-alpine as console-builder

WORKDIR /build

COPY ./manager/console/package.json /build

RUN npm install --loglevel warn --progress false

COPY ./manager/console /build

RUN npm run build
RUN make build-manager-server && make install-manager

FROM ${BASE_IMAGE} as health

Expand All @@ -43,7 +45,6 @@ RUN mkdir -p /opt/dragonfly/bin/manager/console \
&& echo "hosts: files dns" > /etc/nsswitch.conf

COPY --from=server-builder /opt/dragonfly/bin/manager /opt/dragonfly/bin/server
COPY --from=console-builder /build/dist /opt/dragonfly/manager/console/dist
COPY --from=health /bin/grpc_health_probe /bin/grpc_health_probe

EXPOSE 8080 65003
Expand Down
17 changes: 7 additions & 10 deletions build/plugin-builder/images/manager/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
ARG BASE_IMAGE=alpine:3.16

FROM node:12-alpine as console-builder
WORKDIR /build
COPY ./manager/console/package.json /build
RUN npm install --loglevel warn --progress false
COPY ./manager/console /build
RUN npm run build

FROM ${BASE_IMAGE}

WORKDIR /opt/dragonfly

ENV PATH=/opt/dragonfly/bin:$PATH
RUN mkdir -p /opt/dragonfly/bin/manager/console \
&& echo "hosts: files dns" > /etc/nsswitch.conf && \

RUN echo "hosts: files dns" > /etc/nsswitch.conf && \
mkdir -p /usr/local/dragonfly/plugins/

COPY ./artifacts/binaries/manager /opt/dragonfly/bin/server
COPY ./artifacts/plugins/d7y-manager-plugin-* /usr/local/dragonfly/plugins/
COPY --from=console-builder /build/dist /opt/dragonfly/manager/console/dist

EXPOSE 8080 65003

ENTRYPOINT ["/opt/dragonfly/bin/server"]
2 changes: 0 additions & 2 deletions deploy/docker-compose/template/manager.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ server:
rest:
# stand address
addr: :8080
# front-end console resource path
# publicPath: /dist

# database info used for server
database:
Expand Down
12 changes: 8 additions & 4 deletions hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ GOLDFLAGS="${GOLDFLAGS} -X \"d7y.io/dragonfly/v2/version.Gotags=${GOTAGS:-none}\
GOLDFLAGS="${GOLDFLAGS} -X \"d7y.io/dragonfly/v2/version.GoVersion=$(go version | grep -o 'go[^ ].*')\""
GOLDFLAGS="${GOLDFLAGS} -X \"d7y.io/dragonfly/v2/version.Gogcflags=${GOGCFLAGS:-none}\""

curDir=$(cd "$(dirname "$0")" && pwd)
cd "${curDir}" || return
CUR_DIR=$(cd "$(dirname "$0")" && pwd)
cd "${CUR_DIR}" || return
BUILD_SOURCE_HOME=$(cd ".." && pwd)

. ./env.sh
Expand Down Expand Up @@ -114,10 +114,14 @@ build-manager-docker() {

build-manager-console() {
set -x
consoleDir=$(echo $curDir | sed 's#hack#manager/console#')
CONSOLE_DIR=$(echo $CUR_DIR | sed 's#hack#manager/console#')
MANAGER_DIR=$(echo $CUR_DIR | sed 's#hack#manager#')
CONSOLE_ASSETS_DIR=$CONSOLE_DIR/dist/
MANAGER_ASSETS_DIR=$MANAGER_DIR/dist/
docker run --workdir=/build \
--rm -v ${consoleDir}:/build node:12-alpine \
--rm -v ${CONSOLE_DIR}:/build node:12-alpine \
sh -c "npm install --loglevel warn --progress false && npm run build"
cp -r $CONSOLE_ASSETS_DIR $MANAGER_ASSETS_DIR
}

main() {
Expand Down
2 changes: 1 addition & 1 deletion hack/gen-buildx.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

components="scheduler manager"
components="scheduler manager-server"

set -x

Expand Down
6 changes: 1 addition & 5 deletions manager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ type ServerConfig struct {
// Server log directory.
LogDir string `yaml:"logDir" mapstructure:"logDir"`

// Console resource path.
PublicPath string `yaml:"publicPath" mapstructure:"publicPath"`

// GRPC server configuration.
GRPC *TCPListenConfig `yaml:"grpc" mapstructure:"grpc"`

Expand Down Expand Up @@ -249,8 +246,7 @@ type ObjectStorageConfig struct {
func New() *Config {
return &Config{
Server: &ServerConfig{
Name: DefaultServerName,
PublicPath: DefaultPublicPath,
Name: DefaultServerName,
GRPC: &TCPListenConfig{
PortRange: TCPListenPortRange{
Start: DefaultGRPCPort,
Expand Down
5 changes: 2 additions & 3 deletions manager/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ func TestManagerConfig_Load(t *testing.T) {

config := &Config{
Server: &ServerConfig{
Name: "foo",
LogDir: "foo",
PublicPath: "foo",
Name: "foo",
LogDir: "foo",
GRPC: &TCPListenConfig{
Listen: "127.0.0.1",
PortRange: TCPListenPortRange{
Expand Down
3 changes: 0 additions & 3 deletions manager/config/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ const (
// DefaultServerName is default server name.
DefaultServerName = "d7y/manager"

// DefaultPublicPath is default path for frontend assets.
DefaultPublicPath = "manager/console/dist"

// DefaultGRPCPort is default port for grpc server.
DefaultGRPCPort = 65003

Expand Down
1 change: 0 additions & 1 deletion manager/config/testdata/manager.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
server:
name: foo
logDir: foo
publicPath: foo
grpc:
listen: 127.0.0.1
port:
Expand Down
Empty file added manager/dist/.gitkeep
Empty file.
36 changes: 34 additions & 2 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ package manager

import (
"context"
"embed"
"io/fs"
"net/http"
"time"

"github.com/gin-contrib/static"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"google.golang.org/grpc"

Expand All @@ -42,10 +45,39 @@ import (

const (
// gracefulStopTimeout specifies a time limit for
// grpc server to complete a graceful shutdown
// grpc server to complete a graceful shutdown.
gracefulStopTimeout = 10 * time.Second

// assetsTargetPath is target path of embed assets.
assetsTargetPath = "dist"
)

//go:embed dist/*
var assets embed.FS

type embedFileSystem struct {
http.FileSystem
}

func (e embedFileSystem) Exists(prefix string, path string) bool {
_, err := e.Open(path)
if err != nil {
return false
}
return true
}

func EmbedFolder(fsEmbed embed.FS, targetPath string) static.ServeFileSystem {
fsys, err := fs.Sub(fsEmbed, targetPath)
if err != nil {
panic(err)
}

return embedFileSystem{
FileSystem: http.FS(fsys),
}
}

type Server struct {
// Server configuration
config *config.Config
Expand Down Expand Up @@ -107,7 +139,7 @@ func New(cfg *config.Config, d dfpath.Dfpath) (*Server, error) {

// Initialize REST server
restService := service.New(db, cache, job, enforcer, objectStorage)
router, err := router.Init(cfg, d.LogDir(), restService, enforcer)
router, err := router.Init(cfg, d.LogDir(), restService, enforcer, EmbedFolder(assets, assetsTargetPath))
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 12b9957

Please sign in to comment.