Files
houston/client/operation.go

548 lines
12 KiB
Go
Raw Permalink Normal View History

2023-05-21 23:37:54 +09:00
package client
import (
"archive/zip"
2023-11-13 16:43:56 +09:00
"bufio"
2023-05-21 23:37:54 +09:00
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path"
"path/filepath"
2024-08-05 10:20:41 +09:00
"regexp"
"slices"
2024-08-05 10:20:41 +09:00
"strconv"
"strings"
2023-05-21 23:37:54 +09:00
"syscall"
"time"
2023-05-22 02:13:03 +09:00
2024-08-05 10:20:41 +09:00
"github.com/Knetic/govaluate"
2023-06-14 00:13:51 +09:00
"repositories.action2quare.com/ayo/gocommon/logger"
2024-11-14 19:46:47 +09:00
"repositories.action2quare.com/ayo/gocommon/metric"
2023-05-22 02:13:03 +09:00
"repositories.action2quare.com/ayo/houston/shared"
"repositories.action2quare.com/ayo/houston/shared/protos"
2023-05-21 23:37:54 +09:00
)
func lastExecutionArgs(verpath string) []string {
argf, err := os.Open(path.Join(verpath, "@args"))
if os.IsNotExist(err) {
argf, err = os.Open(path.Clean(path.Join(verpath, "..", "@args")))
if os.IsNotExist(err) {
return nil
}
}
defer argf.Close()
var out []string
dec := json.NewDecoder(argf)
dec.Decode(&out)
return out
}
func (hc *houstonClient) uploadToAppendLog(logFile string, name string, version string) {
hc.uploadChan <- uploadRequest{
logFile: logFile,
name: name,
version: version,
}
}
2024-09-26 12:01:53 +09:00
func findMatchFiles(storageRoot, name, version, filter string) (string, []string) {
root := path.Join(storageRoot, name, version)
matches, err := filepath.Glob(path.Join(root, filter))
2023-05-23 10:57:24 +09:00
if err != nil {
2024-09-26 12:01:53 +09:00
return "", nil
2023-05-23 10:57:24 +09:00
}
if len(matches) == 0 {
2024-09-26 12:01:53 +09:00
return "", nil
2023-05-23 10:57:24 +09:00
}
2024-09-26 12:01:53 +09:00
root = path.Join(root, path.Dir(filter))
out := make([]string, 0, len(matches))
for _, file := range matches {
2023-05-28 21:21:37 +09:00
file = filepath.ToSlash(file)
2024-09-26 12:01:53 +09:00
if file == root {
continue
}
2023-05-28 21:21:37 +09:00
2024-09-26 12:01:53 +09:00
out = append(out, file)
}
slices.Sort(out)
2024-09-26 12:01:53 +09:00
return root, out
}
2024-09-26 12:01:53 +09:00
func zipCompressFiles(root string, matches []string) (string, error) {
f, err := os.CreateTemp(os.TempDir(), "*.zip")
2023-05-23 10:57:24 +09:00
if err != nil {
2024-09-26 12:01:53 +09:00
return "", err
2023-05-21 23:37:54 +09:00
}
2023-05-23 10:57:24 +09:00
defer f.Close()
2023-05-21 23:37:54 +09:00
2023-05-23 10:57:24 +09:00
w := zip.NewWriter(f)
defer w.Close()
oldestFile := ""
for i, file := range matches {
if fi, err := os.Lstat(file); err == nil {
if (fi.Mode() & os.ModeSymlink) == os.ModeSymlink {
matches[i] = ""
continue
}
2023-05-23 10:57:24 +09:00
}
2023-05-23 10:57:24 +09:00
if len(oldestFile) == 0 {
oldestFile = path.Base(filepath.ToSlash(file))
2023-05-21 23:37:54 +09:00
}
2023-05-23 10:57:24 +09:00
relative := file[len(root)+1:]
fw, err := w.Create(relative)
if err != nil {
2024-09-26 12:01:53 +09:00
return "", err
2023-05-21 23:37:54 +09:00
}
2023-05-23 10:57:24 +09:00
src, err := os.Open(file)
if err != nil {
2024-09-26 12:01:53 +09:00
return "", err
2023-05-21 23:37:54 +09:00
}
2023-05-23 10:57:24 +09:00
defer src.Close()
2023-05-21 23:37:54 +09:00
2023-05-23 10:57:24 +09:00
if _, err = io.Copy(fw, src); err != nil {
2024-09-26 12:01:53 +09:00
return "", err
2023-05-21 23:37:54 +09:00
}
}
2024-09-26 12:01:53 +09:00
return f.Name(), nil
2023-05-21 23:37:54 +09:00
}
2024-06-05 13:40:10 +09:00
func prepareProcessLaunch(storageRoot string, req *shared.StartProcessRequest) (*procmeta, error) {
2023-06-27 09:44:56 +09:00
if len(req.Args) == 0 {
2024-06-05 13:40:10 +09:00
return nil, errors.New("args is empty")
}
2024-06-05 13:40:10 +09:00
foundVersion := req.Version
2024-06-05 13:40:10 +09:00
if req.Version == "latest" {
entries, err := os.ReadDir(path.Join(storageRoot, req.Name))
if err != nil {
return nil, err
}
var latestTimestamp time.Time
var latestVersion string
for _, entry := range entries {
if !entry.IsDir() {
continue
}
fi, err := entry.Info()
if err != nil {
return nil, err
}
createTime := fi.ModTime()
if latestTimestamp.Before(createTime) {
latestTimestamp = fi.ModTime()
latestVersion = fi.Name()
}
}
if len(latestVersion) > 0 {
foundVersion = latestVersion
2024-06-05 13:40:10 +09:00
}
}
verpath := path.Join(storageRoot, req.Name, foundVersion)
2023-05-23 10:57:24 +09:00
fi, err := os.Stat(verpath)
2024-06-05 13:40:10 +09:00
if err != nil {
return nil, err
}
2023-05-23 10:57:24 +09:00
2024-06-05 13:40:10 +09:00
if fi.IsDir() {
2023-11-08 09:21:09 +09:00
exefile := "./" + path.Clean(strings.TrimPrefix(req.Args[0], "/"))
os.Chmod(path.Join(verpath, exefile), 0777)
2023-11-08 09:21:09 +09:00
exef, _ := os.Executable()
2023-11-29 18:46:10 +09:00
expanded := make([]string, len(req.Args))
for i, arg := range req.Args {
expanded[i] = os.ExpandEnv(arg)
}
2024-06-05 13:40:10 +09:00
exename := path.Join(path.Dir(strings.ReplaceAll(exef, "\\", "/")), verpath, exefile)
2023-11-29 18:46:10 +09:00
logger.Println("exefile :", exefile)
logger.Println("verpath :", verpath)
logger.Println("exef :", exef)
logger.Println("path.Dir :", path.Dir(exef))
logger.Println("exename :", exename)
2023-11-29 18:46:10 +09:00
cmd := exec.Command(os.ExpandEnv(exename), expanded[1:]...)
2023-11-24 00:19:17 +09:00
2024-02-13 15:12:18 +09:00
cmd.Dir = verpath
2023-05-23 10:57:24 +09:00
stdin, _ := cmd.StdinPipe()
2023-11-24 00:19:17 +09:00
seq++
2023-05-23 10:57:24 +09:00
return &procmeta{
id: seq,
cmd: cmd,
name: req.Name,
args: req.Args,
version: foundVersion,
recover: req.AutoRestart,
verpath: verpath,
state: int32(protos.ProcessState_Stopped),
stdin: stdin,
logfile: req.OutputLogFile,
keepLatest: req.Version == "latest",
2024-06-05 13:40:10 +09:00
}, nil
2023-05-21 23:37:54 +09:00
}
2024-06-05 13:40:10 +09:00
return nil, errors.New("not found")
2023-05-21 23:37:54 +09:00
}
2024-08-05 10:20:41 +09:00
func evaluateArgs(args []string, params map[string]any) ([]string, error) {
re := regexp.MustCompile(`\$\(\((.*?)\)\)`)
for i, input := range args {
matches := re.FindAllStringSubmatch(input, -1)
if len(matches) == 0 {
continue
}
for _, match := range matches {
if len(match) > 1 {
expression := strings.TrimSpace(match[1])
expr, err := govaluate.NewEvaluableExpression(expression)
if err != nil {
return nil, err
}
result, err := expr.Evaluate(params)
if err != nil {
return nil, err
}
// 원래 표현식을 결과로 대체
input = strings.Replace(input, match[0], fmt.Sprintf("%v", result), -1)
}
}
args[i] = input
}
return args, nil
}
func parseEnv(input []string) map[string]any {
output := make(map[string]any, len(input))
for _, envkv := range input {
kv := strings.SplitN(envkv, "=", 2)
parsed, err := strconv.ParseInt(kv[1], 10, 0)
if err == nil {
output[kv[0]] = parsed
} else {
parsed, err := strconv.ParseFloat(kv[1], 32)
if err == nil {
output[kv[0]] = parsed
} else {
output[kv[0]] = kv[1]
}
}
}
return output
}
2023-05-23 10:57:24 +09:00
func (hc *houstonClient) launch(meta *procmeta) error {
stdout, err := meta.cmd.StdoutPipe()
2023-05-21 23:37:54 +09:00
if err != nil {
return err
}
2024-09-26 12:01:53 +09:00
stderr, err := meta.cmd.StderrPipe()
if err != nil {
return err
}
2023-05-21 23:37:54 +09:00
2024-09-26 12:01:53 +09:00
logfolder := path.Join(meta.verpath, "logs")
err = os.MkdirAll(logfolder, 0775)
2023-05-21 23:37:54 +09:00
if err != nil {
return err
}
2024-11-14 10:48:30 +09:00
logUploader := func(localctx context.Context, logfilePath string, logChan chan []byte) {
var logFile *os.File
var logFilePath string
2024-09-26 12:01:53 +09:00
ext := path.Ext(logfilePath)
head := logfilePath[:len(logfilePath)-len(ext)]
if len(head) > 0 && !strings.HasSuffix(head, "/") {
head += "."
}
2024-11-14 10:48:30 +09:00
writeLog := func(log []byte) {
if logFile == nil {
logFilePath = head + time.Now().UTC().Format("2006-01-02.150405") + ext
logFile, _ = os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
}
for written := 0; written < len(log); {
n, err := logFile.Write(log[written:])
if err != nil {
logger.Println("write log file failed :", logfilePath, err)
break
} else {
written += n
}
}
}
2024-09-26 12:01:53 +09:00
2024-11-14 10:48:30 +09:00
defer func() {
if logFile != nil {
logFile.Close()
logFile = nil
2024-11-14 10:48:30 +09:00
hc.uploadToAppendLog(logFilePath, meta.name, meta.version)
}
2024-11-14 10:48:30 +09:00
}()
2024-11-07 13:47:53 +09:00
defer func() {
2024-11-14 10:48:30 +09:00
for {
select {
case log := <-logChan:
writeLog(log)
default:
// logChan에 있는 모든 로그 소비
return
}
}
2024-11-07 13:47:53 +09:00
}()
2023-05-21 23:37:54 +09:00
for {
2024-11-14 10:48:30 +09:00
heartbeat := time.After(time.Minute)
select {
case <-localctx.Done():
return
case <-heartbeat:
heartbeat = time.After(time.Minute)
// 지금까지의 로그를 저장해서 업로드
if logFile != nil {
logFile.Close()
logFile = nil
hc.uploadToAppendLog(logFilePath, meta.name, meta.version)
2024-09-26 12:01:53 +09:00
}
2024-11-14 10:48:30 +09:00
case log := <-logChan:
writeLog(log)
2023-11-16 19:47:30 +09:00
}
2024-09-26 12:01:53 +09:00
}
}
2023-11-16 19:47:30 +09:00
2024-11-14 19:46:47 +09:00
stdReader := func(r io.ReadCloser, logfilePath string, verify func(buff []byte) bool) {
2024-09-26 12:01:53 +09:00
defer func() {
reco := recover()
if reco != nil {
logger.Println(reco)
}
2024-11-14 10:48:30 +09:00
r.Close()
2024-09-26 12:01:53 +09:00
}()
2024-11-14 10:48:30 +09:00
localctx, cancel := context.WithCancel(context.Background())
defer cancel()
2024-09-26 12:01:53 +09:00
2024-11-14 10:48:30 +09:00
logChan := make(chan []byte, 1)
go logUploader(localctx, logfilePath, logChan)
2024-11-14 10:48:30 +09:00
reader := bufio.NewReader(r)
2024-09-26 12:01:53 +09:00
for {
2024-11-14 10:48:30 +09:00
buff, err := reader.ReadBytes('\n')
if err != nil {
2024-09-26 12:01:53 +09:00
logger.Println("ReadBytes at stdReader return err :", err, meta.name)
break
}
2024-11-14 19:46:47 +09:00
if verify(buff) {
if len(buff) > 0 {
logChan <- buff
}
2024-09-26 12:01:53 +09:00
}
2023-05-21 23:37:54 +09:00
}
}
2024-11-14 10:48:30 +09:00
var evalfile string
if len(meta.logfile) > 0 {
evalfile = path.Join(logfolder, meta.logfile)
2024-09-26 12:01:53 +09:00
} else {
evalfile = logfolder + "/"
2024-08-22 11:56:50 +09:00
}
2024-11-14 10:48:30 +09:00
go func() {
2024-11-14 19:46:47 +09:00
metricExporter := metric.NewPrometheusExport(hc.config.MetricNamespace)
defer metricExporter.Shutdown()
var metricBuffer []byte
readingMetric := false
stdReader(stdout, evalfile+".log", func(buff []byte) bool {
if readingMetric {
metricBuffer = append(metricBuffer, buff...)
} else if buff[0] == metric.METRIC_HEAD_INLINE {
readingMetric = true
metricBuffer = append(metricBuffer, buff[1:]...)
}
if readingMetric {
if metricBuffer[len(metricBuffer)-2] == metric.METRIC_TAIL_INLINE {
readingMetric = false
metricBuffer = metricBuffer[:len(metricBuffer)-2]
if metricBuffer[0] == '{' {
var desc metric.MetricDescription
if err := json.Unmarshal(metricBuffer, &desc); err != nil {
logger.Println("unmarshal metric failed :", err, string(metricBuffer))
return false
}
if desc.ConstLabels == nil {
desc.ConstLabels = make(map[string]string)
}
for k, v := range hc.config.ConstLabels {
desc.ConstLabels[k] = v
}
desc.ConstLabels["job"] = meta.name
metricExporter.RegisterMetric(&desc)
} else {
key, val := metric.ReadMetricValue(metricBuffer)
metricExporter.UpdateMetric(key, val)
}
metricBuffer = metricBuffer[:0]
}
return false
}
return true
})
2024-11-14 10:48:30 +09:00
logger.Println("stdReader is terminated :", meta.name)
if meta.isState(protos.ProcessState_Running) {
// state는 running인데 종료됐으면 exception처리
hc.operationChan <- &protos.OperationQueryResponse{
Operation: string(shared.Exception),
Args: map[string]string{
"id": fmt.Sprintf("%d", meta.id),
},
}
}
}()
2024-11-14 19:46:47 +09:00
go stdReader(stderr, evalfile+".err", func([]byte) bool { return true })
2024-09-26 12:01:53 +09:00
2024-08-05 10:20:41 +09:00
logger.Println("startChildProcess :", meta.cmd.Args)
2023-05-23 10:57:24 +09:00
err = meta.cmd.Start()
if err == nil {
2023-11-24 00:19:17 +09:00
meta.setState(protos.ProcessState_Running)
2023-05-23 10:57:24 +09:00
}
2023-11-24 00:19:17 +09:00
2023-05-23 10:57:24 +09:00
return err
2023-05-21 23:37:54 +09:00
}
func (hc *houstonClient) startChildProcess(req *shared.StartProcessRequest) error {
2024-06-05 13:40:10 +09:00
meta, err := prepareProcessLaunch(hc.config.StorageRoot, req)
if err != nil {
return err
}
2024-06-05 13:40:10 +09:00
2023-05-23 10:57:24 +09:00
if err := hc.launch(meta); err != nil {
return err
2023-05-21 23:37:54 +09:00
}
2023-05-23 10:57:24 +09:00
// launch가 성공하면 args 저장. this and parent folder
vers := hc.deploys[req.Name]
for _, ver := range vers {
if ver.Version == req.Version {
2023-11-13 16:43:56 +09:00
ver.Args = meta.args
}
}
2023-06-14 14:16:47 +09:00
if argfile, err := os.Create(path.Join(hc.config.StorageRoot, req.Name, "@args")); err == nil {
2023-05-23 10:57:24 +09:00
enc := json.NewEncoder(argfile)
2023-11-08 09:21:09 +09:00
enc.Encode(req.Args)
2023-05-23 10:57:24 +09:00
argfile.Close()
}
2023-06-14 14:16:47 +09:00
if argfile, err := os.Create(path.Join(hc.config.StorageRoot, req.Name, req.Version, "@args")); err == nil {
2023-05-23 10:57:24 +09:00
enc := json.NewEncoder(argfile)
2023-11-08 09:21:09 +09:00
enc.Encode(req.Args)
2023-05-23 10:57:24 +09:00
argfile.Close()
}
2023-05-21 23:37:54 +09:00
2023-05-23 10:57:24 +09:00
hc.childProcs = append(hc.childProcs, meta)
return nil
2023-05-21 23:37:54 +09:00
}
2023-06-14 01:50:40 +09:00
func (hc *houstonClient) stopChildProcess(req *shared.StopProcessRequest, op protos.OperationClient) error {
killer := func(proc *procmeta) {
proc.setState(protos.ProcessState_Stopping)
if err := proc.cmd.Process.Signal(syscall.SIGTERM); err != nil {
proc.cmd.Process.Signal(os.Kill)
}
go func() {
proc.cmd.Wait()
hc.operationChan <- &protos.OperationQueryResponse{
Operation: string(shared.Exception),
Args: map[string]string{
"id": fmt.Sprintf("%d", proc.id),
},
}
}()
}
2023-05-21 23:37:54 +09:00
for _, proc := range hc.childProcs {
2023-11-24 00:19:17 +09:00
if !proc.isState(protos.ProcessState_Running) {
2023-05-21 23:37:54 +09:00
continue
}
if req.Pid != 0 {
if req.Pid == int32(proc.cmd.Process.Pid) {
// 해당 pid만 제거
killer(proc)
2023-05-21 23:37:54 +09:00
}
} else if proc.name == req.Name {
if len(req.Version) == 0 {
// program 다 정지
killer(proc)
2023-05-21 23:37:54 +09:00
} else if req.Version == proc.version {
// program의 특정 버전만 정지
killer(proc)
2023-05-21 23:37:54 +09:00
}
}
}
op.Refresh(context.Background(), hc.makeOperationQueryRequest())
2023-05-21 23:37:54 +09:00
return nil
2023-05-21 23:37:54 +09:00
}
2023-06-14 01:50:40 +09:00
func (hc *houstonClient) restartChildProcess(req *shared.RestartProcessRequest, op protos.OperationClient) error {
2023-05-21 23:37:54 +09:00
for _, proc := range hc.childProcs {
2023-06-26 11:26:57 +09:00
if proc.cmd.Process.Pid == int(req.Pid) {
if len(req.Config) > 0 {
// config.json를 먼저 다운로드 시도
root := proc.verpath
2023-10-24 20:08:48 +09:00
if _, err := download(root, hc.makeDownloadUrl(req.Config), "", nil); err != nil {
2023-06-26 11:26:57 +09:00
return err
}
2023-05-21 23:37:54 +09:00
}
2023-11-24 00:19:17 +09:00
proc.setState(protos.ProcessState_Restart)
2023-06-26 11:26:57 +09:00
op.Refresh(context.Background(), hc.makeOperationQueryRequest())
2023-06-27 11:17:16 +09:00
hc.exitChan <- proc.cmd
2023-05-21 23:37:54 +09:00
2023-06-26 11:26:57 +09:00
break
2023-05-21 23:37:54 +09:00
}
}
2023-06-26 11:26:57 +09:00
2023-05-21 23:37:54 +09:00
return nil
}