Пример #1
0
func (h *Host) Create(name string) error {
	// create the instance
	if err := h.Driver.Create(); err != nil {
		return err
	}

	// save to store
	if err := h.SaveConfig(); err != nil {
		return err
	}

	// TODO: Not really a fan of just checking "none" here.
	if h.Driver.DriverName() != "none" {
		if err := utils.WaitFor(drivers.MachineInState(h.Driver, state.Running)); err != nil {
			return err
		}

		if err := WaitForSSH(h); err != nil {
			return err
		}

		provisioner, err := provision.DetectProvisioner(h.Driver)
		if err != nil {
			return err
		}

		if err := provisioner.Provision(*h.HostOptions.SwarmOptions, *h.HostOptions.AuthOptions, *h.HostOptions.EngineOptions); err != nil {
			return err
		}
	}

	return nil
}
Пример #2
0
func (h *Host) Upgrade() error {
	machineState, err := h.Driver.GetState()
	if err != nil {
		return err
	}

	if machineState != state.Running {
		return errMachineMustBeRunningForUpgrade
	}

	provisioner, err := provision.DetectProvisioner(h.Driver)
	if err != nil {
		return crashreport.CrashError{
			Cause:      err,
			Command:    "Upgrade",
			Context:    "provision.DetectProvisioner",
			DriverName: h.Driver.DriverName(),
		}
	}

	log.Info("Upgrading docker...")
	if err := provisioner.Package("docker", pkgaction.Upgrade); err != nil {
		return crashreport.CrashError{
			Cause:      err,
			Command:    "Upgrade",
			Context:    "provisioner.Package",
			DriverName: h.Driver.DriverName(),
		}
	}

	log.Info("Restarting docker...")
	return provisioner.Service("docker", serviceaction.Restart)
}
Пример #3
0
func (h *Host) WaitForDocker() error {
	provisioner, err := provision.DetectProvisioner(h.Driver)
	if err != nil {
		return err
	}

	return provision.WaitForDocker(provisioner, engine.DefaultPort)
}
Пример #4
0
func (h *Host) Provision() error {
	provisioner, err := provision.DetectProvisioner(h.Driver)
	if err != nil {
		return err
	}

	return provisioner.Provision(*h.HostOptions.SwarmOptions, *h.HostOptions.AuthOptions, *h.HostOptions.EngineOptions)
}
Пример #5
0
// Create is the wrapper method which covers all of the boilerplate around
// actually creating, provisioning, and persisting an instance in the store.
func create(store persist.Store, h *host.Host, callback func(*host.Host)) error {
	if err := cert.BootstrapCertificates(h.HostOptions.AuthOptions); err != nil {
		return fmt.Errorf("Error generating certificates: %s", err)
	}

	log.Info("Running pre-create checks...")

	if err := h.Driver.PreCreateCheck(); err != nil {
		return fmt.Errorf("Error with pre-create check: %s", err)
	}

	if err := store.Save(h); err != nil {
		return fmt.Errorf("Error saving host to store before attempting creation: %s", err)
	}

	log.Info("Creating machine...")

	if err := h.Driver.Create(); err != nil {
		return fmt.Errorf("Error in driver during machine creation: %s", err)
	}

	if err := store.Save(h); err != nil {
		return fmt.Errorf("Error saving host to store after attempting creation: %s", err)
	}

	// TODO: Not really a fan of just checking "none" here.
	if h.Driver.DriverName() != "none" {
		log.Info("Waiting for machine to be running, this may take a few minutes...")
		if err := mcnutils.WaitFor(drivers.MachineInState(h.Driver, state.Running)); err != nil {
			return fmt.Errorf("Error waiting for machine to be running: %s", err)
		}

		log.Info("Machine is running, waiting for SSH to be available...")
		if err := drivers.WaitForSSH(h.Driver); err != nil {
			return fmt.Errorf("Error waiting for SSH: %s", err)
		}

		log.Info("Detecting operating system of created instance...")
		provisioner, err := provision.DetectProvisioner(h.Driver)
		if err != nil {
			return fmt.Errorf("Error detecting OS: %s", err)
		}

		callback(h)

		log.Info("Provisioning created instance...")
		if err := provisioner.Provision(*h.HostOptions.SwarmOptions, *h.HostOptions.AuthOptions, *h.HostOptions.EngineOptions); err != nil {
			return fmt.Errorf("Error running provisioning: %s", err)
		}
	}

	log.Debug("Reticulating splines...")

	return nil
}
Пример #6
0
func (h *Host) ConfigureAuth() error {
	provisioner, err := provision.DetectProvisioner(h.Driver)
	if err != nil {
		return err
	}

	if err := provision.ConfigureAuth(provisioner, *h.HostOptions.AuthOptions); err != nil {
		return err
	}

	return nil
}
Пример #7
0
func (h *Host) ConfigureAuth() error {
	provisioner, err := provision.DetectProvisioner(h.Driver)
	if err != nil {
		return err
	}

	// TODO: This is kind of a hack (or is it?  I'm not really sure until
	// we have more clearly defined outlook on what the responsibilities
	// and modularity of the provisioners should be).
	//
	// Call provision to re-provision the certs properly.
	return provisioner.Provision(swarm.Options{}, *h.HostOptions.AuthOptions, *h.HostOptions.EngineOptions)
}
Пример #8
0
func (h *Host) Upgrade() error {
	provisioner, err := provision.DetectProvisioner(h.Driver)
	if err != nil {
		return err
	}

	if err := provisioner.Package("docker", pkgaction.Upgrade); err != nil {
		return err
	}

	if err := provisioner.Service("docker", pkgaction.Restart); err != nil {
		return err
	}
	return nil
}
Пример #9
0
func (api *Client) performCreate(h *host.Host) error {

	if err := h.Driver.Create(); err != nil {
		return fmt.Errorf("Error in driver during machine creation: %s", err)
	}

	if err := api.Save(h); err != nil {
		return fmt.Errorf("Error saving host to store after attempting creation: %s", err)
	}

	// TODO: Not really a fan of just checking "none" here.
	if h.Driver.DriverName() != "none" {
		log.Info("Waiting for machine to be running, this may take a few minutes...")
		if err := mcnutils.WaitFor(drivers.MachineInState(h.Driver, state.Running)); err != nil {
			return fmt.Errorf("Error waiting for machine to be running: %s", err)
		}

		log.Info("Machine is running, waiting for SSH to be available...")
		if err := drivers.WaitForSSH(h.Driver); err != nil {
			return fmt.Errorf("Error waiting for SSH: %s", err)
		}

		log.Info("Detecting operating system of created instance...")
		provisioner, err := provision.DetectProvisioner(h.Driver)
		if err != nil {
			return fmt.Errorf("Error detecting OS: %s", err)
		}

		log.Infof("Provisioning with %s...", provisioner.String())
		if err := provisioner.Provision(*h.HostOptions.SwarmOptions, *h.HostOptions.AuthOptions, *h.HostOptions.EngineOptions); err != nil {
			return fmt.Errorf("Error running provisioning: %s", err)
		}

		// We should check the connection to docker here
		log.Info("Checking connection to Docker...")
		if _, _, err = check.DefaultConnChecker.Check(h, false); err != nil {
			return fmt.Errorf("Error checking the host: %s", err)
		}

		log.Info("Docker is up and running!")
	}

	return nil

}
Пример #10
0
func (h *Host) Upgrade() error {
	machineState, err := h.Driver.GetState()
	if err != nil {
		return err
	}

	if machineState != state.Running {
		return errMachineMustBeRunningForUpgrade
	}

	provisioner, err := provision.DetectProvisioner(h.Driver)
	if err != nil {
		return err
	}

	log.Info("Upgrading docker...")
	if err := provisioner.Package("docker", pkgaction.Upgrade); err != nil {
		return err
	}

	log.Info("Restarting docker...")
	return provisioner.Service("docker", serviceaction.Restart)
}
Пример #11
0
func (h *Host) Upgrade() error {
	machineState, err := h.Driver.GetState()
	if err != nil {
		return err
	}

	if machineState != state.Running {
		log.Fatal(errMachineMustBeRunningForUpgrade)
	}

	provisioner, err := provision.DetectProvisioner(h.Driver)
	if err != nil {
		return err
	}

	if err := provisioner.Package("docker", pkgaction.Upgrade); err != nil {
		return err
	}

	if err := provisioner.Service("docker", pkgaction.Restart); err != nil {
		return err
	}
	return nil
}
Пример #12
0
func swarmManage(h *host.Host, image string, token string) error {

	provisioner, err := provision.DetectProvisioner(h.Driver)
	dockerDir := provisioner.GetDockerOptionsDir()
	authOptions := setRemoteAuthOptions(provisioner)

	url, err := h.GetURL()
	if err != nil {
		return err
	}

	retryCount := 0

retry:
	exec.Command(os.Args[0], []string{
		"-H", url,
		"--tlscacert=" + h.HostOptions.AuthOptions.CaCertPath,
		"--tlscert=" + h.HostOptions.AuthOptions.ClientCertPath,
		"--tlskey=" + h.HostOptions.AuthOptions.ClientKeyPath,
		"--tlsverify=true",
		"rm", "-f", "swarm-manager"}...).Output()

	cmd := exec.Command(os.Args[0], []string{
		"-H", url,
		"--tlscacert=" + h.HostOptions.AuthOptions.CaCertPath,
		"--tlscert=" + h.HostOptions.AuthOptions.ClientCertPath,
		"--tlskey=" + h.HostOptions.AuthOptions.ClientKeyPath,
		"--tlsverify=true",
		"run", "-d", "--restart=always",
		"--net=bridge",
		"--name", "swarm-manager",
		"-p", "3376:3376",
		"-v", dockerDir + ":" + dockerDir,
		image, "manage",
		"--tlsverify",
		"--tlscacert=" + authOptions.CaCertRemotePath,
		"--tlscert=" + authOptions.ServerCertRemotePath,
		"--tlskey=" + authOptions.ServerKeyRemotePath,
		"-H", "0.0.0.0:3376",
		token}...)

	err = cmd.Run()
	if err == nil {
		fmt.Printf("Manager '%s' started successfully...\n", h.Name)
		return nil
	}

	if _, ok := err.(*exec.ExitError); ok {
		retryCount++
		if retryCount <= 5 {
			fmt.Printf("Failed to start manager. Retry: %d\n", retryCount)
			goto retry
		}
		// if s, ok := exiterr.Sys().(syscall.WaitStatus); ok {
		// 	if s.ExitCode == 8 {
		// 		goto retry
		// 	}
		// }
	}

	return err
}
Пример #13
0
func startZooKeeper(args ...string) error {

	p, err := machine.ReadProvision("provision.yml")
	if err != nil {
		log.Debugf("err: %s", err)
		return err
	}

	// extract pattern
	// fmt.Printf("args: %s\n",args)

	machineList := p.GetMachineList(args...)

	log.Debugf("machines: %s", machineList)

	if len(machineList) == 0 {
		return errors.New("no machine in list")
	}

	store := machine.GetDefaultStore(utils.GetBaseDir())

	zooCfg := `
tickTime=2000
dataDir=/var/zookeeper/
clientPort=2181
initLimit=5
syncLimit=2
{{range $id, $ip := .}}server.{{ inc $id }}={{ $ip }}:2888:3888
{{ end }}
`
	addHosts := []string{}
	hosts := []*host.Host{}
	for _, name := range machineList {
		h, err := loadHost(store, name, utils.GetBaseDir())
		if err != nil {
			return err
		}

		hosts = append(hosts, h)
		ip, err := h.Driver.GetIP()
		if err != nil {
			return err
		}

		addHosts = append(addHosts, "--add-host", name+":"+ip)
	}

	tmpl, err := template.New("zoo").Funcs(template.FuncMap{
		"inc": func(i int) int { return i + 1 },
	}).Parse(zooCfg)

	if err != nil {
		return err
	}
	var str bytes.Buffer
	err = tmpl.Execute(&str, machineList) // change ips to machine name
	if err != nil {
		return err
	}

	for _, h := range hosts {

		provisioner, err := provision.DetectProvisioner(h.Driver)
		// dockerDir := provisioner.GetDockerOptionsDir()
		// authOptions := setRemoteAuthOptions(provisioner)

		url, err := h.GetURL()
		if err != nil {
			return err
		}

		exec.Command(os.Args[0], []string{
			"-H", url,
			"--tlscacert=" + h.HostOptions.AuthOptions.CaCertPath,
			"--tlscert=" + h.HostOptions.AuthOptions.ClientCertPath,
			"--tlskey=" + h.HostOptions.AuthOptions.ClientKeyPath,
			"--tlsverify=true",
			"rm", "-f", "swarm-discovery-service"}...).Output()

		// skip error checking
		provisioner.SSHCommand("sudo mkdir -p /opt/zookeeper/conf")

		// copy zooCfg
		transferCmdFmt := "printf '%%s' '%s' | sudo tee %s"
		if _, err := provisioner.SSHCommand(fmt.Sprintf(transferCmdFmt, str.String(), "/opt/zookeeper/conf/zoo.cfg")); err != nil {
			return err
		}

		cmd := exec.Command(os.Args[0], append([]string{
			"-H", url,
			"--tlscacert=" + h.HostOptions.AuthOptions.CaCertPath,
			"--tlscert=" + h.HostOptions.AuthOptions.ClientCertPath,
			"--tlskey=" + h.HostOptions.AuthOptions.ClientKeyPath,
			"--tlsverify=true",
			"run", "-d", "--restart=always",
			"--name", "swarm-discovery-service",
			"-v", "/opt/zookeeper/conf:/opt/zookeeper/conf"},
			append(addHosts,
				"-p", "2181:2181",
				"-p", "2888:2888",
				"-p", "3888:3888",
				"jplock/zookeeper")...)...)

		output, err := cmd.CombinedOutput()
		if err == nil {
			fmt.Printf("ZK '%s' started successfully...\n", h.Name)
		}
		if err != nil {
			fmt.Print(string(output))
			return err
		}

	}

	return nil
}
Пример #14
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
}