Skip to content

Commit

Permalink
Merge pull request #131 from ifooth/dev-joelei-embed
Browse files Browse the repository at this point in the history
Feat: Use embed instead of packr2
  • Loading branch information
tming authored Oct 26, 2023
2 parents b36f62e + 7733c90 commit 9766210
Show file tree
Hide file tree
Showing 14 changed files with 933 additions and 164 deletions.
40 changes: 18 additions & 22 deletions .github/workflows/backend_booster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ name: Backend Booster Check
# events but only for the master branch
on:
push:
branches: [ master ]
branches: [master]
paths:
- 'src/backend/booster/**'
- "src/backend/booster/**"
pull_request:
branches: [ master, release-* ]
branches: [master, release-*]
paths:
- 'src/backend/booster/**'
- "src/backend/booster/**"

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
Expand All @@ -23,21 +23,17 @@ jobs:

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.17
- name: GO Mod
working-directory: src/backend/booster
run: |
go mod download
go get -u github.com/gobuffalo/packr/v2/packr2
sed -i '8s/^/\/\//' bk_dist/dashboard/pkg/dashboard/dashboard-packr.go
go mod tidy
sed -i '8s/^\/\///' bk_dist/dashboard/pkg/dashboard/dashboard-packr.go
- name: GO Build
working-directory: src/backend/booster
run: make -j buildbooster dist
env:
GITTAG: ${{ github.ref }}
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version-file: src/backend/booster/go.mod
- name: GO Mod Check
uses: katexochen/go-tidy-check@v2
with:
modules: src/backend/booster
- name: GO Build
working-directory: src/backend/booster
run: make -j buildbooster dist
env:
GITTAG: ${{ github.ref }}
1 change: 0 additions & 1 deletion src/backend/booster/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ tools/hook_ld_preload/hook_lib/*.d
tools/hook_ld_preload/json/*.d
tools/hook_ld_preload/*.so
bk_dist/dashboard/pkg/dashboard/packrd/packed-packr.go
go.sum
.temp
build.yml
.codecc
Expand Down
28 changes: 15 additions & 13 deletions src/backend/booster/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ BUILDTIME = $(shell date +%Y-%m-%dT%T%z)
GITHASH=$(shell git rev-parse HEAD)
VERSION=${GITTAG}-$(shell date +%y.%m.%d)

# for dist tools cross compile
SUFFIX =
ifeq ($(shell go env GOOS), windows)
SUFFIX = .exe
endif

LDFLAG=-s -w -X github.com/TencentBlueKing/bk-turbo/src/backend/booster/common/static.EncryptionKey=${encryption_key} \
-X github.com/TencentBlueKing/bk-turbo/src/backend/booster/common/static.ServerCertPwd=${server_cert_pwd} \
-X github.com/TencentBlueKing/bk-turbo/src/backend/booster/common/static.ClientCertPwd=${client_cert_pwd} \
Expand Down Expand Up @@ -75,7 +81,6 @@ BUILDBOOSTER_BIN_PATH=${BIN_PATH}/buildbooster

buildbooster_prepare:
mkdir -p ${BUILDBOOSTER_BIN_PATH}
if [ -z "${DISABLE_PACKR2}" ]; then cd bk_dist/dashboard/pkg/dashboard && packr2; fi

buildbooster_server:buildbooster_prepare
go build -ldflags "${LDFLAG}" -o ${BUILDBOOSTER_BIN_PATH}/bk-buildbooster-server ./server/main.go
Expand Down Expand Up @@ -108,45 +113,42 @@ DISTEXECUTOR_BIN_PATH=${BIN_PATH}/bkdist

dist_prepare:
mkdir -p ${DISTEXECUTOR_BIN_PATH}
go get -u github.com/gobuffalo/packr/v2/packr2
export PATH=${GOPATH}/bin:${PATH}
if [ -z "${DISABLE_PACKR2}" ]; then cd bk_dist/controller/pkg/dashboard && packr2; fi

dist_executor:dist_prepare
# bk-dist-executor
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-dist-executor ./bk_dist/executor/main.go
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-dist-executor${SUFFIX} ./bk_dist/executor/main.go

dist_booster:dist_prepare
# bk-booster
go build -ldflags "${LDFLAG} ${BuildBooster_LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-booster ./bk_dist/booster/main.go
go build -ldflags "${LDFLAG} ${BuildBooster_LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-booster${SUFFIX} ./bk_dist/booster/main.go

dist_controller:dist_prepare
# bk-dist-controller
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-dist-controller ./bk_dist/controller/main.go
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-dist-controller${SUFFIX} ./bk_dist/controller/main.go

dist_worker:dist_prepare
# bk-dist-worker
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-dist-worker ./bk_dist/worker/main.go
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-dist-worker${SUFFIX} ./bk_dist/worker/main.go

dist_idle_loop:dist_prepare
# bk-idle-loop
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-idle-loop ./bk_dist/idleloop/main.go
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-idle-loop${SUFFIX} ./bk_dist/idleloop/main.go

dist_monitor:dist_prepare
# bk-dist-monitor
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-dist-monitor ./bk_dist/monitor/main.go
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-dist-monitor${SUFFIX} ./bk_dist/monitor/main.go

dist_help_tool:dist_prepare
# bk-help-tool
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-help-tool ./bk_dist/helptool/main.go
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-help-tool${SUFFIX} ./bk_dist/helptool/main.go

dist_ubt_tool:dist_prepare
# bk-ubt-tool
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-ubt-tool ./bk_dist/ubttool/main.go
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-ubt-tool${SUFFIX} ./bk_dist/ubttool/main.go

dist_shader_tool:dist_prepare
# bk-shader-tool
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-shader-tool ./bk_dist/shadertool/main.go
go build -ldflags "${LDFLAG}" -o ${DISTEXECUTOR_BIN_PATH}/bk-shader-tool${SUFFIX} ./bk_dist/shadertool/main.go

dist_hook:dist_prepare
cd tools/hook_ld_preload && $(MAKE) all && cp bkhook.so ../../${DISTEXECUTOR_BIN_PATH}/
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ package dashboard
import (
"net/http"

"github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/dashboard/static"
"github.com/TencentBlueKing/bk-turbo/src/backend/booster/common/http/httpserver"

"github.com/gobuffalo/packr/v2"
)

// RegisterStaticServer add the static server to router
func RegisterStaticServer(svr *httpserver.HTTPServer) error {
box := packr.New("controller_box", "../../../dashboard/static/controller")

svr.GetWebContainer().Handle("/", http.FileServer(box))
controllerFS, err := static.ControllerFS()
if err != nil {
return err
}

svr.GetWebContainer().Handle("/", http.FileServer(http.FS(controllerFS)))
return nil
}

This file was deleted.

This file was deleted.

11 changes: 6 additions & 5 deletions src/backend/booster/bk_dist/dashboard/pkg/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ package dashboard
import (
"net/http"

"github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/dashboard/static"
"github.com/TencentBlueKing/bk-turbo/src/backend/booster/common/http/httpserver"

"github.com/gobuffalo/packr/v2"
)

// RegisterStaticServer register the static server into router
func RegisterStaticServer(svr *httpserver.HTTPServer) error {
box := packr.New("dashboard_box", "../../static/stats")

svr.GetWebContainer().Handle("/", http.FileServer(box))
statsFS, err := static.StatsFS()
if err != nil {
return err
}

svr.GetWebContainer().Handle("/", http.FileServer(http.FS(statsFS)))
return nil
}
39 changes: 39 additions & 0 deletions src/backend/booster/bk_dist/dashboard/static/static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2021 THL A29 Limited, a Tencent company. All rights reserved
*
* This source code file is licensed under the MIT License, you may obtain a copy of the License at
*
* http://opensource.org/licenses/MIT
*
*/

