From a45cf5592bc4ce288c3095a003dbfdaf11a9c2b8 Mon Sep 17 00:00:00 2001 From: Gucheng Wang Date: Thu, 5 Oct 2023 20:27:31 +0800 Subject: [PATCH] Add diff to CreateRepo() --- conf/app.conf | 1 + object/site.go | 31 +++++++++++++++++++++++++------ run/git_test.go | 2 +- run/run.go | 8 +++----- run/util.go | 14 ++++++-------- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/conf/app.conf b/conf/app.conf index f7711dc..d8fd427 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -15,3 +15,4 @@ casdoorOrganization = "casbin" casdoorApplication = "app-casibase" isDemoMode = false appDir = "C:/github_repos" +dbPass = "" diff --git a/object/site.go b/object/site.go index e58fc4a..115ba66 100644 --- a/object/site.go +++ b/object/site.go @@ -40,6 +40,7 @@ type Site struct { Domain string `xorm:"varchar(100)" json:"domain"` Host string `xorm:"varchar(100)" json:"host"` + Port int `json:"port"` SslMode string `xorm:"varchar(100)" json:"sslMode"` SslCert string `xorm:"varchar(100)" json:"sslCert"` PublicIp string `xorm:"varchar(100)" json:"publicIp"` @@ -188,6 +189,19 @@ func (site *Site) GetId() string { return fmt.Sprintf("%s/%s", site.Owner, site.Name) } +func (site *Site) GetHost() string { + if site.Host != "" { + return site.Host + } + + if site.Port == 0 { + return "" + } + + res := fmt.Sprintf("http://localhost:%d", site.Port) + return res +} + func (site *Site) checkNodes() { hostname := util.GetHostname() for i, node := range site.Nodes { @@ -195,28 +209,33 @@ func (site *Site) checkNodes() { continue } - if site.Host == "" { + if site.GetHost() == "" { continue } - ok, msg := pingUrl(site.Host) + ok, msg := pingUrl(site.GetHost()) status := "Running" if !ok { status = "Stopped" } - run.CreateRepo(site.Name) + diff := "" + if i != 0 { + diff = site.Nodes[0].Diff + } + + run.CreateRepo(site.Name, !ok, diff) version := getSiteVersion(site.Name) path := run.GetRepoPath(site.Name) - diff := run.GitDiff(path) + newDiff := run.GitDiff(path) - if node.Status != status || node.Message != msg || node.Version != version || node.Diff != diff { + if node.Status != status || node.Message != msg || node.Version != version || node.Diff != newDiff { site.Nodes[i].Status = status site.Nodes[i].Message = msg site.Nodes[i].Version = version - site.Nodes[i].Diff = diff + site.Nodes[i].Diff = newDiff UpdateSite(site.GetId(), site) } } diff --git a/run/git_test.go b/run/git_test.go index f78d721..295cdf5 100644 --- a/run/git_test.go +++ b/run/git_test.go @@ -32,5 +32,5 @@ func TestGitGetDiff(t *testing.T) { //diff := GitDiff("F:/github_repos/casdoor") //println(diff) - CreateRepo("casdoor_test", true) + CreateRepo("casdoor_test", true, "") } diff --git a/run/run.go b/run/run.go index a66620d..db94ae9 100644 --- a/run/run.go +++ b/run/run.go @@ -20,7 +20,7 @@ import ( "github.com/casbin/caswaf/util" ) -func CreateRepo(siteName string, needStart bool) { +func CreateRepo(siteName string, needStart bool, diff string) { path := GetRepoPath(siteName) if !util.FileExist(path) { originalName := getOriginalName(siteName) @@ -30,10 +30,8 @@ func CreateRepo(siteName string, needStart bool) { if strings.HasPrefix(siteName, "cc_") || strings.Count(siteName, "_") == 2 { index := getNameIndex(siteName) updateAppConfFile(siteName, index) - } else if originalName != siteName { - originalPath := GetRepoPath(originalName) - patch := GitDiff(originalPath) - gitApply(path, patch) + } else if diff != "" { + gitApply(path, diff) } } diff --git a/run/util.go b/run/util.go index 520051f..eddfa56 100644 --- a/run/util.go +++ b/run/util.go @@ -15,7 +15,6 @@ package run import ( - "bytes" "fmt" "os" "os/exec" @@ -103,16 +102,15 @@ func startProcess(name string) { } } -func isProcessActive(name string) bool { +func stopProcess(name string) { + fmt.Printf("Stopping process: [%s]\n", name) + windowName := fmt.Sprintf("%s.bat - Shortcut", name) - cmd := exec.Command("cmd", "/C", "tasklist", "/v", "|", "findstr", windowName) - var out bytes.Buffer - cmd.Stdout = &out + // taskkill /IM "casdoor.bat - Shortcut" /F + // taskkill /F /FI "WINDOWTITLE eq casdoor.bat - Shortcut" /T + cmd := exec.Command("taskkill", "/F", "/FI", fmt.Sprintf("WINDOWTITLE eq %s", windowName), "/T") err := cmd.Run() if err != nil { panic(err) } - - res := out.String() != "" - return res }