You've already forked CasaOS
mirror of
https://github.com/IceWhaleTech/CasaOS.git
synced 2025-07-12 23:50:14 +02:00
wip
This commit is contained in:
@ -17,6 +17,8 @@ type DockerStatsModel struct {
|
||||
Previous interface{} `json:"previous"`
|
||||
}
|
||||
|
||||
type DeckerDaemonModel struct {
|
||||
Graph string `json:"graph"`
|
||||
// reference - https://docs.docker.com/engine/reference/commandline/dockerd/#daemon-configuration-file
|
||||
type DockerDaemonConfigurationModel struct {
|
||||
// e.g. `/var/lib/docker`
|
||||
Root string `json:"data-root,omitempty"`
|
||||
}
|
||||
|
@ -15,7 +15,6 @@ import (
|
||||
)
|
||||
|
||||
func NewSshClient(user, password string, port string) (*ssh.Client, error) {
|
||||
|
||||
// connet to ssh
|
||||
// addr = fmt.Sprintf("%s:%d", host, port)
|
||||
|
||||
@ -90,11 +89,11 @@ func (w *wsBufferWriter) Write(p []byte) (int, error) {
|
||||
defer w.mu.Unlock()
|
||||
return w.buffer.Write(p)
|
||||
}
|
||||
|
||||
func (s *SshConn) Close() {
|
||||
if s.Session != nil {
|
||||
s.Session.Close()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const (
|
||||
@ -111,7 +110,6 @@ func ReceiveWsMsgUser(wsConn *websocket.Conn, logBuff *bytes.Buffer) string {
|
||||
// read websocket msg
|
||||
_, wsData, err := wsConn.ReadMessage()
|
||||
if err != nil {
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
@ -273,6 +271,7 @@ func (ssConn *SshConn) SendComboOutput(wsConn *websocket.Conn, exitCh chan bool)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func flushComboOutput(w *wsBufferWriter, wsConn *websocket.Conn) error {
|
||||
if w.buffer.Len() != 0 {
|
||||
err := wsConn.WriteMessage(websocket.TextMessage, w.buffer.Bytes())
|
||||
@ -341,6 +340,7 @@ func (ssConn *SshConn) Login(wsConn *websocket.Conn, logBuff *bytes.Buffer, exit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (ssConn *SshConn) SessionWait(quitChan chan bool) {
|
||||
if err := ssConn.Session.Wait(); err != nil {
|
||||
logrus.WithError(err).Error("ssh session wait failed")
|
||||
|
@ -36,7 +36,7 @@ func ExecResultStrArray(cmdStr string) []string {
|
||||
return nil
|
||||
}
|
||||
// str, err := ioutil.ReadAll(stdout)
|
||||
var networklist = []string{}
|
||||
networklist := []string{}
|
||||
outputBuf := bufio.NewReader(stdout)
|
||||
for {
|
||||
output, _, err := outputBuf.ReadLine()
|
||||
@ -54,6 +54,8 @@ func ExecResultStrArray(cmdStr string) []string {
|
||||
|
||||
func ExecResultStr(cmdStr string) string {
|
||||
cmd := exec.Command("/bin/bash", "-c", cmdStr)
|
||||
println(cmd.String())
|
||||
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@ -107,6 +109,5 @@ func ExecSmartCTLByPath(path string) []byte {
|
||||
}
|
||||
|
||||
func ExecEnabledSMART(path string) {
|
||||
|
||||
exec.Command("smartctl", "-s on", path).Output()
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"mime/multipart"
|
||||
@ -60,7 +61,7 @@ func MkDir(src string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
os.Chmod(src, 0777)
|
||||
os.Chmod(src, 0o777)
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -103,7 +104,7 @@ func MustOpen(fileName, filePath string) (*os.File, error) {
|
||||
return nil, fmt.Errorf("file.IsNotExistMkDir src: %s, err: %v", src, err)
|
||||
}
|
||||
|
||||
f, err := Open(src+fileName, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
|
||||
f, err := Open(src+fileName, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0o644)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Fail to OpenFile :%v", err)
|
||||
}
|
||||
@ -147,7 +148,7 @@ func CreateFile(path string) error {
|
||||
}
|
||||
|
||||
func CreateFileAndWriteContent(path string, content string) error {
|
||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
|
||||
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0o666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -163,7 +164,7 @@ func CreateFileAndWriteContent(path string, content string) error {
|
||||
|
||||
// IsNotExistMkDir create a directory if it does not exist
|
||||
func IsNotExistCreateFile(src string) error {
|
||||
if notExist := CheckNotExist(src); notExist == true {
|
||||
if notExist := CheckNotExist(src); notExist {
|
||||
if err := CreateFile(src); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -336,10 +337,17 @@ func WriteToPath(data []byte, path, name string) error {
|
||||
} else {
|
||||
fullPath += "/" + name
|
||||
}
|
||||
IsNotExistCreateFile(fullPath)
|
||||
return WriteToFullPath(data, fullPath, 0o666)
|
||||
}
|
||||
|
||||
func WriteToFullPath(data []byte, fullPath string, perm fs.FileMode) error {
|
||||
if err := IsNotExistCreateFile(fullPath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := os.OpenFile(fullPath,
|
||||
os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
|
||||
0666,
|
||||
perm,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -352,14 +360,13 @@ func WriteToPath(data []byte, path, name string) error {
|
||||
|
||||
// 最终拼接
|
||||
func SpliceFiles(dir, path string, length int, startPoint int) error {
|
||||
|
||||
fullPath := path
|
||||
|
||||
IsNotExistCreateFile(fullPath)
|
||||
|
||||
file, _ := os.OpenFile(fullPath,
|
||||
os.O_WRONLY|os.O_TRUNC|os.O_CREATE,
|
||||
0666,
|
||||
0o666,
|
||||
)
|
||||
defer file.Close()
|
||||
bufferedWriter := bufio.NewWriter(file)
|
||||
@ -380,7 +387,6 @@ func SpliceFiles(dir, path string, length int, startPoint int) error {
|
||||
}
|
||||
|
||||
func GetCompressionAlgorithm(t string) (string, archiver.Writer, error) {
|
||||
|
||||
switch t {
|
||||
case "zip", "":
|
||||
return ".zip", archiver.NewZip(), nil
|
||||
@ -400,8 +406,8 @@ func GetCompressionAlgorithm(t string) (string, archiver.Writer, error) {
|
||||
return "", nil, errors.New("format not implemented")
|
||||
}
|
||||
}
|
||||
func AddFile(ar archiver.Writer, path, commonPath string) error {
|
||||
|
||||
func AddFile(ar archiver.Writer, path, commonPath string) error {
|
||||
info, err := os.Stat(path)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -447,6 +453,7 @@ func AddFile(ar archiver.Writer, path, commonPath string) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CommonPrefix(sep byte, paths ...string) string {
|
||||
// Handle special cases.
|
||||
switch len(paths) {
|
||||
|
@ -11,7 +11,6 @@ import (
|
||||
)
|
||||
|
||||
func InitRouter() *gin.Engine {
|
||||
|
||||
r := gin.Default()
|
||||
|
||||
r.Use(middleware.Cors())
|
||||
@ -100,8 +99,8 @@ func InitRouter() *gin.Engine {
|
||||
v1ContainerGroup.PUT("/:id/latest", v1.PutAppUpdate)
|
||||
// Not used
|
||||
v1ContainerGroup.POST("/share", v1.ShareAppFile)
|
||||
v1ContainerGroup.GET("/info", v1.GetcontainerInfo)
|
||||
v1ContainerGroup.PUT("/info", v1.PutcontainerInfo)
|
||||
v1ContainerGroup.GET("/info", v1.GetDockerDaemonConfiguration)
|
||||
v1ContainerGroup.PUT("/info", v1.PutDockerDaemonConfiguration)
|
||||
|
||||
}
|
||||
v1AppCategoriesGroup := v1Group.Group("/app-categories")
|
||||
|
@ -2,8 +2,9 @@ package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
|
||||
"github.com/IceWhaleTech/CasaOS/model"
|
||||
@ -16,6 +17,10 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
dockerDaemonConfigurationFilePath = "/etc/docker/daemon.json"
|
||||
)
|
||||
|
||||
// @Summary 获取远程列表
|
||||
// @Produce application/json
|
||||
// @Accept application/json
|
||||
@ -29,7 +34,6 @@ import (
|
||||
// @Success 200 {string} string "ok"
|
||||
// @Router /app/list [get]
|
||||
func AppList(c *gin.Context) {
|
||||
|
||||
// service.MyService.Docker().DockerContainerCommit("test2")
|
||||
|
||||
index := c.DefaultQuery("index", "1")
|
||||
@ -151,7 +155,6 @@ func AppUsageList(c *gin.Context) {
|
||||
// @Success 200 {string} string "ok"
|
||||
// @Router /app/appinfo/{id} [get]
|
||||
func AppInfo(c *gin.Context) {
|
||||
|
||||
id := c.Param("id")
|
||||
language := c.GetHeader("Language")
|
||||
info, err := service.MyService.Casa().GetServerAppInfo(id, "", language)
|
||||
@ -265,53 +268,89 @@ func ShareAppFile(c *gin.Context) {
|
||||
c.JSON(common_err.SUCCESS, json.RawMessage(content))
|
||||
}
|
||||
|
||||
func GetcontainerInfo(c *gin.Context) {
|
||||
func GetDockerDaemonConfiguration(c *gin.Context) {
|
||||
// info, err := service.MyService.Docker().GetDockerInfo()
|
||||
// if err != nil {
|
||||
// c.JSON(common_err.SERVICE_ERROR, &model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
|
||||
// return
|
||||
// }
|
||||
daemon := model.DeckerDaemonModel{}
|
||||
dockerConfig := model.DockerDaemonConfigurationModel{}
|
||||
data := make(map[string]interface{}, 1)
|
||||
data["docker_root_dir"] = ""
|
||||
if file.Exists("/etc/docker/daemon.json") {
|
||||
byteResult := file.ReadFullFile("/etc/docker/daemon.json")
|
||||
err := json.Unmarshal(byteResult, &daemon)
|
||||
|
||||
// TODO read dockerRootDir from /etc/casaos/casaos.conf
|
||||
if file.Exists(dockerDaemonConfigurationFilePath) {
|
||||
byteResult := file.ReadFullFile(dockerDaemonConfigurationFilePath)
|
||||
err := json.Unmarshal(byteResult, &dockerConfig)
|
||||
if err != nil {
|
||||
c.JSON(common_err.CLIENT_ERROR, &model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.INVALID_PARAMS), Data: err.Error()})
|
||||
return
|
||||
}
|
||||
data["docker_root_dir"] = daemon.Graph
|
||||
|
||||
if dockerConfig.Root != "" {
|
||||
data["docker_root_dir"] = dockerConfig.Root
|
||||
} else {
|
||||
data["docker_root_dir"] = "/var/lib/docker"
|
||||
}
|
||||
}
|
||||
c.JSON(common_err.SUCCESS, &model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
|
||||
}
|
||||
func PutcontainerInfo(c *gin.Context) {
|
||||
|
||||
func PutDockerDaemonConfiguration(c *gin.Context) {
|
||||
js := make(map[string]interface{})
|
||||
err := c.BindJSON(&js)
|
||||
if err != nil {
|
||||
c.JSON(common_err.CLIENT_ERROR, &model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.INVALID_PARAMS), Data: err.Error()})
|
||||
if err := c.BindJSON(&js); err != nil {
|
||||
c.JSON(http.StatusBadRequest, &model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.INVALID_PARAMS), Data: err.Error()})
|
||||
return
|
||||
}
|
||||
dockerRootDir := js["docker_root_dir"].(string)
|
||||
daemon := model.DeckerDaemonModel{}
|
||||
if file.Exists("/etc/docker/daemon.json") {
|
||||
byteResult := file.ReadFullFile("/etc/docker/daemon.json")
|
||||
err := json.Unmarshal(byteResult, &daemon)
|
||||
|
||||
value, ok := js["docker_root_dir"]
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, &model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.INVALID_PARAMS), Data: "`docker_root_dir` should not empty"})
|
||||
return
|
||||
}
|
||||
|
||||
dockerConfig := model.DockerDaemonConfigurationModel{}
|
||||
if file.Exists(dockerDaemonConfigurationFilePath) {
|
||||
byteResult := file.ReadFullFile(dockerDaemonConfigurationFilePath)
|
||||
err := json.Unmarshal(byteResult, &dockerConfig)
|
||||
if err != nil {
|
||||
c.JSON(common_err.CLIENT_ERROR, &model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.INVALID_PARAMS), Data: err.Error()})
|
||||
c.JSON(http.StatusInternalServerError, &model.Result{Success: common_err.SERVICE_ERROR, Message: "error when trying to deserialize " + dockerDaemonConfigurationFilePath, Data: err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
dockerRootDir := value.(string)
|
||||
if dockerRootDir == "/" {
|
||||
dockerConfig.Root = "" // omitempty - empty string will not be serialized
|
||||
} else {
|
||||
if !file.Exists(dockerRootDir) {
|
||||
c.JSON(common_err.CLIENT_ERROR, &model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.DIR_NOT_EXISTS), Data: common_err.GetMsg(common_err.DIR_NOT_EXISTS)})
|
||||
c.JSON(http.StatusBadRequest, &model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.DIR_NOT_EXISTS), Data: common_err.GetMsg(common_err.DIR_NOT_EXISTS)})
|
||||
return
|
||||
}
|
||||
daemon.Graph = dockerRootDir
|
||||
byteMode, _ := json.Marshal(daemon)
|
||||
file.WriteToPath(byteMode, "/etc/docker", "daemon.json")
|
||||
|
||||
fmt.Println(command.ExecResultStr("systemctl daemon-reload"))
|
||||
fmt.Println(command.ExecResultStr("systemctl restart docker"))
|
||||
dockerConfig.Root = filepath.Join(dockerRootDir, "docker")
|
||||
|
||||
c.JSON(common_err.SUCCESS, &model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: js})
|
||||
if err := file.IsNotExistMkDir(dockerConfig.Root); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, &model.Result{Success: common_err.SERVICE_ERROR, Message: "error when trying to create " + dockerConfig.Root, Data: err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
byteMode, err := json.Marshal(dockerConfig)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, &model.Result{Success: common_err.CLIENT_ERROR, Message: "error when trying to serialize docker config", Data: dockerConfig})
|
||||
return
|
||||
}
|
||||
|
||||
if err := file.WriteToFullPath(byteMode, dockerDaemonConfigurationFilePath, 0o644); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, &model.Result{Success: common_err.SERVICE_ERROR, Message: "error when trying to write to " + dockerDaemonConfigurationFilePath, Data: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// TODO also write dockerRootDir to /etc/casaos/casaos.conf
|
||||
|
||||
println(command.ExecResultStr("systemctl daemon-reload"))
|
||||
println(command.ExecResultStr("systemctl restart docker"))
|
||||
|
||||
c.JSON(http.StatusOK, &model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: js})
|
||||
}
|
||||
|
Reference in New Issue
Block a user