houston 자체 업데이트 기능 추가 - replacer

This commit is contained in:
2023-06-09 16:16:26 +09:00
parent ae783aabaf
commit 5326e26a8c
5 changed files with 227 additions and 17 deletions

View File

@ -4,15 +4,18 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"os/signal"
"path"
"path/filepath"
"reflect"
"sort"
"strconv"
"sync"
"sync/atomic"
"syscall"
"unsafe"
@ -93,6 +96,7 @@ type houstonClient struct {
exitChan chan *exec.Cmd
httpAddr string
timestamp string
wg sync.WaitGroup
}
func bToMb(b uint64) uint32 {
@ -218,7 +222,9 @@ func NewClient() (HoustonClient, error) {
}
ctx, cancel := context.WithCancel(context.Background())
hc.wg.Add(1)
go func() {
defer hc.wg.Done()
// regularly send status
sc := protos.NewMonitorClient(client)
hn, _ := os.Hostname()
@ -250,9 +256,21 @@ func NewClient() (HoustonClient, error) {
exitChan := make(chan *exec.Cmd, 10)
operationChan := make(chan *protos.OperationQueryResponse, 10)
hc.wg.Add(1)
go func() {
defer hc.wg.Done()
// 메인 operator
op := protos.NewOperationClient(hc.client)
myname, _ := os.Executable()
myname = path.Base(filepath.ToSlash(myname))
if len(path.Ext(myname)) > 0 {
myname = myname[:len(myname)-len(path.Ext(myname))]
}
if myname == "__debug_bin" {
myname = "houston"
}
for {
select {
@ -276,13 +294,33 @@ func NewClient() (HoustonClient, error) {
case shared.Deploy:
var dr shared.DeployRequest
unmarshal(&dr, resp.Args)
err := hc.deploy(&dr)
if err == nil {
prog := gatherDeployedPrograms(dr.Name)
hc.deploys[dr.Name] = prog
op.Refresh(ctx, hc.makeOperationQueryRequest())
if dr.Name == myname {
if srcdir, _, err := hc.prepareUpdateSelf(&dr); err == nil {
args := []string{
fmt.Sprintf("%d", os.Getpid()),
srcdir,
filepath.ToSlash(os.Args[0]),
}
args = append(args, os.Args[1:]...)
logger.Println(args)
// cmd := exec.Command(replacer, args...)
// if err := cmd.Start(); err != nil {
// logger.Println(err)
// } else {
// hc.shutdownFunc()
// }
} else {
logger.Println(err)
}
} else {
logger.Println(err)
if err := hc.deploy(&dr); err == nil {
prog := gatherDeployedPrograms(dr.Name)
hc.deploys[dr.Name] = prog
op.Refresh(ctx, hc.makeOperationQueryRequest())
} else {
logger.Println(err)
}
}
case shared.Withdraw:
@ -344,6 +382,8 @@ func NewClient() (HoustonClient, error) {
func (hc *houstonClient) Start() {
// receive from stream
defer func() {
hc.wg.Wait()
for _, proc := range hc.childProcs {
if err := proc.cmd.Process.Signal(syscall.SIGTERM); err != nil {
proc.cmd.Process.Signal(os.Kill)