/
discovery.go
71 lines (59 loc) · 1.59 KB
/
discovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"log"
"os"
"os/exec"
"time"
"github.com/coreos/go-etcd/etcd"
"github.com/deis/pkg/aboutme"
"github.com/deis/pkg/etcd/discovery"
)
var version = "DEV"
func main() {
log.Printf("Starting etcd-discovery boot version %s", version)
ip, err := aboutme.MyIP()
if err != nil {
log.Printf("Failed to start because could not get IP: %s", err)
os.Exit(321)
}
port := os.Getenv("DEIS_ETCD_CLIENT_PORT")
if port == "" {
port = "2381"
}
aurl := fmt.Sprintf("http://%s:%s", ip, port)
curl := fmt.Sprintf("http://%s:%s,http://localhost:%s", ip, port, port)
cmd := exec.Command("etcd", "-advertise-client-urls", aurl, "-listen-client-urls", curl)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
go func() {
err := cmd.Start()
if err != nil {
log.Printf("Failed to start etcd: %s", err)
os.Exit(2)
}
}()
// Give etcd time to start up.
log.Print("Etcd needs to start. Sleeping for 5 seconds...")
time.Sleep(5 * time.Second)
log.Print("Woke up.")
uuid, err := discovery.Token()
if err != nil {
log.Printf("Failed to read %s", discovery.TokenFile)
os.Exit(404)
}
size := os.Getenv("DEIS_ETCD_CLUSTER_SIZE")
if size == "" {
size = "3"
}
key := fmt.Sprintf(discovery.ClusterSizeKey, uuid)
cli := etcd.NewClient([]string{"http://localhost:2381"})
if _, err := cli.Create(key, size, 0); err != nil {
log.Printf("Failed to add key: %s", err)
}
log.Printf("etcd-discovery service secret is %s.", key)
log.Printf("etcd-discovery service is running on %s:%s.", ip, port)
if err := cmd.Wait(); err != nil {
log.Printf("Etcd stopped running: %s", err)
}
}