diff --git a/internal/native/native.go b/internal/native/native.go index 2193f04..29cf23f 100644 --- a/internal/native/native.go +++ b/internal/native/native.go @@ -35,9 +35,14 @@ func InstallNode(node state.Node, apiToken, apiHost, labels string) error { os.RemoveAll(DeployTo) os.MkdirAll(DeployTo, 0755) - // 2. Per-instance NGINX into /opt/wallarm + // 2. Kill any existing process on the target port, then start per-instance NGINX + port := "80" + if idx := strings.LastIndex(node.Address, ":"); idx != -1 { + port = node.Address[idx+1:] + } + killPort(port) copyNginx(DeployTo) - startNginx(DeployTo, node) + startNginx(DeployTo, node, port) // 3. Download AIO once if _, err := os.Stat(installerPath); os.IsNotExist(err) { @@ -140,12 +145,8 @@ func GenerateSystemdTemplate() error { return nil } func RemoveNode(name string) error { return nil } func Status(name string) (string, error) { return "", nil } -func startNginx(dir string, node state.Node) { +func startNginx(dir string, node state.Node, port string) { nginxDir := filepath.Join(dir, "nginx") - port := "80" - if idx := strings.LastIndex(node.Address, ":"); idx != -1 { - port = node.Address[idx+1:] - } confDir := filepath.Join(nginxDir, "conf") os.MkdirAll(confDir, 0755) confPath := filepath.Join(confDir, "nginx.conf") @@ -173,3 +174,11 @@ http { fmt.Printf("[%s] NGINX started on port %s\n", node.Name, port) } } + +func killPort(port string) { + // Find and kill any process listening on the target port + out, _ := exec.Command("fuser", "-k", port+"/tcp").CombinedOutput() + if len(out) > 0 { + fmt.Printf(" Killed existing process on port %s\n", port) + } +}