From f98f9a5872e47c64cf092a32b974907176ba6c67 Mon Sep 17 00:00:00 2001 From: ssongliu Date: Mon, 10 Oct 2022 16:47:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20http=20=E4=BB=93=E5=BA=93=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=97=B6=E5=A2=9E=E5=8A=A0=E6=8E=88=E4=BF=A1=E6=93=8D?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/image_repo.go | 2 +- backend/app/dto/image.go | 2 +- backend/app/dto/image_repo.go | 6 +- backend/app/model/image_repo.go | 2 +- backend/app/service/image.go | 20 ++++-- backend/app/service/image_repo.go | 33 +++++++++- backend/app/service/image_test.go | 34 ++++++++++ backend/constant/container.go | 2 + frontend/src/api/interface/container.ts | 5 +- frontend/src/lang/modules/en.ts | 5 +- frontend/src/lang/modules/zh.ts | 5 +- frontend/src/views/container/image/index.vue | 64 ++++++++++--------- frontend/src/views/container/repo/index.vue | 9 +-- .../views/container/repo/operator/index.vue | 12 ++-- 14 files changed, 140 insertions(+), 61 deletions(-) diff --git a/backend/app/api/v1/image_repo.go b/backend/app/api/v1/image_repo.go index 3065dd8a8..b701f385f 100644 --- a/backend/app/api/v1/image_repo.go +++ b/backend/app/api/v1/image_repo.go @@ -94,7 +94,7 @@ func (b *BaseApi) UpdateRepo(c *gin.Context) { upMap := make(map[string]interface{}) upMap["download_url"] = req.DownloadUrl - upMap["repo_name"] = req.RepoName + upMap["protocol"] = req.Protocol upMap["username"] = req.Username upMap["password"] = req.Password upMap["auth"] = req.Auth diff --git a/backend/app/dto/image.go b/backend/app/dto/image.go index 6e52ee972..b6c98fbfd 100644 --- a/backend/app/dto/image.go +++ b/backend/app/dto/image.go @@ -19,7 +19,7 @@ type ImageRemove struct { } type ImagePull struct { - RepoID uint `josn:"repoID" validate:"required"` + RepoID uint `josn:"repoID"` ImageName string `josn:"imageName" validate:"required"` } diff --git a/backend/app/dto/image_repo.go b/backend/app/dto/image_repo.go index 5a95a8500..def0e94b1 100644 --- a/backend/app/dto/image_repo.go +++ b/backend/app/dto/image_repo.go @@ -5,7 +5,7 @@ import "time" type ImageRepoCreate struct { Name string `json:"name" validate:"required"` DownloadUrl string `json:"downloadUrl"` - RepoName string `json:"repoName"` + Protocol string `json:"protocol"` Username string `json:"username"` Password string `json:"password"` Auth bool `json:"auth"` @@ -14,7 +14,7 @@ type ImageRepoCreate struct { type ImageRepoUpdate struct { ID uint `json:"id"` DownloadUrl string `json:"downloadUrl"` - RepoName string `json:"repoName"` + Protocol string `json:"protocol"` Username string `json:"username"` Password string `json:"password"` Auth bool `json:"auth"` @@ -25,7 +25,7 @@ type ImageRepoInfo struct { CreatedAt time.Time `json:"createdAt"` Name string `json:"name"` DownloadUrl string `json:"downloadUrl"` - RepoName string `json:"repoName"` + Protocol string `json:"protocol"` Username string `json:"username"` Auth bool `json:"auth"` } diff --git a/backend/app/model/image_repo.go b/backend/app/model/image_repo.go index 9d6bd6ca7..fdc28d000 100644 --- a/backend/app/model/image_repo.go +++ b/backend/app/model/image_repo.go @@ -5,7 +5,7 @@ type ImageRepo struct { Name string `gorm:"type:varchar(64);not null" json:"name"` DownloadUrl string `gorm:"type:varchar(256)" json:"downloadUrl"` - RepoName string `gorm:"type:varchar(256)" json:"repoName"` + Protocol string `gorm:"type:varchar(64)" json:"protocol"` Username string `gorm:"type:varchar(256)" json:"username"` Password string `gorm:"type:varchar(256)" json:"password"` Auth bool `gorm:"type:varchar(256)" json:"auth"` diff --git a/backend/app/service/image.go b/backend/app/service/image.go index e38d7d676..18958502d 100644 --- a/backend/app/service/image.go +++ b/backend/app/service/image.go @@ -78,7 +78,20 @@ func (u *ImageService) ImagePull(req dto.ImagePull) error { if err != nil { return err } - ctx := context.Background() + if req.RepoID == 0 { + go func() { + out, err := client.ImagePull(context.TODO(), req.ImageName, types.ImagePullOptions{}) + if err != nil { + global.LOG.Errorf("image %s pull failed, err: %v", req.ImageName, err) + return + } + defer out.Close() + buf := new(bytes.Buffer) + _, _ = buf.ReadFrom(out) + global.LOG.Debugf("image %s pull stdout: %v", req.ImageName, buf.String()) + }() + return nil + } repo, err := imageRepoRepo.Get(commonRepo.WithByID(req.RepoID)) if err != nil { return err @@ -97,11 +110,8 @@ func (u *ImageService) ImagePull(req dto.ImagePull) error { options.RegistryAuth = authStr } image := repo.DownloadUrl + "/" + req.ImageName - if len(repo.RepoName) != 0 { - image = fmt.Sprintf("%s/%s/%s", repo.DownloadUrl, repo.RepoName, req.ImageName) - } go func() { - out, err := client.ImagePull(ctx, image, options) + out, err := client.ImagePull(context.TODO(), image, options) if err != nil { global.LOG.Errorf("image %s pull failed, err: %v", image, err) return diff --git a/backend/app/service/image_repo.go b/backend/app/service/image_repo.go index 20bc34301..72e3e160c 100644 --- a/backend/app/service/image_repo.go +++ b/backend/app/service/image_repo.go @@ -1,6 +1,9 @@ package service import ( + "encoding/json" + "io/ioutil" + "github.com/1Panel-dev/1Panel/app/dto" "github.com/1Panel-dev/1Panel/constant" "github.com/jinzhu/copier" @@ -48,10 +51,34 @@ func (u *ImageRepoService) List() ([]dto.ImageRepoOption, error) { } func (u *ImageRepoService) Create(imageRepoDto dto.ImageRepoCreate) error { - imageRepo, _ := imageRepoRepo.Get(commonRepo.WithByName(imageRepoDto.RepoName)) + imageRepo, _ := imageRepoRepo.Get(commonRepo.WithByName(imageRepoDto.Name)) if imageRepo.ID != 0 { return constant.ErrRecordExist } + if imageRepo.Protocol == "http" { + file, err := ioutil.ReadFile(constant.DaemonJsonDir) + if err != nil { + return err + } + + deamonMap := make(map[string]interface{}) + if err := json.Unmarshal(file, &deamonMap); err != nil { + return err + } + if _, ok := deamonMap["insecure-registries"]; ok { + if k, v := deamonMap["insecure-registries"].([]interface{}); v { + k = append(k, imageRepoDto.DownloadUrl) + deamonMap["insecure-registries"] = k + } + } + newJson, err := json.Marshal(deamonMap) + if err != nil { + return err + } + if err := ioutil.WriteFile(constant.DaemonJsonDir, newJson, 0777); err != nil { + return err + } + } if err := copier.Copy(&imageRepo, &imageRepoDto); err != nil { return errors.WithMessage(constant.ErrStructTransform, err.Error()) } @@ -61,6 +88,10 @@ func (u *ImageRepoService) Create(imageRepoDto dto.ImageRepoCreate) error { return nil } +type DeamonJson struct { + InsecureRegistries []string `json:"insecure-registries"` +} + func (u *ImageRepoService) BatchDelete(ids []uint) error { for _, id := range ids { if id == 1 { diff --git a/backend/app/service/image_test.go b/backend/app/service/image_test.go index 7f6adbb47..912b22c37 100644 --- a/backend/app/service/image_test.go +++ b/backend/app/service/image_test.go @@ -2,11 +2,14 @@ package service import ( "context" + "encoding/json" "fmt" "io" + "io/ioutil" "os" "testing" + "github.com/1Panel-dev/1Panel/constant" "github.com/1Panel-dev/1Panel/utils/docker" ) @@ -28,3 +31,34 @@ func TestImage(t *testing.T) { fmt.Println(err) } } + +func TestDeam(t *testing.T) { + file, err := ioutil.ReadFile(constant.DaemonJsonDir) + if err != nil { + fmt.Println(err) + } + deamonMap := make(map[string]interface{}) + err = json.Unmarshal(file, &deamonMap) + fmt.Println(err) + for k, v := range deamonMap { + fmt.Println(k, v) + } + if _, ok := deamonMap["insecure-registries"]; ok { + if k, v := deamonMap["insecure-registries"].(string); v { + fmt.Println("string ", k) + } + if k, v := deamonMap["insecure-registries"].([]interface{}); v { + fmt.Println("[]string ", k) + k = append(k, "172.16.10.111:8085") + deamonMap["insecure-registries"] = k + } + } + newss, err := json.Marshal(deamonMap) + if err != nil { + fmt.Println(err) + } + fmt.Println(string(newss)) + if err := ioutil.WriteFile(constant.DaemonJsonDir, newss, 0777); err != nil { + fmt.Println(err) + } +} diff --git a/backend/constant/container.go b/backend/constant/container.go index 49d3a8dbb..fa264518f 100644 --- a/backend/constant/container.go +++ b/backend/constant/container.go @@ -9,4 +9,6 @@ const ( ContainerOpUnpause = "unPause" ContainerOpRename = "reName" ContainerOpRemove = "remove" + + DaemonJsonDir = "/System/Volumes/Data/Users/slooop/.docker/daemon.json" ) diff --git a/frontend/src/api/interface/container.ts b/frontend/src/api/interface/container.ts index 3bf17f2ec..6ce69085f 100644 --- a/frontend/src/api/interface/container.ts +++ b/frontend/src/api/interface/container.ts @@ -53,7 +53,7 @@ export namespace Container { export interface RepoCreate { name: string; downloadUrl: string; - repoName: string; + protocol: string; username: string; password: string; auth: boolean; @@ -61,6 +61,7 @@ export namespace Container { export interface RepoUpdate { id: number; downloadUrl: string; + protocol: string; username: string; password: string; auth: boolean; @@ -70,7 +71,7 @@ export namespace Container { createdAt: Date; name: string; downloadUrl: string; - repoName: string; + protocol: string; username: string; password: string; auth: boolean; diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index 5158752e7..56d05000b 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -172,7 +172,6 @@ export default { last10Min: 'Last 10 Minutes', image: 'Image', - pullFromRepo: 'Pull from repo', imagePull: 'Image pull', imagePush: 'Image push', repoName: 'Repo Name', @@ -189,11 +188,13 @@ export default { exportImage: 'ExportImage', version: 'Version', size: 'Size', + from: 'From', repo: 'Repo', name: 'Name', + protocol: 'protocol', downloadUrl: 'Download URL', - imageRepo: 'ImageRepo', + imageRepo: 'Image repo', repoHelper: 'Does it include a mirror repository/organization/project?', auth: 'Auth', }, diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index 3485b8b1a..4fac0ff8e 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -169,7 +169,6 @@ export default { last10Min: '最近 10 分钟', image: '镜像', - pullFromRepo: '从仓库中拉取', imagePull: '镜像拉取', imagePush: '镜像推送', repoName: '仓库名', @@ -186,11 +185,13 @@ export default { exportImage: '导出镜像', version: '版本', size: '大小', + from: '来源', repo: '仓库', name: '名称', + protocol: '协议', downloadUrl: '下载地址', - imageRepo: '镜像库', + imageRepo: '镜像仓库', repoHelper: '是否包含镜像仓库/组织/项目?', auth: '认证', }, diff --git a/frontend/src/views/container/image/index.vue b/frontend/src/views/container/image/index.vue index 37ca959c2..fe9ab272a 100644 --- a/frontend/src/views/container/image/index.vue +++ b/frontend/src/views/container/image/index.vue @@ -3,10 +3,10 @@