forked from projecteru2/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.go
143 lines (132 loc) · 3.36 KB
/
agent.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
package main
import (
"os"
log "github.com/Sirupsen/logrus"
"github.com/projecteru2/agent/api"
"github.com/projecteru2/agent/common"
"github.com/projecteru2/agent/engine"
"github.com/projecteru2/agent/types"
"github.com/projecteru2/agent/utils"
"github.com/projecteru2/agent/watcher"
"gopkg.in/urfave/cli.v2"
)
func setupLogLevel(l string) error {
level, err := log.ParseLevel(l)
if err != nil {
return err
}
log.SetLevel(level)
return nil
}
func initConfig(c *cli.Context) *types.Config {
config := &types.Config{}
err := config.LoadConfigFromFile(c.String("config"))
if err != nil {
log.Warnf("[main] load config failed %v", err)
}
config.PrepareConfig(c)
return config
}
func serve(c *cli.Context) error {
if err := setupLogLevel(c.String("log-level")); err != nil {
log.Fatal(err)
}
config := initConfig(c)
log.Debugf("[config] %v", config)
utils.WritePid(config.PidFile)
defer os.Remove(config.PidFile)
watcher.InitMonitor()
go watcher.LogMonitor.Serve()
agent, err := engine.NewEngine(config)
if err != nil {
log.Fatal(err)
}
go api.Serve(config.API.Addr)
if err := agent.Run(); err != nil {
log.Fatalf("Agent caught error %s", err)
}
return nil
}
func main() {
app := &cli.App{
Name: "Eru-Agent",
Usage: "Run eru agent",
Version: common.ERU_AGENT_VERSION,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Value: "/etc/eru/agent.yaml",
Usage: "config file path for agent, in yaml",
EnvVars: []string{"ERU_AGENT_CONFIG_PATH"},
},
&cli.StringFlag{
Name: "log-level",
Value: "INFO",
Usage: "set log level",
EnvVars: []string{"ERU_AGENT_LOG_LEVEL"},
},
&cli.StringFlag{
Name: "core-endpoint",
Value: "",
Usage: "core endpoint",
EnvVars: []string{"ERU_AGENT_CORE_ENDPOINT"},
},
&cli.StringFlag{
Name: "docker-endpoint",
Value: "",
Usage: "docker endpoint",
EnvVars: []string{"ERU_AGENT_DOCKER_ENDPOINT"},
},
&cli.Int64Flag{
Name: "metrics-step",
Value: 0,
Usage: "interval for metrics to send",
EnvVars: []string{"ERU_AGENT_METRICS_STEP"},
},
&cli.StringSliceFlag{
Name: "metrics-transfers",
Value: &cli.StringSlice{},
Usage: "metrics destinations",
EnvVars: []string{"ERU_AGENT_METRICS_TRANSFERS"},
},
&cli.StringFlag{
Name: "api-addr",
Value: "",
Usage: "agent API serving address",
EnvVars: []string{"ERU_AGENT_API_ADDR"},
},
&cli.StringSliceFlag{
Name: "log-forwards",
Value: &cli.StringSlice{},
Usage: "log destinations",
EnvVars: []string{"ERU_AGENT_LOG_FORWARDS"},
},
&cli.StringFlag{
Name: "log-stdout",
Value: "",
Usage: "forward stdout out? yes/no",
EnvVars: []string{"ERU_AGENT_LOG_STDOUT"},
},
&cli.StringFlag{
Name: "pidfile",
Value: "",
Usage: "pidfile to save",
EnvVars: []string{"ERU_AGENT_PIDFILE"},
},
&cli.IntFlag{
Name: "health-check-interval",
Value: 0,
Usage: "interval for agent to check container's health status",
EnvVars: []string{"ERU_AGENT_HEALTH_CHECK_INTERVAL"},
},
&cli.IntFlag{
Name: "health-check-timeout",
Value: 0,
Usage: "timeout for agent to check container's health status",
EnvVars: []string{"ERU_AGENT_HEALTH_CHECK_TIMEOUT"},
},
},
Action: serve,
}
app.Run(os.Args)
}