-
Notifications
You must be signed in to change notification settings - Fork 12
/
yavirtd.go
180 lines (162 loc) · 4.32 KB
/
yavirtd.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
package main
import (
"context"
"fmt"
"net/http"
_ "net/http/pprof" //nolint
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/cockroachdb/errors"
"github.com/projecteru2/core/log"
coretypes "github.com/projecteru2/core/types"
"github.com/projecteru2/yavirt/configs"
"github.com/projecteru2/yavirt/internal/debug"
"github.com/projecteru2/yavirt/internal/metrics"
grpcserver "github.com/projecteru2/yavirt/internal/rpc"
"github.com/projecteru2/yavirt/internal/service/boar"
"github.com/projecteru2/yavirt/internal/utils"
"github.com/projecteru2/yavirt/internal/ver"
"github.com/projecteru2/yavirt/internal/virt"
zerolog "github.com/rs/zerolog/log"
cli "github.com/urfave/cli/v2"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() * 2)
utils.EnforceRoot()
cli.VersionPrinter = func(_ *cli.Context) {
fmt.Println(ver.Version())
}
app := &cli.App{
Name: "yavirtd",
Usage: "yavirt daemon",
Version: "v",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Value: "/etc/eru/yavirtd.toml",
Usage: "config file path for yavirt, in toml",
EnvVars: []string{"ERU_YAVIRT_CONFIG_PATH"},
},
&cli.StringFlag{
Name: "log-level",
Value: "",
Usage: "set log level",
EnvVars: []string{"ERU_YAVIRT_LOG_LEVEL"},
},
&cli.StringSliceFlag{
Name: "core-addrs",
Value: cli.NewStringSlice(),
Usage: "core addresses",
EnvVars: []string{"ERU_YAVIRT_CORE_ADDRS"},
},
&cli.StringFlag{
Name: "core-username",
Value: "",
Usage: "core username",
EnvVars: []string{"ERU_YAVIRT_CORE_USERNAME"},
},
&cli.StringFlag{
Name: "core-password",
Value: "",
Usage: "core password",
EnvVars: []string{"ERU_YAVIRT_CORE_PASSWORD"},
},
&cli.StringFlag{
Name: "hostname",
Value: "",
Usage: "change hostname",
EnvVars: []string{"ERU_HOSTNAME", "HOSTNAME"},
},
},
Action: Run,
}
if err := app.Run(os.Args); err != nil {
log.Error(context.TODO(), err, "run failed")
os.Exit(1)
}
os.Exit(0)
}
func initConfig(c *cli.Context) error {
cfg := &configs.Conf
if err := cfg.Load(c.String("config")); err != nil {
return err
}
return cfg.Prepare(c)
}
func startHTTPServer(addr string) {
http.Handle("/metrics", metrics.Handler())
http.HandleFunc("/debug/custom", debug.Handler)
server := &http.Server{
Addr: addr,
ReadHeaderTimeout: 5 * time.Second,
}
if err := server.ListenAndServe(); err != nil {
log.Error(context.TODO(), err, "start http failed")
}
}
// Run .
func Run(c *cli.Context) error {
if err := initConfig(c); err != nil {
zerolog.Fatal().Err(err).Msg("invalid config")
return err
}
svcLog := &coretypes.ServerLogConfig{
Level: configs.Conf.Log.Level,
UseJSON: configs.Conf.Log.UseJSON,
Filename: configs.Conf.Log.Filename,
MaxSize: configs.Conf.Log.MaxSize,
MaxAge: configs.Conf.Log.MaxAge,
MaxBackups: configs.Conf.Log.MaxBackups,
}
if err := log.SetupLog(c.Context, svcLog, configs.Conf.Log.SentryDSN); err != nil {
zerolog.Fatal().Err(err).Msg("failed to setup log")
return err
}
// log config
dump, err := configs.Conf.Dump()
if err != nil {
log.Error(c.Context, err, "failed to dump config")
return errors.Wrap(err, "")
}
log.Infof(c.Context, "%s", dump)
// wait for unix signals and try to GracefulStop
ctx, cancel := signal.NotifyContext(c.Context, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
defer cancel()
br, err := boar.New(ctx, &configs.Conf, nil)
if err != nil {
log.Error(c.Context, err, "failed to create boar")
return err
}
defer br.Close()
if err := virt.Cleanup(); err != nil {
return errors.Wrap(err, "")
}
grpcSrv, err := grpcserver.New(&configs.Conf, br)
if err != nil {
log.Error(c.Context, err, "failed to create grpc server")
return err
}
errExitCh := make(chan struct{})
if configs.Conf.BindHTTPAddr != "" {
go startHTTPServer(configs.Conf.BindHTTPAddr)
}
go func() {
defer close(errExitCh)
if err := grpcSrv.Serve(); err != nil {
log.Error(c.Context, err, "failed to start grpc server")
metrics.IncrError()
}
}()
log.Info(c.Context, "[main] all servers are running")
select {
case <-ctx.Done():
log.Info(c.Context, "[main] interrupt by signal")
case <-errExitCh:
log.Warn(c.Context, "[main] server exit abnormally.")
}
grpcSrv.Stop(false)
return nil
}