コード例 #1
0
ファイル: swarm.go プロジェクト: sdgdsffdsfff/dockerf
func (d *Driver) AddWorker(m *machine.Machine) error {
	cd := container.NewContainerDesc()
	cd.SetRestartPolicy("always", 0)
	cd.SetImage(d.options.String("image"))
	ip, err := m.Host.Driver.GetIP()
	if err != nil {
		return err
	}

	cmds := []string{
		"join",
		"--addr", fmt.Sprintf("%s:%d", ip, 2376),
	}
	heartbeat := d.options.String("heartbeat")
	if heartbeat != "" {
		cmds = append(cmds, "--heartbeat", heartbeat)
	}
	cmds = append(cmds, d.options.String("discovery"))
	cd.SetCmd(cmds...)
	client, err := container.NewDockerClient(m)
	if err != nil {
		return err
	}
	name := "swarm-agent-worker"
	c, err := client.GetByName(name)
	if err != nil {
		return err
	}
	id, err := checkContainerStart(name, c, cd, client)
	if err != nil {
		return err
	}
	logrus.Infof("swarm agent added to cluster. ip:%s, name:%s, id:%s", ip, name, id)
	return nil
}
コード例 #2
0
ファイル: swarm.go プロジェクト: sdgdsffdsfff/dockerf
// install swarm master on the 'm' machine
func (d *Driver) AddMaster(m *machine.Machine) error {
	cd := container.NewContainerDesc()
	cd.SetRestartPolicy("always", 0)

	p, err := provision.DetectProvisioner(m.Host.Driver)
	if err != nil {
		return err
	}
	swarmHost := d.options.String("host")
	u, err := url.Parse(swarmHost)
	if err != nil {
		return err
	}
	parts := strings.Split(u.Host, ":")
	port := parts[1]
	bip := parts[0]
	cd.AddPortBinding(bip, port, port, "tcp")

	dockerDir := p.GetDockerOptionsDir()
	b := fmt.Sprintf("%s:%s", dockerDir, dockerDir)
	cd.AddBind(b)

	cd.SetImage(d.options.String("image"))

	authOptions := setRemoteAuthOptions(p)

	cmds := []string{"manage",
		"--tlsverify",
		"--tlscacert", authOptions.CaCertRemotePath,
		"--tlscert", authOptions.ServerCertRemotePath,
		"--tlskey", authOptions.ServerKeyRemotePath,
		"-H", swarmHost,
		"--strategy", d.options.String("strategy"),
	}
	sopts := d.options.StringSlice("opt")
	if len(sopts) > 0 {
		cmds = append(cmds, sopts...)
	}
	cmds = append(cmds, d.options.String("discovery"))

	cd.SetCmd(cmds...)

	client, err := container.NewDockerClient(m)
	if err != nil {
		return err
	}

	name := "swarm-agent-master"
	master, err := client.GetByName(name)
	if err != nil {
		return err
	}
	id, err := checkContainerStart(name, master, cd, client)
	if err != nil {
		return err
	}
	logrus.Infof("swarm master agent is running. name:%s, id:%s", name, id)

	ip := m.GetCachedIp()
	host := fmt.Sprintf("tcp://%s:%s", ip, port)
	mc, err := container.NewDockerClientWithUrl(host, m)
	if err != nil {
		return err
	}
	d.masterClientChangeChan <- mc
	logrus.Infof("swarm master added to cluster. host:%s, name:%s", host, name)
	return nil
}