refactor: per-instance NGINX, clean native.go
This commit is contained in:
parent
91b8b0e980
commit
c38535398d
1 changed files with 22 additions and 66 deletions
|
|
@ -13,8 +13,6 @@ import (
|
|||
const (
|
||||
BaseDir = "/opt/fw"
|
||||
Symlink = "/opt/wallarm"
|
||||
NginxDir = BaseDir + "/nginx"
|
||||
NodesDir = BaseDir
|
||||
)
|
||||
|
||||
func installerURL() string {
|
||||
|
|
@ -28,13 +26,13 @@ func InstallNode(node state.Node, apiToken, apiHost, labels string) error {
|
|||
if apiToken == "" {
|
||||
return fmt.Errorf("API token required")
|
||||
}
|
||||
_ = labels
|
||||
|
||||
instanceDir := filepath.Join(BaseDir, node.Name, "wallarm")
|
||||
installerPath := filepath.Join(BaseDir, "wallarm-aio.sh")
|
||||
_ = labels
|
||||
|
||||
// 1. Install NGINX (shared)
|
||||
installNginx()
|
||||
// 1. Per-instance NGINX
|
||||
copyNginx(instanceDir)
|
||||
|
||||
// 2. Download AIO once
|
||||
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)
|
||||
}
|
||||
|
||||
// 3. Create instance dir + symlink (setup.sh hardcodes /opt/wallarm)
|
||||
// 3. Create instance dir + symlink
|
||||
os.MkdirAll(instanceDir, 0755)
|
||||
os.Remove(Symlink)
|
||||
os.Symlink(instanceDir, Symlink)
|
||||
|
||||
// 4. Extract AIO to instance via symlink
|
||||
// 4. Extract AIO to instance
|
||||
fmt.Printf("[%s] Extracting...\n", node.Name)
|
||||
cmd := exec.Command("bash", installerPath, "--noexec", "--keep", "--target", instanceDir, "--noprogress", "--accept")
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
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)
|
||||
cmd = exec.Command("bash", filepath.Join(instanceDir, "setup.sh"),
|
||||
"--batch", "--token", apiToken, "--cloud", cloudFromHost(apiHost),
|
||||
|
|
@ -91,85 +89,43 @@ func InstallNode(node state.Node, apiToken, apiHost, labels string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func RemoveNode(nodeName string) error {
|
||||
serviceName := "wallarm-node@" + nodeName
|
||||
exec.Command("systemctl", "stop", serviceName).Run()
|
||||
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 {
|
||||
func copyNginx(instanceDir string) {
|
||||
dst := filepath.Join(instanceDir, "nginx", "sbin", "nginx")
|
||||
if _, err := os.Stat(dst); err == nil {
|
||||
return
|
||||
}
|
||||
fmt.Println("Installing NGINX...")
|
||||
// Try system nginx first
|
||||
fmt.Println("Installing NGINX per instance...")
|
||||
os.MkdirAll(filepath.Dir(dst), 0755)
|
||||
if path, err := exec.LookPath("nginx"); err == nil {
|
||||
os.MkdirAll(NginxDir+"/sbin", 0755)
|
||||
exec.Command("cp", path, NginxDir+"/sbin/nginx").Run()
|
||||
fmt.Println(" Copied from system.")
|
||||
exec.Command("cp", path, dst).Run()
|
||||
return
|
||||
}
|
||||
// Try package managers
|
||||
for _, pm := range [][]string{
|
||||
{"apt-get", "update", "-qq"},
|
||||
{"apt-get", "install", "-y", "-qq", "nginx"},
|
||||
{"yum", "install", "-y", "-q", "nginx"},
|
||||
{"dnf", "install", "-y", "-q", "nginx"},
|
||||
} {
|
||||
if _, err := exec.LookPath(pm[0]); err == nil {
|
||||
exec.Command(pm[0], pm[1:]...).Run()
|
||||
if _, err := os.Stat("/usr/sbin/nginx"); err == nil {
|
||||
os.MkdirAll(NginxDir+"/sbin", 0755)
|
||||
exec.Command("cp", "/usr/sbin/nginx", NginxDir+"/sbin/nginx").Run()
|
||||
if path, err := exec.LookPath("nginx"); err == nil {
|
||||
exec.Command("cp", path, dst).Run()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("Install NGINX manually to", NginxDir)
|
||||
}
|
||||
|
||||
func cloudFromHost(host string) string {
|
||||
if strings.Contains(host, "us1") {
|
||||
return "US"
|
||||
}
|
||||
if strings.Contains(host, "me1") {
|
||||
return "ME"
|
||||
}
|
||||
return "EU"
|
||||
}
|
||||
|
||||
func CreateNodesDir() error { return os.MkdirAll(BaseDir, 0755) }
|
||||
func GenerateSystemdTemplate() error { return nil }
|
||||
|
||||
func CreateNodesDir() error { return os.MkdirAll(NodesDir, 0755) }
|
||||
|
||||
func listenPort(addr string) string {
|
||||
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 RemoveNode(name string) error {
|
||||
exec.Command("systemctl", "stop", "wallarm-node@"+name).Run()
|
||||
exec.Command("systemctl", "disable", "wallarm-node@"+name).Run()
|
||||
return nil
|
||||
}
|
||||
func Status(name string) (string, error) { return "", nil }
|
||||
|
|
|
|||
Loading…
Reference in a new issue