Skip to content

Commit 0dd86da

Browse files
committed
ref(cache): just use go. remove bash script
1 parent b62454c commit 0dd86da

6 files changed

Lines changed: 128 additions & 98 deletions

File tree

cache/Dockerfile

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,8 @@
11
FROM ubuntu-debootstrap:14.04
22

3-
ENV DEBIAN_FRONTEND noninteractive
4-
5-
# install common packages
6-
RUN apt-get update && apt-get install -y curl net-tools sudo
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
3+
ADD . /app
114

12-
# build Redis from source
13-
RUN buildDeps='gcc libc6-dev make'; \
14-
set -x; \
15-
apt-get update && apt-get install -y $buildDeps net-tools --no-install-recommends \
16-
&& rm -rf /var/lib/apt/lists/* \
17-
&& mkdir -p /usr/src/redis \
18-
&& curl -sSL http://download.redis.io/releases/redis-2.8.15.tar.gz -o redis.tar.gz \
19-
&& echo "afc0d753cea68a26038775df2dea75a76e3d0e1d *redis.tar.gz" | sha1sum -c - \
20-
&& tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1 \
21-
&& make -C /usr/src/redis \
22-
&& make -C /usr/src/redis install \
23-
&& rm -r redis.tar.gz /usr/src/redis \
24-
&& apt-get purge -y $buildDeps \
25-
&& apt-get autoremove -y \
26-
&& apt-get clean
5+
EXPOSE 6379
276

287
WORKDIR /app
298
CMD ["/app/bin/boot"]
30-
EXPOSE 6379
31-
ADD . /app
32-
ADD redis.conf /etc/redis/redis.conf

cache/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ include ../includes.mk
33
COMPONENT = cache
44
IMAGE = $(IMAGE_PREFIX)$(COMPONENT):$(BUILD_TAG)
55
DEV_IMAGE = $(DEV_REGISTRY)/$(IMAGE)
6+
BUILD_IMAGE := $(COMPONENT)-build
67

78
build: check-docker
9+
docker build -t $(BUILD_IMAGE) image
10+
docker cp `docker run -d $(BUILD_IMAGE)`:/usr/local/bin/redis-server bin/
11+
docker cp `docker run -d $(BUILD_IMAGE)`:/go/bin/boot bin/
812
docker build -t $(IMAGE) .
13+
rm bin/boot
14+
rm bin/redis-server
915

1016
clean: check-docker check-registry
1117
docker rmi $(IMAGE)

cache/bin/boot

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

cache/image/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM golang:1.3
2+
3+
ADD . /go/src/github.com/deis/deis/boot
4+
5+
RUN DOCKER_BUILD=true /go/src/github.com/deis/deis/boot/build.sh
6+
7+
WORKDIR /go/src/github.com/deis/deis/boot
8+
9+
RUN CGO_ENABLED=0 go get -a -ldflags '-s' github.com/deis/deis/boot

cache/image/build.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
if [[ -z $DOCKER_BUILD ]]; then
4+
echo
5+
echo "Note: this script is intended for use by the Dockerfile and not as a way to build the cache locally"
6+
echo
7+
exit 1
8+
fi
9+
10+
# redis to use
11+
REDIS=2.8.17
12+
SHA=913479f9d2a283bfaadd1444e17e7bab560e5d1e
13+
14+
cd /tmp
15+
16+
curl -sSL http://download.redis.io/releases/redis-$REDIS.tar.gz -o redis.tar.gz
17+
18+
echo "$SHA *redis.tar.gz" | sha1sum -c -
19+
mkdir /usr/src/redis
20+
tar -xzf redis.tar.gz -C /usr/src/redis --strip-components=1
21+
make -C /usr/src/redis
22+
make -C /usr/src/redis install

cache/image/main.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net"
6+
"os"
7+
"os/exec"
8+
"os/signal"
9+
"syscall"
10+
"time"
11+
12+
"github.com/coreos/go-etcd/etcd"
13+
)
14+
15+
const (
16+
timeout time.Duration = 10 * time.Second
17+
ttl time.Duration = timeout * 2
18+
redisWait time.Duration = 5 * time.Second
19+
)
20+
21+
func main() {
22+
host := getopt("HOST", "127.0.0.1")
23+
24+
etcdPort := getopt("ETCD_PORT", "4001")
25+
etcdPath := getopt("ETCD_PATH", "/deis/cache")
26+
27+
externalPort := getopt("EXTERNAL_PORT", "6379")
28+
29+
client := etcd.NewClient([]string{"http://" + host + ":" + etcdPort})
30+
31+
go launchRedis()
32+
33+
go publishService(client, host, etcdPath, externalPort, uint64(ttl.Seconds()))
34+
35+
// Wait for terminating signal
36+
exitChan := make(chan os.Signal, 2)
37+
signal.Notify(exitChan, syscall.SIGTERM, syscall.SIGINT)
38+
<-exitChan
39+
}
40+
41+
func launchRedis() {
42+
cmd := exec.Command("/app/bin/redis-server", "/app/redis.conf")
43+
cmd.Stdout = os.Stdout
44+
cmd.Stderr = os.Stderr
45+
46+
err := cmd.Start()
47+
48+
if err != nil {
49+
log.Printf("Error starting Redis: %v", err)
50+
os.Exit(1)
51+
}
52+
53+
// Wait until the redis server is available
54+
for {
55+
_, err := net.DialTimeout("tcp", "127.0.0.1:6379", redisWait)
56+
if err == nil {
57+
log.Println("deis-cache running...")
58+
break
59+
}
60+
}
61+
62+
err = cmd.Wait()
63+
log.Printf("Redis finished by error: %v", err)
64+
}
65+
66+
func publishService(client *etcd.Client, host string, etcdPath string,
67+
externalPort string, ttl uint64) {
68+
69+
for {
70+
setEtcd(client, etcdPath+"/host", host, ttl)
71+
setEtcd(client, etcdPath+"/port", externalPort, ttl)
72+
time.Sleep(timeout)
73+
}
74+
}
75+
76+
func setEtcd(client *etcd.Client, key, value string, ttl uint64) {
77+
_, err := client.Set(key, value, ttl)
78+
if err != nil {
79+
log.Println(err)
80+
}
81+
}
82+
83+
func getopt(name, dfault string) string {
84+
value := os.Getenv(name)
85+
if value == "" {
86+
value = dfault
87+
}
88+
return value
89+
}

0 commit comments

Comments
 (0)