2022-11-14 19:19:42 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2023-01-05 17:29:27 +08:00
|
|
|
"context"
|
2022-11-14 19:19:42 +08:00
|
|
|
"encoding/json"
|
2023-05-29 11:24:28 +08:00
|
|
|
"fmt"
|
2022-11-14 19:19:42 +08:00
|
|
|
"os"
|
2023-01-05 17:29:27 +08:00
|
|
|
"path"
|
2022-11-14 19:19:42 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2022-11-18 16:14:23 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2023-02-14 14:19:26 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/cmd"
|
2023-01-05 17:29:27 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/docker"
|
2022-11-18 16:14:23 +08:00
|
|
|
"github.com/pkg/errors"
|
2022-11-14 19:19:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type DockerService struct{}
|
|
|
|
|
|
|
|
type IDockerService interface {
|
2023-05-29 11:24:28 +08:00
|
|
|
UpdateConf(req dto.SettingUpdate) error
|
|
|
|
UpdateLogOption(req dto.LogOption) error
|
2022-11-14 19:19:42 +08:00
|
|
|
UpdateConfByFile(info dto.DaemonJsonUpdateByFile) error
|
2022-12-07 17:28:14 +08:00
|
|
|
LoadDockerStatus() string
|
|
|
|
LoadDockerConf() *dto.DaemonJsonConf
|
|
|
|
OperateDocker(req dto.DockerOperation) error
|
2022-11-14 19:19:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewIDockerService() IDockerService {
|
|
|
|
return &DockerService{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type daemonJsonItem struct {
|
2023-05-18 18:38:19 +08:00
|
|
|
Status string `json:"status"`
|
|
|
|
Mirrors []string `json:"registry-mirrors"`
|
|
|
|
Registries []string `json:"insecure-registries"`
|
|
|
|
LiveRestore bool `json:"live-restore"`
|
|
|
|
IPTables bool `json:"iptables"`
|
|
|
|
ExecOpts []string `json:"exec-opts"`
|
|
|
|
LogOption logOption `json:"log-opts"`
|
|
|
|
}
|
|
|
|
type logOption struct {
|
|
|
|
LogMaxSize string `json:"max-size"`
|
|
|
|
LogMaxFile string `json:"max-file"`
|
2022-11-14 19:19:42 +08:00
|
|
|
}
|
|
|
|
|
2022-12-07 17:28:14 +08:00
|
|
|
func (u *DockerService) LoadDockerStatus() string {
|
2023-05-18 16:46:13 +08:00
|
|
|
client, err := docker.NewDockerClient()
|
|
|
|
if err != nil {
|
|
|
|
return constant.Stopped
|
|
|
|
}
|
|
|
|
if _, err := client.Ping(context.Background()); err != nil {
|
|
|
|
return constant.Stopped
|
2023-04-27 10:24:14 +08:00
|
|
|
}
|
2022-12-08 11:56:07 +08:00
|
|
|
|
2023-05-18 16:46:13 +08:00
|
|
|
return constant.StatusRunning
|
2022-12-07 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *DockerService) LoadDockerConf() *dto.DaemonJsonConf {
|
2023-05-18 16:46:13 +08:00
|
|
|
ctx := context.Background()
|
2023-04-10 17:04:23 +08:00
|
|
|
var data dto.DaemonJsonConf
|
|
|
|
data.IPTables = true
|
|
|
|
data.Status = constant.StatusRunning
|
|
|
|
data.Version = "-"
|
2023-01-05 17:29:27 +08:00
|
|
|
client, err := docker.NewDockerClient()
|
2023-05-18 16:46:13 +08:00
|
|
|
if err != nil {
|
|
|
|
data.Status = constant.Stopped
|
|
|
|
} else {
|
|
|
|
if _, err := client.Ping(ctx); err != nil {
|
|
|
|
data.Status = constant.Stopped
|
|
|
|
}
|
2023-01-05 17:29:27 +08:00
|
|
|
itemVersion, err := client.ServerVersion(ctx)
|
|
|
|
if err == nil {
|
2023-04-10 17:04:23 +08:00
|
|
|
data.Version = itemVersion.Version
|
2023-01-05 17:29:27 +08:00
|
|
|
}
|
2022-12-07 17:28:14 +08:00
|
|
|
}
|
2023-05-18 16:46:13 +08:00
|
|
|
data.IsSwarm = false
|
|
|
|
stdout2, _ := cmd.Exec("docker info | grep Swarm")
|
|
|
|
if string(stdout2) == " Swarm: active\n" {
|
|
|
|
data.IsSwarm = true
|
|
|
|
}
|
2023-01-05 17:29:27 +08:00
|
|
|
if _, err := os.Stat(constant.DaemonJsonPath); err != nil {
|
2023-04-10 17:04:23 +08:00
|
|
|
return &data
|
2022-12-07 17:28:14 +08:00
|
|
|
}
|
2023-04-07 11:30:10 +08:00
|
|
|
file, err := os.ReadFile(constant.DaemonJsonPath)
|
2022-12-07 17:28:14 +08:00
|
|
|
if err != nil {
|
2023-04-10 17:04:23 +08:00
|
|
|
return &data
|
2022-11-14 19:19:42 +08:00
|
|
|
}
|
|
|
|
var conf daemonJsonItem
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal(file, &daemonMap); err != nil {
|
2023-04-10 17:04:23 +08:00
|
|
|
return &data
|
2022-11-14 19:19:42 +08:00
|
|
|
}
|
2023-05-30 15:30:57 +08:00
|
|
|
arr, err := json.Marshal(daemonMap)
|
2022-11-14 19:19:42 +08:00
|
|
|
if err != nil {
|
2023-04-10 17:04:23 +08:00
|
|
|
return &data
|
2022-11-14 19:19:42 +08:00
|
|
|
}
|
|
|
|
if err := json.Unmarshal(arr, &conf); err != nil {
|
2023-05-29 11:24:28 +08:00
|
|
|
fmt.Println(err)
|
2023-04-10 17:04:23 +08:00
|
|
|
return &data
|
2023-04-07 17:44:15 +08:00
|
|
|
}
|
2023-05-30 15:30:57 +08:00
|
|
|
if _, ok := daemonMap["iptables"]; !ok {
|
2023-04-07 17:44:15 +08:00
|
|
|
conf.IPTables = true
|
2022-11-14 19:19:42 +08:00
|
|
|
}
|
2023-04-10 17:04:23 +08:00
|
|
|
data.CgroupDriver = "cgroupfs"
|
2022-11-14 19:19:42 +08:00
|
|
|
for _, opt := range conf.ExecOpts {
|
|
|
|
if strings.HasPrefix(opt, "native.cgroupdriver=") {
|
2023-04-10 17:04:23 +08:00
|
|
|
data.CgroupDriver = strings.ReplaceAll(opt, "native.cgroupdriver=", "")
|
2022-11-14 19:19:42 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-05-18 18:38:19 +08:00
|
|
|
data.LogMaxSize = conf.LogOption.LogMaxSize
|
|
|
|
data.LogMaxFile = conf.LogOption.LogMaxFile
|
2023-04-10 17:04:23 +08:00
|
|
|
data.Mirrors = conf.Mirrors
|
|
|
|
data.Registries = conf.Registries
|
|
|
|
data.IPTables = conf.IPTables
|
|
|
|
data.LiveRestore = conf.LiveRestore
|
2022-12-07 17:28:14 +08:00
|
|
|
return &data
|
2022-11-14 19:19:42 +08:00
|
|
|
}
|
|
|
|
|
2023-05-29 11:24:28 +08:00
|
|
|
func (u *DockerService) UpdateConf(req dto.SettingUpdate) 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-07 17:28:14 +08:00
|
|
|
}
|
2023-01-05 17:29:27 +08:00
|
|
|
_, _ = os.Create(constant.DaemonJsonPath)
|
2022-12-07 17:28:14 +08:00
|
|
|
}
|
|
|
|
|
2023-04-07 11:30:10 +08:00
|
|
|
file, err := os.ReadFile(constant.DaemonJsonPath)
|
2022-12-07 17:28:14 +08:00
|
|
|
if err != nil {
|
2022-11-14 19:19:42 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap := make(map[string]interface{})
|
|
|
|
_ = json.Unmarshal(file, &daemonMap)
|
2022-12-07 17:28:14 +08:00
|
|
|
|
2023-05-29 11:24:28 +08:00
|
|
|
switch req.Key {
|
|
|
|
case "Registries":
|
2023-06-25 11:48:18 +08:00
|
|
|
req.Value = strings.TrimRight(req.Value, ",")
|
2023-05-29 11:24:28 +08:00
|
|
|
if len(req.Value) == 0 {
|
2023-05-30 15:30:57 +08:00
|
|
|
delete(daemonMap, "insecure-registries")
|
2023-05-29 11:24:28 +08:00
|
|
|
} else {
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap["insecure-registries"] = strings.Split(req.Value, ",")
|
2023-05-29 11:24:28 +08:00
|
|
|
}
|
|
|
|
case "Mirrors":
|
2023-06-25 11:48:18 +08:00
|
|
|
req.Value = strings.TrimRight(req.Value, ",")
|
2023-05-29 11:24:28 +08:00
|
|
|
if len(req.Value) == 0 {
|
2023-05-30 15:30:57 +08:00
|
|
|
delete(daemonMap, "registry-mirrors")
|
2023-05-29 11:24:28 +08:00
|
|
|
} else {
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap["registry-mirrors"] = strings.Split(req.Value, ",")
|
2023-05-29 11:24:28 +08:00
|
|
|
}
|
|
|
|
case "LogOption":
|
|
|
|
if req.Value == "disable" {
|
2023-05-30 15:30:57 +08:00
|
|
|
delete(daemonMap, "log-opts")
|
2023-05-29 11:24:28 +08:00
|
|
|
}
|
|
|
|
case "LiveRestore":
|
|
|
|
if req.Value == "disable" {
|
2023-05-30 15:30:57 +08:00
|
|
|
delete(daemonMap, "live-restore")
|
2023-05-29 11:24:28 +08:00
|
|
|
} else {
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap["live-restore"] = true
|
2023-05-29 11:24:28 +08:00
|
|
|
}
|
|
|
|
case "IPtables":
|
|
|
|
if req.Value == "enable" {
|
2023-05-30 15:30:57 +08:00
|
|
|
delete(daemonMap, "iptables")
|
2023-05-29 11:24:28 +08:00
|
|
|
} else {
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap["iptables"] = false
|
2023-05-29 11:24:28 +08:00
|
|
|
}
|
|
|
|
case "Dirver":
|
2023-05-30 15:30:57 +08:00
|
|
|
if opts, ok := daemonMap["exec-opts"]; ok {
|
2023-05-29 11:24:28 +08:00
|
|
|
if optsValue, isArray := opts.([]interface{}); isArray {
|
|
|
|
for i := 0; i < len(optsValue); i++ {
|
|
|
|
if opt, isStr := optsValue[i].(string); isStr {
|
|
|
|
if strings.HasPrefix(opt, "native.cgroupdriver=") {
|
|
|
|
optsValue[i] = "native.cgroupdriver=" + req.Value
|
|
|
|
break
|
|
|
|
}
|
2022-11-14 19:19:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-29 11:24:28 +08:00
|
|
|
} else {
|
|
|
|
if req.Value == "systemd" {
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap["exec-opts"] = []string{"native.cgroupdriver=systemd"}
|
2023-05-29 11:24:28 +08:00
|
|
|
}
|
2022-11-14 19:19:42 +08:00
|
|
|
}
|
2023-05-29 11:24:28 +08:00
|
|
|
}
|
2023-05-30 15:30:57 +08:00
|
|
|
if len(daemonMap) == 0 {
|
2023-05-29 11:24:28 +08:00
|
|
|
_ = os.Remove(constant.DaemonJsonPath)
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-30 15:30:57 +08:00
|
|
|
newJson, err := json.MarshalIndent(daemonMap, "", "\t")
|
2023-05-29 11:24:28 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(constant.DaemonJsonPath, newJson, 0640); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout, err := cmd.Exec("systemctl restart docker")
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(string(stdout))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *DockerService) UpdateLogOption(req dto.LogOption) error {
|
|
|
|
if _, err := os.Stat(constant.DaemonJsonPath); err != nil && os.IsNotExist(err) {
|
|
|
|
if err = os.MkdirAll(path.Dir(constant.DaemonJsonPath), os.ModePerm); err != nil {
|
|
|
|
return err
|
2022-11-14 19:19:42 +08:00
|
|
|
}
|
2023-05-29 11:24:28 +08:00
|
|
|
_, _ = os.Create(constant.DaemonJsonPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.ReadFile(constant.DaemonJsonPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-11-14 19:19:42 +08:00
|
|
|
}
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap := make(map[string]interface{})
|
|
|
|
_ = json.Unmarshal(file, &daemonMap)
|
2023-05-29 11:24:28 +08:00
|
|
|
|
2023-05-30 15:30:57 +08:00
|
|
|
changeLogOption(daemonMap, req.LogMaxFile, req.LogMaxSize)
|
|
|
|
if len(daemonMap) == 0 {
|
2023-04-09 23:58:13 +08:00
|
|
|
_ = os.Remove(constant.DaemonJsonPath)
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-30 15:30:57 +08:00
|
|
|
newJson, err := json.MarshalIndent(daemonMap, "", "\t")
|
2022-11-14 19:19:42 +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-11-14 19:19:42 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-11-18 16:14:23 +08:00
|
|
|
|
2023-02-14 14:19:26 +08:00
|
|
|
stdout, err := cmd.Exec("systemctl restart docker")
|
2022-11-18 16:14:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.New(string(stdout))
|
|
|
|
}
|
2022-11-14 19:19:42 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *DockerService) UpdateConfByFile(req dto.DaemonJsonUpdateByFile) error {
|
2023-04-09 23:58:13 +08:00
|
|
|
if len(req.File) == 0 {
|
|
|
|
_ = os.Remove(constant.DaemonJsonPath)
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-07 17:44:15 +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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, _ = os.Create(constant.DaemonJsonPath)
|
|
|
|
}
|
2023-03-10 18:05:02 +08:00
|
|
|
file, err := os.OpenFile(constant.DaemonJsonPath, os.O_WRONLY|os.O_TRUNC, 0640)
|
2022-11-14 19:19:42 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
write := bufio.NewWriter(file)
|
|
|
|
_, _ = write.WriteString(req.File)
|
|
|
|
write.Flush()
|
|
|
|
|
2023-02-14 14:19:26 +08:00
|
|
|
stdout, err := cmd.Exec("systemctl restart docker")
|
2022-11-18 16:14:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.New(string(stdout))
|
|
|
|
}
|
2022-11-14 19:19:42 +08:00
|
|
|
return nil
|
|
|
|
}
|
2022-12-07 17:28:14 +08:00
|
|
|
|
|
|
|
func (u *DockerService) OperateDocker(req dto.DockerOperation) error {
|
2023-03-11 23:44:16 +08:00
|
|
|
service := "docker"
|
|
|
|
if req.Operation == "stop" {
|
2023-05-18 16:46:13 +08:00
|
|
|
service = "docker.socket"
|
2023-03-11 23:44:16 +08:00
|
|
|
}
|
|
|
|
stdout, err := cmd.Execf("systemctl %s %s ", req.Operation, service)
|
2022-12-07 17:28:14 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.New(string(stdout))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-05-18 18:38:19 +08:00
|
|
|
|
2023-05-30 15:30:57 +08:00
|
|
|
func changeLogOption(daemonMap map[string]interface{}, logMaxFile, logMaxSize string) {
|
|
|
|
if opts, ok := daemonMap["log-opts"]; ok {
|
2023-05-18 18:38:19 +08:00
|
|
|
if len(logMaxFile) != 0 || len(logMaxSize) != 0 {
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap["log-driver"] = "json-file"
|
2023-05-18 18:38:19 +08:00
|
|
|
}
|
|
|
|
optsMap, isMap := opts.(map[string]interface{})
|
|
|
|
if isMap {
|
|
|
|
if len(logMaxFile) != 0 {
|
|
|
|
optsMap["max-file"] = logMaxFile
|
|
|
|
} else {
|
|
|
|
delete(optsMap, "max-file")
|
|
|
|
}
|
|
|
|
if len(logMaxSize) != 0 {
|
|
|
|
optsMap["max-size"] = logMaxSize
|
|
|
|
} else {
|
|
|
|
delete(optsMap, "max-size")
|
|
|
|
}
|
|
|
|
if len(optsMap) == 0 {
|
2023-05-30 15:30:57 +08:00
|
|
|
delete(daemonMap, "log-opts")
|
2023-05-18 18:38:19 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
optsMap := make(map[string]interface{})
|
|
|
|
if len(logMaxFile) != 0 {
|
|
|
|
optsMap["max-file"] = logMaxFile
|
|
|
|
}
|
|
|
|
if len(logMaxSize) != 0 {
|
|
|
|
optsMap["max-size"] = logMaxSize
|
|
|
|
}
|
|
|
|
if len(optsMap) != 0 {
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap["log-opts"] = optsMap
|
2023-05-18 18:38:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(logMaxFile) != 0 || len(logMaxSize) != 0 {
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap["log-driver"] = "json-file"
|
2023-05-18 18:38:19 +08:00
|
|
|
}
|
|
|
|
optsMap := make(map[string]interface{})
|
|
|
|
if len(logMaxFile) != 0 {
|
|
|
|
optsMap["max-file"] = logMaxFile
|
|
|
|
}
|
|
|
|
if len(logMaxSize) != 0 {
|
|
|
|
optsMap["max-size"] = logMaxSize
|
|
|
|
}
|
|
|
|
if len(optsMap) != 0 {
|
2023-05-30 15:30:57 +08:00
|
|
|
daemonMap["log-opts"] = optsMap
|
2023-05-18 18:38:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|