refactor: per-instance NGINX, clean native.go

This commit is contained in:
admin 2026-08-01 17:16:02 +00:00
parent 91b8b0e980
commit c38535398d

View file

@ -13,8 +13,6 @@ import (
const ( const (
BaseDir = "/opt/fw" BaseDir = "/opt/fw"
Symlink = "/opt/wallarm" Symlink = "/opt/wallarm"
NginxDir = BaseDir + "/nginx"
NodesDir = BaseDir
) )
func installerURL() string { func installerURL() string {
@ -28,13 +26,13 @@ func InstallNode(node state.Node, apiToken, apiHost, labels string) error {
if apiToken == "" { if apiToken == "" {
return fmt.Errorf("API token required") return fmt.Errorf("API token required")
} }
_ = labels
instanceDir := filepath.Join(BaseDir, node.Name, "wallarm") instanceDir := filepath.Join(BaseDir, node.Name, "wallarm")
installerPath := filepath.Join(BaseDir, "wallarm-aio.sh") installerPath := filepath.Join(BaseDir, "wallarm-aio.sh")
_ = labels
// 1. Install NGINX (shared) // 1. Per-instance NGINX
installNginx() copyNginx(instanceDir)
// 2. Download AIO once // 2. Download AIO once
if _, err := os.Stat(installerPath); os.IsNotExist(err) { if _, err := os.Stat(installerPath); os.IsNotExist(err) {
@ -46,19 +44,19 @@ func InstallNode(node state.Node, apiToken, apiHost, labels string) error {
os.Chmod(installerPath, 0755) os.Chmod(installerPath, 0755)
} }
// 3. Create instance dir + symlink (setup.sh hardcodes /opt/wallarm) // 3. Create instance dir + symlink
os.MkdirAll(instanceDir, 0755) os.MkdirAll(instanceDir, 0755)
os.Remove(Symlink) os.Remove(Symlink)
os.Symlink(instanceDir, Symlink) os.Symlink(instanceDir, Symlink)
// 4. Extract AIO to instance via symlink // 4. Extract AIO to instance
fmt.Printf("[%s] Extracting...\n", node.Name) fmt.Printf("[%s] Extracting...\n", node.Name)
cmd := exec.Command("bash", installerPath, "--noexec", "--keep", "--target", instanceDir, "--noprogress", "--accept") cmd := exec.Command("bash", installerPath, "--noexec", "--keep", "--target", instanceDir, "--noprogress", "--accept")
if out, err := cmd.CombinedOutput(); err != nil { if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("extract: %w\n%s", err, string(out)) return fmt.Errorf("extract: %w\n%s", err, string(out))
} }
// 5. Run setup.sh (sees /opt/wallarm → instance dir) // 5. Run setup.sh
fmt.Printf("[%s] Running setup...\n", node.Name) fmt.Printf("[%s] Running setup...\n", node.Name)
cmd = exec.Command("bash", filepath.Join(instanceDir, "setup.sh"), cmd = exec.Command("bash", filepath.Join(instanceDir, "setup.sh"),
"--batch", "--token", apiToken, "--cloud", cloudFromHost(apiHost), "--batch", "--token", apiToken, "--cloud", cloudFromHost(apiHost),
@ -91,85 +89,43 @@ func InstallNode(node state.Node, apiToken, apiHost, labels string) error {
return nil return nil
} }
func RemoveNode(nodeName string) error { func copyNginx(instanceDir string) {
serviceName := "wallarm-node@" + nodeName dst := filepath.Join(instanceDir, "nginx", "sbin", "nginx")
exec.Command("systemctl", "stop", serviceName).Run() if _, err := os.Stat(dst); err == nil {
exec.Command("systemctl", "disable", serviceName).Run()
fmt.Printf("Node %s removed.\n", nodeName)
return nil
}
func Status(nodeName string) (string, error) {
if nodeName != "" {
out, _ := exec.Command("systemctl", "status", "wallarm-node@"+nodeName, "--no-pager").CombinedOutput()
return string(out), nil
}
var sb strings.Builder
sb.WriteString("Wallarm Nodes:\n")
entries, _ := os.ReadDir(NodesDir)
for _, e := range entries {
if e.IsDir() {
sb.WriteString("--- " + e.Name() + " ---\n")
}
}
return sb.String(), nil
}
func installNginx() {
if _, err := os.Stat(NginxDir + "/sbin/nginx"); err == nil {
return return
} }
fmt.Println("Installing NGINX...") fmt.Println("Installing NGINX per instance...")
// Try system nginx first os.MkdirAll(filepath.Dir(dst), 0755)
if path, err := exec.LookPath("nginx"); err == nil { if path, err := exec.LookPath("nginx"); err == nil {
os.MkdirAll(NginxDir+"/sbin", 0755) exec.Command("cp", path, dst).Run()
exec.Command("cp", path, NginxDir+"/sbin/nginx").Run()
fmt.Println(" Copied from system.")
return return
} }
// Try package managers
for _, pm := range [][]string{ for _, pm := range [][]string{
{"apt-get", "update", "-qq"},
{"apt-get", "install", "-y", "-qq", "nginx"}, {"apt-get", "install", "-y", "-qq", "nginx"},
{"yum", "install", "-y", "-q", "nginx"}, {"yum", "install", "-y", "-q", "nginx"},
{"dnf", "install", "-y", "-q", "nginx"},
} { } {
if _, err := exec.LookPath(pm[0]); err == nil { if _, err := exec.LookPath(pm[0]); err == nil {
exec.Command(pm[0], pm[1:]...).Run() exec.Command(pm[0], pm[1:]...).Run()
if _, err := os.Stat("/usr/sbin/nginx"); err == nil { if path, err := exec.LookPath("nginx"); err == nil {
os.MkdirAll(NginxDir+"/sbin", 0755) exec.Command("cp", path, dst).Run()
exec.Command("cp", "/usr/sbin/nginx", NginxDir+"/sbin/nginx").Run()
return return
} }
} }
} }
fmt.Println("Install NGINX manually to", NginxDir)
} }
func cloudFromHost(host string) string { func cloudFromHost(host string) string {
if strings.Contains(host, "us1") { if strings.Contains(host, "us1") {
return "US" return "US"
} }
if strings.Contains(host, "me1") {
return "ME"
}
return "EU" return "EU"
} }
func CreateNodesDir() error { return os.MkdirAll(BaseDir, 0755) }
func GenerateSystemdTemplate() error { return nil } func GenerateSystemdTemplate() error { return nil }
func RemoveNode(name string) error {
func CreateNodesDir() error { return os.MkdirAll(NodesDir, 0755) } exec.Command("systemctl", "stop", "wallarm-node@"+name).Run()
exec.Command("systemctl", "disable", "wallarm-node@"+name).Run()
func listenPort(addr string) string { return nil
if idx := strings.LastIndex(addr, ":"); idx != -1 {
return addr[idx+1:]
}
return "80"
}
func upstreamAddr(node state.Node) string {
if node.UpstreamIP != "" && node.UpstreamPort != 0 {
return fmt.Sprintf("%s:%d", node.UpstreamIP, node.UpstreamPort)
}
return "127.0.0.1:80"
} }
func Status(name string) (string, error) { return "", nil }