|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
4 | 6 | "log" |
5 | 7 | "os" |
6 | 8 | "os/signal" |
| 9 | + "strconv" |
7 | 10 | "syscall" |
8 | 11 | "time" |
9 | 12 |
|
10 | 13 | "github.com/coreos/go-etcd/etcd" |
11 | | - |
12 | 14 | "github.com/deis/deis/logger/syslogd" |
13 | 15 | ) |
14 | 16 |
|
15 | | -const ( |
16 | | - timeout time.Duration = 10 * time.Second |
17 | | - ttl time.Duration = timeout * 2 |
| 17 | +var ( |
| 18 | + logAddr string |
| 19 | + logPort int |
| 20 | + enablePublish bool |
| 21 | + publishHost string |
| 22 | + publishPath string |
| 23 | + publishPort string |
| 24 | + publishInterval int |
| 25 | + publishTTL int |
18 | 26 | ) |
19 | 27 |
|
20 | | -func main() { |
21 | | - host := getopt("HOST", "127.0.0.1") |
22 | | - |
23 | | - etcdPort := getopt("ETCD_PORT", "4001") |
24 | | - etcdPath := getopt("ETCD_PATH", "/deis/logs") |
| 28 | +func init() { |
| 29 | + flag.StringVar(&logAddr, "log-addr", "0.0.0.0", "bind address for the logger") |
| 30 | + flag.IntVar(&logPort, "log-port", 514, "bind port for the logger") |
| 31 | + flag.StringVar(&syslogd.LogRoot, "log-root", "/data/logs", "log path to store logs") |
| 32 | + flag.BoolVar(&enablePublish, "enable-publish", false, "enable publishing to service discovery") |
| 33 | + flag.StringVar(&publishHost, "publish-host", getopt("HOST", "127.0.0.1"), "service discovery hostname") |
| 34 | + flag.IntVar(&publishInterval, "publish-interval", 10, "publish interval in seconds") |
| 35 | + flag.StringVar(&publishPath, "publish-path", getopt("ETCD_PATH", "/deis/logs"), "path to publish host/port information") |
| 36 | + flag.StringVar(&publishPort, "publish-port", getopt("ETCD_PORT", "4001"), "service discovery port") |
| 37 | + flag.IntVar(&publishTTL, "publish-ttl", publishInterval*2, "publish TTL in seconds") |
| 38 | +} |
25 | 39 |
|
26 | | - externalPort := getopt("EXTERNAL_PORT", "514") |
| 40 | +func main() { |
| 41 | + flag.Parse() |
27 | 42 |
|
28 | | - client := etcd.NewClient([]string{"http://" + host + ":" + etcdPort}) |
| 43 | + client := etcd.NewClient([]string{"http://" + publishHost + ":" + publishPort}) |
29 | 44 |
|
30 | 45 | // Wait for terminating signal |
31 | 46 | exitChan := make(chan os.Signal, 2) |
32 | 47 | cleanupChan := make(chan bool) |
33 | 48 | signal.Notify(exitChan, syscall.SIGTERM, syscall.SIGINT) |
34 | 49 |
|
35 | | - go syslogd.Listen(exitChan, cleanupChan) |
| 50 | + go syslogd.Listen(exitChan, cleanupChan, fmt.Sprintf("%s:%d", logAddr, logPort)) |
36 | 51 |
|
37 | | - go publishService(client, host, etcdPath, externalPort, uint64(ttl.Seconds())) |
| 52 | + if enablePublish { |
| 53 | + go publishService(client, publishHost, publishPath, strconv.Itoa(logPort), uint64(time.Duration(publishTTL).Seconds())) |
| 54 | + } |
38 | 55 |
|
39 | 56 | // Wait for the proper shutdown of the syslog server before exit |
40 | 57 | <-cleanupChan |
41 | 58 | } |
42 | 59 |
|
43 | | -func publishService(client *etcd.Client, host string, etcdPath string, externalPort string, ttl uint64) { |
| 60 | +func publishService(client *etcd.Client, host string, etcdPath string, port string, ttl uint64) { |
44 | 61 | for { |
45 | 62 | setEtcd(client, etcdPath+"/host", host, ttl) |
46 | | - setEtcd(client, etcdPath+"/port", externalPort, ttl) |
47 | | - time.Sleep(timeout) |
| 63 | + setEtcd(client, etcdPath+"/port", port, ttl) |
| 64 | + time.Sleep(time.Duration(publishInterval)) |
48 | 65 | } |
49 | 66 | } |
50 | 67 |
|
|
0 commit comments