2022-10-09 16:17:15 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2023-02-24 18:50:35 +08:00
|
|
|
"context"
|
2022-10-10 16:47:05 +08:00
|
|
|
"encoding/json"
|
2023-06-25 18:31:21 +08:00
|
|
|
"fmt"
|
2022-12-07 18:15:56 +08:00
|
|
|
"os"
|
2022-12-19 12:50:58 +08:00
|
|
|
"path"
|
2022-12-18 23:00:24 +08:00
|
|
|
"strings"
|
2023-02-24 18:50:35 +08:00
|
|
|
"time"
|
2022-10-10 16:47:05 +08:00
|
|
|
|
2022-10-18 18:39:45 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2023-06-26 17:24:06 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
2022-10-18 18:39:45 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-02-24 18:50:35 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2023-02-14 11:28:38 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2022-12-18 23:00:24 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/common"
|
2022-10-09 16:17:15 +08:00
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ImageRepoService struct{}
|
|
|
|
|
|
|
|
type IImageRepoService interface {
|
2023-02-07 18:48:32 +08:00
|
|
|
Page(search dto.SearchWithPage) (int64, interface{}, error)
|
2022-10-10 15:14:49 +08:00
|
|
|
List() ([]dto.ImageRepoOption, error)
|
2023-02-27 11:46:23 +08:00
|
|
|
Login(req dto.OperateByID) error
|
2022-12-18 23:00:24 +08:00
|
|
|
Create(req dto.ImageRepoCreate) error
|
|
|
|
Update(req dto.ImageRepoUpdate) error
|
2022-12-20 13:23:01 +08:00
|
|
|
BatchDelete(req dto.ImageRepoDelete) error
|
2022-10-09 16:17:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIImageRepoService() IImageRepoService {
|
|
|
|
return &ImageRepoService{}
|
|
|
|
}
|
|
|
|
|
2023-02-07 18:48:32 +08:00
|
|
|
func (u *ImageRepoService) Page(req dto.SearchWithPage) (int64, interface{}, error) {
|
|
|
|
total, ops, err := imageRepoRepo.Page(req.Page, req.PageSize, commonRepo.WithLikeName(req.Info), commonRepo.WithOrderBy("created_at desc"))
|
2022-10-09 16:17:15 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-02-27 11:46:23 +08:00
|
|
|
func (u *ImageRepoService) Login(req dto.OperateByID) error {
|
|
|
|
repo, err := imageRepoRepo.Get(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-30 13:56:57 +08:00
|
|
|
if repo.Auth {
|
|
|
|
if err := u.CheckConn(repo.DownloadUrl, repo.Username, repo.Password); err != nil {
|
|
|
|
_ = imageRepoRepo.Update(repo.ID, map[string]interface{}{"status": constant.StatusFailed, "message": err.Error()})
|
|
|
|
return err
|
|
|
|
}
|
2023-02-27 11:46:23 +08:00
|
|
|
}
|
|
|
|
_ = imageRepoRepo.Update(repo.ID, map[string]interface{}{"status": constant.StatusSuccess})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-12-18 23:00:24 +08:00
|
|
|
if op.Status == constant.StatusSuccess {
|
|
|
|
var item dto.ImageRepoOption
|
|
|
|
if err := copier.Copy(&item, &op); err != nil {
|
|
|
|
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
|
|
|
dtoOps = append(dtoOps, item)
|
2022-10-10 15:14:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return dtoOps, err
|
|
|
|
}
|
|
|
|
|
2022-12-18 23:00:24 +08:00
|
|
|
func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error {
|
2023-06-26 17:24:06 +08:00
|
|
|
if cmd.CheckIllegal(req.Username, req.Password, req.DownloadUrl) {
|
2023-07-17 16:34:29 +08:00
|
|
|
return buserr.New(constant.ErrCmdIllegal)
|
2023-06-26 17:24:06 +08:00
|
|
|
}
|
2022-12-18 23:00:24 +08:00
|
|
|
imageRepo, _ := imageRepoRepo.Get(commonRepo.WithByName(req.Name))
|
2022-10-09 16:17:15 +08:00
|
|
|
if imageRepo.ID != 0 {
|
|
|
|
return constant.ErrRecordExist
|
|
|
|
}
|
2022-12-18 23:00:24 +08:00
|
|
|
if req.Protocol == "http" {
|
2022-12-19 12:50:58 +08:00
|
|
|
_ = u.handleRegistries(req.DownloadUrl, "", "create")
|
2023-02-27 11:46:23 +08:00
|
|
|
stdout, err := cmd.Exec("systemctl restart docker")
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(string(stdout))
|
|
|
|
}
|
2023-02-24 18:50:35 +08:00
|
|
|
ticker := time.NewTicker(3 * time.Second)
|
2023-05-30 15:30:57 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
|
2023-02-27 11:46:23 +08:00
|
|
|
if err := func() error {
|
|
|
|
for range ticker.C {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2023-05-30 15:30:57 +08:00
|
|
|
cancel()
|
2023-02-27 11:46:23 +08:00
|
|
|
return errors.New("the docker service cannot be restarted")
|
|
|
|
default:
|
|
|
|
stdout, err := cmd.Exec("systemctl is-active docker")
|
|
|
|
if string(stdout) == "active\n" && err == nil {
|
|
|
|
global.LOG.Info("docker restart with new conf successful!")
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-24 18:50:35 +08:00
|
|
|
}
|
|
|
|
}
|
2023-02-27 11:46:23 +08:00
|
|
|
return nil
|
|
|
|
}(); err != nil {
|
|
|
|
return err
|
2023-02-14 11:28:38 +08:00
|
|
|
}
|
2022-10-10 16:47:05 +08:00
|
|
|
}
|
2022-12-18 23:00:24 +08:00
|
|
|
|
|
|
|
if err := copier.Copy(&imageRepo, &req); err != nil {
|
2022-10-09 16:17:15 +08:00
|
|
|
return errors.WithMessage(constant.ErrStructTransform, err.Error())
|
|
|
|
}
|
2022-12-20 13:23:01 +08:00
|
|
|
|
2022-12-18 23:00:24 +08:00
|
|
|
imageRepo.Status = constant.StatusSuccess
|
2023-05-30 13:56:57 +08:00
|
|
|
if req.Auth {
|
|
|
|
if err := u.CheckConn(req.DownloadUrl, req.Username, req.Password); err != nil {
|
|
|
|
imageRepo.Status = constant.StatusFailed
|
|
|
|
imageRepo.Message = err.Error()
|
|
|
|
}
|
2022-12-18 23:00:24 +08:00
|
|
|
}
|
2022-10-09 16:17:15 +08:00
|
|
|
if err := imageRepoRepo.Create(&imageRepo); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-18 23:00:24 +08:00
|
|
|
|
2022-10-09 16:17:15 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-20 13:23:01 +08:00
|
|
|
func (u *ImageRepoService) BatchDelete(req dto.ImageRepoDelete) error {
|
|
|
|
for _, id := range req.Ids {
|
2022-10-10 15:14:49 +08:00
|
|
|
if id == 1 {
|
|
|
|
return errors.New("The default value cannot be edit !")
|
|
|
|
}
|
|
|
|
}
|
2022-12-20 13:23:01 +08:00
|
|
|
if err := imageRepoRepo.Delete(commonRepo.WithIdsIn(req.Ids)); err != nil {
|
2022-12-18 23:00:24 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-10-09 16:17:15 +08:00
|
|
|
}
|
|
|
|
|
2022-12-18 23:00:24 +08:00
|
|
|
func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error {
|
|
|
|
if req.ID == 1 {
|
2022-10-10 15:14:49 +08:00
|
|
|
return errors.New("The default value cannot be deleted !")
|
|
|
|
}
|
2023-06-26 17:24:06 +08:00
|
|
|
if cmd.CheckIllegal(req.Username, req.Password, req.DownloadUrl) {
|
2023-07-17 16:34:29 +08:00
|
|
|
return buserr.New(constant.ErrCmdIllegal)
|
2023-06-26 17:24:06 +08:00
|
|
|
}
|
2022-12-19 12:50:58 +08:00
|
|
|
repo, err := imageRepoRepo.Get(commonRepo.WithByID(req.ID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-30 13:56:57 +08:00
|
|
|
if repo.DownloadUrl != req.DownloadUrl || (!repo.Auth && req.Auth) {
|
2022-12-19 12:50:58 +08:00
|
|
|
_ = u.handleRegistries(req.DownloadUrl, repo.DownloadUrl, "update")
|
|
|
|
if repo.Auth {
|
2023-06-25 18:31:21 +08:00
|
|
|
_, _ = cmd.ExecWithCheck("docker", "logout", repo.DownloadUrl)
|
2022-12-19 12:50:58 +08:00
|
|
|
}
|
2023-02-14 14:19:26 +08:00
|
|
|
stdout, err := cmd.Exec("systemctl restart docker")
|
2022-12-20 13:23:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.New(string(stdout))
|
|
|
|
}
|
2022-12-19 12:50:58 +08:00
|
|
|
}
|
2022-12-18 23:00:24 +08:00
|
|
|
|
|
|
|
upMap := make(map[string]interface{})
|
|
|
|
upMap["download_url"] = req.DownloadUrl
|
|
|
|
upMap["protocol"] = req.Protocol
|
|
|
|
upMap["username"] = req.Username
|
|
|
|
upMap["password"] = req.Password
|
|
|
|
upMap["auth"] = req.Auth
|
|
|
|
|
|
|
|
upMap["status"] = constant.StatusSuccess
|
|
|
|
upMap["message"] = ""
|
2023-05-30 13:56:57 +08:00
|
|
|
if req.Auth {
|
|
|
|
if err := u.CheckConn(req.DownloadUrl, req.Username, req.Password); err != nil {
|
|
|
|
upMap["status"] = constant.StatusFailed
|
|
|
|
upMap["message"] = err.Error()
|
|
|
|
}
|
2022-12-18 23:00:24 +08:00
|
|
|
}
|
|
|
|
return imageRepoRepo.Update(req.ID, upMap)
|
|
|
|
}
|
|
|
|
|
2023-02-27 11:46:23 +08:00
|
|
|
func (u *ImageRepoService) CheckConn(host, user, password string) error {
|
2023-06-25 18:31:21 +08:00
|
|
|
stdout, err := cmd.ExecWithCheck("docker", "login", "-u", user, "-p", password, host)
|
2022-12-18 23:00:24 +08:00
|
|
|
if err != nil {
|
2023-06-25 18:31:21 +08:00
|
|
|
return fmt.Errorf("stdout: %s, stderr: %v", stdout, err)
|
2022-12-18 23:00:24 +08:00
|
|
|
}
|
|
|
|
if strings.Contains(string(stdout), "Login Succeeded") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return errors.New(string(stdout))
|
2022-10-09 16:17:15 +08:00
|
|
|
}
|
2022-12-19 12:50:58 +08:00
|
|
|
|
|
|
|
func (u *ImageRepoService) handleRegistries(newHost, delHost, handle string) error {
|
2023-01-05 17:29:27 +08:00
|
|
|
if _, err := os.Stat(constant.DaemonJsonPath); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(path.Dir(constant.DaemonJsonPath), os.ModePerm); err != nil {
|
2023-02-21 19:06:24 +08:00
|
|
|
return err
|
2022-12-19 12:50:58 +08:00
|
|
|
}
|
2023-01-05 17:29:27 +08:00
|
|
|
_, _ = os.Create(constant.DaemonJsonPath)
|
2022-12-19 12:50:58 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap := make(map[string]interface{})
|
2023-04-07 11:30:10 +08:00
|
|
|
file, err := os.ReadFile(constant.DaemonJsonPath)
|
2022-12-19 12:50:58 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-30 15:30:57 +08:00
|
|
|
if err := json.Unmarshal(file, &daemonMap); err != nil {
|
2022-12-19 12:50:58 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-30 15:30:57 +08:00
|
|
|
iRegistries := daemonMap["insecure-registries"]
|
2022-12-19 12:50:58 +08:00
|
|
|
registries, _ := iRegistries.([]interface{})
|
|
|
|
switch handle {
|
|
|
|
case "create":
|
|
|
|
registries = common.RemoveRepeatElement(append(registries, newHost))
|
|
|
|
case "update":
|
|
|
|
for i, regi := range registries {
|
|
|
|
if regi == delHost {
|
|
|
|
registries = append(registries[:i], registries[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
2023-11-22 15:42:07 +08:00
|
|
|
registries = common.RemoveRepeatElement(append(registries, newHost))
|
2022-12-19 12:50:58 +08:00
|
|
|
case "delete":
|
|
|
|
for i, regi := range registries {
|
|
|
|
if regi == delHost {
|
|
|
|
registries = append(registries[:i], registries[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(registries) == 0 {
|
2023-05-30 15:30:57 +08:00
|
|
|
delete(daemonMap, "insecure-registries")
|
2022-12-19 12:50:58 +08:00
|
|
|
} else {
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap["insecure-registries"] = registries
|
2022-12-19 12:50:58 +08:00
|
|
|
}
|
2023-05-30 15:30:57 +08:00
|
|
|
newJson, err := json.MarshalIndent(daemonMap, "", "\t")
|
2022-12-19 12:50:58 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-07 11:30:10 +08:00
|
|
|
if err := os.WriteFile(constant.DaemonJsonPath, newJson, 0640); err != nil {
|
2022-12-19 12:50:58 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|