Exemple #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")
}
Exemple #2
0
func (sw Swarm) Run(cont *engine.Container) (err error) {
	// Add image affinity to ensure that image is on same node
	aff := fmt.Sprintf("affinity:image==%s", cont.Image.Name)
	cont.Env = append(cont.Env, aff)
	return sw.engine.Run(cont)
}
Exemple #3
0
func (d Docker) Run(cont *engine.Container) (err error) {
	contPorts := map[dockerclient.Port]struct{}{}
	hostPorts := map[dockerclient.Port][]dockerclient.PortBinding{}
	for _, m := range cont.Ports {
		for k, v := range m {
			var port dockerclient.Port
			port = dockerclient.Port(fmt.Sprintf("%s/tcp", k))
			contPorts[port] = struct{}{}
			hostPorts[port] = []dockerclient.PortBinding{
				dockerclient.PortBinding{HostIP: "", HostPort: v},
			}
		}
	}
	opts := dockerclient.CreateContainerOptions{
		Name: cont.Name,
		Config: &dockerclient.Config{
			Image:        cont.Image.Name,
			Hostname:     cont.Hostname,
			Cmd:          cont.Cmd,
			Tty:          cont.Tty,
			ExposedPorts: contPorts,
			Env:          cont.Env,
		},
	}
	hostConfig := &dockerclient.HostConfig{
		NetworkMode:  cont.NetworkMode,
		PortBindings: hostPorts,
	}
	log.Debugln("Create container:", cont.Name)
	c, err := d.client.CreateContainer(opts)
	if err != nil {
		return
	}
	err = d.client.StartContainer(c.ID, hostConfig)
	if err != nil {
		return
	}
	c, err = d.client.InspectContainer(c.ID)
	if err != nil {
		return
	}
	cont.Id = c.ID

	// Saving network informations
	if c.NetworkSettings == nil {
		return errors.New("Failed to get container network settings")
	}
	cont.Gateway = c.NetworkSettings.Gateway
	for port, bindings := range c.NetworkSettings.Ports {
		contPort := strings.TrimSuffix(string(port), "/tcp")
		for _, elem := range cont.Ports {
			hostPort, prs := elem[contPort]
			if prs {
				if hostPort != bindings[0].HostPort {
					elem[contPort] = bindings[0].HostPort
				}
				break
			}
		}
	}
	// Swarm extras
	if c.Node != nil {
		cont.IP = c.Node.IP
	}
	log.Debugln("Container:", cont)

	log.Infoln("Run:", cont.Name)
	return
}