실행중이지 않은 로그도 업로드

This commit is contained in:
2023-05-26 17:51:28 +09:00
parent 10555ba61a
commit 49921d44ce

View File

@ -39,7 +39,42 @@ func lastExecutionArgs(verpath string) []string {
return out
}
func (meta *procmeta) zipLogFiles(req *shared.UploadRequest, start, except string) (string, []string, error) {
var errUploadZipLogFailed = errors.New("not ok")
func (hc *houstonClient) uploadZipLogFile(zipFile string, name string, version string) error {
zf, err := os.Open(zipFile)
if err != nil {
return err
}
if zf == nil {
return errUploadZipLogFailed
}
defer zf.Close()
req, err := http.NewRequest("POST", hc.httpAddr+"/upload", zf)
if err != nil {
logger.Error(err)
}
req.Header.Set("Houston-Service-Name", name)
req.Header.Set("Houston-Service-Version", version)
req.Header.Set("Houston-Service-Filename", path.Base(zipFile))
req.Header.Set("Content-Type", "application/zip")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errUploadZipLogFailed
}
return nil
}
func zipLogFiles(req *shared.UploadRequest, start, except string) (string, []string, error) {
root := path.Join(req.Name, req.Version)
matches, err := filepath.Glob(path.Join(root, req.Filter))
if err != nil {
@ -51,8 +86,9 @@ func (meta *procmeta) zipLogFiles(req *shared.UploadRequest, start, except strin
}
root = path.Join(root, path.Dir(req.Filter))
// Create a file to write the archive to.
f, err := os.CreateTemp("", "")
zipFileName := path.Join(os.TempDir(), path.Base(matches[0]), ".zip")
f, err := os.OpenFile(zipFileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
return "", nil, err
}
@ -68,7 +104,7 @@ func (meta *procmeta) zipLogFiles(req *shared.UploadRequest, start, except strin
if file == root {
continue
}
if file >= except {
if len(except) > 0 && file >= except {
break
}
if len(start) > 0 && file < start {
@ -213,28 +249,17 @@ func (hc *houstonClient) launch(meta *procmeta) error {
startFile := uploadStartFile
uploadStartFile = nextFile
go func(startFile, nextFile string) {
zipFile, srcFiles, err := meta.zipLogFiles(req, startFile, nextFile)
zipFile, srcFiles, err := zipLogFiles(req, startFile, nextFile)
if err == nil && len(zipFile) > 0 && len(srcFiles) > 0 {
zf, _ := os.Open(zipFile)
if zf != nil {
req, err := http.NewRequest("POST", hc.httpAddr+"/upload", zf)
if err != nil {
logger.Error(err)
}
req.Header.Set("Houston-Service-Name", meta.name)
req.Header.Set("Houston-Service-Version", meta.version)
req.Header.Set("Houston-Service-Filename", path.Base(srcFiles[0])+".zip")
req.Header.Set("Content-Type", "application/zip")
resp, err := http.DefaultClient.Do(req)
if err == nil {
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
if err = hc.uploadZipLogFile(zipFile, meta.name, meta.version); err == nil {
for _, oldf := range srcFiles {
os.Remove(oldf)
}
} else {
logger.Println("uploadZipLogFile failed :", err)
}
}
}
} else if err != nil {
logger.Println("zipLogFiles failed :", err)
}
}(startFile, nextFile)
@ -456,14 +481,29 @@ func (hc *houstonClient) uploadFiles(req *shared.UploadRequest) error {
req.Version = latest
}
logger.Println("uploadFiles req :", *req)
for _, child := range hc.childProcs {
if child.version == req.Version && child.name == req.Name {
logger.Println("uploadFiles found :", child.version, child.name)
child.logUploadChan <- req
break
return nil
}
}
// TODO : 실행 중이 아닌 폴더에서도 대상을 찾는다
// deploys
// 실행 중이 아닌 폴더에서도 대상을 찾는다
// 전체 파일을 대상으로
zipFile, srcFiles, err := zipLogFiles(req, "", "")
if err == nil && len(zipFile) > 0 && len(srcFiles) > 0 {
if err = hc.uploadZipLogFile(zipFile, req.Name, req.Version); err == nil {
for _, oldf := range srcFiles {
os.Remove(oldf)
}
} else {
logger.Println("uploadZipLogFile failed :", err)
}
} else if err != nil {
logger.Println("zipLogFiles failed :", err)
}
return nil
}