2022-10-09 16:17:15 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-10-10 16:47:05 +08:00
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
|
2022-10-18 18:39:45 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-10-09 16:17:15 +08:00
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ImageRepoService struct{}
|
|
|
|
|
|
|
|
type IImageRepoService interface {
|
|
|
|
Page(search dto.PageInfo) (int64, interface{}, error)
|
2022-10-10 15:14:49 +08:00
|
|
|
List() ([]dto.ImageRepoOption, error)
|
2022-10-09 16:17:15 +08:00
|
|
|
Create(imageRepoDto dto.ImageRepoCreate) error
|
|
|
|
Update(id uint, upMap map[string]interface{}) error
|
|
|
|
BatchDelete(ids []uint) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIImageRepoService() IImageRepoService {
|
|
|
|
return &ImageRepoService{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ImageRepoService) Page(search dto.PageInfo) (int64, interface{}, error) {
|
|
|
|
total, ops, err := imageRepoRepo.Page(search.Page, search.PageSize, commonRepo.WithOrderBy("created_at desc"))
|
|
|
|
var dtoOps []dto.ImageRepoInfo
|
|
|
|
for _, op := range ops {
|
|
|
|
var item dto.ImageRepoInfo
|
|
|
|
if err := copier.Copy(&item, &op); err != nil {
|
|
|
|
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
dtoOps = append(dtoOps, item)
|
|
|
|
}
|
|
|
|
return total, dtoOps, err
|
|
|
|
}
|
|
|
|
|
2022-10-10 15:14:49 +08:00
|
|
|
func (u *ImageRepoService) List() ([]dto.ImageRepoOption, error) {
|
|
|
|
ops, err := imageRepoRepo.List(commonRepo.WithOrderBy("created_at desc"))
|
|
|
|
var dtoOps []dto.ImageRepoOption
|
|
|
|
for _, op := range ops {
|
|
|
|
var item dto.ImageRepoOption
|
|
|
|
if err := copier.Copy(&item, &op); err != nil {
|
|
|
|
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
dtoOps = append(dtoOps, item)
|
|
|
|
}
|
|
|
|
return dtoOps, err
|
|
|
|
}
|
|
|
|
|
2022-10-09 16:17:15 +08:00
|
|
|
func (u *ImageRepoService) Create(imageRepoDto dto.ImageRepoCreate) error {
|
2022-10-10 16:47:05 +08:00
|
|
|
imageRepo, _ := imageRepoRepo.Get(commonRepo.WithByName(imageRepoDto.Name))
|
2022-10-09 16:17:15 +08:00
|
|
|
if imageRepo.ID != 0 {
|
|
|
|
return constant.ErrRecordExist
|
|
|
|
}
|
2022-10-10 16:47:05 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2022-11-15 17:20:57 +08:00
|
|
|
newJson, err := json.MarshalIndent(deamonMap, "", "\t")
|
2022-10-10 16:47:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-11-14 19:19:42 +08:00
|
|
|
if err := ioutil.WriteFile(constant.DaemonJsonDir, newJson, 0640); err != nil {
|
2022-10-10 16:47:05 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-10-09 16:17:15 +08:00
|
|
|
if err := copier.Copy(&imageRepo, &imageRepoDto); err != nil {
|
|
|
|
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
if err := imageRepoRepo.Create(&imageRepo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-10 16:47:05 +08:00
|
|
|
type DeamonJson struct {
|
|
|
|
InsecureRegistries []string `json:"insecure-registries"`
|
|
|
|
}
|
|
|
|
|
2022-10-09 16:17:15 +08:00
|
|
|
func (u *ImageRepoService) BatchDelete(ids []uint) error {
|
2022-10-10 15:14:49 +08:00
|
|
|
for _, id := range ids {
|
|
|
|
if id == 1 {
|
|
|
|
return errors.New("The default value cannot be edit !")
|
|
|
|
}
|
|
|
|
}
|
2022-10-09 16:17:15 +08:00
|
|
|
return imageRepoRepo.Delete(commonRepo.WithIdsIn(ids))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ImageRepoService) Update(id uint, upMap map[string]interface{}) error {
|
2022-10-10 15:14:49 +08:00
|
|
|
if id == 1 {
|
|
|
|
return errors.New("The default value cannot be deleted !")
|
|
|
|
}
|
2022-10-09 16:17:15 +08:00
|
|
|
return imageRepoRepo.Update(id, upMap)
|
|
|
|
}
|