Skip to content

Commit

Permalink
多平台构建,一些基础的提示和检测
Browse files Browse the repository at this point in the history
  • Loading branch information
donknap committed Jun 24, 2024
1 parent 47b1fcf commit 96b2353
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 99 deletions.
19 changes: 12 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,29 @@ TARGET_DIR=/Users/renchao/Workspace/docker/dpanel/src
JS_DIR=/Users/renchao/Workspace/js/d-panel

build: clean
CGO_ENABLED=1 GOARCH=amd64 GOOS=linux CC=x86_64-linux-musl-gcc CXX=x86_64-linux-musl-g++ go build -ldflags '-s -w' -gcflags="all=-trimpath=${PWD}" -asmflags="all=-trimpath=${PWD}" -o ${GO_BIN}/${PROJECT_NAME} ${SOURCE_FILES}
CGO_ENABLED=1 GOARCH=amd64 GOOS=linux CC=x86_64-linux-musl-gcc CXX=x86_64-linux-musl-g++ \
go build -ldflags '-s -w' -gcflags="all=-trimpath=${PWD}" -asmflags="all=-trimpath=${PWD}" -o ${GO_BIN}/${PROJECT_NAME} ${SOURCE_FILES}

cp ${GO_BASE}/database/db.sql ${TARGET_DIR}/server
cp ${GO_BIN}/${PROJECT_NAME} ${TARGET_DIR}/server
cp ${GO_BIN}/${PROJECT_NAME} ${TARGET_DIR}/server/${PROJECT_NAME}-amd64

cp ${GO_BASE}/config.yaml ${TARGET_DIR}/server

