Example #1
0
func (sw Swarm) Initialize(servers []string) (err error) {

	var clusterContainers []*engine.Container
	var engines []docker.Docker

	// Ensure the cluster is in Store
	if err = sw.Store.Write("/cluster", "", ""); err != nil {
		return err
	}

	// Nodes
	for _, server := range servers {

		// Connect to Docker
		log.Infoln("Connecting to Docker on:", server)
		eng, err := docker.New(server, "")
		engines = append(engines, eng)
		if err != nil {
			return err
		}

		// Agent
		if _, err = sw.agent(server, eng); err != nil {
			return err
		}
	}

	// Manager
	var swarmManager *engine.Container
	for _, container := range clusterContainers {
		if container.Name == "swarm_manager" {
			swarmManager = container
			break
		}
	}
	if swarmManager == nil {
		i := utils.PickServer(servers)
		if swarmManager, err = sw.manager(servers[i], engines[i]); err != nil {
			return
		}
	}

	// Connect manager
	log.Infoln("Connecting to Swarm manager on:", "tcp://"+swarmManager.Addr())
	for i := 0; i < 3; i++ {
		// We have to wait swarm manager to init
		time.Sleep(200 * time.Millisecond)
		_, err = docker.New("tcp://"+swarmManager.Addr(), "")
		if err == nil {
			break
		}
	}
	if err != nil {
		return
	}
	return sw.Store.Write("manager", swarmManager.Addr(), "cluster/docker/swarm")
}
Example #2
0
func (sw Swarm) Leave(server string) error {
	eng, err := docker.New(server, "")
	if err != nil {
		return err
	}
	containers, err := eng.List()
	if err != nil {
		return err
	}
	var swarmAgent *engine.Container
	for _, container := range containers {
		if container.Name == "swarm_agent" {
			swarmAgent = container
			break
		}
	}
	if swarmAgent != nil {
		if err = eng.Stop(swarmAgent); err != nil {
			return err
		}
		if err = eng.Remove(swarmAgent); err != nil {
			return err
		}
	}
	return nil
}
Example #3
0
func (sw Swarm) Join(server string) error {
	eng, err := docker.New(server, "")
	if err != nil {
		return err
	}
	_, err = sw.agent(server, eng)
	return err
}
Example #4
0
func New(st store.Store) (sw Swarm, err error) {
	addr, err := st.Read("manager", "cluster/docker/swarm")
	if err != nil {
		return
	}
	eng, err := docker.New("tcp://"+addr, "")
	if err != nil {
		return
	}
	sw = Swarm{Store: st, engine: eng}
	return
}
Example #5
0
func (st *Etcd) Initialize(token string, servers []string) error {
	log.Debugln("Installing etcd store ...")

	// Servers and IPs
	ips := []string{}
	for _, server := range servers {
		list := strings.Split(server, "://")
		var ip string
		if list[0] == "unix" {
			ip = LOCALHOST
		} else {
			ip = strings.Split(list[1], ":")[0]
		}
		ips = append(ips, "http://"+ip)
	}
	// Warning if there is not enough server to reach the QUORUM
	if len(ips) < QUORUM {
		log.Infof("Not enough servers to reach the Quorum (%d)", QUORUM)
	}

	// Engines
	engines := []engine.Engine{}
	for _, server := range servers {
		eng, err := docker.New(server, "")
		if err != nil {
			return err
		}
		engines = append(engines, eng)
	}

	// Image
	img := engine.Image{Name: IMAGE}

	// Containers
	localIP := "http://0.0.0.0"
	containers := []*engine.Container{}
	addr := ""
	clusterAddr := ""
	for i, ip := range ips {
		name := "etcd" + strconv.Itoa(i+1)
		cl := strconv.Itoa(4100 + i + 1)
		peer := strconv.Itoa(7100 + i + 1)
		cmd := []string{
			"-name", name,
			"-initial-cluster-token", token,
			"-initial-cluster-state", "new",
			"-listen-client-urls", fmt.Sprintf("%s:%s", localIP, cl),
			"-listen-peer-urls", fmt.Sprintf("%s:%s", localIP, peer),
			"-initial-advertise-peer-urls", fmt.Sprintf("%s:%s", ip, peer),
			"-advertise-client-urls", fmt.Sprintf("%s:%s", ip, cl),
		}
		clusterAddr += fmt.Sprintf("%s=%s:%s", name, ip, peer)
		addr += fmt.Sprintf("%s:%s", ip, cl)
		if i < (len(ips) - 1) {
			addr += ","
			clusterAddr += ","
		}
		container := &engine.Container{
			Name:     name,
			Hostname: name,
			Image:    img,
			Ports: []map[string]string{
				map[string]string{cl: cl},
				map[string]string{peer: peer},
			},
			Cmd: cmd,
		}
		// If ruuning in localhost we have use "host" network mode
		// in order to allow each etcd peer to see each other
		if ip == "http://"+LOCALHOST {
			container.NetworkMode = "host"
		}
		containers = append(containers, container)
	}
	for i, _ := range ips {
		container := containers[i]
		container.Cmd = append(container.Cmd, "-initial-cluster", clusterAddr)
		log.Debugln("Getting img")
		if _, err := engines[i].GetImg(img.Name); err != nil {
			if _, err = engines[i].PullImg(img.Name); err != nil {
				return err
			}
		}
		err := engines[i].Run(container)
		if err != nil {
			return err
		}
	}

	// Connect
	log.Debugln("Connecting to store on:", addr)
	st.New(addr)
	// TODO: check cluster health instead of dirty sleep
	time.Sleep(500 * time.Millisecond)
	fmt.Println("Etcd store running on:", st.Addr())
	st.Write("/projects", "", "")
	return nil
}