-
Notifications
You must be signed in to change notification settings - Fork 1
/
srv.go
153 lines (135 loc) · 3.63 KB
/
srv.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package opnborg
import (
"strings"
"time"
)
// Start Server Application
func srv(config *OPNCall) error {
// init
var err error
var servers []string
// spin up Log/Display Engine
display.Add(1)
// spin up internal log / display engine
go startLog(config)
// startup app version & state, sleep panic gate
suffix := "[CLI-ONE-TIME-PASS-MODE]"
if config.Daemon {
suffix = "[DAEMON-MODE][SLEEP:" + sleep + " SECONDS]"
}
displayChan <- []byte("[STARTING][" + _app + "][" + SemVer + "]" + suffix)
// arm background timer
go func() {
time.Sleep(time.Duration(config.Sleep) * time.Second)
updateOPN <- true
if unifiBackupEnable.Load() {
updateUnifiBackup <- true
}
if unifiExportEnable.Load() {
updateUnifiExport <- true
}
}()
// spin up internal webserver
state := "[DISABLED]"
if config.Httpd.Enable {
go startWeb(config)
state = "[ENABLED]"
}
displayChan <- []byte("[SERVICE][HTTPD]" + state + "[" + config.Httpd.Server + "]")
// spin up internal rsyslog server
state = "[DISABLED]"
if config.RSysLog.Enable {
go startRSysLog(config)
state = "[ENABLED]"
}
displayChan <- []byte("[SERVICE][RSYSLOG]" + state)
// spin up unifi backup server
state = "[DISABLED]"
if config.Unifi.Backup.Enable {
state = "[ENABLED]"
unifiStatus = _na + " <b>Member: </b> " + config.Unifi.WebUI.String() + " <b>Version: </b>n/a <b>Last Seen: </b>n/a<br>"
go unifiBackupServer(config)
}
displayChan <- []byte("[SERVICE][UNIFI-BACKUP-AND-MONITORING]" + state)
// is opnsense hive is enabled?
state = "[DISABLED]"
if config.Enable {
state = "[ENABLED]"
// setup hive
servers = strings.Split(config.Targets, ",")
for _, server := range servers {
s := strings.Split(server, "#")
switch len(s) {
case 1:
if len(s[0]) > 0 {
status := _na + " <b>Member: </b> " + s[0] + " <b>Version: </b>n/a <b>Last Seen: </b>n/a<br>"
hive = append(hive, status)
}
case 2:
if len(s[0]) > 0 && len(s[1]) > 0 {
status := _na + " <b>Member: </b> " + s[0] + " <b>Version: </b>n/a <b>Last Seen: </b>n/a <b>AssetTag: </b>" + s[1] + "<br>"
hive = append(hive, status)
}
}
}
}
displayChan <- []byte("[SERVICE][OPN-BACKUP-AND-MONITORING]" + state)
// main loop
for {
// reset global (atomic) git worktree state tracker
if config.Git {
config.dirty.Store(false)
}
// is opnsense hive is enabled
if config.Enable {
// fetch target configuration from master server
if config.Sync.Enable {
config.Sync.validConf = true
config, err = readMasterConf(config)
if err != nil {
config.Sync.validConf = false
displayChan <- []byte("[ERROR][UNABLE-TO-READ-MASTER-CONFIG]" + err.Error())
}
}
// spinup individual worker for every server
if config.Debug {
displayChan <- []byte("[STARTING][BACKUP]")
}
for id, server := range servers {
s := strings.Split(server, "#")
switch len(s) {
case 1:
wg.Add(1)
go actionOPN(s[0], "", config, id, &wg)
case 2:
wg.Add(1)
go actionOPN(s[0], s[1], config, id, &wg)
}
}
// wait till all worker done
wg.Wait()
}
// check files into local git repo
if config.dirty.Load() {
if config.Git {
if err := gitCheckIn(config); err != nil {
displayChan <- []byte("[GIT][REPO][CHECKIN][FAIL]")
return err
}
displayChan <- []byte("[CHANGES-DETECTED][GIT][REPO][CHECKIN][FINISH]")
}
displayChan <- []byte("[CHANGES-DETECTED][UPDATES-DONE][FINISH]")
}
// finish
if config.Debug {
displayChan <- []byte("[FINISH][BACKUP][ALL]")
}
// exit if not in daemon mode
if !config.Daemon {
close(displayChan)
display.Wait()
return nil
}
<-updateOPN
}
}