deployprogress

This commit is contained in:
2023-10-24 20:08:48 +09:00
parent 64ca01c3a7
commit b6b8aa0794
9 changed files with 300 additions and 115 deletions

View File

@ -31,9 +31,10 @@ import (
)
type clientConfig struct {
GrpcAddress string `json:"grpc_server_address"`
HttpAddress string `json:"http_server_address"`
StorageRoot string `json:"storage_path"`
GrpcAddress string `json:"grpc_server_address"`
HttpAddress string `json:"http_server_address"`
StorageRoot string `json:"storage_path"`
RunNodeExporter bool `json:"run_node_exporter"`
}
func loadClientConfig() (clientConfig, error) {
@ -359,13 +360,29 @@ func NewClient(standalone bool) (HoustonClient, error) {
logger.Println(err)
}
} else {
if err := hc.deploy(&dr); err == nil {
hn, _ := os.Hostname()
if err := hc.deploy(&dr, func(dp *protos.DeployingProgress) {
dp.Hostname = hn
dp.Name = dr.Name
dp.Version = dr.Version
op.ReportDeployingProgress(ctx, dp)
}); err == nil {
prog := gatherDeployedPrograms(hc.config.StorageRoot, dr.Name)
hc.deploys[dr.Name] = prog
op.Refresh(ctx, hc.makeOperationQueryRequest())
} else {
logger.Println(err)
}
op.ReportDeployingProgress(ctx, &protos.DeployingProgress{
Hostname: hn,
Name: dr.Name,
Version: dr.Version,
State: "done",
Progress: 0,
Total: 0,
})
}
case shared.Withdraw:
@ -467,10 +484,15 @@ func (hc *houstonClient) Start() {
}
reconnCount++
var err error
dialContext, cancelDial := context.WithTimeout(context.Background(), 15*time.Second)
client, _ = grpc.DialContext(dialContext, hc.config.GrpcAddress, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
client, err = grpc.DialContext(dialContext, hc.config.GrpcAddress, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
cancelDial()
if client != nil {
if err != nil {
logger.Println("grpc.DialContext returns err :", err)
} else if client != nil {
reconnCount = 0
logger.Println("grpc.DialContext succeeded")
hc.clientChan <- client

View File

@ -11,17 +11,19 @@ import (
"os"
"path"
"path/filepath"
"strconv"
"strings"
"repositories.action2quare.com/ayo/gocommon/logger"
"repositories.action2quare.com/ayo/houston/shared"
"repositories.action2quare.com/ayo/houston/shared/protos"
"golang.org/x/text/encoding/korean"
"golang.org/x/text/transform"
)
func download(dir string, urlpath string, accessToken string) (target string, err error) {
func download(dir string, urlpath string, accessToken string, cb func(int64, int64)) (target string, err error) {
logger.Println("start downloading", dir, urlpath)
defer func() {
if err != nil {
@ -54,9 +56,27 @@ func download(dir string, urlpath string, accessToken string) (target string, er
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return "", err
cl := resp.Header.Get("Content-Length")
totalLength, _ := strconv.ParseInt(cl, 10, 0)
totalWritten := int64(0)
if cb != nil {
cb(0, totalLength)
}
for {
written, err := io.CopyN(out, resp.Body, 1024*1024)
totalWritten += written
if cb != nil {
cb(totalWritten, totalLength)
}
if err != nil {
if err == io.EOF {
break
}
return "", err
}
}
return filepath.ToSlash(out.Name()), nil
@ -248,7 +268,7 @@ func (hc *houstonClient) prepareUpdateSelf(req *shared.DeployRequest) (srcdir st
if err != nil {
return "", "", err
}
fname, err := download(tempdir, hc.makeDownloadUrl(req.Url), req.AccessToken)
fname, err := download(tempdir, hc.makeDownloadUrl(req.Url), req.AccessToken, nil)
if err != nil {
return "", "", err
}
@ -282,7 +302,7 @@ func (hc *houstonClient) prepareUpdateSelf(req *shared.DeployRequest) (srcdir st
return filepath.ToSlash(tempdir), replacer, err
}
func (hc *houstonClient) deploy(req *shared.DeployRequest) error {
func (hc *houstonClient) deploy(req *shared.DeployRequest, cb func(*protos.DeployingProgress)) error {
logger.Println("start deploying")
root, err := hc.prepareDeploy(req.Name, req.Version)
if err != nil {
@ -290,11 +310,25 @@ func (hc *houstonClient) deploy(req *shared.DeployRequest) error {
}
// verpath에 배포 시작
fname, err := download(root, hc.makeDownloadUrl(req.Url), req.AccessToken)
fname, err := download(root, hc.makeDownloadUrl(req.Url), req.AccessToken, func(written int64, total int64) {
prog := protos.DeployingProgress{
State: "download",
Progress: written,
Total: total,
}
cb(&prog)
})
if err != nil {
return err
}
cb(&protos.DeployingProgress{
State: "unzip/untar",
Progress: 0,
Total: 0,
})
switch path.Ext(fname) {
case ".zip":
err = unzip(fname)
@ -304,7 +338,7 @@ func (hc *houstonClient) deploy(req *shared.DeployRequest) error {
if err == nil && len(req.Config) > 0 {
// config.json도 다운로드
_, err = download(root, hc.makeDownloadUrl(req.Config), req.AccessToken)
_, err = download(root, hc.makeDownloadUrl(req.Config), req.AccessToken, nil)
}
return err

9
client/deploy_test.go Normal file
View File

@ -0,0 +1,9 @@
package client
import (
"testing"
)
func TestDownload(t *testing.T) {
download(".", "https://kdcc.action2quare.com/houston/_deploys/game/0.18.186.1/game.zip", "", nil)
}

View File

@ -441,7 +441,7 @@ func (hc *houstonClient) restartChildProcess(req *shared.RestartProcessRequest,
if len(req.Config) > 0 {
// config.json를 먼저 다운로드 시도
root := proc.cmd.Dir
if _, err := download(root, hc.makeDownloadUrl(req.Config), ""); err != nil {
if _, err := download(root, hc.makeDownloadUrl(req.Config), "", nil); err != nil {
return err
}
}