-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathetcdScale.go
More file actions
247 lines (197 loc) · 5.43 KB
/
Copy pathetcdScale.go
File metadata and controls
247 lines (197 loc) · 5.43 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"reflect"
"strings"
"time"
"github.com/cloudbec/etcdScale/machine"
"github.com/coreos/go-etcd/etcd"
)
// http://blog.gopheracademy.com/advent-2013/day-06-service-discovery-with-etcd/
func main() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
log.Printf("captured %v, stopping profiler and exiting..", sig)
os.Exit(1)
}
}()
// used for test
// os.Setenv("etcd_discovery", "testxs")
filename := ".env_etcd"
if len(os.Getenv("etcd_discovery")) > 5 {
if _, err := os.Stat(filename); os.IsNotExist(err) {
fmt.Printf("no such file or directory: %s", filename)
return
}
}
// binary, lookErr := exec.LookPath("etcd")
// if lookErr != nil {
// panic(lookErr)
// }
// // args := []string{"ls", "-a", "-l", "-h"}
// args := []string{"ls,"}
// env := os.Environ()
// execErr := syscall.Exec(binary, args, env)
// if execErr != nil {
// panic(execErr)
// }
client := etcd.NewClient([]string{"http://172.17.42.1:4002"})
clusterEtcdlib := client.GetCluster()
// fmt.Print("this cluster ")
// fmt.Println(cluster_etcdlib[0])
// client
// getEtcdCtlMemberAPI("infra5")
// curl http://172.17.42.1:4002/v2/members -XPOST -H "Content-Type: application/json" -d '{"peerURLs":["http://10.0.0.10:2380"]}'
// postData := `{"name":"infra5", "peerURLs":["http://172.17.42.1:4005"] }
var thismachine machine.Machine
thismachine.Port = 4005
thismachine.ClusterURL = clusterEtcdlib[0]
currentmachineip, err := externalIPFromIf("eth0")
thismachine.Clusterip = net.ParseIP(clusterEtcdlib[0]) // net.ParseIP(cluster[0])
thismachine.ClusterPort = 4002
thismachine.Peerip = currentmachineip
// machine.addetcdmember()
member, err := thismachine.Addetcdmember()
if err != nil {
log.Println(err)
}
// https: //github.com/mitchellh/mapstructurehttps://github.com/mitchellh/mapstructurehttps://github.com/mitchellh/mapstructurehttps://github.com/mitchellh/mapstructurehttps://github.com/mitchellh/mapstructurehttps://github.com/mitchellh/mapstructurehttps://github.com/mitchellh/mapstructure
fmt.Println(reflect.TypeOf(member))
fmt.Println("machine: ", member.ID)
fmt.Println("message: ", err)
// resp, err := client.Get("creds", false, false)
// if err != nil {
// log.Fatal(err)
// }
// fmt.Print("TEST")
// log.Printf("Current creds: %s: %s\n", resp.Node.Key, resp.Node.Value)
// for {
// watchChan := make(chan *etcd.Response)
// loopWatch(client, "/creds", watchChan)
// }
// then
// cat > /data/.env_etcd << EOF
// $ETCD_DISCOVERY
// EOF
// fi
}
func loopWatch(client *etcd.Client, key string, watch chan *etcd.Response) {
time.Sleep(1000)
go client.Watch(key, 0, false, watch, nil)
fmt.Println("test")
log.Println("Waiting for an update...")
r := <-watch
log.Printf("Got updated creds: %s: %s\n", r.Node.Key, r.Node.Value)
}
func externalIPFromIf(ipv4Iface string) (net.IP, error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, iface := range ifaces {
// fmt.Print("nom de l'interface : \t")
// fmt.Print(iface.Name)
if strings.EqualFold(iface.Name, ipv4Iface) {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip, err
}
}
}
return nil, errors.New("are you connected to the network?")
}
func printCommand(cmd *exec.Cmd) {
fmt.Printf("==> Executing: %s\n", strings.Join(cmd.Args, " "))
}
func printError(err error) {
if err != nil {
fmt.Print("error!!!")
os.Stderr.WriteString(fmt.Sprintf("==> Error: %s\n", err.Error()))
}
}
func printOutput(outs []byte) {
if len(outs) > 0 {
fmt.Printf("==> Output: %s\n", string(outs))
}
}
func getEtcdMemberAPI() {
url := "http://172.17.42.1:4002/v2/members"
httpClient := &http.Client{}
//req, err := http.NewRequest("POST", url, strings.NewReader(postData))
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatalln(err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
var f interface{}
body, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(body, &f)
m := f.(map[string]interface{})
for k, v := range m {
switch vv := v.(type) {
case string:
fmt.Println(k, "is string", vv)
case int:
fmt.Println(k, "is int", vv)
case []interface{}:
fmt.Println(k, "is an array:")
for i, u := range vv {
fmt.Println(i, u)
}
default:
fmt.Println(k, "is of a type I don't know how to handle")
}
}
//fmt.Println(string(body))
}
func getEtcdCtlMemberAPI(machineName string) {
// machineName := "infra5"
cmd := exec.Command("etcdctl", "-C", "http://172.17.42.1:4002", "member", "add", machineName, "http://172.17.42.1:4005")
printCommand(cmd)
printMachineStatus(cmd.CombinedOutput())
}
func printMachineStatus(stdOut []byte, stdErr error) {
if strings.Contains(string(stdOut), "Added member named "+"infra5") {
printOutput(
stdOut,
)
} else {
printError(stdErr)
}
}