Skip to content

Commit 525e5e4

Browse files
committed
ref(logger): just use go. remove bash script.
1 parent d9dd781 commit 525e5e4

6 files changed

Lines changed: 89 additions & 104 deletions

File tree

logger/Dockerfile

100644100755
Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,7 @@
1-
FROM ubuntu:14.04
1+
FROM deis/go:latest
22

3-
ENV DEBIAN_FRONTEND noninteractive
3+
WORKDIR /go/src/github.com/deis/deis/logger
4+
CMD /go/bin/syslogd
45

5-
# install common packages
6-
RUN apt-get update && apt-get install -y curl
7-
8-
# install etcdctl
9-
RUN curl -sSL -o /usr/local/bin/etcdctl https://s3-us-west-2.amazonaws.com/opdemand/etcdctl-v0.4.6 \
10-
&& chmod +x /usr/local/bin/etcdctl
11-
12-
# install confd
13-
RUN curl -sSL -o /usr/local/bin/confd https://s3-us-west-2.amazonaws.com/opdemand/confd-v0.5.0-json \
14-
&& chmod +x /usr/local/bin/confd
15-
16-
# install go runtime
17-
RUN curl -sSL https://storage.googleapis.com/golang/go1.3.1.linux-amd64.tar.gz | tar -C /usr/local -xz
18-
19-
# prepare go environment
20-
RUN mkdir -p /go
21-
ENV GOPATH /go
22-
ENV PATH /usr/local/bin:/usr/bin:/bin:/sbin:/usr/local/go/bin
23-
24-
# create /var/log/deis for holding logs (access via bind mount)
25-
RUN mkdir -p /var/log/deis
26-
27-
# prepare execution environment
28-
WORKDIR /app
29-
CMD ["/app/bin/boot"]
30-
EXPOSE 514
31-
32-
ADD . /app
336
ADD . /go/src/github.com/deis/deis/logger
34-
35-
# compile the binary
36-
RUN cd /go/src/github.com/deis/deis/logger/syslogd && go install -v .
7+
RUN CGO_ENABLED=0 go get -a -ldflags '-s' github.com/deis/deis/logger

logger/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ include ../includes.mk
22

33
COMPONENT = logger
44
IMAGE = $(IMAGE_PREFIX)$(COMPONENT):$(BUILD_TAG)
5+
BUILD_IMAGE := $(COMPONENT)-build
56
DEV_IMAGE = $(DEV_REGISTRY)/$(IMAGE)
67

78
build: check-docker
8-
docker build -t $(IMAGE) .
9+
docker build -t $(BUILD_IMAGE) .
10+
docker cp `docker run -d $(BUILD_IMAGE)`:/go/bin/logger image/bin/
11+
docker build -t $(IMAGE) image
12+
rm -rf image/bin/logger
913

1014
clean: check-docker check-registry
1115
docker rmi $(IMAGE)

logger/bin/boot

Lines changed: 0 additions & 59 deletions
This file was deleted.

logger/image/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM scratch
2+
3+
CMD ["/bin/logger"]
4+
EXPOSE 514
5+
6+
ADD . /

logger/main.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
"os/signal"
7+
"syscall"
8+
"time"
9+
10+
"github.com/coreos/go-etcd/etcd"
11+
12+
"github.com/deis/deis/logger/syslogd"
13+
)
14+
15+
const (
16+
timeout time.Duration = 10 * time.Second
17+
ttl time.Duration = timeout * 2
18+
)
19+
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")
25+
26+
externalPort := getopt("EXTERNAL_PORT", "514")
27+
28+
client := etcd.NewClient([]string{"http://" + host + ":" + etcdPort})
29+
30+
// Wait for terminating signal
31+
exitChan := make(chan os.Signal, 2)
32+
cleanupChan := make(chan bool)
33+
signal.Notify(exitChan, syscall.SIGTERM, syscall.SIGINT)
34+
35+
go syslogd.Listen(exitChan, cleanupChan)
36+
37+
go publishService(client, host, etcdPath, externalPort, uint64(ttl.Seconds()))
38+
39+
// Wait for the proper shutdown of the syslog server before exit
40+
<-cleanupChan
41+
}
42+
43+
func publishService(client *etcd.Client, host string, etcdPath string, externalPort string, ttl uint64) {
44+
for {
45+
setEtcd(client, etcdPath+"/host", host, ttl)
46+
setEtcd(client, etcdPath+"/port", externalPort, ttl)
47+
time.Sleep(timeout)
48+
}
49+
}
50+
51+
func setEtcd(client *etcd.Client, key, value string, ttl uint64) {
52+
_, err := client.Set(key, value, ttl)
53+
if err != nil {
54+
log.Println(err)
55+
}
56+
}
57+
58+
func getopt(name, dfault string) string {
59+
value := os.Getenv(name)
60+
if value == "" {
61+
value = dfault
62+
}
63+
return value
64+
}

logger/syslogd/syslogd.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
package main
1+
package syslogd
22

33
import (
44
"fmt"
55
"io"
66
"log"
77
"os"
8-
"os/signal"
98
"path"
109
"regexp"
11-
"syscall"
1210

1311
"github.com/deis/deis/logger/syslog"
1412
)
@@ -92,18 +90,19 @@ func (h *handler) mainLoop() {
9290
h.End()
9391
}
9492

95-
func main() {
93+
func Listen(signalChan chan os.Signal, cleanupDone chan bool) {
94+
fmt.Println("Starting syslog...")
9695
// Create a server with one handler and run one listen gorutine
9796
s := syslog.NewServer()
9897
s.AddHandler(newHandler())
9998
s.Listen("0.0.0.0:514")
99+
fmt.Println("Syslog server started...")
100100

101101
// Wait for terminating signal
102-
sc := make(chan os.Signal, 2)
103-
signal.Notify(sc, syscall.SIGTERM, syscall.SIGINT)
104-
<-sc
105-
106-
// Shutdown the server
107-
fmt.Println("Shutting down...")
108-
s.Shutdown()
102+
for _ = range signalChan {
103+
// Shutdown the server
104+
fmt.Println("Shutting down...")
105+
s.Shutdown()
106+
cleanupDone <- true
107+
}
109108
}

0 commit comments

Comments
 (0)