fix: http 镜像仓库动态授信

pull/77/head
ssongliu 2 years ago committed by ssongliu
parent 1ed0bb691a
commit 0fea645692

@ -79,7 +79,7 @@ func (u *ContainerService) PageCompose(req dto.PageInfo) (int64, interface{}, er
}
for i := 0; i < len(composeCreatedByLocal); i++ {
if composeCreatedByLocal[i].Name == name {
composeItem.CreatedBy = "1panel"
composeItem.CreatedBy = "1Panel"
composeCreatedByLocal = append(composeCreatedByLocal[:i], composeCreatedByLocal[i+1:]...)
break
}

@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"github.com/1Panel-dev/1Panel/backend/app/dto"
@ -62,45 +63,8 @@ func (u *ImageRepoService) Create(req dto.ImageRepoCreate) error {
if imageRepo.ID != 0 {
return constant.ErrRecordExist
}
fileSetting, err := settingRepo.Get(settingRepo.WithByKey("DaemonJsonPath"))
if err != nil {
return err
}
if len(fileSetting.Value) == 0 {
return errors.New("error daemon.json")
}
if _, err := os.Stat(fileSetting.Value); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(fileSetting.Value, os.ModePerm); err != nil {
if err != nil {
return err
}
}
}
if req.Protocol == "http" {
file, err := ioutil.ReadFile(fileSetting.Value)
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 {
deamonMap["insecure-registries"] = common.RemoveRepeatElement(append(k, req.DownloadUrl))
}
} else {
deamonMap["insecure-registries"] = []string{req.DownloadUrl}
}
newJson, err := json.MarshalIndent(deamonMap, "", "\t")
if err != nil {
return err
}
if err := ioutil.WriteFile(fileSetting.Value, newJson, 0640); err != nil {
return err
}
_ = u.handleRegistries(req.DownloadUrl, "", "create")
}
if err := copier.Copy(&imageRepo, &req); err != nil {
@ -123,10 +87,6 @@ func (u *ImageRepoService) Create(req 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 {
@ -137,47 +97,10 @@ func (u *ImageRepoService) BatchDelete(ids []uint) error {
if err != nil {
return err
}
fileSetting, err := settingRepo.Get(settingRepo.WithByKey("DaemonJsonPath"))
if err != nil {
return err
}
if len(fileSetting.Value) == 0 {
return errors.New("error daemon.json")
}
deamonMap := make(map[string]interface{})
file, err := ioutil.ReadFile(fileSetting.Value)
if err != nil {
return err
}
if err := json.Unmarshal(file, &deamonMap); err != nil {
return err
}
iRegistries := deamonMap["insecure-registries"]
registries, _ := iRegistries.([]string)
if len(registries) != 0 {
for _, repo := range repos {
if repo.Protocol == "http" {
for i, regi := range registries {
if regi == repo.DownloadUrl {
registries = append(registries[:i], registries[i+1:]...)
}
}
}
}
}
if len(registries) == 0 {
delete(deamonMap, "insecure-registries")
}
newJson, err := json.MarshalIndent(deamonMap, "", "\t")
if err != nil {
return err
}
if err := ioutil.WriteFile(fileSetting.Value, newJson, 0640); err != nil {
return err
}
for _, repo := range repos {
if repo.Protocol == "http" {
_ = u.handleRegistries("", repo.DownloadUrl, "delete")
}
if repo.Auth {
cmd := exec.Command("docker", "logout", fmt.Sprintf("%s://%s", repo.Protocol, repo.DownloadUrl))
_, _ = cmd.CombinedOutput()
@ -199,6 +122,17 @@ func (u *ImageRepoService) Update(req dto.ImageRepoUpdate) error {
if req.ID == 1 {
return errors.New("The default value cannot be deleted !")
}
repo, err := imageRepoRepo.Get(commonRepo.WithByID(req.ID))
if err != nil {
return err
}
if repo.DownloadUrl != req.DownloadUrl {
_ = u.handleRegistries(req.DownloadUrl, repo.DownloadUrl, "update")
if repo.Auth {
cmd := exec.Command("docker", "logout", fmt.Sprintf("%s://%s", repo.Protocol, repo.DownloadUrl))
_, _ = cmd.CombinedOutput()
}
}
upMap := make(map[string]interface{})
upMap["download_url"] = req.DownloadUrl
@ -227,3 +161,63 @@ func (u *ImageRepoService) checkConn(host, user, password string) error {
}
return errors.New(string(stdout))
}
func (u *ImageRepoService) handleRegistries(newHost, delHost, handle string) error {
fileSetting, err := settingRepo.Get(settingRepo.WithByKey("DaemonJsonPath"))
if err != nil {
return err
}
if len(fileSetting.Value) == 0 {
return errors.New("error daemon.json in settings")
}
if _, err := os.Stat(path.Dir(fileSetting.Value)); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(fileSetting.Value, os.ModePerm); err != nil {
if err != nil {
return err
}
}
_, _ = os.Create(fileSetting.Value)
}
deamonMap := make(map[string]interface{})
file, err := ioutil.ReadFile(fileSetting.Value)
if err != nil {
return err
}
if err := json.Unmarshal(file, &deamonMap); err != nil {
return err
}
iRegistries := deamonMap["insecure-registries"]
registries, _ := iRegistries.([]interface{})
switch handle {
case "create":
registries = common.RemoveRepeatElement(append(registries, newHost))
case "update":
registries = common.RemoveRepeatElement(append(registries, newHost))
for i, regi := range registries {
if regi == delHost {
registries = append(registries[:i], registries[i+1:]...)
}
}
case "delete":
for i, regi := range registries {
if regi == delHost {
registries = append(registries[:i], registries[i+1:]...)
}
}
}
if len(registries) == 0 {
delete(deamonMap, "insecure-registries")
} else {
deamonMap["insecure-registries"] = registries
}
newJson, err := json.MarshalIndent(deamonMap, "", "\t")
if err != nil {
return err
}
if err := ioutil.WriteFile(fileSetting.Value, newJson, 0640); err != nil {
return err
}
return nil
}

@ -1,7 +1,7 @@
<template>
<div v-loading="loading">
<LayoutContent :header="composeName" back-name="Compose" :reload="true">
<div v-if="createdBy === 'local'">
<div v-if="createdBy === '1Panel'">
<el-button icon="VideoPlay" @click="onComposeOperate('start')">{{ $t('container.start') }}</el-button>
<el-button icon="VideoPause" @click="onComposeOperate('stop')">{{ $t('container.stop') }}</el-button>
<el-button icon="Delete" @click="onComposeOperate('down')" plain type="danger">

@ -34,7 +34,7 @@
</el-table-column>
<el-table-column :label="$t('container.from')" prop="createdBy" min-width="80" fix>
<template #default="{ row }">
<span v-if="row.createdBy === ''">local</span>
<span v-if="row.createdBy === ''">Local</span>
<span v-else>{{ row.createdBy }}</span>
</template>
</el-table-column>

@ -148,7 +148,7 @@
:destroy-on-close="true"
>
<el-alert :closable="false" :title="$t('app.checkInstalledWarn', ['phpMyAdmin'])" type="info">
<el-link icon="Position" @click="goRouter('/apps')" type="primary">
<el-link icon="Position" @click="goRouter('/apps/installed')" type="primary">
{{ $t('database.goInstall') }}
</el-link>
</el-alert>

Loading…
Cancel
Save