2022-09-29 18:16:56 +08:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-22 15:32:07 +08:00
|
|
|
|
2024-02-08 16:00:09 +08:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2024-05-22 18:32:47 +08:00
|
|
|
"github.com/docker/docker/api/types/image"
|
2023-11-28 15:00:10 +08:00
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2023-03-22 18:22:29 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-10-08 18:32:02 +08:00
|
|
|
|
2022-09-29 18:16:56 +08:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
cli *client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient() (Client, error) {
|
2023-11-28 15:00:10 +08:00
|
|
|
var settingItem model.Setting
|
|
|
|
_ = global.DB.Where("key = ?", "DockerSockPath").First(&settingItem).Error
|
|
|
|
if len(settingItem.Value) == 0 {
|
|
|
|
settingItem.Value = "unix:///var/run/docker.sock"
|
|
|
|
}
|
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithHost(settingItem.Value), client.WithAPIVersionNegotiation())
|
2022-09-29 18:16:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return Client{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Client{
|
|
|
|
cli: cli,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-04-25 16:45:12 +08:00
|
|
|
func (c Client) Close() {
|
|
|
|
_ = c.cli.Close()
|
|
|
|
}
|
|
|
|
|
2022-10-08 18:32:02 +08:00
|
|
|
func NewDockerClient() (*client.Client, error) {
|
2023-11-28 15:00:10 +08:00
|
|
|
var settingItem model.Setting
|
|
|
|
_ = global.DB.Where("key = ?", "DockerSockPath").First(&settingItem).Error
|
|
|
|
if len(settingItem.Value) == 0 {
|
|
|
|
settingItem.Value = "unix:///var/run/docker.sock"
|
|
|
|
}
|
|
|
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithHost(settingItem.Value), client.WithAPIVersionNegotiation())
|
2022-10-08 18:32:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return cli, nil
|
|
|
|
}
|
|
|
|
|
2022-09-29 18:16:56 +08:00
|
|
|
func (c Client) ListContainersByName(names []string) ([]types.Container, error) {
|
2023-09-12 17:52:11 +08:00
|
|
|
var (
|
2024-02-27 11:28:12 +08:00
|
|
|
options container.ListOptions
|
|
|
|
namesMap = make(map[string]bool)
|
|
|
|
res []types.Container
|
2023-09-12 17:52:11 +08:00
|
|
|
)
|
2024-05-31 17:57:20 +08:00
|
|
|
options.All = true
|
2022-09-29 18:16:56 +08:00
|
|
|
if len(names) > 0 {
|
|
|
|
var array []filters.KeyValuePair
|
|
|
|
for _, n := range names {
|
2024-02-27 11:28:12 +08:00
|
|
|
namesMap["/"+n] = true
|
2022-09-29 18:16:56 +08:00
|
|
|
array = append(array, filters.Arg("name", n))
|
|
|
|
}
|
|
|
|
options.Filters = filters.NewArgs(array...)
|
|
|
|
}
|
|
|
|
containers, err := c.cli.ContainerList(context.Background(), options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-27 11:28:12 +08:00
|
|
|
for _, con := range containers {
|
|
|
|
if _, ok := namesMap[con.Names[0]]; ok {
|
|
|
|
res = append(res, con)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
2022-09-29 18:16:56 +08:00
|
|
|
}
|
2024-05-31 17:57:20 +08:00
|
|
|
func (c Client) ListAllContainers() ([]types.Container, error) {
|
|
|
|
var (
|
|
|
|
options container.ListOptions
|
|
|
|
)
|
|
|
|
options.All = true
|
|
|
|
containers, err := c.cli.ContainerList(context.Background(), options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return containers, nil
|
|
|
|
}
|
2022-10-09 23:35:24 +08:00
|
|
|
|
2022-10-10 22:56:42 +08:00
|
|
|
func (c Client) CreateNetwork(name string) error {
|
|
|
|
_, err := c.cli.NetworkCreate(context.Background(), name, types.NetworkCreate{
|
|
|
|
Driver: "bridge",
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2022-10-09 23:35:24 +08:00
|
|
|
|
2023-04-11 17:08:29 +08:00
|
|
|
func (c Client) DeleteImage(imageID string) error {
|
2024-05-22 18:32:47 +08:00
|
|
|
if _, err := c.cli.ImageRemove(context.Background(), imageID, image.RemoveOptions{Force: true}); err != nil {
|
2023-04-11 17:08:29 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-26 22:50:16 +08:00
|
|
|
func (c Client) InspectContainer(containerID string) (types.ContainerJSON, error) {
|
|
|
|
return c.cli.ContainerInspect(context.Background(), containerID)
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:52:13 +08:00
|
|
|
func (c Client) PullImage(imageName string, force bool) error {
|
|
|
|
if !force {
|
|
|
|
exist, err := c.CheckImageExist(imageName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if exist {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2024-05-22 18:32:47 +08:00
|
|
|
if _, err := c.cli.ImagePull(context.Background(), imageName, image.PullOptions{}); err != nil {
|
2023-08-11 15:52:13 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-11 17:08:29 +08:00
|
|
|
func (c Client) GetImageIDByName(imageName string) (string, error) {
|
|
|
|
filter := filters.NewArgs()
|
|
|
|
filter.Add("reference", imageName)
|
2024-05-22 18:32:47 +08:00
|
|
|
list, err := c.cli.ImageList(context.Background(), image.ListOptions{
|
2023-04-11 17:08:29 +08:00
|
|
|
Filters: filter,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(list) > 0 {
|
|
|
|
return list[0].ID, nil
|
|
|
|
}
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2023-08-11 15:52:13 +08:00
|
|
|
func (c Client) CheckImageExist(imageName string) (bool, error) {
|
|
|
|
filter := filters.NewArgs()
|
|
|
|
filter.Add("reference", imageName)
|
2024-05-22 18:32:47 +08:00
|
|
|
list, err := c.cli.ImageList(context.Background(), image.ListOptions{
|
2023-08-11 15:52:13 +08:00
|
|
|
Filters: filter,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return len(list) > 0, nil
|
|
|
|
}
|
|
|
|
|
2022-10-10 22:56:42 +08:00
|
|
|
func (c Client) NetworkExist(name string) bool {
|
|
|
|
var options types.NetworkListOptions
|
2022-12-04 21:18:24 +08:00
|
|
|
options.Filters = filters.NewArgs(filters.Arg("name", name))
|
2022-10-10 22:56:42 +08:00
|
|
|
networks, err := c.cli.NetworkList(context.Background(), options)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
2022-10-09 23:35:24 +08:00
|
|
|
}
|
2022-10-10 22:56:42 +08:00
|
|
|
return len(networks) > 0
|
2022-10-09 23:35:24 +08:00
|
|
|
}
|
2023-03-22 18:22:29 +08:00
|
|
|
|
|
|
|
func CreateDefaultDockerNetwork() error {
|
|
|
|
cli, err := NewClient()
|
|
|
|
if err != nil {
|
|
|
|
global.LOG.Errorf("init docker client error %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2024-04-25 16:45:12 +08:00
|
|
|
defer cli.Close()
|
2023-03-22 18:22:29 +08:00
|
|
|
if !cli.NetworkExist("1panel-network") {
|
|
|
|
if err := cli.CreateNetwork("1panel-network"); err != nil {
|
|
|
|
global.LOG.Errorf("create default docker network error %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|