-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
199 lines (175 loc) · 5.85 KB
/
main.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
/*
Copyright 2020 The Magma Authors.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"runtime/debug"
"time"
"github.com/golang/glog"
"github.com/go-magma/magma/gateway/go/config"
"github.com/go-magma/magma/gateway/go/services/bootstrapper/gateway_info"
bootstrapper "github.com/go-magma/magma/gateway/go/services/bootstrapper/service"
configurator "github.com/go-magma/magma/gateway/go/services/configurator/service"
"github.com/go-magma/magma/gateway/go/services/magmad/service"
"github.com/go-magma/magma/gateway/go/services/magmad/service_manager"
"github.com/go-magma/magma/gateway/go/services/magmad/status"
sync_rpc "github.com/go-magma/magma/gateway/go/services/sync_rpc/service"
"github.com/go-magma/magma/lib/go/profile"
)
const (
BOOTSTRAP_RESTART_INTERVAL = time.Second * 120
)
const usageExamples string = `
Examples:
1. Run magmad as a service:
$> %s
The command will run magmad service which will periodically
check and update the gateway certificats and cloud managed GW configs
2. Show the gateway information needed for the gateway registration and exit:
$> %s -show
OR
$> %s -s
The command will print the gateway hardware ID and challenge key and exit
`
var (
showGwInfo = flag.Bool("show", false, "Print out gateway information needed for GW registration")
gcPercent = flag.Int("gc_percent", 20, "GC Percent")
freeMemInterval = flag.Duration("memory_purge_interval", time.Hour*6, "Force GC & unused memory purge interval")
)
func main() {
oldUsage := flag.Usage
flag.Usage = func() {
oldUsage()
cmd := os.Args[0]
fmt.Printf(usageExamples, cmd, cmd, cmd)
}
flag.BoolVar(showGwInfo, "s", *showGwInfo, "Print out gateway information needed for GW registration (shortcut)")
flag.Parse()
if *showGwInfo {
info, err := gateway_info.GetFormatted()
if err != nil {
glog.Error(err)
os.Exit(1)
}
fmt.Print(info)
os.Exit(0)
}
if _, isset := os.LookupEnv("GOGC"); !isset {
debug.SetGCPercent(*gcPercent)
}
eventChan := make(chan interface{}, 2)
// Start event loop in a dedicated routine
go mainEventLoop(eventChan, time.Tick(*freeMemInterval))
// Create bootstrapper
b := bootstrapper.NewBootstrapper(eventChan)
if err := b.Initialize(); err != nil {
controlProxyConfigJson, _ := json.MarshalIndent(config.GetControlProxyConfigs(), "", " ")
magmadProxyConfigJson, _ := json.MarshalIndent(config.GetMagmadConfigs(), "", " ")
glog.Fatalf(
"gateway '%s' bootstrap initialization error: %v, for configuration:\ncontrol_proxy: %s\nmagmad: %s",
b.HardwareId, err, string(controlProxyConfigJson), string(magmadProxyConfigJson))
}
// Start bootstrapper
glog.Info("Starting Bootstrapper")
go func() {
for {
err := b.Start() // Start will only return on error
if err != nil {
glog.Error(err)
time.Sleep(BOOTSTRAP_RESTART_INTERVAL)
b.RefreshConfigs()
} else {
glog.Fatal("unexpected Bootstrapper state")
}
}
}()
// Start SyncRPC service if it's enabled
if config.GetMagmadConfigs().EnableSyncRpc {
glog.Info("Starting SynRPC service")
syncRpcService := sync_rpc.NewClient(nil)
go syncRpcService.Run()
}
// start service status collector & reporter
go status.StartReporter()
// Start configurator & block on main()
cfg := configurator.NewConfigurator(eventChan)
glog.Info("Starting Configurator")
go func() {
if err := cfg.Start(); err != nil {
glog.Fatalf("configurator start error: %v", err)
}
}()
if err := service.StartMagmadServer(); err != nil {
glog.Fatalf("magmad start error: %v", err)
}
}
func mainEventLoop(eventChan <-chan interface{}, freeMemChan <-chan time.Time) {
for {
select {
case evnt := <-eventChan:
switch e := evnt.(type) {
case bootstrapper.BootstrapCompletion:
if e.Result != nil {
glog.Errorf("bootstrap failure: %v for Gateway ID: %s", e.Result, e.HardwareId)
} else {
glog.Infof("bootstrapped GW %s", e.HardwareId)
controller := service_manager.Get()
if config.GetControlProxyConfigs().ProxyCloudConnection {
glog.Info("restarting control_proxy")
if err := controller.Restart("control_proxy"); err != nil {
glog.Errorf("failed to restart control_proxy: %v", err)
}
} else {
// Restart all magma services
go func() {
for _, service := range config.GetMagmadConfigs().MagmaServices {
controller.Restart(service)
}
}()
}
}
case configurator.UpdateCompletion:
glog.Verbose(len(e) > 0).Infof("mconfigs updated successfully for services: %v", e)
// Restart all services with updated configs
go func() {
magmaServiceTable := map[string]struct{}{}
for _, service := range config.GetMagmadConfigs().MagmaServices {
magmaServiceTable[service] = struct{}{}
}
controller := service_manager.Get()
for _, service := range e {
// restart only if it's this GW's service
if _, ok := magmaServiceTable[service]; ok {
controller.Restart(service)
}
}
}()
default:
glog.Errorf("unknown completion type: %T", e)
} // switch
case _, ok := <-freeMemChan:
if ok {
glog.Info("purging unused memory")
debug.FreeOSMemory()
if glog.V(2) {
profile.LogMemStats()
}
// write out heap profile if built with -tags with_profiler, noop otherwise
// to use:
// go tool pprof -http=127.0.0.1:9999 <path/to/magmad> <profiles_dir/memory_MMDD_HH.mm.SS.pprof>
profile.MemWrite()
}
} // select
}
}