Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit b673493

Browse files
committed
adding silent-install mode
1 parent 7e0b508 commit b673493

File tree

3 files changed

+82
-60
lines changed

3 files changed

+82
-60
lines changed

configuration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func registerAgent(endPoint, name string, key string, insecure bool) (agentDetai
6060
err = json.Unmarshal(body, &agentList)
6161
if err != nil {
6262
h.Error("can't decode agent details: %v", err)
63+
h.Debug("query result: %s", body)
6364
time.Sleep(10 * time.Second)
6465
os.Exit(1)
6566
}
@@ -73,6 +74,7 @@ func registerAgent(endPoint, name string, key string, insecure bool) (agentDetai
7374
err = json.Unmarshal(body, &agent)
7475
if err != nil {
7576
h.Error("can't decode agent details: %v", err)
77+
h.Debug("query result: %s", body)
7678
time.Sleep(10 * time.Second)
7779
os.Exit(1)
7880
}

install.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"strconv"
6+
"time"
7+
)
8+
9+
func install(ip, utmKey, skip string) {
10+
11+
var insecure bool
12+
if skip == "yes" {
13+
insecure = true
14+
}
15+
16+
hostName, err := os.Hostname()
17+
if err != nil {
18+
h.Error("can't get the hostname: %v", err)
19+
time.Sleep(10 * time.Second)
20+
os.Exit(1)
21+
}
22+
23+
agent, err := registerAgent(AGENTMANAGERPROTO+"://"+ip+":"+strconv.Itoa(AGENTMANAGERPORT), hostName, utmKey, insecure)
24+
if err != nil {
25+
h.Error("can't register agent: %v", err)
26+
time.Sleep(10 * time.Second)
27+
os.Exit(1)
28+
}
29+
30+
cnf := config{Server: ip, AgentID: agent.ID, AgentKey: agent.Key, SkipCertValidation: insecure}
31+
err = writeConfig(cnf)
32+
if err != nil {
33+
h.Error("can't write agent config: %v", err)
34+
time.Sleep(10 * time.Second)
35+
os.Exit(1)
36+
}
37+
38+
err = configureBeat(ip)
39+
if err != nil {
40+
h.Error("can't configure beat: %v", err)
41+
time.Sleep(10 * time.Second)
42+
os.Exit(1)
43+
}
44+
45+
err = configureWazuh(ip, cnf.AgentKey)
46+
if err != nil {
47+
h.Error("can't configure wazuh: %v", err)
48+
time.Sleep(10 * time.Second)
49+
os.Exit(1)
50+
}
51+
52+
err = autoStart()
53+
if err != nil {
54+
h.Error("can't configure agent service: %v", err)
55+
time.Sleep(10 * time.Second)
56+
os.Exit(1)
57+
}
58+
59+
os.Exit(0)
60+
}

main.go

Lines changed: 20 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"os"
66
"os/signal"
7-
"strconv"
87
"syscall"
98
"time"
109

@@ -47,12 +46,8 @@ func main() {
4746
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
4847
<-signals
4948
stopWazuh()
50-
default:
51-
fmt.Println("unknown option")
52-
}
53-
} else {
54-
if _, err := os.ReadFile("config.yml"); err != nil {
55-
49+
50+
case "install":
5651
var ip string
5752
var utmKey string
5853
var skip string
@@ -78,61 +73,26 @@ func main() {
7873
os.Exit(1)
7974
}
8075

81-
hostName, err := os.Hostname()
82-
if err != nil {
83-
h.Error("can't get the hostname: %v", err)
84-
time.Sleep(10 * time.Second)
85-
os.Exit(1)
86-
}
87-
88-
var insecure bool
89-
if skip == "yes" {
90-
insecure = true
91-
}
76+
install(ip, utmKey, skip)
77+
78+
case "silent-install":
79+
ip := os.Args[2]
80+
utmKey := os.Args[3]
81+
skip := os.Args[4]
9282

93-
agent, err := registerAgent(AGENTMANAGERPROTO+"://"+ip+":"+strconv.Itoa(AGENTMANAGERPORT), hostName, utmKey, insecure)
94-
if err != nil {
95-
h.Error("can't register agent: %v", err)
96-
time.Sleep(10 * time.Second)
97-
os.Exit(1)
98-
}
99-
100-
cnf := config{Server: ip, AgentID: agent.ID, AgentKey: agent.Key, SkipCertValidation: insecure}
101-
err = writeConfig(cnf)
102-
if err != nil {
103-
h.Error("can't write agent config: %v", err)
104-
time.Sleep(10 * time.Second)
105-
os.Exit(1)
106-
}
107-
108-
err = configureBeat(ip)
109-
if err != nil {
110-
h.Error("can't configure beat: %v", err)
111-
time.Sleep(10 * time.Second)
112-
os.Exit(1)
113-
}
114-
115-
err = configureWazuh(ip, cnf.AgentKey)
116-
if err != nil {
117-
h.Error("can't configure wazuh: %v", err)
118-
time.Sleep(10 * time.Second)
119-
os.Exit(1)
120-
}
121-
122-
err = autoStart()
123-
if err != nil {
124-
h.Error("can't configure agent service: %v", err)
125-
time.Sleep(10 * time.Second)
126-
os.Exit(1)
127-
}
128-
} else {
129-
err := uninstall()
130-
if err != nil {
131-
h.Error("can't remove agent dependencies or configurations: %v", err)
132-
time.Sleep(10 * time.Second)
133-
os.Exit(1)
134-
}
83+
install(ip, utmKey, skip)
84+
85+
default:
86+
fmt.Println("unknown option")
87+
}
88+
} else {
89+
err := uninstall()
90+
if err != nil {
91+
h.Error("can't remove agent dependencies or configurations: %v", err)
92+
time.Sleep(10 * time.Second)
93+
os.Exit(1)
13594
}
95+
13696
os.Exit(0)
13797
}
13898
}

0 commit comments

Comments
 (0)