// Package static embed static resource and index.html
package static

import (
"embed"
"io/fs"
)

//go:embed controller stats
var assets embed.FS

// StatsFS stats 静态资源
func StatsFS() (fs.FS, error) {
stats, err := fs.Sub(assets, "stats")
if err != nil {
return nil, err
}

return stats, nil
}

// ControllerFS controller 静态资源
func ControllerFS() (fs.FS, error) {
controller, err := fs.Sub(assets, "controller")
if err != nil {
return nil, err
}

return controller, nil
}
34 changes: 34 additions & 0 deletions src/backend/booster/bk_dist/dashboard/static/static_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2021 THL A29 Limited, a Tencent company. All rights reserved
*
* This source code file is licensed under the MIT License, you may obtain a copy of the License at
*
* http://opensource.org/licenses/MIT
*
*/

package static

import (
"io"
"testing"
)

func BenchmarkStatsFS(b *testing.B) {
staticFS, err := StatsFS()
if err != nil {
panic(err)
}

for i := 0; i < b.N; i++ {
f, err := staticFS.Open("index.html")
if err != nil {
panic(err)
}

_, err = io.Copy(io.Discard, f)
if err != nil {
panic(err)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,36 @@ func (c *Example) PostExecuteNeedLock(*dcSDK.BKDistResult) bool {
func (c *Example) FinalExecute([]string) {
}

// LocalLockWeight Implement handler.Handler interface methods
func (c *Example) LocalLockWeight(command []string) int32 {
return 1
}

// NeedRemoteResource Implement handler.Handler interface methods
func (c *Example) NeedRemoteResource(command []string) bool {
return false
}

// OnRemoteFail Implement handler.Handler interface methods
func (c *Example) OnRemoteFail(command []string) (*dcSDK.BKDistCommand, error) {
return nil, nil
}

// PostLockWeight Implement handler.Handler interface methods
func (c *Example) PostLockWeight(result *dcSDK.BKDistResult) int32 {
return 0
}

// PreLockWeight Implement handler.Handler interface methods
func (c *Example) PreLockWeight(command []string) int32 {
return 0
}

// RemoteRetryTimes Implement handler.Handler interface methods
func (c *Example) RemoteRetryTimes() int {
return 0
}

func getPreloadConfig(configPath string) (*dcSDK.PreloadConfig, error) {
f, err := os.Open(configPath)
if err != nil {
Expand Down
Loading

0 comments on commit 9766210

Please sign in to comment.