cd ${JS_DIR} && npm run build && cp -r $(JS_DIR)/dist/* ${TARGET_DIR}/html
cd ${TARGET_DIR} && git add . && git commit -a -m "update"
cd ${TARGET_DIR} && git push
# cd ${TARGET_DIR} && git add . && git commit -a -m "update"
# cd ${TARGET_DIR} && git push
build-windows: clean
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o ${GO_BIN}/${PROJECT_NAME}.exe ${SOURCE_FILES}
cp ${GO_BIN}/${PROJECT_NAME}.exe ${TARGET_DIR}/server
build-arm: clean
GOARM=7 GOARCH=arm64 GOOS=linux go build -v
test: clean
go run ${SOURCE_FILES} make:module --name=attach
CGO_ENABLED=1 GOARM=7 GOARCH=arm64 GOOS=linux CC=aarch64-unknown-linux-gnu-gcc CXX=aarch64-unknown-linux-gnu-g++ \
go build -ldflags '-s -w' -gcflags="all=-trimpath=${PWD}" -asmflags="all=-trimpath=${PWD}" -o ${GO_BIN}/${PROJECT_NAME} ${SOURCE_FILES}
cp ${GO_BIN}/${PROJECT_NAME} ${TARGET_DIR}/server/${PROJECT_NAME}-arm64
clean:
rm -rf ${TARGET_DIR}/server/*
rm -rf ${TARGET_DIR}/html/*
#go clean & rm -rf ${GO_BIN}/* & rm -rf ./output/*
all: build-arm build
help:
@echo "make - 编译 Go 代码, 生成二进制文件"
@echo "make dev - 在开发模式下编译 Go 代码"
50 changes: 29 additions & 21 deletions app/application/http/controller/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/image"
"github.com/donknap/dpanel/app/application/logic"
"github.com/donknap/dpanel/common/accessor"
"github.com/donknap/dpanel/common/dao"
"github.com/donknap/dpanel/common/entity"
"github.com/donknap/dpanel/common/function"
Expand Down Expand Up @@ -143,15 +144,17 @@ func (self Image) ImportByImageTar(http *gin.Context) {

func (self Image) CreateByDockerfile(http *gin.Context) {
type ParamsValidate struct {
Id int32 `form:"id"`
Registry string `form:"registry"`
Tag string `form:"tag" binding:"required"`
Id int32 `json:"id"`
Registry string `json:"registry"`
Tag string `json:"tag" binding:"required"`
Title string `json:"title"`
BuildType string `form:"buildType" binding:"required"`
BuildDockerfile string `form:"buildDockerfile" binding:"omitempty"`
BuildGit string `form:"buildGit" binding:"omitempty"`
BuildZip string `form:"buildZip" binding:"omitempty"`
BuildRoot string `form:"buildRoot" binding:"omitempty"`
BuildType string `json:"buildType" binding:"required"`
BuildDockerfile string `json:"buildDockerfile" binding:"omitempty"`
BuildGit string `json:"buildGit" binding:"omitempty"`
BuildZip string `json:"buildZip" binding:"omitempty"`
BuildRoot string `json:"buildRoot" binding:"omitempty"`
Platform string `json:"platform"`
PlatformArch string `json:"platformArch"`
}
params := ParamsValidate{}
if !self.Validate(http, &params) {
Expand Down Expand Up @@ -210,15 +213,18 @@ func (self Image) CreateByDockerfile(http *gin.Context) {
buildImageTask.GitUrl = params.BuildGit
}
imageNew := &entity.Image{
Registry: params.Registry,
Tag: imageName,
Title: params.Title,
BuildGit: params.BuildGit,
BuildDockerfile: params.BuildDockerfile,
BuildRoot: params.BuildRoot,
BuildType: params.BuildType,
Status: logic.StatusStop,
Message: "",
Tag: imageName,
Title: params.Title,
Setting: &accessor.ImageSettingOption{
Registry: params.Registry,
BuildGit: params.BuildGit,
BuildDockerfile: params.BuildDockerfile,
BuildRoot: params.BuildRoot,
Platform: params.Platform,
},
BuildType: params.BuildType,
Status: logic.StatusStop,
Message: "",
}
imageRow, _ := dao.Image.Where(dao.Image.ID.Eq(params.Id)).First()
if imageRow == nil {
Expand All @@ -231,17 +237,19 @@ func (self Image) CreateByDockerfile(http *gin.Context) {
_ = docker.Sdk.Client.ImageTag(docker.Sdk.Ctx, imageName, imageName+"-deprecated-"+function.GetRandomString(6))
}
dao.Image.Select(
dao.Image.BuildDockerfile,
dao.Image.BuildRoot,
dao.Image.BuildGit,
dao.Image.Status,
dao.Image.Message,
dao.Image.Registry,
dao.Image.Tag,
dao.Image.Setting,
).Where(dao.Image.ID.Eq(imageRow.ID)).Updates(imageNew)
}
buildImageTask.ImageId = imageRow.ID

buildImageTask.Platform = &logic.Platform{
Type: params.Platform,
Arch: params.PlatformArch,
}

err := logic.DockerTask{}.ImageBuild(buildImageTask)
if err != nil {
self.JsonResponseWithError(http, err, 500)
Expand Down
89 changes: 75 additions & 14 deletions app/application/http/controller/site-domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@ func (self SiteDomain) Create(http *gin.Context) {
type ParamsValidate struct {
ContainerId string `json:"containerId" binding:"required"`
ServerName string `json:"serverName" binding:"required"`
Schema string `json:"schema" binding:"omitempty,oneof=http https"`
Port int32 `json:"port" binding:"required"`
EnableBlockCommonExploits bool `json:"enableBlockCommonExploits"`
EnableAssetCache bool `json:"enableAssetCache"`
EnableWs bool `json:"enableWs"`
ExtraNginx string `json:"extraNginx"`
SslCrt string `json:"sslCrt"`
SslKey string `json:"sslKey"`
}

params := ParamsValidate{}
Expand All @@ -50,12 +47,15 @@ func (self SiteDomain) Create(http *gin.Context) {
self.JsonResponseWithError(http, err, 500)
return
}
siteDomainRow, _ := dao.SiteDomain.Where(dao.SiteDomain.ServerName.Eq(params.ServerName)).First()

var siteDomainRow *entity.SiteDomain
var domainSetting *accessor.SiteDomainSettingOption

siteDomainRow, _ = dao.SiteDomain.Where(dao.SiteDomain.ServerName.Eq(params.ServerName)).First()
if siteDomainRow != nil {
self.JsonResponseWithError(http, errors.New("域名已经存在"), 500)
return
}

// 将当前容器加入到默认 dpanel-local 网络中,并指定 Hostname 用于 Nginx 反向代理
_, err = docker.Sdk.Client.NetworkInspect(docker.Sdk.Ctx, defaultNetworkName, network.InspectOptions{})
if err != nil {
Expand All @@ -67,6 +67,7 @@ func (self SiteDomain) Create(http *gin.Context) {
siteRow, _ := dao.Site.Where(dao.Site.ContainerInfo.Eq(&accessor.SiteContainerInfoOption{
ID: params.ContainerId,
})).First()

if siteRow != nil {
hostname = fmt.Sprintf(docker.HostnameTemplate, siteRow.SiteName)
}
Expand All @@ -83,18 +84,16 @@ func (self SiteDomain) Create(http *gin.Context) {
}
}

domainSetting := &accessor.SiteDomainSettingOption{
domainSetting = &accessor.SiteDomainSettingOption{
ServerName: params.ServerName,
Port: params.Port,
ServerAddress: hostname,
Port: params.Port,
EnableBlockCommonExploits: params.EnableBlockCommonExploits,
EnableWs: params.EnableWs,
EnableAssetCache: params.EnableAssetCache,
EnableSSL: false,
ExtraNginx: template.HTML(params.ExtraNginx),
EnableSSL: params.Schema == "https",
TargetName: function.GetMd5(params.ServerName),
SslCrt: params.SslCrt,
SslKey: params.SslKey,
}

err = logic.Site{}.MakeNginxConf(domainSetting)
Expand Down Expand Up @@ -203,10 +202,14 @@ func (self SiteDomain) Delete(http *gin.Context) {
if list != nil && len(list) > 0 {
count, _ := dao.SiteDomain.Where(dao.SiteDomain.ContainerID.Eq(list[0].ContainerID)).Count()
if count == 0 {
err = docker.Sdk.Client.NetworkDisconnect(docker.Sdk.Ctx, defaultNetworkName, list[0].ContainerID, false)
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
// 如果只有dpanel-local一个网络则保留
containerInfo, err := docker.Sdk.ContainerInfo(list[0].ContainerID)
if err == nil && len(containerInfo.NetworkSettings.Networks) > 1 {
err = docker.Sdk.Client.NetworkDisconnect(docker.Sdk.Ctx, defaultNetworkName, list[0].ContainerID, false)
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
}
}
}
}
Expand Down Expand Up @@ -316,6 +319,64 @@ func (self SiteDomain) ApplyDomainCert(http *gin.Context) {
return
}

func (self SiteDomain) UpdateDomain(http *gin.Context) {
type ParamsValidate struct {
Id int32 `json:"id" binding:"required"`
SslCrt string `json:"sslCrt"`
SslKey string `json:"sslKey"`
SslCrtRenewTime string `json:"sslCrtRenewTime"`
Port int32 `json:"port"`
EnableBlockCommonExploits bool `json:"enableBlockCommonExploits"`
EnableAssetCache bool `json:"enableAssetCache"`
EnableWs bool `json:"enableWs"`
ExtraNginx string `json:"extraNginx"`
}

params := ParamsValidate{}
if !self.Validate(http, &params) {
return
}
siteDomainRow, _ := dao.SiteDomain.Where(dao.SiteDomain.ID.Eq(params.Id)).First()
if siteDomainRow == nil {
self.JsonResponseWithError(http, errors.New("域名不存在,请先添加域名"), 500)
return
}
// 站点基本信息
if params.Port != 0 {
siteDomainRow.Setting.Port = params.Port
}

siteDomainRow.Setting.EnableBlockCommonExploits = params.EnableBlockCommonExploits
siteDomainRow.Setting.EnableAssetCache = params.EnableAssetCache
siteDomainRow.Setting.EnableWs = params.EnableWs

if params.ExtraNginx != "" {
siteDomainRow.Setting.ExtraNginx = template.HTML(params.ExtraNginx)
}
// 手动导入证书
if params.SslCrt != "" && params.SslKey != "" {
siteDomainRow.Setting.SslCrt = params.SslCrt
siteDomainRow.Setting.SslKey = params.SslKey
siteDomainRow.Setting.AutoSsl = false
siteDomainRow.Setting.SslCrtRenewTime = params.SslCrtRenewTime
siteDomainRow.Setting.EnableSSL = true
}
err := logic.Site{}.MakeNginxConf(siteDomainRow.Setting)
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
}
_, err = dao.SiteDomain.Where(dao.SiteDomain.ID.Eq(params.Id)).Updates(&entity.SiteDomain{
Setting: siteDomainRow.Setting,
})
if err != nil {
self.JsonResponseWithError(http, err, 500)
return
}
self.JsonSuccessResponse(http)
return
}

func (self SiteDomain) RestartNginx(http *gin.Context) {
exec.Command{}.RunWithOut(&exec.RunCommandOption{
CmdName: "nginx",
Expand Down
3 changes: 3 additions & 0 deletions app/application/logic/docker-image-task.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func (self DockerTask) ImageBuild(buildImageTask *BuildImageMessage) error {
if buildImageTask.GitUrl != "" {
builder.WithGitUrl(buildImageTask.GitUrl)
}
if buildImageTask.Platform != nil {
builder.WithPlatform(buildImageTask.Platform.Type, buildImageTask.Platform.Arch)
}
builder.WithTag(buildImageTask.Tag)
response, err := builder.Execute()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions app/application/logic/docker-task.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ type BuildImageMessage struct {
Tag string // 镜像Tag
ImageId int32
Context string // Dockerfile 所在的目录
Platform *Platform
}

type Platform struct {
Type string
Arch string
}

type ImageRemoteMessage struct {
Expand Down
5 changes: 4 additions & 1 deletion app/application/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ func (provider *Provider) Register(httpServer *http_server.Server) {
cors.POST("/app/site/get-list", controller.Site{}.GetList)
cors.POST("/app/site/get-detail", controller.Site{}.GetDetail)
cors.POST("/app/site/delete", controller.Site{}.Delete)

cors.POST("/app/site/create-domain", controller.SiteDomain{}.Create)
cors.POST("/app/site/get-domain-list", controller.SiteDomain{}.GetList)
cors.POST("/app/site/update-domain", controller.SiteDomain{}.UpdateDomain)
cors.POST("/app/site/delete-domain", controller.SiteDomain{}.Delete)
cors.POST("/app/site/get-domain-list", controller.SiteDomain{}.GetList)
cors.POST("/app/site/get-domain-detail", controller.SiteDomain{}.GetDetail)
cors.POST("/app/site/apply-domain-cert", controller.SiteDomain{}.ApplyDomainCert)

cors.POST("/app/site/restart-nginx", controller.SiteDomain{}.RestartNginx)

// 容器相关
Expand Down
8 changes: 5 additions & 3 deletions app/common/http/controller/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (self Home) WsConsole(http *gin.Context) {
}

func (self Home) Info(http *gin.Context) {
dpanelContainerInfo, _ := docker.Sdk.ContainerInfo(facade.GetConfig().GetString("app.name"))
info, err := docker.Sdk.Client.Info(docker.Sdk.Ctx)
if err != nil {
self.JsonResponseWithError(http, err, 500)
Expand All @@ -143,9 +144,10 @@ func (self Home) Info(http *gin.Context) {
"containerTask": int(containerTask),
"imageTask": int(imageTask),
},
"dpanel": map[string]string{
"version": facade.GetConfig().GetString("app.version"),
"release": "",
"dpanel": map[string]interface{}{
"version": facade.GetConfig().GetString("app.version"),
"release": "",
"containerInfo": dpanelContainerInfo,
},
})
return
Expand Down
9 changes: 9 additions & 0 deletions common/accessor/image_setting_option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package accessor

type ImageSettingOption struct {
BuildGit string `json:"buildGit"`
BuildDockerfile string `json:"buildDockerfile"`
BuildRoot string `json:"buildRoot"`
Platform string `json:"platform"`
Registry string `json:"registry"`
}
Loading

0 comments on commit 96b2353

Please sign in to comment.