This repository has been archived by the owner on Jan 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
etcd.go
64 lines (56 loc) · 1.63 KB
/
etcd.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
package ssh
import (
"strings"
"github.com/juju/errgo"
)
// GetEtcdDiscoveryUrl connects to the given host and extracts the used etcd
// discovery URL.
func GetEtcdDiscoveryUrl(host string) (string, error) {
uuid, err := RunRemoteCommand(host, "systemctl cat etcd2 | grep ETCD_DISCOVERY |grep -oE 'http.*/[a-z0-9A-Z]+'")
return uuid, errgo.Mask(err)
}
// GetEtcd2MemberName connects to the given host and returns the name of the given host in the etcd2 quorum.
// This assumes the member has "name=<machine-id>" set.
func GetEtcd2MemberName(host string) (string, error) {
cmd := "etcdctl member list | fgrep \"name=$(cat /etc/machine-id)\" | cut -d: -f1"
if name, err := RunRemoteCommand(host, cmd); err != nil {
return "", errgo.Mask(err)
} else {
return name, nil
}
}
// RemoveFromEtcd connects to the given host and removes it from the etcd discovery.
//
// Utilizes GetMachineID()
func RemoveFromEtcd(host string) error {
// Dirty: Remove the machine from the etcd cluster by running a HTTP DELETE
// request from the machine we want to kill
cmd := []string{
"etcdctl",
"member",
"remove",
"$(etcdctl member list | fgrep \"name=$(cat /etc/machine-id)\" | cut -d: -f1)",
}
if _, err := RunRemoteCommand(host, strings.Join(cmd, " ")); err != nil {
return errgo.Mask(err)
}
return nil
}
// StopEtcd connects to the given host and stops the etcd2 daemon running.
func StopEtcd(host string) error {
cmd := []string{
"sudo",
"systemctl",
"stop",
"etcd",
"&&",
"sudo",
"systemctl",
"disable",
"etcd",
}
if _, err := RunRemoteCommand(host, strings.Join(cmd, " ")); err != nil {
return errgo.Mask(err)
}
return nil
}