提交 a73c0964 编写于 作者: aaronchen2k2k's avatar aaronchen2k2k

agent features

上级 7fd93152
VERSION=2.0
PROJECT=ztf
QINIU_DIR=/Users/aaron/work/zentao/qiniu/
QINIU_DIST_DIR=${QINIU_DIR}${PROJECT}/${VERSION}/
PACKAGE=${PROJECT}-${VERSION}
BINARY=zd
BIN_DIR=bin
BIN_ZIP_DIR=${BIN_DIR}/zip/${PROJECT}/${VERSION}/
BIN_OUT=${BIN_DIR}/${PROJECT}/${VERSION}/
BIN_WIN64=${BIN_OUT}win64/zd/
BIN_WIN32=${BIN_OUT}win32/zd/
BIN_LINUX=${BIN_OUT}linux/zd/
BIN_MAC=${BIN_OUT}mac/zd/
default: gen_version_file upload_to
gen_version_file:
@echo 'gen version'
@mkdir -p ${QINIU_DIR}/${PROJECT}/
@echo ${VERSION} > ${QINIU_DIR}/${PROJECT}/version.txt
upload_to:
@echo 'upload...'
@find ${QINIU_DIR} -name ".DS_Store" -type f -delete
@qshell qupload2 --src-dir=${QINIU_DIR} --bucket=download --thread-count=10 --log-file=qshell.log --rescan-local --overwrite
......@@ -14,10 +14,13 @@ import (
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
)
type Server struct {
commonService *service.CommonService
configService *service.ConfigService
agentService *service.AgentService
buildService *service.BuildService
taskService *service.TaskService
......@@ -26,6 +29,7 @@ type Server struct {
func NewServer() *Server {
commonService := service.NewCommonService()
configService := service.NewConfigService()
agentService := service.NewAgentService()
heartBeatService := service.NewHeartBeatService()
......@@ -36,7 +40,7 @@ func NewServer() *Server {
cronService := cron.NewCronService(heartBeatService, buildService, taskService, execService)
cronService.Init()
return &Server{commonService: commonService, agentService: agentService,
return &Server{commonService: commonService, configService: configService, agentService: agentService,
buildService: buildService, taskService: taskService,
cronService: cronService}
}
......@@ -76,7 +80,7 @@ func (s *Server) handle(writer http.ResponseWriter, req *http.Request) {
serverUtils.SetupCORS(&writer, req)
if req.Method == "GET" {
resp, err = s.get(req)
resp, err = s.get(writer, req)
if err != nil {
serverUtils.OutputErr(err, writer)
return
......@@ -94,9 +98,9 @@ func (s *Server) handle(writer http.ResponseWriter, req *http.Request) {
io.WriteString(writer, string(bytes))
}
func (s *Server) get(req *http.Request) (resp domain.RespData, err error) {
func (s *Server) get(writer http.ResponseWriter, req *http.Request) (resp domain.RespData, err error) {
resp = domain.RespData{Code: 1, Msg: "success"}
method, _ := serverUtils.ParserGetParams(req)
method, params := serverUtils.ParserGetParams(req)
switch method {
......@@ -104,7 +108,10 @@ func (s *Server) get(req *http.Request) (resp domain.RespData, err error) {
resp.Data = s.taskService.ListTask()
case "listHistory":
resp.Msg = "listHistory"
resp.Data = s.taskService.ListHistory()
case "down":
Download(writer, params["f"])
case "":
resp.Code = 0
......@@ -141,6 +148,9 @@ func (s *Server) post(req *http.Request) (resp domain.RespData, err error) {
case "addTask":
s.buildService.Add(reqData)
case "config":
s.configService.Update(reqData)
default:
resp.Code = 0
resp.Msg = "API NOT FOUND"
......@@ -152,3 +162,23 @@ func (s *Server) post(req *http.Request) (resp domain.RespData, err error) {
return
}
func Download(w http.ResponseWriter, fi string) {
logDir := vari.ZTFDir + "log-agent" + constant.PthSep
file, _ := os.Open(logDir + fi)
defer file.Close()
fileHeader := make([]byte, 512)
file.Read(fileHeader)
fileStat, _ := file.Stat()
w.Header().Set("Content-Disposition", "attachment; filename="+fi)
w.Header().Set("Content-Type", http.DetectContentType(fileHeader))
w.Header().Set("Content-Length", strconv.FormatInt(fileStat.Size(), 10))
file.Seek(0, 0)
io.Copy(w, file)
return
}
package service
import (
"encoding/json"
"fmt"
"github.com/easysoft/zentaoatf/src/model"
"github.com/easysoft/zentaoatf/src/server/domain"
configUtils "github.com/easysoft/zentaoatf/src/utils/config"
logUtils "github.com/easysoft/zentaoatf/src/utils/log"
"github.com/easysoft/zentaoatf/src/utils/vari"
)
var ()
type ConfigService struct {
}
func NewConfigService() *ConfigService {
return &ConfigService{}
}
func (s *ConfigService) Update(req domain.ReqData) {
conf := model.Config{}
reqStr, _ := json.Marshal(req.Data)
err := json.Unmarshal(reqStr, &conf)
if err != nil {
logUtils.PrintTo(fmt.Sprintf("error: %v", err))
return
}
if conf.Version != 0 {
vari.Config.Version = conf.Version
}
if conf.Language != "" {
vari.Config.Language = conf.Language
}
if conf.Url != "" {
vari.Config.Url = conf.Url
}
if conf.Account != "" {
vari.Config.Account = conf.Account
}
if conf.Password != "" {
vari.Config.Password = conf.Password
}
if conf.Javascript != "" {
vari.Config.Javascript = conf.Javascript
}
if conf.Lua != "" {
vari.Config.Lua = conf.Lua
}
if conf.Perl != "" {
vari.Config.Perl = conf.Perl
}
if conf.Php != "" {
vari.Config.Php = conf.Php
}
if conf.Python != "" {
vari.Config.Python = conf.Python
}
if conf.Ruby != "" {
vari.Config.Ruby = conf.Ruby
}
if conf.Tcl != "" {
vari.Config.Tcl = conf.Tcl
}
if conf.Lua != "" {
vari.Config.Autoit = conf.Autoit
}
configUtils.SaveConfig(vari.Config)
}
package service
import (
"fmt"
"github.com/easysoft/zentaoatf/src/server/domain"
serverUtils "github.com/easysoft/zentaoatf/src/server/utils/common"
serverConst "github.com/easysoft/zentaoatf/src/server/utils/const"
"github.com/easysoft/zentaoatf/src/utils/vari"
"strconv"
"time"
)
......@@ -62,3 +66,13 @@ func (s *TaskService) ListTask() (data []domain.Build) {
data = tasks
return
}
func (s *TaskService) ListHistory() (data []map[string]string) {
data = serverUtils.ListHistoryLog()
for key, item := range data {
data[key]["url"] = fmt.Sprintf("http://%s:%s/down?f=%s", vari.IP, strconv.Itoa(vari.Port), item["name"])
}
return
}
......@@ -8,6 +8,7 @@ import (
logUtils "github.com/easysoft/zentaoatf/src/utils/log"
"github.com/easysoft/zentaoatf/src/utils/vari"
"io/ioutil"
"path"
"time"
)
......@@ -15,7 +16,7 @@ func BakLog(src string) {
now := time.Now()
dateStr := dateUtils.DateStr(now)
timeStr := dateUtils.TimeStr(now)
logDir := vari.ServerWorkDir + "log-agent" + constant.PthSep
logDir := vari.ZTFDir + "log-agent" + constant.PthSep
dateDir := logDir + dateStr + constant.PthSep
dist := dateDir + timeStr + ".zip"
......@@ -42,3 +43,26 @@ func removeHistoryLog(dir string) {
}
}
}
func ListHistoryLog() (ret []map[string]string) {
logDir := vari.ServerWorkDir + "log-agent" + constant.PthSep
dirs, _ := ioutil.ReadDir(logDir)
for _, dir := range dirs {
dirName := dir.Name()
files, _ := ioutil.ReadDir(logDir + dirName)
for _, fi := range files {
name := fi.Name()
if path.Ext(name) != ".zip" {
continue
}
item := map[string]string{"name": dirName + constant.PthSep + name}
ret = append(ret, item)
}
}
return
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册