Tiger Wang 2 years ago
parent
commit
cbbb907d6a
1 changed files with 27 additions and 28 deletions
  1. 27 28
      route/v1/app.go

+ 27 - 28
route/v1/app.go

@@ -18,6 +18,7 @@ import (
 )
 )
 
 
 const (
 const (
+	dockerRootDirFilePath             = "/var/lib/casaos/docker_root"
 	dockerDaemonConfigurationFilePath = "/etc/docker/daemon.json"
 	dockerDaemonConfigurationFilePath = "/etc/docker/daemon.json"
 )
 )
 
 
@@ -274,36 +275,27 @@ func GetDockerDaemonConfiguration(c *gin.Context) {
 	// 	c.JSON(common_err.SERVICE_ERROR, &model.Result{Success: common_err.SERVICE_ERROR, Message: common_err.GetMsg(common_err.SERVICE_ERROR), Data: err.Error()})
 	// 	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
 	// 	return
 	// }
 	// }
-	dockerConfig := model.DockerDaemonConfigurationModel{}
-	data := make(map[string]interface{}, 1)
-	data["docker_root_dir"] = ""
+	data := make(map[string]interface{})
 
 
-	// TODO read dockerRootDir from /etc/casaos/casaos.conf
-	if file.Exists(dockerDaemonConfigurationFilePath) {
-		byteResult := file.ReadFullFile(dockerDaemonConfigurationFilePath)
-		err := json.Unmarshal(byteResult, &dockerConfig)
+	if file.Exists(dockerRootDirFilePath) {
+		buf := file.ReadFullFile(dockerRootDirFilePath)
+		err := json.Unmarshal(buf, &data)
 		if err != nil {
 		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(common_err.CLIENT_ERROR, &model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.INVALID_PARAMS), Data: err})
 			return
 			return
 		}
 		}
-
-		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})
 	c.JSON(common_err.SUCCESS, &model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: data})
 }
 }
 
 
 func PutDockerDaemonConfiguration(c *gin.Context) {
 func PutDockerDaemonConfiguration(c *gin.Context) {
-	js := make(map[string]interface{})
-	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()})
+	request := make(map[string]interface{})
+	if err := c.BindJSON(&request); err != nil {
+		c.JSON(http.StatusBadRequest, &model.Result{Success: common_err.CLIENT_ERROR, Message: common_err.GetMsg(common_err.INVALID_PARAMS), Data: err})
 		return
 		return
 	}
 	}
 
 
-	value, ok := js["docker_root_dir"]
+	value, ok := request["docker_root_dir"]
 	if !ok {
 	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"})
 		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
 		return
@@ -314,7 +306,7 @@ func PutDockerDaemonConfiguration(c *gin.Context) {
 		byteResult := file.ReadFullFile(dockerDaemonConfigurationFilePath)
 		byteResult := file.ReadFullFile(dockerDaemonConfigurationFilePath)
 		err := json.Unmarshal(byteResult, &dockerConfig)
 		err := json.Unmarshal(byteResult, &dockerConfig)
 		if err != nil {
 		if err != nil {
-			c.JSON(http.StatusInternalServerError, &model.Result{Success: common_err.SERVICE_ERROR, Message: "error when trying to deserialize " + dockerDaemonConfigurationFilePath, Data: err.Error()})
+			c.JSON(http.StatusInternalServerError, &model.Result{Success: common_err.SERVICE_ERROR, Message: "error when trying to deserialize " + dockerDaemonConfigurationFilePath, Data: err})
 			return
 			return
 		}
 		}
 	}
 	}
@@ -331,26 +323,33 @@ func PutDockerDaemonConfiguration(c *gin.Context) {
 		dockerConfig.Root = filepath.Join(dockerRootDir, "docker")
 		dockerConfig.Root = filepath.Join(dockerRootDir, "docker")
 
 
 		if err := file.IsNotExistMkDir(dockerConfig.Root); err != nil {
 		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()})
+			c.JSON(http.StatusInternalServerError, &model.Result{Success: common_err.SERVICE_ERROR, Message: "error when trying to create " + dockerConfig.Root, Data: err})
 			return
 			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})
+	if buf, err := json.Marshal(request); err != nil {
+		c.JSON(http.StatusBadRequest, &model.Result{Success: common_err.CLIENT_ERROR, Message: "error when trying to serialize docker root json", Data: err})
 		return
 		return
+	} else {
+		if err := file.WriteToFullPath(buf, dockerRootDirFilePath, 0o644); err != nil {
+			c.JSON(http.StatusInternalServerError, &model.Result{Success: common_err.SERVICE_ERROR, Message: "error when trying to write " + dockerRootDirFilePath, Data: err})
+			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()})
+	if buf, err := json.Marshal(dockerConfig); err != nil {
+		c.JSON(http.StatusBadRequest, &model.Result{Success: common_err.CLIENT_ERROR, Message: "error when trying to serialize docker config", Data: dockerConfig})
 		return
 		return
+	} else {
+		if err := file.WriteToFullPath(buf, 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})
+			return
+		}
 	}
 	}
 
 
-	// TODO also write dockerRootDir to /etc/casaos/casaos.conf
-
 	println(command.ExecResultStr("systemctl daemon-reload"))
 	println(command.ExecResultStr("systemctl daemon-reload"))
 	println(command.ExecResultStr("systemctl restart docker"))
 	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})
+	c.JSON(http.StatusOK, &model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS), Data: request})
 }
 }