Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support dynamic distro config #1094

Merged
merged 18 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/resource/distrores/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ import (
"github.com/pingcap/tidb-dashboard/util/distro"
)

var resource = distro.DistributionResource{}
var resource = distro.StringsRes()
65 changes: 65 additions & 0 deletions pkg/uiserver/uiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@ package uiserver
import (
"bytes"
"compress/gzip"
"encoding/base64"
"encoding/json"
"html"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"strings"

"github.com/pingcap/log"
"github.com/shurcooL/httpgzip"
"go.uber.org/zap"

"github.com/pingcap/tidb-dashboard/pkg/config"
"github.com/pingcap/tidb-dashboard/util/distro"
)

type UpdateContentFunc func(fs http.FileSystem, oldFile http.File, path, newContent string, zippedBytes []byte)
Expand All @@ -39,6 +44,9 @@ func RewriteAssets(fs http.FileSystem, cfg *config.Config, updater UpdateContent
tmplText := string(bs)
updated := strings.ReplaceAll(tmplText, "__PUBLIC_PATH_PREFIX__", html.EscapeString(cfg.PublicPathPrefix))

distroStrings, _ := json.Marshal(distro.R()) // this will never fail
updated = strings.ReplaceAll(updated, "__DISTRO_STRINGS_RES__", base64.StdEncoding.EncodeToString(distroStrings))

var b bytes.Buffer
w := gzip.NewWriter(&b)
if _, err := w.Write([]byte(updated)); err != nil {
Expand All @@ -53,6 +61,63 @@ func RewriteAssets(fs http.FileSystem, cfg *config.Config, updater UpdateContent

rewrite("/index.html")
rewrite("/diagnoseReport.html")

overrideDistroAssetsRes(fs, cfg, updater)
}

func overrideDistroAssetsRes(fs http.FileSystem, cfg *config.Config, updater UpdateContentFunc) {
exePath, err := os.Executable()
if err != nil {
log.Fatal("Failed to get work dir", zap.Error(err))
baurine marked this conversation as resolved.
Show resolved Hide resolved
}

distroResDir := path.Join(path.Dir(exePath), distro.DistroResFolderName)
info, err := os.Stat(distroResDir)
if err != nil || !info.IsDir() {
// just ignore
return
}

override := func(assetName string) {
baurine marked this conversation as resolved.
Show resolved Hide resolved
assetPath := path.Join("/", distro.DistroResFolderName, assetName)
breezewish marked this conversation as resolved.
Show resolved Hide resolved
targetFile, err := fs.Open(assetPath)
if err != nil {
// has no target asset to be overried, skip
return
}
defer targetFile.Close()

sourceFile, err := os.Open(path.Join(distroResDir, assetName))
if err != nil {
log.Fatal("Failed to open source file", zap.String("path", assetName), zap.Error(err))
baurine marked this conversation as resolved.
Show resolved Hide resolved
}
defer sourceFile.Close()

data, err := ioutil.ReadAll(sourceFile)
if err != nil {
log.Fatal("Failed to read asset", zap.String("path", assetName), zap.Error(err))
}

var b bytes.Buffer
w := gzip.NewWriter(&b)
if _, err := w.Write(data); err != nil {
log.Fatal("Failed to zip asset", zap.Error(err))
}
if err := w.Close(); err != nil {
log.Fatal("Failed to zip asset", zap.Error(err))
}

updater(fs, targetFile, assetPath, string(data), b.Bytes())
}

// traverse
files, err := ioutil.ReadDir(distroResDir)
if err != nil {
log.Fatal("Failed to read dir", zap.String("dir", distroResDir), zap.Error(err))
}
for _, file := range files {
override(file.Name())
}
}

func Handler(root http.FileSystem) http.Handler {
Expand Down
28 changes: 0 additions & 28 deletions scripts/distro/replace_resources.sh

This file was deleted.

25 changes: 0 additions & 25 deletions scripts/distro/write_strings.go

This file was deleted.

25 changes: 0 additions & 25 deletions scripts/distro/write_strings.sh

This file was deleted.

5 changes: 4 additions & 1 deletion scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ module scripts
go 1.13

require (
github.com/pingcap/errors v0.11.5-0.20200917111840-a15ef68f753d // indirect
github.com/pingcap/log v0.0.0-20210906054005-afc726e70354
github.com/pingcap/tidb-dashboard v0.0.0-00010101000000-000000000000
baurine marked this conversation as resolved.
Show resolved Hide resolved
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546
github.com/swaggo/swag v1.6.6-0.20200529100950-7c765ddd0476
go.uber.org/zap v1.19.0
golang.org/x/lint v0.0.0-20201208152925-83fdc39ff7b5 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/tools v0.0.0-20210112230658-8b4aab62c064 // indirect
baurine marked this conversation as resolved.
Show resolved Hide resolved
)

replace github.com/pingcap/tidb-dashboard => ../
baurine marked this conversation as resolved.
Show resolved Hide resolved
Loading