Пример #1
0
func getMasterMachine(clusterOpts *options.Options, mcluster *machine.Cluster, td *descriptions.TopologyDescription) (*machine.Machine, error) {
	var m *machine.Machine
	masterName := clusterOpts.String("master-machine-name")
	if masterName != "" {
		exists := false
		m, exists = mcluster.Get(masterName)
		if !exists {
			logrus.Infof("cluster master machine is not exists, crating a new one. name:%s", masterName)
			masterGroup := clusterOpts.String("master-machine-group")
			md := td.GetMachineOptionsBy(masterGroup)
			if md == nil {
				return nil, fmt.Errorf("no machine options found for master '%s'", masterGroup)
			}
			m, err := mcluster.Create(masterName, md.DriverName, md)
			if err != nil {
				return nil, err
			}
			return m, nil
		} else {
			s := m.GetCachedState()
			if !machine.IsRunning(s) {
				logrus.Infof("master machine '%s' is '%s', try to restart.", masterName, s.String())
				if err := m.Start(); err != nil {
					return nil, fmt.Errorf("master machine '%s' exists, but start failed.", masterName)
				}
			}
			return m, nil
		}
	} else {
		logrus.Warnf("master name not specified in container cluster.")
	}
	return m, nil
}
Пример #2
0
// 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
}