diff --git a/backend/app/service/app.go b/backend/app/service/app.go index 1d5e52ef8..2a4fc3e4c 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "github.com/1Panel-dev/1Panel/backend/buserr" + "github.com/1Panel-dev/1Panel/backend/utils/docker" "io/ioutil" "net/http" "os" @@ -166,6 +167,9 @@ func (a AppService) GetAppDetail(appId uint, version string) (response.AppDetail } func (a AppService) Install(ctx context.Context, req request.AppInstallCreate) (*model.AppInstall, error) { + if err := docker.CreateDefaultDockerNetwork(); err != nil { + return nil, buserr.WithDetail(constant.Err1PanelNetworkFailed, err.Error(), nil) + } if list, _ := appInstallRepo.ListBy(commonRepo.WithByName(req.Name)); len(list) > 0 { return nil, buserr.New(constant.ErrNameIsExist) } diff --git a/backend/constant/errs.go b/backend/constant/errs.go index 2f82cc94e..05679e8d6 100644 --- a/backend/constant/errs.go +++ b/backend/constant/errs.go @@ -50,15 +50,16 @@ var ( // app var ( - ErrPortInUsed = "ErrPortInUsed" - ErrAppLimit = "ErrAppLimit" - ErrAppRequired = "ErrAppRequired" - ErrFileCanNotRead = "ErrFileCanNotRead" - ErrFileToLarge = "ErrFileToLarge" - ErrNotInstall = "ErrNotInstall" - ErrPortInOtherApp = "ErrPortInOtherApp" - ErrDbUserNotValid = "ErrDbUserNotValid" - ErrUpdateBuWebsite = "ErrUpdateBuWebsite" + ErrPortInUsed = "ErrPortInUsed" + ErrAppLimit = "ErrAppLimit" + ErrAppRequired = "ErrAppRequired" + ErrFileCanNotRead = "ErrFileCanNotRead" + ErrFileToLarge = "ErrFileToLarge" + ErrNotInstall = "ErrNotInstall" + ErrPortInOtherApp = "ErrPortInOtherApp" + ErrDbUserNotValid = "ErrDbUserNotValid" + ErrUpdateBuWebsite = "ErrUpdateBuWebsite" + Err1PanelNetworkFailed = "Err1PanelNetworkFailed" ) //website diff --git a/backend/i18n/lang/en.yaml b/backend/i18n/lang/en.yaml index 2b272cc61..7636cbfa9 100644 --- a/backend/i18n/lang/en.yaml +++ b/backend/i18n/lang/en.yaml @@ -26,6 +26,7 @@ ErrPortInOtherApp: "{{ .port }} port already in use by {{ .apps }}" ErrDbUserNotValid: "Stock database, username and password do not match!" ErrDockerComposeNotValid: "docker-compose file format error!" ErrUpdateBuWebsite: 'The application was updated successfully, but the modification of the website configuration file failed, please check the configuration!' +Err1PanelNetworkFailed: 'Default container network creation failed! {{ .detail }}' #file ErrFileCanNotRead: "File can not read" diff --git a/backend/i18n/lang/zh.yaml b/backend/i18n/lang/zh.yaml index 8f69e1f12..f8aa6d698 100644 --- a/backend/i18n/lang/zh.yaml +++ b/backend/i18n/lang/zh.yaml @@ -26,6 +26,7 @@ ErrPortInOtherApp: "{{ .port }} 端口已被 {{ .apps }}占用!" ErrDbUserNotValid: "存量数据库,用户名密码不匹配!" ErrDockerComposeNotValid: "docker-compose 文件格式错误" ErrUpdateBuWebsite: '应用更新成功,但是网站配置文件修改失败,请检查配置!' +Err1PanelNetworkFailed: '默认容器网络创建失败!{{ .detail }}' #file ErrFileCanNotRead: "此文件不支持预览" diff --git a/backend/init/app/app.go b/backend/init/app/app.go index 03a963284..ce4ae65c6 100644 --- a/backend/init/app/app.go +++ b/backend/init/app/app.go @@ -1,11 +1,11 @@ package app import ( + "github.com/1Panel-dev/1Panel/backend/utils/docker" "path" "github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/global" - "github.com/1Panel-dev/1Panel/backend/utils/docker" "github.com/1Panel-dev/1Panel/backend/utils/files" ) @@ -22,7 +22,7 @@ func Init() { createDir(fileOp, dir) } - createDefaultDockerNetwork() + _ = docker.CreateDefaultDockerNetwork() } func createDir(fileOp files.FileOp, dirPath string) { @@ -30,17 +30,3 @@ func createDir(fileOp files.FileOp, dirPath string) { _ = fileOp.CreateDir(dirPath, 0755) } } - -func createDefaultDockerNetwork() { - cli, err := docker.NewClient() - if err != nil { - global.LOG.Errorf("init docker client error", err.Error()) - return - } - if !cli.NetworkExist("1panel-network") { - if err := cli.CreateNetwork("1panel-network"); err != nil { - global.LOG.Errorf("init docker client error", err.Error()) - return - } - } -} diff --git a/backend/utils/docker/docker.go b/backend/utils/docker/docker.go index 8e9ca32db..031e7c666 100644 --- a/backend/utils/docker/docker.go +++ b/backend/utils/docker/docker.go @@ -2,6 +2,7 @@ package docker import ( "context" + "github.com/1Panel-dev/1Panel/backend/global" "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/filters" @@ -73,3 +74,18 @@ func (c Client) NetworkExist(name string) bool { } return len(networks) > 0 } + +func CreateDefaultDockerNetwork() error { + cli, err := NewClient() + if err != nil { + global.LOG.Errorf("init docker client error %s", err.Error()) + return err + } + if !cli.NetworkExist("1panel-network") { + if err := cli.CreateNetwork("1panel-network"); err != nil { + global.LOG.Errorf("create default docker network error %s", err.Error()) + return err + } + } + return nil +}