forked from deis/deis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswarm.go
115 lines (99 loc) · 2.63 KB
/
swarm.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"text/template"
"time"
"github.com/coreos/go-etcd/etcd"
"github.com/deis/deis/tests/utils"
)
// EtcdCluster information about the nodes in the etcd cluster
type EtcdCluster struct {
Members []etcd.Member `json:"members"`
}
// NodeStat information about the local node in etcd
type NodeStats struct {
LeaderInfo struct {
Name string `json:"leader"`
Uptime string `json:"uptime"`
StartTime time.Time `json:"startTime"`
} `json:"leaderInfo"`
}
const (
swarmpath = "/deis/scheduler/swarm/node"
swarmetcd = "/deis/scheduler/swarm/host"
etcdport = "4001"
timeout time.Duration = 3 * time.Second
ttl time.Duration = timeout * 2
)
func run(cmd string) {
var cmdBuf bytes.Buffer
tmpl := template.Must(template.New("cmd").Parse(cmd))
if err := tmpl.Execute(&cmdBuf, nil); err != nil {
log.Fatal(err)
}
cmdString := cmdBuf.String()
fmt.Println(cmdString)
var cmdl *exec.Cmd
cmdl = exec.Command("sh", "-c", cmdString)
if _, _, err := utils.RunCommandWithStdoutStderr(cmdl); err != nil {
log.Fatal(err)
} else {
fmt.Println("ok")
}
}
func getleaderHost() string {
var nodeStats NodeStats
client := &http.Client{}
resp, _ := client.Get("http://" + os.Getenv("HOST") + ":2379/v2/stats/self")
body, _ := ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &nodeStats)
etcdLeaderID := nodeStats.LeaderInfo.Name
var etcdCluster EtcdCluster
resp, _ = client.Get("http://" + os.Getenv("HOST") + ":2379/v2/members")
defer resp.Body.Close()
body, _ = ioutil.ReadAll(resp.Body)
json.Unmarshal(body, &etcdCluster)
for _, node := range etcdCluster.Members {
if node.ID == etcdLeaderID {
u, err := url.Parse(node.ClientURLs[0])
if err == nil {
return u.Host
}
}
}
return ""
}
func publishService(client *etcd.Client, host string, ttl uint64) {
for {
setEtcd(client, swarmetcd, host, ttl)
time.Sleep(timeout)
}
}
func setEtcd(client *etcd.Client, key, value string, ttl uint64) {
_, err := client.Set(key, value, ttl)
if err != nil && !strings.Contains(err.Error(), "Key already exists") {
log.Println(err)
}
}
func main() {
etcdproto := "etcd://" + getleaderHost() + swarmpath
etcdhost := os.Getenv("HOST")
addr := "--addr=" + etcdhost + ":2375"
client := etcd.NewClient([]string{"http://" + etcdhost + ":" + etcdport})
switch os.Args[1] {
case "join":
run("./deis-swarm join " + addr + " " + etcdproto)
case "manage":
go publishService(client, etcdhost, uint64(ttl.Seconds()))
run("./deis-swarm manage " + etcdproto)
}
}