Skip to content

Commit d6fe180

Browse files
Merge pull request #338 from gliderlabs/mh/fix/strip-hostname
strip \r and \n when reading the file /etc/host_hostname
2 parents 95b284a + 00de35e commit d6fe180

File tree

6 files changed

+31
-10
lines changed

6 files changed

+31
-10
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM alpine:3.5
1+
FROM alpine:3.6
22
ENTRYPOINT ["/bin/logspout"]
33
VOLUME /mnt/routes
44
EXPOSE 80

Dockerfile.dev

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM alpine:3.5
1+
FROM alpine:3.6
22
VOLUME /mnt/routes
33
EXPOSE 80
44

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ test: build-dev
4949
-v /var/run/docker.sock:/var/run/docker.sock \
5050
-v $(PWD):/go/src/github.com/gliderlabs/logspout \
5151
-e TEST_ARGS="" \
52+
-e DEBUG=$(DEBUG) \
5253
$(NAME):dev make -e test-direct
5354

5455
test-direct:

adapters/syslog/syslog.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net"
1111
"os"
1212
"strconv"
13+
"strings"
1314
"syscall"
1415
"text/template"
1516
"time"
@@ -55,6 +56,16 @@ func debug(v ...interface{}) {
5556
}
5657
}
5758

59+
func getHostname() string {
60+
content, err := ioutil.ReadFile("/etc/host_hostname")
61+
if err == nil && len(content) > 0 {
62+
hostname = strings.TrimRight(string(content), "\r\n")
63+
} else {
64+
hostname = getopt("SYSLOG_HOSTNAME", "{{.Container.Config.Hostname}}")
65+
}
66+
return hostname
67+
}
68+
5869
// NewSyslogAdapter returnas a configured syslog.Adapter
5970
func NewSyslogAdapter(route *router.Route) (router.LogAdapter, error) {
6071
transport, found := router.AdapterTransports.Lookup(route.AdapterTransport("udp"))
@@ -69,13 +80,8 @@ func NewSyslogAdapter(route *router.Route) (router.LogAdapter, error) {
6980
format := getopt("SYSLOG_FORMAT", "rfc5424")
7081
priority := getopt("SYSLOG_PRIORITY", "{{.Priority}}")
7182
pid := getopt("SYSLOG_PID", "{{.Container.State.Pid}}")
83+
hostname = getHostname()
7284

73-
content, err := ioutil.ReadFile("/etc/host_hostname") // just pass the file name
74-
if err == nil && len(content) > 0{
75-
hostname = string(content) // convert content to a 'string'
76-
} else {
77-
hostname = getopt("SYSLOG_HOSTNAME", "{{.Container.Config.Hostname}}")
78-
}
7985
tag := getopt("SYSLOG_TAG", "{{.ContainerName}}"+route.Options["append_tag"])
8086
structuredData := getopt("SYSLOG_STRUCTURED_DATA", "")
8187
if route.Options["structured_data"] != "" {

adapters/syslog/syslog_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import (
44
"bufio"
55
"fmt"
66
"io"
7+
"io/ioutil"
78
"log"
89
"net"
910
"os"
1011
"strconv"
12+
"strings"
1113
"sync"
1214
"testing"
1315
"text/template"
@@ -41,6 +43,9 @@ var (
4143
}
4244
testTmplStr = fmt.Sprintf("<%s>%s %s %s[%s]: %s\n",
4345
testPriority, testTimestamp, testHostname, testTag, testPid, testData)
46+
hostHostnameFilename = "/etc/host_hostname"
47+
hostnameContent = "hostname"
48+
badHostnameContent = "hostname\r\n"
4449
)
4550

4651
func TestSyslogRetryCount(t *testing.T) {
@@ -102,6 +107,16 @@ func TestSyslogReconnectOnClose(t *testing.T) {
102107
}
103108
}
104109

110+
func TestHostnameDoesNotHaveLineFeed(t *testing.T) {
111+
if err := ioutil.WriteFile(hostHostnameFilename, []byte(badHostnameContent), 0777); err != nil {
112+
t.Fatal(err)
113+
}
114+
testHostname := getHostname()
115+
if strings.Contains(testHostname, badHostnameContent) {
116+
t.Errorf("expected hostname to be %s. got %s in hostname %s", hostnameContent, badHostnameContent, testHostname)
117+
}
118+
}
119+
105120
func startServer(n, la string, done chan<- string) (addr string, sock io.Closer, wg *sync.WaitGroup) {
106121
if n == "udp" || n == "tcp" {
107122
la = "127.0.0.1:0"

circle.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ version: 2
22
jobs:
33
build:
44
machine: true
5-
working_directory: ~/.go_workspace/src/github.com/gliderlabs/
5+
working_directory: ~/.go_workspace/src/github.com/gliderlabs/logspout
66
environment:
77
DEBUG: true
8-
GO_PROJECT: ../go/src/github.com/gliderlabs/$CIRCLE_PROJECT_REPONAME
98
steps:
109
- checkout
1110
- run: |

0 commit comments

Comments
 (0)