Commit 37ae654f authored by 陈子龙's avatar 陈子龙

Merge branch 'dev-czl' into dev

parents 9cc03d88 13632684
package request package request
type DocLibGetReq struct {
FileName string `uri:"file_name" binding:"required"` //文档名称
Ext string `uri:"ext" binding:"required"` //后缀格式
Opt string `uri:"opt" binding:"required,oneof=preview download"` //操作
}
...@@ -26,13 +26,14 @@ type HostManage struct { ...@@ -26,13 +26,14 @@ type HostManage struct {
} }
type HostList struct { type HostList struct {
Id int `json:"id"` // id //Id int `json:"id"` // id
Ip string `json:"ip"` // ip Ip string `json:"ip"` // ip
Port string `json:"port"` // 端口 Port string `json:"port"` // 端口
VoucherType int `json:"voucher_type"` // 凭证类型(0密码验证 密钥验证) VoucherType int `json:"voucher_type"` // 凭证类型(0密码验证 密钥验证)
UserName string `json:"user_name"` // 用户名 UserName string `json:"user_name"` // 用户名
Password string `json:"password"` // 密码 Password string `json:"password"` // 密码
HostFileUrl string `json:"-"` // 主机文件url HostFileUrl string `json:"-"` // 主机文件url
Cnt int `json:"cnt""` //主机ip数量
} }
type TaskList struct { type TaskList struct {
......
...@@ -13,6 +13,7 @@ type TaskManageListRes struct { ...@@ -13,6 +13,7 @@ type TaskManageListRes struct {
type TaskManageRes struct { type TaskManageRes struct {
Id int `json:"id"` // id Id int `json:"id"` // id
HostGroupId int `json:"host_group_id"` // 主机分组id
TaskName string `json:"task_name"` // 任务名称 TaskName string `json:"task_name"` // 任务名称
TaskDesc string `json:"task_desc"` // 任务描述 TaskDesc string `json:"task_desc"` // 任务描述
YamlDesc string `json:"yaml_desc"` // yaml文件 YamlDesc string `json:"yaml_desc"` // yaml文件
......
...@@ -2,8 +2,12 @@ package controller ...@@ -2,8 +2,12 @@ package controller
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/service" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/service"
"net/http"
"net/http/httputil"
"net/url"
) )
func AddFile(c *gin.Context) { func AddFile(c *gin.Context) {
...@@ -16,3 +20,32 @@ func AddFile(c *gin.Context) { ...@@ -16,3 +20,32 @@ func AddFile(c *gin.Context) {
} }
SendJsonResponse(c, resp.OK, doc) SendJsonResponse(c, resp.OK, doc)
} }
func DownloadFile(c *gin.Context) {
var req request.DocLibGetReq
if err := c.ShouldBindUri(&req); err != nil {
SendJsonResponse(c, resp.InvalidParam.WithError(err), nil)
return
}
docLibSvc := service.DocLibSvc{}
docUrl, err := docLibSvc.DownloadFile(c, req)
if err != nil {
SendJsonResponse(c, resp.FAIL.WithError(err), nil)
return
}
switch req.Opt {
case "download":
//c.Redirect(http.StatusMovedPermanently, docUrl.String())
docUrlParse, _ := url.Parse(docUrl.String())
proxy := httputil.ReverseProxy{
Director: func(req *http.Request) {
req.Header = c.Request.Header
req.Host = docUrlParse.Host
req.URL = docUrlParse
},
}
proxy.ServeHTTP(c.Writer, c.Request)
case "preview":
c.Redirect(http.StatusMovedPermanently, docUrl.String())
}
}
...@@ -80,8 +80,8 @@ func GetMinioFiles(fileName string) (hostManageList []request.HostManageList, er ...@@ -80,8 +80,8 @@ func GetMinioFiles(fileName string) (hostManageList []request.HostManageList, er
} }
for i := 0; i < len(rows); i++ { for i := 0; i < len(rows); i++ {
//默认跳过第一 //默认跳过前两
if i < 1 { if i < 2 {
continue continue
} }
...@@ -371,11 +371,11 @@ func ExportIp(c *gin.Context) { ...@@ -371,11 +371,11 @@ func ExportIp(c *gin.Context) {
SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("导出类型解析错误")), nil) SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("导出类型解析错误")), nil)
return return
} }
if id == "" { if cast.ToInt(detectionType) == 1 && id == "" {
SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("id为空")), nil) SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("id为空")), nil)
return return
} }
if uuid == "" { if cast.ToInt(detectionType) == 0 && uuid == "" {
SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("uuid为空")), nil) SendJsonResponse(c, resp.InvalidParam.WithError(errors.New("uuid为空")), nil)
return return
} }
......
...@@ -28,7 +28,8 @@ func Load(r *gin.Engine, middleware ...gin.HandlerFunc) { ...@@ -28,7 +28,8 @@ func Load(r *gin.Engine, middleware ...gin.HandlerFunc) {
base.GET("/example/list", controller.GetExampleList) // 示例获取列表 base.GET("/example/list", controller.GetExampleList) // 示例获取列表
} }
base.POST("/add_file", controller.AddFile) //文件上传 base.POST("/add_file", controller.AddFile) //文件上传
base.GET("/download_file/:file_name/:ext/:opt", controller.DownloadFile) //文件下载
// 初始化自动化运维路由 // 初始化自动化运维路由
InitAutomatedMaintenRouter(r) InitAutomatedMaintenRouter(r)
...@@ -46,4 +47,6 @@ func Load(r *gin.Engine, middleware ...gin.HandlerFunc) { ...@@ -46,4 +47,6 @@ func Load(r *gin.Engine, middleware ...gin.HandlerFunc) {
InitMetricConfigRouter(r) InitMetricConfigRouter(r)
// 初始化prometheus路由 // 初始化prometheus路由
InitPrometheusRouter(r) InitPrometheusRouter(r)
// 初始化工单管理路由
//InitAutomatedMaintenRouter(r)
} }
...@@ -7,12 +7,15 @@ import ( ...@@ -7,12 +7,15 @@ import (
"github.com/google/uuid" "github.com/google/uuid"
"github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7"
"github.com/pkg/errors" "github.com/pkg/errors"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client"
"gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf"
"io" "io"
"mime/multipart" "mime/multipart"
"net/http" "net/http"
"net/url"
"path" "path"
"time"
) )
var maxFileByte int64 = 20971520 //20M var maxFileByte int64 = 20971520 //20M
...@@ -111,3 +114,33 @@ func (d DocLibSvc) GetFile(fileName string) (obj io.Reader, err error) { ...@@ -111,3 +114,33 @@ func (d DocLibSvc) GetFile(fileName string) (obj io.Reader, err error) {
// return // return
//} //}
} }
func (d DocLibSvc) DownloadFile(ctx *gin.Context, req request.DocLibGetReq) (docUrl *url.URL, err error) {
var (
minioClient *minio.Client
)
minioClient, err = client.GetMinioConnect()
if err != nil {
err = errors.Wrap(err, "getMinio")
return
}
remotePath := fmt.Sprintf("%s%s", req.FileName, req.Ext)
reqParams := make(url.Values)
switch req.Opt {
case "info":
return
case "preview":
reqParams.Set("response-Content-Type", req.Ext)
case "download":
reqParams.Set("response-Content-Disposition", fmt.Sprintf("attachment; filename=%s%s", req.FileName, req.Ext))
reqParams.Set("response-Content-Type", "application/octet-stream")
reqParams.Set("response-Content-Transfer-Encoding", "binary")
}
docUrl, err = minioClient.PresignedGetObject(ctx, conf.Options.MinioBucket, remotePath, time.Second*60*60, reqParams)
if err != nil {
err = errors.Wrap(err, "get file url from minio fail")
return
}
return
}
...@@ -305,7 +305,9 @@ func (h *HostManageSvc) DetailsHostManage(id int) (hostManageRes response.HostMa ...@@ -305,7 +305,9 @@ func (h *HostManageSvc) DetailsHostManage(id int) (hostManageRes response.HostMa
return return
} }
//查询主机列表 //查询主机列表
err = db.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", id).Find(&hostList) err = db.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", id).
Select("string_agg(ip,',') as ip,port,voucher_type,user_name,password,host_file_url,count(1) as cnt").
GroupBy("ip_group,port,voucher_type,user_name,PASSWORD,host_file_url").Find(&hostList)
if err != nil { if err != nil {
err = resp.DbSelectError.WithError(err) err = resp.DbSelectError.WithError(err)
return return
...@@ -321,18 +323,20 @@ func (h *HostManageSvc) DetailsHostManage(id int) (hostManageRes response.HostMa ...@@ -321,18 +323,20 @@ func (h *HostManageSvc) DetailsHostManage(id int) (hostManageRes response.HostMa
if v.HostFileUrl != "" { if v.HostFileUrl != "" {
hostManageRes.HostFileUrl = v.HostFileUrl hostManageRes.HostFileUrl = v.HostFileUrl
} }
hostManageRes.IpCnt = hostManageRes.IpCnt + v.Cnt
} }
hostManageRes.Id = hostManage.Id hostManageRes.Id = hostManage.Id
hostManageRes.HostName = hostManage.HostName hostManageRes.HostName = hostManage.HostName
hostManageRes.TaskCnt = len(taskList) hostManageRes.TaskCnt = len(taskList)
hostManageRes.IpCnt = len(hostList) //hostManageRes.IpCnt = len(hostList)
hostManageRes.CreateUser = hostManage.CreateUser hostManageRes.CreateUser = hostManage.CreateUser
hostManageRes.CreateTime = hostManage.CreateTime hostManageRes.CreateTime = hostManage.CreateTime
hostManageRes.UpdateUser = hostManage.UpdateUser hostManageRes.UpdateUser = hostManage.UpdateUser
hostManageRes.UpdateTime = hostManage.UpdateTime hostManageRes.UpdateTime = hostManage.UpdateTime
hostManageRes.HostList = hostList if hostManageRes.HostFileUrl == "" {
hostManageRes.HostList = hostList
}
hostManageRes.TaskList = taskList hostManageRes.TaskList = taskList
return return
......
...@@ -117,7 +117,7 @@ func (t *TaskManageSvc) DetailsTaskManage(id int) (taskManageRes response.TaskMa ...@@ -117,7 +117,7 @@ func (t *TaskManageSvc) DetailsTaskManage(id int) (taskManageRes response.TaskMa
//查询任务详情 //查询任务详情
finder := db.Table("task_manage").Alias("tm"). finder := db.Table("task_manage").Alias("tm").
Where("is_delete = 0 AND id = ?", id) Where("is_delete = 0 AND id = ?", id)
_, err = finder.Select("tm.id,tm.task_name,tm.task_desc,tm.yaml_desc,tm.yaml_url,tm.create_user,tm.create_time," + _, err = finder.Select("tm.id,tm.task_name,tm.task_desc,tm.yaml_desc,tm.yaml_url,tm.create_user,tm.create_time,tm.host_group_id," +
"(select count(1) from task_history th where th.task_id = tm.id) as exec_cnt," + "(select count(1) from task_history th where th.task_id = tm.id) as exec_cnt," +
"(select count(1) from task_history th where th.task_id = tm.id and th.state = 1) as success_cnt," + "(select count(1) from task_history th where th.task_id = tm.id and th.state = 1) as success_cnt," +
"(select count(1) from task_history th where th.task_id = tm.id and th.state = 2) as fail_cnt").Get(&taskManageRes) "(select count(1) from task_history th where th.task_id = tm.id and th.state = 2) as fail_cnt").Get(&taskManageRes)
...@@ -128,18 +128,27 @@ func (t *TaskManageSvc) DetailsTaskManage(id int) (taskManageRes response.TaskMa ...@@ -128,18 +128,27 @@ func (t *TaskManageSvc) DetailsTaskManage(id int) (taskManageRes response.TaskMa
//查询主机列表 //查询主机列表
hostList := make([]response.HostList, 0) hostList := make([]response.HostList, 0)
err = db.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", id).Find(&hostList) err = db.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", taskManageRes.HostGroupId).
Select("string_agg(ip,',') as ip,port,voucher_type,user_name,password,host_file_url").
GroupBy("ip_group,port,voucher_type,user_name,PASSWORD,host_file_url").Find(&hostList)
if err != nil { if err != nil {
err = resp.DbSelectError.WithError(err) err = resp.DbSelectError.WithError(err)
return return
} }
//err = db.Table("host_manage_list").Where("is_delete = 0 AND host_group_id = ?", id).Find(&hostList)
//if err != nil {
// err = resp.DbSelectError.WithError(err)
// return
//}
for _, v := range hostList { for _, v := range hostList {
if v.HostFileUrl != "" { if v.HostFileUrl != "" {
taskManageRes.HostFileUrl = v.HostFileUrl taskManageRes.HostFileUrl = v.HostFileUrl
} }
} }
taskManageRes.HostList = hostList if taskManageRes.HostFileUrl == "" {
taskManageRes.HostList = hostList
}
return return
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment