chore: 完善个模块打包
This commit is contained in:
@@ -2,6 +2,7 @@ package maker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -21,6 +22,13 @@ type ClientPKGOption func(*clientPKGOption)
|
||||
type clientPKGOption struct {
|
||||
Downloads []*clientPKGDownload
|
||||
CMDs []string
|
||||
MakePkg bool
|
||||
}
|
||||
|
||||
func WithClientPkgMakePkg(pkg bool) ClientPKGOption {
|
||||
return func(o *clientPKGOption) {
|
||||
o.MakePkg = pkg
|
||||
}
|
||||
}
|
||||
|
||||
func WithClientPKGDownload(url, location string) ClientPKGOption {
|
||||
@@ -36,22 +44,21 @@ func WithClientPKGDownload(url, location string) ClientPKGOption {
|
||||
}
|
||||
}
|
||||
|
||||
func WithClientPKGCMD(cmd string) ClientPKGOption {
|
||||
func WithClientPKGCMDs(cmds ...string) ClientPKGOption {
|
||||
return func(o *clientPKGOption) {
|
||||
if cmd == "" {
|
||||
return
|
||||
}
|
||||
o.CMDs = append(o.CMDs, cmd)
|
||||
adds := lo.Filter(cmds, func(cmd string, _ int) bool { return cmd != "" })
|
||||
o.CMDs = append(o.CMDs, adds...)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *maker) ClientPKG(ctx context.Context, _os string, _version string, api string, opts ...ClientPKGOption) error {
|
||||
const (
|
||||
Dockerfile = `
|
||||
FROM hub.yizhisec.com/external/nginx:1.29.4-alpine3.23
|
||||
FROM hub.yizhisec.com/hsv2/base/nginx:latest
|
||||
|
||||
RUN mkdir -p /data
|
||||
%s
|
||||
COPY version.json /data/version.json
|
||||
|
||||
%s
|
||||
|
||||
COPY nginx.conf /etc/nginx/nginx.conf
|
||||
@@ -71,7 +78,6 @@ COPY nginx.conf /etc/nginx/nginx.conf
|
||||
location = filepath.Join(m.workdir, "client", _os)
|
||||
_file string
|
||||
_content string
|
||||
_cmds = ""
|
||||
)
|
||||
|
||||
for _, fn := range opts {
|
||||
@@ -80,16 +86,31 @@ COPY nginx.conf /etc/nginx/nginx.conf
|
||||
|
||||
logger.Info("☑️ maker.ClientPKG: start build client pkg, os = %s, version = %s, location = %s", _os, _version, location)
|
||||
|
||||
if err = os.RemoveAll(location); err != nil {
|
||||
logger.Debug("❌ maker.ClientPKG: remove directory failed, directory = %s, err = %s", location, err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
if err = os.MkdirAll(location, 0755); err != nil {
|
||||
logger.Debug("❌ maker.ClientPKG: create directory failed, directory = %s, err = %s", location, err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
vd := map[string]string{
|
||||
"version": _version,
|
||||
}
|
||||
vbs, _ := json.Marshal(vd)
|
||||
|
||||
if err = os.WriteFile(filepath.Join(location, "version.txt"), []byte(_version), 0644); err != nil {
|
||||
logger.Debug("❌ maker.ClientPKG: write file failed, file = %s, err = %s", filepath.Join(location, "version.txt"), err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
if err = os.WriteFile(filepath.Join(location, "version.json"), []byte(vbs), 0644); err != nil {
|
||||
logger.Debug("❌ maker.ClientPKG: write file failed, file = %s, err = %s", filepath.Join(location, "version.json"), err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
_file = filepath.Join(location, "nginx.conf")
|
||||
_content = fmt.Sprintf(resource.NGINXClientPKG, api)
|
||||
logger.Debug("☑️ maker.ClientPKG: start write file = %s", _file)
|
||||
@@ -100,15 +121,15 @@ COPY nginx.conf /etc/nginx/nginx.conf
|
||||
logger.Debug("✅ maker.ClientPKG: write file success, file = %s", _file)
|
||||
|
||||
lines := lo.Map(o.Downloads, func(d *clientPKGDownload, index int) string {
|
||||
return fmt.Sprintf("RUN wget -O /data/%s %s", d.Name, d.URL)
|
||||
return fmt.Sprintf("wget -O /data/%s %s", d.Name, d.URL)
|
||||
})
|
||||
|
||||
if len(o.CMDs) > 0 {
|
||||
_cmds = fmt.Sprintf("RUN %s", strings.Join(o.CMDs, " && "))
|
||||
lines = append(lines, o.CMDs...)
|
||||
}
|
||||
|
||||
_file = filepath.Join(location, "Dockerfile")
|
||||
_content = fmt.Sprintf(Dockerfile, strings.Join(lines, "\n"), _cmds)
|
||||
_content = fmt.Sprintf(Dockerfile, "RUN "+strings.Join(lines, " && "))
|
||||
logger.Debug("☑️ maker.ClientPKG: start write file = %s", _file)
|
||||
if os.WriteFile(_file, []byte(_content), 0644); err != nil {
|
||||
logger.Debug("❌ maker.ClientPKG: write file failed, file = %s, err = %s", _file, err.Error())
|
||||
@@ -148,6 +169,15 @@ COPY nginx.conf /etc/nginx/nginx.conf
|
||||
return err
|
||||
}
|
||||
|
||||
if o.MakePkg {
|
||||
_cmd := fmt.Sprintf("7za a -pRrPt7Uo9WM1dkXOJmHps56T8BZY2qA4g -mhe=on client.%s.pkg *", _os)
|
||||
logger.Debug("☑️ maker.ClientPKG: start create client.%s.pkg by 7zip(7za), cmd = '%s'", _os, _cmd)
|
||||
if err = m.RunCommand(ctx, location, _cmd); err != nil {
|
||||
logger.Debug("❌ maker.ClientPKG: run command failed, cmd = %s, err = %s", _cmd, err.Error())
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
logger.Info("️✅ maker.ClientPKG: build client pkg success, os = %s, version = %s, location = %s", _os, _version, location)
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user