2022-10-10 07:14:49 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2022-10-12 05:42:58 +00:00
|
|
|
"bufio"
|
2022-10-10 07:14:49 +00:00
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2022-12-27 10:57:11 +00:00
|
|
|
"path"
|
2022-12-07 06:28:11 +00:00
|
|
|
"strings"
|
2022-10-10 07:14:49 +00:00
|
|
|
"time"
|
|
|
|
|
2022-10-18 10:39:45 +00:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/docker"
|
2022-10-10 07:14:49 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2022-10-12 05:42:58 +00:00
|
|
|
"github.com/docker/docker/pkg/archive"
|
2022-10-10 07:14:49 +00:00
|
|
|
)
|
|
|
|
|
2023-01-29 08:38:34 +00:00
|
|
|
var dockerLogDir = constant.TmpDir + "/docker_logs"
|
2022-12-07 06:28:11 +00:00
|
|
|
|
2022-10-10 07:14:49 +00:00
|
|
|
type ImageService struct{}
|
|
|
|
|
|
|
|
type IImageService interface {
|
|
|
|
Page(req dto.PageInfo) (int64, interface{}, error)
|
2022-10-12 10:55:47 +00:00
|
|
|
List() ([]dto.Options, error)
|
2022-12-07 06:28:11 +00:00
|
|
|
ImageBuild(req dto.ImageBuild) (string, error)
|
|
|
|
ImagePull(req dto.ImagePull) (string, error)
|
2022-10-10 07:14:49 +00:00
|
|
|
ImageLoad(req dto.ImageLoad) error
|
|
|
|
ImageSave(req dto.ImageSave) error
|
2022-12-07 06:28:11 +00:00
|
|
|
ImagePush(req dto.ImagePush) (string, error)
|
2022-10-11 09:46:52 +00:00
|
|
|
ImageRemove(req dto.BatchDelete) error
|
2022-10-10 07:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIImageService() IImageService {
|
|
|
|
return &ImageService{}
|
|
|
|
}
|
|
|
|
func (u *ImageService) Page(req dto.PageInfo) (int64, interface{}, error) {
|
|
|
|
var (
|
|
|
|
list []types.ImageSummary
|
|
|
|
records []dto.ImageInfo
|
|
|
|
backDatas []dto.ImageInfo
|
|
|
|
)
|
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
2022-10-12 10:55:47 +00:00
|
|
|
list, err = client.ImageList(context.Background(), types.ImageListOptions{})
|
2022-10-10 07:14:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, image := range list {
|
|
|
|
size := formatFileSize(image.Size)
|
2022-10-11 09:46:52 +00:00
|
|
|
records = append(records, dto.ImageInfo{
|
|
|
|
ID: image.ID,
|
|
|
|
Tags: image.RepoTags,
|
|
|
|
CreatedAt: time.Unix(image.Created, 0),
|
|
|
|
Size: size,
|
|
|
|
})
|
2022-10-10 07:14:49 +00:00
|
|
|
}
|
|
|
|
total, start, end := len(records), (req.Page-1)*req.PageSize, req.Page*req.PageSize
|
|
|
|
if start > total {
|
|
|
|
backDatas = make([]dto.ImageInfo, 0)
|
|
|
|
} else {
|
|
|
|
if end >= total {
|
|
|
|
end = total
|
|
|
|
}
|
|
|
|
backDatas = records[start:end]
|
|
|
|
}
|
|
|
|
|
|
|
|
return int64(total), backDatas, nil
|
|
|
|
}
|
|
|
|
|
2022-10-12 10:55:47 +00:00
|
|
|
func (u *ImageService) List() ([]dto.Options, error) {
|
|
|
|
var (
|
|
|
|
list []types.ImageSummary
|
|
|
|
backDatas []dto.Options
|
|
|
|
)
|
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
list, err = client.ImageList(context.Background(), types.ImageListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, image := range list {
|
|
|
|
for _, tag := range image.RepoTags {
|
|
|
|
backDatas = append(backDatas, dto.Options{
|
|
|
|
Option: tag,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return backDatas, nil
|
|
|
|
}
|
|
|
|
|
2022-10-12 05:42:58 +00:00
|
|
|
func (u *ImageService) ImageBuild(req dto.ImageBuild) (string, error) {
|
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if req.From == "edit" {
|
2023-01-29 08:38:34 +00:00
|
|
|
dir := fmt.Sprintf("%s/docker/build/%s", constant.DataDir, req.Name)
|
2022-10-12 05:42:58 +00:00
|
|
|
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(dir, os.ModePerm); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 06:20:51 +00:00
|
|
|
|
2022-12-26 09:51:45 +00:00
|
|
|
pathItem := fmt.Sprintf("%s/Dockerfile", dir)
|
|
|
|
file, err := os.OpenFile(pathItem, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
2022-10-12 05:42:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
write := bufio.NewWriter(file)
|
|
|
|
_, _ = write.WriteString(string(req.Dockerfile))
|
|
|
|
write.Flush()
|
|
|
|
req.Dockerfile = dir
|
|
|
|
}
|
|
|
|
tar, err := archive.TarWithOptions(req.Dockerfile+"/", &archive.TarOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := types.ImageBuildOptions{
|
|
|
|
Dockerfile: "Dockerfile",
|
|
|
|
Tags: []string{req.Name},
|
|
|
|
Remove: true,
|
|
|
|
Labels: stringsToMap(req.Tags),
|
|
|
|
}
|
|
|
|
logName := fmt.Sprintf("%s/build.log", req.Dockerfile)
|
|
|
|
|
2022-12-26 09:51:45 +00:00
|
|
|
pathItem := logName
|
|
|
|
file, err := os.OpenFile(pathItem, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
2022-10-12 05:42:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
defer file.Close()
|
2022-12-07 06:28:11 +00:00
|
|
|
defer tar.Close()
|
2022-10-12 05:42:58 +00:00
|
|
|
res, err := client.ImageBuild(context.TODO(), tar, opts)
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("build image %s failed, err: %v", req.Name, err)
|
|
|
|
return
|
|
|
|
}
|
2022-12-07 06:28:11 +00:00
|
|
|
defer res.Body.Close()
|
2022-10-12 05:42:58 +00:00
|
|
|
global.LOG.Debugf("build image %s successful!", req.Name)
|
|
|
|
_, _ = io.Copy(file, res.Body)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return logName, nil
|
2022-10-11 06:20:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 06:28:11 +00:00
|
|
|
func (u *ImageService) ImagePull(req dto.ImagePull) (string, error) {
|
2022-10-10 07:14:49 +00:00
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
2022-12-07 06:28:11 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(dockerLogDir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(dockerLogDir, os.ModePerm); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 10:57:11 +00:00
|
|
|
imageItemName := strings.ReplaceAll(path.Base(req.ImageName), ":", "_")
|
2022-12-26 09:51:45 +00:00
|
|
|
pathItem := fmt.Sprintf("%s/image_pull_%s_%s.log", dockerLogDir, imageItemName, time.Now().Format("20060102150405"))
|
|
|
|
file, err := os.OpenFile(pathItem, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
2022-12-07 06:28:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2022-10-10 07:14:49 +00:00
|
|
|
}
|
2022-10-10 08:47:05 +00:00
|
|
|
if req.RepoID == 0 {
|
|
|
|
go func() {
|
2022-12-07 06:28:11 +00:00
|
|
|
defer file.Close()
|
2022-10-10 08:47:05 +00:00
|
|
|
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()
|
2022-12-07 06:28:11 +00:00
|
|
|
global.LOG.Debugf("pull image %s successful!", req.ImageName)
|
|
|
|
_, _ = io.Copy(file, out)
|
2022-10-10 08:47:05 +00:00
|
|
|
}()
|
2022-12-26 09:51:45 +00:00
|
|
|
return pathItem, nil
|
2022-10-10 08:47:05 +00:00
|
|
|
}
|
2022-10-10 07:14:49 +00:00
|
|
|
repo, err := imageRepoRepo.Get(commonRepo.WithByID(req.RepoID))
|
|
|
|
if err != nil {
|
2022-12-07 06:28:11 +00:00
|
|
|
return "", err
|
2022-10-10 07:14:49 +00:00
|
|
|
}
|
|
|
|
options := types.ImagePullOptions{}
|
|
|
|
if repo.Auth {
|
|
|
|
authConfig := types.AuthConfig{
|
|
|
|
Username: repo.Username,
|
|
|
|
Password: repo.Password,
|
|
|
|
}
|
|
|
|
encodedJSON, err := json.Marshal(authConfig)
|
|
|
|
if err != nil {
|
2022-12-07 06:28:11 +00:00
|
|
|
return "", err
|
2022-10-10 07:14:49 +00:00
|
|
|
}
|
|
|
|
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
|
|
|
|
options.RegistryAuth = authStr
|
|
|
|
}
|
|
|
|
image := repo.DownloadUrl + "/" + req.ImageName
|
|
|
|
go func() {
|
2022-12-07 06:28:11 +00:00
|
|
|
defer file.Close()
|
2022-10-10 08:47:05 +00:00
|
|
|
out, err := client.ImagePull(context.TODO(), image, options)
|
2022-10-10 07:14:49 +00:00
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("image %s pull failed, err: %v", image, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer out.Close()
|
2022-12-07 06:28:11 +00:00
|
|
|
global.LOG.Debugf("pull image %s successful!", req.ImageName)
|
|
|
|
_, _ = io.Copy(file, out)
|
2022-10-10 07:14:49 +00:00
|
|
|
}()
|
2022-12-26 09:51:45 +00:00
|
|
|
return pathItem, nil
|
2022-10-10 07:14:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ImageService) ImageLoad(req dto.ImageLoad) error {
|
|
|
|
file, err := os.Open(req.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := client.ImageLoad(context.TODO(), file, true); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *ImageService) ImageSave(req dto.ImageSave) error {
|
2022-10-11 09:46:52 +00:00
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := client.ImageSave(context.TODO(), []string{req.TagName})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
2022-10-10 07:14:49 +00:00
|
|
|
file, err := os.OpenFile(fmt.Sprintf("%s/%s.tar", req.Path, req.Name), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
2022-10-11 09:46:52 +00:00
|
|
|
if _, err = io.Copy(file, out); err != nil {
|
2022-10-10 07:14:49 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-10-11 09:46:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
2022-10-10 07:14:49 +00:00
|
|
|
|
2022-10-11 09:46:52 +00:00
|
|
|
func (u *ImageService) ImageTag(req dto.ImageTag) error {
|
|
|
|
client, err := docker.NewDockerClient()
|
2022-10-10 07:14:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-11 09:46:52 +00:00
|
|
|
|
|
|
|
if err := client.ImageTag(context.TODO(), req.SourceID, req.TargetName); err != nil {
|
2022-10-10 07:14:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-07 06:28:11 +00:00
|
|
|
func (u *ImageService) ImagePush(req dto.ImagePush) (string, error) {
|
2022-10-10 07:14:49 +00:00
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
2022-12-07 06:28:11 +00:00
|
|
|
return "", err
|
2022-10-10 07:14:49 +00:00
|
|
|
}
|
|
|
|
repo, err := imageRepoRepo.Get(commonRepo.WithByID(req.RepoID))
|
|
|
|
if err != nil {
|
2022-12-07 06:28:11 +00:00
|
|
|
return "", err
|
2022-10-10 07:14:49 +00:00
|
|
|
}
|
|
|
|
options := types.ImagePushOptions{}
|
|
|
|
if repo.Auth {
|
|
|
|
authConfig := types.AuthConfig{
|
|
|
|
Username: repo.Username,
|
|
|
|
Password: repo.Password,
|
|
|
|
}
|
|
|
|
encodedJSON, err := json.Marshal(authConfig)
|
|
|
|
if err != nil {
|
2022-12-07 06:28:11 +00:00
|
|
|
return "", err
|
2022-10-10 07:14:49 +00:00
|
|
|
}
|
|
|
|
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
|
|
|
|
options.RegistryAuth = authStr
|
|
|
|
}
|
2022-10-11 09:46:52 +00:00
|
|
|
newName := fmt.Sprintf("%s/%s", repo.DownloadUrl, req.Name)
|
|
|
|
if newName != req.TagName {
|
|
|
|
if err := client.ImageTag(context.TODO(), req.TagName, newName); err != nil {
|
2022-12-07 06:28:11 +00:00
|
|
|
return "", err
|
2022-10-11 06:20:51 +00:00
|
|
|
}
|
2022-10-10 07:14:49 +00:00
|
|
|
}
|
2022-12-07 06:28:11 +00:00
|
|
|
if _, err := os.Stat(dockerLogDir); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(dockerLogDir, os.ModePerm); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 10:57:11 +00:00
|
|
|
imageItemName := strings.ReplaceAll(path.Base(req.Name), ":", "_")
|
2022-12-26 09:51:45 +00:00
|
|
|
pathItem := fmt.Sprintf("%s/image_push_%s_%s.log", dockerLogDir, imageItemName, time.Now().Format("20060102150405"))
|
|
|
|
file, err := os.OpenFile(pathItem, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
|
2022-12-07 06:28:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-10-10 07:14:49 +00:00
|
|
|
go func() {
|
2022-12-07 06:28:11 +00:00
|
|
|
defer file.Close()
|
2022-10-10 07:14:49 +00:00
|
|
|
out, err := client.ImagePush(context.TODO(), newName, options)
|
|
|
|
if err != nil {
|
2022-10-11 09:46:52 +00:00
|
|
|
global.LOG.Errorf("image %s push failed, err: %v", req.TagName, err)
|
2022-10-10 07:14:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer out.Close()
|
2022-12-07 06:28:11 +00:00
|
|
|
global.LOG.Debugf("push image %s successful!", req.Name)
|
|
|
|
_, _ = io.Copy(file, out)
|
2022-10-10 07:14:49 +00:00
|
|
|
}()
|
|
|
|
|
2022-12-26 09:51:45 +00:00
|
|
|
return pathItem, nil
|
2022-10-10 07:14:49 +00:00
|
|
|
}
|
|
|
|
|
2022-10-11 09:46:52 +00:00
|
|
|
func (u *ImageService) ImageRemove(req dto.BatchDelete) error {
|
2022-10-10 07:14:49 +00:00
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-13 10:54:28 +00:00
|
|
|
for _, ids := range req.Names {
|
2022-10-12 05:42:58 +00:00
|
|
|
if _, err := client.ImageRemove(context.TODO(), ids, types.ImageRemoveOptions{Force: true, PruneChildren: true}); err != nil {
|
2022-10-11 09:46:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-10-10 07:14:49 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatFileSize(fileSize int64) (size string) {
|
|
|
|
if fileSize < 1024 {
|
|
|
|
return fmt.Sprintf("%.2fB", float64(fileSize)/float64(1))
|
|
|
|
} else if fileSize < (1024 * 1024) {
|
|
|
|
return fmt.Sprintf("%.2fKB", float64(fileSize)/float64(1024))
|
|
|
|
} else if fileSize < (1024 * 1024 * 1024) {
|
|
|
|
return fmt.Sprintf("%.2fMB", float64(fileSize)/float64(1024*1024))
|
|
|
|
} else if fileSize < (1024 * 1024 * 1024 * 1024) {
|
|
|
|
return fmt.Sprintf("%.2fGB", float64(fileSize)/float64(1024*1024*1024))
|
|
|
|
} else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
|
|
|
|
return fmt.Sprintf("%.2fTB", float64(fileSize)/float64(1024*1024*1024*1024))
|
|
|
|
} else {
|
|
|
|
return fmt.Sprintf("%.2fEB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
|
|
|
|
}
|
|
|
|
}
|