-
Notifications
You must be signed in to change notification settings - Fork 45
/
daemon.go
274 lines (241 loc) · 5.92 KB
/
daemon.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package main
import (
"errors"
"fmt"
"net"
"os"
"os/signal"
"runtime/pprof"
"strings"
"time"
ptp "github.com/subutai-io/p2p/lib"
)
var (
errEmptyDHTEndpoint = errors.New("DHT endpoint wasn't specified")
errBadDHTEndpoint = errors.New("Endpoint have wrong format")
)
// DaemonArgs arguments used by daemon to manipulate
// p2p behaviour
type DaemonArgs struct {
IP string `json:"ip"`
Mac string `json:"mac"`
Dev string `json:"dev"`
Hash string `json:"hash"`
Dht string `json:"dht"`
Keyfile string `json:"keyfile"`
Key string `json:"key"`
TTL string `json:"ttl"`
Fwd bool `json:"fwd"`
Port int `json:"port"`
Interfaces bool `json:"interfaces"` // show only
All bool `json:"all"` // show only
Command string `json:"command"`
Args string `json:"args"`
Log string `json:"log"`
Bind bool `json:"bind"`
MTU bool `json:"mtu"`
}
var bootstrap DHTConnection
var UsePMTU bool
func processConfigFile(configFile string) (*ptp.Conf, error) {
if configFile == "" {
return nil, fmt.Errorf("no config file provided")
}
conf := new(ptp.Conf)
err := conf.Load(configFile)
if err != nil {
return conf, err
}
return conf, nil
}
func configureMTU(conf *ptp.Conf, mtu int, pmtu bool) {
if conf == nil {
ptp.GlobalMTU = ptp.DefaultMTU
ptp.UsePMTU = false
ptp.Log(ptp.Error, "Couldn't configure MTU: conf nil")
return
}
ptp.GlobalMTU = conf.GetMTU(mtu)
ptp.Log(ptp.Info, "MTU is set to %d", ptp.GlobalMTU)
if pmtu == false && conf.GetPMTU() == false {
ptp.Log(ptp.Info, "PMTU disabled")
ptp.UsePMTU = false
} else {
ptp.Log(ptp.Info, "PMTU enabled")
ptp.UsePMTU = true
}
}
// ExecDaemon starts P2P daemon
func ExecDaemon(port int, targetURL, sFile, profiling, syslog, logLevel, configFile string, mtu int, pmtu bool) {
ptp.Log(ptp.Info, "Initializing P2P Daemon")
if logLevel == "" {
ptp.SetMinLogLevelString(DefaultLog)
} else {
ptp.SetMinLogLevelString(logLevel)
}
var err error
if configFile == "" {
configFile = ptp.DefaultConfigLocation
}
config, err := processConfigFile(configFile)
if err != nil {
ptp.Log(ptp.Error, "Failed to load config file %s: %s", configFile, err.Error())
} else {
ptp.Log(ptp.Info, "Loaded configuration from %s", configFile)
}
if targetURL == "" {
targetURL = "subutai.io"
}
if syslog != "" {
ptp.SetSyslogSocket(syslog)
}
StartProfiling(profiling)
ptp.InitPlatform()
ptp.InitErrors()
configureMTU(config, mtu, pmtu)
if !ptp.HavePrivileges(ptp.GetPrivilegesLevel()) {
os.Exit(1)
}
StartTime = time.Now()
ReadyToServe = false
bootstrapConnected := false
bootstrapLastConnection := time.Unix(0, 0)
for !bootstrapConnected {
if time.Since(bootstrapLastConnection) > time.Duration(time.Second*5) {
bootstrapLastConnection = time.Now()
err := bootstrap.init(targetURL)
if err == nil {
bootstrapConnected = true
} else {
ptp.Log(ptp.Error, "Failed to connect to %s", targetURL)
}
}
time.Sleep(time.Millisecond * 100)
}
go bootstrap.run()
go waitOutboundIP()
proc := new(Daemon)
proc.init(sFile)
setupRESTHandlers(port, proc)
go restoreInstances(proc)
ReadyToServe = true
SignalChannel = make(chan os.Signal, 1)
signal.Notify(SignalChannel, os.Interrupt)
go waitActiveBootstrap()
go func() {
for sig := range SignalChannel {
fmt.Println("Received signal: ", sig)
pprof.StopCPUProfile()
os.Exit(0)
}
}()
// main loop
for {
for id, inst := range proc.Instances.get() {
if inst == nil || inst.PTP == nil {
continue
}
if inst.PTP.ReadyToStop {
err := proc.Stop(&DaemonArgs{Hash: id}, &Response{})
if err != nil {
ptp.Log(ptp.Error, "Failed to stop instance: %s", err)
}
}
}
time.Sleep(time.Millisecond * 100)
}
}
func waitOutboundIP() {
for _, r := range bootstrap.routers {
if r != nil {
go r.run()
go r.keepAlive()
}
}
for !bootstrap.isActive {
for _, r := range bootstrap.routers {
if r.running && r.handshaked {
bootstrap.isActive = true
break
}
}
time.Sleep(time.Millisecond * 100)
}
for bootstrap.ip == "" {
time.Sleep(time.Millisecond * 100)
}
OutboundIP = net.ParseIP(bootstrap.ip)
}
func waitActiveBootstrap() {
for {
active := 0
for _, r := range bootstrap.routers {
if !r.stop {
active++
}
}
if active == 0 {
ptp.Log(ptp.Info, "No active bootstrap nodes")
os.Exit(0)
}
time.Sleep(time.Millisecond * 100)
}
}
func restoreInstances(daemon *Daemon) {
for !bootstrap.isActive {
time.Sleep(100 * time.Millisecond)
}
if daemon.Restore != nil && daemon.Restore.isActive() {
ptp.Log(ptp.Info, "Restore subsystem initialized")
// loading from restore file
err := daemon.Restore.load()
if err != nil {
ptp.Log(ptp.Error, "Failed to restore from file")
return
}
entries := daemon.Restore.get()
if len(entries) == 0 {
return
}
ptp.Log(ptp.Info, "Attempt to restore %d instances", len(entries))
restored := 0
for _, e := range entries {
err := daemon.run(&RunArgs{
IP: e.IP,
Mac: e.Mac,
Dev: e.Dev,
Hash: e.Hash,
Keyfile: e.Keyfile,
Key: e.Key,
TTL: e.TTL,
}, new(Response))
if err != nil {
ptp.Log(ptp.Error, "Failed to start instance %s during restore: %s", e.Hash, err.Error())
continue
} else {
restored++
daemon.Restore.bumpInstance(e.Hash)
}
}
err = daemon.Restore.save()
if err != nil {
ptp.Log(ptp.Error, "Failed to save restore file")
}
ptp.Log(ptp.Info, "Restored %d of %d instances", restored, len(entries))
}
}
func validateDHT(dht string) error {
if dht == "" {
ptp.Log(ptp.Error, "Empty bootstrap list")
return errEmptyDHTEndpoint
}
eps := strings.Split(dht, ",")
for _, ep := range eps {
_, err := net.ResolveTCPAddr("tcp4", ep)
if err != nil {
ptp.Log(ptp.Error, "Bootstrap %s have bad format or wrong address: %s", ep, err)
return errBadDHTEndpoint
}
}
return nil
}