Skip to content

Commit

Permalink
feat: Podman插件
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jun 10, 2024
1 parent aa22933 commit e27940a
Show file tree
Hide file tree
Showing 9 changed files with 1,125 additions and 1 deletion.
239 changes: 239 additions & 0 deletions app/http/controllers/plugins/podman_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package plugins

import (
"github.com/goravel/framework/contracts/http"

"github.com/TheTNB/panel/app/http/controllers"
requests "github.com/TheTNB/panel/app/http/requests/plugins/podman"
"github.com/TheTNB/panel/pkg/tools"
)

type PodmanController struct {
}

func NewPodmanController() *PodmanController {
return &PodmanController{}
}

// Status
//
// @Summary 服务状态
// @Description 获取 Podman 服务状态
// @Tags 插件-Podman
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/podman/status [get]
func (r *PodmanController) Status(ctx http.Context) http.Response {
status, err := tools.ServiceStatus("podman")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取 Podman 服务运行状态失败")
}

return controllers.Success(ctx, status)
}

// IsEnabled
//
// @Summary 是否启用服务
// @Description 获取是否启用 Podman 服务
// @Tags 插件-Podman
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/podman/isEnabled [get]
func (r *PodmanController) IsEnabled(ctx http.Context) http.Response {
enabled, err := tools.ServiceIsEnabled("podman")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取 Podman 服务启用状态失败")
}

return controllers.Success(ctx, enabled)
}

// Enable
//
// @Summary 启用服务
// @Description 启用 Podman 服务
// @Tags 插件-Podman
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/podman/enable [post]
func (r *PodmanController) Enable(ctx http.Context) http.Response {
if err := tools.ServiceEnable("podman"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "启用 Podman 服务失败")
}

return controllers.Success(ctx, nil)
}

// Disable
//
// @Summary 禁用服务
// @Description 禁用 Podman 服务
// @Tags 插件-Podman
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/podman/disable [post]
func (r *PodmanController) Disable(ctx http.Context) http.Response {
if err := tools.ServiceDisable("podman"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "禁用 Podman 服务失败")
}

return controllers.Success(ctx, nil)
}

// Restart
//
// @Summary 重启服务
// @Description 重启 Podman 服务
// @Tags 插件-Podman
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/podman/restart [post]
func (r *PodmanController) Restart(ctx http.Context) http.Response {
if err := tools.ServiceRestart("podman"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "重启 Podman 服务失败")
}

return controllers.Success(ctx, nil)
}

// Start
//
// @Summary 启动服务
// @Description 启动 Podman 服务
// @Tags 插件-Podman
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/podman/start [post]
func (r *PodmanController) Start(ctx http.Context) http.Response {
if err := tools.ServiceStart("podman"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "启动 Podman 服务失败")
}

status, err := tools.ServiceStatus("podman")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取 Podman 服务运行状态失败")
}

return controllers.Success(ctx, status)
}

// Stop
//
// @Summary 停止服务
// @Description 停止 Podman 服务
// @Tags 插件-Podman
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/podman/stop [post]
func (r *PodmanController) Stop(ctx http.Context) http.Response {
if err := tools.ServiceStop("podman"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "停止 Podman 服务失败")
}

status, err := tools.ServiceStatus("podman")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, "获取 Podman 服务运行状态失败")
}

return controllers.Success(ctx, !status)
}

// GetRegistryConfig
//
// @Summary 获取注册表配置
// @Description 获取 Podman 注册表配置
// @Tags 插件-Podman
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/podman/registryConfig [get]
func (r *PodmanController) GetRegistryConfig(ctx http.Context) http.Response {
config, err := tools.Read("/etc/containers/registries.conf")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

return controllers.Success(ctx, config)
}

// UpdateRegistryConfig
//
// @Summary 更新注册表配置
// @Description 更新 Podman 注册表配置
// @Tags 插件-Podman
// @Produce json
// @Security BearerToken
// @Param data body requests.UpdateRegistryConfig true "request"
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/podman/registryConfig [post]
func (r *PodmanController) UpdateRegistryConfig(ctx http.Context) http.Response {
var updateRequest requests.UpdateRegistryConfig
sanitize := controllers.Sanitize(ctx, &updateRequest)
if sanitize != nil {
return sanitize
}

if err := tools.Write("/etc/containers/registries.conf", updateRequest.Config, 0644); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

if err := tools.ServiceRestart("podman"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

return controllers.Success(ctx, nil)
}

// GetStorageConfig
//
// @Summary 获取存储配置
// @Description 获取 Podman 存储配置
// @Tags 插件-Podman
// @Produce json
// @Security BearerToken
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/podman/storageConfig [get]
func (r *PodmanController) GetStorageConfig(ctx http.Context) http.Response {
config, err := tools.Read("/etc/containers/storage.conf")
if err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

return controllers.Success(ctx, config)
}

// UpdateStorageConfig
//
// @Summary 更新存储配置
// @Description 更新 Podman 存储配置
// @Tags 插件-Podman
// @Produce json
// @Security BearerToken
// @Param data body requests.UpdateStorageConfig true "request"
// @Success 200 {object} controllers.SuccessResponse
// @Router /plugins/podman/storageConfig [post]
func (r *PodmanController) UpdateStorageConfig(ctx http.Context) http.Response {
var updateRequest requests.UpdateStorageConfig
sanitize := controllers.Sanitize(ctx, &updateRequest)
if sanitize != nil {
return sanitize
}

if err := tools.Write("/etc/containers/storage.conf", updateRequest.Config, 0644); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

if err := tools.ServiceRestart("podman"); err != nil {
return controllers.Error(ctx, http.StatusInternalServerError, err.Error())
}

return controllers.Success(ctx, nil)
}
2 changes: 2 additions & 0 deletions app/http/middleware/must_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ func MustInstall() http.Middleware {
var slug string
if strings.HasPrefix(path, "/api/panel/website") {
slug = "openresty"
} else if strings.HasPrefix(path, "/api/panel/container") {
slug = "podman"
} else {
pathArr := strings.Split(path, "/")
if len(pathArr) < 4 {
Expand Down
32 changes: 32 additions & 0 deletions app/http/requests/plugins/podman/update_registry_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
)

type UpdateRegistryConfig struct {
Config string `form:"config" json:"config"`
}

func (r *UpdateRegistryConfig) Authorize(ctx http.Context) error {
return nil
}

func (r *UpdateRegistryConfig) Rules(ctx http.Context) map[string]string {
return map[string]string{
"config": "required|string",
}
}

func (r *UpdateRegistryConfig) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *UpdateRegistryConfig) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *UpdateRegistryConfig) PrepareForValidation(ctx http.Context, data validation.Data) error {
return nil
}
32 changes: 32 additions & 0 deletions app/http/requests/plugins/podman/update_storage_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package requests

import (
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/contracts/validation"
)

type UpdateStorageConfig struct {
Config string `form:"config" json:"config"`
}

func (r *UpdateStorageConfig) Authorize(ctx http.Context) error {
return nil
}

func (r *UpdateStorageConfig) Rules(ctx http.Context) map[string]string {
return map[string]string{
"config": "required|string",
}
}

func (r *UpdateStorageConfig) Messages(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *UpdateStorageConfig) Attributes(ctx http.Context) map[string]string {
return map[string]string{}
}

func (r *UpdateStorageConfig) PrepareForValidation(ctx http.Context, data validation.Data) error {
return nil
}
Loading

0 comments on commit e27940a

Please sign in to comment.