2017-10-29 09:45:21 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/go-connections/nat"
|
2017-10-31 13:55:35 +00:00
|
|
|
"github.com/rancher/rke/docker"
|
2017-10-29 09:45:21 +00:00
|
|
|
"github.com/rancher/rke/hosts"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Etcd struct {
|
|
|
|
Version string `yaml:"version"`
|
|
|
|
Image string `yaml:"image"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunEtcdPlane(etcdHosts []hosts.Host, etcdService Etcd) error {
|
2017-10-31 13:55:35 +00:00
|
|
|
logrus.Infof("[%s] Building up Etcd Plane..", ETCDRole)
|
2017-10-29 09:45:21 +00:00
|
|
|
for _, host := range etcdHosts {
|
2017-10-31 13:55:35 +00:00
|
|
|
imageCfg, hostCfg := buildEtcdConfig(host, etcdService)
|
|
|
|
err := docker.DoRunContainer(imageCfg, hostCfg, EtcdContainerName, &host, ETCDRole)
|
2017-10-29 09:45:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-10-31 13:55:35 +00:00
|
|
|
logrus.Infof("[%s] Successfully started Etcd Plane..", ETCDRole)
|
2017-10-29 09:45:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-31 13:55:35 +00:00
|
|
|
func buildEtcdConfig(host hosts.Host, etcdService Etcd) (*container.Config, *container.HostConfig) {
|
2017-10-29 09:45:21 +00:00
|
|
|
imageCfg := &container.Config{
|
|
|
|
Image: etcdService.Image + ":" + etcdService.Version,
|
|
|
|
Cmd: []string{"/usr/local/bin/etcd",
|
|
|
|
"--name=etcd-" + host.Hostname,
|
|
|
|
"--data-dir=/etcd-data",
|
2017-10-31 11:39:21 +00:00
|
|
|
"--advertise-client-urls=http://" + host.ControlPlaneIP + ":2379,http://" + host.ControlPlaneIP + ":4001",
|
2017-10-29 09:45:21 +00:00
|
|
|
"--listen-client-urls=http://0.0.0.0:2379",
|
2017-10-31 11:39:21 +00:00
|
|
|
"--initial-advertise-peer-urls=http://" + host.ControlPlaneIP + ":2380",
|
2017-10-29 09:45:21 +00:00
|
|
|
"--listen-peer-urls=http://0.0.0.0:2380",
|
|
|
|
"--initial-cluster-token=etcd-cluster-1",
|
2017-10-31 11:39:21 +00:00
|
|
|
"--initial-cluster=etcd-" + host.Hostname + "=http://" + host.ControlPlaneIP + ":2380"},
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
|
|
|
hostCfg := &container.HostConfig{
|
|
|
|
RestartPolicy: container.RestartPolicy{Name: "always"},
|
|
|
|
Binds: []string{
|
|
|
|
"/var/lib/etcd:/etcd-data"},
|
|
|
|
PortBindings: nat.PortMap{
|
|
|
|
"2379/tcp": []nat.PortBinding{
|
|
|
|
{
|
|
|
|
HostIP: "0.0.0.0",
|
|
|
|
HostPort: "2379",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"2380/tcp": []nat.PortBinding{
|
|
|
|
{
|
|
|
|
HostIP: "0.0.0.0",
|
|
|
|
HostPort: "2380",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2017-10-31 13:55:35 +00:00
|
|
|
return imageCfg, hostCfg
|
2017-10-29 09:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getEtcdConnString(hosts []hosts.Host) string {
|
|
|
|
connString := ""
|
|
|
|
for i, host := range hosts {
|
2017-10-31 11:39:21 +00:00
|
|
|
connString += "http://" + host.ControlPlaneIP + ":2379"
|
2017-10-29 09:45:21 +00:00
|
|
|
if i < (len(hosts) - 1) {
|
|
|
|
connString += ","
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return connString
|
|
|
|
}
|