Skip to content

Commit 79479ca

Browse files
author
Matthew Fisher
committed
ref(router): shift things around
This follows Docker's "best practices" for structuring projects. This also simplifies some of the build process by building everything in one `docker build` rather than compiling nginx, then injecting it into another chained `docker build`. The image size remains the same. This is the same as what occurred over in builder a few months ago. This also removes the log tailing feature to stdout in boot.go by using symbolic links to /dev/stdout and /dev/stderr as is expected. git.log is nonexistent and unused.
1 parent d3a333f commit 79479ca

28 files changed

Lines changed: 78 additions & 115 deletions

router/Dockerfile

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
11
FROM alpine:3.1
22

33
# install common packages
4-
RUN apk add --update-cache curl bash sudo && rm -rf /var/cache/apk/*
4+
RUN apk add --update-cache \
5+
bash \
6+
curl \
7+
geoip \
8+
libssl1.0 \
9+
openssl \
10+
pcre \
11+
sudo \
12+
&& rm -rf /var/cache/apk/*
513

6-
ADD firewall /tmp/firewall
14+
# install confd
15+
RUN curl -sSL -o /usr/local/bin/confd https://s3-us-west-2.amazonaws.com/opdemand/confd-git-73f7489 \
16+
&& chmod +x /usr/local/bin/confd
717

8-
ADD build.sh /tmp/build.sh
18+
# add nginx user
19+
RUN addgroup -S nginx && \
20+
adduser -S -G nginx -H -h /opt/nginx -s /sbin/nologin -D nginx
921

10-
WORKDIR /tmp
22+
COPY rootfs /
1123

12-
RUN mkdir -p /opt/nginx && DOCKER_BUILD=true /tmp/build.sh
24+
# forward request and error logs to docker
25+
RUN ln -sf /dev/stdout /opt/nginx/logs/access.log
26+
RUN ln -sf /dev/stderr /opt/nginx/logs/error.log
1327

14-
RUN tar -C /opt/nginx -czf /nginx.tgz .
28+
# compile nginx from source
29+
RUN build
30+
31+
CMD ["boot"]
32+
EXPOSE 80 2222
33+
34+
ENV DEIS_RELEASE 1.10.0-dev

router/Makefile

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,22 @@ include ../includes.mk
44
repo_path = github.com/deis/deis/router
55

66
GO_FILES = $(wildcard *.go)
7-
GO_PACKAGES = logger tests
7+
GO_PACKAGES = cmd/boot logger tests
88
GO_PACKAGES_REPO_PATH = $(addprefix $(repo_path)/,$(GO_PACKAGES))
99

10-
SHELL_SCRIPTS = $(shell find "." -name '*.sh') $(wildcard image/bin/*)
10+
SHELL_SCRIPTS = $(shell find "." -name '*.sh') $(wildcard rootfs/bin/*)
1111

1212
COMPONENT = $(notdir $(repo_path))
1313
IMAGE = $(IMAGE_PREFIX)$(COMPONENT):$(BUILD_TAG)
1414
DEV_IMAGE = $(REGISTRY)$(IMAGE)
1515
BUILD_IMAGE = $(COMPONENT)-build
16-
BINARY_DEST_DIR = image/bin
16+
BINARY_DEST_DIR = rootfs/bin
1717

1818
build: check-docker
19-
docker build -t $(BUILD_IMAGE) .
20-
docker cp `docker run -d $(BUILD_IMAGE) /bin/bash`:/nginx.tgz image/
21-
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 godep go build -a -installsuffix -v -ldflags '-s' -o $(BINARY_DEST_DIR)/boot boot.go || exit 1
22-
@$(call check-static-binary,image/bin/boot)
23-
docker build -t $(IMAGE) image
24-
rm image/nginx.tgz
25-
rm image/bin/boot
19+
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 godep go build -a -installsuffix -v -ldflags '-s' -o $(BINARY_DEST_DIR)/boot cmd/boot/boot.go || exit 1
20+
@$(call check-static-binary,rootfs/bin/boot)
21+
docker build -t $(IMAGE) .
22+
rm rootfs/bin/boot
2623

2724
clean: check-docker check-registry
2825
docker rmi $(IMAGE)
@@ -70,7 +67,7 @@ test-style:
7067
# display output, then check
7168
$(GOFMT) $(GO_PACKAGES) $(GO_FILES)
7269
@$(GOFMT) $(GO_PACKAGES) $(GO_FILES) | read; if [ $$? == 0 ]; then echo "gofmt check failed."; exit 1; fi
73-
$(GOVET) $(repo_path) $(GO_PACKAGES_REPO_PATH)
70+
$(GOVET) $(GO_PACKAGES_REPO_PATH)
7471
$(GOLINT) ./...
7572
shellcheck $(SHELL_SCRIPTS)
7673

router/boot.go renamed to router/cmd/boot/boot.go

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"syscall"
1212
"time"
1313

14-
"github.com/ActiveState/tail"
1514
"github.com/Sirupsen/logrus"
1615
"github.com/coreos/go-etcd/etcd"
1716

@@ -21,11 +20,8 @@ import (
2120
var log = logrus.New()
2221

2322
const (
24-
timeout time.Duration = 10 * time.Second
25-
ttl time.Duration = timeout * 2
26-
gitLogFile string = "/opt/nginx/logs/git.log"
27-
nginxAccessLog string = "/opt/nginx/logs/access.log"
28-
nginxErrorLog string = "/opt/nginx/logs/error.log"
23+
timeout time.Duration = 10 * time.Second
24+
ttl time.Duration = timeout * 2
2925
)
3026

3127
func main() {
@@ -65,10 +61,6 @@ func main() {
6561
setDefaultEtcd(client, etcdPath+"/gzip", "on")
6662

6763
log.Info("Starting Nginx...")
68-
// tail the logs
69-
go tailFile(nginxAccessLog)
70-
go tailFile(nginxErrorLog)
71-
go tailFile(gitLogFile)
7264

7365
nginxChan := make(chan bool)
7466
go launchNginx(nginxChan)
@@ -87,7 +79,6 @@ func main() {
8779

8880
exitChan := make(chan os.Signal, 2)
8981
signal.Notify(exitChan, syscall.SIGTERM, syscall.SIGINT)
90-
go cleanOnExit(exitChan)
9182
<-exitChan
9283
}
9384

@@ -106,19 +97,13 @@ func launchCron() {
10697
}
10798
}
10899

109-
func cleanOnExit(exitChan chan os.Signal) {
110-
for _ = range exitChan {
111-
tail.Cleanup()
112-
}
113-
}
114-
115100
// Wait until the compilation of the templates
116101
func waitForInitialConfd(etcd string, timeout time.Duration) {
117102
for {
118103
var buffer bytes.Buffer
119104
output := bufio.NewWriter(&buffer)
120105
log.Debugf("Connecting to etcd server %s", etcd)
121-
cmd := exec.Command("confd", "-node", etcd, "--confdir", "/app", "-onetime", "--log-level", "error")
106+
cmd := exec.Command("confd", "-node", etcd, "-onetime", "--log-level", "error")
122107
cmd.Stdout = output
123108
cmd.Stderr = output
124109

@@ -135,7 +120,7 @@ func waitForInitialConfd(etcd string, timeout time.Duration) {
135120
}
136121

137122
func launchConfd(etcd string) {
138-
cmd := exec.Command("confd", "-node", etcd, "--confdir", "/app", "--log-level", "error", "--interval", "5")
123+
cmd := exec.Command("confd", "-node", etcd, "--log-level", "error", "--interval", "5")
139124
cmd.Stdout = os.Stdout
140125
cmd.Stderr = os.Stderr
141126

@@ -169,15 +154,6 @@ func launchNginx(nginxChan chan bool) {
169154
}
170155
}
171156

172-
func tailFile(path string) {
173-
mkfifo(path)
174-
t, _ := tail.TailFile(path, tail.Config{Follow: true})
175-
176-
for line := range t.Lines {
177-
log.Info(line.Text)
178-
}
179-
}
180-
181157
func mkfifo(path string) {
182158
os.Remove(path)
183159
if err := syscall.Mkfifo(path, syscall.S_IFIFO|0666); err != nil {

router/image/Dockerfile

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
#!/usr/bin/env bash
22

3-
# fail on any command exiting non-zero
4-
set -eo pipefail
3+
set -eof pipefail
54

6-
if [[ -z $DOCKER_BUILD ]]; then
7-
echo
8-
echo "Note: this script is intended for use by the Dockerfile and not as a way to build the router locally"
9-
echo
10-
exit 1
11-
fi
12-
13-
function get_src {
14-
hash="$1"
15-
url="$2"
16-
f=$(basename "$url")
17-
18-
curl -sSL "$url" -o "$f"
19-
echo "$hash $f" | sha256sum -c - || exit 10
20-
tar xzf "$f"
21-
rm "$f"
22-
}
23-
24-
export VERSION_NGINX=nginx-1.9.3
25-
export VERSION_NAXSI=0d53a64ed856e694fcb4038748c8cf6d5551a603
26-
export VERSION_NDK=0.2.19
27-
export VERSION_SETMISC=0.29
5+
export NGINX_VERSION=1.9.3
6+
export NAXSI_VERSION=0d53a64ed856e694fcb4038748c8cf6d5551a603
7+
export NDK_VERSION=0.2.19
8+
export VTS_VERSION=22c51e201a550bb94e96239fef541347beb4eeca
9+
export SETMISC_VERSION=0.29
2810

2911
export BUILD_PATH=/tmp/build
3012

@@ -52,23 +34,23 @@ apk add --update-cache \
5234

5335
# download, verify and extract the source files
5436
get_src 4298c5341b2a262fdb8dbc0a1389756181af8f098c7720abfb30bd3060f673eb \
55-
"http://nginx.org/download/$VERSION_NGINX.tar.gz"
37+
"http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz"
5638

5739
get_src 128b56873eedbd3f240dc0f88a8b260d791321db92f14ba2fc5c49fc5307e04d \
58-
"https://github.com/nbs-system/naxsi/archive/$VERSION_NAXSI.tar.gz"
40+
"https://github.com/nbs-system/naxsi/archive/$NAXSI_VERSION.tar.gz"
5941

6042
get_src 501f299abdb81b992a980bda182e5de5a4b2b3e275fbf72ee34dd7ae84c4b679 \
61-
"https://github.com/simpl/ngx_devel_kit/archive/v$VERSION_NDK.tar.gz"
43+
"https://github.com/simpl/ngx_devel_kit/archive/v$NDK_VERSION.tar.gz"
6244

6345
get_src 8d280fc083420afb41dbe10df9a8ceec98f1d391bd2caa42ebae67d5bc9295d8 \
64-
"https://github.com/openresty/set-misc-nginx-module/archive/v$VERSION_SETMISC.tar.gz"
46+
"https://github.com/openresty/set-misc-nginx-module/archive/v$SETMISC_VERSION.tar.gz"
6547

6648
# replace with a release instead a commit once e06a618e5780b4b94b21cab37d61820dcd3bb585 is contained in one.
6749
get_src 47340cf0c711b10a0231fca9264d73ea791ff8420b823b0295ae3df2d968a205 \
68-
"https://github.com/vozlt/nginx-module-vts/archive/22c51e201a550bb94e96239fef541347beb4eeca.tar.gz"
50+
"https://github.com/vozlt/nginx-module-vts/archive/$VTS_VERSION.tar.gz"
6951

7052
# build nginx
71-
cd "$BUILD_PATH/$VERSION_NGINX"
53+
cd "$BUILD_PATH/nginx-$NGINX_VERSION"
7254

7355
./configure \
7456
--prefix="$PREFIX" \
@@ -89,10 +71,22 @@ cd "$BUILD_PATH/$VERSION_NGINX"
8971
--with-mail \
9072
--with-mail_ssl_module \
9173
--with-stream \
92-
--add-module="$BUILD_PATH/naxsi-$VERSION_NAXSI/naxsi_src" \
93-
--add-module="$BUILD_PATH/ngx_devel_kit-$VERSION_NDK" \
94-
--add-module="$BUILD_PATH/set-misc-nginx-module-$VERSION_SETMISC" \
95-
--add-module="$BUILD_PATH/nginx-module-vts-22c51e201a550bb94e96239fef541347beb4eeca" \
74+
--add-module="$BUILD_PATH/naxsi-$NAXSI_VERSION/naxsi_src" \
75+
--add-module="$BUILD_PATH/ngx_devel_kit-$NDK_VERSION" \
76+
--add-module="$BUILD_PATH/set-misc-nginx-module-$SETMISC_VERSION" \
77+
--add-module="$BUILD_PATH/nginx-module-vts-$VTS_VERSION" \
9678
&& make && make install
9779

98-
mv /tmp/firewall /opt/nginx/firewall
80+
rm -rf "$BUILD_PATH"
81+
apk del \
82+
build-base \
83+
curl \
84+
geoip-dev \
85+
libcrypto1.0 \
86+
libpcre32 \
87+
patch \
88+
pcre-dev \
89+
openssl-dev \
90+
zlib \
91+
zlib-dev
92+
rm -rf /var/cache/apk/*

router/rootfs/bin/get_src

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
hash="$1"
4+
url="$2"
5+
f=$(basename "$url")
6+
7+
curl -sSL "$url" -o "$f"
8+
echo "$hash $f" | sha256sum -c - || exit 10
9+
tar xzf "$f"
10+
rm "$f"
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)