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

@ -11,6 +11,7 @@ import (
"net/url"
"os"
"path"
"path/filepath"
"strings"
"repositories.action2quare.com/ayo/gocommon/logger"
@ -54,7 +55,7 @@ func download(dir string, urlpath string, accessToken string) (string, error) {
return "", err
}
return out.Name(), nil
return filepath.ToSlash(out.Name()), nil
}
func unzip(fname string) error {
@ -63,7 +64,10 @@ func unzip(fname string) error {
os.Remove(fname)
return err
}
defer archive.Close()
defer func() {
archive.Close()
os.Remove(fname)
}()
verpath := path.Dir(fname)
for _, f := range archive.File {
@ -102,6 +106,7 @@ func unzip(fname string) error {
dstFile.Close()
fileInArchive.Close()
}
return nil
}
@ -110,7 +115,10 @@ func untar(fname string) error {
if err != nil {
return err
}
defer file.Close()
defer func() {
file.Close()
os.Remove(fname)
}()
verpath := path.Dir(fname)
tarReader := tar.NewReader(file)
@ -142,6 +150,7 @@ func untar(fname string) error {
return errors.New("unknown type")
}
}
return nil
}
@ -179,6 +188,83 @@ func (hc *houstonClient) prepareDeploy(name string, version string) (destPath st
return verpath, nil
}
func (hc *houstonClient) makeDownloadUrl(req *shared.DeployRequest) string {
out := req.Url
if !strings.HasPrefix(out, "http") {
tks := strings.SplitN(hc.httpAddr, "://", 2)
out = fmt.Sprintf("%s://%s", tks[0], path.Join(tks[1], req.Url))
}
return out
}
func copy(src, dst string) error {
fi, err := os.Stat(src)
if err != nil {
return err
}
inmode := fi.Mode()
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()
copied, err := io.Copy(out, in)
if err != nil {
return err
}
if copied < fi.Size() {
return errors.New("copy not completed")
}
if err := out.Sync(); err != nil {
return err
}
if err := out.Chmod(inmode); err != nil {
return err
}
return nil
}
func (hc *houstonClient) prepareUpdateSelf(req *shared.DeployRequest) (srcdir string, replacer string, err error) {
// 내가 스스로 업데이트
// 다운로드 받고 압축 푼 다음에 교체용 프로세스 시작
tempdir, err := os.MkdirTemp(os.TempDir(), "*")
if err != nil {
return "", "", err
}
fname, err := download(tempdir, hc.makeDownloadUrl(req), req.AccessToken)
if err != nil {
return "", "", err
}
switch path.Ext(fname) {
case ".zip":
err = unzip(fname)
case ".tar":
err = untar(fname)
}
if err != nil {
return "", "", err
}
selfname, _ := os.Executable()
srcreplacer := path.Join(path.Dir(fname), "replacer") + path.Ext(selfname)
replacer = "replacer" + path.Ext(selfname)
// replacer먼저 가져옴
return filepath.ToSlash(tempdir), filepath.ToSlash(replacer), copy(srcreplacer, replacer)
}
func (hc *houstonClient) deploy(req *shared.DeployRequest) error {
logger.Println("start deploying")
root, err := hc.prepareDeploy(req.Name, req.Version)
@ -186,14 +272,9 @@ func (hc *houstonClient) deploy(req *shared.DeployRequest) error {
return err
}
if !strings.HasPrefix(req.Url, "http") {
tks := strings.SplitN(hc.httpAddr, "://", 2)
req.Url = fmt.Sprintf("%s://%s", tks[0], path.Join(tks[1], req.Url))
}
logger.Println("start downloading", req.Url)
// verpath에 배포 시작
fname, err := download(root, req.Url, req.AccessToken)
fname, err := download(root, hc.makeDownloadUrl(req), req.AccessToken)
if err != nil {
return err
}